<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: docs/agents/ in the magma monorepo.
  Regenerate with: npm run sync.agent-docs (from the stencil package).
-->
# Magma styles - theming, dark mode and preferences

> Scope: dark mode, the user preferences (contrast, motion, consumption), the named
> themes, the surface levels and elevation, the global design decisions you may override,
> and the corner geometry axis. Requires the styles setup in [`assets.md`](assets.md).
> The color roles themselves: [`color.md`](color.md).

The component layer already handles these - **do not re-implement them in app code**.

## Dark mode and preferences

Handled at the palette level - no per-element classes. Components read tokens, not
literal colours, so they invert automatically. Activate on `<html>`:

```html
<html class="pref-mode-system"><!-- or pref-mode-light / pref-mode-dark --></html>
```

Same pattern for `pref-contrast-*`, `pref-animation-*` and `pref-consumption-*` (high
contrast, reduced motion, low consumption): the classes on `<html>` cascade through. To
let the user choose, each preference has its component - `mds-pref-mode`,
`mds-pref-contrast`, `mds-pref-animation`, `mds-pref-consumption`, `mds-pref-theme`,
`mds-pref-language` - and `mds-pref` groups them in one panel; they write the classes and
remember the choice. Never write `color-scheme` or dark-mode media queries by hand.

Tailwind's `dark:` follows the same mode: the Magma Tailwind theme brings a `dark`
variant that turns on where the palette turns dark - `pref-mode-dark`, `pref-mode-system`
or no controller on a dark OS, a theme locked to dark (`pref-theme-scheme-dark`) - and
never under `pref-mode-light` or a theme locked to light (`pref-theme-scheme-light`). The
roles flip without it, so `dark:` is never needed to correct a colour: use it only for a
deliberate per-mode choice. It matches the light DOM only (pages, layouts, stories);
inside a component's shadow root it matches nothing.

## Named themes

A named theme retints the whole neutral scaffolding of the page at once - surfaces,
wash, borders, text, the inverse chip - and the accent, and keeps it contrast-checked.
It is one attribute on `<html>`, and it needs `themes.css` ([`assets.md`](assets.md)):

```html
<html data-theme-name="editorial"><!-- no attribute = business --></html>
```

| Theme | For | Family | Accent |
| --- | --- | --- | --- |
| `business` (no attribute) | the management software | `neutral`, grey | the Maggioli brand |
| `editorial` | the publishing side | `bisque`, warm grey | the Maggioli Editore brand |

No `data-theme-name` means `business`: it is the base `:root` and emits no rule.
`default` (its old name), `cool` and `warm` still work and are deprecated: they leave at
the next major.

The status colors (info, success, warning, danger) never change with the theme. To let
the user choose, use `mds-pref-theme`: it writes the attribute, remembers the choice and
also carries the corner shape (below). Each `mds-pref-theme-item` shows the theme it
names, from the preview every theme publishes on the page (`--magma-preview-<name>-wash`,
`--magma-preview-<name>-accent`). A theme works only on code that paints with the
semantic roles; a raw palette step stays as it is. Defining your own theme is not a
consumer feature yet.

The studio of Magma shows the themes on a page of a product, in light and dark mode,
with every contrast checked: its Theme page is
<https://magma-design-system.github.io/magma/studio/#/theme>. Its address is `studio` at
the top of `documentation.json` (a page of the studio is that address and its hash).

A theme can be made for one colour mode only (`color.modes` of its file; `business` and
`editorial` take both). `mds-pref-theme` applies such a theme in its mode, whatever the
person picked, and `mds-pref-mode` shows that mode with its other choices locked; the
person's own choice comes back with a theme of both modes. A page that writes
`data-theme-name` by hand, with no `mds-pref-theme`, writes the lock too:
`pref-theme-scheme-dark` or `pref-theme-scheme-light` on `<html>`.

## Surfaces and elevation

Every neutral background is one of five surface levels. They are not five greys to pick
from: each one says where the element sits relative to the page. When an element gets a
surface at all (a card is an item of a set, the groups of one subject are bare sections, a
band is drawn only by a component that stays put) is the domain
[`surfaces`](patterns/surfaces.md).

```text
surface-default          the page, the canvas everything else sits on
  surface-sunken         recessed into the page: wells, insets, code blocks, tracks
  surface-muted          on the page plane, grouped, inside components: zebra rows
  surface-raised         lifted off the page: cards, panels, the bars that stay put
    surface-overlay      floating above everything: modal, dropdown, popover, tooltip
```

The ladder in the default theme (`--magma-surface-*`, Tailwind `bg-surface-*`):

| Level | Light | Dark |
| --- | --- | --- |
| `sunken` | `#e8e8e8` | `#020202` |
| `muted` | `#ebebeb` | `#0d0d0d` |
| `default` | `#f2f2f2` | `#1d1d1d` |
| `raised` | `#f8f8f8` | `#303030` |
| `overlay` | `#ffffff` | `#4a4a4a` |

In both modes the ladder rises toward the light: `sunken` and `muted` sit below the page,
`raised` and `overlay` above it. Light keeps the page a soft grey so that a card can be
lighter than it; dark lifts the page off pure black so that there is room below it.

**In light, elevation needs a shadow.** The steps above the page are close in light
(`#f2f2f2` page, `#f8f8f8` card), so the fill alone barely separates a raised element; in
dark the fill does most of the work. Pair `raised` and `overlay` with a shadow, as the
components do (`mds-card` uses `--shadow-md-sharp`, `mds-modal` `--shadow-2xl`):

```css
.panel {
  background: rgb(var(--magma-surface-raised));
  box-shadow: var(--shadow-md-sharp);
}
```

```html
<div class="bg-surface-raised shadow-md-sharp">...</div>
```

An overlay separates from what is under it by its shadow, not by a border: in dark
`border-muted` is darker than `surface-overlay`.

**A field stands a step off its surface** (#1193). On the page a field (`mds-input`,
`mds-input-select`, `mds-input-date`, the drop area of `mds-input-upload`) paints
`surface-overlay`; inside a surface that paints `overlay` (the window of a dialog, `mds-banner`,
`mds-status-bar`, `mds-dropdown`, `mds-table`) it paints `surface-default`, the step below, so
its fill never melts into the window. The surface passes the step down as
`--magma-field-fill`, an inherited custom property, and a field reads it with `overlay` as the
fallback. A surface of your own that paints `overlay` passes it the same way:

```css
.panel {
  --magma-field-fill: var(--magma-surface-default);

  background: rgb(var(--magma-surface-overlay));
}
```

**Text is safe on every level.** Each text role is computed against the least
contrasting surface of its family, so `text-default`, `text-muted` and the rest are
legible on all five levels in both modes (checked in CI). The tinted pills are a
different device with tighter rules: see the wash levels in [`color.md`](color.md).

**Themes and contrast move the levels, not your code.** Under a named theme the five
levels take the theme's cast. Under `pref-contrast-more` the page (`surface-default`)
becomes the paper of the mode (`#fff` light, `#000` dark) while `raised` and `overlay`
keep their step, so cards and floating layers still stand off the page. Code that paints
with the roles follows both with no rule of its own.

**Roles, not primitives.** `--surface-neutral-raised` and its siblings
(`--surface-<family>-*`) are the design-tokens primitives behind the roles, one set per
family: they follow the mode but not the named theme nor the contrast preference. Paint
with `--magma-surface-*`.

## Global design decisions

Override `--magma-*` vars only inside the `overrides` cascade layer:
`--magma-disabled-opacity` (`0.5`), `--magma-backdrop-opacity` (`0.1`),
`--magma-outline-focus`.

```css
@layer overrides { :root { --magma-disabled-opacity: 0.35; } }
```

## Corner geometry

Corner geometry is an axis of its own, because the shape and the radius scale tuned
for it have to move together: a squircle cuts a much smaller corner than a round one
at the same radius. Set `data-corner-shape` - on `<html>`, or on any element to
deviate for that subtree only - and both follow. Overriding `--magma-corner-shape`
by itself changes the shape WITHOUT the scale.

```html
<html data-corner-shape="round">
<section data-corner-shape="squircle">...</section>
```

In your own CSS take radii from the `--magma-radius-*` scale (`none`, `3xs` ... `5xl`,
`full`), which follows the axis: `border-radius: var(--magma-radius-lg)`. A raw
`--radius-*` token or a pixel value stays the same under every shape.

A browser without `corner-shape` (Firefox, Safari) gets round corners and the round
scale on its own. A control of your own whose radius is past half its height
(Chromium clamps it to half the height) needs one more step to keep its look there:
cap the radius at a part of its height, as Magma's buttons and fields do.

```css
.my-control {
  --my-control-height: 2.25rem;

  border-radius: min(var(--magma-radius-2xl), var(--magma-corner-cap) * var(--my-control-height));
  corner-shape: var(--magma-corner-shape);
  min-height: var(--my-control-height);
}
```
