# Product Image Gallery

## Objective

Show a product's photos as a browsable set that zooms, swipes, and never jumps the page.

A gallery on the product page with a main image, thumbnails, zoom, and touch swipe, driven by an ordered set of product photos.

## 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. Reserve the main image's space from its stored aspect ratio before anything loads, so the page never reflows when the photo arrives. Serve a small blurred or solid placeholder in that box rather than leaving it empty.
2. Extend the app's existing Image Lightbox for the full-screen zoomed view rather than building a second overlay. The gallery supplies the ordered image set and the current index; the lightbox handles the overlay, focus trapping, and escape.
3. Serve appropriately sized images for the thumbnail, the main view, and the zoomed view, using the app's existing Image Compression or media pipeline. Do not scale one large original down in the browser.
4. Store the display order and per-image alt text with the product, and require alt text at upload. If the app has AI Image Alt Text, use it to draft the text and let the merchant correct it.
5. Do not preload every image at full resolution. Load the first image and its neighbours eagerly and the rest as they are approached, because a product with fifty photos otherwise costs several megabytes before the page is usable.

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

- The main image's box must be sized from its known aspect ratio before load, or the price and add-to-cart controls will jump down the page as each photo arrives and shoppers will click the wrong thing.
- Pinch zoom on touch must scale the image without hijacking the page scroll. Consume the gesture only while zoomed past the natural size, and release it back to the page at the edges so the shopper is never trapped inside the gallery.
- When the variant changes, the thumbnails and the main image must switch together and the selected index must reset to a valid position, rather than leaving a highlighted thumbnail that no longer matches what is shown.
- A product with one image must show no thumbnail strip, no arrows, and no swipe affordance, and a product with fifty must give the strip its own scroll with a clear indication of position rather than growing the page.
- Arrow keys must move between images when the gallery has focus, and every image needs alt text describing that specific photo. Repeating the product name on all fifty is the same as having none.
- A missing or failed image must render a labelled placeholder in the reserved box and stay in the sequence, so the count and the thumbnail positions do not shift.
- Swipe must not fire on a vertical scroll that drifts sideways. Require a dominant horizontal movement before taking over the gesture.
- Autoplaying carousels and animated transitions must respect a reduced-motion preference and must never advance on their own while the shopper is looking at a photo.

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

- [ ] The layout does not shift when any gallery image loads.
- [ ] Thumbnails, the main image, and the zoomed view each receive an appropriately sized file.
- [ ] The full-screen view is the app's existing lightbox, not a second overlay implementation.
- [ ] Changing the variant updates thumbnails and main image together with a valid selected index.
- [ ] Galleries of one image and of fifty images both render sensibly without arrows or an overgrown strip.
- [ ] Arrow keys navigate the set and each image carries its own alt text.
- [ ] Pinch zoom works on touch without trapping the page scroll.
- [ ] 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.
