/* ALEROQ wordmark — fixed top nav with blur on scroll.
   The mark PNG is a solid-white glyph, so on a light backdrop it must be
   inverted to black. `onDark` says whether the logo sits on a dark backdrop
   (dark theme, or the transparent nav over the dark hero). When omitted it
   follows the page theme — which is what the Footer wants. */
function AleroqLogo({ size = 22, onDark }) {
  const theme = window.useTheme();
  const dark = onDark === undefined ? theme === 'dark' : onDark;
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
      <img src="aleroq-mark.png" alt="ALEROQ" data-logo-mark="true"
        style={{ height: size * 0.82, width: 'auto', display: 'block', flexShrink: 0,
          filter: dark ? 'none' : 'brightness(0)',
          transition: 'filter var(--dur-base) var(--ease-out)' }} />
      <span data-logo-wordmark="true" style={{
        fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: size,
        letterSpacing: '0.12em', color: dark ? '#ffffff' : 'var(--text-strong)',
        textTransform: 'uppercase',
        transition: 'color var(--dur-base) var(--ease-out)',
      }}>
        ALEROQ
      </span>
    </div>
  );
}

function Nav() {
  const { Button } = window.RevvoDesignSystem_a89086;
  const Ic = window.Ic;
  const [scrolled, setScrolled] = React.useState(false);
  const [theme, setTheme] = React.useState(
    () => document.documentElement.getAttribute('data-theme') === 'light' ? 'light' : 'dark'
  );

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

  const toggleTheme = () => {
    const next = theme === 'dark' ? 'light' : 'dark';
    setTheme(next);
    localStorage.setItem('aleroq-theme', next);
    if (next === 'light') {
      document.documentElement.setAttribute('data-theme', 'light');
    } else {
      document.documentElement.removeAttribute('data-theme');
    }
    window.dispatchEvent(new Event('aleroq-themechange'));
  };

  const isLight = theme === 'light';
  const navBg = scrolled
    ? (isLight ? 'rgba(243,243,247,0.88)' : 'rgba(10,10,10,0.72)')
    : 'transparent';

  // Nav content follows the theme: the hero (top, transparent nav) and the
  // scrolled surface both match the active theme now, so light text on dark
  // theme and dark text on light theme is correct throughout.
  const onDark = theme === 'dark';

  const link = {
    fontSize: 14, color: onDark ? 'rgba(255,255,255,0.82)' : 'var(--text-body)',
    textDecoration: 'none', fontWeight: 500,
    transition: 'color var(--dur-base) var(--ease-out)',
  };
  const ctlColor = onDark ? 'rgba(255,255,255,0.72)' : 'var(--text-muted)';
  const ctlBorder = onDark ? 'rgba(255,255,255,0.22)' : 'var(--border)';

  return (
    <nav style={{
      position: 'fixed', top: 0, left: 0, right: 0, zIndex: 50,
      padding: '16px clamp(20px, 5vw, 48px)',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      background: navBg,
      backdropFilter: scrolled ? 'blur(14px)' : 'none',
      borderBottom: `1px solid ${scrolled ? 'var(--line-100)' : 'transparent'}`,
      transition: 'background var(--dur-base) var(--ease-out), border-color var(--dur-base) var(--ease-out)',
    }}>
      <AleroqLogo onDark={onDark} />
      <div style={{ display: 'flex', alignItems: 'center', gap: 28 }} className="nav-links">
        <a href="#how" style={link} onClick={(e) => { e.preventDefault(); window.aleroqScrollTo('how'); }}>How it works</a>
        <a href="#features" style={link} onClick={(e) => { e.preventDefault(); window.aleroqScrollTo('features'); }}>Features</a>
        <a href="#compare" style={link} onClick={(e) => { e.preventDefault(); window.aleroqScrollTo('compare'); }}>Compare</a>
        <a href="#faq" style={link} onClick={(e) => { e.preventDefault(); window.aleroqScrollTo('faq'); }}>FAQ</a>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
        <button onClick={toggleTheme} title={isLight ? 'Switch to dark mode' : 'Switch to light mode'} style={{
          background: 'transparent', border: `1px solid ${ctlBorder}`, borderRadius: 'var(--radius-md)',
          width: 34, height: 34, display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          cursor: 'pointer', color: ctlColor, flexShrink: 0,
          transition: 'border-color var(--dur-base) var(--ease-out), color var(--dur-base) var(--ease-out)',
        }}>
          <Ic name={isLight ? 'Moon' : 'Sun'} size={15} color={ctlColor} />
        </button>
        <a href="#waitlist" className="nav-signin" style={{ ...link, fontWeight: 600 }} onClick={(e) => { e.preventDefault(); window.aleroqScrollTo('waitlist'); }}>Sign in</a>
        <Button variant="primary" size="sm" className="pulse-glow" onClick={() => window.aleroqScrollTo('waitlist')}>Request beta access</Button>
      </div>
    </nav>
  );
}

window.Nav = Nav;
window.AleroqLogo = AleroqLogo;
