const menuToggle = document.querySelector("#menu-toggle");
const mobileNav = document.querySelector("#mobile-nav");
const mobileLinks = document.querySelectorAll(".mobile-link");
const year = document.querySelector("#year");

if (menuToggle && mobileNav) {
  menuToggle.addEventListener("click", () => {
    const isExpanded = menuToggle.getAttribute("aria-expanded") === "true";
    menuToggle.setAttribute("aria-expanded", String(!isExpanded));
    mobileNav.classList.toggle("hidden");
  });

  document.addEventListener("keydown", (event) => {
    if (event.key === "Escape") {
      mobileNav.classList.add("hidden");
      menuToggle.setAttribute("aria-expanded", "false");
    }
  });

  window.addEventListener("resize", () => {
    if (window.innerWidth >= 1024) {
      mobileNav.classList.add("hidden");
      menuToggle.setAttribute("aria-expanded", "false");
    }
  });
}

mobileLinks.forEach((link) => {
  link.addEventListener("click", () => {
    if (mobileNav && menuToggle) {
      mobileNav.classList.add("hidden");
      menuToggle.setAttribute("aria-expanded", "false");
    }
  });
});

if (year) {
  year.textContent = new Date().getFullYear();
}
