// =========================================================
// FILE: marketing-nav.js
// Handles mobile navigation toggle on public pages.
// =========================================================

(function () {
  "use strict";

  const toggle  = document.getElementById("mobileNavToggle");
  const navWrap = document.getElementById("mobileNav");

  if (!toggle || !navWrap) return;

  // Close when a nav link is clicked
  navWrap.querySelectorAll("a").forEach(function (link) {
    link.addEventListener("click", close);
  });

  toggle.addEventListener("click", function () {
    const isOpen = navWrap.style.display === "block";
    isOpen ? close() : open();
  });

  // Close on outside click
  document.addEventListener("click", function (e) {
    if (!toggle.contains(e.target) && !navWrap.contains(e.target)) {
      close();
    }
  });

  // Close on Escape key
  document.addEventListener("keydown", function (e) {
    if (e.key === "Escape") close();
  });

  function open() {
    navWrap.style.display = "block";
    toggle.setAttribute("aria-expanded", "true");
    toggle.querySelector("i").className = "fa-solid fa-xmark";
  }

  function close() {
    navWrap.style.display = "none";
    toggle.setAttribute("aria-expanded", "false");
    toggle.querySelector("i").className = "fa-solid fa-bars";
  }
})();
