# @svelte-lean/styles

The optional visual layer for Svelte Lean: design tokens and one stylesheet per primitive. The
package ships CSS only. It has no JavaScript, no build step and no dependencies; the files in `css/`
are the published files.

## Three styling modes

Svelte Lean primitives are plain HTML with a `data-slean` protocol (see ADR 0002). How they look
is a separate decision:

1. **Your own CSS.** Target the protocol attributes and platform state directly
   (`[data-slean="button"][data-variant="outlined"]`, `[data-slean="tabs"] [aria-selected="true"]`).
   Nothing from this package is required.
2. **This package.** Import the tokens, the base file and only the primitive files you use. This is
   the documented default.
3. **Copied files.** The same CSS files copied into the application and owned there; they keep
   depending on `tokens.css` or on application-level tokens.

## Granular imports first

```ts
import '@svelte-lean/styles/tokens.css';
import '@svelte-lean/styles/base.css';
import '@svelte-lean/styles/button.css';
import '@svelte-lean/styles/dialog.css';
```

`tokens.css` and `base.css` are required by every primitive file. A file that builds on another
imports it itself, so every file works when it is the only one imported: `menu.css` imports
`popover.css` (a menu is a popover: surface, anchor positioning and open transition), `select.css`,
`autocomplete.css` and `date-picker.css` import the control shell `control.css`, `date-picker.css`
also imports `calendar.css`, `hover-card.css` imports `popover.css`,
`button-group.css` imports `button.css`, and `toolbar.css` imports `toggle.css` and
`toggle-group.css`. The Imports column below lists them.

A file reached twice (you import `popover.css` and `menu.css`, or `index.css` imports both) is
harmless. Inside one stylesheet, Vite and Tailwind's import inliner include it once. Where the
browser sees a second copy (two separate `import` statements in JavaScript, or no bundler), the
copy changes nothing: the imported rules sit in an earlier layer than the rules of the file that
imports them (`slean.components` below `slean.compositions`, or the `slean.components.shell`
sublayer below every component rule), and every selector has zero specificity, so where a copy
lands never decides a conflict. It only costs its bytes, so import the file you use and let it
bring its dependencies.

`@svelte-lean/styles` (or `@svelte-lean/styles/index.css`) imports everything. Use it for
prototypes or when you use most primitives; it is not the primary path.

| Export                                   | Contents                                                                                        | Imports                          |
| ---------------------------------------- | ----------------------------------------------------------------------------------------------- | -------------------------------- |
| `@svelte-lean/styles`                    | `index.css`: tokens, base and every primitive, each once                                        |                                  |
| `@svelte-lean/styles/tokens.css`         | `--slean-*` custom properties: light and dark palettes, aliases, icons, media adjustments       |                                  |
| `@svelte-lean/styles/base.css`           | layer order, reset scoped to `[data-slean]`, shared focus ring, icon sizing                     |                                  |
| `@svelte-lean/styles/button.css`         | `[data-slean="button"]`: variants, sizes, icon-only, busy, disabled                             |                                  |
| `@svelte-lean/styles/dialog.css`         | native `<dialog>`: centered, drawer sides, alert dialog, sizes, sticky header and footer, close |                                  |
| `@svelte-lean/styles/popover.css`        | native `[popover]`: surface, header and footer, anchor positioning, side and align              |                                  |
| `@svelte-lean/styles/checkbox.css`       | native checkbox: indeterminate, `role="switch"` track and thumb, sizes, label layout            |                                  |
| `@svelte-lean/styles/checkbox-group.css` | checkboxes in a fieldset with a select-all box, the members indented                            |                                  |
| `@svelte-lean/styles/radio-group.css`    | native radios in a fieldset; `data-variant="segmented"` one bar, `"swatch"` color squares       |                                  |
| `@svelte-lean/styles/tabs.css`           | tablist, triggers, panels, vertical orientation, overflow scrolling                             |                                  |
| `@svelte-lean/styles/menu.css`           | menu rows by element and role, check column, separators, group labels, `<kbd>`, colored rows    | `popover.css`                    |
| `@svelte-lean/styles/listbox.css`        | `[role="listbox"]` frame and option rows, multi-select marks, groups, `data-color`              |                                  |
| `@svelte-lean/styles/control.css`        | the field shell of the owned select, autocomplete and date picker; chevron, dropdown, options   |                                  |
| `@svelte-lean/styles/calendar.css`       | the day grid, header, navigation, month and year selects and range marks                        |                                  |
| `@svelte-lean/styles/date-picker.css`    | date and range picker on the control shell, with the calendar popup                             | `control.css`, `calendar.css`    |
| `@svelte-lean/styles/input.css`          | every input type, `<textarea>`, and the field shell: prefix, suffix, clear, reveal, steppers    |                                  |
| `@svelte-lean/styles/field.css`          | label, helper texts (description, error) and the layout around a control                        |                                  |
| `@svelte-lean/styles/select.css`         | the styled native `<select>` (base-select) and the owned select: search, tags, create row       | `control.css`                    |
| `@svelte-lean/styles/rating.css`         | star rating on native radios                                                                    |                                  |
| `@svelte-lean/styles/slider.css`         | native `<input type="range">`; a slider group: filled track, two thumbs                         |                                  |
| `@svelte-lean/styles/autocomplete.css`   | native `<input list>` in the field look; the owned autocomplete on the control shell            | `control.css`                    |
| `@svelte-lean/styles/accordion.css`      | native `<details>` items that join into a bordered list; the ghost look; height transition      |                                  |
| `@svelte-lean/styles/card.css`           | `<article>` card: media, header, body, footer, variants                                         |                                  |
| `@svelte-lean/styles/separator.css`      | `<hr>` and `role="separator"`                                                                   |                                  |
| `@svelte-lean/styles/avatar.css`         | image with initials fallback, sizes, groups                                                     |                                  |
| `@svelte-lean/styles/badge.css`          | label or count: variants, colors, sizes and an optional remove button                           |                                  |
| `@svelte-lean/styles/kbd.css`            | keyboard keys and combinations                                                                  |                                  |
| `@svelte-lean/styles/carousel.css`       | scroll-snapping carousel with CSS scroll buttons and markers                                    |                                  |
| `@svelte-lean/styles/alert.css`          | message block: variants, colors, status icons and the close part                                |                                  |
| `@svelte-lean/styles/progress.css`       | native `<progress>`: bar and ring, indeterminate, colors, sizes                                 |                                  |
| `@svelte-lean/styles/meter.css`          | native `<meter>` colored by region                                                              |                                  |
| `@svelte-lean/styles/skeleton.css`       | placeholder shapes with a shimmer                                                               |                                  |
| `@svelte-lean/styles/breadcrumb.css`     | breadcrumb trail with chevron separators                                                        |                                  |
| `@svelte-lean/styles/nav.css`            | sidebar links and `<details>` groups, the current page, link tabs, the icon rail                |                                  |
| `@svelte-lean/styles/pagination.css`     | page links, previous/next, ellipsis                                                             |                                  |
| `@svelte-lean/styles/steps.css`          | numbered steps with complete and current states                                                 |                                  |
| `@svelte-lean/styles/toggle.css`         | `aria-pressed` button                                                                           |                                  |
| `@svelte-lean/styles/toggle-group.css`   | a track of toggle items                                                                         |                                  |
| `@svelte-lean/styles/toolbar.css`        | a bar of controls with separators                                                               | `toggle.css`, `toggle-group.css` |
| `@svelte-lean/styles/tree.css`           | `role="tree"` rows, chevrons, guides                                                            |                                  |
| `@svelte-lean/styles/toast.css`          | toast region and items                                                                          |                                  |
| `@svelte-lean/styles/button-group.css`   | buttons joined in a group                                                                       | `button.css`                     |
| `@svelte-lean/styles/scroll-area.css`    | focusable scrolling region with a themed scrollbar                                              |                                  |
| `@svelte-lean/styles/descriptions.css`   | label and value pairs                                                                           |                                  |
| `@svelte-lean/styles/timeline.css`       | dated events with dots and a line                                                               |                                  |
| `@svelte-lean/styles/empty.css`          | empty state layout                                                                              |                                  |
| `@svelte-lean/styles/splitter.css`       | two resizable panes with a handle                                                               |                                  |
| `@svelte-lean/styles/sortable.css`       | a reorderable list: items, grip handles, previous and next buttons, the lifted item             |                                  |
| `@svelte-lean/styles/file-drop.css`      | drop zone with a file list                                                                      |                                  |
| `@svelte-lean/styles/hover-card.css`     | a `popover="hint"` card a trigger names with `interestfor`                                      | `popover.css`                    |
| `@svelte-lean/styles/menubar.css`        | a bar of menu triggers (its menus need `menu.css`)                                              |                                  |
| `@svelte-lean/styles/hint.css`           | the tooltip: one bubble for `data-slean-hint`, sides, mono, rich content                        |                                  |
| `@svelte-lean/styles/copy.css`           | `data-slean-copy`: pointer, disabled, the copied check mark hook                                |                                  |
| `@svelte-lean/styles/findable.css`       | `data-slean-findable`: the short form, the whole value found by find-in-page drawn over it      |                                  |

Every file starts with the same `@layer` order statement, so import order between primitive files
does not matter (only `tokens.css` and `base.css` must be present).

## Setup

Fonts are not applied to the page. Controls (buttons, fields) inherit the page font; the text the
package sets itself (dialog and popover titles, `data-typography`, hover cards) uses
`--slean-font-sans`. If you want the token stack on the page, set it yourself:

```css
body {
	font-family: var(--slean-font-sans);
}
```

### Theming fonts

- **One family everywhere**: set `--slean-font-sans` to your family list on `:root`
  (`--slean-font-sans: 'IBM Plex Sans', system-ui, sans-serif;`) and apply it to the page as
  above.
- **The page's own font**: set `--slean-font-sans: initial` (on `:root` or on any subtree;
  `inherit` works on `:root` only, since below it a custom property inherits the parent's token).
  The token is then invalid: the package reads the typography scale through longhands
  (`--slean-typography-<variant>-weight`, `-size`, `-line-height`) and the family through
  `font-family: var(--slean-font-sans)`, which falls back to the inherited family, so every
  element keeps its size and weight and takes the family of its parent. A `<DataTable />` falls
  back to `system-ui` for an invalid token (it also works without this package): set
  `.slean-table { --slean-table-font: initial; }` as well for the page's font there.
- **The scale**: change a step with its longhands (`--slean-typography-h6-size: 1.0625rem;`). The
  `--slean-typography-<variant>` tokens are `font` shorthands of the three longhands and
  `--slean-font-sans` for your own rules (`font: var(--slean-typography-h4)`), computed on
  `:root`; with the family token invalid the whole shorthand is invalid, so then use the
  longhands in your rules too.
- **Buttons**: `--slean-button-font` is a shorthand of the button step; its family is a
  placeholder, because the button takes the page's family.

## Cascade layers and overriding

All rules live in the layers `slean.reset, slean.tokens, slean.base, slean.components,
slean.compositions, slean.utilities`, and every selector is wrapped in `:where()` (zero
specificity). `slean.compositions` holds the files that restyle another primitive (menu, hover
card, button group, toolbar, date picker), so they win whatever order a bundler loads the files
in. The control shell (`control.css`) sits in the sublayer `slean.components.shell`, below every
rule of the component files that build on it. Any unlayered rule in your application wins,
whatever its specificity:

```css
[data-slean='button'] {
	border-radius: 0;
}
```

This also means a page-level rule such as `h2 { … }` applies to a `<h2 data-slean-part="title">`
inside a dialog; the package does not fight your global styles.

### Tailwind CSS v4

Put the `slean` layer between Tailwind's preflight and its components, before importing anything,
and import the package files without `layer()` (they declare `slean.*` sublayers themselves):

```css
@layer theme, base, slean, components, utilities;

@import 'tailwindcss';
@import '@svelte-lean/styles/tokens.css';
@import '@svelte-lean/styles/base.css';
@import '@svelte-lean/styles/button.css';

@custom-variant dark (&:where(.dark, .dark *));

@theme inline {
	--color-background: var(--slean-bg);
	--color-foreground: var(--slean-fg);
	--color-primary: var(--slean-primary-main);
	--color-primary-foreground: var(--slean-primary-contrast-text);
	--color-muted: var(--slean-muted);
	--color-muted-foreground: var(--slean-fg-muted);
	--color-border: var(--slean-border);
	--color-input: var(--slean-control-border);
	--color-ring: var(--slean-focus-ring-color);
	--radius-md: var(--slean-radius-md);
}
```

Preflight (`base`) comes first and the package rules replace it on every `data-slean` element;
utilities come last and win over the package rule they meet, so `class="rounded-full"` on a
`data-slean="button"` works. A utility without a state variant also replaces the package's hover
or focus value for that property (`bg-*` needs a matching `hover:bg-*`).

## Tokens: the theme

All tokens are `--slean-*` custom properties, defined in `oklch`, inside the `slean.tokens` layer.
The theme is CSS only: override tokens in your own (unlayered) CSS and every primitive follows.

- **Palette** (MUI-style), per color `primary`, `secondary`, `error`, `warning`, `info`,
  `success` and `neutral`: `--slean-<color>-main`, `-light`, `-dark`, `-contrast-text` (text on
  main), `-soft` (the tint of standard variants) and `-soft-text`, in light and dark.
- **Current color**: `--slean-color-main|light|dark|contrast-text|soft|soft-text`, primary by
  default. `data-color="<color>"` on any element selects another palette for it and its subtree;
  badge, alert, toast items and timeline items default to neutral. Every variant of every
  primitive is drawn from these six tokens and two derived from them: `--slean-color-text` and
  `--slean-color-border`, the ink and the line of outlined and text variants (the main tone and a
  half-tone line of it; for neutral, MUI's "inherit": `--slean-fg` and the `--slean-border`
  hairline). Any `[data-color]` element derives the two from its own main tone, so a color of
  your own (`[data-color='brand']` setting the six) needs nothing more.
- **Surfaces and text**: `--slean-bg`, `--slean-surface`, `--slean-fg`, `--slean-fg-muted`,
  `--slean-muted`, `--slean-muted-hover`, `--slean-border`, `--slean-border-strong`,
  `--slean-overlay`, the neutral scale `--slean-neutral-1` … `-12`.
- **Elevation**: `--slean-elevation-0` … `-5` (MUI's 0–24 compressed: 1 resting surfaces, 2 raised
  cards, 3 menus and popovers, 4 dialogs, 5 drawers and toasts).
- **Spacing**: `--slean-spacing` (0.25rem) and `--slean-space-1` … `-8`, its multiples.
- **Shape**: `--slean-radius` (the medium step) and `--slean-radius-sm|md|lg|full`.
- **Typography**: `--slean-font-sans` (a family list, or `initial` for the page's font),
  `--slean-font-mono`, `--slean-text-xs|sm|md|lg`, `--slean-leading`, weights
  `--slean-font-weight-regular|medium|semibold|bold`, and the scale `h1` … `h6`, `subtitle1|2`,
  `body1|2`, `caption`, `overline`, `button`: per step `--slean-typography-<step>-weight`, `-size`
  and `-line-height` (what the package reads) and `--slean-typography-<step>`, a `font` shorthand
  of them and `--slean-font-sans`; `--slean-typography-button-transform` and `-overline-transform`.
  Apply a step with `data-typography="h4"` on any element (see "Theming fonts").
- **Motion**: `--slean-duration-fast|normal|slow|indicator`, `--slean-ease` and
  `--slean-ease-in-out|out|in|sharp`.
- **Stacking**: `--slean-z-app-bar|drawer|modal|snackbar|tooltip` (1100–1500, MUI's order) for
  chrome outside the top layer; dialogs, popovers, menus and toasts are in the top layer already.
- **Breakpoints** (reference, container queries): `--slean-breakpoint-xs|sm|md|lg|xl` = 0, 600,
  900, 1200, 1536px.
- **Control shell**: `--slean-control-height-xs|sm|md|lg` (28, 32, 36, 44px), `--slean-control-*`
  backgrounds and borders, status rings (`-ring-error`, `-ring-warning`), `--slean-control-filled-bg|-hover`,
  `--slean-control-border-hover` (the border of an outlined field under the pointer; unset, it is
  the field's current color; `var(--slean-fg-muted)` softens it),
  `--slean-option-*`, `--slean-popup-*`.
- **Component defaults** (MUI's `theme.components`): `--slean-button-radius`,
  `--slean-button-font`, `--slean-button-transform`, `--slean-field-radius`,
  `--slean-card-radius`, `--slean-card-elevation`, `--slean-dialog-radius`,
  `--slean-dialog-elevation`, `--slean-dialog-padding` (the content inset, the sticky header and
  footer bleed and the close button follow it), `--slean-dialog-gap` (between the dialog's
  children; use it instead of `gap`), `--slean-chip-radius`.
- **Focus and icons**: `--slean-focus-ring`, `-color`, `-width`, `-offset`; `--slean-icon-*`
  (chevrons, check, minus, plus, close, clear, more, search, calendar, arrow-right, info, success,
  warning, error).

### Create a theme

A theme is one block of overrides. Put it in your own stylesheet (unlayered rules win over the
package's layers):

```css
:root {
	/* Brand colors */
	--slean-primary-main: oklch(52% 0.16 160);
	--slean-primary-light: oklch(65% 0.13 160);
	--slean-primary-dark: oklch(45% 0.16 160);
	--slean-primary-contrast-text: oklch(100% 0 0);
	--slean-primary-soft: oklch(95% 0.03 160);
	--slean-primary-soft-text: oklch(40% 0.12 160);
	/* Shape, spacing, type */
	--slean-radius: 0.375rem;
	--slean-spacing: 0.25rem;
	--slean-font-sans: 'IBM Plex Sans', system-ui, sans-serif;
	--slean-typography-button-transform: uppercase;
	/* Component defaults */
	--slean-button-radius: 9999px;
	--slean-card-elevation: var(--slean-elevation-0);
}

/* Dark mode: the same tokens under the dark scopes. */
@media (prefers-color-scheme: dark) {
	:root:not([data-theme='light'], .light) {
		--slean-primary-main: oklch(66% 0.14 160);
		--slean-primary-soft: oklch(28% 0.06 160);
		--slean-primary-soft-text: oklch(86% 0.08 160);
	}
}
[data-theme='dark'],
.dark {
	--slean-primary-main: oklch(66% 0.14 160);
	--slean-primary-soft: oklch(28% 0.06 160);
	--slean-primary-soft-text: oklch(86% 0.08 160);
}
```

Density is a theme decision too: `data-density="compact"` (or `"comfortable"`) on `<html>`, a page
or a region changes the control heights and option rows of everything inside it.

The control border (`--slean-neutral-8`) is at least 3:1 against the surface and the page
background in both themes, so unchecked boxes, radios, switch tracks and field borders stay
visible. Every field-like primitive (an input of any type and the field shell, the native and
owned select, autocomplete, date picker) shares the same border, hover border, focus halo,
`aria-invalid` / `data-status="warning"` and disabled surface.

### Dark mode

Light is the default. The dark palette applies

- under `@media (prefers-color-scheme: dark)` unless the root has `data-theme="light"` or the
  `light` class, and
- always on an element with `data-theme="dark"` or the `dark` class (mode-watcher and Tailwind's
  convention), the root or any subtree.

`data-theme="light"` or the `light` class on an inner element puts that region back in the light
palette. Primitives set `color-scheme` from `--slean-color-scheme`, so native parts (scrollbars,
dialog canvas) follow the theme without the page's own `color-scheme` being touched.

### Theming a subtree

Some tokens are aliases derived from others (the current color, the neutral palette, the focus
ring, the field border on hover and focus and its halo, the selected option, the component
defaults). A custom property that reads another is computed where
it is declared, so the aliases are declared on `:root` and again on every theme scope:
`data-theme`, `data-slean-theme`, `.dark` and `.light`. Put `data-slean-theme` on the element that
redefines palette tokens and the aliases below it follow:

```html
<div data-slean-theme="brand">…</div>
<style>
	[data-slean-theme='brand'] {
		--slean-primary-main: oklch(45% 0.13 150);
		--slean-primary-dark: oklch(40% 0.13 150);
		--slean-primary-soft: oklch(95% 0.04 150);
		--slean-primary-soft-text: oklch(40% 0.12 150);
	}
</style>
```

An alias you override on `:root` is derived again inside a theme scope; override it on the scope
selectors as well, or override the palette token it reads.

### Forced colors

Under `@media (forced-colors: active)` the platform replaces most colors. The tokens map the focus
ring to `Highlight` and borders to `CanvasText` and drop shadows; buttons get a `ButtonText` border
so contained buttons keep an outline; checkbox (with its switch look) and radio group opt out of the automatic
adjustment and use `Canvas`, `ButtonText`, `Highlight`, `HighlightText` and `GrayText` explicitly;
masked icons are painted in a system color; disabled controls of every primitive show as
`GrayText`.

### Reduced motion

`@media (prefers-reduced-motion: reduce)` sets every transition duration token to `0ms` and slows
`--slean-duration-indicator` (one turn of the busy button's and the circular progress's ring) from 750ms to
1.6s instead of stopping it: a still ring reads as a stuck page. Component files use those tokens
only.

### Pointer targets

`@media (pointer: coarse)` raises the control heights (small, medium, large) to 40, 44 and 48px,
and checkbox and radio grow an invisible hit area to the medium height.

## Protocol used by the styles

State comes from the platform and ARIA, never from mirrored `data-state` attributes. Looks are
unprefixed attributes read by the styles only (ADR 0002 as amended by ADR 0011 §3.3), with MUI
and Ant Design's model: a variant is never a color.

| Attribute         | Values                                                                                                                                                                                                                                                       |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `data-variant`    | shape or fill, per family: button, button group, toggle `contained` (default) \| `outlined` \| `text`; field shells `outlined` (default) \| `filled` \| `standard`; alert, badge, toast `standard` \| `filled` \| `outlined`; card `elevation` \| `outlined` |
| `data-color`      | `primary` \| `secondary` \| `error` \| `warning` \| `info` \| `success` \| `neutral`, on the element or an ancestor                                                                                                                                          |
| `data-size`       | `small` \| `medium` (default) \| `large`                                                                                                                                                                                                                     |
| `data-full-width` | presence: button, field shells, tabs                                                                                                                                                                                                                         |
| `data-density`    | `compact` \| `comfortable`, on any element, inherited                                                                                                                                                                                                        |
| `data-status`     | `warning` on a form control; an error is `aria-invalid="true"` or `:user-invalid`                                                                                                                                                                            |
| `data-typography` | `h1` … `h6`, `subtitle1`, `subtitle2`, `body1`, `body2`, `caption`, `overline`, `button`                                                                                                                                                                     |

Behavior options are `data-slean-<option>`. Icons inside a control are sized for you: an `<svg>`
child of a button, link, summary, menu item, option or tab of a primitive is 1rem square; a
button's `start-icon` and `end-icon` parts are placed like MUI's `startIcon` and `endIcon`. Every
primitive's variants, colors, sizes and parts are listed in its `contract.md` in
`@svelte-lean/primitives`.

The styling page of the documentation lists every primitive's variants, options and parts, read
from these files.

### Popover positioning

A popover is placed with CSS anchor positioning (`position-area`, flip fallbacks). The anchor is
the implicit one the browser creates for an invoker: `popovertarget`, `command`/`commandfor`, or
`showPopover({ source })`. A popover opened without any anchor has no reference and renders at the
top layer's static position; give it an invoker or set `anchor-name` / `position-anchor` yourself.
Where `position-area` is unsupported the popover keeps the platform default: centered in the top
layer.

## Scripts

- `yarn workspace @svelte-lean/styles check` — static checks (`scripts/check.mjs`): layer order,
  dependency imports, token references, no `!important`, no hard-coded colors, zero-specificity
  selectors, token block structure and dark-theme parity, exports map, `index.css` imports.
- `build` — runs the checks, prints the export map, then `publint`. There is no compile step.
- `test` — Vitest suite calling the same checks plus tokenizer and distribution tests.
- `size` — source, minified, gzip and brotli bytes per file to `artifacts/size.json`; the resolved
  `index.css` (each file once, as a bundler resolves it) carries the gzip budget (`BUDGET_GZIP` in
  `scripts/size.mjs`); a file that imports others also reports its `standalone` size. Import the
  per-primitive files to ship only what a page uses.
