Workbench / The TiffinBox Build
S14 · V2·14
A Design System in Tokens
Volume 2, chapter 14. The stage 13 pages get one stylesheet: cascade layers in a fixed order, design tokens as the only place colours and sizes are written, light and dark themes from the user's preference, a mobile-first layout that never scrolls sideways at 320 px, and forced-colours and reduced-motion support. A Playwright audit checks every page in both themes for reflow, text and control contrast, text spacing, visible focus in forced colours and CSP violations. The audit first passed a deliberately broken stylesheet, because a server left over from a crashed run was answering instead; the fix, and a mutation test that proves the audit can fail, are part of the stage.
Stage 14: a design system in tokens
The brief
What you'll build.
web/app.css, in six cascade layers (reset, tokens, base, layout, components, utilities):- tokens for colour, space and type, redefined for dark mode under
prefers-color-scheme; - a page frame (header, main, footer) and components for the menu, the order lines and the error summary, all mobile-first;
- user preferences: forced colours, reduced motion, the user's font size (everything in
rem). web-tests/stage14.py: an audit of 7 pages × 2 colour schemes × 4 widths for reflow (WCAG 1.4.10), text contrast (1.4.3), control border contrast (1.4.11), text spacing (1.4.12), focus visible in forced colours (2.4.7) and CSP violations, plus screenshots.web-tests/validate.shalso validates the CSS.- ADR-0016: the CSS architecture.
Why a real company needs this. A stylesheet without structure becomes a specificity war within a year: every fix is a more specific selector, then an !important. Tokens make themes, dark mode and a later redesign a change in one place. The accessibility checks aren't optional extras: contrast, reflow and focus visibility are legal requirements in many markets (WCAG 2.1 AA is referenced by the EU's accessibility rules and by Indian government guidelines), and they're cheap to keep right with an automated check and expensive to retrofit.
Why now. Chapter 14 is CSS: the cascade and layers, custom properties, layout, responsive design, colour and motion. Stage 13's pages are semantic HTML with no styling to fight against, which is the best possible starting point.
What you'll have at the end.
- one 7.7 KB stylesheet (2.5 KB gzipped) with no framework;
- an audit that passes on every page in both themes, and is proven to fail when the CSS is broken;
- ADR-0016.
Theory to own
- Layers. Why declare all six layer names in one
@layerstatement at the top, and what does a single class incomponentsdo againstul#nav li.active ainbase? - Tokens and nesting. A future "kitchen dashboard" is dark inside a light page. Why do themes built from custom properties nest correctly when themes built from selectors (
.dark p { … }) don't? (Companion, Gaps 1.) - Contrast. What contrast ratio does WCAG 2.1 AA require for body text, for large text, and for the borders of inputs? What counts as "large"?
- Reflow. WCAG 1.4.10 requires no horizontal scrolling at 320 CSS px. Why 320, and what does it have to do with zoom?
- Forced colours. What survives Windows contrast themes (forced-colours mode), what doesn't, and how do you keep buttons and focus visible? (Companion, Gaps 5.)
Build it
Step 1: the layers and a reset that guarantees
Start app.css with @layer reset, tokens, base, layout, components, utilities;. In reset, only guarantees, not looks: box-sizing: border-box, zeroed margins, form controls that inherit the font (font: inherit), images that never overflow, unstyled lists for lists with a class, and [hidden] { display: none !important; }.
Checkpoint. Explain why the [hidden] rule needs !important and why it's safe here.
Step 2: tokens, and a dark theme
In tokens, define every colour as a role, not a hue: --surface, --surface-raised, --surface-sunken, --text, --text-muted, --border, --accent, --accent-text, --focus, --danger, --danger-surface. Add a space scale in rem (--space-1 to --space-8), fluid type steps with clamp() (rem plus a little vw, so they follow both the user's font size and the screen), and --measure for line length. Redefine the colours under @media (prefers-color-scheme: dark), and set color-scheme: light dark (plus <meta name="color-scheme" content="light dark"> in every page, so the browser's own form controls and scrollbars match before the CSS loads).
Checkpoint. Every text/background pair you intend to use meets 4.5:1, and --border meets 3:1 against the surfaces, in both themes. Write down how you checked.
Step 3: elements and the page frame
In base, style elements only (no classes): body text from tokens with a unitless line-height: 1.6 (Companion, Gaps 3); headings from the type steps; links in the accent with a visible underline; one focus style for everything, :focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }; inputs and buttons at least 2.75 rem (44 px) tall; buttons with border: 2px solid transparent; tables full width with right-aligned, tabular-nums amounts. In layout, make body a three-row grid (header, main, footer) with min-height: 100dvh, a wrapping header, and main limited to --measure and centred.
Checkpoint. At 375 px, the header's brand and navigation sit on one line; at 1,280 px, the content column stays readable (about 65 characters per line).
Step 4: components, mobile first
Write the menu as a grid of cards that fills each row and never overflows: grid-template-columns: repeat(auto-fill, minmax(min(15rem, 100%), 1fr)). Each card is a flex row with the name (allowed to wrap anywhere) and the price (never wrapping, tabular figures). The order lines are a wrapping flex row: label on the left, a 4.5 rem quantity box on the right, dropping below the label when the name is long. The error summary gets a thick start border in --danger, not just a red background.
Checkpoint. No page scrolls sideways at 320 px, including with a dish named with one very long word.
Step 5: forced colours and reduced motion
In utilities, wrap any motion override in @media (prefers-reduced-motion: reduce), and in @media (forced-colors: active) give the cards and the error summary CanvasText borders. Everything else already works in forced colours: transparent button borders, outline focus.
Checkpoint. In Playwright with forced_colors="active", tab through the menu, order and review pages; every stop shows an outline.
Step 6: an audit, and a test that the audit can fail
Write web-tests/stage14.py. Start the server, place one order, then for each colour scheme (color_scheme="light"/"dark") and each page:
- at widths 320, 375, 768 and 1,280 px, check
scrollWidth <= clientWidth(reflow); - for every element with its own text, compute the WCAG contrast ratio of its colour against its nearest non-transparent background, and compare with 4.5 (or 3 for large text);
- for every visible input and select, compare its border colour with its parent's background (3:1);
- count CSP violations in the console.
Then inject the WCAG 1.4.12 text-spacing styles at 320 px and look for clipped or overflowing elements; and in forced colours, press Tab through three pages and check that each focused element has an outline. Run it. Then break the CSS on purpose (a pale --text-muted and --border, border: 0 on buttons, and focus as a box-shadow) in a copy of web/, run the audit against the copy (WEB=/tmp/webmut), and check that it fails.
Checkpoint. The real stylesheet passes with 0 failures; the broken copy fails, with the failures pointing at the right elements.
Step 7: validate the CSS, and look at it
Add java -jar "$VNU" --css web/app.css to validate.sh. Take screenshots of the menu, order and review pages at 375 px in both themes, the menu at 1,280 px, and the review in forced colours, and look at every one.
Checkpoint. validate.sh prints both "valid" lines; the screenshots match what you intended.
Senior and architect lens
1. Architecture is mostly about the order of priorities. Six named layers say, once, what may override what: resets give guarantees, tokens give values, base styles elements, layout places regions, components style pieces, utilities and user preferences win. Every later decision ("where does this rule go?") has an answer, and no rule needs a more specific selector to win.
2. ADR-0016: CSS architecture. Write it with these points:
- Decision: one stylesheet in six cascade layers; design tokens as custom properties are the only place colours, space and type sizes are written; no CSS framework.
- Themes: colour roles, not hues; light and dark from
prefers-color-scheme; nested themes by redefining tokens on a container. - Accessibility contract: WCAG 2.1 AA contrast for text and controls, reflow at 320 px, text spacing, visible focus in forced colours, reduced motion; enforced by
stage14.pyin every build. - Manual checks: hover and active states, images, focus-ring contrast, real screen readers (chapter 20).
- Revisit when: a second frontend package (stage 30's workspace) needs the same tokens, which makes a token source file (JSON generating CSS) worth it.
3. Tests that can't fail are worse than no tests. They give confidence they haven't earned. A mutation test (break the code on purpose, check the test fails) is cheap insurance for any check that "always passes", and the stale-server bug here is the kind that hides for months in CI.
4. Accessibility is cheapest as defaults. Almost nothing in the stylesheet is "accessibility code": real borders instead of border: 0, outlines instead of box-shadows, rem instead of px, tokens checked for contrast. Designing those defaults in at the start costs nothing; retrofitting them into hundreds of components is a project.
5. Measure what users get, not what you wrote. The audit computes contrast on rendered pages, not by reading token pairs, because the pairs that fail are often the unplanned ones (muted text on a sunken header, text on a hover colour). The same principle drives stage 32's real-user monitoring.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| a second frontend needs the same look | tokens live in one CSS file | stage 30 (a shared token source in the workspace) |
| a user wants dark mode on a light system, or the reverse | the theme only follows the system preference | stage 25 (a remembered preference, applied without a flash) |
| the stylesheet grows past what one person holds in their head | one file, no ownership boundaries | stage 21 (UI architecture), stage 30 (workspace) |
new CSS features (anchor positioning, @scope) are tempting |
no policy on browser support | stage 27 (budgets and support policy, from RUM) |
| a CSS change breaks a page nobody looked at | the audit checks rules, not appearance | stage 26 (visual regression screenshots) |
Evidence
web-tests/stage14.pyprintsfailures: 0on the real CSS, and the mutation run'sfailures: 76(or your number) is in the README.- The stale-server fix (port check and
atexit) is in the script, with a comment saying why. validate.shprints "valid" for the pages and forapp.css.- Screenshots: menu, order and review at 375 px in light and dark, the menu at 1,280 px, the review in forced colours.
stage12.pyandstage13.pystill pass.- ADR-0016 is committed.
Carry forward
- Tokens are the only colours and sizes any later UI may use: stage 15's search results, stage 17's kitchen display (dark by default), stage 22's React view (the same stylesheet), and stage 30's workspace packages (through a shared token source).
- The accessibility audit runs on every stage from now on, and stage 20 extends it with a real screen reader and keyboard-only flows.
- The mutation test becomes a habit: stage 26 applies it to the test suite as a whole.
- The layer order is the contract stage 21's UI architecture builds on.