/* book3d: a printed book drawn as a CSS 3D object that turns as the page scrolls.

   book3d.js builds the object from a plain cover image inside <div class="b3d">, works out the light,
   and writes the motion. Without JavaScript the plain cover is shown; without scroll-driven animation
   (older browsers), or when the reader has asked for reduced motion, the book is shown still.

   Size: set --bw (the on-screen width of a 152 mm wide book) on .b3d or any ancestor. Books of other
   widths are drawn to scale from it (data-width-mm). Everything else is set by book3d.js. */

.b3d { --w: calc(var(--bw, 224px) * var(--scale, 1)); --ratio: 1.5; --thick: .1756;
  --bt: calc(var(--w) * var(--thick)); --bh: calc(var(--w) * var(--ratio));
  --rest: 24; --tilt: 2; --eye: 30%; --kx: .577; --ky: .614; --dist: .547; --soft: .175; --cs: .2; --gl: .12;
  --drift: 14; --fo: 1; --fo-drift: 18; --head: #cfc9bb;
  position: relative; width: var(--w); height: calc(var(--bh) + var(--w) * .14); view-timeline: --b3d-pass block; }
/* put .b3d-entry on the element whose passage should time the turn (here, each entry's pinned scene) */
.b3d-entry { view-timeline: --b3d-entry block; }
/* the flat cover, before the script runs or without it */
.b3d > picture, .b3d > img { display: block; width: var(--w); }
.b3d > picture img, .b3d > img { display: block; width: var(--w); height: var(--bh); object-fit: cover; box-shadow: 0 10px 24px rgba(21,23,30,.18); }

.b3d-leave, .b3d-rise, .b3d-cast, .b3d-castmove, .b3d-persp, .b3d-cpersp, .b3d-book, .b3d-sbook, .b3d-face { position: absolute; }
.b3d-leave, .b3d-rise, .b3d-cast, .b3d-castmove { inset: 0; }
.b3d-persp, .b3d-cpersp { left: 0; top: 0; width: var(--w); height: var(--bh); perspective: calc(var(--w) * 3.8); perspective-origin: 50% var(--eye); }
.b3d-book, .b3d-sbook { inset: 0; transform-style: preserve-3d; will-change: transform; }
.b3d-book { transform: rotateY(calc(var(--rest) * 1deg)) rotateX(calc(var(--tilt) * 1deg)); }

/* every side keeps a transparent 1 px margin, so its outline is drawn smooth rather than stepped */
.b3d-face { padding: 1px; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.b3d-skin { position: absolute; inset: 1px; display: block; overflow: hidden; }
.b3d-skin i { position: absolute; inset: 0; pointer-events: none; }
.b3d-shade, .b3d-far { background: #1d1912; opacity: 0; }   /* how little light a side receives */
.b3d-sheen { background: #e9e5dc; opacity: 0; }             /* a laminated cover seen nearly edge-on reflects the room */

.b3d-front { inset: -1px; transform: translateZ(calc(var(--bt) / 2)); }
.b3d-front .b3d-skin { border-radius: 1px 2.5px 2.5px 1px; background: #fff; }
.b3d-front picture, .b3d-front img { display: block; width: 100%; height: 100%; box-shadow: none; }
.b3d-front img { object-fit: cover; }
.b3d-skin .b3d-glint { inset: 0 -90%; background: linear-gradient(102deg, rgba(255,255,255,0) 44%, rgba(255,255,255,var(--gl)) 50%, rgba(255,255,255,0) 56%); opacity: 0; transform: translateX(-40%); }
.b3d-back { inset: -1px; transform: rotateY(180deg) translateZ(calc(var(--bt) / 2)); }
.b3d-back .b3d-skin { background: var(--back, #f4f3ef); }
.b3d-back img { display: block; width: 100%; height: 100%; object-fit: cover; }
.b3d-spine, .b3d-fore { top: -1px; height: calc(100% + 2px); left: calc(50% - var(--bt) / 2 - 1px); width: calc(var(--bt) + 2px); }
.b3d-spine { transform: rotateY(-90deg) translateZ(calc(var(--w) / 2)); }
.b3d-spine .b3d-skin { background: #fff; border-radius: 1.5px; }
.b3d-spine img { display: block; width: 100%; height: 100%; }
/* a paperback's pages are trimmed flush with its cover */
.b3d-fore { transform: rotateY(90deg) translateZ(calc(var(--w) / 2)); }
.b3d-fore .b3d-skin { background: #ece6d9 url(textures/page-fore.webp) 0 0 / 100% 100%; }
/* the head is only seen when the viewpoint is above the book. Seen from just above, its depth is squeezed several
   times over, so its long edges are feathered in the drawing itself and its texture only comes in as it opens out. */
.b3d-top { left: -1px; width: calc(100% + 2px); top: calc(50% - var(--bt) / 2 - 1px); height: calc(var(--bt) + 2px); transform: rotateX(90deg) translateZ(calc(var(--bh) / 2)); }
.b3d-base { background: linear-gradient(to right, var(--head), #d9d3c5 2.2%, #e8e2d4 4%, #e8e2d4 96%, #ddd6c7);
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 11%, #000 89%, transparent); mask-image: linear-gradient(to bottom, transparent, #000 11%, #000 89%, transparent); }
.b3d-grain { background: #e8e2d4 url(textures/page-head.webp) 0 0 / 100% 100%; opacity: 0; }

/* a hardback: the boards stand 3 mm beyond the pages, the printed case covers their edges,
   and a shallow groove runs down the front where the case bends between spine and board */
.b3d-hb .b3d-fore .b3d-skin { background: none; }
.b3d-skin .b3d-pg { inset: var(--sq) var(--bd); background: #ece6d9 url(textures/page-fore.webp) 0 0 / 100% 100%; }
.b3d-skin .b3d-bd { width: var(--bd); }
.b3d-skin .b3d-bd-f { inset: 0 auto 0 0; background: var(--case-f); box-shadow: 1.5px 0 2.5px rgba(29,25,18,.28); }
.b3d-skin .b3d-bd-b { inset: 0 0 0 auto; background: var(--case-b); box-shadow: -1.5px 0 2.5px rgba(29,25,18,.28); }
.b3d-skin .b3d-groove { inset: 0 auto 0 calc(var(--hinge) - 1.1%); width: 2.2%;
  background: linear-gradient(to right, rgba(29,25,18,0), rgba(29,25,18,.2) 38%, rgba(255,248,236,.14) 62%, rgba(255,248,236,0)); }
.b3d-hb .b3d-base { background:
    linear-gradient(to bottom, var(--case-b) 0, var(--case-b) 9%, rgba(232,226,212,0) 26%, rgba(232,226,212,0) 74%, var(--case-h) 91%, var(--case-h) 100%),
    linear-gradient(to right, var(--head), #d9d3c5 2.2%, #e8e2d4 4%, #e8e2d4 96%, #ddd6c7); }
.b3d-hb .b3d-grain { background:
    linear-gradient(to bottom, var(--case-b) 0 var(--bd), rgba(232,226,212,0) var(--bd) calc(100% - var(--bd)), var(--case-h) calc(100% - var(--bd))),
    linear-gradient(to bottom, rgba(29,25,18,.3), rgba(29,25,18,0) 6%, rgba(29,25,18,0) 94%, rgba(29,25,18,.3)) 50% 50% / 100% calc(100% - 2 * var(--bd)) no-repeat,
    #e8e2d4 url(textures/page-head.webp) 50% 50% / 100% calc(100% - 2 * var(--bd)) no-repeat; }

/* the shadow on the page: a second copy of the book, flattened along the light onto a plane behind it and
   blurred once as a whole. The browser does the projection, so the outline is right at every angle. */
.b3d-cast { opacity: var(--cs); filter: blur(calc(var(--w) * var(--soft) * .21)); pointer-events: none; }
.b3d-sbook {
  --proj: translate3d(calc(var(--w) * var(--kx) * var(--dist) * -1), calc(var(--w) * var(--ky) * var(--dist)), calc(var(--w) * var(--dist) * -1))
          matrix3d(1, 0, 0, 0, 0, 1, 0, 0, calc(var(--kx) * -1), var(--ky), .01, 0, 0, 0, 0, 1);
  transform: var(--proj) rotateY(calc(var(--rest) * 1deg)) rotateX(calc(var(--tilt) * 1deg)); }
.b3d-sbook i { position: absolute; display: block; background: #281e14; }
.b3d-sf, .b3d-sb { inset: 0; }
.b3d-sf { transform: translateZ(calc(var(--bt) / 2)); }
.b3d-sb { transform: translateZ(calc(var(--bt) / -2)); }
.b3d-ss, .b3d-se { top: 0; height: 100%; left: calc(50% - var(--bt) / 2); width: var(--bt); }
.b3d-ss { transform: rotateY(-90deg) translateZ(calc(var(--w) / 2)); }
.b3d-se { transform: rotateY(90deg) translateZ(calc(var(--w) / 2)); }
.b3d-sh, .b3d-sl { left: 0; width: 100%; top: calc(50% - var(--bt) / 2); height: var(--bt); }
.b3d-sh { transform: rotateX(90deg) translateZ(calc(var(--bh) / 2)); }
.b3d-sl { transform: rotateX(-90deg) translateZ(calc(var(--bh) / 2)); }

/* ---------------- motion ---------------- */
@keyframes b3d-rise  { from { transform: translateY(calc(var(--drift) * 1px)); } to { transform: none; } }
@keyframes b3d-leave { from { transform: none; opacity: 1; } to { transform: translateY(calc(var(--fo-drift) * -1px)); opacity: calc(1 - var(--fo)); } }
/* Per book, book3d.js writes the turn and everything that follows from it (b3d-N-turn, the brightness of each
   side, sheen, glint, shadow), all sampled from one curve so they stay in step; and, when the light is at the
   reader, how the shadow and brightness change as the book passes up the screen (b3d-N-castmove, b3d-N-far). */
@supports (animation-timeline: view()) {
  .b3d-on .b3d-rise  { animation: b3d-rise auto cubic-bezier(.22,.62,.32,1) both; animation-timeline: --b3d-pass; animation-range: entry 0% entry 100%; }
  .b3d-on .b3d-leave { animation: b3d-leave auto ease-in both; animation-timeline: --b3d-pass; animation-range: exit -40% exit 70%; }
  /* the turn: by default the book's own passage across the screen is its clock */
  .b3d-on .b3d-t { animation-duration: auto; animation-timing-function: linear; animation-fill-mode: both;
    animation-timeline: --b3d-pass; animation-range: entry 0% exit 65%; }
  /* inside an element marked .b3d-entry, that element's passage is the clock; the host page can set the exact span */
  .b3d-on.b3d-by-entry .b3d-t { animation-timeline: --b3d-entry; animation-range: var(--b3d-range, entry 0% exit 85%); }
  /* the book's height on the screen, for a light at the reader */
  .b3d-on .b3d-p { animation-duration: auto; animation-timing-function: linear; animation-fill-mode: both;
    animation-timeline: --b3d-pass; animation-range: cover 0% cover 100%; }
}
@media (prefers-reduced-motion: reduce) {
  .b3d * { animation: none !important; }
}
