document.addEventListener("DOMContentLoaded", () => {
  const toggle = document.getElementById("dark-mode-toggle");
  if (!toggle) return;

  if (localStorage.getItem("darkMode") === "true") {
    document.body.classList.add("dark");
  }

  updateToggle(toggle);

  toggle.addEventListener("click", () => {
    const isDark = document.body.classList.toggle("dark");
    localStorage.setItem("darkMode", isDark);
    updateToggle(toggle);
  });

  document.querySelectorAll('.dropdown > a').forEach(btn => {
    btn.addEventListener('click', (e) => {
      e.preventDefault();
      btn.closest('.dropdown').classList.toggle('open');
    });
  });

  document.addEventListener('click', (e) => {
    if (!e.target.closest('.dropdown')) {
      document.querySelectorAll('.dropdown').forEach(d => d.classList.remove('open'));
    }
  });
});

function updateToggle(toggle) {
  const isDark = document.body.classList.contains("dark");
  toggle.textContent = isDark ? "☀️ Light" : "🌙 Dark";
}