# DESIGN.md (`@aisquare/ui`)

This package is AISquare's design system: **one package, three layers** (stock shadcn/ui v4 primitives, brand-flavored primitives, and composed AISquare components), all re-themed against the brand tokens. It **replaced `@aisquare/pretty` as of v1.0.0** (pretty is retired; everything it owned now lives here). The design rules below are the quality bar every layer holds to.

## The three layers

Closest-to-upstream first (this mirrors the `src/` layout and the playground nav):

- **Stock shadcn** (`src/ui/`): byte-for-byte shadcn primitives, re-themed by tokens only. No motion vocabulary, glow shadows, or AIKO-flavored defaults are added here. Two reasons for the restraint. First, upstream parity: anyone landing on a shadcn doc page sees a recognizable component when they import from `@aisquare/ui/stock`. Second, bundle weight: a consumer who just wants a Dialog should not pay for motion / glow / signal hooks. Always reachable via the `@aisquare/ui/stock` subpath.
- **Brand primitives** (`src/primitives/`): brand-flavored wrappers with the same API surface plus AISquare styling, motion, and defaults (Button, Input, Card, Badge, Tooltip, Spinner, AttributionChip, RedlineSpan, and more). This is where the house style that used to live in pretty now lives.
- **Composed components** (`src/components/`, incl. `src/components/blocks/`): higher-level AISquare-original patterns (the `Aiko*` surface system, `ChatInput`, the chat-block renderers, `CommentPopover`, `DiffRow`, the v1.4 port-wave components, and more).

When a consumer wants the unstyled stock version of a primitive that has a brand wrapper, they import it from the stock subpath:

```ts
import { Button } from "@aisquare/ui";              // brand-flavored
import { Button as StockButton } from "@aisquare/ui/stock"; // plain shadcn
```

## Token contract

Every class string reads from CSS variables, never hardcoded values. The tokens are defined in [src/tokens/tokens.template.css](src/tokens/tokens.template.css) and mapped to utilities by [tailwind.preset.ts](tailwind.preset.ts).

**Foundations (primitive) tier.** Beneath the semantic tokens sits an absolute primitive tier (WS-15 token set): color ramps 50→950 (`navy`/`accent`/`slate` + the status hues), the 4px spacing vars, radius primitives, `--motion-*` durations/easings, the 12-column grid contract, and the `--elevation-1..4` ladder. Values live in [src/lib/foundation-tokens.ts](src/lib/foundation-tokens.ts), are injected by the preset's `foundationTokensPlugin`, and are mirrored opt-in at [src/tokens/foundations.css](src/tokens/foundations.css) — with [src/lib/foundation-tokens.test.ts](src/lib/foundation-tokens.test.ts) asserting mirror parity (TS ↔ CSS, both directions) and the Tailwind-verbatim ramp invariant (the preset shadows `slate`, so retuning it is a silent recolor — the test makes that impossible to ship unnoticed). Primitives never flip under `.dark` (elevation excepted); the semantic tokens remain the mode-aware layer components consume. Ramp anchors: `navy-900` = light `--primary` (#071E3C), `accent-500` = `--accent` (#1F84FF), status 500s = their semantic tokens. The whole tier is documented live in Storybook under **Foundations** (Colors, Typography, Spacing, Radius, Shadows, Motion, Grid & Layout).

Sidebar- and Chart-specific variables (not every consumer loads them) live in [src/tokens/shadcn-extras.css](src/tokens/shadcn-extras.css). They are opt-in: only the apps using Sidebar / Chart import that file. If a new primitive needs another var, add it there in HSL, document the default, and pre-tune it for the deep-navy `--primary`.

**No-framework consumers.** [docs/aisquare-foundations.css](docs/aisquare-foundations.css) mirrors the full foundations tier (primitives + semantics) as a standalone, dependency-free stylesheet — for prototypes, non-React/non-Tailwind stacks, or handing brand tokens to an AI agent that isn't wiring up the package. It is documentation, not a build artifact: keep it in sync by hand when `tokens.template.css` / `foundation-tokens.ts` change.

## Shape system: plain rounded surfaces (the Onyx hex-notch is retired)

Every clickable/display surface is a plain shape now — no `clip-path`, no
chamfered corner, no offset-key sibling layer:

- **Buttons are a full pill** (`rounded-full`) — this is the shape the
  product actually ships (verified against the deployed app's default button
  chrome), with flat token colors and a simple hover/focus state. No
  gradient, no shadow-depth press, no translate-on-active by default.
- **Cards, panels, and fields use the standard `--radius`-derived Tailwind
  scale** (`rounded-md/lg/xl/2xl`) with a real CSS `border` — clickable and
  static cards share identical chrome now (previously only static cards were
  round; clickable ones were notched).
- **Stock `ui/button.tsx` stays genuinely upstream shadcn** (`rounded-md`,
  real shadcn v4 classes) — the pill is a brand choice (`primitives/Button`),
  not a shadcn one. Don't pill-ify the stock layer.

Wiring is unchanged and simpler than before — one preset import, no separate
material stylesheet to reason about:

```ts
// tailwind.config.ts
import aisquare from "@aisquare/ui/tailwind.preset";
export default { presets: [aisquare], content: [/* ...incl. @aisquare/ui */] };
```

```css
@import "@aisquare/ui/tokens/tokens.template.css";
@import "@aisquare/ui/tokens/fonts.css";
/* only if you use Sidebar / Chart: */
@import "@aisquare/ui/tokens/shadcn-extras.css";
```

**Retired — the hex-notch clip-path system.** `nx-l/s/m/b/t`, `nx-rim` keyline
overlays, the offset-key `raise`/`key` press, `.hex`, and the `elev-*`
filter-based drop-shadows (a clip-path workaround) are gone — every
primitive and composed component (`Button`, `KeycapButton`, stock
`Button`/`Toggle`/`ToggleGroup`, `Card`, `NotchedInput`, `NotchTextarea`,
`NotchPanel`, `NotchSegmented`, `GlassSurface`, `StudioCard`,
`CollectionCard`, `ExperienceCard`, `ToolCard`, `SpotlightCard`, `DepthCard`,
`BrandTile`, `ArtifactTile`, `IconButton`, and the rest) is a plain
`rounded-*` shape with a real `border`/`box-shadow` now.
[src/lib/onyx-material.ts](src/lib/onyx-material.ts), the preset's
`glassMaterialPlugin`, and [src/tokens/onyx-hex.css](src/tokens/onyx-hex.css)
(filename is legacy) still exist, but now carry ONLY the **glass** material
(`glass.back/panel/composer`) for floating surfaces (`GlassSurface`) — that
was always a separate system from the notch (`backdrop-filter` and `filter`
cancel on one DOM node, which is why it needed its own two-layer split, not
because it depended on clip-path). Removing `nx`/`hex`/`raise`/`key`/`elev`
and the geometry constants from the public barrel is a breaking change
(major version bump) — real consumers may still import them directly.

## Anatomy

The rules every layer follows, applied to anything we add on top of an imported shadcn file:

- `cn(...)` from `./lib/cn` for className composition. Never inline `clsx`.
- `cva` for variants where a primitive has them.
- `data-slot` attributes on every Radix-backed element (shadcn v4 already does this; don't strip it).
- No hardcoded colors (`#hex`, `bg-blue-500`) or radii (`rounded-[12px]`); every value comes from a token class.
- Motion is a first-class, refined-by-default layer (motion tokens `--ease`/`--ease-snap`/`--dur-*`), always gated by `useReducedMotion()`. `motion` + `gsap` are real dependencies of the branded `.` entry; consumers who want zero JS motion import from `@aisquare/ui/stock`. (Supersedes the v1 "no motion defaults" stance.)
- No em-dashes in default rendered strings.
- A11y: keep every `aria-*`, `role`, `sr-only`, `focus-visible:` class shadcn ships. Don't trim them.
- SSR-safe: no `window` / `document` at module top.

## Field vs halo vs beam (don't rebuild one as another)

Four primitives add ambient/edge light. They are easy to confuse and easy to
accidentally re-implement, so keep them straight:

- **Field** — `<MoodLighting>` ([src/components/MoodLighting.tsx](src/components/MoodLighting.tsx)).
  A full-bleed *ambient backdrop*: slow drifting radial blobs plus an optional
  cursor glow, behind reserved-moment surfaces (hero / empty / onboarding /
  preview-stage). Renders `absolute inset-0 z-0 pointer-events-none aria-hidden`;
  wraps no content. Token contract `--mood-field-1/2`, `--mood-glow`,
  `--mood-stage-bg` (a FIXED dark mat for `tone="stage"`), `--mood-stage-glow`.
- **Perimeter halo** — `<BorderGlow>` ([src/components/BorderGlow.tsx](src/components/BorderGlow.tsx)).
  A steady or breathing glow *around a surface's perimeter* ("active / special /
  stay-here").
- **Sweeping arc** — `<BorderBeam>` ([src/lib/border-beam.tsx](src/lib/border-beam.tsx),
  NOT under `src/components/`). A bright arc rotating *around the border* (used
  internally by `Badge variant="premium"`).
- **Traveling editing accent** — `<EditingBeam>` ([src/components/EditingBeam.tsx](src/components/EditingBeam.tsx)).
  A beam *gliding across a block* being rewritten ("processing, hands off").

Rule of thumb: if you want ambience *behind* content, it is the field
(`MoodLighting`); if you want light *on the edge* of a surface, it is one of the
border effects. Do not duplicate one as the other.

## Shape application rule (post-notch)

No surface gets a notch anymore — the rule that used to gate *which* surfaces
earned the hex-notch is retired along with the notch itself. What replaces
it, for anything you touch or add:

- **Buttons are pills** (`rounded-full`) — `Button`, `KeycapButton`. Round
  icon controls (`IconButton`, `Switch`) keep their own circle/pill shape,
  unrelated to this rule.
- **Cards, panels, tiles, and fields are the standard rounded-rect scale**
  (`rounded-md/lg/xl/2xl`, picked by surface size — see
  [src/primitives/Card.tsx](src/primitives/Card.tsx) for the reference:
  `rounded-2xl` for every card, clickable or static, same chrome either way).
- **Stock `ui/button.tsx` stays `rounded-md`** — genuine upstream shadcn, not
  a pill; the pill is a brand-layer choice.
- **A real CSS `border` is the outline everywhere** — no clip-path, so no
  `nx-rim` ring-overlay `<span>` is needed; `focus-within:border-ring` /
  `has-[[aria-invalid=true]]:border-destructive` express state changes
  directly.

Nothing in the library passes `nx.*`/`elev.*`/`.hex`/`.raise` classes anymore
(see the "Retired" callout above) — if you land on one that does, it's a
regression, not an intentional not-yet-migrated component; fix it rather than
copying the pattern into something new.

## Adding a new primitive

1. Add the name to the `V0_1_0` list (or run `--all`) in [scripts/import-from-shadcn.mjs](scripts/import-from-shadcn.mjs).
2. Run `pnpm import:shadcn <name>`.
3. Add `export * from "./ui/<name>";` to `src/index.ts`.
4. If it needs a CSS variable the tokens don't ship, add it to `src/tokens/shadcn-extras.css` in HSL with a one-line comment explaining intent.
5. Add a `.meta.json` next to it (copy [src/_template/component.meta.json](src/_template/component.meta.json); see [src/_template/README.md](src/_template/README.md)), then run `pnpm generate:registry` to refresh [registry/components.json](registry/components.json). This is what makes a new component discoverable to an AI agent consuming the library — see [AGENTS.md](AGENTS.md).
6. `pnpm typecheck && pnpm lint && pnpm build && pnpm size`, green before opening a PR.
7. Bump the version per `CONTRIBUTING.md`.

## Where the house style lives

There is no separate brand package anymore. The motion vocabulary, brand surfaces, and AIKO voice / copy rules that used to live in `@aisquare/pretty` now live in this repo:

- AIKO voice + copy rules: applied in the composed `Aiko*` components.
- Motion vocabulary: `src/lib/` (the `springs` / `easings` / `durations` / `transitions` presets).
- Brand surfaces (glow scale, mesh gradients, brand radii / shadows): the Tailwind preset + tokens.

If a consumer wants a glow-on-hover Button, the answer is the brand-flavored `Button` from the main `@aisquare/ui` entry (not the stock subpath).
