# Spacing Scale

## Objective

Reduce the app's spacing to a small set of steps that repeat across every surface.

A named spacing scale covering gaps, padding, and section rhythm, with every existing value in the app mapped onto it.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Collect every spacing value currently in the codebase and count how often each appears. The result is usually a handful of genuine intentions surrounded by dozens of near-misses that were nudged once and never revisited.
2. Define a scale that is fine at the small end, where control padding lives, and coarse at the large end, where section separation lives. A linear scale gives too many indistinguishable large steps and too few small ones.
3. Separate the tokens used inside a component from the ones used between components and between page sections, and name them so the difference is obvious. The same number serving both purposes means a change to button padding also changes the gap between sections.
4. Migrate one component family at a time and remove the old values as you go. Do not add the scale alongside the existing spacing and leave both in use — a half-migrated system is less consistent than the one it replaced, because now there are two right answers.
5. Display Density owns switching the whole app between comfortable and compact; this entry owns the steps themselves. Express the scale so a density mode can rescale it, and leave the mode switching to that feature.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Spacing inside a component and spacing between components change for different reasons and at different rates. Keep them in separate token groups, or tightening a card's padding will also close up the gap between cards.
- A density mode has to rescale the whole scale coherently. Values that are hardcoded, or expressed in units the mode cannot touch, will stay at their comfortable size while everything around them tightens.
- Optical alignment sometimes demands a value that is not on the scale — an icon that needs a half step to sit on the text baseline, a badge that needs to overlap its anchor. Allow those, name them, and record why, rather than pretending the scale covers every case.
- Values that sit close to a token but not on it are the ones that erode the system. Flag them during review so a stray value has to be justified rather than merged unnoticed.
- Adjacent margins can collapse into one another, so a value that looks correct in isolation produces a smaller gap in place. Prefer a single owner of each gap over margins on both sides.
- The scale must hold across breakpoints. A section gap that is right on a wide screen is usually too large on a phone, so define which steps change responsively and which are fixed.
- Do not renumber or reorder the scale after adoption. Every existing usage silently shifts, and the change is invisible in review.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] A single spacing scale is defined, with steps that are fine at the small end and coarse at the large end.
- [ ] Internal component spacing and layout spacing are separate named groups.
- [ ] Every migrated component uses scale tokens, with no leftover hardcoded values alongside them.
- [ ] The scale can be rescaled as a whole for a density mode.
- [ ] Off-scale values exist only as documented optical exceptions with a recorded reason.
- [ ] Near-token values are flagged during review rather than merged unnoticed.
- [ ] Which steps change at each breakpoint is defined and applied consistently.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
