// Set initial theme on page load
(function() {
  const isDark = localStorage.theme === "dark" ||
    (!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches);
  document.documentElement.classList.toggle("dark", isDark);

  // Update label on load
  const label = document.getElementById("darkmode-label");
  if (label) label.textContent = !isDark ? "Dark" : "Light";
})();

// Toggle dark mode and update label/localStorage
function toggleDarkMode() {
  const html = document.documentElement;
  const isDark = html.classList.toggle("dark");
  localStorage.theme = isDark ? "dark" : "light";
  const label = document.getElementById("darkmode-label");
  if (label) label.textContent = !isDark ? "Dark" : "Light";
}