# Elevation Layer Map

## Objective

Decide once what covers what, so overlays stop appearing behind the page.

A documented ordering of the app's overlay layers — sticky chrome, menus, popovers, dialogs, notifications — and the rules for how they interact.

## 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. List every element in the app that can overlap something else: sticky headers, bottom navigation, drawers, dropdown menus, popovers, tooltips, dialogs, toasts, and the command palette. Order them by what must always win when two are on screen together.
2. Write down the interaction rules alongside the order, not just the order itself: which layers may open while another is open, which dismiss the layer below, and which trap focus. An ordering without those rules only fixes the paint, not the behaviour.
3. Handle nesting explicitly. A popover opened from inside a dialog must render above that dialog, and closing the dialog must close it, so nested overlays need to know the layer of their opener rather than assuming a global value.
4. Decide how each layer interacts with persistent navigation. A drawer that slides under a fixed sidebar and a toast that lands beneath the mobile tab bar are both the same unspecified overlap, and both need an answer written down.
5. The numeric values themselves belong to Z-Index Registry; this feature owns the ordering and the behaviour, that one owns the tokens and enforcement. Do not define a second set of numbers here.

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

- Any ancestor with a transform, filter, opacity below one, or a containment property creates a local stacking context, and a child inside it cannot escape upward no matter how high its value goes. Those ancestors have to be found and either removed or the overlay rendered outside them.
- Nested overlays — a menu inside a popover inside a dialog — must stack in the order they were opened and unwind in reverse, or dismissing the outer one strands the inner one on screen with no way to close it.
- Persistent navigation is the most common collision. Toasts landing under a mobile tab bar, drawers sliding beneath a fixed sidebar, and sticky action bars fighting a cookie banner all need a stated winner for the edge they share.
- Overlays rendered into a portal at the end of the document leave the DOM order, so keyboard focus and screen reader order must be restored deliberately rather than left to the browser.
- Fullscreen modes, whether the app's own focus layout or the browser's native fullscreen, change what the top layer even is. Anything that must remain reachable in fullscreen has to be rendered inside the fullscreen element.
- Native top-layer elements such as dialogs and popovers ignore the ordering entirely and always paint above everything, which will surprise anyone assuming the map governs all layers.
- Two toasts and a dialog opening in the same frame must not race — the ordering has to be deterministic regardless of which mounted first.

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

- [ ] There is a single written ordering covering every overlapping surface in the app.
- [ ] Each layer's rules state what it may open over, what it dismisses, and whether it traps focus.
- [ ] A popover opened from within a dialog renders above it and closes with it.
- [ ] No overlay is clipped or hidden by a local stacking context.
- [ ] Every collision with persistent navigation has a documented winner.
- [ ] Numeric values come from Z-Index Registry rather than being redefined here.
- [ ] 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.
