# Theme system

CSS custom properties are the only runtime mechanism. Every built-in theme provides the same token set, and components never branch on a theme name.

| Covers | Leaves alone |
| --- | --- |
| Appearance and Font in Settings | Component dimensions, layout, interaction |
| First frame, React hydration, system colour-scheme changes | Saved user CSS overlays (never auto-rewritten beyond the selector migration) |
| Main window, desktop overlays, new windows | Business logic |
| Surfaces, text, borders, states, focus, composer, DAG | Syntax highlighting, website content |

## From setting to component

1. **Select.** Appearance: Mode (Auto, Light, Dark), theme pack, accent colour, CSS overlay. Font options come from `font-pref.ts`.
2. **Persist.** Schema 3: `agentic_theme_style` + `agentic_theme_mode`; `agentic_theme_accent`; `agentic_custom_css_enabled`; `agentic_tabrow_color`. Fonts also keep a cookie for the first frame.
3. **Apply.** The first-frame script and hook set `data-theme`, `data-custom-css`, accent tokens and `--font-sans`.
4. **Resolve.** The pack's token file. Accent overrides three tokens; the overlay sits on top.
5. **Consume.** The Tailwind bridge, CSS modules and desktop overlays read tokens.

Entry points: `apps/web/lib/prefs/appearance/theme-pref.ts`, `apps/web/app/globals.css`, `apps/web/app/styles/themes/`.

## Theme packs

| Pack | `data-theme` | `--bg-primary` | `--accent-fill` | Primary |
| --- | --- | --- | --- | --- |
| Beige | `beige-dark` `beige-light` | `#191918` `#fbfaf8` | `#d97757` `#c15f3c` | Orange |
| Neutral | `dark` `light` | `#1e1e20` `#ffffff` | `#3b82f6` `#2563eb` | Blue |
| Aurora | `aurora` `aurora-light` | `#171528` `#fbfaff` | `#35b8a4` `#0f766e` | Teal, dark foreground on fills |
| Vivid | `vivid` `vivid-light` | `oklch` tones of `--tabrow-seed` | `oklch` tones of `--tabrow-seed` | Follows the tab-strip colour |

Vivid has Beige's panel layout. The strip colour is its own setting from Chrome's colour set, a pale tone of it in light mode. `custom` and `custom-light` remain as fallback ids and CSS files for old snippets, not as cards; `custom-light.css` matches beige-light.

Vivid's default seed (`#3d475c`) is in `themes/fallback.css`, so an application frame that loads only the themes still gets Vivid's colours.

## Token contract

Theme files give values, components define use, and one purpose has one token name in every theme.

| Group | Tokens |
| --- | --- |
| Surface | `--bg-primary/secondary/tertiary` `--bg-input/hover/selected` `--surface-popover/tooltip` |
| Text | `--text-bright/primary/secondary/muted` `--nav-color/hover` |
| Boundary, depth | `--border` `--border-light` `--border-popover` `--shadow-*` `--scrim-*` |
| Action, status | `--theme-accent` `--theme-accent-fill/fill-hover` `--accent-green/red/yellow` `--success-soft` `--warning-soft` `--danger-soft` |
| Interaction | `--selection-bg` `--focus-ring` `--chip-bg/ring` `--meter-fill/track` |
| Component roles | `--composer-surface/shadow` `--provider-icon-bg` `--dag-ghost` |

| Setting | Sets | Reaches | Never |
| --- | --- | --- | --- |
| Appearance | `data-theme` | Backgrounds, text, borders, shadows, states, tab focus, composer, popovers, DAG, desktop overlays | Component-level `[data-theme="light"]` |
| Accent | `--accent-orange` `--accent-fill` `--accent-orange-hover` | Primary actions, selection borders, link colour on desktop error pages and directory listings. Empty means the pack's default. | A fourth accent token |
| Font | `--font-sans` | body, button, input, select, textarea, optgroup, except explicit mono regions | System font stacks in ordinary components |
| CSS overlay | `#user-custom-css` + `data-custom-css` | Sits on the current pack, behind the Enable switch; unset values come from the pack. Old `[data-theme=custom]` selectors are rewritten to `data-custom-css`. | A separate skin card |

**Focus, active tab, pages in the centre card**

| Aspect | Rule |
| --- | --- |
| Text entry, native select | One existing border in the accent colour on focus: no outline, offset ring, box-shadow or size change. Search and composer wrappers own the border; their inner editor has none. Navigation, checkboxes, radios, sliders and embedded third-party surfaces keep their own keyboard focus. |
| Active desktop tab | In dark mode `--tab-active-bg` mixes the content background with 12% white, for ordinary tabs and split groups, stable on hover. Light mode and browser tabs use the content background. |
| Pages in the centre card | The shell marks non-chat routes (`.page-col`) and panes (`[data-page-surface]`). A theme may restate `--bg-secondary`, `--bg-tertiary` and their derivatives for those scopes: beige-light uses `#fbfaf8` and `#f3f1ec`, so pages read like the chat while the frame, rails and chat pane keep theme values. |
| Desktop window | All theme ids pass unchanged to overlays; the window background is the pack's `--bg-primary` even with an accent override. |

**Diagnosing intermittent theme switches**

| Aspect | Behaviour |
| --- | --- |
| Desktop source of truth | A complete valid `theme-prefs.json` from the main UI. Legacy browser-storage migration runs only when the file is missing or its mode or style is invalid. Raw browser-database scans cannot find the latest record, so they never override it or restore a cleared accent. |
| After load | The web page's localStorage is the authority and syncs the desktop cache |
| What Auto follows | In the desktop App, the OS setting as the main process reads it (`theme.systemDark`, pushed on change). A debugger attached to the page can override `prefers-color-scheme`, and Playwright forces it to light on every page it adopts, so the App does not read it. In a browser, the media query. |
| Event log | `theme-events.json` under desktop userData, at most 64 records: message sends, first-screen init, settings changes, system colour events, cross-window sync, HTML attribute changes |
| Log contents | Timestamps, fixed event names, theme enums, system light or dark. No chat content, URLs, CSS or user paths. Write failures do not affect rendering. |
| Limit | The log identifies a switch's source; it does not establish that an unknown trigger is fixed |

**Options compared**

| Option | Use | Why, and what it lacks |
| --- | --- | --- |
| CSS custom properties + `data-theme` | Adopted | Cascades through Web, CSS modules, Tailwind arbitrary values and same-origin overlays. Cannot catch a missing token, hence the static check. |
| Tailwind `@theme` | Bridge only | Names tokens as utilities without a second set of values. Cannot generate imports; a script aligns theme files with TS ids. |
| JS theme object, Context | Rejected | Duplicates values; misses native CSS, the first frame and Electron overlays |
| Component `[data-theme]` branches | Rejected | Each new theme would miss edits |
| `light-dark()` | Rejected | Expresses only light and dark, not named packs |

Basis: [CSS Custom Properties Level 1](https://www.w3.org/TR/css-variables-1/), [CSS Color Adjustment color-scheme](https://www.w3.org/TR/css-color-adjust-1/#color-scheme-prop).

## Acceptance

| Area | Requirement |
| --- | --- |
| Settings | Lists Beige, Neutral, Aurora and Vivid |
| Web checks | `apps/web/scripts/settings/check-theme-contract.mjs` keeps CSS imports, theme files and allowed overlay values in agreement and every built-in theme on one token set |
| Component CSS | Outside theme files, no branch on a built-in theme name |
| Fonts | Ordinary text and native form controls inherit one `--font-sans` |
| Accents | Pack defaults come from the CSS files |
| Installed App | Inspect the main window, tabs, composer, popovers and overlays in dark, light and aurora |
| Windows tests | Node's `fileURLToPath` converts module URLs, avoiding an extra slash before drive paths |

## Implementation status

Implemented: Mode × theme pack × accent × overlay, with `THEME_STYLE_PAIRS` as the light and dark matrix. Stored `custom` and `custom-light` migrate to beige, and saved CSS turns the overlay on. Composer, provider icons and DAG use role tokens only. Desktop first-paint details: [Desktop chrome](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/visual-system/unification-work.html#desktop-chrome).
