// ============================================================
// mobile.jsx — viewport detection hook + mobile nav menu
// Loaded before chrome.jsx so window.useViewport / window.MobileNavOverlay
// are available when the other components evaluate.
// ============================================================

// Single source of truth for the mobile breakpoint.
window.__FL_MOBILE_BREAKPOINT = 768;

// useViewport — returns { mobile: boolean }. Listens to matchMedia so
// rotation / window-resize updates trigger a React re-render.
window.useViewport = function useViewport() {
  const query = "(max-width: " + window.__FL_MOBILE_BREAKPOINT + "px)";
  const get = () => (typeof window !== "undefined" && window.matchMedia
    ? window.matchMedia(query).matches
    : false);
  const [mobile, setMobile] = React.useState(get);
  React.useEffect(() => {
    const mql = window.matchMedia(query);
    const onChange = (e) => setMobile(e.matches);
    // Safari < 14 uses addListener / removeListener.
    if (mql.addEventListener) mql.addEventListener("change", onChange);
    else mql.addListener(onChange);
    return () => {
      if (mql.removeEventListener) mql.removeEventListener("change", onChange);
      else mql.removeListener(onChange);
    };
  }, []);
  return { mobile };
};

// MobileNavOverlay — full-screen drawer that drops down when the
// hamburger is tapped. Renders the same nav items the desktop bar would.
window.MobileNavOverlay = function MobileNavOverlay({ open, onClose, items, current, onNavigate }) {
  // Lock body scroll while the drawer is open so the page underneath doesn't slide.
  React.useEffect(() => {
    if (!open) return;
    const prev = document.body.style.overflow;
    document.body.style.overflow = "hidden";
    return () => { document.body.style.overflow = prev; };
  }, [open]);

  if (!open) return null;
  return (
    <div className="mobile-nav-overlay" onClick={onClose}
      style={{
        position: "fixed", inset: 0, zIndex: 9000,
        background: "rgba(8,12,9,0.96)",
        display: "flex", flexDirection: "column",
        animation: "pagefade 160ms linear",
      }}>
      <div style={{
        padding: "18px 20px",
        borderBottom: "1px solid var(--border-default)",
        display: "flex", justifyContent: "space-between", alignItems: "center",
        fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.22em",
        textTransform: "uppercase", color: "var(--fg-muted)",
      }}>
        <span>// NAVIGATION</span>
        <button onClick={onClose} aria-label="Close navigation"
          style={{
            background: "transparent", border: "1px solid var(--border-default)",
            color: "var(--phosphor-500)", padding: "4px 10px",
            fontFamily: "var(--font-mono)", fontSize: 14, cursor: "pointer",
          }}>✕</button>
      </div>

      <nav style={{ display: "flex", flexDirection: "column", padding: "12px 0" }}
        onClick={(e) => e.stopPropagation()}>
        {items.map((n) => {
          const active = current === n.id;
          const color = n.anom ? "var(--offspec-500)" : (active ? "var(--phosphor-500)" : "var(--bone-100)");
          return (
            <a key={n.id} onClick={() => { onNavigate(n.id); onClose(); }}
              style={{
                padding: "20px 24px",
                borderBottom: "1px solid var(--border-faint)",
                fontFamily: "var(--font-display)", fontSize: 18,
                letterSpacing: "0.18em", textTransform: "uppercase",
                color, cursor: "pointer",
                textShadow: active ? "0 0 8px #33ff6688" : "none",
              }}>
              {n.label}
            </a>
          );
        })}
        <a onClick={() => { onNavigate("contact"); onClose(); }}
          className="btn solid"
          style={{ margin: "20px 24px 8px", textAlign: "center", padding: "14px 0" }}>
          Intake →
        </a>
      </nav>
    </div>
  );
};
