# Picture-in-Picture Mode

## Objective

Let viewers pop a video into a floating window and keep using the rest of the app.

A control on the video player that hands the video to the browser's floating always-on-top window.

## 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. Add the control to the app's existing video player rather than building a separate popped-out player. Two players competing over the same element is the main way this feature goes wrong.
2. Keep the app in sync with the floating window in both directions. Pausing, seeking, or changing volume from the floating window must update the in-page player, and closing the floating window must return playback to the page at the same position.
3. Decide explicitly what happens when the user navigates away from the page that owns the video. Either keep the floating window alive and reconnect when they return, or end it cleanly, but do not leave a floating window playing a video the app has forgotten about.
4. Feature-detect and hide the control entirely where the browser does not support it. A visible button that does nothing is worse than no button.
5. Do not fight a sticky or mini player if the app already has one. When the video enters the floating window, collapse the sticky player rather than running both.

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

- Where the browser has no picture-in-picture support, or the site is inside a frame that forbids it, the control must be absent rather than present and inert.
- The viewer can pause, seek, or skip from the floating window's own controls, which the page never sees unless you listen for it. Without that listening the in-page transport shows the wrong state the moment they use it.
- If the video element unmounts while the floating window is open, decide the behaviour before the framework decides it for you. Detaching the element usually kills playback silently mid-sentence.
- A sticky mini player and a floating window both claiming the same video will flip between each other. Make entering one the exit condition for the other.
- When the floating window closes, restore focus to the control that opened it and leave the page scrolled where the viewer was, not jumped back to the player.
- Some browsers require a user gesture to enter this mode. Entering it from a timer or on page load will be refused, and that refusal must not surface as an error.
- Only one floating window can exist at a time. Starting a second video must take over the window rather than fail quietly.
- The control needs a label that says which state it will move to, and it must be reachable by keyboard like the rest of the transport.

## 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 floating-window control appears only where the browser supports it.
- [ ] Playback state, position, and volume stay in sync in both directions.
- [ ] Closing the floating window returns playback to the page at the correct position.
- [ ] The video element unmounting mid-playback has a defined, non-silent behaviour.
- [ ] The app's sticky or mini player yields rather than competing for the same video.
- [ ] Focus and scroll position are restored when the floating window closes.
- [ ] The control is keyboard reachable and clearly labelled.
- [ ] 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.
