# Theme

```ts
import { Theme } from '@iyulab/components';
```

Manages the document theme (light / dark / system). Uses CSS custom properties and injects built-in stylesheets on init.

## Initialization

Call once at app startup before rendering any components:

```ts
await Theme.init({
  default: 'system',      // initial theme — 'system' | 'light' | 'dark'
  useBuiltIn: true,       // inject bundled CSS variables (default: true)
  store: {                // persist to storage (optional)
    type: 'localStorage',
    prefix: 'myapp-'
  },
  debug: false
});
```

## Brand accent

**Recommended — derive the whole ramp from one seed:**

```ts
Theme.accent('#7c3aed');   // computes --u-primary-color-{weakest…strong} + --u-primary-txt-color
Theme.accent(null);        // back to the sheet defaults
```

The computed ramp satisfies the contrast contract this library tests against — text on the accent
surface ≥ 4.5:1, accent text on the page background ≥ 4.5:1, `-strong` distinguishable from
`-color`, and `-weak` usable as a non-text graphic (≥ 3:1). It is **recalculated when the theme
changes**, because those targets are relative to the page background.

**Manual override** — set the steps you use, not just one:

```css
:root {
  --u-primary-color-weak: #a78bfa;    /* graphics on the page background */
  --u-primary-color: #7c3aed;         /* accent surface */
  --u-primary-color-strong: #5b21b6;  /* text/icons on the page background */
  --u-primary-txt-color: #ffffff;     /* text on the accent surface */
}
```

> ⚠ Overriding `--u-primary-color` **alone is not enough**. Hover, focus and link colors resolve
> from `--u-primary-color-strong`, so they stay on the default ramp and the brand looks
> half-applied. (The sheet derives no step from `--u-primary-color` — measured: 0 references.)

> **Load that block however you like — a plain static `import` and a plain `:root` rule are
> enough.** Since 2.0 the built-in sheets live in a cascade layer (`@layer iyu.base, iyu.house;`),
> and unlayered application CSS beats every layer regardless of load order, `Theme.init()` timing
> or selector specificity (a `:where(:root)` wrapper wins too). A house theme sits between the two
> in `@layer iyu.house { … }`.
>
> 🔴 **That includes dark mode.** The dark palette is scoped to `:root[theme="dark"]` inside
> `iyu.base`, so an unlayered `:root` override applies in both themes. Scope a mode-specific value
> yourself with `:root[theme="dark"] { … }`.

## A screen with its own theme

`Theme.override('dark')` applies a theme to the document without touching the user's preference: nothing is stored,
`get()` still answers the preference, `resolved()` and `<html theme>` answer the override. `Theme.override(null)` clears
it and returns to the preference (under `'system'`, the OS value at that moment). A preference set while an override
is on waits until it is cleared. Use it for a screen that owns its background — a floor board on a TV stays dark even
where the user chose light — and clear it when that screen goes (`@iyulab/modern-app` does both for a route with
`metadata: { theme }`).

## API

| Member | Type | Description |
|--------|------|-------------|
| `Theme.init(options?)` | `Promise<void>` | Initialize and apply theme |
| `Theme.get()` | `ThemeType \| undefined` | Get current theme |
| `Theme.set(theme)` | `void` | Set theme (`'light'`, `'dark'`, `'system'`) |
| `Theme.resolved()` | `'light' \| 'dark'` | The theme actually applied — use this, not `get()`, for brightness branches |
| `Theme.override(theme)` | `void` | Apply `'light'` / `'dark'` without changing or storing the preference; `null` returns to the preference |
| `Theme.accent(seed)` | `void` | Derive the `--u-primary-*` ramp from a brand color; `null` clears it |
| `Theme.isInitialized` | `boolean` | Whether `init()` has been called |

## Types

```ts
type ThemeType = 'system' | 'light' | 'dark';

interface ThemeInitOptions {
  debug?: boolean;
  store?: false | BrowserStorageOptions;
  default?: ThemeType;
  useBuiltIn?: boolean;
}
```
