/* The outward face seen in the pocket is also the back of the first fold. */
.envelope .peek,
.leaf-top .leaf-back {
  background: #f8edda url("assets/stationery.webp") center top / 100% auto no-repeat;
  border: 4px double #b8a17d;
  border-radius: 2px;
}
.envelope .peek .leaf { display: none }
.letter { position: relative }

@media (prefers-reduced-motion: no-preference) {
  .opening::before { opacity: 0; transition: opacity 1.4s 2.6s }
  .opening :is(#cover-sound, #cover-tilt) { opacity: 0; transition: opacity 1s }
  .unfolding { touch-action: none; overflow-anchor: none }
  .unfolding .cover {
    position: fixed; z-index: 1; pointer-events: none;
    left: var(--cover-left); top: var(--cover-top); width: var(--cover-width);
    animation: pocket-recedes 1.65s cubic-bezier(.36,.08,.40,1) both;
  }
  @keyframes pocket-recedes {
    from { opacity: 1; transform: translateY(0) }
    to { opacity: 0; transform: translateY(22vh) rotate(1deg) }
  }
  .unfolding .letter { z-index: 2 }
  .unfolding .mount, .unfolding .card-actions { opacity: 0 }
  .unfolding .letter.tilted .mount { transform: none }
  .unfolding .paper > * { visibility: hidden }
  .unfolding.landed .mount { opacity: 1; transition: opacity .13s ease-in }
  .arriving .card-actions { animation: controls-arrive .85s ease-out both }
  @keyframes controls-arrive { from { opacity: 0 } to { opacity: 1 } }

  /* This is the sheet taken out of the pocket. Its middle third carries
     the two leaves all the way to their final position on the paper. */
  .letter > .peek {
    position: absolute; inset: auto; pointer-events: none;
    border: 0; border-radius: 0; background: none; transition: none;
    perspective: 1850px; perspective-origin: 50% 50%;
    transform-origin: 0 0;
  }
  .unfolding.landed .letter > .peek { opacity: 0; transition: opacity .23s .13s ease-out }
  .leaf, .leaf-face, .leaf-back { position: absolute; inset: 0; display: block }
  .leaf {
    transform-style: preserve-3d;
    box-shadow: 0 1px 2px #70583d40, 0 7px 16px -10px #5133298c;
  }
  .leaf-top {
    bottom: 100%; top: -100%; transform-origin: center bottom;
    animation: upper-leaf 2.55s .14s cubic-bezier(.43,.10,.29,1) both;
  }
  .leaf-bottom {
    top: 100%; bottom: -100%; transform-origin: center top;
    animation: lower-leaf 2s .86s cubic-bezier(.47,.09,.27,1) both;
  }
  /* A tiny depth offset keeps the two folded backs in the right paint order. */
  @keyframes upper-leaf {
    from { transform: translateZ(.6px) rotateX(-180deg) }
    to { transform: translateZ(0) rotateX(0) }
  }
  @keyframes lower-leaf {
    from { transform: translateZ(.3px) rotateX(180deg) }
    to { transform: translateZ(0) rotateX(0) }
  }
  .leaf-face, .leaf-back { backface-visibility: hidden; -webkit-backface-visibility: hidden }
  .leaf-face {
    background: #fbf1df url("assets/stationery.webp") center / 100% 300% no-repeat;
    border-inline: 1px solid #bfa67f75;
  }
  .leaf-top .leaf-face { background-position: center top; border-top: 1px solid #bfa67f75 }
  .leaf-bottom .leaf-face { background-position: center bottom; border-bottom: 1px solid #bfa67f75 }
  .leaf-face::before {
    content: ""; position: absolute; inset: 0 7px; border-inline: 1px solid #a3845547;
  }
  .leaf-top .leaf-face::before { top: 7px; border-top: 1px solid #a3845547 }
  .leaf-bottom .leaf-face::before { bottom: 7px; border-bottom: 1px solid #a3845547 }
  .leaf-back {
    transform: rotateX(180deg);
    background: #f4e6cf url("assets/stationery.webp") center bottom / 100% auto no-repeat;
    border: 1px solid #b5a182;
  }
  .leaf-middle .leaf-face::after, .leaf-bottom .leaf-back::after {
    content: ""; position: absolute; inset: 0;
    background: linear-gradient(to bottom, #67482e59, #84623d20 54%, transparent 94%);
    transform-origin: center top;
    animation: fold-shadow 2s .78s ease-in-out both;
  }
  .leaf-bottom .leaf-back::after { animation-delay: .16s }
  @keyframes fold-shadow {
    from { opacity: .2; transform: scaleY(1) }
    40% { opacity: .70; transform: scaleY(.60) }
    to { opacity: 0; transform: scaleY(.04) }
  }
}
