:root {
  --green: #0f2a21;
  --green-ink: #1d4a3a;
  --cream: #f3ece0;
  --cream-light: #f8f3ea;
  --cream-line: #b8ab90;
  --crimson: #7d1d24;
  --silver: #cbcfc8;
  --serif: "Cormorant Garamond", Garamond, Georgia, serif;

  /* One motion language: slow out, settled in */
  --ease: cubic-bezier(0.65, 0, 0.35, 1);
  --ease-out: cubic-bezier(0.2, 0.7, 0.2, 1);
}

*,
*::before,
*::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100svh;
  background: var(--green);
  color: var(--silver);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

:focus-visible {
  outline: 1px solid rgba(243, 236, 224, 0.7);
  outline-offset: 6px;
}

/* Double hairline frame, like a printed card border */
.frame {
  position: fixed;
  inset: max(14px, env(safe-area-inset-top)) 14px 14px;
  border: 1px solid rgba(203, 207, 200, 0.2);
  pointer-events: none;
}

.frame::after {
  content: "";
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(203, 207, 200, 0.1);
}

.scene {
  min-height: 100svh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  justify-items: center;
  padding: clamp(40px, 7vh, 72px) 24px clamp(36px, 6vh, 60px);
}

.house {
  margin: 0;
  font-weight: 500;
  font-size: 11.5px;
  letter-spacing: 0.34em;
  text-transform: uppercase;
  color: rgba(203, 207, 200, 0.7);
  transition: opacity 0.8s var(--ease);
}

/* The letterhead says it now */
.scene:not([data-state="sealed"]):not([data-state="open"]) .house { opacity: 0; }

.centre {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: clamp(28px, 5vh, 44px);
  width: 100%;
}

.addressee {
  margin: 0;
  max-width: 30em;
  font-style: italic;
  font-size: clamp(17px, 2.3vw, 21px);
  line-height: 1.5;
  text-align: center;
  text-wrap: balance;
  color: rgba(243, 236, 224, 0.9);
  transition: opacity 0.8s var(--ease), transform 0.8s var(--ease);
}

@media (max-width: 520px) {
  .addressee br { display: none; }
}

.hint {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 0;
  font-weight: 500;
  font-size: 11px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  min-height: 1.3em;
  color: rgba(203, 207, 200, 0.6);
  transition: opacity 0.6s var(--ease);
}

.hint::before,
.hint::after {
  content: "";
  width: 28px;
  height: 1px;
  background: rgba(203, 207, 200, 0.35);
}

.hint.quiet { opacity: 0; }

/* ---------- Envelope ---------- */

.env {
  position: relative;
  width: min(100%, 620px, calc((100svh - 330px) * 1.5));
  min-width: 260px;
  aspect-ratio: 3 / 2;
  perspective: 1600px;
  transition: transform 1.2s var(--ease);
}

.env-shadow {
  position: absolute;
  inset: 0;
  box-shadow: 0 28px 40px rgba(0, 0, 0, 0.32), 0 2px 3px rgba(0, 0, 0, 0.25);
}

.layer {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

#envShade { transition: opacity 0.4s var(--ease); }

/* Back to front: lining, letter, pocket, shade, flap, seal.
   Once open, the flap drops behind so the letter can rise in front of it. */
.env-shadow { z-index: 0; }
#envInner { z-index: 1; }
.letter { z-index: 2; }
#envPocket { z-index: 3; }
#envShade { z-index: 4; }
.flap { z-index: 5; }
.seal { z-index: 6; }
.scene[data-state="open"] .flap,
.scene[data-state="drawing"] .flap,
.scene[data-state="reading"] .flap,
.scene[data-state="returning"] .flap { z-index: 0; }

/* Flying back to the envelope, the letter passes over everything */
.letter.homing { z-index: 7; }

.scene[data-state="open"] .env { cursor: grab; }
.scene[data-state="open"] .env:active { cursor: grabbing; }
.scene[data-state="open"] .env { touch-action: none; }

/* ---------- The letter ----------
   A portrait sheet folded once. In the envelope it is the folded size; the sheet
   hangs from its top edge. Every measurement inside is in sheet widths (cqi),
   so the letter lays out identically at any size, like a printed page. */

.letter {
  position: absolute;
  left: 5%;
  top: 7.5%;
  width: 90%;
  aspect-ratio: 540 / 340;
  transition: translate 0.5s var(--ease-out);
}

.scene[data-state="open"] .env:hover .letter { translate: 0 -5%; }

.letter.presenting {
  position: fixed;
  z-index: 20;
  transition: none;
}

.sheet {
  position: absolute;
  inset: 0 0 auto 0;
  aspect-ratio: 540 / 680;
  container-type: inline-size;
  perspective: 2400px;
}

.panel {
  position: absolute;
  left: 0;
  width: 100%;
  height: 50%;
}

.panel.upper {
  top: 0;
  overflow: hidden;
  background: var(--cream-light);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  transition: box-shadow 1s var(--ease);
}

.panel.lower {
  top: 50%;
  transform-origin: 50% 0;
  transform-style: preserve-3d;
  transform: rotateX(180deg);
}

.side {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--cream-light);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* The outside of the fold reads upright once the lower half is flipped over */
.side.outside {
  transform: rotateX(180deg);
  box-shadow: inset 0 0 0 1px rgba(184, 171, 144, 0.45), 0 1px 2px rgba(0, 0, 0, 0.12);
}

.outside-mark {
  position: absolute;
  left: 50%;
  top: 2%;
  width: 15%;
  translate: -50% 0;
}

/* The full page is set once per half; each half shows its own part */
.content {
  position: absolute;
  left: 0;
  width: 100%;
  height: 200%;
  top: 0;
}

.side.inside .content { top: -100%; }

.letter.presenting .panel.upper { box-shadow: 0 1.5cqi 4cqi rgba(0, 0, 0, 0.22); }

/* Offset down and pulled in, so none of it falls back over the fold */
.letter.presenting .side.inside { box-shadow: 0 3cqi 4cqi -1cqi rgba(0, 0, 0, 0.26); }

/* Beneath the open sheet, which is twice the folded height */
.fold-away {
  position: absolute;
  left: 50%;
  top: calc(200% + 18px);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 8px 12px calc(8px + 0.3em);
  border: 0;
  background: none;
  font-weight: 500;
  font-size: 11px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  white-space: nowrap;
  color: rgba(203, 207, 200, 0.7);
  cursor: pointer;
  translate: -50% 8px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.9s var(--ease), translate 0.9s var(--ease), visibility 0s 0.9s, color 0.3s ease;
}

.fold-away::before,
.fold-away::after {
  content: "";
  width: 28px;
  height: 1px;
  background: rgba(203, 207, 200, 0.35);
  transition: width 0.4s var(--ease);
}

.fold-away:hover { color: rgba(243, 236, 224, 0.95); }
.fold-away:hover::before,
.fold-away:hover::after { width: 40px; }

.fold-away.shown {
  translate: -50% 0;
  opacity: 1;
  visibility: visible;
  transition: opacity 0.9s var(--ease), translate 0.9s var(--ease), visibility 0s, color 0.3s ease;
}

/* What she says if you look away while she writes; it takes the fold line's place */
.aside {
  position: absolute;
  left: 50%;
  top: calc(200% + 22px);
  margin: 0;
  font-style: italic;
  font-size: 18px;
  white-space: nowrap;
  color: rgba(243, 236, 224, 0.85);
  pointer-events: none;
  translate: -50% 6px;
  opacity: 0;
  transition: opacity 0.9s var(--ease), translate 0.9s var(--ease);
}

.aside.shown {
  translate: -50% 0;
  opacity: 1;
}

/* Once open it is one flat sheet, with no seam between the halves */
.letter.flat .panel.upper { height: 100%; }
.letter.flat .panel.upper .content { height: 100%; }
.letter.flat .panel.lower { visibility: hidden; }

/* ---------- The page ---------- */

.page {
  height: 100%;
  padding: 7.5cqi 10cqi 6.5cqi;
  color: var(--green-ink);
  font-size: 3.5cqi;
  line-height: 1.5;
  box-shadow: inset 0 0 0 1.6cqi var(--cream-light), inset 0 0 0 calc(1.6cqi + 1px) rgba(184, 171, 144, 0.55);
}

.page p { margin: 0 0 0.6em; }

.letterhead {
  display: grid;
  justify-items: center;
  gap: 1.2cqi;
  margin-bottom: 4cqi;
}

.letterhead svg { width: 7.5cqi; }

.letterhead span {
  font-weight: 500;
  font-size: 1.85cqi;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  opacity: 0.8;
}

.page .date {
  margin-bottom: 3cqi;
  text-align: right;
  font-style: italic;
  font-size: 3.1cqi;
  opacity: 0.85;
}

.page .salute,
.page .close { font-style: italic; }

.page .salute { margin-bottom: 2cqi; }

/* A line she thinks better of, struck through by hand once the letter is open */
.page .strike { position: relative; }

.page .strike::after {
  content: "";
  position: absolute;
  left: -0.15em;
  right: -0.15em;
  top: 0.66em;
  height: 1px;
  background: rgba(29, 74, 58, 0.85);
  transform: scaleX(0);
  transform-origin: 0 50%;
}

.page .strike.struck::after {
  transform: scaleX(1);
  transition: transform 0.8s cubic-bezier(0.5, 0, 0.3, 1);
}

.page .close { margin: 3.5cqi 0 0.6cqi; }

/* Lines she writes while you watch, hidden until the pen crosses them. The mask is
   a soft edge that travels left to right; the padding keeps swashes inside it. */
.page .sign {
  margin: -1cqi 0 -1.5cqi -2cqi;
  padding: 1cqi 4cqi 1.5cqi 2cqi;
  font-family: "Pinyon Script", cursive;
  font-size: 7.4cqi;
  line-height: 1.15;
}

.page .ps {
  margin: 3cqi 0 0 -1cqi;
  padding: 0 2cqi 0 1cqi;
  font-style: italic;
  font-size: 3.2cqi;
  --ink-time: 1.2s;
}

.page .ink {
  width: max-content;
  -webkit-mask-image: linear-gradient(90deg, #000 45%, transparent 55%);
  mask-image: linear-gradient(90deg, #000 45%, transparent 55%);
  -webkit-mask-size: 220% 100%;
  mask-size: 220% 100%;
  -webkit-mask-position: 100% 0;
  mask-position: 100% 0;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
}

.page .ink.inked { animation: ink var(--ink-time, 1.8s) cubic-bezier(0.45, 0.05, 0.4, 0.95) forwards; }

@keyframes ink {
  to {
    -webkit-mask-position: 0 0;
    mask-position: 0 0;
  }
}

/* ---------- The Howler ---------- */

.page { transition: opacity 0.5s var(--ease); }
.page.hush { opacity: 0; }

/* A crimson copy of the page over the green one, revealed by a circle that grows
   from her signature, and later shrinks back into it */
@property --r {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

.page.crimson {
  position: absolute;
  inset: 0;
  color: var(--crimson);
  pointer-events: none;
  box-shadow: none;
  -webkit-mask-image: radial-gradient(circle at var(--x) var(--y), #000 calc(var(--r) - 14cqi), transparent var(--r));
  mask-image: radial-gradient(circle at var(--x) var(--y), #000 calc(var(--r) - 14cqi), transparent var(--r));
}

/* The page's hairline border, in crimson */
.page.crimson::before {
  content: "";
  position: absolute;
  inset: 1.6cqi;
  border: 1px solid rgba(125, 29, 36, 0.45);
}

/* A copy of the page as it stands, with everything already written */
.page.crimson .ink,
.page.settled .ink {
  animation: none;
  -webkit-mask-image: none;
  mask-image: none;
}

.page.crimson svg circle,
.page.crimson svg path { stroke: var(--crimson); }
.page.crimson svg text { fill: var(--crimson); }
.page.crimson .strike::after { background: var(--crimson); }

.page.crimson.spread { animation: spread 1.5s cubic-bezier(0.45, 0, 0.55, 1) forwards; }
.page.crimson.drain { animation: drain 1.5s cubic-bezier(0.25, 0, 0.45, 1) forwards; }

@keyframes spread {
  from { --r: 0px; }
  to { --r: 125cqi; }
}

@keyframes drain {
  from { --r: 125cqi; }
  to { --r: 0px; }
}

/* What she shouts, set like engraved capitals */
.howl {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 5cqi;
  padding: 12cqi;
  pointer-events: none;
}

.howl-line {
  margin: 0;
  padding-left: 0.14em;
  font-weight: 600;
  font-size: 7cqi;
  line-height: 1.2;
  letter-spacing: 0.14em;
  text-align: center;
  text-transform: uppercase;
  text-wrap: balance;
  color: var(--crimson);
}

@media (prefers-reduced-motion: reduce) {
  .page .ink.inked { animation-duration: 1ms; }
  .page.crimson.spread,
  .page.crimson.drain { animation-duration: 1ms; }
  .page .strike.struck::after { transition: none; }
}

.page .title {
  margin: 0.8cqi 0 0;
  font-weight: 500;
  font-size: 1.7cqi;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  opacity: 0.7;
}

/* The flap turns on its top edge; its inside is the green lining */
.flap {
  position: absolute;
  inset: 0 0 auto 0;
  height: 62.5%;
  transform-origin: 50% 0;
  transform-style: preserve-3d;
}

.face {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.face.back { transform: rotateX(180deg); }

/* ---------- Seal ---------- */

.seal {
  position: absolute;
  left: 50%;
  top: 62.5%;
  width: 23.333%;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  translate: -50% -50%;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.seal > * {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.crest svg { display: block; width: 100%; height: 100%; }

.crest.whole { transition: transform 0.35s var(--ease-out); }
.seal.holding .crest.whole { transform: scale(0.97); }

.crest.half { visibility: hidden; }
.crest.left { clip-path: polygon(0 0, 46% 0, 54% 100%, 0 100%); }
.crest.right { clip-path: polygon(46% 0, 100% 0, 100% 100%, 54% 100%); }

.seal.split .crest.whole { visibility: hidden; }
.seal.split .crest.half { visibility: visible; }

.seal-ring circle {
  fill: none;
  stroke: var(--green-ink);
}

.seal-ring .track {
  stroke-width: 0.6;
  opacity: 0;
  transition: opacity 0.4s var(--ease);
}

.seal:hover .seal-ring .track,
.seal.holding .seal-ring .track { opacity: 0.25; }

.seal-ring .progress {
  stroke-width: 1;
  stroke-dasharray: 1 1;
  stroke-dashoffset: 1;
  transition: stroke-dashoffset 0.45s var(--ease);
}

.seal.holding .seal-ring .progress { transition: none; }

.seal-cut line {
  stroke: var(--cream);
  stroke-width: 0.8;
  stroke-dasharray: 1 1;
  stroke-dashoffset: 1;
}

.seal-cut { pointer-events: none; }

/* ---------- Closing screen ---------- */

.ending {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-content: center;
  justify-items: center;
  padding: 24px;
  text-align: center;
  outline: none;
}

.ending > * { animation: rise 1.4s var(--ease-out) both; }
.ending > :nth-child(2) { animation-delay: 0.35s; }
.ending > :nth-child(3) { animation-delay: 0.9s; }
.ending > :nth-child(4) { animation-delay: 1.5s; }
.ending > :nth-child(5) { animation-delay: 1.9s; }

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

.end-mark {
  width: 46px;
  margin-bottom: 26px;
  opacity: 0.85;
}

.end-title {
  margin: 0;
  font-weight: 400;
  font-size: clamp(40px, 8vw, 64px);
  line-height: 1.1;
  letter-spacing: 0.01em;
  color: var(--cream);
}

.end-sub {
  margin: 12px 0 40px;
  font-style: italic;
  font-size: clamp(18px, 2.4vw, 21px);
  color: rgba(243, 236, 224, 0.75);
}

.end-link {
  padding: 15px 30px 15px calc(30px + 0.3em);
  border: 1px solid rgba(203, 207, 200, 0.4);
  color: var(--cream);
  font-weight: 500;
  font-size: 11.5px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  text-decoration: none;
  transition: border-color 0.3s ease, background 0.3s ease;
}

.end-link:hover {
  border-color: rgba(243, 236, 224, 0.85);
  background: rgba(243, 236, 224, 0.04);
}

.end-again {
  margin-top: 22px;
  padding: 10px;
  border: 0;
  background: none;
  font-style: italic;
  font-size: 16px;
  color: rgba(203, 207, 200, 0.6);
  cursor: pointer;
  transition: color 0.3s ease;
}

.end-again:hover { color: rgba(243, 236, 224, 0.9); }

@media (prefers-reduced-motion: reduce) {
  .ending > * { animation: none; }
}

.noscript {
  position: fixed;
  inset: auto 16px 40px;
  margin: 0;
  text-align: center;
  font-style: italic;
}
