/* The stage and the envelope on it: baked paper faces (art/envelope.py) moved in 3D, the wax seal, the names in foil.
   Every length is a multiple of --u (one hundredth of the stage width), so the piece scales as one object.
   js/opening.js sets [data-state] to sealed or open; the opening is the rules under "open" playing their delays.
   Paint order is the order in the HTML, helped by a few pixels of translateZ. Nothing depends on depth sorting. */

.stage {
  --u: calc(var(--stage-w) / 100);
  position: relative; width: var(--stage-w); height: calc(var(--u) * 72); margin-inline: auto;
  touch-action: pan-y; -webkit-tap-highlight-color: transparent;
  transition: opacity 0.22s linear;
}
.stage.is-swapping { opacity: 0; }

/* Three nested frames. The rig follows the light, the lean is the camera (looking down on the envelope,
   then up at the standing book), the pose is how the envelope happens to lie on the table. */
/* Only the seal takes presses. A frame left hittable would sit in front of whatever leans back behind it. */
.rig, .lean, .pose { position: absolute; inset: 0; transform-style: preserve-3d; transform-origin: 50% calc(var(--u) * 56); pointer-events: none; }
.rig { transform: perspective(calc(var(--u) * 300)) rotateX(calc(var(--ly, 0) * -2.5deg)) rotateY(calc(var(--lx, 0) * 4deg)); }
.lean { transform: rotateX(10deg); }
.pose { transform: rotate(-2.2deg); transform-origin: 50% 50%; }
.stage[data-state="open"] .lean { transform: rotateX(-15deg); transition: transform 1.2s var(--ease-in-out) 0.8s; }
.stage[data-state="open"] .pose { transform: rotate(0deg); transition: transform 0.7s var(--ease-out) 0.2s; }

/* ---------- light that spills out as the flap lifts: flat, behind the rig ---------- */
.spill { position: absolute; left: 50%; top: calc(var(--u) * 30); width: 0; height: 0; pointer-events: none; }
.spill i { position: absolute; left: calc(var(--u) * -80); top: calc(var(--u) * -80); width: calc(var(--u) * 160); height: calc(var(--u) * 160); border-radius: 50%; opacity: 0; }
.spill-halo { background: radial-gradient(closest-side, rgba(255, 228, 172, 0.9), rgba(255, 196, 112, 0.34) 38%, transparent 70%); }
.spill-rays {
  background: repeating-conic-gradient(from 3deg, rgba(255, 232, 184, 0) 0 5deg, rgba(255, 232, 184, 0.5) 7.5deg 9deg, rgba(255, 232, 184, 0) 12deg 19deg, rgba(255, 232, 184, 0.3) 21deg 21.8deg, rgba(255, 232, 184, 0) 24deg 30deg);
  -webkit-mask-image: radial-gradient(closest-side, #000 16%, transparent 76%); mask-image: radial-gradient(closest-side, #000 16%, transparent 76%);
}
.stage[data-state="open"] .spill-halo { animation: spill-halo 2.1s linear 0.42s both; }
.stage[data-state="open"] .spill-rays { animation: spill-rays 2.3s linear 0.46s both; }
@keyframes spill-halo { 0% { opacity: 0; scale: 0.35; } 26% { opacity: 1; scale: 0.95; } 100% { opacity: 0; scale: 1.25; } }
@keyframes spill-rays { 0% { opacity: 0; scale: 0.5; rotate: -10deg; } 24% { opacity: 0.85; } 100% { opacity: 0; scale: 1.3; rotate: 12deg; } }

/* ---------- envelope: a rear half and a fore half, with the card between them ---------- */
.env-rear, .env-fore {
  position: absolute; left: calc(var(--u) * 6); top: calc(var(--u) * 4); width: calc(var(--u) * 88); height: calc(var(--u) * 60);
  transform-style: preserve-3d; transform: translateY(0);
}
.env-fore { pointer-events: none; }
.env-rear img, .env-fore img { position: absolute; max-width: none; will-change: transform; }
/* The envelope falls away as the card rises out of it, right out of the opening's frame (which clips it), and is hidden
   once it has gone. It is never faded: see-through paper shows whatever is behind it, the card and its own lining
   included. art/clearance.py prints where the card and the envelope are through the fall. Only its shadow on the
   table fades, as it leaves the table. The fall is a transform, not the translate property: Safari jumps a delayed
   transition of translate straight to its end. */
.stage[data-state="open"] .env-rear, .stage[data-state="open"] .env-fore { transform: translateY(calc(var(--u) * 190)); transition: transform 0.8s cubic-bezier(0.5, 0, 0.9, 0.6) 0.86s; }
.stage[data-state="open"] .env-rear > *, .stage[data-state="open"] .env-fore > * { visibility: hidden; transition: visibility 0s linear 1.7s; }
.stage[data-state="open"] .env-rear > .env-ground { opacity: 0; transition: opacity 0.3s linear 0.8s, visibility 0s linear 1.7s; }

.env-ground { left: -13.64%; top: -20%; width: 127.27%; height: 140%; transform: translateZ(-3px); translate: calc(var(--lx, 0) * var(--u) * -1.6) calc(var(--ly, 0) * var(--u) * -0.9); }
.env-inside { left: 0; top: 0; width: 100%; height: 66%; }
.env-pocket { inset: 0; width: 100%; height: 100%; transform: translateZ(4px); }

/* The flap is two single-sided pieces. flap-out is the paper outside, shown from closed to edge-on; flap-in is the
   lined inside, shown from edge-on to fully open. Their two curves meet at the same speed, so it reads as one movement.
   The open flap ends past upright, leaning away (198 degrees), and is there before the card shows above the envelope:
   a flap that stopped short of upright would lean towards the viewer and be drawn in front of the card. */
.flap { position: absolute; left: 0; top: 0; width: 100%; height: 60.77%; transform-origin: 50% 0; }
.flap img { inset: 0; width: 100%; height: 100%; }
.flap-out { transform: translateZ(6px) rotateX(0deg); }
.flap-in { visibility: hidden; transform: translateZ(1px) rotateX(90deg); }
.stage[data-state="open"] .env-fore > .flap-out { visibility: hidden; transform: translateZ(6px) rotateX(90deg); transition: transform 0.3s cubic-bezier(0.5, 0, 0.8, 0.6) 0.2s, visibility 0s linear 0.5s; }
.stage[data-state="open"] .env-rear > .flap-in { transform: translateZ(1px) rotateX(198deg); transition: transform 0.5s cubic-bezier(0.15, 0.5, 0.3, 1.04) 0.5s; }
/* The inside shows only from the moment the outside goes edge-on until the envelope has gone. */
.stage[data-state="open"] .env-rear > .flap-in { animation: flap-in-shows 1.2s linear 0.5s; }
@keyframes flap-in-shows { from, to { visibility: visible; } }
.flap-cast { left: -3.41%; top: -4.2%; width: 106.82%; height: 70.77%; transform: translateZ(5px); translate: calc(var(--u) * 0.35 + var(--lx, 0) * var(--u) * -0.4) calc(var(--u) * 0.6); }
.stage[data-state="open"] .env-fore > .flap-cast { opacity: 0; transition: opacity 0.22s linear 0.2s; }

/* Warm light in the mouth of the envelope, over the card and under the pocket. */
.env-glow {
  position: absolute; left: -8%; right: -8%; top: -30%; height: 110%; transform: translateZ(3px); opacity: 0;
  background: radial-gradient(ellipse 46% 44% at 50% 62%, rgba(255, 240, 200, 0.95), rgba(255, 208, 132, 0.5) 38%, transparent 72%);
}
.stage[data-state="open"] .env-fore > .env-glow { animation: mouth-glow 1.5s linear 0.5s both; }
@keyframes mouth-glow { 0% { opacity: 0; } 30% { opacity: 1; } 100% { opacity: 0; } }

/* A band of light that crosses the sealed paper as the light moves. */
.env-sheen {
  position: absolute; inset: 0; border-radius: calc(var(--u) * 0.7); transform: translateZ(7px);
  background: linear-gradient(114deg, transparent 30%, rgba(255, 251, 238, 0.3) 47%, rgba(255, 251, 238, 0.06) 56%, transparent 66%) no-repeat;
  background-size: 240% 100%; background-position: calc(50% + var(--lx, 0) * 50%) 0;
}
.stage[data-state="open"] .env-fore > .env-sheen { opacity: 0; transition: opacity 0.15s linear; }

/* ---------- the names, stamped in foil ---------- */
.foil {
  background-image: linear-gradient(104deg, #7d5a1c 0 16%, #a57c2c 32%, #cfa850 45%, #ead08a 50%, #cfa850 55%, #a57c2c 68%, #7d5a1c 84% 100%);
  background-size: 260% 100%;
  background-position: calc(50% + var(--lx, 0) * 48% + var(--ly, 0) * 12%) 0;
  -webkit-background-clip: text; background-clip: text; color: transparent; -webkit-text-fill-color: transparent;
}
.env-names {
  position: absolute; left: 2%; right: 2%; top: 77.5%; transform: translateZ(5px);
  text-align: center; white-space: nowrap; font-family: var(--text); font-style: italic; font-size: calc(var(--u) * 6.3 * var(--fit, 1)); line-height: 1.4; letter-spacing: 0.005em;
}
.env-names > span { position: relative; display: inline-block; }
.env-names .amp { font-size: 0.78em; padding: 0 0.08em; }
/* Each part is drawn twice: a pressed edge underneath (a highlight below, a shadow above), then the foil. */
.env-names b { position: relative; font-weight: 400; display: block; padding: 0 0.14em; }
.env-names .press { position: absolute; inset: 0; color: #7d5a1c; text-shadow: 0 0.045em 0.02em rgba(255, 255, 255, 0.9), 0 -0.025em 0.03em rgba(70, 44, 10, 0.45); }
/* Before a name is typed its place is blind embossed: pressed into the paper, with no foil. */
.env-names [data-empty] .foil { background-image: none; color: rgba(140, 116, 78, 0.16); -webkit-text-fill-color: currentColor; }
.env-names [data-empty] .press { color: transparent; text-shadow: 0 0.045em 0.02em rgba(255, 255, 255, 0.95), 0 -0.03em 0.035em rgba(82, 58, 28, 0.4); }
.env-names .is-writing::after {
  content: ""; position: absolute; right: 0.06em; top: 22%; width: max(1.5px, 0.04em); height: 56%; background: var(--foil); rotate: 12deg;
  animation: caret 1.05s steps(1) infinite;
}
@keyframes caret { 50% { opacity: 0; } }
.env-names.is-struck .foil { animation: strike 0.5s ease-out; }
@keyframes strike { from { background-position: 110% 0; } }

/* ---------- wax seal ---------- */
.seal {
  position: absolute; left: 50%; top: 60%; width: calc(var(--u) * 19); height: calc(var(--u) * 19);
  margin: calc(var(--u) * -9.5) 0 0 calc(var(--u) * -9.5); padding: 0;
  border: 0; border-radius: 50%; background: none; cursor: pointer; pointer-events: auto;
  transform: translateZ(8px); transform-style: preserve-3d;
  transition: scale 0.25s var(--ease-spring);
}
.seal:hover { scale: 1.035; }
.seal:active, .seal.is-pressed { scale: 0.94; transition: scale 0.09s ease-out; }
.seal::before {
  content: ""; position: absolute; inset: 5% -5% -9% 5%; border-radius: 50%;
  background: radial-gradient(closest-side, rgba(46, 14, 8, 0.58), rgba(46, 14, 8, 0.26) 62%, transparent);
  translate: calc(var(--lx, 0) * -4%) calc(var(--ly, 0) * -3%);
}
.seal-whole, .seal-half { position: absolute; inset: 0; }
.seal img { inset: 0; width: 100%; height: 100%; }
/* The initials are gilded: foil pressed into the wax, with a dark edge below where the stamp bit. */
.seal-mono {
  position: absolute; left: 0; right: 0; top: 50%; translate: -1% -54%; text-align: center; white-space: nowrap;
  font-family: var(--text); font-style: italic; font-size: calc(var(--u) * 4.7); line-height: 1.2; letter-spacing: -0.01em;
}
.seal-mono b { font-weight: 400; display: block; padding: 0 0.1em; }
.seal-mono .press { position: absolute; inset: 0; color: transparent; text-shadow: 0.03em 0.06em 0.05em rgba(26, 0, 4, 0.85), -0.02em -0.03em 0.03em rgba(255, 190, 170, 0.35); }
.seal-mono .foil { position: relative; background-image: linear-gradient(104deg, #b08a3c 0 20%, #d9b663 36%, #fff0c4 50%, #d9b663 64%, #b08a3c 80% 100%); }
.stage[data-ready="false"] .seal-mono { font-size: calc(var(--u) * 8.6); }
/* A soft highlight that slides over the wax with the light. */
.seal-shine {
  position: absolute; left: 20%; top: 14%; width: 46%; height: 42%; border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 232, 224, 0.42), rgba(255, 220, 210, 0.1) 58%, transparent);
  translate: calc(var(--lx, 0) * 20%) calc(var(--ly, 0) * 16%);
}
.seal.is-stamped .seal-whole { animation: stamp 0.5s var(--ease-spring); }
@keyframes stamp { 0% { scale: 1; } 30% { scale: 0.86; } 100% { scale: 1; } }
/* Breaking: the whole seal gives way to two halves along a crack, which spring apart and fall. */
.seal-half { opacity: 0; }
.seal-half-a { clip-path: polygon(0 0, 47% 0, 54% 21%, 45% 40%, 56% 57%, 46% 77%, 52% 100%, 0 100%); transform-origin: 30% 60%; }
.seal-half-b { clip-path: polygon(47% 0, 100% 0, 100% 100%, 52% 100%, 46% 77%, 56% 57%, 45% 40%, 54% 21%); transform-origin: 70% 60%; }
.stage[data-state="open"] .seal { pointer-events: none; }
.stage[data-state="open"] .seal::before { opacity: 0; transition: opacity 0.2s linear 0.3s; }
.stage[data-state="open"] .seal-whole { opacity: 0; transition: opacity 0s linear 0.09s; }
.stage[data-state="open"] .seal-half-a { animation: break-a 0.72s 0.09s both; }
.stage[data-state="open"] .seal-half-b { animation: break-b 0.72s 0.09s both; }
@keyframes break-a {
  0% { opacity: 1; translate: 0 0; rotate: 0deg; animation-timing-function: cubic-bezier(0.1, 0.8, 0.2, 1); }
  28% { opacity: 1; translate: calc(var(--u) * -3.4) calc(var(--u) * 0.5); rotate: -10deg; animation-timing-function: cubic-bezier(0.5, 0, 0.9, 0.5); }
  100% { opacity: 0; translate: calc(var(--u) * -6) calc(var(--u) * 15); rotate: -30deg; }
}
@keyframes break-b {
  0% { opacity: 1; translate: 0 0; rotate: 0deg; animation-timing-function: cubic-bezier(0.1, 0.8, 0.2, 1); }
  28% { opacity: 1; translate: calc(var(--u) * 3.8) calc(var(--u) * 0.9); rotate: 13deg; animation-timing-function: cubic-bezier(0.5, 0, 0.9, 0.5); }
  100% { opacity: 0; translate: calc(var(--u) * 7) calc(var(--u) * 17); rotate: 38deg; }
}
/* A flash at the crack, and crumbs of wax. */
.crack { position: absolute; left: 50%; top: 60%; width: 0; height: 0; transform: translateZ(9px); pointer-events: none; }
.crack::before {
  content: ""; position: absolute; left: calc(var(--u) * -11); top: calc(var(--u) * -11); width: calc(var(--u) * 22); height: calc(var(--u) * 22); border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 244, 214, 0.95), rgba(255, 214, 150, 0.4) 45%, transparent 70%); opacity: 0;
}
.stage[data-state="open"] .crack::before { animation: flash 0.42s ease-out 0.08s both; }
@keyframes flash { 0% { opacity: 0; scale: 0.3; } 25% { opacity: 1; } 100% { opacity: 0; scale: 1.7; } }
.crack i {
  position: absolute; width: calc(var(--u) * var(--s)); height: calc(var(--u) * var(--s) * 0.8); margin: calc(var(--u) * -0.6);
  background: #7e1c28; border-radius: 45% 55% 50% 40%; opacity: 0;
}
.stage[data-state="open"] .crack i { animation: crumb 0.8s cubic-bezier(0.2, 0.7, 0.4, 1) 0.09s both; }
@keyframes crumb {
  0% { opacity: 1; translate: 0 0; rotate: 0deg; }
  100% { opacity: 0; translate: calc(var(--u) * var(--dx)) calc(var(--u) * var(--dy)); rotate: var(--r); }
}
/* The envelope takes the press: a small dip as the seal gives. */
.stage[data-state="open"] .pose { animation: jolt 0.3s ease-out 0.07s; }
@keyframes jolt { 30% { transform: rotate(-2.2deg) scale(0.988); } }

/* A state reached with no movement (a prefilled link, resealing, reduced motion): nothing transitions and the
   one-off animations of the opening are skipped. The class stays on until the next opening that moves. */
.stage.is-settled, .stage.is-settled *, .stage.is-settled *::before, .stage.is-settled *::after { transition: none !important; }
.stage.is-settled :is(.spill i, .env-glow, .flap-in, .seal-half, .crack i, .pose, .book), .stage.is-settled .crack::before { animation: none !important; }
