---
name: design-system
description: "Build a complete, scalable design system in two gated stages: (1) a 3-tier token system (brand primitives, semantic aliases, component-ready mapped tokens) with light/dark modes, contrast-solved WCAG AA colour roles and type/space/radius/elevation/motion scales, exported to CSS variables, Tailwind v3/v4 and W3C design-tokens JSON; then, only after the user approves the tokens, (2) a tested component library on those tokens (buttons, forms, badges, tags, avatars, cards, alerts, toasts, tabs, tooltips, dialogs, menus, navigation, pagination, tables...) that is consistent, accessible and optically aligned, with a gallery and a browser verifier. Use whenever the user wants a design system, design tokens, colour palettes, dark mode, a Tailwind theme, Figma variables, a UI kit or component library, components that 'look right' or 'feel consistent', visual or optical alignment, or a new component added to an existing system, even if they never say 'design system'."
---

# Design System

One skill, two stages, two approval gates. Tokens are built and validated first; the user approves them;
only then are components built on them, verified in a browser, and approved. Never skip or merge the gates:
every component inherits its colours, sizes and contrast from the tokens, so building components on tokens the
user has not approved wastes the whole second stage.

```
Stage 1  TOKENS      discover → palette → config → build + validate → GATE 1 (user approves tokens)
Stage 2  COMPONENTS  character → build + verify in browser → look → GATE 2 (user approves components) → hand off
```

`SKILL_DIR` = the folder containing this file. Token scripts are Python 3 standard library only. The component
verifier needs Node with `playwright-core`, `pngjs` and a Chromium (`components/scripts/package.json`).

---

# Stage 1: Tokens

## The model in one screen

| Tier | JSON group | Holds | Who may use it |
| :-- | :-- | :-- | :-- |
| 1 Brand | `brand.*` | raw values: colour ramps 50-950, font/space/radius/shadow/motion primitives | nobody (internal) |
| 2 Alias | `alias.*` | meaning + modes. Colour aliases (`alias.color.*`) switch light/dark and are internal. Geometry, type and motion aliases (`space`, `radius`, `type`, `elevation`, `motion`, `size`, `gap`, `padding`, `z`, `focus`, `breakpoint`, `border-width`, `opacity`) are public | components (geometry only) |
| 3 Mapped | `mapped.*` | component-ready colour roles: `surface.*`, `text.*`, `icon.*`, `border.*`, mode-agnostic references to Tier 2 | components |

Public tokens drop their tier prefix (`mapped.surface.raised` → `surface.raised` → CSS `--surface-raised`;
`alias.space.md` → `space.md`). `brand.*` and `alias.color.*` keep their prefix and components never use them, so
`brand.space.8` (8px) and `space.md` (16px) cannot be confused. Colour roles are **solved, not guessed**: the
generator searches each ramp for the first step that passes WCAG on every surface it can sit on, and an
independent validator re-checks every pair in both modes.

## 1.1 Discovery (fast)
Check the conversation and uploaded files first. Ask only what is unknown, in **one message with at most 3
questions**, 2-4 options each (tappable choices if available):
1. Brand colour: hex, palette name, or a mood (calm, playful, serious, energetic, earthy...)
2. Platform / stack: web (CSS, Tailwind v3/v4), React Native, native, Figma-first
3. Feel: corners `sharp | default | soft`, dark mode yes/no

"Just do it" → defaults: default radius, dark mode on, tinted neutral, system sans stack.

## 1.2 Palette
```bash
python3 SKILL_DIR/scripts/pick_palettes.py brand --vibe calm -n 5     # mood → options (usable ramps only)
python3 SKILL_DIR/scripts/pick_palettes.py show "Dodger Blue"          # inspect one ramp
python3 SKILL_DIR/scripts/pick_palettes.py ramp --hex "#1A73E8"        # any hex → generated 50-950 ramp
```
Show 3-5 options with hex and ramp strip; the user picks. Red, amber, green, blue and the neutral are matched to the
brand automatically (`utilities` / `neutral` show alternatives).

## 1.3 Config
```bash
python3 SKILL_DIR/scripts/pick_palettes.py config --brand "Dodger Blue" --radius default \
        --neutral tinted --font-heading "'Inter', sans-serif" --out token-config.json
```
Edit only fonts, `radius_style`, `dark_mode`, `contrast_level` (AA or AAA), an optional `accent` ramp,
`surface_steps` and `overrides` (`references/extending.md`). Shape: `assets/token-config.example.json`.
To match a reference brand's feel (Vercel, Stripe, Notion...), read `references/styles.md` now: it gives the
config for each, and the component character to use in Stage 2.

## 1.4 Build and validate
```bash
python3 SKILL_DIR/scripts/build_all.py tokens token-config.json --out dist/
```
Builds tokens, runs the validator, renders `dist/showcase.html`, and writes `dist/STAGE1_PASSED` only on a pass.
If it fails, fix and rebuild before showing anything. A `WARNING` in `build-report.md` means a ramp cannot reach
the target somewhere: say which role, and offer another ramp or accept the documented limit.

## 1.5 GATE 1: token approval (stop here)
Present one compact summary, then **stop and wait for the user's answer**:
- brand, utilities and neutral: names and hex (primary 500/600)
- type families, radius style, dark mode, AA or AAA
- the 3-6 most visible solver decisions from `build-report.md`, in plain words
  (e.g. "your orange fails white text at 600, so primary buttons use 700")
- the validator result line, and `dist/showcase.html` (publish it as a page if the session can, so they can look)

Ask: **1. Approve tokens and build components  2. Change something  3. See the full catalog** (`dist/token-catalog.md`).

- *Change something* → edit the config, re-run 1.4, show the new summary, ask again.
- *See the catalog* → share it, ask again.
- Only an explicit approval moves to Stage 2. Silence, "looks nice" on a detail, or a question is not approval: answer
  it and ask again. If the user only wanted tokens, hand off here (see Hand-off) and stop.

---

# Stage 2: Components

Runs only after Gate 1 is approved. The components read nothing but the public tokens from Stage 1.

## 2.1 Character
One knob on `<html data-character="…">` sets the family's feel (`references/styles.md`):
`crisp` (hairlines, no shadow: Vercel, Stripe, Dub, Cursor) · `soft` (hairline + quiet elevation: shadcn/ui,
Notion) · `playful` (2px borders: Duolingo). Infer it from the style the user named in Stage 1; ask only if nothing
points either way (one question, three options).

## 2.2 Build and verify
```bash
python3 SKILL_DIR/scripts/build_all.py components --out dist/ --verify [--chrome /path/to/chrome]
```
Bundles `dist/ds-components.css`, copies `dist/ds.js`, builds `dist/gallery.html` and a self-contained
`dist/component-gallery.html`, then runs the browser verifier over 2 themes × 3 characters × 2 widths. It must end
`RESULT: PASS`. It checks: equal heights across a row and across variants, text centring (±0.75px), rendered
contrast, 24px targets, accessible names, nested radius, overflow, focus rings, console errors.
Without Node or a browser, run without `--verify`, say plainly that the browser checks did not run, and do not
claim they passed.

## 2.3 Look
Screenshot the gallery's **Composed screen** in light and dark at 1280 and 390 wide (or open it yourself). The
verifier proves geometry and contrast; only eyes judge whether a part looks foreign next to its neighbours. Fix
what looks foreign, rebuild, re-verify.

## 2.4 GATE 2: component approval (stop here)
Share the gallery (publish `component-gallery.html` as a page if the session can) with a short summary: the
character, the components included, the verifier result, and anything you could not check. Ask:
**1. Approve components  2. Change the feel** (character, radius, density) **3. Change a component**.
- Feel changes that live in tokens (radius, colour, fonts) go back through Stage 1's build and validator, then this
  stage again; do not patch component CSS to change a token decision.
- Only an explicit approval moves to hand-off.

## Rules that make components look like one family
Full reasoning and numbers: `references/visual-alignment.md`.
- **One height ladder**: every control is 32 / 40 / 48 (`size.control.sm`, `size.control.md`, `size.control.lg`).
- **Same outer size across variants**: every variant draws the same border width (transparent when unseen); invalid
  and selected states use inset shadows, so state never moves layout.
- **Centre the letters**: control labels sit in `.ds-label` (`text-box: trim-both cap alphabetic`); native inputs
  get a measured 1px nudge (`--ds-input-nudge`) that the verifier re-checks when the font changes.
- **First-line alignment** for checkbox, radio, switch, alert and error icons and dismiss buttons.
- **Icons weigh more than text**: the icon side of a button gets 2px less padding; one icon set, one stroke weight.
- **Nested radius**: inner radius = outer radius − the inset between them.
- **One depth language per character**; floating layers (menus, toasts, dialogs) always get elevation, nothing else does.
- **Colour only where it means something**; text on a fill always uses that fill's paired token; state is never colour alone.

## Accessibility floor (verified)
Text ≥ 4.5:1; control borders, icons and focus rings ≥ 3:1 · targets ≥ 24×24, 16px input text on touch · one 2px
focus ring · every control named · keyboard per WAI-ARIA APG for tabs, radiogroup, menu button and dialog (`components/js/ds.js`) ·
tooltips hoverable and Esc-dismissible · reduced motion and forced colours handled. Say when handing over that a
screen-reader pass on real flows is still needed.

## New component on request
Any time after Gate 1 the user can ask for a component that is not in the core set ("a pricing card", "a rating
control", "a filter pill"). It must come out in the system's style, so it is built from the system, never from scratch:

1. **Check the core set and `references/roadmap.md` first.** If an existing component covers it with a variant, add
   the variant instead of a new component (a "filter pill" is a `ds-tag` with `aria-pressed`). Say so, briefly.
2. **Scaffold** with the kind that matches its job:
   ```bash
   python3 SKILL_DIR/components/scripts/new_component.py pricing-card --kind container   # control | container | overlay | display
   ```
   This writes `components/css/custom/<name>.css` (token-only CSS with tone variables, the control-height ladder,
   every state, focus ring, disabled, reduced motion) and `components/gallery/custom/<name>.html` (variants × tones,
   states, and for controls an alignment row next to a button of each size). The bundler, gallery, lint and
   verifier pick both up automatically.
3. **Fill in the anatomy** within the rules: colours from `surface/text/icon/border.*` through the `--t-*` tone
   variables, geometry from `space/size/radius/padding/gap`, type from `type.*`, the alignment rules above, the
   APG keyboard pattern in `components/js/ds.js` if it is interactive and custom. If it needs a colour pairing or
   size the tokens do not have, **do not invent a value**: add it in Stage 1 (`references/extending.md`), rebuild
   and validate the tokens, tell the user what was added, then continue.
4. **Build, lint, verify**: `python3 SKILL_DIR/scripts/build_all.py components --out dist/ --verify`. The build refuses
   a custom component with raw colours, raw px, internal `--brand-*` tokens or unknown variables; the verifier checks
   its height and text centring beside a button, its rendered contrast, targets, names and focus ring.
5. **Look and show**: screenshot its gallery section in light and dark, and use it once in the composed screen when it
   belongs in an app view. Share it and ask for approval (**1. Approve  2. Change it**), the same as Gate 2.
6. **Document**: add its markup, states and keyboard notes to `references/components.md`.

---

# Hand-off
Deliver `dist/`: `tokens.css`, `tokens.json`, `tokens.light.json`, `tokens.dark.json`, `tailwind.theme.css`,
`tailwind.preset.cjs`, `token-catalog.md`, `build-report.md`, `showcase.html`, and after Stage 2
`ds-components.css`, `ds.js`, `component-gallery.html`. Explain which files fit their stack
(`references/platforms.md`; Figma: Brand 1 mode, Alias Light/Dark, Mapped 1 mode). Point them at
`references/components.md` for markup and at `scripts/lint_usage.py src/ --tokens dist/tokens.css` to keep product
code on tokens (no raw hex, no `--brand-*`).

## Audit an existing system
Validate their tokens with `scripts/validate_tokens.py` if they follow this structure; otherwise map their ramps into a
config (primary, neutral, red, amber, green, blue), rebuild, and report before/after (dangling references, text
below 4.5:1, borders below 3:1). Existing components: run them through the same gallery rules and the verifier.

## Guaranteed, and not
Guaranteed by the validator in both modes: text ≥ 4.5:1 (AAA 7:1) on every surface in the pairing table; control
borders, focus ring and icons ≥ 3:1; tier rules; no dangling references. By the verifier: the Stage 2 checks above
on the gallery. Not guaranteed: disabled states (WCAG-exempt), decorative borders, the translucent overlay, APCA,
pairings that break the rules, AAA on ramps that cannot reach it, fonts the verifier has not measured, and
screen-reader behaviour in real flows.

# Files
- `scripts/build_all.py` — the two-stage pipeline (tokens, components) with the Stage 1 gate file
- `scripts/` — `pick_palettes.py`, `build_tokens.py`, `validate_tokens.py`, `build_showcase.py`, `lint_usage.py`, `selftest.py`, `tokenlib.py`
- `collections/01-03` — spec of each token tier · `resources/color-palettes.csv` — 1,566 ramps by vibe
- `references/token-mapping-guide.md` — pairing contract for every mapped token
- `references/platforms.md`, `references/extending.md` — stacks, Figma, accent, overrides, density, multi-brand, versioning
- `examples/component-token-recipes.md` — token recipes per component (the contract the CSS implements)
- `components/css/` — `foundation.css` and `components/*.css` · `components/js/ds.js` · `components/icons/` (Lucide, ISC)
- `components/gallery/build_gallery.py`, `components/scripts/bundle.py`, `components/scripts/verify.mjs`
- `references/components.md` — markup, variants, states, keyboard, a11y per component
- `references/visual-alignment.md` — the optical rules · `references/styles.md` — reference brands → settings
- `references/roadmap.md` — what to build next from a full component catalogue, and what to skip
