const STORAGE_KEY = 'mc-theme';

type Theme = 'dark' | 'light';

function createThemeStore() {
	let current = $state<Theme>('dark');

	// Initialize from localStorage
	if (typeof window !== 'undefined') {
		const stored = localStorage.getItem(STORAGE_KEY);
		if (stored === 'light' || stored === 'dark') {
			current = stored;
		}
		document.documentElement.setAttribute('data-theme', current);
	}

	return {
		get current() { return current; },
		get isDark() { return current === 'dark'; },

		toggle() {
			current = current === 'dark' ? 'light' : 'dark';
			document.documentElement.setAttribute('data-theme', current);
			localStorage.setItem(STORAGE_KEY, current);
		},
	};
}

export const theme = createThemeStore();
