# Colour Contrast Guardrails

## Objective

Stop custom colours and themes from making text and controls unreadable.

An audit of the app's colour tokens against WCAG contrast thresholds, fixes made in the palette itself, and an automated check that keeps regressions out.

## 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. Start by auditing the existing tokens: compute the contrast ratio for every foreground and background pair the app actually uses, and report the failures before changing anything.
2. Fix the failures in the palette, not in the components that happen to expose them. Patching one button leaves the same bad pair waiting in nine other places.
3. Check the combinations people forget: disabled text, placeholder text, borders on inputs, icons carrying meaning, and text sitting over images or gradients.
4. Check focus indicators against both the component and the surface behind it. A focus ring that only contrasts with one of them disappears half the time.
5. Add a check that runs in CI over the token pairs and fails the build on a regression, so a designer adding a lighter grey next quarter finds out immediately.
6. Do NOT silently darken a brand colour to make it pass. Report the conflict and propose an adjusted value or a different pairing; changing brand colours without asking is not yours to do.

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

- Opacity changes the effective colour — compute against the composited result, not the declared value.
- Large text has a lower threshold, so an audit that applies one number everywhere will report failures that are not failures.
- Text over photographs has no single background; require a scrim or a solid plate rather than guessing an average.
- Every interactive state needs checking — hover, active, selected, visited, disabled — not just the resting one.
- Dark mode needs its own pass. Inverted palettes routinely fail where the light theme passed.
- Charts and status colours must not rely on hue alone; two hues at equal lightness are indistinguishable to many users regardless of contrast.
- If users can pick their own accent colour, validate it at the point of selection instead of shipping a broken theme.

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

- [ ] Every foreground and background pair in use has been measured and the failures reported.
- [ ] Body text meets WCAG AA, and non-text UI (borders, icons, focus rings) meets 3:1.
- [ ] Fixes were made in the colour tokens rather than in individual components.
- [ ] Disabled text, placeholder text, and text over images all pass or are explicitly documented as exempt.
- [ ] Contrast is verified in every theme and every interactive state.
- [ ] A CI check fails the build when a new token pair drops below threshold.
- [ ] 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.
