#letter {
  position: relative;
  isolation: isolate;
  padding: clamp(32px, 8vh, 80px) 0 26px;
}
/* Keep the gift in its original grid cell while the envelope takes the foreground. */
html:has(:is(#gift, #letter):target) { scrollbar-gutter: stable both-edges }
main:has(#letter:target) { display: grid }
main:has(#letter:target) > :is(#gift, #letter) { grid-area: 1 / 1; align-self: start; min-width: 0 }
main:has(#letter:target) #gift {
  display: block;
  pointer-events: none;
}
main:has(#letter:target) #gift .gift-object { opacity: .13; transition: opacity .42s ease }
main:has(#letter:target) #gift > :is(h1,a) { opacity: 0 }
@media (max-width: 700px) {
  main:has(#letter:target) #gift {
    min-height: calc(94svh - 76px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow: clip;
  }
}
@media (prefers-reduced-motion: no-preference) {
  .js main:has(#letter:target) #gift .gift-object {
    opacity: calc(1 - .87 * var(--letter-presence, 0));
  }
  .js main:has(#letter:target) #gift > :is(h1,a) {
    opacity: clamp(0, calc(1 - 3 * var(--letter-presence, 0)), 1);
  }
  .js #letter:target .letter-stage {
    --remaining: calc(1 - var(--letter-presence, 0));
    transform-origin: 50% calc(100% - var(--envelope-height) * .5);
    opacity: var(--letter-presence, 0);
    transform: translateY(calc(var(--remaining) * var(--arrival-y, 70px)))
               rotate(calc(var(--remaining) * -3deg))
               scale(calc(1 - var(--remaining) * .18));
  }
  .js #letter:target > a {
    opacity: clamp(0, calc(2 * var(--letter-presence, 0) - 1), 1);
  }
}
.letter-stage {
  --envelope-y: 0px;
  --envelope-opacity: 1;
  --envelope-height: min(calc(63.291vw - 25.316px), 304px);
  position: relative;
  isolation: isolate;
  width: min(100%, 480px);
  height: 460px;
  margin: 0 auto 26px;
  overflow: clip;
  perspective: 1200px;
}
.envelope-part {
  position: absolute;
  left: 0;
  bottom: 0;
  display: none;
  width: 100%;
  height: var(--envelope-height);
  opacity: var(--envelope-opacity);
  transform: translateY(var(--envelope-y));
  pointer-events: none;
}
.js .envelope-part { display: block }
.envelope-back {
  z-index: 0;
  overflow: hidden;
  border-radius: 2px;
  background-color: #2a403a;
  background-image: url("./envelope-stock.jpg");
  background-size: 460px;
  /* The lower cut edge and contact shadow describe thick, uncoated stock. */
  box-shadow: 0 2px 0 #172923, 0 18px 34px #030a1066; /* taste-skill-ignore */
}
.envelope-mouth {
  position: absolute;
  inset: 0;
  clip-path: polygon(0 0, 100% 0, 50% 48%);
  background-color: #1d302b;
  background-image: linear-gradient(180deg, #0c1c1899, transparent 70%), url("./envelope-stock.jpg"); /* taste-skill-ignore */
  background-size: 100% 100%, 390px;
  background-position: 0 0, 37px 21px;
}
.envelope-lid {
  z-index: 4;
  perspective: 1200px;
}
.wax-seal {
  position: absolute;
  left: 50%;
  top: 94%;
  width: clamp(42px, 13%, 62px);
  height: auto;
  transform: translate(-50%, -50%) rotate(-6deg);
  backface-visibility: hidden;
  filter: drop-shadow(0 1px 1px #1c110ede) drop-shadow(1px 3px 2px #16161055);
}
.envelope-lid.show-lining .wax-seal { visibility: hidden }
.flap {
  position: absolute;
  inset: 0 0 auto;
  height: 54%;
  transform-origin: center top;
  transform-style: preserve-3d;
  /* The diagonal cut edges stay legible without an outline. */
  filter: drop-shadow(0 2px 1px #07171266); /* taste-skill-ignore */
}
.flap-face {
  position: absolute;
  inset: 0;
  clip-path: polygon(0 0, 100% 0, 50.3% 99.2%, 49.7% 99.2%);
  backface-visibility: hidden;
  background-image: url("./envelope-stock.jpg");
  background-size: 430px;
}
.flap-front {
  background-color: #385349;
  background-image: linear-gradient(164deg, #ffffff12, transparent 38%, #07171238), url("./envelope-stock.jpg"); /* taste-skill-ignore */
  background-position: 0 0, 80px 41px;
  /* A narrow lit cut edge, not an ornamental outline. */
  box-shadow: inset 0 1px #dce5d12b; /* taste-skill-ignore */
}
.flap-inner {
  transform: rotateX(180deg);
  /* Pre-mirror the asymmetric cut so the turned lining matches the front. */
  clip-path: polygon(0 100%, 100% 100%, 50.3% .8%, 49.7% .8%);
  background-color: #223832;
  background-image: linear-gradient(15deg, #071b1740, transparent 58%), url("./envelope-stock.jpg"); /* taste-skill-ignore */
  background-position: 0 0, 12px 67px;
}
.flap-shadow {
  z-index: 4;
  height: var(--envelope-height);
  opacity: var(--flap-shadow-opacity, 0);
  filter: blur(var(--flap-shadow-blur, 2px));
  transform: translateY(var(--envelope-y)) scaleY(var(--flap-shadow-scale, 1));
  transform-origin: center top;
  clip-path: polygon(0 0, 100% 0, 50% 54%);
  background: #07130f;
}
.envelope-pocket {
  z-index: 3;
  overflow: hidden;
  border-radius: 2px;
}
.pocket-base,
.pocket-fold,
.pocket-bottom {
  position: absolute;
  inset: 0;
}
.pocket-base {
  clip-path: polygon(0 0, 50% 43%, 100% 0, 100% 100%, 0 100%);
  background-color: #304941;
  background-image: linear-gradient(104deg, #ffffff0a, transparent 45%, #0717112e), url("./envelope-stock.jpg"); /* taste-skill-ignore */
  background-size: 100% 100%, 440px;
  background-position: 0 0, 106px 28px;
}
.pocket-fold { opacity: .5 }
.pocket-fold.left {
  clip-path: polygon(0 0, 50% 43%, 47% 47%, 0 8%);
  background: #132922;
}
.pocket-fold.right {
  clip-path: polygon(100% 0, 50% 43%, 53% 47%, 100% 8%);
  background: #162d25;
}
.pocket-bottom {
  clip-path: polygon(0 100%, 50% 40%, 100% 100%);
  background-color: #344e45;
  background-image: linear-gradient(168deg, #ffffff0d, transparent 52%), url("./envelope-stock.jpg"); /* taste-skill-ignore */
  background-size: 100% 100%, 410px;
  background-position: 0 0, 51px 93px;
  /* The bottom fold sits over the side seams with a soft paper edge. */
  filter: drop-shadow(0 -1px 1px #0a1b1766); /* taste-skill-ignore */
}
.letter-stage .letter-paper {
  position: absolute;
  z-index: 2;
  top: 0;
  left: 16px;
  right: 16px;
  width: auto;
  height: clamp(340px, 54svh, 400px);
  min-height: 0;
  max-height: none;
  margin: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 38px 42px 34px;
  border: 0;
  border-top: 1px solid #fffdf5;
  border-bottom: 2px solid #cfc7b6;
  border-radius: 1px;
  background-color: #f5eee0;
  /* Cold-pressed cotton stock: the tooth has to survive at reading size. */
  background-image: linear-gradient(101deg, #fffdf426, transparent 36%, #6b5c3f1a), url("./cotton-paper.jpg"); /* taste-skill-ignore */
  background-size: 100% 100%, 430px;
  color: #30312d;
  /* The sheet is genuinely lifted above the scene during the exchange. */
  box-shadow: inset 0 0 0 1px #ffffff47, inset 0 0 28px 5px #b5a78924, 0 2px 0 #b9b09d55, 0 20px 42px #02081059; /* taste-skill-ignore */
  font: 19px/2.02 "Noto Serif CJK SC", "Source Han Serif SC", "Songti SC", STSong, SimSun, serif;
  text-align: left;
  text-rendering: optimizeLegibility;
  transform-origin: center bottom;
  scrollbar-color: #a8a190 transparent;
  scrollbar-width: thin;
}
.letter-stage .letter-paper p { margin: 0 }
.letter-stage.from-globe .letter-paper {
  width: min(calc(100% - 26px), 325px, 44svh);
  height: auto;
  aspect-ratio: 188 / 231;
  left: 0;
  right: 0;
  margin-inline: auto;
}
.letter-stage.transferring { overflow: visible }
.letter-stage.sheet-in-scene { visibility: hidden }
.js main.returning-globe:has(#letter:target) #gift .gift-object { opacity: 1 }
.js main.returning-globe #letter:target > a {
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
}
.letter-stage .letter-paper .salutation { margin-bottom: 22px }
.letter-stage .letter-paper .signature {
  margin-top: 28px;
  padding-right: 2px;
  text-align: right;
}
#letter > .button {
  position: relative;
  z-index: 5;
  transition: transform 140ms cubic-bezier(.22, 1, .36, 1), background-color 180ms ease;
}
#letter > .button:active { transform: scale(.97) }
#letter > .button[aria-busy="true"] { cursor: wait }
#letter a:focus-visible {
  outline: 2px solid #f0d39c;
  outline-offset: 5px;
}
.letter-paper:focus-visible {
  outline: 2px solid #526b60;
  outline-offset: -5px;
}
@media (hover: hover) and (pointer: fine) {
  #letter > .button:hover { background: #f1e3c8 }
}
@media (max-width: 700px) {
  #letter { padding-top: 34px }
  .letter-stage {
    height: 420px;
    margin-bottom: 22px;
  }
  .letter-stage .letter-paper {
    left: 13px;
    right: 13px;
    height: clamp(340px, 52svh, 390px);
    padding: 30px 27px 28px;
    font-size: 18px;
    line-height: 1.95;
  }
  .letter-stage .letter-paper .salutation { margin-bottom: 18px }
  .letter-stage .letter-paper .signature { margin-top: 24px }
}
@media (prefers-reduced-motion: reduce) {
  #letter > .button { transition: none }
}
.draft-paper { background-image: url("./writing-paper.jpg") }
