# Architecture and implementation plan

## Contracts decided before implementation

- **Engine:** `Calendar extends EventTarget`, no DOM imports or automatic timers.
  `register(entry)`, `remove(id)`, `open(id)`, `close(id)`, `unlock(id)`, `refresh()`,
  `get(id)`, `entries`, `progress`, `dispose()`. Options include `id`, `version`,
  `start`, `end`, `timeZone`, `preview`, `clock`, `policy`, `persistence`, `entries`.
- **Entries:** stable string `id`; optional `day` (1-based from `start`), `unlockAt`,
  `alwaysAvailable`, `manual`, `disabled`, `once`, labels/content metadata and
  layout hints. Missing schedule means always available. Explicit dates take
  precedence over day offsets. End bounds day-based registration; it does not
  expire already available content. No fixed month or entry count.
- **Time:** date-only strings are civil days in `timeZone` (default UTC). Exact
  timestamps must include Z or an offset. Add calendar days in UTC civil-date
  space, then compare in the configured timezone; never add 24 hours to a local
  timestamp. Custom clocks return epoch milliseconds or a Date. The engine does
  not depend on Temporal or a timezone library.
- **Policies:** synchronous `(entry, context) => boolean`, context contains `now`,
  `timeZone`, `manuallyUnlocked`, `preview`. Disabled always wins; preview bypasses
  scheduling only. Manual unlocks override dates. Exceptions fail closed and
  dispatch an error. Consumers call `refresh` when external policy inputs change.
- **Persistence:** synchronous `{ load(key), save(key, state), remove(key) }` with
  JSON-compatible state. Key includes calendar ID and version. Adapter errors and
  malformed saved state never prevent basic interaction. Persist open/ever-opened
  and manual-unlock IDs; preview state uses an isolated namespace.
- **Components:** `<advent-calendar>` owns an engine, observes only its direct
  `<advent-door>` children, forwards engine events as bubbling/composed events,
  and refreshes schedules while connected. `<advent-door>` also works alone.
  Public `.configure(options)`, `.register(entry, contentNode?)`, `.load(source)`
  on calendars; `.open()`, `.close()`, `.toggle()` on doors. Static trusted HTML
  belongs in named slots; manifest text is rendered with DOM text APIs only.
- **Layouts:** pure `orderEntries(entries, {layout, seed})` plus CSS layout rules.
  Shuffle changes DOM order so keyboard/reading order matches visual order.
  Masonry means portable CSS columns (column-major reading), not experimental
  native masonry. Custom positioning is CSS grid hints or consumer CSS/JS.
- **Reveals:** CSS state transitions selected by `reveal`, independent of themes.
  Slots: `front`, `content`; calendar adds `heading`, `description`, `empty`.
  Parts and custom properties are the stable styling surface.
- **Events:** `advent:ready/unlock/open/close/progress/complete/error`, detail
  includes `calendarId`, `entryId` where relevant, entry/state/progress or error.
  Completion means every current entry has been opened at least once, not that
  every door is currently open. No events are cancelable; policy controls access.

## Accessibility and compatibility

Native buttons supply Enter/Space and Tab behavior; no synthetic grid role or
unnecessary arrow-key interception. Locked buttons stay focusable with
`aria-disabled` and a visible availability explanation. Closed content is inert
and hidden from accessibility/navigation; a close that contains focus restores
focus to its trigger. Front slots should contain noninteractive labeling content.
Focus outlines, minimum touch targets and reduced motion are built in.

Target modern evergreen Chromium, Firefox and WebKit with ES modules, Custom
Elements, Shadow DOM, Intl timezone data and inert. No legacy browser polyfills.
Test all three Playwright engines where available; explicitly report omissions.
Without JS, declarative content stays readable. Remote content cannot be fetched
without JS. Client locks are presentation only: secret content must stay off the
client until authorized by a server.

## Phased implementation

1. **Foundation:** engine, date validation, event/state model, door/calendar,
   neutral grid and hinge; independently test engine boundaries.
2. **Composability:** adapters, manifests, layout/reveal variants, dynamic content,
   styling contracts and TypeScript API; test combinations and cleanup.
3. **Demonstration:** seven examples and a live four-axis playground, with Jake
   content isolated from the generic library. Local static server only.
4. **Hardening:** unit/browser/accessibility tests, packaging, Nix lock, CI,
   documentation and recorded verification evidence. No publish step.

## Decisions and deviations

Use a few cohesive modules rather than one file for every CSS animation. Source
ES modules are the distribution; no consumer build or mandatory bundle. Ship
handwritten declarations checked with a TypeScript consumer test. No content
sanitizer is needed because manifests cannot contain executable HTML. Persistence
is synchronous for the first release; network persistence belongs in an explicit
consumer integration rather than ambiguous async engine methods.
