# Connection Status Indicator

## Objective

Tell people the app lost the server before their next click fails.

A persistent indicator that reports the app's real connectivity to the backend, and what the user can do about it.

## 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. Derive connection state from the outcome of real requests — timeouts, network errors, repeated failures — not from a browser flag.
2. Distinguish at least four states: device offline, server unreachable, realtime channel dropped, and session expired. Each gets different copy, because each has a different fix.
3. Show a reconnect or retry action on the indicator, and reconnect automatically with backoff in the background.
4. Clear the indicator only after a successful request, not after a single successful ping to something unrelated.
5. Do NOT trust navigator.onLine as the source of truth. It reports whether a network interface exists, which is true on captive-portal wifi, on a VPN that has dropped, and when your own API is down.

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

- Require two or three consecutive failures before showing the banner, so a single dropped request does not flash a scary message.
- Backoff must be capped and jittered, or every client reconnects at the same instant and finishes the server off.
- A 401 after reconnect is a session problem, not a network problem — route the user to sign in rather than retrying forever.
- Degraded is a real state: requests succeeding slowly deserve a quieter message than requests failing.
- The indicator must not cover a fixed bottom nav, a toast region, or the primary action on mobile.
- Announce state changes to assistive technology once, not on every retry attempt.

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

- [ ] Connection state is computed from actual request outcomes.
- [ ] Offline, server-unreachable, realtime-dropped, and session-expired are shown as distinct states with distinct copy.
- [ ] Brief network blips do not cause the indicator to flicker.
- [ ] Reconnection is automatic with capped, jittered backoff, and a manual retry is available.
- [ ] The indicator never obstructs navigation or primary actions.
- [ ] State changes are announced once via an aria-live region.
- [ ] 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.
