# Dead CSS Detection

## Objective

Find the selectors that no route or component still renders, and remove them safely.

An audit of the app's stylesheets separating selectors still reachable at runtime from those nothing renders.

## 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. Enumerate the selectors the app ships and match them against the class names and structures the components actually produce, including every route, not only the ones that render on first load.
2. Drive the audit from a real render of the app across its routes, breakpoints, and themes, rather than from static text matching alone. Static matching alone cannot see what a component composes at runtime.
3. Classify each selector as confidently used, confidently unused, or unknown, and require a human decision on everything in the unknown bucket.
4. Remove in small, reviewable batches, each one shippable and revertible on its own, so a regression can be traced to a specific removal instead of to a thousand-line deletion.
5. Do not treat this as the same job as Unused Token Detection. That feature owns the token definitions and their references; this one owns the rules and selectors in the stylesheets, and the two reports must not both claim authority over the same file.

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

- Class names composed at runtime from variables, and class names arriving inside content authored elsewhere such as a CMS body or a user-supplied rich text field, will never appear literally in the source. Both must be treated as live unless proven otherwise.
- Routes that load on demand are the classic false positive: a selector used only by a screen that is never visited during the audit will be reported as dead. Exercise every lazy route, or exclude their stylesheets from the sweep entirely.
- States that are rare or hard to reach — an error banner, a print layout, a disabled control, a high-contrast or reduced-motion variant, an empty result set — are used but almost never rendered during a crawl. Protect them explicitly rather than relying on coverage.
- Every removal batch must go through visual comparison against the previous build across the routes and breakpoints that matter, because the failure mode here is silent and cosmetic rather than a thrown error.
- Selectors that exist purely to override a third-party or vendor stylesheet look unused when the vendor markup is absent from the audit environment, and deleting them breaks the page the moment that widget loads.
- Print styles, email styles, and anything served to a context the audit never renders must be listed as out of scope in the report rather than silently swept.
- Server-rendered markup and client-rendered markup can produce different class sets for the same screen, so an audit run against only one of them is incomplete.

## 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 audit covers every route, including those loaded on demand, plus every theme and breakpoint the app supports.
- [ ] Selectors are classified as used, unused, or unknown, and unknown is never removed automatically.
- [ ] Dynamically composed class names and class names from authored content are excluded from removal.
- [ ] Rare states such as errors, empty states, print, and accessibility variants are explicitly protected.
- [ ] Each removal batch is independently revertible and passes a visual comparison against the previous build.
- [ ] The report states which contexts were out of scope.
- [ ] 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.
