(() => {
  "use strict";
  const mq = matchMedia("(max-width: 767px)");
  let drawer = null;
  let trigger = null;
  let backdrop = null;
  let bound = false;

  const isOpen = () => Boolean(
    mq.matches && drawer && !drawer.classList.contains("hidden") &&
    getComputedStyle(drawer).display !== "none"
  );

  function closeMenu() {
    if (!isOpen()) return;
    trigger?.click();
    requestAnimationFrame(sync);
  }

  function sync() {
    if (!drawer || !trigger) return;
    if (!drawer.classList.contains("ai-architect-drawer")) drawer.classList.add("ai-architect-drawer");
    if (!trigger.classList.contains("ai-menu-trigger")) trigger.classList.add("ai-menu-trigger");
    const currentHeader = trigger.closest("header");
    if (currentHeader && !currentHeader.classList.contains("ai-architect-header")) currentHeader.classList.add("ai-architect-header");
    const open = isOpen();
    trigger.setAttribute("aria-expanded", String(open));
    trigger.setAttribute("aria-label", open ? "Close AI Architect navigation" : "Open AI Architect navigation");
    document.documentElement.classList.toggle("ai-architect-nav-open", open);
    document.body.classList.toggle("ai-architect-nav-open", open);
    if (backdrop) {
      backdrop.hidden = true;
      backdrop.setAttribute("aria-hidden", "true");
      backdrop.style.display = "none";
    }
  }

  function setup() {
    drawer = document.querySelector("aside.ai-architect-drawer") || [...document.querySelectorAll("aside")].find(el => {
      const text = el.textContent || "";
      return text.includes("Enterprise RAG") && text.includes("Cloud Compare") && text.includes("Governance");
    });
    if (!drawer) return false;

    trigger = document.querySelector("header .ai-menu-trigger") || document.querySelector("header button.md\\:hidden") || document.querySelector("header button");
    if (!trigger) return false;

    drawer.classList.add("ai-architect-drawer");
    trigger.classList.add("ai-menu-trigger");
    backdrop = document.querySelector(".ai-nav-backdrop");
    if (backdrop) {
      backdrop.hidden = true;
      backdrop.setAttribute("aria-hidden", "true");
      backdrop.style.display = "none";
    }

    if (!bound) {
      bound = true;
      document.addEventListener("pointerdown", event => {
        if (!isOpen()) return;
        const target = event.target;
        if (drawer?.contains(target) || trigger?.contains(target)) return;
        closeMenu();
      }, {capture: true, passive: true});

      document.addEventListener("keydown", event => {
        if (event.key === "Escape" && isOpen()) {
          event.preventDefault();
          closeMenu();
        }
      }, {capture: true});

      addEventListener("resize", () => requestAnimationFrame(sync), {passive: true});
      addEventListener("orientationchange", () => setTimeout(sync, 120), {passive: true});
    }

    new MutationObserver(sync).observe(drawer, {attributes:true, attributeFilter:["class","style"]});
    sync();
    return true;
  }

  let attempts = 0;
  const boot = () => {
    if (setup()) return;
    if (++attempts < 80) setTimeout(boot, 100);
  };
  if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot, {once:true});
  else boot();
})();
