# Spreadsheet Viewer

## Objective

Open an uploaded spreadsheet in the browser with its sheets and headers intact.

An in-app read-only viewer for uploaded spreadsheet and delimited files, with sheet tabs and frozen headers.

## 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. Reuse the app's existing file preview surface and Attachment Gallery entry points so a spreadsheet opens where every other file type opens, rather than in a separate viewer page.
2. Parse on the server and send the viewer only the rows it currently needs. Shipping an entire workbook to the browser will stall the tab on any file of real size.
3. Build on the app's existing table components, including its sticky header and frozen column behaviour, instead of a second grid implementation.
4. Keep it read-only and say so. If the app has a CSV Import, link to it from the viewer for anyone who wants the data in the app rather than quietly adding editing here.
5. Do not evaluate formulas. Show what the file recorded and be explicit when a value could not be resolved.

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

- A sheet with a hundred thousand rows or three hundred columns will freeze the browser if every cell is rendered. Virtualize in both directions and keep scroll position stable as rows load.
- Files often store a formula alongside its last computed value, and files written by other tools may store neither. Show the cached value where one exists, show the formula text where it does not, and never show an error token as if it were data.
- Plain text files arrive with semicolons, tabs, or pipes as separators and in encodings other than UTF-8, and guessing wrong produces one column of mojibake. Detect delimiter and encoding, show what was detected, and let the user override it.
- Merged cells and frozen header rows drift out of alignment as soon as rows are virtualized or columns are resized. Measure them explicitly rather than assuming a uniform row height.
- Past a defined row or cell ceiling the viewer should decline clearly and offer the original file for download instead of attempting the render and timing out.
- Spreadsheets carry hidden sheets, hidden columns, and cell comments that the uploader may not know are there. Decide whether to surface them and be consistent, rather than exposing them by accident.
- Dates, currencies, and percentages lose their meaning when rendered as raw serial numbers. Apply the recorded cell format, and render numbers right-aligned so columns read as columns.
- A corrupt, password-protected, or macro-bearing file must fail with a specific message, and macros must never be executed or offered.

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

- [ ] An uploaded spreadsheet opens in the app with its sheets selectable and headers frozen.
- [ ] Very long and very wide sheets scroll smoothly with rows and columns virtualized.
- [ ] Cached values are shown where formulas cannot be evaluated, and unresolvable cells are marked as such.
- [ ] Delimiter and encoding are detected for text files, displayed, and overridable.
- [ ] Merged cells and frozen headers stay aligned while scrolling and resizing.
- [ ] Files past the row ceiling are declined with a download offered instead.
- [ ] Dates, currencies, and percentages render using the recorded cell format.
- [ ] 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.
