/* ============================================================
   A thank-you and an apology, sealed in one envelope
   ============================================================ */

:root {
  --night-top: #12162a;
  --night: #1a1f3a;
  --night-low: #282848;
  --glow: rgba(226, 194, 114, .11);
  --envelope: #f4ecdc;
  --envelope-shade: #e9dfca;
  --paper: #f7f1e5;
  --ink: #2b2433;
  --ink-soft: rgba(43, 36, 51, .66);
  --amber-ink: #7a5c22;
  --gold: #c8a250;
  --gold-light: #e9cf86;
  --focus: #e9cf86;
  --mist: rgba(244, 236, 218, .84);

  --serif: "Newsreader", "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --hand: "Homemade Apple", "Segoe Script", "Bradley Hand", "Snell Roundhand", cursive;

  --env-w: min(86vw, 440px);
  --sheet-w: min(calc(100vw - 32px), 620px);
  --sheet-h: 760px; /* script.js resizes this to fit the screen */
  --ph: calc(var(--sheet-h) / 3);

  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .26 0 0 0 0 .19 0 0 0 0 .12 .13 0 0 0 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");

  color-scheme: dark;
}

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

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

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--night);
  color: var(--mist);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.js body.is-sealed { overflow: hidden; }

::selection { background: rgba(200, 162, 80, .3); }

.defs { position: absolute; width: 0; height: 0; overflow: hidden; }

/* ---------- Background: night sky with drifting gold ---------- */

.sky {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 60% 45% at 50% 50%, var(--glow), transparent 70%),
    linear-gradient(180deg, var(--night-top) 0%, var(--night) 55%, var(--night-low) 100%);
}

.dust {
  position: absolute;
  width: var(--size);
  height: var(--size);
  color: var(--c);
  opacity: var(--o);
  animation: dust var(--d) ease-in-out var(--delay) infinite alternate;
}

@keyframes dust {
  from { transform: translateY(14px) rotate(var(--rot)); opacity: calc(var(--o) * .3); }
  to { transform: translateY(-20px) rotate(calc(var(--rot) + 60deg)); opacity: var(--o); }
}

/* ---------- Password prompt ---------- */

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

.gate {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 48px 16px 24px;
  overflow-y: auto;
}

.gate[hidden] { display: none; }

.gate-card {
  position: relative;
  width: min(100%, 360px);
  margin: 0;
  padding: 48px clamp(22px, 7vw, 34px) 30px;
  border-radius: 6px;
  background:
    var(--grain),
    linear-gradient(160deg, #faf4e8, var(--envelope) 60%, #ebe1cd);
  background-size: 180px 180px, auto;
  box-shadow: 0 22px 45px -16px rgba(4, 5, 16, .75), 0 2px 5px rgba(4, 5, 16, .4);
  color: var(--ink);
  text-align: center;
}

.gate-seal {
  position: absolute;
  top: 0;
  left: 50%;
  width: 64px;
  height: 64px;
  transform: translate(-50%, -50%) rotate(-8deg);
  filter: drop-shadow(0 2px 2px rgba(4, 5, 16, .32));
  /* Green wax on the password card */
  --wax: #5f7a2a;
  --wax-face: #546d24;
  --wax-ring: #7c9838;
  --wax-deep: #3e521b;
  --wax-emboss: #88a442;
}

.gate-title {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 500;
  line-height: 1.25;
}

.gate-note {
  margin: 6px 0 22px;
  color: var(--ink-soft);
  font-style: italic;
}

.gate-input {
  display: block;
  width: 100%;
  padding: 6px 4px;
  border: 0;
  border-bottom: 1px solid rgba(200, 162, 80, .75);
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  caret-color: var(--ink);
  font: inherit;
  font-size: 1.15rem; /* 16px or more, so iPhones don't zoom in */
  letter-spacing: .12em;
  text-align: center;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
}

.gate-input:focus { border-bottom-color: var(--gold); box-shadow: 0 1px 0 var(--gold); }
.gate-card.is-wrong .gate-input { border-bottom-color: var(--amber-ink); box-shadow: 0 1px 0 var(--amber-ink); }

.gate-error {
  min-height: 1.5em;
  margin: 8px 0 14px;
  color: var(--amber-ink);
  font-size: .92rem;
  font-style: italic;
}

.gate-button {
  -webkit-appearance: none;
  appearance: none;
  min-width: 140px;
  min-height: 44px;
  padding: 10px 26px;
  border: 0;
  border-radius: 999px;
  background: var(--night);
  color: var(--paper);
  font: inherit;
  font-size: 1rem;
  letter-spacing: .02em;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(200, 162, 80, .55), 0 6px 14px -6px rgba(18, 22, 42, .7);
  transition: background-color .2s, transform .2s;
}

.gate-button:hover { background-color: #2a3156; }
.gate-button:active { transform: translateY(1px); }
.gate-button:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }
.gate-button:disabled { cursor: progress; opacity: .85; }

/* Nothing behind the prompt shows until the password is right. */
.js body.is-locked .stage { visibility: hidden; }
.no-js .reading, .no-js .gate-controls { display: none; }

/* ---------- Art in the corner of the background ----------
   Filled in from the sealed letter once the password is right (its own
   styles come with it). Tapping it plays it again, so the layers above let
   taps through wherever they're empty. */

.backdrop-art {
  position: fixed;
  left: max(10px, 2.5vw, env(safe-area-inset-left));
  bottom: max(6px, 1.5vh, env(safe-area-inset-bottom));
  z-index: 1;
  width: clamp(140px, 19vw, 240px);
  opacity: 0;
  transition: opacity 1.2s ease;
}

.backdrop-art.is-visible { opacity: .92; }

.backdrop-art svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
  pointer-events: none;
  -webkit-tap-highlight-color: transparent;
}

.backdrop-art svg > * { pointer-events: auto; cursor: pointer; }

.stage, .reading { pointer-events: none; }
.stage > *, .sheet { pointer-events: auto; }

/* ---------- Stage: the sealed envelope ---------- */

.stage {
  position: fixed;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(28px, 6vh, 48px);
  padding: 12vh 16px 16px;
}

.stage[hidden], .no-js .stage { display: none; }

.float {
  width: var(--env-w);
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0) rotate(-.8deg); }
  50% { transform: translateY(-9px) rotate(.6deg); }
}

.envelope {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  position: relative;
  width: 100%;
  aspect-ratio: 100 / 62;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  perspective: 1400px;
  container-type: inline-size;
  transition: transform .5s cubic-bezier(.2, .7, .2, 1);
}

@media (hover: hover) {
  .envelope:not(.is-opening):hover { transform: translateY(-6px) rotate(-1deg); }
}

.envelope:focus-visible { outline: 2px solid var(--focus); outline-offset: 12px; }
.envelope.is-opening { cursor: default; }

.envelope-turn {
  position: absolute;
  inset: 0;
  display: block;
  transform-style: preserve-3d;
}

.env-face {
  position: absolute;
  inset: 0;
  display: block;
  border-radius: 5px;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  box-shadow: 0 22px 45px -16px rgba(4, 5, 16, .75), 0 2px 5px rgba(4, 5, 16, .4);
}

/* Front: stamp and the handwritten address */

.env-front {
  background:
    var(--grain),
    linear-gradient(160deg, #faf4e8, var(--envelope) 60%, #ebe1cd);
  background-size: 180px 180px, auto;
}

.stamp {
  position: absolute;
  top: 6cqw;
  right: 6.5cqw;
  display: block;
  width: 13cqw;
  transform: rotate(4deg);
  filter: drop-shadow(0 1px 1px rgba(43, 36, 51, .22));
}

.stamp svg { display: block; width: 100%; height: auto; }
.stamp-paper { fill: #fdf9f1; }
.stamp-field { fill: var(--night); }
.stamp-moon { fill: var(--gold-light); }

.address {
  position: absolute;
  top: 25cqw;
  left: 13cqw;
  right: 13cqw;
  display: block;
  color: var(--ink);
  text-align: left;
}

.address-to {
  display: block;
  color: var(--ink-soft);
  font-size: max(11px, 3.6cqw);
  font-style: italic;
  line-height: 1;
}

.address-name {
  display: block;
  padding: 1.2cqw 0 .6cqw;
  border-bottom: 1px solid rgba(200, 162, 80, .75);
  font-family: var(--hand);
  font-size: max(18px, 6.2cqw);
  line-height: 1.7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.address-note {
  display: block;
  margin-top: 2cqw;
  color: var(--ink-soft);
  font-size: max(11px, 3.6cqw);
  font-style: italic;
  line-height: 1.3;
}

/* Back: pocket, flap, wax seal, and the letter inside */

.env-back {
  transform: rotateY(180deg);
  background: var(--envelope-shade);
  perspective: 900px;
}

.env-inside {
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: 5px;
  background: repeating-linear-gradient(45deg, rgba(200, 162, 80, .32) 0 1px, transparent 1px 9px), #1e2442;
}

.env-letter {
  position: absolute;
  left: 7%;
  right: 7%;
  bottom: 6%;
  z-index: 2;
  display: block;
  height: var(--env-letter-h, 60%);
  border-radius: 2px;
  background: var(--grain), var(--paper);
  background-size: 180px 180px, auto;
  box-shadow: 0 1px 3px rgba(4, 5, 16, .25);
}

.env-pocket {
  position: absolute;
  inset: 0;
  z-index: 3;
  width: 100%;
  height: 100%;
  clip-path: inset(0 round 5px);
}

.pocket-side, .pocket-bottom, .flap-out {
  stroke: rgba(43, 36, 51, .14);
  stroke-width: 1px;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

.pocket-side { fill: #ece2cf; }
.pocket-bottom { fill: #f4ecdd; filter: url(#pocket-shadow); }

.env-from {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 7cqw;
  z-index: 3;
  color: var(--ink-soft);
  font-size: max(11px, 3.2cqw);
  font-style: italic;
  text-align: center;
}

.env-from-name {
  margin-left: .3em;
  color: var(--ink);
  font-family: var(--hand);
  font-size: 1.35em;
  font-style: normal;
}

.env-flap {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 4;
  width: 100%;
  height: 62%;
  overflow: visible;
  transform-origin: 50% 0;
  filter: drop-shadow(0 2px 2px rgba(4, 5, 16, .16));
}

.flap-out { fill: url(#flap-paper); }
.flap-in { fill: url(#lining); display: none; }

.env-back.is-open .env-flap { z-index: 1; }
.env-back.is-open .flap-out { display: none; }
.env-back.is-open .flap-in { display: inline; }

.seal {
  position: absolute;
  top: 57.5%;
  left: 50%;
  z-index: 5;
  display: block;
  width: 20cqw;
  aspect-ratio: 1;
  transform: translate(-50%, -50%) rotate(-8deg);
  filter: drop-shadow(0 2px 2px rgba(4, 5, 16, .32));
}

.seal-half { position: absolute; inset: 0; display: block; }
.seal-half svg { display: block; width: 100%; height: 100%; overflow: visible; }
.seal-left { z-index: 1; clip-path: polygon(0 0, 53% 0, 47% 28%, 55% 52%, 46% 76%, 51% 100%, 0 100%); }
/* The right half stays whole underneath until the envelope is opened, so no seam shows. */
.is-opening .seal-right { clip-path: polygon(53% 0, 100% 0, 100% 100%, 51% 100%, 46% 76%, 55% 52%, 47% 28%); }

/* Hint under the envelope */

.hint {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
}

.hint-action {
  color: var(--mist);
  font-size: 1.1rem;
  font-style: italic;
}

.hint-sound {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: rgba(244, 236, 218, .55);
  font-size: .88rem;
}

.hint-sound svg { width: 15px; height: 15px; }

/* ---------- The letter ---------- */

.reading {
  position: relative;
  z-index: 3;
  padding: max(24px, 6vh) 16px max(96px, 12vh);
}

.js .reading { visibility: hidden; }
.js .reading.is-visible { visibility: visible; }

.sheet {
  position: relative;
  width: var(--sheet-w);
  min-height: var(--sheet-h);
  margin: 0 auto;
  outline: none;
}

/* Paper with two soft creases where it was folded */
.paper-surface {
  background-color: var(--paper);
  background-image:
    linear-gradient(to bottom,
      transparent calc(var(--ph) - 18px),
      rgba(43, 36, 51, .05) calc(var(--ph) - 1px),
      rgba(43, 36, 51, .11) calc(var(--ph) - 1px),
      rgba(43, 36, 51, .11) var(--ph),
      rgba(255, 255, 255, .6) var(--ph),
      rgba(255, 255, 255, .6) calc(var(--ph) + 1px),
      rgba(255, 255, 255, .14) calc(var(--ph) + 1px),
      transparent calc(var(--ph) + 16px)),
    linear-gradient(to bottom,
      transparent calc(var(--ph) * 2 - 18px),
      rgba(43, 36, 51, .05) calc(var(--ph) * 2 - 1px),
      rgba(43, 36, 51, .11) calc(var(--ph) * 2 - 1px),
      rgba(43, 36, 51, .11) calc(var(--ph) * 2),
      rgba(255, 255, 255, .6) calc(var(--ph) * 2),
      rgba(255, 255, 255, .6) calc(var(--ph) * 2 + 1px),
      rgba(255, 255, 255, .14) calc(var(--ph) * 2 + 1px),
      transparent calc(var(--ph) * 2 + 16px)),
    var(--grain);
  background-size: 100% var(--sheet-h), 100% var(--sheet-h), 180px 180px;
  background-repeat: no-repeat, no-repeat, repeat;
}

.paper {
  position: absolute;
  inset: 0;
  z-index: 1;
  border-radius: 3px;
  box-shadow: 0 30px 60px -24px rgba(4, 5, 16, .7), 0 3px 8px rgba(4, 5, 16, .3);
}

.js .paper { opacity: 0; }
.js .sheet.is-open .paper { opacity: 1; }

.folds {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  width: 100%;
  height: var(--sheet-h);
  perspective: 1800px;
  transform-origin: 0 0;
  filter: drop-shadow(0 20px 26px rgba(4, 5, 16, .4));
}

.no-js .folds, .js .sheet.is-open .folds { display: none; }

.fold {
  position: absolute;
  left: 0;
  width: 100%;
  height: var(--ph);
  transform-style: preserve-3d;
}

.fold-a { top: 0; border-radius: 3px 3px 0 0; }

.fold-b, .fold-c {
  top: 100%;
  transform-origin: 50% 0;
  transform: rotateX(180deg);
}

.fold-b { background-position: 0 calc(var(--ph) * -1); }
.fold-c { background-position: 0 calc(var(--ph) * -2); border-radius: 0 0 3px 3px; }

.shade {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(30, 24, 44, .38), rgba(30, 24, 44, .08));
  opacity: 0;
  pointer-events: none;
}

.sheet-body {
  position: relative;
  z-index: 3;
  padding: clamp(32px, 8vw, 64px) clamp(24px, 8vw, 64px) clamp(40px, 9vw, 72px);
  color: var(--ink);
  font-size: clamp(1.08rem, .98rem + .45vw, 1.22rem);
  line-height: 1.72;
}

.sheet-body p { margin: 0 0 1.05em; }

.js .sheet:not(.is-revealed) [data-reveal] { opacity: 0; }

.sheet-body .letter-date {
  margin-bottom: 1.6em;
  color: var(--ink-soft);
  font-style: italic;
  text-align: right;
}

/* The letter itself */

.sheet-body .letter-greeting { margin-bottom: .8em; font-size: 1.1em; font-weight: 500; }

.sheet-body .letter-closing {
  margin: 2em 0 0;
  font-style: italic;
}

.sheet-body .letter-signature {
  display: inline-block;
  max-width: 100%;
  margin: 0;
  font-family: var(--hand);
  font-size: 1.5em;
  line-height: 2;
}

/* ---------- Gold flakes that rise and fall ---------- */

.flake {
  position: fixed;
  z-index: 6;
  pointer-events: none;
}

/* ---------- Music button ---------- */

.sound {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 5;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid rgba(244, 236, 218, .25);
  border-radius: 50%;
  background: rgba(18, 22, 42, .65);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: var(--paper);
  cursor: pointer;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .6s ease, transform .6s cubic-bezier(.2, .7, .2, 1), background-color .2s;
}

.sound[hidden] { display: none; }
.sound.is-visible { opacity: 1; transform: none; }
.sound:hover { background-color: rgba(200, 162, 80, .45); }
.sound:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
.sound svg { width: 20px; height: 20px; }
.sound .icon-off { display: none; }
.sound[aria-pressed="false"] .icon-on { display: none; }
.sound[aria-pressed="false"] .icon-off { display: block; }

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  .float, .dust { animation: none; }
  .envelope, .sound, .gate-input, .gate-button { transition: none; }
}
