document.addEventListener("DOMContentLoaded", function () {
  var toggle = document.querySelector(".menu-toggle");
  var nav = document.querySelector(".main-nav");
  if (!toggle || !nav) {
    return;
  }

  toggle.addEventListener("click", function () {
    var isOpen = nav.classList.toggle("is-open");
    toggle.classList.toggle("is-open", isOpen);
    toggle.setAttribute("aria-expanded", isOpen ? "true" : "false");
  });

  var submenuToggles = document.querySelectorAll(".submenu-toggle");
  submenuToggles.forEach(function (button) {
    button.addEventListener("click", function () {
      var item = button.closest(".menu-item");
      if (!item) {
        return;
      }

      var isOpen = item.classList.contains("is-open");
      document.querySelectorAll(".menu-item.is-open").forEach(function (openItem) {
        if (openItem !== item) {
          openItem.classList.remove("is-open");
          var openButton = openItem.querySelector(".submenu-toggle");
          if (openButton) {
            openButton.setAttribute("aria-expanded", "false");
          }
        }
      });

      item.classList.toggle("is-open", !isOpen);
      button.setAttribute("aria-expanded", !isOpen ? "true" : "false");
    });
  });
});
