// Fixed header — logo lockup with byline, section links, gold triage CTA.
// Scroll-spy highlights the active section; mobile collapses to a sheet.
function Nav() {
  const links = [
    { label: 'The Problem', id: '#problem' },
    { label: 'Services', id: '#services' },
    { label: 'Methodology', id: '#methodology' },
    { label: 'About', id: '#about' },
  ];
  const [active, setActive] = React.useState('#problem');
  const [scrolled, setScrolled] = React.useState(false);
  const [menu, setMenu] = React.useState(false);

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

  React.useEffect(() => {
    const ids = links.map((l) => l.id.slice(1));
    const onScroll = () => {
      const mid = window.innerHeight * 0.4;
      let current = ids[0];
      for (const id of ids) {
        const el = document.getElementById(id);
        if (el && el.getBoundingClientRect().top <= mid) current = id;
      }
      setActive('#' + current);
    };
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const go = (e, id) => {
    e.preventDefault();
    setMenu(false);
    const t = document.querySelector(id);
    if (t) t.scrollIntoView({ behavior: 'auto', block: 'start' });
  };

  const LogoLockup = (
    <a className="logo-lockup" href="#top" onClick={(e) => go(e, '#top')}>
      <img className="mark" src="assets/monogram.svg" alt="" />
      <span>
        <span className="logo-word">Franz Schöning</span>
        <span className="logo-byline" style={{ display: 'block' }}>Principal Enterprise Architect</span>
      </span>
    </a>
  );

  return (
    <header id="top" style={{
      position: 'sticky', top: 0, zIndex: 60,
      background: scrolled ? 'rgba(253,253,254,0.90)' : 'rgba(253,253,254,0.72)',
      backdropFilter: 'blur(12px)', WebkitBackdropFilter: 'blur(12px)',
      borderBottom: `1px solid ${scrolled ? 'var(--fs-border)' : 'transparent'}`,
      transition: 'background var(--fs-dur) var(--fs-ease), border-color var(--fs-dur) var(--fs-ease)',
    }}>
      <div className="wrap" style={{ height: 84, display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 24 }}>
        {LogoLockup}

        <nav className="nav-links-desktop" style={{ display: 'flex', alignItems: 'center', gap: 36 }}>
          {links.map((l) => (
            <a key={l.id} href={l.id} onClick={(e) => go(e, l.id)}
              className={`nav-link${active === l.id ? ' active' : ''}`}>{l.label}</a>
          ))}
          <Button variant="primary" size="sm" href="#contact" icon="arrow-right">Request Triage Audit</Button>
        </nav>

        <button className="nav-toggle" onClick={() => setMenu((m) => !m)} aria-label="Menu" style={{
          background: 'transparent', border: '1px solid var(--fs-border-strong)', borderRadius: 'var(--fs-radius-sm)',
          width: 42, height: 42, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: 'var(--fs-ink)',
        }}>
          <Icon name={menu ? 'x' : 'menu'} size={20} />
        </button>
      </div>

      {/* mobile sheet */}
      {menu && (
        <div className="nav-toggle" style={{
          borderTop: '1px solid var(--fs-border)', background: 'var(--fs-surface)', padding: '8px var(--fs-gutter) 20px',
        }}>
          {links.map((l) => (
            <a key={l.id} href={l.id} onClick={(e) => go(e, l.id)} style={{
              display: 'block', padding: '14px 0', borderBottom: '1px solid var(--fs-border)',
              fontFamily: 'var(--fs-font-body)', fontWeight: 600, fontSize: 15, color: 'var(--fs-ink)', textDecoration: 'none',
            }}>{l.label}</a>
          ))}
          <div style={{ marginTop: 18 }}>
            <Button variant="primary" href="#contact" icon="arrow-right" style={{ width: '100%' }}>Request Triage Audit</Button>
          </div>
        </div>
      )}
    </header>
  );
}
Object.assign(window, { Nav });
