.ink-field { position: relative; overflow: clip; }
.ink-trace {
  position: absolute;
  box-sizing: border-box;
  white-space: pre-wrap;
  pointer-events: none;
  user-select: none;
  color: transparent;
  text-shadow: none;
}
#composer[data-sealing] { overflow: hidden; }
#letter-dispatch:not([hidden]) {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: url('./assets/stationery-grain.png'),
    radial-gradient(ellipse at 47% 38%, #fff8e8, #eae0cf 76%, #d8cbb7);
}
.dispatch-scene {
  position: relative;
  width: min(76%, 21rem, 61vh);
  width: min(76%, 21rem, 61dvh);
  aspect-ratio: 1 / 1.12;
  --sealing-time: 2.1s;
  isolation: isolate;
  animation: center-packet var(--sealing-time) both;
}
.dispatch-pocket, .dispatch-liner, .dispatch-flap, .dispatch-wax {
  position: absolute;
  inset: auto 5% 4%;
  aspect-ratio: 1184 / 966;
  background: url('./assets/open-envelope.webp') center / 100% 100% no-repeat;
}
.dispatch-liner {
  clip-path: inset(43% 0 0);
  animation: dispatch-arrive var(--sealing-time) both;
}
.dispatch-flap {
  transform-origin: center 43%;
  clip-path: polygon(0 0,100% 0,100% 43.5%,0 43.5%);
  animation: fold-flap var(--sealing-time) both;
}
.dispatch-pocket {
  clip-path: polygon(0 43%, 5% 45%, 49.6% 77.1%, 50.4% 77.1%, 95% 45%, 100% 43%, 100% 100%, 0 100%);
  animation: dispatch-arrive var(--sealing-time) both;
}
.dispatch-track { position: absolute; inset: 0 0 4%; overflow: hidden; }
.dispatch-sheet {
  position: absolute;
  inset: 4% 15% auto;
  height: 54%;
  perspective: 44rem;
  animation: slide-letter var(--sealing-time) both;
}
.dispatch-bottom, .dispatch-crease {
  position: absolute;
  left: 0; right: 0;
  height: 50%;
  border: 1px solid #cebea2;
  background: repeating-linear-gradient(transparent 0 1.25em, #bfae8f55 1.25em calc(1.25em + 1px)),
    linear-gradient(110deg, #fff9e8, #f7eed9);
  box-shadow: 0 2px 5px #59452424;
}
.dispatch-bottom { bottom: 0; border-top: 0; }
.dispatch-crease {
  top: 0;
  transform-origin: center bottom;
  transform-style: preserve-3d;
  animation: fold-letter var(--sealing-time) both;
}
.dispatch-handwriting {
  position: absolute;
  inset: 10% 7% 5%;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  backface-visibility: hidden;
  font: 400 clamp(1.08rem, 3.7vw, 1.38rem)/1.24 var(--letter-pen);
  color: #38352e;
}
.dispatch-crease::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(2deg, #d9cbae, #fbf2dc 38%, #ede1c7);
  transform: rotateX(180deg);
  backface-visibility: hidden;
}
.dispatch-wax {
  background: none;
  animation: press-seal var(--sealing-time) both;
  transform-origin: 50% 80%;
}
.dispatch-wax::after {
  content: "";
  position: absolute;
  width: 14%;
  aspect-ratio: 1;
  left: 43%; top: 72%;
  background: url('./assets/letter-packets.webp') 13.9% 70% / 1263% 279% no-repeat;
  clip-path: polygon(39% 3%,58% 1%,76% 7%,88% 19%,95% 40%,99% 62%,95% 79%,80% 92%,64% 98%,38% 98%,18% 91%,7% 78%,2% 59%,4% 38%,11% 22%,23% 9%);
}
@keyframes dispatch-arrive {
  0%, 12% {opacity: 0; transform: translateY(8%);}
  34%, 100% {opacity: 1; transform: none;}
}
@keyframes center-packet {
  0%, 63% { transform: none; }
  91%, 100% { transform: translateY(-21%); }
}
@keyframes fold-letter {
  0%, 15% {transform: rotateX(0);}
  41%, 100% {transform: rotateX(-179.9deg);}
}
@keyframes slide-letter {
  0% {transform: translateY(2%) rotate(-2deg); opacity: 0;}
  8%, 38% {transform: translateY(0) rotate(-2deg); opacity: 1;}
  46% {transform: translateY(7%) rotate(1deg);}
  69%, 100% {transform: translateY(100%) rotate(0) scale(.94);}
}
@keyframes fold-flap {
  0%, 12% {opacity: 0; transform: translateY(8%);}
  34%, 65% {opacity: 1; z-index: 0; transform: none;}
  66% {z-index: 2; transform: rotateX(0);}
  85%, 100% {opacity: 1; z-index: 2; transform: rotateX(180deg);}
}
@keyframes press-seal {
  0%, 82% {opacity: 0; z-index: 3; transform: scale(1.22);}
  91% {opacity: 1; z-index: 3; transform: scale(.96);}
  97%, 100% {opacity: 1; z-index: 3; transform: none;}
}
@media (prefers-reduced-motion: reduce) {
  #letter-dispatch *, .ink-trace { animation: none !important; }
}
:root[data-motion-paused] #letter-dispatch * { animation: none; }
