import { createContext, useContext, useEffect, useState } from 'react'; const ThemeContext = createContext(); function readThemeFromStorage() { if (typeof window === 'undefined') return 'light'; return localStorage.getItem('theme') || 'light'; } export function ThemeProvider({ children }) { const [theme, setTheme] = useState(readThemeFromStorage); useEffect(() => { document.documentElement.setAttribute('data-theme', theme); }, [theme]); const toggleTheme = () => { const newTheme = theme === 'light' ? 'dark' : 'light'; setTheme(newTheme); localStorage.setItem('theme', newTheme); document.documentElement.setAttribute('data-theme', newTheme); }; return ( {children} ); } export function useTheme() { const context = useContext(ThemeContext); if (context === undefined) { throw new Error('useTheme must be used within a ThemeProvider'); } return context; }