// site-nav.jsx — shared site chrome used by all pages.
// Exports SiteNav + SiteFooter. Loaded BEFORE each page's own script.

const { useState: useS_n, useEffect: useE_n } = React;

function useSiteNavScrollY() {
  const [y, setY] = useS_n(0);
  useE_n(() => {
    let raf = 0;
    const h = () => { if (raf) return; raf = requestAnimationFrame(() => { setY(window.scrollY); raf = 0; }); };
    window.addEventListener('scroll', h, { passive: true });
    return () => { window.removeEventListener('scroll', h); cancelAnimationFrame(raf); };
  }, []);
  return y;
}

const SITE_NAV_LINKS = [
  ['How we work', 'methodology.html'],
  ['Services',    'services.html'],
  ['Sectors',     'sectors.html'],
  ['About',       'about.html'],
];

// Determine which nav item is the current page so we can mark it.
function currentPage() {
  if (typeof window === 'undefined') return '';
  const p = window.location.pathname.split('/').pop() || 'index.html';
  return p;
}

function SiteNav() {
  const scrollY = useSiteNavScrollY();
  const scrolled = scrollY > 30;
  const here = currentPage();
  const isHome = here === '' || here === 'index.html';
  const [open, setOpen] = useS_n(false);

  // Close drawer if window resizes past mobile breakpoint
  useE_n(() => {
    const onResize = () => { if (window.innerWidth > 900) setOpen(false); };
    window.addEventListener('resize', onResize);
    return () => window.removeEventListener('resize', onResize);
  }, []);

  // Lock body scroll while drawer is open
  useE_n(() => {
    if (open) document.body.style.overflow = 'hidden';
    else      document.body.style.overflow = '';
    return () => { document.body.style.overflow = ''; };
  }, [open]);

  // Rewrite same-page hash links so they smooth-scroll instead of reloading.
  const resolveHref = (h) => {
    if (!isHome) return h;
    const [file, hash] = h.split('#');
    if (hash && (file === 'index.html' || file === '')) return '#' + hash;
    return h;
  };
  const close = () => setOpen(false);
  const contactHref = isHome ? '#contact' : 'index.html#contact';

  return (
    <>
      <a className="skip-link" href="#main-content">Skip to content</a>
      <div className="sn-bar" style={{
        position:'fixed', top: 0, left: 0, right: 0, zIndex: 50,
        display:'flex', alignItems:'center', justifyContent:'space-between',
        padding: scrolled ? '14px 48px' : '22px 48px',
        transition:'padding .35s ease, background .35s ease, border-color .35s ease, box-shadow .35s ease',
        background: scrolled ? 'rgba(248,247,244,0.92)' : 'rgba(248,247,244,0.72)',
        backdropFilter: 'blur(20px) saturate(160%)',
        WebkitBackdropFilter: 'blur(20px) saturate(160%)',
        borderBottom: scrolled ? '1px solid var(--line)' : '1px solid rgba(20,30,55,0.06)',
        boxShadow: scrolled ? '0 6px 24px rgba(15,23,42,0.06)' : 'none',
        color: 'var(--ink)',
      }}>
      <style>{`
        .sn-link {
          position: relative;
          color: var(--ink-2);
          text-decoration: none;
          padding: 6px 2px;
          transition: color .25s ease, letter-spacing .25s ease;
        }
        .sn-link::after {
          content:'';
          position: absolute; left: 2px; right: 2px; bottom: 0;
          height: 1.5px; background: var(--accent-warm);
          transform: scaleX(0); transform-origin: left center;
          transition: transform .35s cubic-bezier(.2,.7,.2,1);
        }
        .sn-link:hover { color: var(--ink); letter-spacing: 0.22em; }
        .sn-link:hover::after { transform: scaleX(1); }
        .sn-link.is-active { color: var(--ink); font-weight: 600; }
        .sn-link.is-active::after { transform: scaleX(1); }
        .sn-link:focus-visible { outline: 2px solid var(--accent-warm); outline-offset: 6px; border-radius: 2px; }
        .sn-cta {
          display: inline-flex; align-items: center; gap: 10px;
          padding: 11px 20px;
          border: 1.4px solid var(--ink);
          background: transparent; color: var(--ink);
          font-family: 'JetBrains Mono', monospace; font-size: 12px;
          letter-spacing: 0.14em; text-transform: uppercase;
          text-decoration: none; border-radius: 4px;
          transition: background .25s ease, color .25s ease, border-color .25s ease;
        }
        .sn-cta:hover { background: var(--ink); color: var(--paper); }

        /* Hamburger button — desktop hidden, mobile shown via styles.css */
        .sn-burger {
          display: none;
          width: 42px; height: 42px;
          padding: 0;
          background: transparent;
          border: 1.4px solid var(--ink);
          border-radius: 4px;
          cursor: pointer;
          align-items: center;
          justify-content: center;
          flex-direction: column;
          gap: 5px;
        }
        .sn-burger:focus-visible {
          outline: 2px solid var(--accent-warm);
          outline-offset: 3px;
        }
        .sn-burger-line {
          display: block;
          width: 18px; height: 1.6px;
          background: var(--ink);
          transition: transform .25s ease, opacity .25s ease;
          transform-origin: center;
        }
        .sn-burger[aria-expanded="true"] .sn-burger-line:first-child {
          transform: translateY(3.3px) rotate(45deg);
        }
        .sn-burger[aria-expanded="true"] .sn-burger-line:last-child {
          transform: translateY(-3.3px) rotate(-45deg);
        }

        /* Drawer — off-canvas right slide-in. Backdrop dims background. */
        .sn-drawer-scrim {
          position: fixed; inset: 0;
          background: rgba(12,24,52,0.42);
          backdrop-filter: blur(4px);
          z-index: 48;
          opacity: 0;
          pointer-events: none;
          transition: opacity .3s ease;
        }
        .sn-drawer-scrim.is-open {
          opacity: 1;
          pointer-events: auto;
        }
        .sn-drawer {
          position: fixed; top: 0; right: 0; bottom: 0;
          width: 100%; max-width: 380px;
          background: var(--paper);
          z-index: 49;
          transform: translateX(100%);
          transition: transform .35s cubic-bezier(.2,.7,.2,1);
          display: flex;
          flex-direction: column;
          padding: 96px 28px 32px;
          border-left: 1px solid var(--line);
          box-shadow: -12px 0 36px rgba(15,23,42,0.10);
          overflow-y: auto;
        }
        .sn-drawer.is-open { transform: translateX(0); }
        .sn-drawer-label {
          font-family: 'JetBrains Mono', monospace;
          font-size: 10.5px;
          letter-spacing: 0.22em;
          text-transform: uppercase;
          color: var(--ink-3);
          margin-bottom: 18px;
        }
        .sn-drawer-link {
          display: flex;
          align-items: baseline;
          justify-content: space-between;
          padding: 18px 0;
          font-family: 'Inter', system-ui;
          font-size: 22px;
          font-weight: 600;
          letter-spacing: -0.018em;
          color: var(--ink);
          text-decoration: none;
          border-top: 1px solid var(--line);
        }
        .sn-drawer-link:last-of-type { border-bottom: 1px solid var(--line); }
        .sn-drawer-link .sn-drawer-num {
          font-family: 'JetBrains Mono', monospace;
          font-size: 11px;
          letter-spacing: 0.16em;
          color: var(--accent-warm);
          font-weight: 600;
        }
        .sn-drawer-link.is-active {
          color: var(--accent-warm);
        }
        .sn-drawer-cta {
          margin-top: 28px;
          padding: 14px 18px;
          background: var(--accent-warm);
          color: var(--paper);
          text-align: center;
          font-family: 'JetBrains Mono', monospace;
          font-size: 12px;
          letter-spacing: 0.14em;
          text-transform: uppercase;
          text-decoration: none;
          border-radius: 4px;
        }
        .sn-drawer-meta {
          margin-top: auto;
          padding-top: 28px;
          font-family: 'JetBrains Mono', monospace;
          font-size: 10.5px;
          letter-spacing: 0.16em;
          color: var(--ink-3);
        }
      `}</style>
      <a href="index.html" className="sn-logo" style={{display:'flex', alignItems:'center', textDecoration:'none'}}>
        <img src="assets/logo-blue.webp" alt="StratAlliance Global"
          style={{height: 60, width:'auto', display:'block'}}/>
      </a>
      <nav className="sn-nav" style={{display:'flex', gap:34, fontFamily:'JetBrains Mono, monospace', fontSize:12.5, letterSpacing:'0.18em', textTransform:'uppercase'}}>
        {SITE_NAV_LINKS.map(([t, h]) => {
          const file = h.split('#')[0];
          const isActive = !isHome && (file === here);
          return (
            <a key={t} href={resolveHref(h)} className={`sn-link${isActive ? ' is-active' : ''}`} aria-current={isActive ? 'page' : undefined}>{t}</a>
          );
        })}
      </nav>
      <a href={contactHref} className="sn-cta">
        Start a conversation
      </a>
      <button
        className="sn-burger"
        type="button"
        aria-label={open ? 'Close menu' : 'Open menu'}
        aria-expanded={open}
        aria-controls="sn-drawer"
        onClick={() => setOpen(o => !o)}
      >
        <span className="sn-burger-line"/>
        <span className="sn-burger-line"/>
      </button>
    </div>

    {/* Mobile drawer — rendered always; visibility driven by state */}
    <div
      className={`sn-drawer-scrim${open ? ' is-open' : ''}`}
      onClick={close}
      aria-hidden="true"
    />
    <aside
      id="sn-drawer"
      className={`sn-drawer${open ? ' is-open' : ''}`}
      aria-hidden={!open}
    >
      <div className="sn-drawer-label">Menu</div>
      <nav>
        {SITE_NAV_LINKS.map(([t, h], i) => {
          const file = h.split('#')[0];
          const isActive = !isHome && (file === here);
          return (
            <a
              key={t}
              href={resolveHref(h)}
              onClick={close}
              className={`sn-drawer-link${isActive ? ' is-active' : ''}`}
              aria-current={isActive ? 'page' : undefined}
            >
              <span>{t}</span>
              <span className="sn-drawer-num">{`0${i + 1}`}</span>
            </a>
          );
        })}
      </nav>
      <a href={contactHref} onClick={close} className="sn-drawer-cta">
        Start a conversation →
      </a>
      <div className="sn-drawer-meta">
        © 2026 · StratAlliance Global<br/>
        Washington · Tokyo · Brussels
      </div>
    </aside>
    </>
  );
}

function SiteFooter() {
  const here = currentPage();
  const isHome = here === '' || here === 'index.html';
  const resolveHref = (h) => {
    if (!isHome) return h;
    const [file, hash] = h.split('#');
    if (hash && (file === 'index.html' || file === '')) return '#' + hash;
    return h;
  };
  return (
    <footer style={{padding:'36px 64px', background:'var(--paper)', borderTop:'1px solid var(--line)'}}>
      <style>{`
        .sf-link {
          position: relative;
          color: var(--ink-2);
          text-decoration: none;
          padding: 6px 2px;
          transition: color .25s ease, letter-spacing .25s ease;
        }
        .sf-link::after {
          content:'';
          position: absolute; left: 2px; right: 2px; bottom: 0;
          height: 1.5px; background: var(--accent-warm);
          transform: scaleX(0); transform-origin: left center;
          transition: transform .35s cubic-bezier(.2,.7,.2,1);
        }
        .sf-link:hover { color: var(--ink); letter-spacing: 0.22em; }
        .sf-link:hover::after { transform: scaleX(1); }
      `}</style>
      <div className="h-footer__row" style={{maxWidth: 1280, margin:'0 auto', display:'flex', alignItems:'center', justifyContent:'space-between', gap: 24, flexWrap:'wrap'}}>
        <a href="index.html" style={{display:'flex'}}>
          <img src="assets/logo-blue.webp" alt="StratAlliance Global" style={{height: 50, width:'auto', display:'block'}}/>
        </a>
        <nav style={{display:'flex', gap: 28, fontFamily:'JetBrains Mono, monospace', fontSize:11.5, letterSpacing:'0.16em', textTransform:'uppercase'}}>
          {SITE_NAV_LINKS.map(([t, h]) => (
            <a key={t} href={resolveHref(h)} className="sf-link">{t}</a>
          ))}
        </nav>
        <div className="f-mono" style={{fontSize:10.5, color:'var(--ink-3)', letterSpacing:'0.14em'}}>© 2026 · StratAlliance Global</div>
      </div>
    </footer>
  );
}

Object.assign(window, { SiteNav, SiteFooter });
