window.tmTheme = {
    _key: 'tm_theme',
    apply(theme) {
        const t = theme === 'dark' ? 'dark' : 'light';
        document.documentElement.setAttribute('data-theme', t);
        try { localStorage.setItem(this._key, t); } catch { }
    },
    get() {
        try { return localStorage.getItem(this._key) || 'light'; } catch { return 'light'; }
    },
    toggle() {
        const cur = this.get();
        this.apply(cur === 'dark' ? 'light' : 'dark');
        return this.get();
    }
};
