# Video Chapters

## Objective

Split a long video into named sections viewers can jump to.

A chapter list beside the video and matching markers on the scrub bar, both seeking on click.

## 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. Extend the app's Custom Video Player rather than adding a second player to host chapters. Chapters are a layer over the existing transport controls.
2. Show chapters in two places that stay in step: a list of titles with start times next to or under the video, and segment markers on the scrub bar. Clicking either seeks to that chapter's start.
3. Keep the current chapter highlighted in the list and scroll it into view as playback moves, including while the viewer is dragging the scrub bar.
4. Accept chapter data from wherever the app already stores video metadata, and validate it on save so a bad chapter list is rejected at authoring time rather than breaking the player.
5. Do not require chapters. A video without them must render exactly as it does today, with no empty list and no gaps in the scrub bar.

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

- Authored chapter timestamps arrive out of order, overlapping, or past the end of the video. Sort them, drop or clamp the ones that cannot be reconciled, and tell the author which were rejected rather than rendering a broken bar.
- On a narrow player the markers crowd into an unreadable stripe. Merge or hide markers below a minimum spacing and keep the list as the reliable way to navigate.
- The active chapter must follow the scrub position while the viewer is dragging, not only update when playback resumes. A list that lags behind the cursor makes scrubbing feel disconnected.
- Support a link that opens the page at a given chapter, using the app's existing Shareable Deep Links conventions rather than a new URL format, and update the address as chapters change without flooding the history stack.
- Some sources cannot be seeked reliably, including certain live and progressive streams. Fall back to a plain, non-clickable list of chapter titles rather than offering jumps that do nothing.
- A chapter title long enough to wrap must not push the video off the layout. Truncate in the list with the full title available on hover and focus.
- Chapter titles are typed by whoever uploaded the video and then render over the player for every viewer who opens it. Escape them on display, and where videos come from users rather than staff, hold new titles for review before the video is public.
- The chapter list must be keyboard navigable and announce the active chapter, not rely on colour alone to show it.

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

- [ ] Chapters appear as both a list and scrub bar markers, and either one seeks on activation.
- [ ] Out-of-order, overlapping, and out-of-range timestamps are rejected or reconciled at save time with feedback to the author.
- [ ] Markers collapse rather than overlap on narrow players.
- [ ] The active chapter stays highlighted and in view during playback and during scrubbing.
- [ ] A chapter deep link opens the video at the right position using the app's existing link format.
- [ ] Unseekable sources show a plain list rather than dead controls.
- [ ] A video with no chapters renders unchanged.
- [ ] 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.
