# Hydration Mismatch Guard

## Objective

Find and fix the places where the server markup and the first client render disagree.

A detection and prevention pass over every value that can differ between the server render and the browser's first render.

## 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. Walk the known sources of divergence and fix them at the source: timestamps formatted in the server's zone, generated identifiers, locale and currency chosen from a header on one side and a browser setting on the other, and a theme read from storage after the markup was already sent.
2. Format dates, numbers, and currency from an explicit locale and zone resolved once and passed through the render, rather than letting each side pick up its own environment default.
3. Give anything genuinely browser-only — a chart sized to the viewport, a value read from storage, a random promotion slot — a reserved space in the server output that matches the final dimensions, so the swap changes content and not layout.
4. Make a mismatch loud in development and reportable in production: capture which component and which value diverged, so the report names a cause rather than saying the markup did not match.
5. Do not silence the warning globally or paper over a component by forcing it to render only on the client. A blanket suppression hides the next mismatch, which will be a wrong price or a stale permission rather than a stray timestamp.

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

- Dates, generated identifiers, locale, and theme are the four that cause most mismatches, and each needs a stable answer decided once per request rather than independently on each side of the boundary.
- Client-only content must occupy the space it will eventually fill. Rendering nothing on the server and something afterwards shifts everything below it, which is both a visible jump and a layout-stability penalty.
- A mismatch report that says only that the markup differed is not actionable. Capture the component, the offending attribute or text, and both values, or nobody will find it in a large page.
- Suppression must stay narrow and deliberate, attached to the specific element whose difference is expected — a relative timestamp, for instance — and never applied at the root.
- Browser extensions inject attributes and elements into the markup and will trigger mismatches nobody wrote, so reporting needs to tolerate them without drowning real findings.
- Content that depends on the signed-in user must not be cached from a render made for someone else; a mismatch here is a data leak, not a cosmetic bug.
- Feature flags and experiment assignments must be resolved once per request and passed to the client, or the two sides will render different variants of the same page.

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

- [ ] Dates, identifiers, locale, currency, and theme resolve identically on both sides of the render boundary.
- [ ] Client-only regions reserve their final dimensions in the server output and cause no layout shift.
- [ ] A mismatch in development names the component and both differing values.
- [ ] Mismatches in production are reported with enough context to locate the source.
- [ ] Suppression appears only on specific elements with a documented reason, never at the root.
- [ ] The app renders through its main flows with no hydration warnings from its own code.
- [ ] 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.
