#letter {
  timeline-scope: --unseal, --slide, --settle, --stamp;
}

#letter-unseal { view-timeline: --unseal; }
#letter-slide  { view-timeline: --slide; }
#letter-settle { view-timeline: --settle; }
#letter-stamp  { view-timeline: --stamp; }

#letter-scene {
  --height: calc(var(--topline) + var(--env-h) * 1.1);

  position: sticky;
  top: max(0px, calc((100vh - var(--height)) / 4));
  height: var(--height);
  perspective: calc(var(--let-w) * 4);
  transform-style: preserve-3d;

  --let-w: min(60vw, 60vh);
  --let-h: calc(var(--let-w) * 1.3);
  --panel-h: calc(var(--let-h) / 3);
  --env-margin: calc(var(--let-h) / 50);
  --env-w: calc(var(--let-w) + 2 * var(--env-margin));
  --env-h: calc(var(--panel-h) + 2 * var(--env-margin));
  --flap-h: calc(var(--env-h) * 0.3);
  --slide-dist: calc(var(--env-h));
  --topline: calc(var(--panel-h) + var(--slide-dist));
}

#envelope-back {
  position: absolute;
  width: var(--env-w);
  height: var(--env-h);
  left: calc(50% - var(--env-w) / 2);
  top: var(--topline);
  background: oklch(92% 0.005 230);
  transform: translateZ(-1px);

  background-image: url(slay.png);
  background-size: 60%;
  background-position: center -15%;
  background-repeat: no-repeat;
  image-rendering: pixelated;
}

#envelope-front {
  position: absolute;
  width: var(--env-w);
  height: var(--env-h);
  left: calc(50% - var(--env-w) / 2);
  top: var(--topline);
  background: oklch(96% 0.01 70);
  clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%, 50% 30%);
  transform: translateZ(1px);
}

#flap {
  position: absolute;
  width: var(--env-w);
  height: var(--flap-h);
  left: calc(50% - var(--env-w) / 2);
  top: var(--topline);
  transform-origin: top center;
  transform-style: preserve-3d;
  animation: flap-open ease both;
  &, .shade::after {
    animation-timeline: --unseal;
    animation-range: cover contain;
  }
}

#flap .face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
}

#flap .front {
  background: oklch(92% 0.01 70);
  clip-path: polygon(0% 0%, 100% 0%, 50% 100%);
}

#flap .back  {
  background: oklch(90% 0.005 230);
  transform: rotateX(180deg);
  clip-path: polygon(0% 100%, 100% 100%, 50% 0%);

  background-image: url(slay.png);
  background-size: 60%;
  background-position: center calc(var(--flap-h) * .65);
  background-repeat: no-repeat;
  image-rendering: pixelated;
}

@keyframes flap-open {
  from { transform: translateZ(2px) rotateX(0deg); }
  to   { transform: translateZ(-1px) translateY(1px) rotateX(180deg); }
}

#letter-page {
  position: absolute;
  width: var(--let-w);
  height: var(--let-h);
  left: calc(50% - var(--let-w) / 2);
  top: calc(var(--topline) - var(--panel-h) + var(--env-margin));
  transform-style: preserve-3d;
  animation: page-slide ease-in both, page-settle ease-out forwards;
  animation-timeline: --slide, --settle;
  animation-range: cover contain, contain;
}

@keyframes page-slide {
  from { translate: 0 0; }
  to   { translate: 0 calc(-1 * var(--slide-dist)); }
}

@keyframes page-settle {
  0%   { translate: 0px  calc(-1 * var(--slide-dist));       rotate: 0deg; }
  40%  { translate: calc(var(--slide-dist) * .04)  calc(-1.1 * var(--slide-dist)); rotate: -1deg; }
  100% { translate: calc(var(--slide-dist) * .05) calc(-.7 * var(--slide-dist)) 2px; rotate: -3deg; }
}

.panel {
  width: var(--let-w);
  height: var(--panel-h);
  transform-style: preserve-3d;
}

.panel .face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
}

.panel .front {
  background-image: url(loan.jpg);
  background-size: 100%;
  background-position-x: 0;
}

#panel-top .front { background-position-y: 0%; }
#panel-mid .front { background-position-y: center; }
#panel-bot .front { background-position-y: bottom; }

.panel .back {
  background: oklch(94% 0.005 70);
  transform: rotateX(180deg);
}

#panel-top {
  transform-origin: bottom center;
  animation: unfold-top ease both;
  &, .shade::after {
    animation-timeline: --settle;
    animation-range: contain 0% contain 80%;
  }
}

#panel-bot {
  transform-origin: top center;
  animation: unfold-bot ease both;
  &, .shade::after {
    animation-timeline: --settle;
    animation-range: contain 45% contain 105%;
  }
}

@keyframes unfold-top {
  from { transform: rotateX(-180deg); }
  to   { transform: rotateX(-7deg) translateY(1px); }
}

@keyframes unfold-bot {
  from { transform: rotateX(180deg); }
  to   { transform: rotateX(5deg) translateY(-1px); }
}

/* .shade::after { */
/*   content: ''; */
/*   position: absolute; */
/*   inset: 0; */
/*   background: linear-gradient(to bottom, #000f, #000d); */
/*   pointer-events: none; */
/*   animation: shade-fold ease both; */
/* } */

@keyframes shade-fold {
  0% { opacity: 0; }
  50% { opacity: 0.1; }
  100% { opacity: .015; }
}

#stamp {
  --stamp-w: calc(var(--panel-h) * 1.2);
  position: absolute;
  width: var(--stamp-w);
  bottom: calc(var(--panel-h) * 0.3);
  left: calc(50% - var(--stamp-w) * .6);
  transform: translateZ(10px);
  animation: stamp-y ease-in both;
  animation-timeline: --stamp;
  animation-range: cover 20% contain;

  img {
    width: 100%;
    animation: stamp-x ease-out both, stamp-rotate ease both;
    animation-timeline: --stamp, --stamp;
    animation-range: cover 20% contain;
  }
}

@keyframes stamp-y {
  0%   { translate: 0 calc(var(--height) * -.4) var(--panel-h); }
  35%  { translate: 0 0 21px; }
  65%  { translate: 0 0 21px; }
  100% { translate: 0 calc(var(--height) * -.2) var(--panel-h); }
}

@keyframes stamp-x {
  0%   { translate: 70vw 0; }
  30%  { translate: 0 0; }
  55%  { translate: 0 0; }
  100% { translate: calc(var(--stamp-w) * -2) 0; }
}

@keyframes stamp-rotate {
  0%   { rotate: 20deg; }
  35%  { rotate: -3deg; }
  50%  { rotate: -6deg; }
  100% { rotate: -15deg; }
}

#paid {
  --width: calc(var(--panel-h) * .9);
  position: absolute;
  width: var(--width);
  bottom: calc(var(--panel-h) * 0.3);
  left: calc(50% - var(--width) * .6);
  transform: translateZ(20px);
  rotate: -3deg;
  animation: paid-appear ease-out both;
  animation-timeline: --stamp;
  animation-range: cover 20% contain;
}

@keyframes paid-appear {
  0%   { opacity: 0; }
  40%  { opacity: 0; }
  60%  { opacity: .85; }
  100% { opacity: .85; }
}
