/* Navigation: scroll-spy side-nav + auto-built on-page TOC, mobile hamburger,
   current-page marking, and the review banner toggle. All vanilla. */
(function () {
  function autoToc() {
    const toc = document.querySelector(".toc[data-auto]");
    if (!toc || toc.dataset.built) return;
    const sections = document.querySelectorAll(".platform-content section[id] > h2, .platform-content section[id] h2");
    const list = document.createElement("nav");
    document.querySelectorAll(".platform-content section[id]").forEach((sec) => {
      const h = sec.querySelector("h2"); if (!h) return;
      const a = document.createElement("a");
      a.href = "#" + sec.id; a.textContent = h.textContent;
      list.appendChild(a);
    });
    toc.appendChild(list); toc.dataset.built = "1";
  }

  function scrollSpy() {
    const links = Array.from(document.querySelectorAll('.side-nav a[href^="#"], .toc a[href^="#"]'));
    if (!links.length) return;
    const map = new Map();
    links.forEach((a) => {
      const sec = document.getElementById(a.getAttribute("href").slice(1));
      if (sec) (map.get(sec) || map.set(sec, []).get(sec)).push(a);
    });
    const obs = new IntersectionObserver((entries) => {
      entries.forEach((en) => {
        if (!en.isIntersecting) return;
        links.forEach((l) => { l.classList.remove("is-active"); l.removeAttribute("aria-current"); });
        (map.get(en.target) || []).forEach((a) => { a.classList.add("is-active"); a.setAttribute("aria-current", "true"); });
      });
    }, { rootMargin: "0px 0px -68% 0px", threshold: 0 });
    map.forEach((_, sec) => obs.observe(sec));
  }

  function mobileNav() {
    const t = document.querySelector(".nav-toggle");
    const nav = document.getElementById("primary-nav");
    if (!t || !nav || t.dataset.bound) return;
    t.dataset.bound = "1";
    t.addEventListener("click", () => {
      const open = nav.classList.toggle("is-open");
      t.setAttribute("aria-expanded", open);
    });
  }

  function markCurrent() {
    const page = (location.pathname.split("/").pop() || "index.html");
    document.querySelectorAll(".nav a[href]").forEach((a) => {
      const href = a.getAttribute("href");
      if (href && href.split("/").pop() === page) a.setAttribute("aria-current", "page");
    });
  }

  function reviewBanner() {
    const cfg = window.RGP_CONFIG || {};
    const banner = document.querySelector(".review-banner");
    if (!banner) return;
    if (!cfg.REVIEW_MODE) { banner.remove(); return; }
    const close = banner.querySelector("button");
    if (close) close.addEventListener("click", () => banner.remove());
  }

  function headerShadow() {
    const header = document.querySelector(".site-header");
    if (!header || header.dataset.scrollBound) return;
    header.dataset.scrollBound = "1";
    const onScroll = () => header.classList.toggle("is-scrolled", window.scrollY > 4);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
  }

  function build() { autoToc(); scrollSpy(); mobileNav(); markCurrent(); reviewBanner(); headerShadow(); }
  document.addEventListener("DOMContentLoaded", build);
  document.addEventListener("partials:loaded", () => { mobileNav(); markCurrent(); reviewBanner(); headerShadow(); });
})();
