# Device Preview Toolbar

## Objective

Check the design at phone, tablet, and desktop widths without leaving the editor.

A toolbar above the editor canvas that constrains the canvas to phone, tablet, or desktop width so the author can see how the layout responds.

## 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. Take the widths from the app's real breakpoints. If a responsive token system or a responsive QA matrix already defines them, read from that source rather than inventing round numbers that do not match anything the app ships.
2. Scale the frame down to fit the available editor space instead of forcing the page into a horizontal scrollbar. The author is checking layout, not driving a sideways scroll.
3. Keep the canvas fully editable at every width. Clicking, dragging, and inline text editing must land on the right element after scaling, which means the pointer coordinates have to be mapped through the scale factor.
4. Preserve the canvas scroll position across a width change so the author stays on the section they were working on rather than being returned to the top.
5. Do not build a second read-only preview mode alongside the editor. A separate preview drifts out of sync with the editing canvas and doubles the rendering code you have to keep correct.

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

- Constraining the canvas by adding a horizontal scrollbar defeats the purpose. Scale the frame proportionally to fit the available width and show the true pixel width as a label.
- Scaling a frame breaks hit testing unless coordinates are transformed. Dragging, selection handles, and inline text editing must all still land on the element under the cursor at every zoom level.
- Switching from desktop to phone must not scroll the author back to the top of a long page. Keep the vertical position anchored to the same content, not to the same pixel offset.
- Arbitrary widths mislead. Use the breakpoints the stylesheet actually declares, so a layout that looks correct in the toolbar is correct in a real browser.
- The chosen width should persist per person across sessions, since someone designing a mobile-first page does not want to reset to desktop on every load.
- Content that depends on the viewport rather than the container, such as sticky headers or full-height sections, will not respond to a scaled frame. Say so in the interface rather than showing a misleading result.
- Very narrow widths on a small laptop screen leave the frame tiny. Set a floor on the scale factor and allow scrolling below it rather than rendering something unreadable.

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

- [ ] Preview widths match the breakpoints declared in the app's stylesheet.
- [ ] The canvas scales to fit rather than producing horizontal page scroll.
- [ ] Selection, dragging, and inline editing work correctly at every preview width.
- [ ] Scroll position is preserved when the width changes.
- [ ] The last chosen width is remembered per user across sessions.
- [ ] There is one canvas, not a separate preview renderer.
- [ ] 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.
