# Themes

A theme is one JSON document that sets how AFUI pages and native renderers
look: colors, fonts, text styles, spacing, shapes, control sizes, and the
layout of collections and forms. Every renderer reads the same resolved
result, so a theme changes every surface without touching a panel or a
renderer.

## The model

A theme is a partial assignment: every field may be left out.

```json
{
  "density": "compact",
  "colors": {"inputs": {"accent": "#7a3cff"}},
  "font": {"ui": "Inter, sans-serif"},
  "control": {"regular": {"height_pt": 40}},
  "shape": {"control": "rounded", "radius": {"control_pt": 6}}
}
```

Themes combine in one way only: `overlay(lower, upper)` takes every field
`upper` writes over the same field of `lower`. It is associative, so a host
can stack a workspace theme on a profile theme on the baseline in any
grouping.

`Theme::baseline()` is the AFUI baseline, a theme with every field written.
The baseline stylesheet's `:root` values are generated from it.

Resolving a theme is `overlay(density(baseline), theme)` for one color
scheme, with each group then given one of two states.

### Groups and the platform

The parameters fall into six groups: `text`, `space`, `shape`, `control`,
`collection` and `form`. After resolution each group is in one of two
states:

- `platform`: the theme wrote no field of the group and no density touches
  it. A native renderer draws the group with the system's own controls and
  text styles; a page uses the AFUI baseline.
- `resolved`: the theme wrote at least one field of the group, or it wrote a
  `density` (which resolves `space` and `control`). Every value of the group
  is given; a field the theme left out takes the baseline's value, after
  density, never the platform's.

There is no state between the two. An empty theme looks native on every
platform; a full theme looks the same on every platform.

Two kinds of value sit outside the groups. Colors always resolve to every
role. `font.ui` and `font.mono` are independent font family lists; an
omitted one is the platform font and does not change any group's state.

### Density

`density` is `compact`, `regular` or `comfortable`. It scales the `_pt`
lengths of `space` and `control` in the baseline by 0.85, 1 or 1.15 and
rounds them to the nearest half point, before the theme's own values apply.
A theme that writes every field resolves the same at every density.

### Units

A parameter's name carries its unit. `_pt` is a length in logical points:
one CSS pixel at the default text size, one native point. `_ratio` is a
multiplier, `_percent` a percentage, `_lines` a line count; enumerations
have no suffix. The JSON holds numbers only: there are no references, aliases
or spacing scales. The one name that points elsewhere is `control.<size>.text`,
which names a text role.

### Diagnostics

Resolution reports contradictions that hold for every renderer, and keeps
going:

- a length below zero (`negative_length`);
- a spacing or a control height that is zero after density
  (`not_positive`);
- a control lower than one line of the text role it uses, when both `text`
  and `control` are resolved (`control_shorter_than_text`);
- a control's minimum width below twice its horizontal padding
  (`min_width_below_padding`);
- a line count below one (`lines_below_one`);
- the color diagnostics: unmet contrast, surface order, neutral tint and
  unknown pins (`color`).

The minimum hit area under a coarse pointer is a rule of the renderer
contract, not a diagnostic: a renderer extends the hit area beyond what it
draws, whatever the theme says.

## Parameters

Each row is one parameter, with the CSS variable a page reads it from and
its AFUI baseline. A page gets lengths in `rem` (the value over 16, so the
browser's text size setting still applies), except `border.*`, `focus.*` and
`elevation.*`, which are pixels. A control's `text` becomes the CSS `font`
of its role, and `shape.control` becomes the corner radius it implies.

<!-- BEGIN GENERATED theme-params: scripts/test.sh generate -->
| Group | Parameter | CSS variable | Unit | Baseline | Use |
|---|---|---|---|---|---|
| `independent` | `font.ui` | `--afui-font-ui` | font family list | `-apple-system` | Interface font family; omitted means the platform font |
| `independent` | `font.mono` | `--afui-font-mono` | font family list | `ui-monospace` | Monospaced font family; omitted means the platform font |
| `text` | `text.scale_ratio` | `--afui-text-scale` | ratio | 1 | Multiplies every text size |
| `text` | `text.title.size_pt` | `--afui-text-title-size` | points | 28 | Page and view titles |
| `text` | `text.title.weight` | `--afui-text-title-weight` | `regular` `medium` `semibold` `bold` | `bold` | Page and view titles |
| `text` | `text.title.line_height_ratio` | `--afui-text-title-line-height` | ratio | 1.25 | Page and view titles |
| `text` | `text.title2.size_pt` | `--afui-text-title2-size` | points | 22 | Second-level headings |
| `text` | `text.title2.weight` | `--afui-text-title2-weight` | `regular` `medium` `semibold` `bold` | `bold` | Second-level headings |
| `text` | `text.title2.line_height_ratio` | `--afui-text-title2-line-height` | ratio | 1.25 | Second-level headings |
| `text` | `text.title3.size_pt` | `--afui-text-title3-size` | points | 18 | Third-level headings and dialog titles |
| `text` | `text.title3.weight` | `--afui-text-title3-weight` | `regular` `medium` `semibold` `bold` | `semibold` | Third-level headings and dialog titles |
| `text` | `text.title3.line_height_ratio` | `--afui-text-title3-line-height` | ratio | 1.3 | Third-level headings and dialog titles |
| `text` | `text.headline.size_pt` | `--afui-text-headline-size` | points | 15 | Entry titles, section titles and emphasized labels |
| `text` | `text.headline.weight` | `--afui-text-headline-weight` | `regular` `medium` `semibold` `bold` | `semibold` | Entry titles, section titles and emphasized labels |
| `text` | `text.headline.line_height_ratio` | `--afui-text-headline-line-height` | ratio | 1.5 | Entry titles, section titles and emphasized labels |
| `text` | `text.body.size_pt` | `--afui-text-body-size` | points | 15 | Running text |
| `text` | `text.body.weight` | `--afui-text-body-weight` | `regular` `medium` `semibold` `bold` | `regular` | Running text |
| `text` | `text.body.line_height_ratio` | `--afui-text-body-line-height` | ratio | 1.5 | Running text |
| `text` | `text.callout.size_pt` | `--afui-text-callout-size` | points | 14 | Descriptions, hints and secondary lines |
| `text` | `text.callout.weight` | `--afui-text-callout-weight` | `regular` `medium` `semibold` `bold` | `regular` | Descriptions, hints and secondary lines |
| `text` | `text.callout.line_height_ratio` | `--afui-text-callout-line-height` | ratio | 1.5 | Descriptions, hints and secondary lines |
| `text` | `text.caption.size_pt` | `--afui-text-caption-size` | points | 12.8 | Small annotations |
| `text` | `text.caption.weight` | `--afui-text-caption-weight` | `regular` `medium` `semibold` `bold` | `regular` | Small annotations |
| `text` | `text.caption.line_height_ratio` | `--afui-text-caption-line-height` | ratio | 1.4 | Small annotations |
| `text` | `text.label.size_pt` | `--afui-text-label-size` | points | 14.4 | Form field labels |
| `text` | `text.label.weight` | `--afui-text-label-weight` | `regular` `medium` `semibold` `bold` | `semibold` | Form field labels |
| `text` | `text.label.line_height_ratio` | `--afui-text-label-line-height` | ratio | 1.5 | Form field labels |
| `text` | `text.mono.size_pt` | `--afui-text-mono-size` | points | 15 | Code and fixed-width values |
| `text` | `text.mono.weight` | `--afui-text-mono-weight` | `regular` `medium` `semibold` `bold` | `regular` | Code and fixed-width values |
| `text` | `text.mono.line_height_ratio` | `--afui-text-mono-line-height` | ratio | 1.5 | Code and fixed-width values |
| `space` | `space.page_pt` | `--afui-space-page` | points | 16 | Inset of a panel's content from its edges |
| `space` | `space.segment_pt` | `--afui-space-segment` | points | 24 | Gap between the segments of a segmented panel |
| `space` | `space.section_pt` | `--afui-space-section` | points | 16 | Gap between sections, and a section's own inset |
| `space` | `space.field_pt` | `--afui-space-field` | points | 12 | Gap between the fields of a form |
| `space` | `space.grid_pt` | `--afui-space-grid` | points | 12 | Gap between and around the tiles of a grid |
| `space` | `space.row_pt` | `--afui-space-row` | points | 12 | Horizontal inset of a row's content |
| `space` | `space.inline_pt` | `--afui-space-inline` | points | 8 | Gap between items on one line: an icon and its label, buttons in a group |
| `space` | `space.stack_pt` | `--afui-space-stack` | points | 4 | Gap between closely related lines, such as a title and its description |
| `space` | `space.label_pt` | `--afui-space-label` | points | 6.4 | Gap between a field's label and its control |
| `space` | `space.inset_pt` | `--afui-space-inset` | points | 12 | Inset inside a small framed box: a callout, a code block, a menu |
| `space` | `space.page_max_width_pt` | `--afui-space-page-max-width` | points | 1056 | Widest a panel's content column is drawn |
| `shape` | `radius.control_pt` | `--afui-radius-control` | points | 8 | Corner radius of buttons and menu items when controls are rounded |
| `shape` | `radius.field_pt` | `--afui-radius-field` | points | 12 | Corner radius of text fields, pickers and choice cards |
| `shape` | `radius.tile_pt` | `--afui-radius-tile` | points | 12 | Corner radius of tiles and form sections |
| `shape` | `radius.group_pt` | `--afui-radius-group` | points | 16 | Corner radius of cards, dialogs and the panel surface |
| `shape` | `radius.icon_pt` | `--afui-radius-icon` | points | 8 | Corner radius of icon tiles and media thumbnails |
| `shape` | `shape.control` | `--afui-shape-control` | `rounded` `capsule` | `capsule` | Outline of buttons, chips and pickers: rounded or capsule |
| `shape` | `border.width_pt` | `--afui-border-width` | points | 1 | Width of borders and dividers |
| `shape` | `border.strong_width_pt` | `--afui-border-strong-width` | points | 3 | Width of emphasized borders and the selection indicator |
| `shape` | `focus.width_pt` | `--afui-focus-width` | points | 3 | Width of the keyboard focus ring |
| `shape` | `focus.offset_pt` | `--afui-focus-offset` | points | 2 | Gap between an element and its focus ring |
| `shape` | `elevation.1.y_pt` | `--afui-elevation-1-y` | points | 8 | Raised surfaces |
| `shape` | `elevation.1.blur_pt` | `--afui-elevation-1-blur` | points | 24 | Raised surfaces |
| `shape` | `elevation.1.opacity_percent` | `--afui-elevation-1-opacity` | percent | 8 | Shadow opacity of ink; a dark scheme draws no shadow |
| `shape` | `elevation.2.y_pt` | `--afui-elevation-2-y` | points | 10 | Floating menus and popovers |
| `shape` | `elevation.2.blur_pt` | `--afui-elevation-2-blur` | points | 32 | Floating menus and popovers |
| `shape` | `elevation.2.opacity_percent` | `--afui-elevation-2-opacity` | percent | 22 | Shadow opacity of ink; a dark scheme draws no shadow |
| `control` | `control.small.height_pt` | `--afui-control-small-height` | points | 36 | Height of a control of this size |
| `control` | `control.small.padding_x_pt` | `--afui-control-small-padding-x` | points | 12.8 | Space between a control's edge and its content |
| `control` | `control.small.min_width_pt` | `--afui-control-small-min-width` | points | 36 | Narrowest a control of this size is drawn |
| `control` | `control.small.icon_pt` | `--afui-control-small-icon` | points | 16 | Icon size inside a control of this size |
| `control` | `control.small.text` | `--afui-control-small-text` | text role | `headline` | Text role of a control's label |
| `control` | `control.regular.height_pt` | `--afui-control-regular-height` | points | 44 | Height of a control of this size |
| `control` | `control.regular.padding_x_pt` | `--afui-control-regular-padding-x` | points | 16 | Space between a control's edge and its content |
| `control` | `control.regular.min_width_pt` | `--afui-control-regular-min-width` | points | 44 | Narrowest a control of this size is drawn |
| `control` | `control.regular.icon_pt` | `--afui-control-regular-icon` | points | 20 | Icon size inside a control of this size |
| `control` | `control.regular.text` | `--afui-control-regular-text` | text role | `headline` | Text role of a control's label |
| `control` | `control.large.height_pt` | `--afui-control-large-height` | points | 52 | Height of a control of this size |
| `control` | `control.large.padding_x_pt` | `--afui-control-large-padding-x` | points | 24 | Space between a control's edge and its content |
| `control` | `control.large.min_width_pt` | `--afui-control-large-min-width` | points | 52 | Narrowest a control of this size is drawn |
| `control` | `control.large.icon_pt` | `--afui-control-large-icon` | points | 24 | Icon size inside a control of this size |
| `control` | `control.large.text` | `--afui-control-large-text` | text role | `headline` | Text role of a control's label |
| `control` | `control.number_width_pt` | `--afui-control-number-width` | points | 96 | Width of a number input |
| `control` | `control.disabled_opacity_percent` | `--afui-control-disabled-opacity` | percent | 55 | How opaque a control is drawn while it is out of service |
| `control` | `control.menu_min_width_pt` | `--afui-control-menu-min-width` | points | 176 | Narrowest a menu is drawn |
| `control` | `control.menu_max_width_pt` | `--afui-control-menu-max-width` | points | 352 | Widest a menu is drawn |
| `collection` | `collection.row_min_height_pt` | `--afui-collection-row-min-height` | points | 52 | Shortest a list row is drawn |
| `collection` | `collection.media_column_pt` | `--afui-collection-media-column` | points | 48 | Size of a row's media |
| `collection` | `collection.icon_tile_pt` | `--afui-collection-icon-tile` | points | 32 | Size of the icon of an icon tile |
| `collection` | `collection.tile_min_width_pt` | `--afui-collection-tile-min-width` | points | 192 | Narrowest a grid tile is drawn |
| `collection` | `collection.rail_pt` | `--afui-collection-rail` | points | 40 | Width of the column that holds an entry's more button |
| `collection` | `collection.title_lines` | `--afui-collection-title` | lines | 2 | Lines a tile's title takes before it is cut |
| `collection` | `collection.description_lines` | `--afui-collection-description` | lines | 2 | Lines a tile's description takes before it is cut |
| `collection` | `collection.media_aspect_ratio` | `--afui-collection-media-aspect` | ratio | 1.3333 | Width over height of a tile's picture |
| `collection` | `collection.avatar_pt` | `--afui-collection-avatar` | points | 40 | Size of a standalone icon and an avatar picture |
| `collection` | `collection.media_min_height_pt` | `--afui-collection-media-min-height` | points | 144 | Shortest a video is drawn |
| `collection` | `collection.media_max_height_pt` | `--afui-collection-media-max-height` | points | 192 | Tallest a banner picture is drawn |
| `collection` | `collection.trailing_max_width_pt` | `--afui-collection-trailing-max-width` | points | 160 | Widest a row's trailing status is drawn |
| `collection` | `collection.cell_max_width_pt` | `--afui-collection-cell-max-width` | points | 336 | Widest a table cell is drawn before its text wraps |
| `collection` | `collection.bar_height_pt` | `--afui-collection-bar-height` | points | 48 | Height of a panel's toolbar |
| `collection` | `collection.bar_threshold_pt` | `--afui-collection-bar-threshold` | points | 544 | Toolbar width below which a button with an icon shows only the icon |
| `collection` | `collection.narrow_threshold_pt` | `--afui-collection-narrow-threshold` | points | 640 | Panel width below which tables, fact lists and buttons take their narrow layout |
| `form` | `form.label_min_width_pt` | `--afui-form-label-min-width` | points | 128 | Narrowest the label column of a wide form is drawn |
| `form` | `form.label_max_width_pt` | `--afui-form-label-max-width` | points | 224 | Widest the label column of a wide form is drawn |
| `form` | `form.label_width_percent` | `--afui-form-label-width` | percent | 30 | Share of a wide form's width the label column takes between its bounds |
| `form` | `form.wide_threshold_pt` | `--afui-form-wide-threshold` | points | 544 | Form width from which labels sit beside their controls |
| `form` | `form.dialog_width_pt` | `--afui-form-dialog-width` | points | 576 | Widest a dialog is drawn |
<!-- END GENERATED theme-params -->

## The resolved theme

`resolve_theme(&theme, scheme)` returns a `ResolvedTheme`. Its JSON is the
canonical exit, read by renderers that do not link this crate:

```json
{
  "scheme": "light",
  "colors": {"opaque": {"page": "#f3f7fc"}, "overlays": {"fill": "#484b4f12"}},
  "font": {"ui": null, "mono": null},
  "groups": {
    "text": {"state": "platform"},
    "control": {"state": "resolved", "values": {"regular": {"height_pt": 44}}}
  },
  "diagnostics": []
}
```

Colors are `#rrggbb` and `#rrggbbaa`; overlays keep their alpha. A dark
scheme resolves every elevation's opacity to zero: dark surfaces draw no
shadow. The vocabulary in
`tests/fixtures/view/theme-conformance/params.json` lists every parameter
with its JSON path, CSS variable and baseline.

`ResolvedTheme::css_tokens()` is the CSS projection: every color role, each
font family that is set, and every parameter of every resolved group. A
group left to the platform writes no variable, so a page falls back to its
baseline. A host hands the projection to a page with `afui.setTheme`, which
replaces the whole host theme each time it is called.

## On the command line

```sh
afui theme css theme.json --output json | jq -r '.result.css' > theme.css
afui theme resolve theme.json --scheme dark --output json
```

`theme css` writes the light and dark variables of every resolved group
with the diagnostics of both schemes; load its CSS after the baseline
stylesheet. `theme resolve` writes the resolved theme. A file that is not
JSON fails with `theme_invalid_json`, a field of the wrong structure or type
with `theme_invalid` and its path, and an unreadable file with
`theme_unreadable`. Diagnostics are not failures.

## Choosing color roles

| Use | Roles |
|---|---|
| Page, raised panel, and distinct input or header surface | `--afui-page`, `--afui-panel`, `--afui-panel-strong` |
| Body and secondary text | `--afui-ink`, `--afui-muted` |
| Decorative separator, control border, and keyboard focus | `--afui-line`, `--afui-line-strong`, `--afui-focus` |
| A solid fill with text or icons over it | `--afui-X` with `--afui-on-X` |
| A tinted surface with semantic text, icons, or borders | `--afui-X-bg` with `--afui-X-ink` |
| Hover and press | `--afui-fill`, `--afui-fill-strong` |
| Selection and the current item within a selection | `--afui-accent-fill`, `--afui-accent-fill-strong` |

`X` is one of `accent`, `info`, `warn`, `danger`, or `ok`. Use
`--afui-X-ink` for semantic foregrounds on neutral surfaces as well. State
overlays stay translucent so the surface beneath remains visible. Use focus
rings, labels, and other visible cues when interaction or status must remain
identifiable without color.

Set a color identity in `colors.inputs` to generate coordinated roles for
both schemes. A pin in `colors.pins.light` or `colors.pins.dark` fixes one
role in one scheme; the other roles are solved with it in place. Use
`role_kind(name)` to classify a pin name as `RoleKind::Opaque` or
`RoleKind::Overlay`. Rust hosts can call `theme::derive_color_family` for a
color supplied by application data.
