// APPLY THEME SAAT LOAD (biar gak reset)
(function () {
  const theme = localStorage.getItem("theme");

  if (theme === "dark") {
    document.documentElement.classList.add("dark");
    document.body.classList.add("dark");
  }
})();

// TOGGLE DARK MODE
function toggleDark(){
  const isDark = document.body.classList.toggle("dark");

  // sync ke html juga
  document.documentElement.classList.toggle("dark", isDark);

  // icon
  const icon = document.getElementById("icon");
  if (icon) {
    icon.innerHTML = isDark ? "🌙" : "☀️";
  }

  // simpan
  localStorage.setItem("theme", isDark ? "dark" : "light");
}

// SYNC SAAT LOAD
window.addEventListener("DOMContentLoaded", () => {
  const icon = document.getElementById("icon");

  const isDark =
    document.body.classList.contains("dark") ||
    document.documentElement.classList.contains("dark");

  if (isDark) {
    document.body.classList.add("dark");
    if (icon) icon.innerHTML = "🌙";
  } else {
    if (icon) icon.innerHTML = "☀️";
  }
});