window.MktNav = function MktNav() {
  const isMobile = useIsMobile();
  const isTablet = useIsTablet();
  const [scrolled, setScrolled] = React.useState(false);
  const [mobileOpen, setMobileOpen] = React.useState(false);

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

  const smoothScroll = (e, href) => {
    if (href.startsWith('#') || href.startsWith('/#')) {
      const id = href.split('#')[1];
      const el = document.getElementById(id);
      if (el) {
        e.preventDefault();
        const y = el.getBoundingClientRect().top + window.pageYOffset - 80;
        window.scrollTo({ top: y, behavior: 'smooth' });
      }
    }
    setMobileOpen(false);
  };

  const links = [
    { href: '#pillars', label: 'How' },
    { href: '#work', label: 'Work' },
    { href: '#faq', label: 'FAQ' },
  ];

  const navPadding = isMobile ? '0 24px' : isTablet ? '0 32px' : '0 48px';

  const arrow = (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
      <path d="M3 7h8M7 3l4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );

  return (
    <nav style={{
      position: 'sticky', top: 0, zIndex: 100,
      height: 80, padding: navPadding,
      background: 'rgba(255,255,255,0.85)',
      backdropFilter: 'blur(12px)',
      WebkitBackdropFilter: 'blur(12px)',
      borderBottom: scrolled ? '1px solid var(--border)' : '1px solid transparent',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      transition: 'border-color var(--dur-base) var(--ease)',
    }}>
      <a href="/" style={{
        fontFamily: 'var(--font-sans)',
        fontWeight: 800,
        fontSize: 24,
        letterSpacing: '-0.04em',
        color: 'var(--text-primary)',
      }}>stitch<span className="cursor-blink" style={{ color: 'var(--accent)' }}>_</span></a>

      {!isMobile && (
        <div style={{ display: 'flex', alignItems: 'center', gap: 32 }}>
          {links.map(l => (
            <a key={l.href} href={l.href} onClick={e => smoothScroll(e, l.href)} style={{
              fontWeight: 500, fontSize: 14, color: 'var(--text-secondary)',
              transition: 'color var(--dur-micro) var(--ease)',
            }}
              onMouseEnter={(ev) => ev.currentTarget.style.color = 'var(--text-primary)'}
              onMouseLeave={(ev) => ev.currentTarget.style.color = 'var(--text-secondary)'}
            >{l.label}</a>
          ))}
          <a href="#contact" onClick={e => smoothScroll(e, '#contact')} className="cta-arrow" style={{
            fontWeight: 600, fontSize: 14, color: 'var(--text-primary)',
          }}>
            Book a consultation
            <span className="arrow">{arrow}</span>
          </a>
        </div>
      )}

      {isMobile && (
        <button type="button" onClick={() => setMobileOpen(o => !o)}
          aria-expanded={mobileOpen}
          aria-label={mobileOpen ? 'Close menu' : 'Open menu'}
          style={{
            width: 44, height: 44,
            background: 'transparent', border: 'none', padding: 0,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            cursor: 'pointer',
          }}>
          <span style={{ position: 'relative', width: 22, height: 16, display: 'inline-block' }}>
            <span style={{
              position: 'absolute', left: 0, right: 0, height: 2,
              background: 'var(--text-primary)',
              top: mobileOpen ? 7 : 0,
              transform: mobileOpen ? 'rotate(45deg)' : 'rotate(0)',
              transition: 'transform 200ms var(--ease), top 200ms var(--ease)',
            }}></span>
            <span style={{
              position: 'absolute', left: 0, right: 0, height: 2, top: 7,
              background: 'var(--text-primary)',
              opacity: mobileOpen ? 0 : 1,
              transition: 'opacity 200ms var(--ease)',
            }}></span>
            <span style={{
              position: 'absolute', left: 0, right: 0, height: 2,
              background: 'var(--text-primary)',
              top: mobileOpen ? 7 : 14,
              transform: mobileOpen ? 'rotate(-45deg)' : 'rotate(0)',
              transition: 'transform 200ms var(--ease), top 200ms var(--ease)',
            }}></span>
          </span>
        </button>
      )}

      {isMobile && (
        <div style={{
          position: 'absolute', top: '100%', left: 0, right: 0,
          background: 'var(--bg)',
          borderBottom: '1px solid var(--border)',
          display: 'flex', flexDirection: 'column', gap: 4,
          padding: mobileOpen ? '16px 24px 24px' : '0 24px',
          maxHeight: mobileOpen ? 480 : 0,
          opacity: mobileOpen ? 1 : 0,
          transform: mobileOpen ? 'translateY(0)' : 'translateY(-8px)',
          overflow: 'hidden',
          pointerEvents: mobileOpen ? 'auto' : 'none',
          transition: 'opacity 200ms var(--ease), transform 200ms var(--ease), max-height 200ms var(--ease), padding 200ms var(--ease)',
        }}>
          {links.map(l => (
            <a key={l.href} href={l.href} onClick={e => smoothScroll(e, l.href)} style={{
              fontWeight: 600, fontSize: 18, color: 'var(--text-primary)',
              minHeight: 48, display: 'flex', alignItems: 'center',
            }}>{l.label}</a>
          ))}
          <a href="#contact" onClick={e => smoothScroll(e, '#contact')} className="btn-solid" style={{
            marginTop: 12, width: '100%',
          }}>
            Book a consultation
            {arrow}
          </a>
        </div>
      )}
    </nav>
  );
};
