# Utility Class Conventions

## Objective

Agree how utility classes are written so the markup stays readable as the app grows.

A written convention for the order, grouping, merging, and extraction of utility classes across the app.

## 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. Fix a canonical order for utility classes — layout, then box model, then typography, then colour, then state and responsive variants — and apply it mechanically so that reviewers never argue about it.
2. Write down the extraction threshold in plain terms: how many repetitions, in how many places, before a run of utilities becomes a named component. State the number rather than leaving it to taste.
3. Define which variants and states are permitted inline and which must be expressed as a component variant, so a screen does not accumulate a dozen conditional branches in its markup.
4. Record where handwritten CSS is the correct answer — complex selectors, keyframes, print rules, anything that reads worse as a chain of utilities — so people do not contort the utility vocabulary to avoid a stylesheet.
5. Leave the mechanics of combining conditional classes to Class Merge Helper and the mapping of component variants to classes to Variant Class Generator. This feature sets the rules; those two implement them.

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

- Extracting a component the first time a pattern repeats is premature and produces a primitive with one caller and five props. Hold the repetition threshold, and prefer a second literal copy over an abstraction invented too early.
- Two utilities setting the same property in one class list produce a result decided by stylesheet order rather than by the order they were written, which is why contradictory pairs must be prevented at authoring time rather than reasoned about later.
- Conditional classes must be combined through one agreed mechanism everywhere. Hand-rolled string concatenation in some components and a helper in others guarantees that caller overrides work in one place and not the next.
- The convention needs to say when custom CSS is clearer than utilities, or every rule ends up expressed as utilities regardless of whether that reads well.
- Long utility chains on a single element are a signal, not a style crime. Say what length triggers a look at extraction, and what to do when the element is genuinely one of a kind.
- Arbitrary one-off values written inline defeat the token system entirely. Require a token, or a deliberate documented exception, rather than a raw measurement in the markup.

## 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 convention is written down in the repository, not held as folklore.
- [ ] Utility class order is canonical and applied consistently across the app.
- [ ] The extraction threshold is stated as a number and observed in the codebase.
- [ ] Contradictory utilities on the same element are prevented rather than tolerated.
- [ ] All conditional class composition goes through a single agreed mechanism.
- [ ] Cases where handwritten CSS is preferred are documented with examples.
- [ ] Arbitrary inline values are absent, or carry a documented exception.
- [ ] 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.
