// The colour theme of an app (plan step 2.6f): light | dark | system (follows the OS). // tokens.css reads it from data-theme on . The choice is per device (localStorage), // like a screen setting, not per user. No React here. // // applyTheme(getStoredTheme()) at startup // applyTheme('dark', { save: true }) when the user picks one // // The manager offers the choice only once its older pages use the tokens (phase 6): until // then it always applies 'light' (decided 2026-11-14), so a saved choice waits unused. export const THEMES = ['light', 'dark', 'system'] export const THEME_STORAGE_KEY = 'xenia_theme' const browserStorage = () => { try { return globalThis.localStorage ?? null } catch { return null } } /** The saved theme, or 'light'. */ export function getStoredTheme(storage = browserStorage()) { try { const v = storage?.getItem(THEME_STORAGE_KEY) return THEMES.includes(v) ? v : 'light' } catch { return 'light' } } /** Set data-theme on the root element (and save it when asked). Unknown values become 'light'. */ export function applyTheme(theme, { root = globalThis.document?.documentElement, storage = browserStorage(), save = false } = {}) { const t = THEMES.includes(theme) ? theme : 'light' root?.setAttribute('data-theme', t) if (save) { try { storage?.setItem(THEME_STORAGE_KEY, t) } catch { /* private mode: not remembered */ } } return t }