// Top navigation — sticky, glass on scroll, with language + currency switcher

function LangCurrencySwitcher({ compact = false }) {
  const { lang, setLang, currency, setCurrency } = useI18n();
  const [open, setOpen] = React.useState(false);
  const ref = React.useRef(null);

  React.useEffect(() => {
    function onClick(e) { if (ref.current && !ref.current.contains(e.target)) setOpen(false); }
    document.addEventListener('mousedown', onClick);
    return () => document.removeEventListener('mousedown', onClick);
  }, []);

  const currentLang = LANGS.find(l => l.code === lang) || LANGS[0];

  return (
    <div ref={ref} className="relative">
      <button onClick={() => setOpen(!open)}
        className="flex items-center gap-2 px-3 py-2 rounded-lg text-sm text-white/70 hover:text-white hover:bg-white/5 transition">
        <Icon.Globe className="w-4 h-4"/>
        <span className="font-mono text-xs font-medium tracking-wider">{currentLang.label} · {currency}</span>
        <svg className={`w-3 h-3 text-white/40 transition-transform ${open ? 'rotate-180' : ''}`} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M6 9l6 6 6-6" strokeLinecap="round"/></svg>
      </button>
      {open && (
        <div className="absolute top-full right-0 mt-2 w-64 glass-strong rounded-2xl p-4 z-50" style={{boxShadow:'0 20px 60px -20px rgba(0,0,0,0.6)'}}>
          <div className="text-[10px] font-mono uppercase tracking-widest text-white/40 mb-2 px-2">Language</div>
          <div className="space-y-0.5 mb-3">
            {LANGS.map(l => (
              <button key={l.code} onClick={() => { setLang(l.code); }}
                className={`w-full flex items-center gap-3 px-3 py-2 rounded-lg text-sm transition ${lang === l.code ? 'bg-cyan-300/10 text-cyan-300' : 'text-white/70 hover:bg-white/5 hover:text-white'}`}>
                <span className="font-mono font-semibold w-7">{l.label}</span>
                <span>{l.name}</span>
                {lang === l.code && <Icon.Check className="w-4 h-4 ml-auto"/>}
              </button>
            ))}
          </div>
          <div className="text-[10px] font-mono uppercase tracking-widest text-white/40 mb-2 px-2 pt-2 border-t border-white/10">Currency</div>
          <div className="space-y-0.5">
            {Object.values(CURRENCIES).map(c => (
              <button key={c.code} onClick={() => setCurrency(c.code)}
                className={`w-full flex items-center gap-3 px-3 py-2 rounded-lg text-sm transition ${currency === c.code ? 'bg-violet-400/10 text-violet-300' : 'text-white/70 hover:bg-white/5 hover:text-white'}`}>
                <span className="font-mono font-semibold w-7">{c.symbol}</span>
                <span>{c.name}</span>
                {currency === c.code && <Icon.Check className="w-4 h-4 ml-auto"/>}
              </button>
            ))}
          </div>
        </div>
      )}
    </div>
  );
}

function Nav() {
  const { t } = useI18n();
  const [scrolled, setScrolled] = React.useState(false);
  const [open, setOpen] = React.useState(false);

  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const links = [
    { href: '#productos', key: 'nav.products' },
    { href: '#verticales', key: 'nav.verticals' },
    { href: '#casos', key: 'nav.cases' },
    { href: '#demo', key: 'nav.demo' },
    { href: '#precios', key: 'nav.pricing' },
    { href: '#faq', key: 'nav.faq' },
  ];

  return (
    <header className={`fixed top-0 left-0 right-0 z-40 transition-all duration-500 ${scrolled ? 'py-3' : 'py-5'}`}>
      <div className="max-w-7xl mx-auto px-4 sm:px-6">
        <div className={`flex items-center justify-between rounded-2xl px-5 transition-all duration-500 ${scrolled ? 'glass-strong py-2.5' : 'py-2'}`}>
          <a href="#" className="flex items-center"><SinapsioLogo /></a>
          <nav className="hidden lg:flex items-center gap-1">
            {links.map(l => (
              <a key={l.href} href={l.href} className="px-3.5 py-2 text-sm text-white/70 hover:text-white transition rounded-lg hover:bg-white/5 whitespace-nowrap">{t(l.key)}</a>
            ))}
          </nav>
          <div className="flex items-center gap-2">
            <div className="hidden md:block">
              <LangCurrencySwitcher/>
            </div>
            <a href={waLink()} target="_blank" rel="noopener" className="hidden md:inline-flex items-center gap-2 btn-primary text-sm whitespace-nowrap" style={{padding:'10px 20px'}}>
              <svg viewBox="0 0 24 24" className="w-4 h-4" fill="currentColor"><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>
              <span>{t('nav.cta')}</span>
            </a>
            <button onClick={() => setOpen(!open)} className="lg:hidden p-2 text-white">
              <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d={open ? "M6 18L18 6M6 6l12 12" : "M4 6h16M4 12h16M4 18h16"}/></svg>
            </button>
          </div>
        </div>
        {open && (
          <div className="lg:hidden mt-2 glass-strong rounded-2xl p-4 space-y-1">
            {links.map(l => (
              <a key={l.href} href={l.href} onClick={() => setOpen(false)} className="block px-3 py-2.5 text-sm text-white/80 hover:text-white hover:bg-white/5 rounded-lg">{t(l.key)}</a>
            ))}
            <div className="pt-3 border-t border-white/10 mt-3">
              <LangCurrencySwitcher/>
            </div>
          </div>
        )}
      </div>
    </header>
  );
}

window.Nav = Nav;
