/* The card inside the envelope, which is a folded pop-up book, and the paper world it opens into.
   Lengths are multiples of --u, as in css/envelope.css.

   The card and its cover live in the envelope's 3D scene. The world's layers do not: each layer is projected on its
   own, with the same camera, and they are painted in the order they are written. Browsers sort the planes of a shared
   3D scene by depth and disagree about how (Chrome hid whatever stood on the ground, Safari hid the ground), so the
   world gives them nothing to sort. */

.book {
  position: absolute; left: calc(var(--u) * 10); top: calc(var(--u) * 7.5); width: calc(var(--u) * 80); height: calc(var(--u) * 53);
  transform-style: preserve-3d; pointer-events: none; translate: 0 0 2px;
}
/* The card leaps clear of the envelope, then lowers into place as its cover falls open. */
.stage[data-state="open"] .book { translate: 0 calc(var(--u) * -4.5) 2px; scale: var(--scene-scale); transform-origin: 50% 100%; animation: card-rise 1.5s 0.74s backwards; }
@keyframes card-rise {
  0% { translate: 0 0 2px; scale: 1; animation-timing-function: cubic-bezier(0.3, 0, 0.2, 1); }
  52% { translate: 0 calc(var(--u) * -27) 2px; scale: 1; animation-timing-function: cubic-bezier(0.5, 0, 0.3, 1); }
  100% { translate: 0 calc(var(--u) * -4.5) 2px; scale: var(--scene-scale); }
}

.book-shadow {
  position: absolute; left: -10%; right: -10%; top: 97%; height: 50%;
  background: radial-gradient(closest-side, rgba(0, 0, 0, 0.62), rgba(0, 0, 0, 0.24) 55%, transparent);
  transform: translateZ(-4px); opacity: 0;
}
.stage[data-state="open"] .book-shadow { opacity: 1; transition: opacity 0.9s linear 2s; }

.paper-face {
  background-color: var(--paper);
  background-image: var(--fibre), linear-gradient(160deg, #fffdf6, var(--paper) 45%, #efe3cb);
  background-size: calc(var(--u) * 46), cover; background-blend-mode: multiply, normal;
}
.book-back { position: absolute; inset: 0; border-radius: calc(var(--u) * 0.6); overflow: hidden; background-color: var(--s-sky2); }
.book-back img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* ---------- the cover: the card's front, which falls forward and becomes the ground ---------- */
.cover {
  position: absolute; inset: 0; transform-origin: 50% 100%; transform-style: preserve-3d;
  transform: translateZ(1px) rotateX(0deg);
}
.stage[data-state="open"] .cover { transform: translateZ(0.5px) rotateX(-90deg); transition: transform 0.8s var(--ease-in-out) 1.5s; }
/* Each face hides when it turns its back, and the two also swap by the clock as the cover passes edge-on,
   so a browser that draws back faces still never shows the wrong one. */
.cover-out, .cover-in { position: absolute; inset: 0; border-radius: calc(var(--u) * 0.6); -webkit-backface-visibility: hidden; backface-visibility: hidden; }
.cover-in { opacity: 0; }
.stage[data-state="open"] .cover-out { opacity: 0; transition: opacity 0s linear 2.06s; }
.stage[data-state="open"] .cover-in { opacity: 1; transition: opacity 0s linear 2.06s; }
.cover-out { display: grid; place-content: center; justify-items: center; gap: calc(var(--u) * 1.2); text-align: center; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.5), inset 0 0 calc(var(--u) * 3) rgba(120, 90, 40, 0.14); }
.cover-out::before, .cover-out::after {
  content: ""; position: absolute; border: calc(var(--u) * 0.16) solid var(--foil); border-radius: calc(var(--u) * 0.3); pointer-events: none;
}
.cover-out::before { inset: calc(var(--u) * 2); opacity: 0.85; }
.cover-out::after { inset: calc(var(--u) * 2.8); opacity: 0.45; }
.cover-in {
  transform: rotateX(180deg);
  background-color: var(--s-base);
  background-image:
    linear-gradient(180deg, rgba(0, 0, 0, 0.5), transparent 9%),
    linear-gradient(180deg, transparent 10%, rgba(0, 0, 0, 0.24) 14%, transparent 22%),
    linear-gradient(180deg, transparent 23%, rgba(0, 0, 0, 0.24) 27%, transparent 37%),
    linear-gradient(180deg, transparent 40%, rgba(0, 0, 0, 0.24) 44%, transparent 55%),
    linear-gradient(180deg, transparent 57%, rgba(0, 0, 0, 0.24) 61%, transparent 71%),
    linear-gradient(180deg, transparent 74%, rgba(0, 0, 0, 0.3) 78%, transparent 91%),
    var(--fibre);
  background-size: cover, cover, cover, cover, cover, cover, calc(var(--u) * 46); background-blend-mode: normal, normal, normal, normal, normal, normal, multiply;
}
.cover-in::after {
  content: ""; position: absolute; inset: calc(var(--u) * 1.6); border: calc(var(--u) * 0.18) solid var(--s-rule);
  border-radius: calc(var(--u) * 0.3); opacity: 0.75;
}
.cover-mono {
  display: grid; place-items: center; width: calc(var(--u) * 15); height: calc(var(--u) * 15); border-radius: 50%;
  border: calc(var(--u) * 0.16) solid var(--foil); outline: calc(var(--u) * 0.16) solid rgba(160, 122, 50, 0.45); outline-offset: calc(var(--u) * 0.7);
  margin-bottom: calc(var(--u) * 1.6);
}
.cover-mono span { font-family: var(--text); font-style: italic; font-size: calc(var(--u) * 6.2); line-height: 1; padding: 0 calc(var(--u) * 0.3) calc(var(--u) * 0.4); white-space: nowrap; }
.cover-names { font-family: var(--display); font-size: calc(var(--u) * 7.6 * var(--fit, 1)); line-height: 1.05; padding: 0 calc(var(--u) * 5) calc(var(--u) * 0.6); white-space: nowrap; }

/* ---------- the world: .scene holds its layers ---------- */
/* --cam is the camera: the same perspective, light-following tilt and lean the envelope's scene ends on.
   Each layer is a canvas larger than the card (104u by 79u) standing at its depth --z, so the world can rise past
   the card's edges. Its .fold lies flat on the ground until the book opens, then stands. */
.stage {
  --scene-scale: 1;
  --cam: perspective(calc(var(--u) * 300)) rotateX(calc(var(--ly, 0) * -2.5deg)) rotateY(calc(var(--lx, 0) * 4deg)) rotateX(-15deg);
}
.scene { position: absolute; inset: 0; pointer-events: none; scale: var(--scene-scale); transform-origin: 50% calc(var(--u) * 56); }
.ly {
  position: absolute; left: calc(var(--u) * -2); bottom: calc(var(--u) * 16); width: calc(var(--u) * 104); height: calc(var(--u) * 79);
  transform-origin: 50% 100%; transform-style: preserve-3d; transform: var(--cam) translateZ(var(--z));
}
/* Folded, a layer is all but invisible rather than at opacity 0, so the browser has its picture drawn and ready
   before the opening and the first frame of the rise is not spent on it. */
.fold { position: absolute; inset: 0; transform-origin: 50% 100%; opacity: 0.002; transform: rotateX(-84deg); }
.stage[data-state="open"] .fold {
  opacity: 1; transform: rotateX(0deg);
  transition: transform 0.9s var(--ease-spring) calc(2.1s + var(--d) * 0.1s), opacity 0.15s linear calc(2.08s + var(--d) * 0.1s);
}
/* will-change gives each picture its own compositor layer; without it Chrome draws pictures on a 3D plane at low
   resolution. A CSS filter on a 3D plane does the same, so shadows are baked into the pictures or drawn as SVG filters. */
.fold > img { position: absolute; left: var(--x, 0); bottom: var(--b, 0); width: var(--w, 100%); height: auto; max-width: none; will-change: transform; }
.fold svg { width: 100%; height: 100%; overflow: visible; }
.fold .cut { filter: url(#cut-shadow); }
.fold .lit { filter: url(#lamp-glow); }
#lamp-glow feDropShadow { flood-color: var(--s-glow); }
.ly-hills { --z: calc(var(--u) * 6); --d: 0; --w: calc(var(--u) * 104); }
.ly-trees { --z: calc(var(--u) * 15); --d: 1; }
.ly-arch { --z: calc(var(--u) * 24); --d: 2; --w: calc(var(--u) * 46); --x: calc(var(--u) * 29); }
.ly-glow { --z: calc(var(--u) * 24.6); --d: 2.6; }
.ly-ribbon { --z: calc(var(--u) * 33); --d: 3.4; }
.ly-flora { --z: calc(var(--u) * 40); --d: 4.2; --w: calc(var(--u) * 100); --x: calc(var(--u) * 2); }

/* ---------- the lanterns ---------- */
/* A lamp is a pool of warm light laid over a lantern in the picture. They come on one by one as the world stands up,
   then breathe slowly. Nothing else in the open world moves by itself. */
.tree { position: absolute; bottom: 0; width: calc(var(--u) * 44); }
.tree img { display: block; width: 100%; height: auto; will-change: transform; }
.tree-l { left: calc(var(--u) * -11); }
.tree-r { left: calc(var(--u) * 71); scale: -1 1; }
.lamp {
  position: absolute; left: var(--px); top: var(--py); width: calc(var(--u) * 14); height: calc(var(--u) * 14); margin: calc(var(--u) * -7);
  border-radius: 50%; opacity: 0;
  background: radial-gradient(closest-side, rgba(255, 246, 220, 0.85), color-mix(in srgb, var(--s-glow) 55%, transparent) 22%, color-mix(in srgb, var(--s-glow) 17%, transparent) 55%, transparent);
}
.stage[data-state="open"] .lamp { opacity: 1; animation: lamp-on 0.5s ease-out calc(2.75s + var(--n, 0) * 0.13s) backwards, breathe 5s ease-in-out calc(3.4s + var(--n, 0) * 0.4s) infinite alternate; }
.stage.is-settled[data-state="open"] .lamp { animation: breathe 5s ease-in-out calc(var(--n, 0) * -0.7s) infinite alternate; }
@keyframes lamp-on { 0% { opacity: 0; } 40% { opacity: 1; } 60% { opacity: 0.4; } }
@keyframes breathe { to { opacity: 0.6; } }

/* ---------- the colours of the world's drawn pieces ---------- */
.stage {
  --s-sky2: #51486e; --s-fruit: #f0c05a; --s-trunk: #4f3d30; --s-glow: #ffd98a;
  --s-ribbon: #fbf6ea; --s-ribbon2: #dccdaa; --s-ink: #1d3a32; --s-wire: #c9b98f; --s-base: #24463d; --s-rule: #d8b866;
}
.c-fruit { fill: var(--s-fruit); }
.c-trunk { fill: var(--s-trunk); }
.c-glow { fill: var(--s-glow); }
.c-ribbon { fill: var(--s-ribbon); } .c-ribbon2 { fill: var(--s-ribbon2); }
.c-foil-line { fill: none; stroke: #c9a35a; stroke-width: 1.6; opacity: 0.8; }
.wire { fill: none; stroke: var(--s-wire); stroke-width: 1.8; }
.ribbon-names { font-family: var(--text); font-style: italic; font-size: calc(52px * var(--fit, 1)); fill: var(--s-ink); }
