Create an inline theme script
Add an inline script to the <head> of your base layout. It runs before the body renders, reads the stored preference from localStorage, and toggles the dark class on the <html> element so the page never flashes the wrong theme.
The preference is stored in localStorage under the theme key as light, dark, or system. With system, the script follows the operating system via prefers-color-scheme.
Add a mode toggle
Display the mode toggle
Place a mode toggle on your site to toggle between light and dark mode.