(function() {
    try {
        const saved = localStorage.getItem("theme");
        const osPref = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
        document.documentElement.setAttribute("data-theme", saved || osPref);
    } catch (e) {
        document.documentElement.setAttribute("data-theme", "light");
    }
})();