# Theme Migration Guard

## Objective

Keep saved themes working when the tokens underneath them are renamed or replaced.

A versioned migration path that upgrades stored themes as the token set changes beneath them.

## 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. Stamp every stored theme with the version of the token set it was written against, and refuse to apply a theme whose version you do not recognise rather than applying it partially.
2. Write each token change as an explicit forward migration step — a rename, a split of one token into two, a merge, or a removal — and run the steps in order. Inferring the mapping by matching names produces plausible and wrong results the first time a token is split.
3. Keep a record of which values a user actually chose and which were inherited from defaults, so a migration can carry the deliberate overrides forward and let everything else be regenerated from the current defaults.
4. Supply a value for every token that becomes required, derived from the theme's existing values where a sensible derivation exists and from the shipped default where it does not. A theme missing a required token must never reach the renderer.
5. Do not migrate lazily on read without persisting the result. A theme that is re-migrated on every load will be migrated twice by two tabs, will hide the cost of a slow migration, and will keep failing in exactly the same way with no record of it having failed before.

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

- Every saved theme needs a version stamp, because a theme with an unknown provenance cannot be migrated safely and must be refused rather than half-applied.
- User overrides have to survive the migration intact and distinguishable from inherited defaults, or a migration quietly resets the one colour somebody cared enough to change.
- Tokens that become required need a supplied value — derived where a derivation makes sense, defaulted where it does not — so no theme ever reaches the renderer incomplete.
- A migration interrupted midway must be recoverable. Write the result atomically, or keep the original alongside the new one until the migration is confirmed complete, so a failed upgrade does not leave a theme that is half old and half new.
- Two tabs or two devices may attempt the same migration at once; make the operation idempotent so running it twice produces the same theme as running it once.
- A theme created by a newer version of the app than the one running must be refused with a clear message rather than downgraded by guesswork.
- Keep the previous version of a migrated theme long enough for someone to revert, since a bad migration is usually only noticed after the user has closed the tab.

## 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 stored theme carries the token-set version it was written against.
- [ ] Migrations are explicit ordered steps, not name matching.
- [ ] User-chosen values are preserved through migration and remain distinguishable from defaults.
- [ ] Newly required tokens receive derived or default values, and no incomplete theme reaches the renderer.
- [ ] An interrupted migration leaves either the original theme or the fully migrated one, never a mixture.
- [ ] Running a migration twice produces the same result as running it once.
- [ ] A theme from a newer version is refused with a clear message.
- [ ] 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.
