# Chart Color Tokens

## Objective

Make every chart in the app draw from one palette that stays readable for everyone.

A token set covering series colours, fills, grid lines, axis and label text, and hover, selected, and muted states across the app's charts.

## 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 the colours currently hardcoded in the app's charts and reduce them to a categorical series scale, a sequential scale, a diverging scale, and a small set of neutrals for grid lines, axes, and labels.
2. Fix the assignment order of the categorical scale so the same series gets the same colour in every chart on the page and across reloads, keyed on the series identity rather than its index in the current filtered result.
3. Define interaction states as derived tokens — hover, selected, muted, and disabled — so a highlighted series is emphasised by dimming the others rather than by swapping in an unrelated colour.
4. Check every adjacent pair in the categorical scale for separation under the common colour-vision deficiencies and for contrast against both the light and dark chart backgrounds, and reorder or replace entries until they hold.
5. Do not treat colour as the only carrier of meaning. Pair it with a shape, dash pattern, or direct label; the textual alternative to the chart itself is owned by Accessible Chart Summary and should not be duplicated here.

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

- Series must remain distinguishable to viewers with deuteranopia, protanopia, and tritanopia, so red against green and any pair separated only by hue at similar lightness has to go — vary lightness as well as hue.
- Semantic colours carry an assumption that is not universal: green for up and red for down inverts in some markets, and in a cost or emissions chart a rising line is bad news. Reserve semantic tokens for genuinely semantic quantities and use the neutral categorical scale for everything else.
- Colour alone cannot carry a series identity. Provide dash patterns or markers for lines, fill patterns or direct labels for areas and bars, so a printed or greyscale copy is still readable.
- A chart with more series than the palette holds must not wrap around and reuse colours, which silently implies two series are the same. Define an overflow behaviour — group the tail into an other bucket, or switch to a scale designed for many categories and add direct labels.
- A single-series chart should use one deliberate colour, not the first entry of the categorical scale by accident.
- Grid lines and axis labels need enough contrast to be read but must sit visually behind the data; give them their own neutral tokens rather than a transparency applied to a series colour.
- Charts exported as images take the theme they were rendered in, so a dark-theme export dropped into a light document must still be legible or must be re-rendered on a light background.

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

- [ ] No chart in the app contains a hardcoded colour value.
- [ ] The same series renders in the same colour across every chart and across reloads.
- [ ] Every adjacent pair in the categorical scale is distinguishable under simulated colour-vision deficiency and in greyscale.
- [ ] Series carry a non-colour identifier as well as a colour.
- [ ] Hover, selected, and muted states are derived tokens applied consistently.
- [ ] Charts with more series than the palette follow a defined overflow rule rather than repeating colours.
- [ ] Grid lines, axes, and labels meet contrast requirements in both light and dark themes.
- [ ] 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.
