# Previous and Next Record Navigation

## Objective

Move through a result set one record at a time without going back to the list.

Previous and next controls on a detail view that walk the same sequence the user's list produced.

## 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. Derive the sequence from the filter, sort, and search that were active on the originating list — not from the table's default ordering. A different order than the list the user came from is a bug, not a fallback.
2. Carry the sequence identity in the URL so the detail page is shareable and survives a refresh.
3. Disable the controls at genuine boundaries and make that state visible; do not fetch a page of results just to discover there is nothing after the last record.
4. Prompt before leaving a record with unsaved changes, and keep the user on the record if they cancel.
5. Capturing where the user came from and returning them there is owned by Return-to-Origin Navigation; extend that rather than building a second one. This feature only walks the sequence.
6. Bind keyboard access (conventionally J/K or the arrow keys with a modifier) and do not swallow those keys while a text field is focused.

## 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 current record can be deleted while it is open — move to the neighbour and say what happened, do not dead-end.
- Records inserted or edited by other users can shift the sequence; decide whether the sequence is a snapshot or live, and be consistent about it.
- A record that no longer matches the active filter after an edit must still be navigable away from.
- Crossing a pagination boundary must load the next page transparently rather than stopping at position 25.
- A user arriving by direct link has no sequence — hide the controls rather than inventing one.
- A record the user may see in a list but not open must be skipped or shown as denied, never a blank page.

## 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 traversal order matches the list's active filter and sort exactly.
- [ ] The sequence survives refresh and is encoded in the URL.
- [ ] Boundaries disable the controls without an extra request.
- [ ] Deleted or filtered-out records do not strand the user.
- [ ] Pagination boundaries are crossed transparently.
- [ ] Unsaved changes are protected before navigating away.
- [ ] 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.
