(() => {
  "use strict";

  if (!location.pathname.includes("/projects/academic-evidence/")) return;

  document.body.classList.add("project-academic-evidence-v25");

  let header = null;
  let trigger = null;
  let drawer = null;
  let backdrop = null;
  let attempts = 0;

  function findHeader() {
    return [...document.querySelectorAll("div")].find(element => {
      const classes = element.classList;
      const title = element.querySelector(":scope > h1");
      return (
        classes.contains("md:hidden") &&
        classes.contains("fixed") &&
        classes.contains("top-0") &&
        classes.contains("left-0") &&
        classes.contains("right-0") &&
        title &&
        (title.textContent || "").includes("Ocaviantyo") &&
        element.querySelectorAll(":scope > div button").length >= 2
      );
    }) || null;
  }

  function findDrawer() {
    return [...document.querySelectorAll("div")].find(element => {
      const classes = element.classList;
      const nav = element.querySelector(":scope > nav[aria-label='Mobile navigation']");
      return (
        classes.contains("md:hidden") &&
        classes.contains("fixed") &&
        classes.contains("inset-0") &&
        nav
      );
    }) || null;
  }

  function ensureBackdrop() {
    if (backdrop && document.body.contains(backdrop)) return backdrop;

    backdrop = document.createElement("button");
    backdrop.type = "button";
    backdrop.className = "ae25-backdrop";
    backdrop.setAttribute("aria-label", "Close AI Portfolio navigation");
    backdrop.addEventListener("click", closeDrawer);
    document.body.appendChild(backdrop);
    return backdrop;
  }

  function closeDrawer() {
    if (trigger && trigger.getAttribute("aria-label")?.toLowerCase().includes("close")) {
      trigger.click();
    } else if (trigger && drawer && document.body.contains(drawer)) {
      trigger.click();
    }

    backdrop?.classList.remove("is-visible");
    document.documentElement.classList.remove("ae25-nav-open");
    document.body.classList.remove("ae25-nav-open");
  }

  function decorateHeader() {
    header = findHeader();
    if (!header) return false;

    header.classList.add("ae25-header");

    const title = header.querySelector(":scope > h1");
    if (title) {
      title.textContent = "AI Portfolio Hub";
      title.setAttribute("title", "AI Portfolio | Academic Evidence Hub");
    }

    const buttons = [...header.querySelectorAll(":scope > div button")];
    trigger = buttons.find(button => {
      const label = button.getAttribute("aria-label") || "";
      return /navigation/i.test(label);
    }) || buttons.at(-1) || null;

    if (trigger) {
      trigger.setAttribute("aria-controls", "ae25-mobile-navigation");
      trigger.setAttribute("aria-expanded", drawer ? "true" : "false");
    }

    return true;
  }

  function decorateDrawer() {
    const nextDrawer = findDrawer();

    if (!nextDrawer) {
      drawer = null;
      backdrop?.classList.remove("is-visible");
      document.documentElement.classList.remove("ae25-nav-open");
      document.body.classList.remove("ae25-nav-open");
      trigger?.setAttribute("aria-expanded", "false");
      return false;
    }

    drawer = nextDrawer;
    drawer.id = "ae25-mobile-navigation";
    drawer.classList.add("ae25-drawer");

    const nav = drawer.querySelector(":scope > nav[aria-label='Mobile navigation']");
    if (!nav) return false;

    if (!drawer.querySelector(":scope > .ae25-drawer-heading")) {
      const heading = document.createElement("div");
      heading.className = "ae25-drawer-heading";
      heading.innerHTML = `
        <div>
          <strong>AI Portfolio Hub</strong>
          <span>Academic evidence navigation</span>
        </div>
        <button class="ae25-drawer-close" type="button"
          aria-label="Close AI Portfolio navigation"></button>
      `;
      drawer.insertBefore(heading, nav);
      heading.querySelector(".ae25-drawer-close").addEventListener("click", closeDrawer);
    }

    nav.querySelectorAll("a").forEach(link => {
      if (link.dataset.ae25Bound === "true") return;
      link.dataset.ae25Bound = "true";
      link.addEventListener("click", () => {
        setTimeout(closeDrawer, 30);
      });
    });

    ensureBackdrop().classList.add("is-visible");
    document.documentElement.classList.add("ae25-nav-open");
    document.body.classList.add("ae25-nav-open");
    trigger?.setAttribute("aria-expanded", "true");
    return true;
  }

  function sync() {
    decorateHeader();
    decorateDrawer();
  }

  const observer = new MutationObserver(sync);
  observer.observe(document.documentElement, {
    childList: true,
    subtree: true
  });

  addEventListener("keydown", event => {
    if (event.key === "Escape" && drawer) {
      event.preventDefault();
      closeDrawer();
    }
  });

  addEventListener("resize", () => {
    if (innerWidth >= 768) closeDrawer();
  }, { passive: true });

  const timer = setInterval(() => {
    attempts += 1;
    sync();
    if ((header && attempts > 8) || attempts > 100) {
      clearInterval(timer);
    }
  }, 120);

  sync();
})();
