/* Layout and components. Values come from tokens.css.
   The world: a night mist you write into (direction contract in
   index.html, system in DESIGN.md). Everything sits in one calm column. */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Components set their own display; hidden still wins over all of them. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

/* The background is set on body only, so it carries to the whole canvas and
   the fog layers below can sit between it and the page. */
body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  background: var(--mist);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: var(--text-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: clip; /* the top bar's ground reaches past the column */
}

/* ---- The mist: slow blooms of fog drifting behind everything. Never
   lighter than --mist-high where text sits, so contrast holds. */
body::before,
body::after {
  content: "";
  position: fixed;
  z-index: -1;
  pointer-events: none;
}

body::before {
  inset: -20%;
  background:
    radial-gradient(38% 30% at 22% 28%, color-mix(in srgb, var(--fog) 12%, transparent), transparent 70%),
    radial-gradient(34% 26% at 78% 62%, color-mix(in srgb, var(--fog) 10%, transparent), transparent 70%),
    radial-gradient(60% 34% at 50% 104%, color-mix(in srgb, var(--fog) 13%, transparent), transparent 72%);
  animation: fog-drift var(--fog-drift) ease-in-out infinite alternate;
}

/* Stillness: the mist closes in over where the letter was. */
body::after {
  inset: 0;
  background: radial-gradient(50% 42% at 50% 46%, color-mix(in srgb, var(--fog) 14%, transparent), transparent 75%);
  opacity: 0;
  transition: opacity var(--fog-close) var(--ease-out);
}

body:has(.stillness:not([hidden]))::after {
  opacity: 1;
}

@keyframes fog-drift {
  from { transform: translate3d(-3%, -2%, 0); }
  to { transform: translate3d(3%, 2%, 0); }
}

@media (prefers-reduced-motion: reduce) {
  body::before {
    animation: none;
  }

  body::after {
    transition: none;
  }
}

#app {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: var(--space-3);
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

input[type="checkbox"],
input[type="radio"] {
  accent-color: var(--accent);
}

:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

/* One heading voice: the UI face, light, like something seen through fog. */
.prompt h1,
.read h1,
.choose h1,
.stillness h1 {
  margin: 0;
  font-size: var(--text-display);
  font-weight: var(--weight-display);
  letter-spacing: var(--tracking-display);
  line-height: 1.15;
  text-wrap: balance;
}

.read h1:focus,
.choose h1:focus,
.stillness h1:focus {
  outline: none;
}

.hint {
  color: var(--text-soft);
  font-size: var(--text-sm);
}

/* ---- Buttons: frost pills. Primary is filled; the rest are outlined or
   bare, so there's only ever one bright thing to press. */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-h);
  padding: 0 var(--space-4);
  border: 1px solid var(--edge);
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
  transition:
    background-color 0.3s var(--ease-out),
    border-color 0.3s var(--ease-out),
    color 0.3s var(--ease-out);
}

.button:hover {
  background: var(--veil);
}

.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.button.primary:hover {
  border-color: var(--text-soft);
}

.button.quiet {
  border-color: transparent;
  color: var(--text-soft);
}

.button.quiet:hover {
  color: var(--text);
}

.button:disabled {
  opacity: 0.4;
  cursor: default;
}

.button:disabled:hover {
  background: transparent;
}

.button.primary:disabled:hover {
  background: var(--accent);
  border-color: var(--accent);
}

/* Undo and Redo: an icon, with the word kept as the button's name. */
.icon-button {
  width: var(--control-h);
  padding: 0;
}

.icon-button::before {
  content: "";
  width: 20px;
  height: 20px;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}

.icon-undo { --icon: url("../assets/icons/undo.svg"); }
.icon-redo { --icon: url("../assets/icons/redo.svg"); }

.icon-label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- Choice pills: every radio group (mode, text size, font, paper, mood,
   pen). The radio stays in the label, unseen, so the keyboard and screen
   readers work as normal; the label is the target and shows the choice. */

.segmented {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  border: 0;
  margin: 0;
  padding: 0;
  min-width: 0;
}

.segmented legend {
  color: var(--text-soft);
  font-size: var(--text-sm);
  margin-bottom: var(--space-2);
  padding: 0;
}

.segmented label {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 36px;
  padding: 0 var(--space-3);
  border: 1px solid var(--edge);
  border-radius: var(--radius-pill);
  color: var(--text);
  cursor: pointer;
  transition:
    background-color 0.3s var(--ease-out),
    color 0.3s var(--ease-out);
}

.segmented label:hover {
  background: var(--veil);
}

/* The unseen radio covers its pill, so a tap or click lands on it directly. */
.segmented input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.segmented label:has(input:checked) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.segmented label:has(input:focus-visible) {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

/* ---- Compose screen */

.compose[hidden] {
  display: none;
}

.compose,
.read,
.choose,
.release,
.stillness {
  width: 100%;
  max-width: var(--column);
}

.compose {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* The top bar, after Medium's editor: the name and the draft status on the
   left; Type / Handwrite, Undo, Redo and Page settings on the right. It
   stays at the top while the page scrolls, and gains a frosted ground and a
   hairline once it does. */
.topbar {
  position: sticky;
  top: 0;
  /* Wider than the page column on a big screen, like Medium's full-width
     bar, so everything fits on one row. Centered over the page. */
  align-self: center;
  width: min(calc(100vw - 2 * var(--space-3)), 1040px);
  z-index: 3; /* over the page, and the anchor for the settings panel */
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-3);
  padding-block: var(--space-2);
}

/* The bar's ground spans the whole window, not just the column. */
.topbar::before {
  content: "";
  position: absolute;
  inset: 0 calc(50% - 50vw);
  z-index: -1;
  border-bottom: 1px solid transparent;
  transition:
    background-color 0.3s var(--ease-out),
    border-color 0.3s var(--ease-out);
}

.topbar.is-scrolled::before {
  background: color-mix(in srgb, var(--mist) 86%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border-bottom-color: var(--hairline);
}

.topbar-start {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1) var(--space-3);
  min-width: 0;
}

.wordmark {
  margin: 0;
  color: var(--text-soft);
  font-size: var(--text-xs);
  font-weight: 500;
  letter-spacing: var(--tracking-mark);
  text-transform: uppercase;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-left: auto;
}

.topbar-status {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1);
  min-width: 0;
  color: var(--text-soft);
  font-size: var(--text-sm);
}

.topbar-status .button {
  min-height: 32px;
  padding: 0 var(--space-2);
  font-size: var(--text-sm);
}

.topbar-status:has(.draft-where:empty):has(> button[hidden]) {
  display: none;
}

.draft-confirm {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1);
}

.draft-confirm[hidden],
.resume[hidden] {
  display: none;
}

/* Type / Handwrite in the bar: a two-part switch. Words on a wide screen,
   a keyboard and a pen on a phone (the words stay as their names). */
.topbar .mode-switch {
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
}

.topbar .mode-switch label {
  min-height: 36px;
  padding: 0 var(--space-3);
  border-color: transparent;
  font-size: var(--text-sm);
}

.mode-switch label::before {
  content: "";
  width: 16px;
  height: 16px;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}

.mode-type { --icon: url("../assets/icons/keyboard.svg"); }
.mode-ink { --icon: url("../assets/icons/pen.svg"); }

@media (max-width: 40rem) {
  /* One row on a 390 px phone: name left, tools right. The draft status,
     when there is one, takes the row below. */
  .topbar-start {
    display: contents;
  }

  .topbar-status {
    order: 3;
    flex-basis: 100%;
  }

  .topbar .icon-button {
    width: 40px;
    min-height: 40px;
  }

  .topbar .mode-switch label {
    min-height: 32px;
    padding: 0 9px;
  }

  .mode-label,
  .settings-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

.resume {
  display: grid;
  gap: var(--space-3);
  justify-items: center;
  padding: var(--space-5) var(--space-4);
  background: var(--veil);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  text-align: center;
}

/* The question is the screen's heading while it's the only thing showing. */
.resume h1 {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: var(--weight-display);
}

/* A kept draft was found: ask first, show nothing else until answered. */
.compose.is-resuming > :not(.resume, .topbar),
.compose.is-resuming .topbar-actions,
.compose.is-resuming .topbar-status {
  display: none;
}

.compose.is-resuming .resume {
  margin-top: var(--space-6);
}

.prompt {
  margin-top: var(--space-5);
}

/* The question folds away into the mist once writing starts, and comes back
   on a blank page. It stays the screen's heading for screen readers. */
.prompt-intro {
  display: grid;
  grid-template-rows: 1fr;
  transition:
    grid-template-rows 0.7s var(--ease-out),
    filter 0.7s var(--ease-out);
}

.prompt-intro > div {
  min-height: 0;
  overflow: hidden;
}

.compose.has-writing .prompt-intro {
  grid-template-rows: 0fr;
  filter: blur(6px);
}

.compose.is-instant .prompt-intro {
  transition: none;
}

.prompt-help {
  max-width: 46ch;
  margin: var(--space-2) 0 var(--space-4);
  color: var(--text-soft);
}

.compose.has-writing .prompt {
  margin-top: var(--space-2);
}

/* Ways to start: one quiet control above the page; the letter type and the
   starter phrases open under it. */
.ways-panel {
  padding-top: var(--space-3);
}

.letter-type-label {
  display: block;
  margin-bottom: var(--space-2);
  color: var(--text-soft);
  font-size: var(--text-sm);
}

/* The native select, as a pill, with a drawn chevron. */
.select-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.select-wrap::after {
  content: "";
  position: absolute;
  right: var(--space-3);
  width: 16px;
  height: 16px;
  background: var(--text-soft);
  -webkit-mask: url("../assets/icons/chevron.svg") center / contain no-repeat;
  mask: url("../assets/icons/chevron.svg") center / contain no-repeat;
  pointer-events: none;
}

.letter-type {
  appearance: none;
  min-height: 40px;
  padding: 0 calc(var(--space-3) * 2 + 16px) 0 var(--space-3);
  border: 1px solid var(--edge);
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.letter-type option {
  background: var(--night);
  color: var(--text);
}

/* Ways to start: a row of quiet pills. On a phone they scroll sideways in
   one line, so the page starts higher up the screen. */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

.chip {
  min-height: 36px;
  padding: 0 var(--space-3);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  background: var(--veil);
  color: var(--text);
  font-size: var(--text-sm);
  white-space: nowrap;
  cursor: pointer;
  transition: border-color 0.3s var(--ease-out);
}

.chip:hover {
  border-color: var(--edge);
}

@media (max-width: 40rem) {
  .prompt {
    margin-top: var(--space-3);
  }

  .prompt-help {
    margin-bottom: var(--space-3);
  }

  .chips {
    margin-top: var(--space-3);
  }

  .chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin-inline: calc(-1 * var(--space-3));
    padding: 2px var(--space-3) var(--space-1);
    scroll-padding-inline: var(--space-3);
    scrollbar-width: none;
  }

  .chips::-webkit-scrollbar {
    display: none;
  }
}

/* Page settings and Ways to start: quiet toggles with a chevron that
   turns over when open. */
.settings-toggle,
.ways-toggle {
  min-height: 36px;
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
}

.ways-toggle {
  border-color: var(--hairline);
}

.settings-toggle::before,
.settings-toggle::after,
.ways-toggle::before,
.ways-toggle::after {
  content: "";
  width: 16px;
  height: 16px;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}

.settings-toggle::before { --icon: url("../assets/icons/sliders.svg"); }
.ways-toggle::before { --icon: url("../assets/icons/ways.svg"); }

.settings-toggle::after,
.ways-toggle::after {
  --icon: url("../assets/icons/chevron.svg");
  transition: transform 0.3s var(--ease-out);
}

/* Phones: Page settings is an icon in the bar. */
@media (max-width: 40rem) {
  .settings-toggle {
    width: 40px;
    min-height: 40px;
    padding: 0;
  }

  .settings-toggle::after {
    display: none;
  }
}

.settings-toggle[aria-expanded="true"],
.ways-toggle[aria-expanded="true"] {
  background: var(--veil);
  border-color: var(--text-soft);
}

.settings-toggle[aria-expanded="true"]::after,
.ways-toggle[aria-expanded="true"]::after {
  transform: rotate(180deg);
}

/* ---- The paper. One page in PAGE units, scaled to fit the width.
   --pu is one PAGE unit in CSS px, via container query units. The one lit
   thing in the mist. */

.paper {
  position: relative; /* the ink layer sits on top */
  container-type: inline-size;
  display: flex;
  /* One PAGE unit is one CSS px at full size; smaller screens scale down. */
  width: min(100%, calc(var(--page-width) * 1px));
  aspect-ratio: var(--page-width) / var(--page-height);
  margin: 0 auto;
  background: var(--paper);
  color: var(--ink);
  caret-color: var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.paper-inner {
  --pu: calc(100cqw / var(--page-width));
  --letter-size: var(--letter-size-2);
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5em; /* letter.js mirrors this: half the letter size */
  padding: calc(var(--page-margin) * var(--pu));
  font-family: var(--font-letter);
  /* Scaled with the page, but never below the small-screen floor. A phone
     page therefore holds fewer words than a desktop page. */
  font-size: max(
    calc(var(--letter-size) * var(--pu)),
    calc((var(--letter-min) + (var(--letter-size) - var(--letter-min)) * var(--letter-min-step)) * 1px)
  );
  line-height: var(--letter-line);
  min-width: 0;
}

.paper[data-size="1"] .paper-inner { --letter-size: var(--letter-size-1); }
.paper[data-size="3"] .paper-inner { --letter-size: var(--letter-size-3); }
.paper[data-font="clean"] .paper-inner { font-family: var(--font-ui); }

.paper input,
.paper .letter-body {
  border: 0;
  background: transparent;
  padding: 0;
  margin: 0;
  color: var(--ink);
}

.paper input:focus-visible,
.paper .letter-body:focus-visible {
  outline: 1px dashed var(--line);
  outline-offset: 4px;
}

.letter-to,
.letter-signoff {
  border-bottom: 1px solid var(--line);
  line-height: var(--letter-line);
}

.letter-signoff {
  align-self: flex-end;
  width: 60%;
  text-align: right;
}

.letter-body-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
}

/* Paper styles: rules one text line apart (lh), under the body only.
   letter.js draws the same rules on the read-it-once letter. */
.paper[data-paper="lined"] .letter-body-wrap {
  background-image: linear-gradient(to bottom, transparent calc(1lh - 1px), var(--rule) calc(1lh - 1px));
  background-size: 100% 1lh;
}

.paper[data-paper="grid"] .letter-body-wrap {
  background-image:
    linear-gradient(to bottom, transparent calc(1lh - 1px), var(--rule) calc(1lh - 1px)),
    linear-gradient(to right, transparent calc(1lh - 1px), var(--rule) calc(1lh - 1px));
  background-size: 1lh 1lh;
}

.letter-body {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  /* Typing can't overflow the page, but a resize or rotation can reflow
     existing text past it. Let it scroll rather than hide words. */
  overflow-y: auto;
  line-height: var(--letter-line);
  /* The same wrapping rules as wrap.js, so the drawn page matches. */
  white-space: pre-wrap;
  overflow-wrap: break-word;
  cursor: text;
}

/* Rich letters: each block is a paragraph or a list item. Indent steps and
   the list marker's room come from tokens.css; letter.js draws the same. */
.letter-body .blk {
  --level: 0;
  --gutter: 0;
  position: relative;
  padding-left: calc((var(--level) * var(--letter-indent) + var(--gutter)) * 1em);
}

.letter-body .blk[data-indent="1"] { --level: 1; }
.letter-body .blk[data-indent="2"] { --level: 2; }
.letter-body .blk[data-indent="3"] { --level: 3; }

.letter-body .blk[data-list] {
  --gutter: var(--letter-list-gutter);
}

.letter-body .blk[data-list]::before {
  content: attr(data-marker);
  position: absolute;
  left: calc(var(--level) * var(--letter-indent) * 1em);
  font-weight: 400;
  font-style: normal;
  text-decoration: none;
  user-select: none;
  -webkit-user-select: none;
}

/* The empty page's prompt, in the same contrast as the field placeholders. */
.letter-body.is-empty::after {
  content: attr(data-placeholder);
  position: absolute;
  top: 0;
  left: 0;
  color: var(--ink-soft);
  pointer-events: none;
}

.compose.is-ink .letter-body.is-empty::after {
  color: transparent;
}

/* Placeholders in a contrast that passes (WCAG AA, 5.5:1 on --paper).
   Firefox fades placeholders by default; opacity 1 keeps them the same. */
.paper-inner ::placeholder {
  color: var(--ink-soft);
  opacity: 1;
}

/* Placeholders under the ink read as clutter; typed words still show. */
.compose.is-ink .paper-inner ::placeholder {
  color: transparent;
}

/* Under the paper: "This page is full." in the middle, the word count at
   the right. */
.paper-foot {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  gap: var(--space-2);
}

.page-full {
  grid-column: 2;
  margin: 0;
  min-height: 1.5em;
  text-align: center;
  color: var(--text-soft);
  font-size: var(--text-sm);
}

.word-count {
  grid-column: 3;
  justify-self: end;
  margin: 0;
  color: var(--text-soft);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

.compose.is-ink .word-count {
  visibility: hidden; /* ink has no words to count */
}

/* Phones: the page runs edge to edge, like Medium's canvas. It's still the
   one 600 x 800 page, scaled to the screen's width. */
@media (max-width: 40rem) {
  .compose .paper {
    width: calc(100% + 2 * var(--space-3));
    margin-inline: calc(-1 * var(--space-3));
    border-radius: 0;
    box-shadow: none;
    border-block: 1px solid var(--hairline);
  }

  .compose .ink-layer {
    border-radius: 0;
  }
}

/* ---- The keyboard bar (touch screens, while typing on the page). It rides
   on top of the on-screen keyboard: compose.js sets --kbar-lift to the
   keyboard's height from the visual viewport. */
.kbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 4;
  transform: translateY(calc(-1 * var(--kbar-lift, 0px)));
  padding: var(--space-1) var(--space-2);
  background: var(--night);
  border-top: 1px solid var(--hairline);
}

.kbar-row {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

/* The tools scroll sideways on a narrow phone; Done stays put. */
.kbar-scroll {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}

.kbar-scroll::-webkit-scrollbar {
  display: none;
}

.kbar-formats {
  display: flex;
  gap: 2px;
  padding-right: var(--space-1);
  margin-right: var(--space-1);
  border-right: 1px solid var(--hairline);
}

.kbar .icon-button {
  flex: none;
  width: 40px;
  min-height: 40px;
}

/* A style that's on: the frost fill, like a chosen pill. */
.kbar [aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

.icon-bold { --icon: url("../assets/icons/bold.svg"); }
.icon-italic { --icon: url("../assets/icons/italic.svg"); }
.icon-underline { --icon: url("../assets/icons/underline.svg"); }
.icon-bullets { --icon: url("../assets/icons/bullets.svg"); }
.icon-numbers { --icon: url("../assets/icons/numbers.svg"); }
.icon-indent { --icon: url("../assets/icons/indent.svg"); }
.icon-outdent { --icon: url("../assets/icons/outdent.svg"); }

/* The writing bar shows only once someone types, but its icons are fetched
   with the page, so nothing is requested after load (privacy audit, item 1):
   one invisible pixel wears them all. */
.compose::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
  -webkit-mask-image:
    url("../assets/icons/bold.svg"),
    url("../assets/icons/italic.svg"),
    url("../assets/icons/underline.svg"),
    url("../assets/icons/bullets.svg"),
    url("../assets/icons/numbers.svg"),
    url("../assets/icons/outdent.svg"),
    url("../assets/icons/indent.svg");
  mask-image:
    url("../assets/icons/bold.svg"),
    url("../assets/icons/italic.svg"),
    url("../assets/icons/underline.svg"),
    url("../assets/icons/bullets.svg"),
    url("../assets/icons/numbers.svg"),
    url("../assets/icons/outdent.svg"),
    url("../assets/icons/indent.svg");
}

/* Desktop: the same bar floats at the bottom of the window while writing.
   No Done: there's no on-screen keyboard to put away. */
@media (pointer: fine) {
  .kbar {
    left: 50%;
    right: auto;
    bottom: var(--space-4);
    transform: translateX(-50%);
    max-width: calc(100vw - 2 * var(--space-3));
    padding: var(--space-1);
    border: 1px solid var(--hairline);
    border-radius: var(--radius-lg);
    box-shadow: var(--tray-shadow);
  }

  .kbar-done {
    display: none;
  }
}

.kbar .chips {
  margin: 0;
  padding: var(--space-1) 0 var(--space-2);
}

.kbar-done {
  margin-left: auto;
  color: var(--text);
  font-weight: var(--weight-strong);
}

.icon-ways { --icon: url("../assets/icons/ways.svg"); }
.icon-pen { --icon: url("../assets/icons/pen.svg"); }
.icon-type { --icon: url("../assets/icons/keyboard.svg"); }
.icon-settings { --icon: url("../assets/icons/sliders.svg"); }

/* While the bar is up, its Undo, Redo and settings stand in for the top bar's. */
.compose.has-kbar .topbar .icon-button,
.compose.has-kbar .topbar .settings-toggle {
  display: none;
}

/* Leave room for the bar when the browser scrolls a field into view. */
html:has(.compose.has-kbar) {
  scroll-padding-bottom: 72px;
}

/* ---- Handwriting */

.ink-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none; /* Type mode: clicks reach the text fields */
  border-radius: var(--radius);
}

.ink-layer.is-active {
  pointer-events: auto;
  touch-action: none; /* set before any gesture starts (MDN) */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  cursor: crosshair;
}

/* In Handwrite mode the typed fields stay visible but can't be reached by
   pointer; the text is still there when switching back. */
.compose.is-ink .paper-inner {
  user-select: none;
}

.ink-tray {
  /* Stays at the bottom of the screen while the paper is in view: where the
     thumb is on a phone (docs/writing-experience.md). */
  position: sticky;
  bottom: calc(env(safe-area-inset-bottom, 0px) + var(--space-2));
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--space-2) var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--night);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg); /* it wraps to several rows on a phone */
  box-shadow: var(--tray-shadow);
  font-size: var(--text-sm);
}

.ink-tray[hidden] {
  display: none;
}

.ink-tray .segmented label {
  min-height: 32px;
  padding: 0 var(--space-2);
}

.swatch span::before {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-right: var(--space-1);
  border-radius: 50%;
  border: 1px solid var(--edge);
  vertical-align: -0.05em;
}

.swatch-black span::before { background: var(--ink-black); }
.swatch-blue span::before { background: var(--ink-blue); }
.swatch-sepia span::before { background: var(--ink-sepia); }

.ink-confirm {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1);
}

.ink-confirm[hidden] {
  display: none;
}

/* ---- Page settings: a panel that drops from the top bar. */

.compose-settings {
  position: absolute;
  top: calc(100% + var(--space-1));
  right: 0;
  z-index: 2; /* over the page and the pen tray */
  width: min(100%, 460px);
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 220px), 1fr));
  gap: var(--space-4);
  max-height: min(70vh, 560px);
  overflow-y: auto;
  padding: var(--space-4);
  background: var(--night);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--tray-shadow);
  font-size: var(--text-sm);
  transform-origin: top right;
  animation: settings-open 0.25s var(--ease-out);
}

@keyframes settings-open {
  from { opacity: 0; transform: translateY(-4px) scale(0.98); }
}

/* "Let it go", where the writing ends: under the page. */
.compose-actions {
  display: flex;
  justify-content: center;
}

.compose-actions .primary {
  min-width: 240px;
}

@media (max-width: 40rem) {
  .compose-actions .primary {
    width: 100%;
  }
}

.check label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--target-min); /* WCAG 2.5.8 */
  cursor: pointer;
}

.check input {
  width: 18px;
  height: 18px;
  margin: 0;
}

.check .hint {
  margin: var(--space-1) 0 0;
  max-width: 36ch;
}

/* ---- Read it once */

.read {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-4);
}

.read[hidden] {
  display: none;
}

.read-top {
  align-self: flex-start;
}

.read h1 {
  text-align: center;
}

.read-letter {
  /* No taller than the screen leaves room for, like the release stage. */
  width: min(
    100%,
    calc(var(--page-width) * 1px),
    max(var(--release-min), calc((100svh - var(--read-chrome)) * var(--page-width) / var(--page-height)))
  );
}

.read-letter canvas {
  display: block;
  width: 100%;
  height: auto;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  opacity: 0;
  filter: blur(6px);
  transition:
    opacity var(--read-fade) var(--ease-out),
    filter var(--read-fade) var(--ease-out);
}

.read.settled .read-letter canvas {
  opacity: 1;
  filter: none;
}

.read-actions {
  display: flex;
  flex-direction: column; /* "Let it go" first, "Keep writing" under it */
  align-items: center;
  gap: var(--space-2);
}

.read-actions .read-let-go {
  min-width: 240px;
}

@media (max-width: 40rem) {
  .read-actions {
    align-self: stretch;
  }

  .read-actions .read-let-go {
    width: 100%;
  }
}

/* The soft pause: "Let it go" waits as an outlined button, then fills in.
   Both states pass WCAG AA (frost text on the mist: 11.3:1). */
.button.read-let-go {
  background: transparent;
  color: var(--accent);
  transition:
    background-color var(--read-fade) ease var(--read-pause),
    color var(--read-fade) ease var(--read-pause);
}

.read.settled .read-let-go,
.read-let-go:focus-visible,
.read-let-go:hover {
  background: var(--accent);
  color: var(--accent-ink);
}

@media (prefers-reduced-motion: reduce) {
  .read-letter canvas,
  .button.read-let-go {
    transition: none;
  }
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ---- Choose, release, stillness */

.choose,
.release,
.stillness {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

.choose[hidden],
.release[hidden],
.stillness[hidden] {
  display: none;
}

.screen-top {
  align-self: flex-start;
}

.screen-top-end {
  align-self: flex-end;
}

/* Bare buttons in the top row line their words up with the column's edge. */
.screen-top .button.quiet,
.read-top .button.quiet {
  margin-inline-start: calc(-1 * var(--space-4));
}

.screen-top-end .button.quiet {
  margin-inline: 0 calc(-1 * var(--space-3)); /* no further: a 320 px screen has 16 px to spare */
}

.choose h1,
.stillness h1 {
  text-align: center;
}

.choose h1 {
  margin-bottom: var(--space-2);
}

.screen-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-2);
}

.choose .screen-actions .primary {
  min-width: 200px;
}

/* The five ways to let go: a list of rows, one chosen. */
.paths {
  display: grid;
  gap: var(--space-2);
  width: 100%;
  border: 0;
  margin: 0;
  padding: 0;
}

.path-card {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: var(--space-3);
  align-items: center;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition:
    background-color 0.3s var(--ease-out),
    border-color 0.3s var(--ease-out);
}

.path-card:hover {
  background: var(--veil);
}

.path-card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

/* The ring at the end of each row; filled when chosen. */
.path-card::after {
  content: "";
  grid-column: 2;
  grid-row: 1 / span 2;
  width: 20px;
  height: 20px;
  border: 1.5px solid var(--edge);
  border-radius: 50%;
  transition:
    border-width 0.3s var(--ease-out),
    border-color 0.3s var(--ease-out);
}

.path-card:has(input:checked) {
  background: var(--veil);
  border-color: var(--accent);
}

.path-card:has(input:checked)::after {
  border: 6px solid var(--accent);
}

.path-card:has(input:focus-visible) {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

/* Preview: a small button under the line, and the scene below it. */
.path-preview-button {
  grid-column: 1;
  justify-self: start;
  min-height: 32px;
  margin: var(--space-1) 0 0 calc(-1 * var(--space-3));
  padding-inline: var(--space-3);
  font-size: var(--text-sm);
}

.path-preview-button[aria-pressed="true"] {
  color: var(--text);
}

.path-preview {
  grid-column: 1 / -1;
  height: clamp(180px, 34vh, 260px);
  margin-top: var(--space-3);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--mist);
}

.path-preview[hidden] {
  display: none;
}

.path-title {
  grid-column: 1;
  font-size: var(--text-lg);
  font-weight: 500;
}

.path-about {
  grid-column: 1;
  color: var(--text-soft);
  font-size: var(--text-sm);
}

/* Phones: five rows stacked, so keep them compact and Begin in reach. */
@media (max-width: 40rem) {
  .path-card {
    padding: var(--space-2) var(--space-3);
  }

  .path-title {
    font-size: var(--text-md);
  }

  /* Begin stays under the thumb. */
  .choose .screen-actions {
    position: sticky;
    bottom: 0;
    width: 100%;
    padding: var(--space-3) 0 calc(var(--space-2) + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(transparent, var(--mist) 35%);
  }

  .choose .screen-actions .primary {
    width: 100%;
  }
}

/* Forced colors (e.g. Windows contrast themes) drop backgrounds, shadows
   and custom border colors: keep the paper's edge and the chosen card. */
@media (forced-colors: active) {
  .paper {
    outline: 1px solid CanvasText;
  }

  .path-card:has(input:checked) {
    border: 4px double Highlight;
    margin: -3px; /* the wider border grows outward; the text stays put */
  }

  .segmented label:has(input:checked) {
    border: 2px solid Highlight;
  }
}

.mood {
  justify-content: center;
  margin-top: var(--space-2);
}

.mood legend {
  margin-inline: auto;
}

.mood .hint {
  flex-basis: 100%;
  margin: 0;
  text-align: center;
}

.release .screen-top {
  margin-top: calc(-1 * var(--space-2));
}

.release-hint {
  margin: 0;
  color: var(--text-soft);
  text-align: center;
}

.release-stage {
  /* As wide as fits, up to the page's own size, and no taller than the
     screen leaves room for (svh: the height with browser bars showing). */
  width: min(
    100%,
    calc(var(--page-width) * 1px),
    max(var(--release-min), calc((100svh - var(--release-chrome)) * var(--page-width) / var(--page-height)))
  );
  aspect-ratio: var(--page-width) / var(--page-height);
}

/* The boat's lake and the lantern's night have no box: their edges melt
   into the mist. */
.release-stage[data-scene="boat"],
.release-stage[data-scene="lantern"] {
  -webkit-mask-image: radial-gradient(farthest-side at 50% 50%, #000 72%, transparent);
  mask-image: radial-gradient(farthest-side at 50% 50%, #000 72%, transparent);
}

/* The scenic send-offs open out to a wide view: the full width of a phone,
   up to --scenic-max on a big screen, and as tall as the screen allows. */
.release-stage.is-scenic {
  width: min(var(--scenic-max), 100vw);
  max-width: none;
  margin-inline: calc((100% - min(var(--scenic-max), 100vw)) / 2);
  height: max(var(--release-min), calc(100svh - var(--release-chrome)));
  aspect-ratio: auto;
  /* The scene melts its own edges into the mist (the page itself never fades). */
  -webkit-mask-image: none;
  mask-image: none;
}

.stage-layers {
  position: relative;
  width: 100%;
  height: 100%;
}

.stage-layers > canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.stillness {
  position: relative;
  min-height: 60vh;
  justify-content: center;
}

.stillness > :not(.stillness-scene) {
  position: relative; /* above the scene */
}

.stillness .screen-top {
  position: absolute;
  top: 0;
  right: 0;
}

.stillness-scene {
  position: absolute;
  inset: 0;
  pointer-events: none;
  /* Fade every edge into the page, so the scene has no box around it. */
  -webkit-mask-image: radial-gradient(closest-side at 50% 55%, #000 55%, transparent);
  mask-image: radial-gradient(closest-side at 50% 55%, #000 55%, transparent);
}

.stillness-scene canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.stillness-line {
  margin: 0 0 var(--space-3);
  color: var(--text-soft);
  font-size: var(--text-lg);
}

/* After a scene: the release screen fades out instead of cutting away. */
.release {
  transition: opacity var(--leave-fade) var(--ease-calm);
}

.release.is-leaving {
  opacity: 0;
  pointer-events: none;
}

/* Then the ending arrives a piece at a time, slowly: the backdrop, the
   heading (rising a little), the line, and last "Write another". */
.stillness-scene,
.stillness h1,
.stillness-line,
.stillness-actions {
  opacity: 0;
  transition:
    opacity var(--still-fade) var(--ease-calm) var(--still-delay, 0s),
    transform var(--still-fade) var(--ease-calm) var(--still-delay, 0s);
}

.stillness h1 {
  --still-delay: calc(var(--still-step) * 0.5);
  transform: translateY(6px);
}

.stillness-line {
  --still-delay: calc(var(--still-step) * 1.5);
}

.stillness-actions {
  --still-delay: calc(var(--still-step) * 3);
}

.stillness.settled .stillness-scene,
.stillness.settled h1,
.stillness.settled .stillness-line,
.stillness.settled .stillness-actions,
.stillness-actions:focus-within {
  opacity: 1;
  transform: none;
}

/* Same soft pause as the read screen, and the same reason for it. */
.stillness-actions .button {
  background: transparent;
  color: var(--accent);
  transition:
    background-color var(--read-fade) ease var(--stillness-pause),
    color var(--read-fade) ease var(--stillness-pause);
}

.stillness.settled .stillness-actions .button,
.stillness-actions .button:focus-visible,
.stillness-actions .button:hover {
  background: var(--accent);
  color: var(--accent-ink);
}

@media (prefers-reduced-motion: reduce) {
  .stillness-actions .button {
    transition: none;
  }

  /* No staged arrival: the ending is there at once, as before. */
  .release,
  .stillness-scene,
  .stillness h1,
  .stillness-line,
  .stillness-actions {
    transition: none;
  }

  .stillness h1 {
    transform: none;
  }
}

/* ---- Release stage */

.stage {
  display: block;
  width: 100%;
  height: 100%;
  /* Set before any gesture starts (MDN): no scrolling or zooming on the canvas. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none; /* Safari: no long-press menu while holding to burn */
  cursor: none; /* each tool draws its own cursor */
}

/* ---- Footer: the privacy promise and the help links, on every screen. */

.site-footer {
  display: grid;
  justify-items: center;
  gap: var(--space-2);
  width: 100%;
  max-width: calc(var(--column) + 2 * var(--space-3));
  margin: 0 auto;
  padding: var(--space-4) var(--space-3) calc(var(--space-4) + env(safe-area-inset-bottom, 0px));
  color: var(--text-soft);
  font-size: var(--text-sm);
  text-align: center;
}

.site-footer p {
  margin: 0;
  max-width: 48ch;
}

.safety-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-1) var(--space-4);
}

.site-footer a {
  display: inline-block;
  min-height: var(--target-min); /* an easy target: these are the safety links */
  padding-block: var(--space-1);
  color: var(--text);
  text-decoration: underline;
  text-decoration-color: var(--edge);
  text-underline-offset: 3px;
}

.site-footer a:hover {
  text-decoration-color: var(--text);
}

/* Light / dark: a small sun or moon before the words. Offered before
   writing and after letting go; while a letter is on screen its colors are
   already drawn, so the switch steps aside. */
body:has(.read:not([hidden]), .choose:not([hidden]), .release:not([hidden])) .theme-switch {
  display: none;
}

.theme-switch {
  min-height: 36px;
  font-size: var(--text-sm);
}

.theme-switch::before {
  content: "";
  width: 16px;
  height: 16px;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}

.theme-switch[data-next="light"] { --icon: url("../assets/icons/sun.svg"); }
.theme-switch[data-next="dark"] { --icon: url("../assets/icons/moon.svg"); }

/* Changing the look fades it in, rather than flashing. */
body {
  transition: background-color 0.4s var(--ease-out), color 0.4s var(--ease-out);
}

@media (prefers-reduced-motion: reduce) {
  body {
    transition: none;
  }
}

/* Reduced motion for the controls. Last in the file on purpose: a media
   query adds no weight, so this has to come after the rules it undoes. */
@media (prefers-reduced-motion: reduce) {
  .button,
  .segmented label,
  .chip,
  .path-card,
  .path-card::after,
  .settings-toggle::after,
  .ways-toggle::after {
    transition: none;
  }

  .compose-settings {
    animation: none;
  }

  .prompt-intro,
  .topbar::before,
  .ways-toggle::after {
    transition: none;
  }
}
