# Saved Blocks Library

## Objective

Save a section once and reuse it anywhere, with copies that stay linked if you want.

A workspace library of reusable saved sections, each insertable as a linked instance or a detached copy.

## 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. Let an author save any section they have built into the library with a name, and insert it into another page from a picker. Reuse the app's existing Template Picker rather than building a second chooser.
2. Ask at insert time whether the copy stays linked to the original or detaches immediately, and mark which it is on the canvas. An author who cannot tell will eventually edit a linked block and change six live pages by accident.
3. When a linked original changes, merge the update into its instances field by field and leave locally overridden fields alone. Define which fields an instance may override at all.
4. Scope the library to the workspace that owns it, and check that scope on insert and on render, not only when listing the picker.
5. Do not build a general component system with parameters and slots. A named saved section with a small set of overridable fields covers the real use; anything beyond that is a programming language nobody asked for.

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

- Linked and detached copies behave completely differently and look identical on the page. Mark every instance and state plainly what editing it will affect.
- Publishing a change to a linked original must not wipe the text an instance overrode locally. Merge per field rather than replacing the instance wholesale.
- Deleting a saved block still used on live pages breaks all of them. Show the usage list, refuse the deletion, and offer to detach every instance instead.
- A saved block containing an instance of itself, directly or through another block, renders forever. Refuse the save that would create the cycle rather than catching it at render time.
- One workspace must never see another's library. A picker filtered correctly but a render path that is not will leak content across tenants.
- Renaming or reorganising the library must not change what a published page renders. Identity lives in a stable identifier, never in the name.
- Editing a linked block changes pages the author may not have open. Report how many places will change and treat the action as a publish rather than an ordinary save.
- A saved block referencing a deleted image or a removed section type must degrade to something renderable rather than blanking the page it sits on.

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

- [ ] Any built section can be saved to the library and inserted elsewhere.
- [ ] Each insert is explicitly linked or detached, and its state is visible on the canvas.
- [ ] Updating a linked original updates its instances without discarding local overrides.
- [ ] Deleting a saved block that is in use is blocked, with a usage list and a detach-all option.
- [ ] A saved block cannot be made to contain itself.
- [ ] The library is scoped to its workspace on listing, insert, and render.
- [ ] Editing a linked block reports how many published places will change before it is applied.
- [ ] 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.
