# CSS Custom Property Bridge

## Objective

Expose the design tokens as CSS variables so theming and styling can change at runtime.

A generated layer of CSS custom properties mirroring the token source, which components are expected to style against.

## 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. Generate the custom properties from the same token source the rest of the app already uses, so a token and its variable can never disagree. A hand-maintained parallel list will drift within a release.
2. Decide and document where each group of variables is declared — the document root for global theme values, a container for anything that must vary per section or per component — and apply that rule consistently.
3. Fix the naming scheme and treat the generated names as a public contract, since stylesheets, components, and any customer-facing theming all reference them by name.
4. Give every consumption point a fallback value so a component still renders sensibly when a variable is absent, which is the normal state during theme switching and partial loads.
5. Coordinate with Unused Token Detection: that feature judges whether a token is still referenced, and it must be able to see references made through these variables, or every bridged token will look dead.

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

- A theme that defines only some of the variables must not leave components unstyled. Every reference needs a fallback, and the base theme must supply a complete set beneath any partial one.
- Scope decides behaviour: a variable declared at the document root affects everything, one declared on a container affects only its subtree. A value intended to vary per section but declared globally will leak into every other section on the page.
- Generated names must stay stable across builds. A name that changes because a build step reordered or hashed it will break every stylesheet and every customer theme that referenced it.
- Custom properties are readable by anything running on the page, so nothing derived from configuration, keys, entitlements, or internal identifiers may be emitted into them. Only presentation values belong here.
- Values that participate in animation or transition must be declared with a type, or the browser will step between them rather than interpolating.
- Switching themes must not repaint through an intermediate state where some variables have updated and others have not.
- A variable referencing another variable that is itself undefined resolves to nothing rather than to the fallback the author expected, so chains need a fallback at each step.

## 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 custom property is generated from the token source, with no hand-maintained duplicates.
- [ ] The scope of each variable group is documented and matches where it is declared.
- [ ] Generated names are stable across builds and treated as a public contract.
- [ ] Every consumption point carries a fallback and renders sensibly under a partial theme.
- [ ] No configuration, credential, or internal identifier is exposed as a custom property.
- [ ] Theme switching applies in one pass with no visible intermediate state.
- [ ] Unused Token Detection resolves references made through the bridge.
- [ ] 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.
