# CSS Layer Strategy

## Objective

Give the stylesheet an explicit order of precedence instead of one decided by accident.

An ordered set of cascade layers — reset, tokens, base, components, utilities, overrides — that every stylesheet is assigned to.

## 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. Define the layer order once, in one file, and have every other stylesheet declare which layer it belongs to rather than relying on the order the bundler happens to emit.
2. Assign the existing stylesheets to layers before changing any of them. The map from current file to intended layer is the whole design decision, and it should be reviewable on its own.
3. Migrate in stages: place everything currently unlayered into a defined position relative to the new layers, then move files into their proper layer one at a time, checking the rendered result after each move.
4. Put third-party and vendor stylesheets into a layer of their own, low in the order, so the app's own rules win without anyone reaching for higher specificity or forced importance to get there.
5. Do not use this to replace what CSS Module Boundaries owns. Layers decide which rule wins when two rules collide; module boundaries decide who is permitted to write a colliding rule in the first place. Keep the two answers separate.

## 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

- Unlayered styles beat every layered style, which means a partial migration can leave the old code stronger than the new. Decide deliberately where unlayered styles sit during the transition, and track the count of unlayered files down to zero.
- Third-party CSS that is not authored by the team must be wrapped into its own layer at import time. Left outside the system it will either overpower the app's components or be fought with importance flags that undo the whole strategy.
- The topmost override layer will become a dumping ground unless entry to it is restricted and reviewed. Define what legitimately belongs there — genuinely exceptional, documented, temporary fixes — and require a reason for each addition.
- Every layer needs a written statement of what may be authored in it and what may not, kept next to the layer definition. Without that, the order survives and the discipline does not.
- Importance flags cut across layers in a way that inverts their order, so any remaining use of forced importance must be inventoried and removed rather than layered around.
- Styles injected at runtime, including anything a component adds to the document head after load, land outside the declared order unless they are explicitly assigned to a layer.
- Layer names that appear in more than one bundle must be declared in the same order in each, or the effective precedence will differ between routes.

## 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

- [ ] The full layer order is declared in exactly one place and imported everywhere.
- [ ] Every stylesheet in the app is assigned to a named layer, with no unlayered files remaining.
- [ ] Third-party CSS is contained in its own low-precedence layer.
- [ ] The override layer has documented entry rules and a listed, justified set of occupants.
- [ ] Forced importance flags are inventoried and no longer used to win the cascade.
- [ ] The rendered appearance of the app is unchanged at the end of the migration.
- [ ] 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.
