(() => { const storageKey = "joeavanzato-theme"; const root = document.documentElement; let theme = "dark"; try { if (localStorage.getItem(storageKey) === "light") theme = "light"; } catch { // The switch still works when storage is unavailable. } root.dataset.theme = theme; root.classList.add("js"); // shows the header buttons from first paint, no layout shift document.addEventListener("DOMContentLoaded", () => { const toggle = document.querySelector(".theme-toggle"); if (!toggle) return; function applyTheme() { root.dataset.theme = theme; const next = theme === "dark" ? "light" : "dark"; toggle.setAttribute("aria-label", `Switch to ${next} theme`); toggle.title = toggle.getAttribute("aria-label"); } toggle.addEventListener("click", () => { theme = theme === "dark" ? "light" : "dark"; applyTheme(); try { localStorage.setItem(storageKey, theme); } catch { // Keep the selected theme on this page. } }); applyTheme(); }); })();