// Sticky nav with frosted glass; adapts to dark sections
function Nav() {
  const [scrolled, setScrolled] = React.useState(false);
  const [onDark, setOnDark] = React.useState(false);
  const [time, setTime] = React.useState('');

  React.useEffect(() => {
    const t = () => {
      const d = new Date();
      setTime(d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false }));
    };
    t();
    const id = setInterval(t, 30000);
    return () => clearInterval(id);
  }, []);

  React.useEffect(() => {
    const onScroll = () => {
      setScrolled(window.scrollY > 40);
      // Detect if nav overlaps a dark section
      const darkEls = document.querySelectorAll('[data-nav-dark]');
      let dark = false;
      darkEls.forEach(el => {
        const r = el.getBoundingClientRect();
        if (r.top <= 40 && r.bottom >= 68) dark = true;
      });
      setOnDark(dark);
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const links = [
    { label: 'Products', href: '#products', n: '01' },
    { label: 'How',      href: '#how',      n: '02' },
    { label: 'Owners',   href: '#owners',   n: '03' },
    { label: 'Record',   href: '#record',   n: '04' },
  ];

  const smoothScrollTo = (e) => {
    const href = e.currentTarget.getAttribute('href');
    if (!href || !href.startsWith('#') || href === '#') return;
    const target = document.getElementById(href.slice(1));
    if (!target) return;
    e.preventDefault();
    const navH = window.scrollY > 40 ? 60 : 72;
    const top = target.getBoundingClientRect().top + window.scrollY - navH + 1;
    window.scrollTo({ top, behavior: 'smooth' });
    if (history.pushState) history.pushState(null, '', href);
  };

  return (
    <>
      <div className="scroll-rail" id="scrollRail" />
      <header className={`nav-frost ${onDark ? 'on-dark' : ''}`}>
        <div style={{ height: scrolled ? 60 : 72, transition: 'height 0.4s var(--ease-out)' }}>
          <div className="max-w-[1680px] mx-auto px-6 md:px-10 w-full h-full flex items-center justify-between">
            <a href="#hero" onClick={smoothScrollTo} className="flex items-center gap-2.5 nav-brand" style={{ color: onDark ? 'var(--parchment)' : 'var(--abyss)' }}>
              <MountainMark size={26} />
              <span className="font-display font-bold tracking-tight" style={{ fontSize: '1.02rem' }}>
                Alp<span style={{ color: onDark ? 'var(--alpine-300)' : 'var(--alpine-500)' }}>·</span>Web Studio
              </span>
            </a>
            <nav className="hidden lg:flex items-center gap-0.5 nav-pill rounded-full px-2 py-1.5">
              {links.map(l => (
                <a key={l.label} href={l.href} onClick={smoothScrollTo} className="nav-link group flex items-center gap-2 px-4 py-1.5 rounded-full font-body font-medium transition-colors"
                   style={{ fontSize: '.82rem', color: onDark ? 'rgba(245,240,235,0.8)' : 'rgba(26,36,54,0.75)' }}>
                  <span className="font-mono uppercase" style={{ letterSpacing: '.16em', fontSize: '.58rem', color: onDark ? 'rgba(245,240,235,0.4)' : 'rgba(26,36,54,0.32)' }}>{l.n}</span>
                  <span>{l.label}</span>
                </a>
              ))}
            </nav>
            <div className="flex items-center gap-3">
              <a href="#contact" onClick={smoothScrollTo} className="inline-flex items-center gap-1.5 sm:gap-2 px-3 sm:px-5 py-2 sm:py-2.5 rounded-full font-display font-semibold transition-colors"
                 style={{
                   background: onDark ? 'var(--parchment)' : 'var(--abyss)',
                   color: onDark ? 'var(--abyss)' : 'var(--parchment)',
                   fontSize: '.82rem'
                 }}>
                <span className="hidden sm:inline">Start a project</span>
                <span className="sm:hidden">Start</span>
                <ArrowRight size={13}/>
              </a>
            </div>
          </div>
        </div>
      </header>
    </>
  );
}
window.Nav = Nav;
