/* Floating site navigation — bunkerme (responsive).
   Desktop: bunkerme · Platform ▾ · How it works · Ports · About | Log in · Get started
   Mobile (≤860px): logo + hamburger → full-screen drawer with the same links. */
function SiteNav() {
  const vw = useVW();
  const isMobile = vw <= 860;

  const [scrolled, setScrolled] = React.useState(false);
  const [openPlatform, setOpenPlatform] = React.useState(false);
  const [drawer, setDrawer] = React.useState(false);
  const [mPlatform, setMPlatform] = React.useState(false);
  const closeTimer = React.useRef(null);

  React.useEffect(() => {
    const onScroll = () => setScrolled((window.scrollY || 0) > 8);
    onScroll();
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  React.useEffect(() => {
    document.body.style.overflow = drawer ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [drawer]);
  React.useEffect(() => { if (!isMobile) setDrawer(false); }, [isMobile]);

  const open = () => { clearTimeout(closeTimer.current); setOpenPlatform(true); };
  const close = () => { closeTimer.current = setTimeout(() => setOpenPlatform(false), 110); };

  const platformItems = [
    ['radar', 'Source', 'Live market prices and quotes across 100+ ports.', 'platform.html#source'],
    ['lock', 'Fix', 'Lock a stem and confirm nomination in a single flow.', 'platform.html#fix'],
    ['wallet', 'Pay later', 'Settle on flexible Net 30 / 60 credit terms.', 'platform.html#pay-later'],
    ['activity', 'Monitor', 'Track deliveries, claims, and spend across your fleet.', 'platform.html#monitor'],
  ];
  const plainLinks = [
    ['How it works', 'index.html#how'],
    ['Ports', 'index.html#ports'],
    ['About', 'about.html'],
  ];

  const Brand = (
    <a href="index.html" style={{ display: 'inline-flex', alignItems: 'center', gap: 8, lineHeight: 1 }}>
      <img src="../assets/logo-mark-black.svg" width="24" height="24" alt="" />
      <span style={{ fontSize: 23, fontWeight: 700, letterSpacing: '-0.035em' }}>
        <span style={{ color: 'var(--text-primary)' }}>bunker</span><span style={{ color: 'var(--blue-500)' }}>me</span>
      </span>
    </a>
  );

  /* ---------- MOBILE ---------- */
  if (isMobile) {
    return (
      <header style={{ position: 'sticky', top: 0, zIndex: 60, padding: '12px 14px' }}>
        <div style={{
          maxWidth: 'var(--container-max)', margin: '0 auto',
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
          height: 60, padding: '0 10px 0 16px',
          background: (scrolled || drawer) ? 'rgba(255,255,255,0.9)' : 'var(--neutral-0)',
          backdropFilter: (scrolled || drawer) ? 'saturate(180%) blur(16px)' : 'none',
          WebkitBackdropFilter: (scrolled || drawer) ? 'saturate(180%) blur(16px)' : 'none',
          border: '1px solid var(--border-subtle)',
          borderRadius: 'var(--radius-2xl)',
          boxShadow: scrolled ? 'var(--shadow-md)' : 'var(--shadow-sm)',
          transition: 'background 240ms var(--ease-out), box-shadow 240ms var(--ease-out)',
          position: 'relative', zIndex: 2,
        }}>
          {Brand}
          <button type="button" aria-label={drawer ? 'Close menu' : 'Open menu'} aria-expanded={drawer}
            onClick={() => setDrawer((d) => !d)}
            style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', width: 44, height: 44, border: 'none', background: 'transparent', color: 'var(--text-primary)', cursor: 'pointer', borderRadius: 'var(--radius-md)' }}>
            <Icon name={drawer ? 'x' : 'menu'} size={24} color="currentColor" />
          </button>
        </div>

        {/* drawer */}
        <div style={{
          position: 'fixed', inset: 0, top: 0, zIndex: 1,
          background: 'rgba(255,255,255,0.98)',
          backdropFilter: 'blur(6px)', WebkitBackdropFilter: 'blur(6px)',
          padding: '92px 20px 32px',
          display: 'flex', flexDirection: 'column',
          opacity: drawer ? 1 : 0,
          transform: drawer ? 'translateY(0)' : 'translateY(-8px)',
          pointerEvents: drawer ? 'auto' : 'none',
          transition: 'opacity 220ms var(--ease-out), transform 220ms var(--ease-out)',
          overflowY: 'auto',
        }}>
          <nav style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
            {/* Platform accordion */}
            <button type="button" onClick={() => setMPlatform((v) => !v)}
              style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', width: '100%', padding: '16px 8px', border: 'none', background: 'transparent', fontSize: 'var(--text-h4)', fontWeight: 600, color: 'var(--text-primary)', cursor: 'pointer', fontFamily: 'inherit', letterSpacing: 'var(--tracking-tight)' }}>
              Platform
              <span style={{ display: 'inline-flex', transition: 'transform 200ms var(--ease-out)', transform: mPlatform ? 'rotate(180deg)' : 'none' }}>
                <Icon name="chevron-down" size={20} color="var(--neutral-500)" />
              </span>
            </button>
            <div style={{ overflow: 'hidden', maxHeight: mPlatform ? 360 : 0, transition: 'max-height 280ms var(--ease-out)' }}>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 4, padding: '2px 0 10px' }}>
                {platformItems.map(([icon, title, , href]) => (
                  <a key={title} href={href} style={{ display: 'flex', gap: 12, padding: '11px 12px', borderRadius: 'var(--radius-md)', alignItems: 'center', background: 'var(--neutral-50)' }}>
                    <span style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', width: 32, height: 32, flexShrink: 0, borderRadius: 'var(--radius-sm)', background: 'var(--accent-subtle)', color: 'var(--blue-600)' }}>
                      <Icon name={icon} size={17} color="var(--blue-600)" />
                    </span>
                    <span style={{ fontSize: 'var(--text-base)', fontWeight: 600, color: 'var(--text-primary)' }}>{title}</span>
                  </a>
                ))}
              </div>
            </div>
            {plainLinks.map(([l, href]) => (
              <a key={l} href={href} style={{ padding: '16px 8px', fontSize: 'var(--text-h4)', fontWeight: 600, color: 'var(--text-primary)', borderTop: '1px solid var(--border-subtle)', letterSpacing: 'var(--tracking-tight)' }}>{l}</a>
            ))}
          </nav>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 12, marginTop: 'auto', paddingTop: 28 }}>
            <a href="login.html" style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', height: 52, fontSize: 'var(--text-base)', fontWeight: 600, color: 'var(--text-primary)', border: '1px solid var(--border-strong)', borderRadius: 'var(--radius-md)' }}>Log in</a>
            <a href="signup.html" style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 9, height: 52, fontSize: 'var(--text-base)', fontWeight: 600, color: '#fff', background: 'var(--blue-500)', borderRadius: 'var(--radius-md)', boxShadow: '0 1px 0 rgba(255,255,255,0.18) inset, var(--shadow-sm)' }}>Get started<Icon name="arrow-right" size={17} color="#fff" /></a>
          </div>
        </div>
      </header>
    );
  }

  /* ---------- DESKTOP ---------- */
  return (
    <header style={{ position: 'sticky', top: 0, zIndex: 60, padding: '14px 20px' }}>
      <div style={{
        maxWidth: 'var(--container-max)', margin: '0 auto',
        display: 'flex', alignItems: 'center', gap: 24,
        height: 72, padding: '0 14px 0 26px',
        background: scrolled ? 'rgba(255,255,255,0.85)' : 'var(--neutral-0)',
        backdropFilter: scrolled ? 'saturate(180%) blur(16px)' : 'none',
        WebkitBackdropFilter: scrolled ? 'saturate(180%) blur(16px)' : 'none',
        border: '1px solid var(--border-subtle)',
        borderRadius: 'var(--radius-2xl)',
        boxShadow: scrolled ? 'var(--shadow-md)' : 'var(--shadow-sm)',
        transition: 'background 240ms var(--ease-out), box-shadow 240ms var(--ease-out)',
      }}>
        {Brand}
        <nav style={{ display: 'flex', alignItems: 'center', gap: 6, marginLeft: 18 }}>
          {/* Platform dropdown */}
          <div style={{ position: 'relative' }} onMouseEnter={open} onMouseLeave={close}>
            <a href="platform.html" className="bm-navlink" aria-expanded={openPlatform} aria-haspopup="true"
               style={{ display: 'inline-flex', alignItems: 'center', gap: 5, padding: '9px 12px', fontSize: 'var(--text-base)', fontWeight: 500, color: openPlatform ? 'var(--text-primary)' : 'var(--neutral-700)', background: openPlatform ? 'rgba(17,21,26,0.05)' : 'transparent', borderRadius: 'var(--radius-md)', whiteSpace: 'nowrap', cursor: 'pointer' }}>
              Platform
              <span style={{ display: 'inline-flex', transition: 'transform 200ms var(--ease-out)', transform: openPlatform ? 'rotate(180deg)' : 'none' }}>
                <Icon name="chevron-down" size={16} color="var(--neutral-500)" />
              </span>
            </a>
            {/* panel */}
            <div style={{
              position: 'absolute', top: '100%', left: -8, paddingTop: 14,
              opacity: openPlatform ? 1 : 0,
              transform: openPlatform ? 'translateY(0)' : 'translateY(-6px)',
              pointerEvents: openPlatform ? 'auto' : 'none',
              transition: 'opacity 180ms var(--ease-out), transform 180ms var(--ease-out)',
            }}>
              <div style={{ width: 384, background: 'var(--neutral-0)', border: '1px solid var(--border-subtle)', borderRadius: 'var(--radius-xl)', boxShadow: 'var(--shadow-xl)', padding: 8 }}>
                {platformItems.map(([icon, title]) => (
                  <div key={title} className="bm-menuitem-static"
                     style={{ display: 'flex', gap: 12, padding: '10px 12px', borderRadius: 'var(--radius-md)', alignItems: 'center', cursor: 'default' }}>
                    <span style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', width: 32, height: 32, flexShrink: 0, borderRadius: 'var(--radius-sm)', background: 'var(--accent-subtle)', color: 'var(--blue-600)' }}>
                      <Icon name={icon} size={17} color="var(--blue-600)" />
                    </span>
                    <span style={{ fontSize: 'var(--text-sm)', fontWeight: 600, color: 'var(--text-primary)' }}>{title}</span>
                  </div>
                ))}
                <a href="platform.html" className="bm-menucta"
                   style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 6, padding: '13px 14px', borderRadius: 'var(--radius-md)', background: 'var(--neutral-50)', border: '1px solid var(--border-subtle)' }}>
                  <span style={{ display: 'flex', flexDirection: 'column' }}>
                    <span style={{ fontSize: 'var(--text-sm)', fontWeight: 600, color: 'var(--text-primary)' }}>Explore the platform</span>
                    <span style={{ fontSize: 'var(--text-xs)', color: 'var(--text-tertiary)' }}>See how it all fits together</span>
                  </span>
                  <Icon name="arrow-right" size={17} color="var(--blue-600)" />
                </a>
              </div>
            </div>
          </div>
          {plainLinks.map(([l, href]) => (
            <a key={l} href={href} className="bm-navlink"
               style={{ display: 'inline-flex', alignItems: 'center', padding: '9px 12px', fontSize: 'var(--text-base)', fontWeight: 500, color: 'var(--neutral-700)', borderRadius: 'var(--radius-md)', whiteSpace: 'nowrap' }}>{l}</a>
          ))}
        </nav>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginLeft: 'auto' }}>
          <a href="login.html" className="bm-signin" style={{ padding: '9px 8px', fontSize: 'var(--text-base)', fontWeight: 500, color: 'var(--neutral-600)', whiteSpace: 'nowrap' }}>Log in</a>
          <a href="signup.html" className="bm-cta" style={{
            display: 'inline-flex', alignItems: 'center', gap: 9, whiteSpace: 'nowrap',
            padding: '12px 22px', fontSize: 'var(--text-base)', fontWeight: 600,
            color: '#fff', background: 'var(--blue-500)', borderRadius: '14px',
            boxShadow: '0 1px 0 rgba(255,255,255,0.18) inset, var(--shadow-sm)',
            transition: 'background 160ms var(--ease-out), transform 120ms var(--ease-out)',
          }}>Get started<Icon name="arrow-right" size={17} color="#fff" /></a>
        </div>
      </div>
      <style>{`
        .bm-navlink:hover{ background: rgba(17,21,26,0.05); color: var(--text-primary); }
        .bm-signin:hover{ color: var(--text-primary); }
        .bm-cta:hover{ background: var(--blue-600); }
        .bm-cta:active{ transform: translateY(0.5px); }
        .bm-menucta{ transition: background 140ms var(--ease-out), border-color 140ms var(--ease-out); }
        .bm-menucta:hover{ background: var(--accent-subtle); border-color: var(--accent-border); }
      `}</style>
    </header>
  );
}

Object.assign(window, { SiteNav });
