# UI Performance Budget

## Objective

Put a number on how heavy and how slow each screen is allowed to get.

Per-route limits on script, stylesheet, image and font weight, first render, and interaction delay, checked automatically before a change ships.

## 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. Measure the app's current numbers per route first and set the budget slightly under them. A budget invented from a best-practice article that the app already exceeds is ignored on the day it lands.
2. Budget each route separately. The marketing page, the sign-in screen, and the main editor have nothing in common, and one global number either strangles the editor or lets the landing page rot.
3. Cover both weight and behaviour: bytes shipped by category, time to first meaningful render, and the delay between a click and a visible response.
4. Run the check on every proposed change and report the delta against the base branch, so the person making the change sees the cost while they can still do something about it.
5. Do not fail the build on noise. Set a tolerance that absorbs normal variance and fail only on a sustained or substantial regression, because a check that cries wolf gets bypassed and then removed.

## 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 single budget for the whole app is the failure mode to avoid — set limits per route so the heaviest screen does not define the ceiling for the lightest.
- Small variations between runs are normal, so the check must distinguish a genuine regression from measurement noise or it will block work at random.
- Both a cold first visit and a warm repeat visit need budgets. Testing only the cached case hides everything that makes a first impression slow.
- Numbers gathered on a fast laptop over office broadband describe nobody; target a mid-range device on a constrained connection and state which.
- Bundle weight specifically is owned by Bundle-Aware Component Imports, which prevents the regression; this entry measures and enforces the ceiling. Point the budget at the same numbers rather than measuring bundles a second way.
- A budget that is exceeded needs a defined route to a deliberate exception, recorded with a reason, or the only options are to bypass the check or abandon the change.
- Third-party scripts must be counted in the budget, since they are frequently the largest single item and the easiest to add without noticing.

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

- [ ] Each route has its own documented budget for weight, render time, and interaction delay.
- [ ] Budgets were derived from current measurements rather than generic targets.
- [ ] Every proposed change is checked and its delta reported against the base branch.
- [ ] Cold and cached loads are both measured.
- [ ] Measurement targets a stated device class and network profile.
- [ ] Only sustained or substantial regressions fail the check.
- [ ] Exceeding a budget deliberately is possible and leaves a recorded reason.
- [ ] 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.
