#letter-view {
  width: min(100% - 36px, 880px);
  margin: 0 auto;
  padding: clamp(18px, 3.5vw, 38px) 0 56px;
  overflow-anchor: none;
  overflow-x: clip;
}

#letter-view:focus { outline: none; }

.letter-back {
  padding: 8px 0;
  color: var(--muted);
  font-size: 12px;
}

.letter-back:hover { color: var(--paper); }

.letter-experience {
  --letter-grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='paper'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.83' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' opacity='.12' filter='url(%23paper)'/%3E%3C/svg%3E");
  --letter-sheet: var(--letter-grain), radial-gradient(ellipse at 28% 9%, #f7edd6, #eee0c3 78%, #e5d4b5);
  position: relative;
  scroll-margin-top: 20px;
}

.letter-envelope-stage {
  position: relative;
  height: clamp(310px, 66vw, 516px);
  background: radial-gradient(ellipse at 50% 61%, #49543a38, #1c272000 68%);
}

.letter-envelope {
  --envelope-grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='176' height='176'%3E%3Cfilter id='fiber'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.32 .78' numOctaves='3' seed='23' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' opacity='.28' filter='url(%23fiber)'/%3E%3C/svg%3E");
  position: absolute;
  width: min(88%, 508px);
  aspect-ratio: 1.68;
  top: 52%;
  left: 50%;
  translate: -50% -20%;
  isolation: isolate;
  perspective: 950px;
}

.letter-envelope-back,
.letter-lining,
.letter-pocket,
.letter-pocket-surface {
  position: absolute;
  inset: 0;
  border-radius: 3px;
}

.letter-envelope-back {
  background: var(--envelope-grain), linear-gradient(150deg, #eddab9, #d0b38a);
  background-blend-mode: soft-light, normal;
  border: 1px solid #e7d0aa;
  box-shadow: inset 0 1px #fff0d884, inset -1px -1px #a7825740, 0 22px 40px #10170eb3, 1px 6px 13px #0e180fa1;
}

.letter-lining {
  inset: 2px 3px 0;
  background: var(--envelope-grain), linear-gradient(181deg, #765d49 0%, #977b5d 7%, #b59a73 22%, #d7bd99 47%);
  background-blend-mode: soft-light, normal;
  box-shadow: inset 0 2px 1px #4f392b53, inset 1px 5px 11px #59402d4c;
}

.letter-pocket {
  z-index: 4;
  filter: drop-shadow(0 -1.6px 1.8px #5c422c50);
  pointer-events: none;
}

.letter-pocket-surface {
  --wing-mouth-y: 2%;
  --wing-edge-thickness: 1.4px;
}
.letter-pocket-surface > span {
  position: absolute;
  inset: 0;
  border-radius: inherit;
}

/* The only front surfaces are the two wings and the bottom fold. */
.letter-pocket-surface > span:not(:last-child) {
  clip-path: polygon(
    var(--wing-outside) var(--wing-mouth-y),
    var(--wing-tip-x) var(--wing-tip-y),
    var(--wing-outside) 100%
  );
  pointer-events: auto;
}

.letter-pocket-surface > span:first-child {
  --wing-outside: 0%;
  --wing-tip-x: 65%;
  --wing-tip-y: 54%;
  background: var(--envelope-grain), radial-gradient(ellipse at 7% 7%, #fff0d24d, transparent 65%), linear-gradient(132deg, #ead7b8, #dfc6a2 47%, #cbb08b 89%);
  background-blend-mode: soft-light, normal, normal;
}
.letter-pocket-surface > span:nth-child(2) {
  --wing-outside: 100%;
  --wing-tip-x: 40%;
  --wing-tip-y: 47%;
  background: var(--envelope-grain), radial-gradient(ellipse at 100% 2%, #f3dfbe36, transparent 64%), linear-gradient(218deg, #e5ceab, #d9bd98 48%, #c5a980 90%);
  background-blend-mode: soft-light, normal, normal;
}
.letter-pocket-surface > span:last-child {
  filter: drop-shadow(-2px -1.5px 2.5px #6b4a3240) drop-shadow(3px -1px 2px #59402b37);
}

.letter-pocket-surface > span::after,
.letter-pocket-surface > span:last-child::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.letter-pocket-surface > span:not(:last-child)::after {
  background: linear-gradient(105deg, #fff5dc94, #fff0ce69 53%, #f3d3a442);
  clip-path: polygon(
    var(--wing-outside) var(--wing-mouth-y),
    var(--wing-tip-x) var(--wing-tip-y),
    var(--wing-tip-x) calc(var(--wing-tip-y) + var(--wing-edge-thickness)),
    var(--wing-outside) calc(var(--wing-mouth-y) + var(--wing-edge-thickness))
  );
}
.letter-pocket-surface > span:last-child::before {
  background: var(--envelope-grain), radial-gradient(ellipse at 23% 35%, #fff2d04f, transparent 74%), linear-gradient(148deg, #eedcba 26%, #e6cba2 72%, #d7b98d);
  background-blend-mode: soft-light, normal, normal;
  border-radius: inherit;
  clip-path: polygon(0 100%, 49.7% 30%, 50.3% 30%, 100% 100%);
  pointer-events: auto;
}
.letter-pocket-surface > span:last-child::after {
  background: linear-gradient(98deg, #fff3d55c, #f6e1bd24 67%, #96735233);
  clip-path: polygon(0 100%, 49.7% 30%, 50.3% 30%, 100% 100%, 99.3% 100%, 50% calc(30% + 1.8px), .7% 100%);
}

.letter-flap {
  position: absolute;
  inset: 0 0 auto;
  height: 55%;
  z-index: 5;
  transform-origin: 50% 1px;
  transform-style: preserve-3d;
  will-change: transform;
}
.letter-flap span {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
}
.letter-flap span::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 3px 3px 20% 20%;
  clip-path: polygon(0 0, 100% 0, 100% 4%, 52% 95%, 50% 97%, 48% 95%, 0 4%);
}
.letter-flap span:first-child {
  filter: drop-shadow(0 3px 2.5px #62452e50);
}
.letter-flap span:first-child::before {
  background: var(--envelope-grain), radial-gradient(ellipse at 20% 3%, #fff1d377, transparent 74%), linear-gradient(165deg, #f2e1c4 5%, #ead2ae 56%, #d1b08a);
  background-blend-mode: soft-light, normal, normal;
  box-shadow: inset 0 1px #f9ebcf;
}
.letter-flap span:last-child {
  transform: rotateY(180deg);
}
.letter-flap span:last-child::before {
  background: var(--envelope-grain), radial-gradient(ellipse at 77% 66%, #f8e4ba52, transparent 58%), linear-gradient(0deg, #ecd6b3 20%, #dcc19d 65%, #baa07b 100%);
  background-blend-mode: soft-light, normal, normal;
  box-shadow: inset 0 3px 5px #72563a35;
}
.letter-envelope.is-open .letter-flap { z-index: 2; }

.letter-wax {
  position: absolute;
  z-index: 6;
  left: 50%;
  top: 51%;
  translate: -50% -50%;
  display: grid;
  place-items: center;
  width: clamp(50px, 14%, 72px);
  aspect-ratio: 1;
  padding: 0;
  color: #f1bd91;
  background: radial-gradient(ellipse at 29% 15%, #e4a57129, transparent 49%), radial-gradient(ellipse at 70% 82%, #42191d39, transparent 51%), radial-gradient(ellipse at 39% 22%, #a35744, #773327 61%, #4c211e 86%);
  border: 1px solid #8d4838;
  border-radius: 49% 46% 51% 45% / 43% 50% 45% 53%;
  box-shadow: 0 1px 1px #56291f82, 1px 3px 5px #59342379, inset 0 2px 3px #d18d624b, inset -1px -3px 4px #411b1b91;
}
.letter-wax::before {
  content: '';
  position: absolute;
  inset: 11%;
  border: 1px solid #bb7152a6;
  border-right-color: #b3674d87;
  border-bottom-color: #7f4033b8;
  border-radius: 48% 50% 46% 51%;
  box-shadow: inset 0 -1px 4px #59251f98, 0 1px 2px #49211d9c;
}
.letter-wax, .letter-folded { -webkit-tap-highlight-color: transparent; }
.letter-wax span {
  position: relative;
  font: bold clamp(14px, 3.1vw, 20px) Georgia, serif;
  letter-spacing: .08em;
  text-shadow: 0 -1px 1px #4a211c, 0 1px 1px #5e2823;
}
.letter-wax:disabled, .letter-folded:disabled { opacity: 1; cursor: default; }
.letter-wax:focus-visible { outline: 2px solid #f0d4a7; outline-offset: 5px; }

.letter-folded {
  position: absolute;
  z-index: 3;
  inset: 18% 7% auto;
  height: 62%;
  padding: clamp(6px, 1vw, 10px) clamp(10px, 2.8vw, 24px);
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: center;
  text-align: center;
  color: #4c3a2b;
  background: var(--letter-sheet);
  border: 1px solid #f7e8c9;
  border-radius: 2px;
  box-shadow: 1px 3px 9px #533d2e6b, 0 2px 0 #bca585, 0 4px 0 #e4d0ae;
}
.letter-folded:disabled { pointer-events: none; }
.letter-folded:not(:disabled):hover { filter: brightness(1.035); }
.letter-folded:focus-visible { outline: 2px solid #c19d64; outline-offset: 2px; }
.letter-folded span { font: clamp(12px, 2.7vw, 20px)/1.5 var(--serif); }
.letter-folded small, .letter-experience .final-letter small {
  font: 10px/1.45 var(--serif);
  letter-spacing: .09em;
  color: #78614b;
}

.letter-prompt {
  margin: clamp(14px, 3vw, 25px) 0 0;
  color: #c4c8ac;
  text-align: center;
  font: 16px/1.6 var(--serif);
  letter-spacing: .12em;
}
[data-letter-phase='extracting'] .letter-prompt,
[data-letter-phase='unfolding'] .letter-prompt { visibility: hidden; }
[data-letter-phase='unfolding'] .letter-theatre { position: absolute; inset: 0 0 auto; pointer-events: none; }

.letter-experience .final-letter {
  --letter-inset: clamp(22px, 6.2vw, 64px);
  width: min(100%, 658px);
  max-width: none;
  margin: 16px auto 0;
  padding: 0;
  color: #47382c;
  background: none;
  box-shadow: none;
  transform: none;
  transform-origin: left top;
  perspective: 1250px;
}
.letter-experience .final-letter h2 {
  margin: 15px 0 21px;
  font: 400 clamp(18px, 4.3vw, 29px)/1.55 var(--serif);
  color: #3e3428;
}
.letter-experience .final-letter h2:focus { outline: none; }
.letter-experience .final-letter p {
  margin: 0 0 1em;
  font: clamp(16px, 3.4vw, 19px)/2.04 var(--serif);
  overflow-wrap: anywhere;
}
.letter-experience .final-letter .signature {
  margin-top: 25px;
  color: #795741;
  font: clamp(16px, 3.8vw, 20px)/1.8 var(--serif);
}
.letter-leaf { padding: 14px var(--letter-inset) 3px; }
.letter-leaf-first { padding-top: clamp(30px, 7.2vw, 59px); }
.letter-leaf-last { padding-bottom: 26px; }
.letter-unfold, .letter-leaf-last { transform-origin: center top; }
.letter-unfold { transform-style: preserve-3d; }

[data-letter-phase='unfolding'] .letter-leaf {
  background: var(--letter-sheet);
  box-shadow: inset 0 1px #ffefd588, 1px 3px 11px #43321e59;
  backface-visibility: hidden;
}
[data-letter-phase='unfolding'] .letter-unfold { transform: rotateX(172deg); }
[data-letter-phase='unfolding'] .letter-leaf-last { transform: rotateX(-172deg); }
[data-letter-phase='unfolding'] .letter-first-paragraph { opacity: 0; }

[data-letter-phase='opened'] .final-letter {
  background: var(--letter-sheet);
  border-radius: 2px;
  box-shadow: 0 18px 45px #0d20158f, inset 0 0 0 1px #f4e6c8;
  perspective: none;
}
[data-letter-phase='opened'] .letter-leaf + .letter-leaf {
  background: linear-gradient(#92744f13, #fff5e329 1px, transparent 11px);
}
.letter-replay { padding: 0 var(--letter-inset) 33px; text-align: right; }
.letter-replay button { color: #745a40; font: 13px/1.6 var(--serif); padding: 10px 0 5px; text-decoration: underline; text-underline-offset: 4px; }

@media (max-width: 500px) {
  #letter-view { width: calc(100% - 28px); }
  .letter-envelope { aspect-ratio: 1.57; }
  .letter-folded small { font-size: 8.5px; }
  .letter-folded { gap: 2px; }
}
