"use client"; import { useEffect, useState } from "react"; export default function ThemeToggle() { const [theme, setTheme] = useState("dark"); const isDark = theme === "dark"; useEffect(() => { if (typeof window === "undefined") return; try { const stored = window.localStorage.getItem("theme"); if (stored === "dark" || stored === "light") { setTheme(stored); document.documentElement.classList.toggle("dark", stored === "dark"); } else { // Default to dark theme for a forensic look if no preference stored const initial = "dark"; setTheme(initial); document.documentElement.classList.toggle("dark", initial === "dark"); } } catch (e) { // ignore } }, []); const toggle = () => { const next = isDark ? "light" : "dark"; setTheme(next); if (typeof window !== "undefined") { try { window.localStorage.setItem("theme", next); } catch (e) {} } if (typeof document !== "undefined") { document.documentElement.classList.toggle("dark", next === "dark"); } }; return ( ); }