# Dark Mode / Theme Toggle — Design Spec **Issue:** #26 **Date:** 2026-03-20 **Status:** Draft ## Summary Add a dark variant of the retro parchment theme with a three-way toggle (system / light / dark) in Settings > Preferences. The dark theme preserves the retro 8-bit character — warm dark tones, not cold grays. ## Decisions - **Toggle location:** Settings > Preferences only (no header icon) - **UI pattern:** Segmented buttons (system / light / dark), matching existing `defaultNoteMode` toggle style - **Implementation:** CSS `[data-theme="dark"]` attribute on ``, variable overrides in `app.css` - **Pixel grid texture:** Inverted to light dots (`#e8dcc8`) at 3% opacity in dark mode - **Default:** `system` (follows `prefers-color-scheme`) - **Light mode:** No `data-theme` attribute set (`:root` defaults apply). `data-theme="dark"` is only set for dark mode. This means no redundant `[data-theme="light"]` block is needed. ## 1. CSS Variable Overrides Add `--grid-dot` to the existing `:root` block and add a `[data-theme="dark"]` block after it in `app.css`: ```css :root { /* ...existing vars... */ --grid-dot: #1a1a2e; /* new — extracted from body::before */ } [data-theme="dark"] { --bg-base: #1a1715; --bg-surface: #2a2520; --text: #e8dcc8; --text-muted: #9a8e7e; --border: #e8dcc8; --border-subtle: #3a3530; --destructive: #d4604e; --error-bg: #3a2020; --error-border: #5a3030; --error-text: #e8a090; --success-bg: #203020; --success-text: #90c880; --card-shadow: 2px 2px 0px var(--border-subtle); --card-shadow-hover: 3px 3px 0px var(--primary); --grid-dot: #e8dcc8; } ``` `--primary` and `--primary-hover` remain unchanged — gold works on both backgrounds. `--card-shadow` uses `var(--border-subtle)` in both modes for consistency — the dark `--border-subtle` (#3a3530) produces the right effect. Update `body::before` to use the new variable: ```css body::before { background-image: repeating-conic-gradient(var(--grid-dot) 0% 25%, transparent 0% 50%); } ``` ## 2. Theme Preference Storage Extend the existing `UserPreferences` system: - **Type change:** Add `theme: 'system' | 'light' | 'dark'` to `UserPreferences` interface - **Default:** `'system'` - **Storage:** Same key-value `userPreferences` table + localStorage cache — no schema migration needed - **Do NOT add `theme` to `BOOLEAN_PREF_KEYS`** — it's a string enum, not a boolean Files to modify: - `src/lib/types/preferences.ts` — add `theme` field and default The existing `fromServerRecord` in `preferences.svelte.ts` does not validate string values. The `applyTheme` function (Section 4) must guard against unexpected values and fall back to `'system'` if the value is not one of the three valid options. ## 3. FOUC Prevention Add a blocking ` ``` This runs synchronously before first paint. The script validates the theme value and null-checks the meta element. ## 4. Reactive Theme Application Create `src/lib/utils/theme.svelte.ts` (`.svelte.ts` to enable Svelte 5 runes) with: - `applyTheme(theme: 'system' | 'light' | 'dark')` — resolves system preference, sets/removes `data-theme="dark"` on `document.documentElement`, updates ``. Guards against unexpected values (falls back to `'system'`). All DOM/`matchMedia` access guarded with `browser` check from `$app/environment`. - `isDarkMode` — a reactive `$state` boolean, updated by `applyTheme` and the `matchMedia` listener. Components import this to conditionally use dark color variants. Place the theme `$effect` in the **root `+layout.svelte`** (not just `(app)` layout). This ensures both `(app)` and `(auth)` layout groups get the theme applied. The root layout currently only imports CSS — this adds a small `$effect` that watches `preferences.theme` and calls `applyTheme`. It also registers a `matchMedia('(prefers-color-scheme: dark)')` change listener when in system mode, cleaned up on destroy. ## 5. Note Card Dark Colors Add a parallel `NOTE_COLORS_DARK` map in `src/lib/utils/colors.ts`: | Color | Light | Dark | |-------|-------|------| | default | `#faf5eb` | `#2a2520` | | coral | `#faafa8` | `#4a2522` | | peach | `#f39f76` | `#4a3020` | | sand | `#fff8b8` | `#3a3520` | | mint | `#e2f6d3` | `#2a3a22` | | sage | `#b4ddd3` | `#223a32` | | fog | `#d4e4ed` | `#222e3a` | | storm | `#aeccdc` | `#1e2a35` | | dusk | `#d3bfdb` | `#352540` | | blossom | `#f6e2dd` | `#3a2830` | | clay | `#e9e3d4` | `#302e28` | | chalk | `#efeff1` | `#2e2e30` | Export a helper `getNoteColor(color: NoteColor, isDark: boolean): string` that returns the correct bg hex. Components (`NoteCard.svelte`, `NoteEditor.svelte`, `ColorPicker.svelte`) import `isDarkMode` from `theme.svelte.ts` and pass it to `getNoteColor`. This is the single approach — do not use direct DOM inspection for dark mode detection. ## 6. Settings UI Add a "Theme" section to `src/routes/(app)/settings/preferences/+page.svelte` as the **first** preference (before "Default note mode"): ```svelte