# Foundations — the primitive token tier (WS-15)

> Reviewer handoff for the foundations PR. What changed, why it matters, how
> the pieces fit, and what to check. Living doc for anyone adding or consuming
> tokens after it lands.

## Why foundations, and why now

Until now `@aisquare/ui` had strong **semantic** tokens (`--primary`,
`--success`, `--radius`, the glow scale) but no **primitive layer underneath
them** — no color ramps, no named spacing scale, no motion or grid contract,
and no place where the design system's raw rules were visible. In practice
that meant:

- When a surface needed anything beyond the semantic set (a soft success
  fill, a darker warning text, a hover tint), authors reached for Tailwind
  literals (`bg-emerald-500`, `text-slate-700`) — values with **no
  relationship to the brand palette** and invisible to theming.
- Spacing, motion timing, and layout dimensions were conventions in people's
  heads, not rules in the system. Two surfaces could disagree and neither was
  "wrong", because nothing defined right.
- The docs site showed components but never the **rules that make them one
  system** — new contributors had no single place to learn the palette, the
  type scale, or the depth language.

A design system is only as coherent as its foundation. Defining the primitive
tier is the step that turns a component library into a design system: every
color, size, shadow, and duration now traces to a **named, documented,
overridable decision**. This is the base the rest of WS-15 (component order,
brand guidelines, surface migrations) builds on.

## What this PR adds

### 1. Primitive color ramps (50 → 950)

Seven ramps, exposed both as CSS variables and Tailwind utilities:

| Ramp | Anchor | Utilities |
| --- | --- | --- |
| `navy` | `navy-900` **= light `--primary`** (brand navy `#071E3C`) | `bg-navy-900`, … |
| `accent` | `accent-500` **= `--accent`** (brand blue `#1F84FF`) | `bg-accent-300`, … |
| `slate` | neutral; `200` ≈ `--border`, `500` ≈ `--muted-foreground` | `text-slate-600`, … |
| `destructive` (red) | `500` ≈ light `--destructive` | `bg-destructive-100`, … |
| `success` (green) | `500` ≈ light `--success` | `border-success-300`, … |
| `warning` (amber) | `500` ≈ light `--warning`/`--premium` | `text-warning-700`, … |
| `info` (sky) | `500` ≈ light `--info` | `bg-info-50`, … |

**Visually seamless adoption:** the neutral and status ramps are the Tailwind
v3 palettes verbatim (as HSL triplets). Their 500 steps sit within 1 RGB
point per channel of the semantic tokens components already render (the
semantics keep their historical rounded triplets; the ramps carry Tailwind's
decimals) — imperceptible, so adopting a ramp step never *visibly* shifts a
surface. The two brand anchors ARE exact: `navy-900` = light `--primary` and
`accent-500` = `--accent`, byte-for-byte. Because the near-matches are not
literal equalities, **never "dedupe" a semantic token to
`hsl(var(--<ramp>-500))`** — that ships a real color change.
`src/lib/foundation-tokens.test.ts` pins the mirror-parity and
Tailwind-verbatim invariants.

The two-tier contract:

- **Semantic tokens** (`--primary`, `--success`, …) are *mode-aware* — they
  flip under `.dark` and are what components consume first.
- **Primitive ramps** are *absolute* — they never flip. Reach for a step when
  a semantic token doesn't cover the surface (soft fills, borders, non-default
  text emphasis), instead of a raw Tailwind literal.

### 2. Spacing, radius, motion, grid — the missing contracts

- **Spacing**: Tailwind's numeric 4px grid *remains* the spacing system; the
  new `--space-*` vars name its canonical steps 1:1 (`p-4` ↔ `--space-4` =
  16px) for docs and non-Tailwind consumers. Two named layout spacings carry
  the layout contract: `gap-gutter` (24px) and `p-page` (32px).
- **Radius**: the semantic `--radius` knob (12px) and the `rounded-sm…2xl`
  derivation are **unchanged**; a primitive `--radius-0…6 + full` ladder now
  documents the raw scale.
- **Elevation**: a named neutral depth ladder `shadow-elevation-1..4`,
  reconciled with what already shipped (elevation-2 ≡ `--dropCard` as
  box-shadow, elevation-3 ≡ `--dropPanel`, elevation-4 ≡ `shadow-pop`).
  Mode-aware like `shadow-pop`. **Dark values are derived, not designed —
  flagged for design sign-off.**
- **Motion**: five durations (120–480ms) and four easings, tokenized as
  `--motion-*`. The existing `duration-*` / `ease-*` utilities now resolve
  *through* the tokens (with the old literals as fallbacks), so a consumer
  can retime the whole system from `:root`. New: `duration-120`,
  `ease-out-soft` (the card-in curve, previously anonymous).
- **Grid & layout**: `--grid-columns` (12), `--grid-gutter` (24px),
  `--grid-margin` (32px), `--content-max` (1280px, `max-w-content`).
  Breakpoints stay Tailwind defaults — documented, not tokenized.

### 3. One brand typeface

Typography consolidates from three shipped faces (Open Sans + Merriweather +
Geist Mono) to **one: Inter Variable** (weight + optical-size axes,
self-hosted via Fontsource). The display voice is Inter's optical-size cut
(`--font-display` resolves to `--font-sans`); the mono role becomes a system
stack with `tabular-nums` still on by default. Rationale: one face means one
brand voice, hierarchy carried by the type *scale* rather than family
mixing, and two fewer font payloads.

**Compatibility:** every `--font-*` token name and every `font-sans /
font-display / font-serif / font-mono` utility survives — no class breaks.
But text renders in Inter everywhere, which is why this PR is proposed as a
**major** version (see semver note below).

### 4. Storybook "Foundations" section

Seven live documentation pages — **Colors, Typography, Spacing, Radius,
Shadows, Motion, Grid & Layout** — pinned first in the sidebar. They render
the *actual tokens* (computed at runtime, correct in both themes), so the
docs can't drift from the implementation. The docs site now teaches the
rules, not just the components. (Also fixes a latent bug: Storybook never
loaded the brand fonts, so every story previously rendered in system
fallbacks.)

## Where things live (architecture)

```
src/lib/foundation-tokens.ts     ← single source of truth for all values
        │
        ├── tailwind.preset.ts   foundationTokensPlugin injects the vars via
        │                        addBase + theme.extend maps them to utilities
        │                        (consumers get everything with zero imports)
        │
        ├── src/tokens/foundations.css   opt-in mirror for NON-preset
        │                                consumers (the onyx-hex.css pattern)
        │                                → @aisquare/ui/tokens/foundations.css
        │
        └── src/foundations/*.stories.tsx   Storybook pages iterate the same
                                            data — docs can't drift
```

This follows the library's established token-distribution pattern
(`aikoTokensPlugin` / `elevationTokensPlugin` + CSS mirror). Per-studio
overrides in a consumer's own `:root { }` still win the cascade.

## What reviewers should check

1. **No visual drift**: existing stories (Button, Badge, Card, …) should be
   pixel-identical except the typeface. The `slate` utilities are value-
   identical to Tailwind's by construction.
2. **Ramp anchors**: `navy-900` ≡ light `--primary` and `accent-500` ≡
   `--accent` exactly; status 500s ≈ their semantic tokens (within 1 RGB
   point — verifiable on the Foundations/Colors page, where the resolved
   triplets are printed).
3. **Dark elevation values** (`ELEVATION_DARK` in `foundation-tokens.ts`) —
   derived from the existing dark drop-filter family; needs a design eye.
4. **The Inter call**: one family, no shipped mono. If a surface genuinely
   needs a branded mono later, it's an additive minor release.

## Semver

Proposed **major (v2.0.0)**: no API breaks, but the font contract changes
meaning (`--font-display` serif → sans) and three Fontsource packages are
removed — consumers importing `fonts.css` re-render every text surface.
Everything else is additive-minor and rides along. Final call: release owner.

## Known pre-existing failures (NOT from this PR)

Reproduced on pristine `main` with zero changes:

- `PermissionWrapper > Denied Disabled` and `StreamingText > Default`
  storybook tests fail.
- `size-limit` full-bundle entry is 693 B over its 520 kB limit.

## Follow-ups (out of scope here)

- Snap the hand-tuned dark status values onto ramp steps (visual-churn
  decision, separate PR).
- Brand guidelines doc + adoption rule → `engineering/rules.md` (WS-15
  subtasks).
- Migrate remaining Tailwind color literals in components/stories onto ramp
  steps.
