export type Theme = "light" | "dark"; const KEY = "margincal-theme"; export function initialTheme(): Theme { const attr = document.documentElement.getAttribute("data-theme"); if (attr === "light" || attr === "dark") return attr; const saved = localStorage.getItem(KEY); if (saved === "light" || saved === "dark") return saved; return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; } export function applyTheme(theme: Theme) { document.documentElement.setAttribute("data-theme", theme); localStorage.setItem(KEY, theme); }