/* CalZone web app. One stylesheet, mobile first, no inline styles (the CSP
   forbids them). Four layers, lowest first: a reset, the design tokens,
   element defaults, then the components. Classes name what a thing is
   (panel, rows, swatch), never how it looks. The reset, tokens and base
   layers are the same as WordWarZ's and Converge's (decision 13); the
   components start from Converge's.

   Units: rem for type, space and tap sizes, so browser zoom and the phone's
   text size scale everything together; em where a size should follow its
   own text (badges, icons); px only for hairlines. Fluid steps keep a rem
   term, so they still grow with zoom. Logical properties throughout. */

@layer reset, tokens, base, components;

/* ---- Reset ------------------------------------------------------------- */

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  * { margin: 0; }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  img, svg { display: block; max-inline-size: 100%; }
  input, button, select, textarea { font: inherit; color: inherit; }
  p, h1, h2, h3, li { overflow-wrap: break-word; }
  [hidden] { display: none !important; }
}

/* ---- Tokens ------------------------------------------------------------ */

@layer tokens {
  /* Dark unless the person chose otherwise (decision 36); light-dark()
     below follows color-scheme. */
  :root { color-scheme: dark; }
  :root[data-theme="light"] { color-scheme: light; }
  :root[data-theme="system"] { color-scheme: light dark; }
  :root[data-theme="black"] { color-scheme: dark; }
  :root {

    /* Color roles. light-dark() keeps both themes in one place. */
    --canvas: light-dark(#f6f4ef, #131317);
    --surface: light-dark(#ffffff, #1c1c22);
    --surface-sunken: light-dark(#efece5, #24242c);
    --text: light-dark(#1f1d1a, #ecebe7);
    --text-quiet: light-dark(#625d55, #a3a09a);
    --line: light-dark(#e2ded5, #33333c);
    --accent: light-dark(#0f766e, #5fd0c4);
    --on-accent: light-dark(#ffffff, #0e1513);
    --good: light-dark(#176a3f, #6fd8a3);
    --caution: light-dark(#8a5a00, #f2c063);
    --danger: light-dark(#b3261e, #ff8a80);
    /* Tints for backgrounds, derived rather than picked. */
    --accent-tint: color-mix(in oklab, var(--accent) 14%, var(--surface));
    --good-tint: color-mix(in oklab, var(--good) 14%, var(--surface));
    --caution-tint: color-mix(in oklab, var(--caution) 16%, var(--surface));
    --danger-tint: color-mix(in oklab, var(--danger) 12%, var(--surface));
    /* Today in the calendar: light enough that the hour lines still show. */
    --today-tint: color-mix(in oklab, var(--accent) 7%, var(--surface));

    /* Type: system fonts, a rem scale; headings breathe a little on big screens. */
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
    --font-mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, monospace;
    --step--1: 0.875rem;
    --step-0: 1rem;
    --step-1: 1.125rem;
    --step-2: clamp(1.25rem, 1.1rem + 0.5vi, 1.5rem);
    --step-3: clamp(1.6rem, 1.3rem + 1.2vi, 2.1rem);
    --leading: 1.5;

    /* Space: one scale, rem based. */
    --space-3xs: 0.25rem;
    --space-2xs: 0.5rem;
    --space-xs: 0.75rem;
    --space-s: 1rem;
    --space-m: 1.5rem;
    --space-l: 2rem;
    --gutter: clamp(1rem, 0.6rem + 1.6vi, 2rem);

    /* Shape and sizes. */
    --panel-pad: clamp(1rem, 0.7rem + 1vi, 1.5rem);
    --radius-s: 0.4rem;
    --radius-m: 0.75rem;
    --hairline: 1px solid var(--line);
    --tap: 2.75rem;
    --focus: 2px solid var(--accent);
    /* Admin pages are forms and lists: a centered, comfortable width. */
    --page: 72rem;
    --measure: 65ch;
  }
  /* Black (decision 55): Dark's colors on a true black canvas for OLED
     screens, surfaces lifted just enough to tell apart, as in the app. */
  :root[data-theme="black"] {
    --canvas: #000000;
    --surface: #0e0e11;
    --surface-sunken: #18181d;
    --line: #2a2a31;
  }
}

/* ---- Element defaults --------------------------------------------------- */

@layer base {
  body {
    min-block-size: 100dvb;
    background: var(--canvas);
    color: var(--text);
    font: var(--step-0) / var(--leading) var(--font);
  }
  h1 { font-size: var(--step-3); line-height: 1.15; letter-spacing: -0.01em; }
  h2 { font-size: var(--step-2); line-height: 1.25; }
  h3 { font-size: var(--step-1); line-height: 1.3; }
  p { max-inline-size: var(--measure); }
  a { color: var(--accent); text-underline-offset: 0.15em; }
  a:not(:hover) { text-decoration: none; }
  /* A link inside a sentence must not rely on color alone (WCAG 1.4.1). */
  :where(p, .hint, .notice, td) a:not(.button) { text-decoration: underline; }
  :focus-visible { outline: var(--focus); outline-offset: 2px; }
  /* Off screen until a keyboard reaches it. */
  .skip-link { position: absolute; inset-block-start: var(--space-2xs); inset-inline-start: var(--space-2xs); z-index: 100; padding: var(--space-2xs) var(--space-xs); border-radius: var(--radius-m); background: var(--surface); color: var(--text); transform: translateY(-200%); }
  .skip-link:focus-visible { transform: none; }
  main:focus { outline: none; }
  code { font: 0.9em var(--font-mono); overflow-wrap: anywhere; }
  pre { font: var(--step--1) / 1.5 var(--font-mono); white-space: pre-wrap; overflow-wrap: anywhere; }
  time { white-space: nowrap; }
  ::selection { background: var(--accent-tint); }

  /* Controls: one height, one border, tap-sized. */
  input:where(:not([type=checkbox], [type=radio])), select, textarea {
    inline-size: 100%;
    min-block-size: var(--tap);
    padding: var(--space-2xs) var(--space-xs);
    background: var(--canvas);
    border: var(--hairline);
    border-radius: var(--radius-s);
    /* Labels wrap their controls and are bold; what you type is not. */
    font-weight: 400;
  }
  input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: -1px; border-color: transparent; }
  input:disabled { color: var(--text-quiet); }
  /* A color is picked, not read: a swatch-sized control. */
  input[type=color] { inline-size: 5rem; padding: var(--space-3xs); cursor: pointer; }
  input[type=checkbox], input[type=radio] { inline-size: 1.15em; block-size: 1.15em; accent-color: var(--accent); }

  button, .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2xs);
    min-block-size: var(--tap);
    padding: var(--space-2xs) var(--space-s);
    border: var(--hairline);
    border-radius: var(--radius-s);
    background: var(--surface-sunken);
    color: var(--text);
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
  }
  button:hover, .button:hover { background: color-mix(in oklab, var(--surface-sunken), var(--text) 6%); }
  button:disabled { opacity: 0.55; cursor: not-allowed; }

  table { border-collapse: collapse; inline-size: 100%; }
  th, td { padding: var(--space-2xs) var(--space-xs); text-align: start; border-block-end: var(--hairline); vertical-align: middle; }
  th { color: var(--text-quiet); font-size: var(--step--1); font-weight: 600; }
  tbody th { color: var(--text); font-size: inherit; }
  tbody tr:last-child > * { border-block-end: 0; }

  ul.plain, ol.plain { list-style: none; padding: 0; }

  fieldset { display: grid; gap: var(--space-2xs); min-inline-size: 0; padding: var(--space-xs) var(--space-s) var(--space-s); border: var(--hairline); border-radius: var(--radius-m); }
  legend { padding-inline: var(--space-3xs); font-weight: 600; }
  textarea { resize: vertical; line-height: 1.45; }
  summary { cursor: pointer; inline-size: fit-content; }
  progress { inline-size: 100%; block-size: 0.6rem; appearance: none; border: 0; border-radius: 999px; background: var(--surface-sunken); overflow: hidden; }
  progress::-webkit-progress-bar { background: var(--surface-sunken); }
  progress::-webkit-progress-value { background: var(--accent); border-radius: 999px; }
  progress::-moz-progress-bar { background: var(--accent); border-radius: 999px; }

  @media (prefers-reduced-motion: no-preference) {
    button, .button, a { transition: background-color 0.15s, color 0.15s; }
  }
}

/* ---- Components --------------------------------------------------------- */

@layer components {
  /* Buttons: variants by meaning. */
  .primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
  .primary:hover { background: color-mix(in oklab, var(--accent), var(--text) 12%); }
  .danger { color: var(--danger); }
  .danger:hover { background: var(--danger-tint); }
  .quiet { background: transparent; border-color: transparent; color: var(--text-quiet); font-weight: 500; }
  .quiet:hover { background: var(--surface-sunken); color: var(--text); }
  /* Compact still meets the tap height; it only loses horizontal room and size. */
  .compact { padding-inline: var(--space-xs); font-size: var(--step--1); }

  /* Site header: the brand, Calendar and Contacts, and the Settings gear,
     on one row (decision 53); it wraps only at big text sizes. */
  .site-header {
    container: header / inline-size;
    position: sticky;
    inset-block-start: 0;
    z-index: 10;
    background: color-mix(in oklab, var(--surface) 92%, transparent);
    backdrop-filter: blur(8px);
    border-block-end: var(--hairline);
  }
  .site-header-inner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3xs) var(--space-s);
    max-inline-size: var(--page);
    margin-inline: auto;
    padding: var(--space-2xs) var(--gutter);
  }
  .brand { margin-inline-end: auto; color: var(--text); font-weight: 800; font-size: var(--step-1); letter-spacing: -0.01em; }
  .sections {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    gap: var(--space-3xs);
    padding-block: var(--space-3xs);
  }
  .sections a, .settings-link, .subnav a {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    min-block-size: var(--tap);
    padding-inline: var(--space-2xs);
    border-radius: var(--radius-s);
    color: var(--text-quiet);
    white-space: nowrap;
  }
  .sections a:hover, .settings-link:hover, .subnav a:hover { background: var(--surface-sunken); color: var(--text); }
  .sections a[aria-current=page], .settings-link[aria-current=page], .subnav a[aria-current=page] {
    background: var(--accent-tint); color: var(--text); font-weight: 600;
  }
  /* The gear: its word only where there is room (decision 53). */
  .settings-link svg { inline-size: 1.25em; block-size: 1.25em; flex: none; }
  .settings-label { display: none; }
  @container header (inline-size > 32rem) {
    .settings-label { display: inline; }
  }
  /* A 360px phone in Dutch: tighter gaps keep the gear on the row. */
  @container header (inline-size <= 24rem) {
    .site-header-inner { column-gap: var(--space-2xs); }
  }
  /* Settings' sections, under the header on every settings page. */
  .subnav { border-block-end: var(--hairline); background: var(--surface); }
  .subnav-inner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3xs);
    max-inline-size: var(--page);
    margin-inline: auto;
    padding: var(--space-3xs) var(--gutter);
  }
  .settings-logout { margin-block-start: var(--space-l); display: flex; justify-content: center; }

  /* Pages. */
  .page {
    display: grid;
    /* minmax(0, 1fr), not auto: a long select or code never widens the page. */
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-m);
    align-content: start;
    max-inline-size: var(--page);
    margin-inline: auto;
    padding: var(--space-m) var(--gutter) var(--space-l);
  }
  /* Title, a line about the page, and the page's own actions at the end;
     the actions drop below the title when there is no room beside it. */
  .page-header { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2xs) var(--space-m); }
  .page-header > :first-child { flex: 1 1 20rem; min-inline-size: 0; display: grid; gap: var(--space-3xs); }
  .page-header h1 code { font-size: 0.8em; }
  .page-actions { display: flex; flex-wrap: wrap; gap: var(--space-2xs); }
  #flash:empty { display: none; }

  /* Log in, join: one centered panel. */
  .page.auth { --page: 26rem; padding-block-start: clamp(var(--space-l), 10dvb, 6rem); }
  .wordmark { font-size: var(--step-3); font-weight: 800; letter-spacing: -0.02em; }

  /* Panels: the one surface everything sits on. */
  .panel {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-s);
    align-content: start;
    padding: var(--panel-pad);
    background: var(--surface);
    border: var(--hairline);
    border-radius: var(--radius-m);
  }
  .panel-header { display: grid; gap: var(--space-3xs); }
  /* A panel header with actions: same wrap as the page header. */
  .panel-header:has(> .row-actions) { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2xs) var(--space-s); }
  .panel-header:has(> .row-actions) > :first-child { flex: 1 1 18rem; min-inline-size: 0; display: grid; gap: var(--space-3xs); }
  .panels { display: grid; gap: var(--space-m); grid-template-columns: repeat(auto-fit, minmax(min(100%, 26rem), 1fr)); align-items: start; }

  /* A main column with a side column: side by side when both fit, the side
     below the main one otherwise. */
  .with-aside { display: flex; flex-wrap: wrap; gap: var(--space-m); align-items: flex-start; }
  .with-aside > .main-column { flex: 999 1 34rem; min-inline-size: 0; display: grid; gap: var(--space-m); }
  .with-aside > .side-column { flex: 1 1 20rem; min-inline-size: 0; display: grid; gap: var(--space-m); }

  /* Every one-column grid gets a minmax(0, 1fr) column, never the auto
     one: an auto column grows to its widest control and the page with it. */
  :is(.page, .panel, .form, .field, .rows, .row-main, .row-more, .main-column, .side-column, .callout) {
    grid-template-columns: minmax(0, 1fr);
  }

  .meta, .hint, .empty { color: var(--text-quiet); }
  .meta > .hint { font-size: inherit; }
  .hint { font-size: var(--step--1); font-weight: 400; }
  .caution { color: var(--caution); }

  /* Forms. */
  .form { display: grid; gap: var(--space-s); }
  .field { display: grid; gap: var(--space-3xs); font-weight: 600; }
  .field .hint { display: block; }
  /* Short fields share a line while they keep a readable width. */
  .field-grid { display: grid; gap: var(--space-s); grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); align-items: start; }
  .field-grid.small { grid-template-columns: repeat(auto-fill, minmax(min(100%, 10rem), 1fr)); gap: var(--space-2xs); }
  .checkbox { display: flex; align-items: center; gap: var(--space-2xs); min-block-size: var(--tap); font-weight: 400; }
  .checkbox > input { flex: none; }
  /* Choices that wrap into columns (grants, key scopes). */
  .checklist { display: grid; gap: 0 var(--space-m); grid-template-columns: repeat(auto-fill, minmax(min(100%, 18rem), 1fr)); }
  /* A row of controls that wraps: the text input takes what is left. */
  .field-row { display: flex; flex-wrap: wrap; gap: var(--space-2xs) var(--space-xs); align-items: center; }
  .field-row > input:where(:not([type=checkbox], [type=radio])) { flex: 1 1 14rem; inline-size: auto; }
  .field-row > select { flex: 1 1 auto; inline-size: auto; max-inline-size: 22rem; }
  .field-row > :is(button, .button, .checkbox) { flex: 0 1 auto; }
  .field-row > * { min-inline-size: 0; }
  .field-row > .push { margin-inline-start: auto; }
  .form > :is(button, .button), .panel > .button { justify-self: start; }
  /* Save, then a way out. */
  .form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-xs) var(--space-s); }

  /* Disclosure: details that open more of the same panel. */
  .disclosure > summary { color: var(--text-quiet); font-size: var(--step--1); padding-block: var(--space-3xs); }
  .disclosure > summary:hover { color: var(--text); }
  .disclosure[open] > summary { margin-block-end: var(--space-xs); }

  /* Messages. */
  .notice { padding: var(--space-xs) var(--space-s); border-radius: var(--radius-s); border-inline-start: 0.25rem solid; max-inline-size: none; }
  .notice.error { background: var(--danger-tint); color: var(--danger); }
  .notice.ok { background: var(--good-tint); color: var(--good); }
  .notice.warn { background: var(--caution-tint); color: var(--caution); }
  .callout { display: grid; gap: var(--space-xs); justify-items: start; padding: var(--space-s); background: var(--accent-tint); border-radius: var(--radius-s); }
  .callout > * { max-inline-size: 100%; }

  .badge {
    display: inline-block;
    padding: 0.1em 0.55em;
    border-radius: 999px;
    background: var(--surface-sunken);
    color: var(--text-quiet);
    font-size: 0.78em;
    font-weight: 600;
    line-height: 1.5;
    vertical-align: 0.1em;
    white-space: nowrap;
  }
  .badge.ok { background: var(--good-tint); color: var(--good); }
  .badge.warn { background: var(--caution-tint); color: var(--caution); }
  .badge.error { background: var(--danger-tint); color: var(--danger); }

  .copy-line { display: flex; flex-wrap: wrap; gap: var(--space-2xs); align-items: center; inline-size: 100%; }
  .copy-line code {
    flex: 1 1 14rem;
    min-inline-size: 0;
    padding: var(--space-2xs) var(--space-xs);
    background: var(--surface);
    border: var(--hairline);
    border-radius: var(--radius-s);
  }
  /* Lists of things (calendars, devices, people): rows that wrap. The
     main block takes the room; actions sit at the end, or drop below it
     when the row gets narrow. No media query needed. */
  .rows { list-style: none; padding: 0; display: grid; }
  .rows > li { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2xs) var(--space-s); padding-block: var(--space-xs); border-block-end: var(--hairline); }
  .rows > li:last-child { border-block-end: 0; }
  .rows > li:first-child { padding-block-start: 0; }
  .row-main { flex: 1 1 16rem; min-inline-size: 0; display: grid; gap: 0.1rem; }
  .row-title { font-weight: 600; }
  .row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2xs); }
  .row-actions form { display: contents; }
  .rows > li.off > .row-main { opacity: 0.6; }

  /* A calendar's color: an SVG square (a fill attribute, not a style, so
     the CSP allows it). */
  .swatch { flex: none; inline-size: 1rem; block-size: 1rem; border-radius: 0.25rem; overflow: hidden; }
  .swatch.big { display: inline-block; inline-size: 0.8em; block-size: 0.8em; margin-inline-end: var(--space-2xs); vertical-align: -0.05em; }

  /* Label and value pairs (a new password, a temporary passphrase). */
  .facts { display: grid; gap: var(--space-2xs) var(--space-s); grid-template-columns: minmax(0, 1fr); }
  .facts dt { color: var(--text-quiet); font-size: var(--step--1); font-weight: 600; }
  .facts dd { margin: 0 0 var(--space-xs); }
  @media (min-width: 40rem) {
    .facts { grid-template-columns: max-content minmax(0, 1fr); align-items: center; }
    .facts dd { margin: 0; }
  }

  /* Two-factor: the QR code stays dark on white in both themes. */
  .qr { inline-size: min(100%, 15rem); padding: var(--space-2xs); background: #fff; border-radius: var(--radius-s); }
  .qr svg { display: block; inline-size: 100%; block-size: auto; }
  .codes { display: grid; grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr)); gap: var(--space-3xs); }

  /* Per-collection choices for a device password. */
  .choices { display: grid; gap: var(--space-3xs); padding-inline-start: var(--space-m); }
  .choices .choice-name { flex: 1 1 12rem; font-weight: 400; }
  .choices select { flex: 0 1 12rem; }

  /* A group on the people page. */
  .group { display: grid; gap: var(--space-2xs); padding-block: var(--space-xs); border-block-end: var(--hairline); }
  .group h3 code { font-size: var(--step--1); font-weight: 400; }

  /* HTMX's own inline indicator styles are off; requests dim their button. */
  .htmx-request button[type=submit] { opacity: 0.6; }
  .visually-hidden { position: absolute; inline-size: 1px; block-size: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

  /* ---- Contacts (phase 2 step 4) ---- */
  .avatar { flex: none; inline-size: 2.5rem; block-size: 2.5rem; border-radius: 50%; object-fit: cover; }
  .avatar.big { inline-size: 4.5rem; block-size: 4.5rem; }
  .contact-search input { inline-size: 100%; }
  .contact-list { display: grid; gap: var(--space-s); }
  .contact-book { gap: var(--space-2xs); }
  .contact-letter { font-size: var(--step--1); color: var(--text-quiet); padding-block-start: var(--space-2xs); }
  .contact-rows > li { padding: 0; }
  .contact-row { display: flex; align-items: center; gap: var(--space-xs); inline-size: 100%; padding-block: var(--space-2xs); color: inherit; text-decoration: none; }
  .contact-row:hover .row-title { text-decoration: underline; }
  .contact-head { align-items: center; }
  /* Beats the page header's own rule for its first child. */
  .page-header > .contact-id { display: flex; align-items: center; gap: var(--space-s); min-inline-size: 0; }
  .contact-email { overflow-wrap: anywhere; }
  .contact-note { white-space: pre-wrap; }
  /* Kind and number on one line, even on a phone. */
  .contact-typed { flex-wrap: nowrap; }
  .contact-typed > select { flex: 0 0 7.5rem; inline-size: 7.5rem; }
  .contact-typed > input { flex: 1 1 auto; min-inline-size: 0; inline-size: auto; }
  .contact-address { display: grid; gap: var(--space-2xs); border: 0; border-block-end: var(--hairline); padding: 0 0 var(--space-xs); margin: 0; min-inline-size: 0; }
  .contact-address > select { justify-self: start; inline-size: auto; }
  /* Clean-up (decision 56): filters, select mode, duplicates, merge. */
  .contact-search { display: grid; gap: var(--space-2xs); }
  .contact-filter > summary { list-style: none; }
  .contact-filter > summary::-webkit-details-marker { display: none; }
  .contact-filter[open] > summary { margin-block-end: var(--space-xs); }
  .contact-filter { display: grid; gap: var(--space-xs); }
  .filter-grid { display: grid; gap: var(--space-2xs) var(--space-s); grid-template-columns: repeat(auto-fill, minmax(min(100%, 12rem), 1fr)); }
  .filter-grid select { inline-size: 100%; }
  .contact-count { margin: 0; }
  .contact-row.picking { cursor: pointer; }
  .contact-row.picking > input { flex: none; inline-size: 1.25rem; block-size: 1.25rem; accent-color: var(--accent); }
  .contact-row.picking:has(> input:checked) .row-title { font-weight: 700; }
  .pick-bar { position: sticky; inset-block-start: 0; z-index: 2; gap: var(--space-2xs); }
  .pick-bar > .hint { margin: 0; }
  .dup-reasons { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3xs) var(--space-2xs); margin: 0; }
  .dup-group > .field-row form { display: contents; }
  .pick-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-2xs) var(--space-s); }
  .pick-group { display: flex; align-items: center; gap: var(--space-2xs); }
  .merge-cards > li { display: flex; flex-wrap: nowrap; align-items: center; gap: var(--space-xs); }
  .merge-cards > li > .contact-row { flex: 1 1 auto; min-inline-size: 0; }
  .merge-cards > li > .row-actions { flex: none; }
  .merge-cards .row-main { flex-basis: auto; }
  .merge-cards > li.kept .row-title { font-weight: 700; }
  .merge-choice { border: 0; padding: 0; margin: 0; min-inline-size: 0; display: grid; gap: 0; }
  .merge-choice + .merge-choice { margin-block-start: var(--space-s); }
  .merge-choice > legend { font-weight: 600; padding: 0; margin-block-end: var(--space-3xs); }
  .merge-choice .checkbox { align-items: center; min-inline-size: 0; }
  .merge-choice .row-main { min-inline-size: 0; }
  .delete-list { max-block-size: 50vh; overflow-y: auto; }
  /* Birthdays (decision 57): its settings and the clean-up list. */
  .choice-list { border: 0; padding: 0; margin: 0; min-inline-size: 0; display: grid; gap: 0; }
  .choice-list + .choice-list { margin-block-start: var(--space-s); }
  .choice-list > legend { font-weight: 600; padding: 0; margin-block-end: var(--space-3xs); }
  .choice-list .row-main, .birthday-list .row-main { min-inline-size: 0; }
  .birthday-list > li { display: grid; gap: var(--space-3xs); }
  .birthday-list > li > .hint, .birthday-copy { margin: 0; padding-inline-start: calc(1.25rem + var(--space-2xs)); }
  .birthday-copy { display: grid; gap: var(--space-3xs); }
  .birthday-copy .hint { margin: 0; }
  .birthday-copy .field-row > input[type=date] { flex: 0 1 12rem; inline-size: auto; min-inline-size: 0; }
  /* A notice with an Undo button beside it (decision 12). */
  .notice-undo { display: flex; align-items: center; justify-content: space-between; gap: var(--space-xs); flex-wrap: wrap; }

  /* ---- The calendar page (phase 2 step 3) ---- */
  /* Full width: the grid is the page. */
  .page.calendar-page { max-inline-size: none; padding-block-end: var(--space-2xs); }
  /* minmax(0, 1fr): the calendar never grows wider than the screen. */
  .cal { display: grid; gap: var(--space-2xs); grid-template-columns: minmax(0, 1fr); }
  .cal-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2xs) var(--space-xs); }
  .cal-nav { display: flex; align-items: center; gap: var(--space-3xs); flex: 1 1 auto; min-inline-size: 0; }
  .cal-title { flex: 1 1 auto; min-inline-size: 0; font-size: var(--step-1); margin-inline-start: var(--space-2xs); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  /* The view menu (decision 55): the current view, opening a short list. */
  .cal-views { position: relative; }
  .cal-views > summary { list-style: none; inline-size: auto; gap: var(--space-2xs); }
  .cal-views > summary::-webkit-details-marker { display: none; }
  .cal-views-caret { font-size: 0.8em; color: var(--text-quiet); }
  .cal-views[open] > summary { background: var(--accent-tint); }
  .cal-views-list {
    position: absolute; inset-block-start: calc(100% + var(--space-3xs)); inset-inline-end: 0; z-index: 20;
    display: grid; min-inline-size: 13rem; padding: var(--space-3xs);
    border: var(--hairline); border-radius: var(--radius-m); background: var(--surface);
    box-shadow: 0 0.5rem 1.5rem color-mix(in oklab, black 35%, transparent);
  }
  .cal-views-list > button { justify-content: space-between; color: var(--text); font-weight: 400; }
  .cal-views-list > button[aria-pressed=true] { background: var(--accent-tint); font-weight: 600; }
  .cal-views-list kbd { font-family: var(--font-mono); font-size: var(--step--1); color: var(--text-quiet); }
  /* Quick add at the top of a new event's panel: it fills the form. */
  .ev-quick { display: flex; gap: var(--space-2xs); }
  .ev-quick > input { flex: 1 1 auto; min-inline-size: 0; }
  .ev-quick > button { white-space: nowrap; }
  .ev-quick-hint { margin-block-start: calc(-1 * var(--space-2xs)); }
  .cal-grid { block-size: max(26rem, calc(100dvb - 13rem)); min-inline-size: 0; }

  /* The calendar list: beside the grid on a desk, above it on a phone, and
     folded away either way with the Calendars button. */
  .cal-body { display: grid; gap: var(--space-xs); grid-template-columns: minmax(0, 1fr); }
  .cal-body.list-closed .cal-list { display: none; }
  @media (min-width: 64rem) {
    .cal-body { grid-template-columns: 15rem minmax(0, 1fr); align-items: start; }
    .cal-body.list-closed { grid-template-columns: minmax(0, 1fr); }
  }
  .cal-list { display: grid; gap: var(--space-2xs); align-content: start; justify-items: start; min-inline-size: 0; padding: var(--space-2xs); border: var(--hairline); border-radius: var(--radius-m); background: var(--surface); }
  .cal-list > .cal-cals { justify-self: stretch; }
  .cal-cals { list-style: none; padding: 0; display: grid; gap: 0; }
  .cal-cals > li { display: flex; align-items: center; gap: var(--space-3xs); }
  .cal-check { flex: 1 1 auto; display: flex; align-items: center; gap: var(--space-2xs); min-block-size: var(--tap); min-inline-size: 0; font-weight: 400; cursor: pointer; }
  .cal-check > input { flex: none; }
  /* min-inline-size: 0 lets a long name shrink (with an ellipsis) instead
     of pushing the settings button out of the list. */
  .cal-cal-name { flex: 1 1 auto; min-inline-size: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .cal-check > .cal-ev-who { flex: none; align-self: center; }
  .cal-sub-mark { flex: none; color: var(--text-quiet); }
  .cal-link-made { display: grid; gap: var(--space-2xs); }
  .cal-link-made input { font-family: var(--font-mono); font-size: var(--step--1); }
  .cal-cal-more { flex: none; }
  .cal-list-toggle[aria-expanded=true] { background: var(--accent-tint); color: var(--text); }

  /* Month and days (decision 54): the small month over the month list on a
     phone, beside it from a tablet up. */
  .cal-main { display: grid; gap: var(--space-xs); grid-template-columns: minmax(0, 1fr); align-items: start; min-inline-size: 0; }
  /* Sideways swipes page (calendar.js); without pan-y the browser claims
     them as a pan and cancels the pointer before it lifts. Every element,
     because the grid's own scroll boxes start touch-action afresh. */
  .cal-main, .cal-main * { touch-action: pan-y pinch-zoom; }
  @media (min-width: 48rem) {
    .cal-main.month-days { grid-template-columns: minmax(16rem, 22rem) minmax(0, 1fr); }
  }
  .cal-mini { display: grid; gap: var(--space-2xs); padding: var(--space-2xs); border: var(--hairline); border-radius: var(--radius-m); background: var(--surface); }
  .cal-mini[hidden] { display: none; }
  .cal-mini-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 2px; }
  .cal-mini-wd { text-align: center; font-size: var(--step--1); color: var(--text-quiet); padding-block-end: var(--space-3xs); }
  .cal-mini-day {
    flex-direction: column; gap: 3px; min-block-size: 2.75rem; padding: var(--space-3xs) 0;
    border: 0; background: transparent; font-weight: 400; font-variant-numeric: tabular-nums;
  }
  .cal-mini-day:hover { background: var(--surface-sunken); }
  .cal-mini-day.out { color: var(--text-quiet); }
  .cal-mini-day.out .cal-mini-dots { opacity: 0.6; }
  .cal-mini-day.today .cal-mini-num { color: var(--accent); font-weight: 700; }
  .cal-mini-day[aria-pressed=true] { background: var(--accent-tint); }
  .cal-mini-dots { display: flex; gap: 2px; block-size: 6px; }
  .cal-mini-dot { flex: none; display: inline-block; inline-size: 6px; block-size: 6px; border-radius: 50%; }
  .cal-mini-legend { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: var(--space-3xs) var(--space-xs); font-size: var(--step--1); color: var(--text-quiet); }
  .cal-mini-legend:empty { display: none; }
  .cal-mini-legend > li { display: flex; align-items: center; gap: var(--space-3xs); }
  @media (prefers-reduced-motion: no-preference) {
    .cal-mini-grid.turn { animation: cal-mini-in 0.12s ease-out; }
  }
  @keyframes cal-mini-in { from { opacity: 0.4; } }
  .sheet-section { display: grid; gap: var(--space-2xs); padding-block-start: var(--space-xs); border-block-start: var(--hairline); }
  .cal-keys { display: none; }
  @media (min-width: 64rem) { .cal-keys { display: block; } }
  @media (max-width: 40rem) {
    /* The month on its own line above the buttons. */
    .cal-nav { flex-wrap: wrap; }
    .cal-title { order: -1; flex-basis: 100%; margin-inline-start: 0; }
    .cal-new-text { display: none; }
    /* The menu sits at the start of its row here; open it from there. */
    .cal-views-list { inset-inline: 0 auto; }
    .cal-grid { block-size: max(24rem, calc(100dvb - 15rem)); }
    .cal-main.month-days .cal-grid { block-size: max(16rem, calc(100dvb - 33rem)); }
  }

  /* What an event shows: time, title, and initials when someone else owns it. */
  .cal-ev-time { font-size: 0.85em; margin-inline-end: 0.3em; }
  .cal-ev-title + .cal-ev-time { margin-inline: 0.3em 0; }
  .cal-ev-title { font-weight: 600; }
  .cal-ev-who {
    display: inline-grid; place-items: center; vertical-align: 0.1em; margin-inline-start: 0.35em; padding-inline: 0.3em;
    min-inline-size: 1.6em; block-size: 1.6em; line-height: 1; border-radius: 999px; font-size: 0.75em; font-weight: 700;
    /* A ring, not a tint: a tinted fill lowers the initials' contrast. */
    box-shadow: inset 0 0 0 1.5px currentColor;
  }

  /* The event panel: a sheet at the side, full screen on a phone. */
  .cal-panel {
    position: fixed; inset-block: 0; inset-inline-end: 0; z-index: 20; inline-size: min(100vi, 28rem);
    background: var(--surface); border-inline-start: var(--hairline); overflow-y: auto;
    box-shadow: 0 0 2rem color-mix(in oklab, black 35%, transparent);
  }
  .sheet { display: grid; gap: var(--space-xs); padding: var(--panel-pad); }
  .sheet-header { display: flex; align-items: start; justify-content: space-between; gap: var(--space-xs); }
  .sheet-header h2 { overflow-wrap: anywhere; }
  .sheet-meta { display: flex; align-items: center; gap: var(--space-2xs); }
  .sheet-fields { display: grid; gap: var(--space-xs); border: 0; padding: 0; margin: 0; min-inline-size: 0; }
  .sheet .when { border: 0; padding: 0; margin: 0; }
  .sheet .when legend { font-weight: 600; margin-block-end: var(--space-3xs); }
  @media (max-width: 40rem) {
    .cal-panel { inline-size: 100vi; border: 0; }
    /* The page behind a full-screen sheet stays put. */
    body.cal-panel-open { overflow: hidden; }
  }

  /* Which occurrences: a small modal with three plain choices. */
  /* margin: auto centers a modal dialog; the reset's margin: 0 undid that. */
  .cal-scope { margin: auto; border: var(--hairline); border-radius: var(--radius-m); background: var(--surface); color: var(--text); padding: var(--panel-pad); inline-size: min(92vi, 22rem); }
  .cal-scope::backdrop { background: color-mix(in oklab, black 50%, transparent); }
  .cal-scope .form > button { inline-size: 100%; }
  .cal-scope-q { font-weight: 600; }

  /* Saved, moved, deleted: with Undo (decision 12). */
  .cal-toast {
    position: fixed; z-index: 30; inset-block-end: var(--space-s); inset-inline-start: 50%; translate: -50% 0;
    display: flex; align-items: center; gap: var(--space-xs); max-inline-size: min(92vi, 32rem);
    padding: var(--space-2xs) var(--space-xs); border-radius: var(--radius-m);
    background: var(--text); color: var(--canvas); box-shadow: 0 0.5rem 1.5rem color-mix(in oklab, black 30%, transparent);
  }
  .cal-toast button { background: transparent; color: inherit; border-color: color-mix(in oklab, currentColor 50%, transparent); }
}

/* The grid takes our tokens (decisions 13 and 36). Unlayered on purpose:
   EventCalendar's own stylesheet is unlayered, and unlayered rules beat
   every layer, so these sit at the same level with a stronger selector. */
.cal .ec {
  /* EventCalendar forces color-scheme: light on .ec, which would flip our
     light-dark() tokens inside the grid; follow the page instead. */
  color-scheme: inherit;
  --ec-color-400: var(--text-quiet);
  --ec-color-300: var(--line);
  --ec-color-200: var(--surface-sunken);
  --ec-color-100: var(--surface);
  --ec-color-50: var(--canvas);
  --ec-bg-color: var(--surface);
  --ec-text-color: var(--text);
  --ec-border-color: var(--line);
  --ec-today-bg-color: var(--today-tint);
  --ec-highlight-color: color-mix(in oklab, var(--accent) 22%, transparent);
  --ec-now-indicator-color: var(--danger);
  --ec-popup-bg-color: var(--surface);
  --ec-event-bg-color: var(--accent);
  --ec-event-text-color: var(--on-accent);
  --ec-bg-event-color: var(--text-quiet);
  --ec-button-bg-color: var(--surface);
  --ec-button-border-color: var(--line);
  --ec-button-text-color: var(--text);
  --ec-button-active-bg-color: var(--accent-tint);
  --ec-button-active-border-color: var(--line);
  --ec-button-active-text-color: var(--text);
  border-radius: var(--radius-m);
  font-size: var(--step--1);
}
.cal .ec .ec-event { border-radius: var(--radius-s); cursor: pointer; overflow: hidden; }
/* Time, title and initials flow as one line that wraps, so a short event
   shows "08:00-08:30 School run" instead of a title spilling out below. */
.cal .ec .ec-event-body { display: block; overflow: hidden; line-height: 1.25; }
.cal .ec.ec-time-grid .ec-event.cal-ev-short { padding-block: 0; }
.cal .ec.ec-time-grid .cal-ev-short .ec-event-body { white-space: nowrap; text-overflow: ellipsis; }
.cal .ec.ec-time-grid .cal-ev-tiny .ec-event-body { font-size: 0.8em; line-height: 1; }
/* Today (decision 55): a light tint so the hour lines still show, and
   the date in a filled accent pill, which reads by brightness as well as
   hue (not only for color-blind eyes). */
.cal .ec .ec-col-head.ec-today > time,
.cal .ec:not(.ec-list) .ec-day.ec-today > .ec-day-head > time {
  display: inline-block;
  padding: 0.1em 0.55em;
  border-radius: 999px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
}
@media (max-width: 40rem) {
  /* Narrow columns: the pill shrinks with them, as the other days do. */
  .cal .ec .ec-col-head.ec-today > time { padding-inline: 0.2em; max-inline-size: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: middle; }
}
/* Its own built-in shades switch on a polyfill pair; set it from our theme. */
:root:is([data-theme="dark"], [data-theme="black"]) .cal .ec { --lightningcss-light: ; --lightningcss-dark: initial; }
:root[data-theme="light"] .cal .ec { --lightningcss-light: initial; --lightningcss-dark: ; }
@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] .cal .ec { --lightningcss-light: ; --lightningcss-dark: initial; }
}
