/* Sealed letters: the write / read sheets (src/letters/). A cream deckled paper card over the dimmed
   moon scene — "Dear Future Me," in script, mountains + sun line art, leaf sprigs at the corners,
   ruled writing lines — plus the send animation (fold → envelope → wax seal → off to the lantern).
   Light paper on a dark page, so it carries its own palette; buttons reuse the moon theme's red.
   The dialog never scrolls the page: it is a fixed layer, the paper is centred and capped at the
   viewport, and only its body scrolls (the writing area first gives up lines). Inputs ≥ 16px. */
.letter-sheet,
.letter-seal {
  --paper: #f4eee3;
  --paper-shade: #ebe3d4;
  --paper-ink: #2b2b2b;
  --paper-ink-soft: #6e675c;
  --paper-green: #2f4a3a;
  --paper-rule: rgba(43, 43, 43, 0.14);
  --paper-error: #a8432c;
  --line: 34px; /* one ruled line: the textarea's line-height and the rules' period */
  --art-width: 132px;
  --paper-art: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 170 92'%3E%3Ccircle cx='104' cy='22' r='14' fill='%23F2DDA4' stroke='%23D8BC78' stroke-width='0.8'/%3E%3Cg fill='none' stroke='%237A7163' stroke-width='1.1' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 84L44 54L54 61L84 30L104 50L114 43L162 84'/%3E%3Cpath d='M84 30L79 44L87 53L80 66'/%3E%3Cpath d='M114 43L110 55L118 63'/%3E%3Cpath d='M44 54L48 64'/%3E%3Cpath d='M70 47L66 57'/%3E%3Cpath d='M132 60L128 70'/%3E%3C/g%3E%3Cpath d='M0 84H168' stroke='%237A7163' stroke-opacity='0.45' stroke-width='0.9'/%3E%3C/svg%3E");
  --paper-sprig: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 124 150'%3E%3Cdefs%3E%3ClinearGradient id='g1' x1='0' y1='0' x2='1' y2='0'%3E%3Cstop offset='0' stop-color='%235E7A5C'/%3E%3Cstop offset='1' stop-color='%2393A98B'/%3E%3C/linearGradient%3E%3ClinearGradient id='g2' x1='0' y1='0' x2='1' y2='0'%3E%3Cstop offset='0' stop-color='%234E6A50'/%3E%3Cstop offset='1' stop-color='%237E9878'/%3E%3C/linearGradient%3E%3ClinearGradient id='g3' x1='0' y1='0' x2='1' y2='0'%3E%3Cstop offset='0' stop-color='%236D8768'/%3E%3Cstop offset='1' stop-color='%23A9BBA0'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cpath d='M6 148C28 112 56 66 116 6' fill='none' stroke='%235C5A45' stroke-width='1.6' stroke-linecap='round'/%3E%3Cg transform='translate(16 128) rotate(-150) scale(1)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g1)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(26 112) rotate(-18) scale(1)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g2)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(38 92) rotate(-138) scale(0.95)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g1)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(50 76) rotate(-24) scale(0.92)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g3)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(63 58) rotate(-128) scale(0.86)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g2)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(77 42) rotate(-34) scale(0.8)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g1)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(92 26) rotate(-118) scale(0.7)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g3)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3Cg transform='translate(104 14) rotate(-52) scale(0.62)'%3E%3Cpath d='M0 0C9-7 27-8 40 0C27 8 9 7 0 0Z' fill='url(%23g2)'/%3E%3Cpath d='M3 0Q20 -1 37 0' fill='none' stroke='%23EEF2E4' stroke-opacity='0.4' stroke-width='0.9'/%3E%3C/g%3E%3C/svg%3E");
}

.letter-sheet {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Room above for the close button that overlaps the paper's corner */
  padding: calc(var(--safe-top) + 22px) calc(env(safe-area-inset-right, 0px) + 16px)
    calc(var(--safe-bottom) + 14px) calc(env(safe-area-inset-left, 0px) + 16px);
  overflow: hidden;
  background: rgba(5, 7, 18, 0.76); /* no backdrop blur: it would re-blur the live canvas every frame */
  opacity: 0;
  transition: opacity 0.35s ease;
}

.letter-sheet[hidden] { display: none; }
.letter-sheet.is-visible { opacity: 1; }
.letter-sheet.is-leaving { opacity: 0; pointer-events: none; }

/* Kept rendered (0 × 0): a display:none <svg> would switch its filter off in some browsers */
.letter-defs { position: absolute; width: 0; height: 0; overflow: hidden; }

/* ---------- The paper: a flex column capped at the viewport; only its body scrolls ---------- */
.letter-paper {
  position: relative;
  isolation: isolate;
  display: flex;
  flex-direction: column;
  width: min(440px, 100%);
  max-height: 100%;
  color: var(--paper-ink);
  font-family: 'Be Vietnam Pro', system-ui, sans-serif;
  outline: none;
  transform: translateY(14px) scale(0.985);
  transition: transform 0.45s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.12s ease;
}

.letter-sheet.is-visible .letter-paper { transform: none; }

/* The sheet itself and a second page peeking out behind it. Only these pseudo-elements go through
   the deckle filter, so typing never re-runs it; the drop shadow follows the torn outline. */
.letter-paper::before,
.letter-paper::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 3px;
}

.letter-paper::before {
  z-index: -1;
  background: radial-gradient(130% 90% at 25% 8%, #f9f5ec 0%, var(--paper) 55%, var(--paper-shade) 100%);
  filter: url(#paper-deckle) drop-shadow(0 18px 26px rgba(0, 0, 0, 0.5));
}

.letter-paper::after {
  z-index: -2;
  background: #e4dbca;
  transform: rotate(1.4deg) translate(5px, 6px);
  filter: url(#paper-deckle) drop-shadow(0 10px 16px rgba(0, 0, 0, 0.35));
}

.letter-paper__body {
  position: relative; /* the line art scrolls with the heading */
  display: flex;
  flex: 0 1 auto;
  flex-direction: column;
  min-height: 0;
  padding: 28px 26px 26px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.letter-paper__body > * { flex-shrink: 0; }

/* On the paper's corner, so it travels with the sheet instead of hiding in the viewport's corner */
.letter-sheet__close {
  position: absolute;
  z-index: 3;
  top: -14px;
  right: -14px;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: rgba(16, 14, 26, 0.9);
  box-shadow: inset 0 0 0 1px rgba(217, 181, 110, 0.55), 0 8px 18px -8px rgba(0, 0, 0, 0.7);
  color: #f1e3c2;
  cursor: pointer;
}

.letter-sheet__close .icon { width: 19px; height: 19px; }
.letter-sheet__close:focus-visible { outline: 2px solid #d9b56e; outline-offset: 2px; }

.letter-paper__art {
  position: absolute;
  top: 20px;
  right: 22px;
  width: var(--art-width);
  height: calc(var(--art-width) * 0.54);
  background: var(--paper-art) center / contain no-repeat;
  pointer-events: none;
}

.letter-paper__sprig {
  position: absolute;
  z-index: 1;
  width: 100px;
  height: 121px;
  background: var(--paper-sprig) center / contain no-repeat;
  pointer-events: none;
}

.letter-paper__sprig--top { top: -34px; right: -34px; transform: rotate(186deg); }
.letter-paper__sprig--bottom { bottom: -30px; left: -30px; width: 82px; height: 99px; transform: rotate(8deg); }

.letter-paper__heading {
  margin: 6px 0 0;
  padding-right: calc(var(--art-width) - 12px); /* clear of the mountains */
  font: 600 clamp(27px, 8.4vw, 38px) / 1.15 'Dancing Script', cursive;
  color: var(--paper-ink);
}

/* Ruled writing area. The rules are the textarea's own background, one per line-height; the JS keeps
   them glued to the text while it scrolls (background-attachment: local is unreliable on iOS).
   It is the one part that gives up height when the screen is short (down to 3 lines). */
.letter-paper__lines {
  --rows: 8;
  display: block;
  flex: 1 1 auto;
  width: 100%;
  height: calc(var(--line) * var(--rows) + 2px);
  min-height: calc(var(--line) * 3 + 2px);
  margin: 14px 0 0;
  padding: 0 4px;
  border: 0;
  border-radius: 0;
  outline: none;
  resize: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  background-color: transparent;
  background-image: linear-gradient(transparent calc(var(--line) - 1px), var(--paper-rule) calc(var(--line) - 1px));
  background-size: 100% var(--line);
  background-position: 0 calc(-5px + var(--scroll, 0px)); /* rules sit just under the baseline */
  color: var(--paper-ink);
  caret-color: var(--paper-green);
  font: 400 17px / var(--line) 'Be Vietnam Pro', system-ui, sans-serif;
  -webkit-appearance: none;
  appearance: none;
}

.letter-paper__lines::placeholder { color: #a39c90; }
.letter-paper__lines:focus { --paper-rule: rgba(47, 74, 58, 0.3); }
.letter-paper__lines[aria-invalid='true'] { --paper-rule: rgba(168, 67, 44, 0.42); }

.letter-paper__count {
  margin: 4px 2px 0;
  text-align: right;
  font-size: 12px;
  color: var(--paper-ink-soft);
  font-variant-numeric: tabular-nums;
}

.letter-paper__count[data-full] { color: var(--paper-error); }
.letter-paper__count[hidden] { display: none; }
/* ---------- Fields ---------- */
.letter-field { margin-top: 18px; }

.letter-field__label {
  display: block;
  margin: 0 0 8px 2px;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.3;
  color: var(--paper-ink);
}
.letter-field__optional { font-weight: 400; opacity: 0.6; }

.letter-field__box { position: relative; }

.letter-field__icon {
  position: absolute;
  left: 15px;
  top: 50%;
  width: 20px;
  height: 20px;
  transform: translateY(-50%);
  color: var(--paper-ink-soft);
  pointer-events: none;
}

.letter-field__input {
  width: 100%;
  height: 52px;
  padding: 0 16px 0 46px;
  border: 1px solid rgba(43, 43, 43, 0.17);
  border-radius: 12px;
  background: rgba(255, 253, 248, 0.5);
  color: var(--paper-ink);
  font: 400 16px / 1.2 'Be Vietnam Pro', system-ui, sans-serif; /* ≥ 16px keeps iOS from zooming */
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.letter-field__input--code { padding-right: 54px; }
.letter-field__input::placeholder { color: #a39c90; }
.letter-field__input:focus { border-color: var(--paper-green); background: rgba(255, 253, 248, 0.85); box-shadow: 0 0 0 3px rgba(47, 74, 58, 0.14); }
.letter-field__input[aria-invalid='true'] { border-color: var(--paper-error); box-shadow: 0 0 0 3px rgba(168, 67, 44, 0.12); }
.letter-field__input:read-only { opacity: 0.75; }

.letter-field__eye {
  position: absolute;
  right: 4px;
  top: 50%;
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--paper-ink);
  transform: translateY(-50%);
  cursor: pointer;
}

.letter-field__eye .icon { width: 22px; height: 22px; }
.letter-field__eye-slash { display: none; }
.letter-field__eye[aria-pressed='true'] .letter-field__eye-slash { display: inline; }
.letter-field__eye:focus-visible { outline: 2px solid var(--paper-green); outline-offset: -3px; }

.letter-field__hint { margin: 7px 2px 0; font-size: 12.5px; line-height: 1.45; color: var(--paper-ink-soft); }
.letter-field__hint + .letter-field__hint { margin-top: 2px; }

/* Offline demo only (?demo=1): the sheet still asks for an email and a code, but keeps nothing */
.letter-paper__demo-note {
  margin: 14px 2px 0;
  padding: 6px 12px;
  border: 1px dashed rgba(47, 74, 58, 0.35);
  border-radius: 8px;
  font-size: 12.5px;
  font-weight: 500;
  line-height: 1.4;
  text-align: center;
  color: var(--paper-green);
}

.letter-paper__demo-note[hidden] { display: none; }

/* Sheet-local status: sealing / opening progress and errors, room reserved so nothing jumps */
.letter-paper__message {
  min-height: 20px;
  margin: 14px 2px 0;
  font-size: 13.5px;
  font-weight: 500;
  line-height: 1.45;
  text-align: center;
  color: var(--paper-green);
}

.letter-paper__message[data-tone='error'] { color: var(--paper-error); }

.letter-paper__message[data-tone='busy']::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 8px;
  border-radius: 50%;
  background: currentColor;
  vertical-align: 1px;
  animation: letter-busy 1s ease-in-out infinite;
}

@keyframes letter-busy { 50% { opacity: 0.2; } }

/* ---------- Buttons ---------- */
/* The same lacquer-red pill as the panel's "Viết thư" (moon theme variables on :root) */
.letter-send {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 56px;
  margin-top: 10px;
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: var(--button-bg);
  box-shadow: inset 0 0 0 1px var(--button-ring), 0 12px 22px -12px rgba(60, 14, 8, 0.85);
  color: var(--button-text);
  font: 600 15px / 1.2 'Be Vietnam Pro', system-ui, sans-serif;
  letter-spacing: 0.02em;
  white-space: nowrap; /* one line down to 320px-wide phones */
  cursor: pointer;
  transition: background 0.2s ease, transform 0.15s ease, opacity 0.2s ease;
}

.letter-send:hover { background: var(--button-bg-hover); }
.letter-send:active { transform: scale(0.98); }
.letter-send:focus-visible { outline: 2px solid var(--paper-green); outline-offset: 3px; }
.letter-send:disabled { opacity: 0.7; cursor: progress; }
.letter-send .icon { width: 20px; height: 20px; }

.letter-send--quiet {
  min-height: 50px;
  margin-top: 26px;
  background: none;
  box-shadow: inset 0 0 0 1px rgba(47, 74, 58, 0.35);
  color: var(--paper-green);
}

.letter-send--quiet:hover { background: rgba(47, 74, 58, 0.06); }
.letter-send[hidden] { display: none; } /* author display beats the hidden attribute otherwise */
[data-read-next] { margin-bottom: 8px; }

/* ---------- Read sheet ---------- */
.letter-unlock__intro { margin: 14px 2px 0; font-size: 15px; line-height: 1.5; color: var(--paper-ink-soft); }
.letter-opened { outline: none; }
.letter-opened[hidden], .letter-unlock[hidden] { display: none; }

.letter-opened__text {
  min-height: calc(var(--line) * 6);
  margin: 14px 0 0;
  padding: 0 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background-image: linear-gradient(transparent calc(var(--line) - 1px), var(--paper-rule) calc(var(--line) - 1px));
  background-size: 100% var(--line);
  background-position: 0 -5px;
  font: 400 17px / var(--line) 'Be Vietnam Pro', system-ui, sans-serif;
}

.letter-opened__sign { margin: 18px 4px 0; text-align: right; font: 600 27px / 1.2 'Dancing Script', cursive; overflow-wrap: anywhere; }
.letter-opened__sign:not(:empty)::before { content: '— '; }
.letter-opened__date { margin: 4px 4px 0; text-align: right; font-size: 12.5px; color: var(--paper-ink-soft); }

/* ---------- After a send: the paper hands over to the seal stage, the dim lifts as it flies ---------- */
.letter-sheet.is-sealing { opacity: 0; pointer-events: none; transition: opacity 0.3s ease 1.24s; }
.letter-sheet.is-sealing .letter-paper { opacity: 0; }

/* Timeline (ms): 0 hand-over · 120 bottom third folds up · 360 top third folds down · 560 envelope
   rises · 740 letter slides in · 980 flap shuts · 1150 wax seal · 1260–1560 flies to the lantern.
   src/letters/letter-seal-animation.js places the stage and ends it with a timer. */
.letter-seal {
  position: fixed;
  z-index: 21;
  pointer-events: none;
  transform-origin: var(--env-cx) var(--env-cy);
}

.letter-seal[hidden] { display: none; }
.letter-seal.is-playing { animation: seal-fly 0.3s cubic-bezier(0.5, 0, 0.75, 0.35) 1.26s both; }

.letter-seal__sheet {
  position: absolute;
  inset: 0;
  perspective: 1400px;
  transform-style: preserve-3d;
  transform-origin: 50% 50%;
}

.letter-seal.is-playing .letter-seal__sheet {
  animation: seal-appear 0.12s ease-out both, seal-slide-in 0.26s cubic-bezier(0.4, 0, 0.6, 1) 0.74s both;
}

.letter-seal__panel {
  position: absolute;
  left: 0;
  top: calc(var(--i) * 100% / 3);
  width: 100%;
  height: calc(100% / 3 + 0.5px);
  transform-style: preserve-3d; /* clipping lives one level down: overflow here would flatten the fold */
}

.letter-seal__front,
.letter-seal__back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  box-shadow: 0 10px 22px -14px rgba(0, 0, 0, 0.6);
}

.letter-seal__front { overflow: hidden; }
.letter-seal__back { background: linear-gradient(#ece3d2, #e2d8c4); transform: rotateX(180deg); }

.letter-seal__face {
  position: absolute;
  left: 0;
  top: calc(var(--i) * var(--sheet-h) / -3);
  width: 100%;
  height: var(--sheet-h);
  background: radial-gradient(130% 90% at 25% 8%, #f9f5ec 0%, var(--paper) 55%, var(--paper-shade) 100%);
}

.letter-seal__face .letter-paper__art { right: auto; }
.letter-seal .letter-seal__heading { position: absolute; margin: 0; padding: 0; white-space: nowrap; }
.letter-seal__clip { position: absolute; overflow: hidden; }

.letter-seal__text {
  margin: 0;
  padding: 0 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background-image: linear-gradient(transparent calc(var(--line) - 1px), var(--paper-rule) calc(var(--line) - 1px));
  background-size: 100% var(--line);
  background-position: 0 -5px;
  color: var(--paper-ink);
  font: 400 17px / var(--line) 'Be Vietnam Pro', system-ui, sans-serif;
}

.letter-seal.is-playing .letter-seal__panel:nth-child(3) {
  transform-origin: 50% 0;
  animation: seal-fold-up 0.26s cubic-bezier(0.4, 0, 0.2, 1) 0.12s both;
}

.letter-seal.is-playing .letter-seal__panel:nth-child(1) {
  transform-origin: 50% 100%;
  animation: seal-fold-down 0.26s cubic-bezier(0.4, 0, 0.2, 1) 0.36s both;
}

/* The envelope: the cream and the red wax of the tiny letters on the lanterns (envelope-sprite-factory.js) */
.letter-seal__env {
  position: absolute;
  left: var(--env-left);
  top: var(--env-top);
  width: var(--env-w);
  height: var(--env-h);
  overflow: visible;
  stroke: rgba(176, 140, 84, 0.9);
  stroke-width: 0.8;
  vector-effect: non-scaling-stroke;
}

.letter-seal__env path, .letter-seal__env rect { vector-effect: non-scaling-stroke; }
.letter-seal__env--back { fill: #dcc395; }
.letter-seal__env--front { fill: #f6e7c8; filter: drop-shadow(0 -1px 1px rgba(90, 60, 20, 0.18)); }
.letter-seal__crease { fill: none; stroke: rgba(176, 140, 84, 0.55); }
.letter-seal__flap { fill: #ecd9b3; }
.letter-seal__flap--open { top: calc(var(--env-top) - var(--env-h) * 0.58); height: calc(var(--env-h) * 0.58); transform-origin: 50% 100%; }
.letter-seal__flap--closed { fill: #f1e0bd; transform-origin: 50% 0; }

.letter-seal__wax {
  position: absolute;
  left: calc(var(--env-left) + var(--env-w) * 0.415);
  top: calc(var(--env-top) + var(--env-h) * 0.58 - var(--env-w) * 0.085);
  width: calc(var(--env-w) * 0.17);
  height: calc(var(--env-w) * 0.17);
  fill: #b3261e;
  filter: drop-shadow(0 2px 2px rgba(60, 10, 6, 0.45));
}

.letter-seal__wax-ring { fill: none; stroke: #7e1712; stroke-width: 1.4; }
.letter-seal__wax-mark { fill: none; stroke: rgba(255, 214, 196, 0.6); stroke-width: 1.3; stroke-linecap: round; stroke-linejoin: round; }

.letter-seal.is-playing .letter-seal__env { animation: seal-env-in 0.24s ease-out 0.56s both; }
.letter-seal.is-playing .letter-seal__flap--open { animation: seal-env-in 0.24s ease-out 0.56s both, seal-flap-down 0.09s ease-in 0.98s both; }
.letter-seal.is-playing .letter-seal__flap--closed { animation: seal-env-in 0.24s ease-out 0.56s both, seal-flap-shut 0.1s ease-out 1.07s both; }
.letter-seal.is-playing .letter-seal__wax { animation: seal-wax 0.14s ease-out 1.15s both; }

@keyframes seal-appear { from { opacity: 0; } to { opacity: 1; } }
/* Matching transform lists, so each rotation interpolates 0 → 180° instead of a matrix shortcut;
   the small translateZ keeps the folded thirds stacked (no z-fighting) */
@keyframes seal-fold-up { from { transform: rotateX(0deg) translateZ(0); } to { transform: rotateX(180deg) translateZ(-1px); } }
@keyframes seal-fold-down { from { transform: rotateX(0deg) translateZ(0); } to { transform: rotateX(-180deg) translateZ(-2px); } }
@keyframes seal-slide-in { from { transform: translateY(0) scale(1); } to { transform: translateY(var(--packet-drop)) scale(var(--packet-scale)); } }
/* `translate` (not transform) so the flaps can rise and fold at the same time */
@keyframes seal-env-in { from { opacity: 0; translate: 0 40px; } to { opacity: 1; translate: 0 0; } }
@keyframes seal-flap-down { from { transform: scaleY(1); } to { transform: scaleY(0); } }
@keyframes seal-flap-shut { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes seal-wax { 0% { opacity: 0; transform: scale(0); } 70% { opacity: 1; transform: scale(1.18); } 100% { opacity: 1; transform: scale(1); } }
@keyframes seal-fly {
  from { opacity: 1; transform: translate(0, 0) scale(1); }
  to { opacity: 0; transform: translate(var(--fly-x), var(--fly-y)) scale(0.1); }
}

/* ---------- Short screens: tighter spacing first, so the whole sheet fits without scrolling ---------- */
@media (max-height: 900px) {
  .letter-paper__body { padding: 22px 24px 20px; }
  .letter-paper__art { top: 16px; }
  .letter-paper__heading { margin-top: 2px; font-size: clamp(27px, 7.6vw, 33px); }
  .letter-paper__lines { margin-top: 8px; }
  .letter-field { margin-top: 12px; }
  .letter-field__label { margin-bottom: 6px; font-size: 13.5px; }
  .letter-field__input { height: 46px; }
  .letter-field__eye { width: 42px; height: 42px; }
  .letter-field__hint { margin-top: 4px; font-size: 12px; }
  .letter-field__hint + .letter-field__hint { margin-top: 1px; }
  .letter-paper__demo-note { margin-top: 8px; padding: 4px 10px; font-size: 12px; }
  .letter-paper__message { min-height: 18px; margin-top: 8px; }
  .letter-send { min-height: 50px; margin-top: 6px; }
  .letter-unlock__intro { margin-top: 8px; }
}

@media (min-height: 1000px) {
  .letter-paper__lines { --rows: 10; }
}

/* Landscape phones: the body scrolls, so keep the chrome small */
@media (max-height: 500px) {
  .letter-sheet { --art-width: 96px; padding-top: calc(var(--safe-top) + 18px); padding-bottom: calc(var(--safe-bottom) + 8px); }
  .letter-paper__body { padding: 16px 22px 16px; }
  .letter-paper__heading { font-size: 27px; }
}

@media (max-width: 420px) {
  .letter-sheet { --art-width: 108px; }
  .letter-sheet__close { right: -8px; }
}

@media (max-width: 360px) {
  .letter-sheet { --art-width: 92px; }
  .letter-paper__body { padding-right: 18px; padding-left: 18px; }
  .letter-paper__art { right: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  .letter-sheet, .letter-paper { transition-duration: 0.01s; }
  .letter-paper { transform: none; }
  .letter-paper__message[data-tone='busy']::before { animation: none; }
}
