// shared.jsx — Header, Footer, Placeholder, Ornament, Lightbox

const { useState, useEffect, useRef, useMemo } = React;

const WA_NUMBER = '15148677519';

function WaIcon({ size = 20, color = '#25D366' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={color} style={{ flexShrink: 0 }}>
      <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/>
    </svg>
  );
}

// ---------- Placeholder with mono label (now supports real images) ----------
function Placeholder({ label, ratio = '4/5', tone = 'default', corner, style = {}, className = '', children, src, objectPosition = 'center', contain = false, blend = false }) {
  const base = { width: '100%', position: 'relative' };
  if (ratio && ratio !== 'unset') base.aspectRatio = ratio;

  if (src) {
    return (
      <div
        className={`ph-img ${className}`}
        style={{
          ...base,
          overflow: 'hidden',
          background: blend ? 'var(--parchment)' : contain ? 'var(--bone)' : 'var(--saddle-900)',
          border: blend ? 'none' : '1px solid rgba(107, 63, 29, 0.2)',
          ...style,
        }}
      >
        <img
          src={src}
          alt={label}
          loading="lazy"
          decoding="async"
          style={{
            width: '100%', height: '100%',
            objectFit: contain ? 'contain' : 'cover',
            objectPosition,
            display: 'block',
            mixBlendMode: blend ? 'multiply' : 'normal',
          }}
        />
        {corner && (
          <div style={{
            position: 'absolute', top: 10, left: 10,
            fontFamily: 'var(--font-mono)', fontSize: 9, letterSpacing: '0.2em',
            color: 'rgba(245,237,224,0.9)', textTransform: 'uppercase',
            padding: '4px 8px', background: 'rgba(27,18,10,0.5)',
            backdropFilter: 'blur(4px)',
          }}>{corner}</div>
        )}
        {children}
      </div>
    );
  }

  return (
    <div
      className={`ph ${tone === 'dark' ? 'dark' : tone === 'gold' ? 'gold' : ''} ${className}`}
      style={{ ...base, ...style }}
    >
      {corner && <div className="ph-corner">{corner}</div>}
      <div className="ph-label">[ {label} ]</div>
      {children}
    </div>
  );
}

// ---------- Logo mark ----------
function Logo({ height = 52, invert = false }) {
  const [broken, setBroken] = React.useState(false);
  if (broken) {
    return (
      <span style={{
        fontFamily: 'var(--font-display)',
        fontStyle: 'italic',
        fontWeight: 500,
        fontSize: Math.round(height * 0.55) + 'px',
        letterSpacing: '0.04em',
        color: invert ? 'var(--cream)' : 'var(--ink)',
        lineHeight: 1,
        whiteSpace: 'nowrap',
      }}>
        SensStyle
      </span>
    );
  }
  return (
    <img
      src="/uploads/SS SensStyle.gif"
      alt="SensStyle"
      onError={() => setBroken(true)}
      style={{
        height, width: 'auto',
        filter: invert ? 'brightness(1.1) saturate(0.95)' : 'none',
      }}
    />
  );
}

// ---------- Ornament ----------
function Ornament({ color }) {
  return (
    <div className="ornament" style={color ? { color } : undefined}>
      <span className="line" />
      <span className="dot" />
      <span className="line" />
    </div>
  );
}

// ---------- CaptchaField (invisible: honeypot + signed timestamp token) ----------

function CaptchaField({ onCaptcha }) {
  const [token, setToken] = useState('');

  useEffect(() => {
    fetch(API_BASE + '/api/public/captcha').then(r => r.json()).then(data => {
      setToken(data.token);
      if (onCaptcha) onCaptcha(data.token);
    }).catch(() => {});
  }, []);

  return (
    <div aria-hidden="true" style={{ position: 'absolute', left: '-9999px', top: '-9999px', opacity: 0, height: 0, width: 0, overflow: 'hidden' }}>
      <input name="website" autoComplete="off" tabIndex={-1} defaultValue="" />
      <input name="url" autoComplete="off" tabIndex={-1} defaultValue="" />
      <input name="_hp" autoComplete="off" tabIndex={-1} defaultValue="" />
      <input name="captchaToken" defaultValue={token} />
    </div>
  );
}

// ---------- Header ----------
function Header({ lang, setLang, route, navigate, onDark = false }) {
  const t = I18N[lang];
  const [collOpen, setCollOpen] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);
  const collRef = useRef(null);
  const langs = ['es', 'en', 'fr'];

  const isCollectionActive = ['sombreros','hebillas','correas','product'].includes(route.page);

  // Close dropdown on outside click
  useEffect(() => {
    const handler = (e) => {
      if (collRef.current && !collRef.current.contains(e.target)) setCollOpen(false);
    };
    document.addEventListener('mousedown', handler);
    return () => document.removeEventListener('mousedown', handler);
  }, []);

  // Close mobile menu on route change
  useEffect(() => { setMobileOpen(false); }, [route]);

  // Lock body scroll when mobile menu open
  useEffect(() => {
    document.body.style.overflow = mobileOpen ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [mobileOpen]);

  const collSubItems = [
    { id: 'sombreros', label: t.nav.sombreros, icon: '✦' },
    { id: 'hebillas',  label: t.nav.hebillas,  icon: '◈' },
    { id: 'correas',   label: t.nav.correas,   icon: '◇' },
  ];

  const leftPages = [
    { id: 'home',         label: t.nav.home },
    { id: 'distributors', label: t.nav.distributors },
  ];
  const rightPages = [
    { id: 'about',   label: t.nav.about },
    { id: 'gallery', label: t.nav.gallery },
    { id: 'blog',    label: t.nav.blog },
    { id: 'contact', label: t.nav.contact },
  ];

  const allPages = [...leftPages, ...rightPages];

  return (
    <>
      <div className="site-top-bar">
      <div className="announce">
        <div className="announce-track">
          {[...Array(2)].map((_, i) =>
            t.announce.map((m, j) => (
              <span key={`${i}-${j}`}>
                <span className="mark">✦</span> {m}
              </span>
            ))
          )}
        </div>
      </div>
      <header className={`site-header ${onDark ? 'on-dark' : ''}`}>
        <div className="container header-top">
          {/* Hamburger — mobile only */}
          <button
            className="hamburger-btn"
            onClick={() => setMobileOpen(v => !v)}
            aria-label="Menu"
            aria-expanded={mobileOpen}
            aria-controls="mobile-drawer"
          >
            <span className={`hamburger-icon ${mobileOpen ? 'open' : ''}`}>
              <span /><span /><span />
            </span>
          </button>

          {/* Left nav — desktop only */}
          <nav className="header-left nav-desktop">
            {leftPages.map(p => (
              <a key={p.id}
                className={`nav-link ${route.page === p.id ? 'active' : ''}`}
                href={`#${p.id}`}
                aria-current={route.page === p.id ? 'page' : undefined}
                onClick={(e) => { e.preventDefault(); navigate({ page: p.id }); }}
              >{p.label}</a>
            ))}

            {/* Collection dropdown */}
            <div className="nav-dropdown" ref={collRef}>
              <button
                className={`nav-link nav-dropdown-trigger ${isCollectionActive ? 'active' : ''}`}
                onClick={() => setCollOpen(v => !v)}
                onMouseEnter={() => setCollOpen(true)}
                aria-expanded={collOpen}
                aria-haspopup="true"
              >
                {t.nav.collection}
                <svg width="10" height="6" viewBox="0 0 10 6" style={{ marginLeft: 5, transition: 'transform 200ms', transform: collOpen ? 'rotate(180deg)' : 'none' }}>
                  <path d="M1 1l4 4 4-4" stroke="currentColor" strokeWidth="1.5" fill="none" strokeLinecap="round"/>
                </svg>
              </button>
              {collOpen && (
                <div className="nav-dropdown-menu" onMouseLeave={() => setCollOpen(false)}>
                  {collSubItems.map(item => (
                    <a key={item.id}
                      className={`nav-dropdown-item ${route.page === item.id ? 'active' : ''}`}
                      href={`#${item.id}`}
                      onClick={(e) => { e.preventDefault(); setCollOpen(false); navigate({ page: item.id }); }}
                    >
                      <span className="dropdown-icon">{item.icon}</span>
                      {item.label}
                    </a>
                  ))}
                </div>
              )}
            </div>
          </nav>

          {/* Logo center */}
          <a className="header-logo" href="#home" aria-label="SensStyle Home" onClick={(e) => { e.preventDefault(); navigate({ page: 'home' }); }}>
            <Logo height={52} invert={onDark} />
          </a>

          {/* Right nav — desktop only */}
          <div className="header-right">
            <nav className="nav-desktop" style={{ display: 'flex', alignItems: 'center', gap: 28 }}>
              {rightPages.map(p => (
                <a key={p.id}
                  className={`nav-link ${route.page === p.id ? 'active' : ''}`}
                  href={`#${p.id}`}
                  aria-current={route.page === p.id ? 'page' : undefined}
                  onClick={(e) => { e.preventDefault(); navigate({ page: p.id }); }}
                >{p.label}</a>
              ))}
            </nav>
            <div className="header-right-utils">
              <div className="lang-picker">
                {langs.map((l, i) => (
                  <React.Fragment key={l}>
                    <button className={`lang-btn ${lang === l ? 'active' : ''}`} onClick={() => setLang(l)}>
                      {l.toUpperCase()}
                    </button>
                    {i < langs.length - 1 && <span className="lang-sep" />}
                  </React.Fragment>
                ))}
              </div>
              <a className="wholesale-btn nav-desktop" href="/portal" style={{ textDecoration: 'none' }}>
                {t.nav.wholesale}
              </a>
            </div>
          </div>
        </div>
      </header>
      </div>

      {/* MOBILE DRAWER OVERLAY */}
      {mobileOpen && (
        <div className="mobile-overlay" onClick={() => setMobileOpen(false)} role="presentation" />
      )}
      <nav className={`mobile-drawer ${mobileOpen ? 'open' : ''}`} id="mobile-drawer" role="dialog" aria-label="Navigation menu" aria-modal={mobileOpen}>
        <div className="mobile-drawer-head">
          <Logo height={40} />
          <button onClick={() => setMobileOpen(false)} className="mobile-close-btn" aria-label="Close">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
              <line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
            </svg>
          </button>
        </div>

        <div className="mobile-drawer-links">
          {allPages.map(p => (
            <a key={p.id}
              className={`mobile-link ${route.page === p.id ? 'active' : ''}`}
              href={`#${p.id}`}
              onClick={(e) => { e.preventDefault(); setMobileOpen(false); navigate({ page: p.id }); }}
            >{p.label}</a>
          ))}

          {/* Collection sub-items */}
          <div className="mobile-section-label">{t.nav.collection}</div>
          {collSubItems.map(item => (
            <a key={item.id}
              className={`mobile-link sub ${route.page === item.id ? 'active' : ''}`}
              href={`#${item.id}`}
              onClick={(e) => { e.preventDefault(); setMobileOpen(false); navigate({ page: item.id }); }}
            >
              <span className="dropdown-icon">{item.icon}</span>
              {item.label}
            </a>
          ))}
        </div>

        <div className="mobile-drawer-footer">
          <a className="btn btn-gold" href="/portal" style={{ width: '100%', justifyContent: 'center', textDecoration: 'none' }}>
            {t.nav.wholesale} <span className="arrow"/>
          </a>
          <div className="lang-picker" style={{ justifyContent: 'center', marginTop: 16 }}>
            {langs.map((l, i) => (
              <React.Fragment key={l}>
                <button className={`lang-btn ${lang === l ? 'active' : ''}`} onClick={() => setLang(l)}>
                  {l.toUpperCase()}
                </button>
                {i < langs.length - 1 && <span className="lang-sep" />}
              </React.Fragment>
            ))}
          </div>
        </div>
      </nav>
    </>
  );
}

// ---------- Footer Newsletter (with invisible captcha) ----------

function FooterNewsletter({ lang, f }) {
  const [captchaToken, setCaptchaToken] = useState('');

  useEffect(() => {
    fetch(API_BASE + '/api/public/captcha').then(r => r.json()).then(d => setCaptchaToken(d.token)).catch(() => {});
  }, []);

  return (
    <form onSubmit={async (e) => {
      e.preventDefault();
      const email = e.target.elements.email.value;
      const hp = e.target.elements.website?.value;
      if (hp) return;
      if (!email) return;
      try {
        const res = await fetch(API_BASE + '/api/public/newsletter', {
          method: 'POST', headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ email, source: 'footer', captchaToken }),
        });
        const data = await res.json();
        if (data.ok) {
          e.target.elements.email.value = '';
          e.target.elements.email.placeholder = '✦ ' + (lang === 'es' ? '¡Suscrito!' : lang === 'fr' ? 'Abonné!' : 'Subscribed!');
          setTimeout(() => { e.target.elements.email.placeholder = f.newsletterPh; }, 3000);
        }
      } catch {}
    }}
      style={{ display: 'flex', gap: 0, maxWidth: 380, borderBottom: '1px solid rgba(245,237,224,0.25)', paddingBottom: 10 }}>
      <input name="website" autoComplete="off" tabIndex={-1} aria-hidden="true" style={{ position: 'absolute', opacity: 0, height: 0, width: 0, pointerEvents: 'none' }} />
      <input
        name="email"
        type="email"
        placeholder={f.newsletterPh}
        style={{
          flex: 1, background: 'transparent', border: 0, color: 'var(--cream)',
          fontSize: 16, padding: '6px 0', outline: 'none',
        }}
      />
      <button type="submit" style={{
        fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: '0.22em',
        textTransform: 'uppercase', color: 'var(--gold-300)', padding: '6px 0',
      }}>
        {f.subscribe} →
      </button>
    </form>
  );
}

// ---------- Footer ----------
function Footer({ lang, navigate }) {
  const t = I18N[lang];
  const f = t.footer;
  return (
    <footer className="site-footer" role="contentinfo">
      <div className="container">
        <div className="footer-grid">
          <div className="footer-col footer-brand">
            <Logo height={56} invert />
            <p className="lead">{f.tagline}</p>
            <FooterNewsletter lang={lang} f={f} />
          </div>
          <div className="footer-col">
            <h4>{f.shop}</h4>
            <ul>
              <li><a href="#sombreros" onClick={(e) => { e.preventDefault(); navigate({ page: 'sombreros' }); }}>{t.nav.sombreros}</a></li>
              <li><a href="#hebillas" onClick={(e) => { e.preventDefault(); navigate({ page: 'hebillas' }); }}>{t.nav.hebillas}</a></li>
              <li><a href="#correas" onClick={(e) => { e.preventDefault(); navigate({ page: 'correas' }); }}>{t.nav.correas}</a></li>
              {Object.values(HAT_STYLES).map(s => (
                <li key={s.id}>
                  <a href={`#${s.id}`} onClick={(e) => { e.preventDefault(); navigate({ page: 'sombreros', filter: s.id }); }}>
                    {s.name[lang]}
                  </a>
                </li>
              ))}
            </ul>
          </div>
          <div className="footer-col">
            <h4>{f.company}</h4>
            <ul>
              <li><a href="#about" onClick={(e) => { e.preventDefault(); navigate({ page: 'about' }); }}>{t.nav.about}</a></li>
              <li><a href="#distributors" onClick={(e) => { e.preventDefault(); navigate({ page: 'distributors' }); }}>{t.nav.distributors}</a></li>
              <li><a href="#gallery" onClick={(e) => { e.preventDefault(); navigate({ page: 'gallery' }); }}>{t.nav.gallery}</a></li>
              <li><a href="/portal">{t.nav.wholesale}</a></li>
            </ul>
          </div>
          <div className="footer-col">
            <h4>{f.contact}</h4>
            <ul>
              <li><a href="mailto:info@sensstyle.com">info@sensstyle.com</a></li>
              <li><a href="tel:+15148677519">+1 514 867 7519</a></li>
              <li style={{ color: 'rgba(245,237,224,0.7)' }}>5420 Boulevard Lévis<br/>Saint-Léonard, QC · Canada</li>
            </ul>
          </div>
        </div>
        <div className="footer-meta">
          <span>{f.rights}</span>
          <span style={{ display: 'flex', gap: 24 }}>
            {f.legal.map((l, i) => {
              const pages = ['privacy', 'terms', 'cookies'];
              return <a key={l} href={`#${pages[i]}`} onClick={(e) => { e.preventDefault(); navigate({ page: pages[i] }); }}>{l}</a>;
            })}
          </span>
        </div>
      </div>
    </footer>
  );
}

// ---------- Speed-Dial FAB ----------
function WhatsappFloat({ lang, navigate }) {
  const t = I18N[lang];
  const [open, setOpen] = React.useState(false);
  const fabRef = React.useRef(null);

  // Close on outside click
  React.useEffect(() => {
    const handler = (e) => {
      if (fabRef.current && !fabRef.current.contains(e.target)) setOpen(false);
    };
    document.addEventListener('mousedown', handler);
    return () => document.removeEventListener('mousedown', handler);
  }, []);

  const actions = [
    {
      id: 'whatsapp',
      label: 'WhatsApp',
      icon: (
        <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
          <path d="M17.5 14.4c-.3-.1-1.7-.8-2-.9-.3-.1-.5-.1-.7.1-.2.3-.8.9-.9 1.1-.2.2-.3.2-.6.1-.3-.1-1.2-.5-2.3-1.4-.9-.8-1.4-1.7-1.6-2-.2-.3 0-.5.1-.6.1-.1.3-.3.4-.5.1-.2.2-.3.3-.5.1-.2 0-.4 0-.5 0-.1-.7-1.7-1-2.3-.3-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.8.4-.3.3-1 1-1 2.4 0 1.4 1 2.8 1.2 3 .1.2 2 3.1 4.9 4.4.7.3 1.2.5 1.6.6.7.2 1.3.2 1.8.1.5-.1 1.7-.7 1.9-1.4.2-.7.2-1.2.2-1.4-.1-.1-.3-.2-.6-.4zM12 2C6.5 2 2 6.5 2 12c0 1.9.5 3.7 1.4 5.2L2 22l4.9-1.3c1.5.8 3.3 1.3 5.1 1.3 5.5 0 10-4.5 10-10S17.5 2 12 2z"/>
        </svg>
      ),
      color: '#25D366',
      action: () => { window.open(`https://wa.me/${WA_NUMBER}`, '_blank'); setOpen(false); },
    },
    {
      id: 'catalog',
      label: lang === 'es' ? 'Catálogo' : lang === 'fr' ? 'Catalogue' : 'Catalog',
      icon: (
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
          <rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/>
          <rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/>
        </svg>
      ),
      color: 'var(--gold-600)',
      action: () => { navigate && navigate({ page: 'sombreros' }); setOpen(false); },
    },
    {
      id: 'contact',
      label: lang === 'es' ? 'Contacto' : lang === 'fr' ? 'Contact' : 'Contact',
      icon: (
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
          <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
        </svg>
      ),
      color: 'var(--saddle-500)',
      action: () => { navigate && navigate({ page: 'contact' }); setOpen(false); },
    },
  ];

  return (
    <div ref={fabRef} style={{ position: 'fixed', right: 24, bottom: 24, zIndex: 50, display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 12 }}>
      {/* Speed-dial menu */}
      {open && (
        <div style={{
          display: 'flex', flexDirection: 'column', gap: 10, alignItems: 'flex-end',
          animation: 'fabMenuIn 220ms var(--ease)',
        }}>
          {actions.map((act, i) => (
            <div key={act.id} style={{ display: 'flex', alignItems: 'center', gap: 12,
              animation: `fabItemIn 200ms var(--ease) ${i * 40}ms both`,
            }}>
              {/* Label chip */}
              <div style={{
                background: 'var(--ink)', color: 'var(--cream)',
                fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: '0.2em', textTransform: 'uppercase',
                padding: '6px 12px',
                boxShadow: '0 4px 16px rgba(0,0,0,0.25)',
                whiteSpace: 'nowrap',
              }}>
                {act.label}
              </div>
              {/* Action button */}
              <button
                onClick={act.action}
                title={act.label}
                style={{
                  width: 48, height: 48, borderRadius: 0,
                  background: act.color, color: 'white',
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  boxShadow: '0 8px 24px rgba(0,0,0,0.25)',
                  transition: 'transform 200ms var(--ease), box-shadow 200ms var(--ease)',
                  flexShrink: 0,
                }}
                onMouseEnter={e => { e.currentTarget.style.transform = 'scale(1.1)'; e.currentTarget.style.boxShadow = '0 12px 32px rgba(0,0,0,0.35)'; }}
                onMouseLeave={e => { e.currentTarget.style.transform = 'scale(1)'; e.currentTarget.style.boxShadow = '0 8px 24px rgba(0,0,0,0.25)'; }}
              >
                {act.icon}
              </button>
            </div>
          ))}
        </div>
      )}

      {/* Main FAB trigger */}
      <button
        onClick={() => setOpen(v => !v)}
        title={open ? 'Cerrar' : 'Contacto rápido'}
        style={{
          width: 60, height: 60, borderRadius: 0,
          background: open ? 'var(--ink)' : 'var(--gold-600)',
          color: open ? 'var(--gold-300)' : 'var(--cream)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          boxShadow: open ? '0 8px 32px rgba(0,0,0,0.4)' : '0 8px 32px rgba(169,123,44,0.45)',
          transition: 'all 280ms var(--ease)',
          border: open ? '1px solid var(--gold-600)' : '1px solid transparent',
        }}
        onMouseEnter={e => { if (!open) { e.currentTarget.style.background = 'var(--gold-700)'; e.currentTarget.style.transform = 'scale(1.06)'; } }}
        onMouseLeave={e => { if (!open) { e.currentTarget.style.background = 'var(--gold-600)'; e.currentTarget.style.transform = 'scale(1)'; } }}
      >
        {open ? (
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
            <line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
          </svg>
        ) : (
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
            <circle cx="12" cy="5" r="1.5" fill="currentColor" stroke="none"/>
            <circle cx="12" cy="12" r="1.5" fill="currentColor" stroke="none"/>
            <circle cx="12" cy="19" r="1.5" fill="currentColor" stroke="none"/>
          </svg>
        )}
      </button>
    </div>
  );
}

// ---------- Lightbox ----------
function Lightbox({ images, index, onClose, onNav }) {
  const [zoomed, setZoomed] = React.useState(false);

  useEffect(() => {
    setZoomed(false);
  }, [index]);

  useEffect(() => {
    const onKey = (e) => {
      if (e.key === 'Escape') { if (zoomed) setZoomed(false); else onClose(); }
      if (e.key === 'ArrowRight' && !zoomed) onNav(1);
      if (e.key === 'ArrowLeft' && !zoomed) onNav(-1);
    };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [onClose, onNav, zoomed]);

  const src = images[index];
  return (
    <div className="lightbox" onClick={() => { if (zoomed) setZoomed(false); else onClose(); }} role="dialog" aria-modal="true" aria-label="Image viewer">
      <div className="lightbox-stage" onClick={(e) => e.stopPropagation()}>
        <button className="lightbox-close" onClick={onClose} aria-label="Close image viewer">✕ Cerrar</button>
        {images.length > 1 && (
          <>
            <button className="lightbox-nav prev" onClick={() => { setZoomed(false); onNav(-1); }} aria-label="Previous image">‹</button>
            <button className="lightbox-nav next" onClick={() => { setZoomed(false); onNav(1); }} aria-label="Next image">›</button>
          </>
        )}
        {src ? (
          <img
            className={`lb-img${zoomed ? ' zoomed' : ''}`}
            src={src}
            alt={`Imagen ${index + 1}`}
            onClick={() => setZoomed(v => !v)}
          />
        ) : (
          <div style={{ color: 'rgba(245,237,224,0.4)', fontFamily: 'var(--font-mono)', fontSize: 13, letterSpacing: '0.2em' }}>
            SIN IMAGEN
          </div>
        )}
        <div className="lightbox-counter">{index + 1} / {images.length}</div>
      </div>
    </div>
  );
}

Object.assign(window, { Placeholder, Logo, Ornament, Header, Footer, WhatsappFloat, Lightbox, WaIcon, WA_NUMBER });
