import { create } from 'zustand' import { persist } from 'zustand/middleware' type Theme = 'light' | 'dark' | 'system' interface ThemeState { theme: Theme setTheme: (theme: Theme) => void } function applyTheme(theme: Theme) { const root = document.documentElement if (theme === 'dark' || (theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)) { root.classList.add('dark') } else { root.classList.remove('dark') } } export const useThemeStore = create()( persist( (set) => ({ theme: 'system', setTheme: (theme) => { applyTheme(theme) set({ theme }) }, }), { name: 'cartsnitch-theme', onRehydrateStorage: () => (state) => { if (state) applyTheme(state.theme) }, }, ), )