:root {
  --table: #fff;
  --cover: #0b0b0c;
  --cover-ink: #f4f1ea;
  --paper: #f6f3ec;
  --paper-tex: url("images/paper.jpg");
  /* Fine film grain: separate dark and light specks, so photos keep their colours and contrast */
  --grain-dark: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='256' height='256'%3E%3Cfilter id='d' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.35' numOctaves='1' seed='4' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .22 0 0 0 -.075'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23d)'/%3E%3C/svg%3E");
  --grain-light: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='256' height='256'%3E%3Cfilter id='l' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.35' numOctaves='1' seed='11' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 .17 0 0 -.06'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23l)'/%3E%3C/svg%3E");
  --film: var(--grain-dark), var(--grain-light);
  /* A soft, pebbled surface for the black covers, lit from one side like real bookcloth */
  --tex-board: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='b' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.62' numOctaves='3' seed='9' stitchTiles='stitch'/%3E%3CfeDiffuseLighting surfaceScale='1.1' lighting-color='%23fff' diffuseConstant='1'%3E%3CfeDistantLight azimuth='225' elevation='58'/%3E%3C/feDiffuseLighting%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 .42 0 0 0 -.285'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23b)'/%3E%3C/svg%3E");
}

html, body { height: 100%; margin: 0; overflow: hidden; background: var(--table); }

.book {
  position: fixed;
  inset: 0;
  overflow: hidden;
  isolation: isolate;
  background: #fff;
  outline: none;
  touch-action: pan-y pinch-zoom;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-family: Inter, "Inter Placeholder", system-ui, sans-serif;
  --radius: calc(var(--ph) * .014);
  --view: 0deg; /* seen from directly above */
  --page-shadow: 0 0 0 .5px rgba(30, 26, 20, .14), 0 calc(var(--ph) * .018) calc(var(--ph) * .05) rgba(30, 26, 20, .2);
  --shift: -.5;
  --tilt: 0deg;
  --open: 0;
  --fold: 0deg;
  /* The bound edge of a hardback: a rounded spine, then the hinge groove, with soft light across the board */
  --spine-front: linear-gradient(90deg, rgba(255, 255, 255, .13) 0, rgba(255, 255, 255, .04) 2.2%, rgba(0, 0, 0, .12) 5.4%, rgba(0, 0, 0, .66) 6.6%, rgba(255, 255, 255, .14) 7.4%, transparent 9.5%), radial-gradient(120% 90% at 30% 15%, rgba(255, 255, 255, .09), transparent 62%);
  --spine-back: linear-gradient(270deg, rgba(255, 255, 255, .13) 0, rgba(255, 255, 255, .04) 2.2%, rgba(0, 0, 0, .12) 5.4%, rgba(0, 0, 0, .66) 6.6%, rgba(255, 255, 255, .14) 7.4%, transparent 9.5%), radial-gradient(120% 90% at 70% 15%, rgba(255, 255, 255, .09), transparent 62%);
}
.book:focus-visible { box-shadow: inset 0 0 0 2px rgba(0, 0, 0, .35); }

.sr { position: absolute; width: 1px; height: 1px; margin: 0; overflow: hidden; clip: rect(0 0 0 0); }

.publish {
  position: fixed;
  z-index: 11;
  top: clamp(12px, 2.6vh, 22px);
  right: 16px;
  height: 36px;
  padding: 0 16px;
  border: 0;
  border-radius: 18px;
  background: #fff;
  color: #20243f;
  font: 500 13px Inter, system-ui, sans-serif;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(10, 12, 32, .4);
}
.publish:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.publish:disabled { opacity: .6; cursor: default; }

.scene { position: absolute; inset: 0; perspective: calc(var(--ph) * 5); }
.rig {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  transform-style: preserve-3d;
  transform: translate3d(calc(var(--shift) * var(--pw)), 0, 0) rotateX(calc(var(--view) + var(--tilt)));
}

.ground {
  position: absolute;
  top: calc(var(--ph) * -.5);
  height: var(--ph);
  transform: translate3d(calc(var(--ph) * -.015), calc(var(--ph) * .025), calc(var(--drop, 0px) * -1));
  background: rgba(30, 26, 20, .34);
  filter: blur(calc(var(--ph) * .035));
  pointer-events: none;
}
.stack {
  position: absolute;
  top: calc(var(--ph) / -2);
  width: var(--pw);
  height: var(--ph);
  transform-style: preserve-3d;
  pointer-events: none;
  --t: 0px;
  --c: 0px;
}
.stack.right { left: 0; transform-origin: 0 50%; transform: rotateY(calc(var(--fold) * -1)); }
.stack.left { left: calc(var(--pw) * -1); transform-origin: 100% 50%; transform: rotateY(var(--fold)); }
/* The block of paper and the back board, showing just past the top sheet */
.sheets { position: absolute; inset: 0; transform: translateZ(-3px); } /* edges only: a filled sheet would show as an extra page under a turning one */
.stack.right .sheets { border-radius: 0 var(--radius) var(--radius) 0; }
.stack.left .sheets { border-radius: var(--radius) 0 0 var(--radius); }
.edge {
  position: absolute;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  --sheets: repeating-linear-gradient(var(--grain-dir), #f5f1e8 0 1px, #cfc9bc 1px 2px);
}
.edge.bottom {
  --grain-dir: to bottom;
  top: 100%;
  height: var(--t);
  transform-origin: 50% 0;
  transform: rotateX(-90deg);
  background: linear-gradient(to bottom, transparent calc(100% - var(--c)), #161616 0), var(--sheets);
}
.stack.right .edge.bottom { left: 0; right: var(--radius); }
.stack.left .edge.bottom { left: var(--radius); right: 0; }
.edge.outer { top: var(--radius); bottom: var(--radius); width: var(--t); }
.stack.right .edge.outer {
  --grain-dir: to right;
  left: 100%;
  transform-origin: 0 50%;
  transform: rotateY(90deg);
  background: linear-gradient(to right, transparent calc(100% - var(--c)), #161616 0), var(--sheets);
}
.stack.left .edge.outer {
  --grain-dir: to left;
  right: 100%;
  transform-origin: 100% 50%;
  transform: rotateY(-90deg);
  background: linear-gradient(to left, transparent calc(100% - var(--c)), #161616 0), var(--sheets);
}

.cast { position: absolute; top: calc(var(--ph) / -2); height: var(--ph); width: var(--pw); transform: translateZ(.4px); pointer-events: none; }
.cast.right { left: 0; border-radius: 0 var(--radius) var(--radius) 0; transform-origin: 0 50%; transform: translateZ(.4px) rotateY(calc(var(--fold) * -1)); }
.cast.left { left: calc(var(--pw) * -1); border-radius: var(--radius) 0 0 var(--radius); transform-origin: 100% 50%; transform: translateZ(.4px) rotateY(var(--fold)); }

.leaf {
  --lw: var(--pw);
  position: absolute;
  left: 0;
  top: calc(var(--ph) / -2);
  width: var(--lw);
  height: var(--ph);
  transform-style: preserve-3d;
  transform-origin: 0 50%;
  pointer-events: none;
  transition: transform .5s cubic-bezier(.2, .8, .2, 1); /* pages slide along the fan as the book turns */
}
.leaf.moving { transition: none; }
@media (prefers-reduced-motion: reduce) { .leaf { transition: none; } }

/* A page at rest is one flat piece, so no strip seams show */
.flat {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--lw);
  height: 100%;
  overflow: hidden;
  display: none;
  box-shadow: var(--page-shadow);
  filter: brightness(var(--dim, 1));
}
.flat.front { border-radius: 0 var(--radius) var(--radius) 0; }
.flat.back { left: calc(var(--lw) * -1); border-radius: var(--radius) 0 0 var(--radius); }
.leaf:not(.moving):not(.at-left) > .flat.front,
.leaf.at-left:not(.moving) > .flat.back { display: block; }

/* A turning page is a chain of hinged strips */
.strip {
  position: absolute;
  top: 0;
  left: calc(var(--lw) / 12);
  width: calc(var(--lw) / 12 + 1.2px); /* overlaps its neighbour so no seam shows mid-turn */
  height: 100%;
  transform-origin: 0 50%;
  transform-style: preserve-3d;
}
.strip.first { left: 0; }
.leaf:not(.moving) > .strip { display: none; }
.side { position: absolute; inset: 0; overflow: hidden; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.side.b { transform: rotateY(180deg); transform-origin: calc(var(--lw) / 24) 50%; }
.shade { position: absolute; inset: 0; }

.face {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--lw);
  height: 100%;
  overflow: hidden;
  isolation: isolate; /* keeps photos and captions under the page-turn shading */
  background: var(--cover) center / cover no-repeat;
}
.face.front { border-radius: 0 var(--radius) var(--radius) 0; }
.face.back { border-radius: var(--radius) 0 0 var(--radius); }
/* The texture sits above the photos, so they look printed into the paper */
.face::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1001;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .5), inset 0 0 calc(var(--ph) * .03) rgba(58, 46, 30, .07);
}
.face.photo { background: var(--paper-tex) center / 100% 100% no-repeat, var(--paper); }
.face.photo.back { background-position: center; background-size: 100% 100%; }
.face.cover { background: var(--cover-art, none) center / cover no-repeat, var(--tex-board), var(--cover); background-size: cover, calc(var(--ph) * .49), auto; }
.face.rear { background: var(--tex-board), var(--cover); background-size: calc(var(--ph) * .49); }
.face.cover::before, .face.rear::before { content: ""; position: absolute; inset: 0; }
.face.cover::before { background: var(--spine-front); }
.face.rear::before { background: var(--spine-back); }
.face.endpaper { background: var(--tex-board), var(--cover); background-size: calc(var(--ph) * .49); } /* the texture scales with the book */
.slot { position: absolute; background: center / cover no-repeat; box-shadow: 0 1px 2px rgba(40, 30, 20, .34); }
.grip { display: none; position: absolute; right: 0; bottom: 0; width: 16px; height: 16px; cursor: nwse-resize; }
.grip::after { content: ""; position: absolute; right: 0; bottom: 0; width: 14px; height: 14px; background: #fff; border: 2px solid #0099ff; box-sizing: border-box; }
.arranging .leaf:not([inert]) .flat .slot { pointer-events: auto; cursor: move; touch-action: none; outline: 1px dashed rgba(0, 153, 255, .9); }
.arranging .leaf:not([inert]) .flat .slot:focus-visible { outline: 2px solid #0099ff; outline-offset: 1px; }
.arranging .leaf:not([inert]) .flat .grip { display: block; }
.page-tools { display: none; position: absolute; z-index: 1002; top: 8px; left: 10px; right: 10px; flex-wrap: wrap; gap: 4px; }
.arranging .leaf:not([inert]) .page-tools { display: flex; }
.page-tools button {
  height: 28px;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: #fff;
  color: #000;
  font: 500 12px Inter, system-ui, sans-serif;
  cursor: pointer;
  pointer-events: auto;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .28);
}
.page-tools button[hidden] { display: none; }
.page-tools button:focus-visible { outline: 2px solid #0099ff; outline-offset: 1px; }
@media (pointer: coarse) { .grip { width: 36px; height: 36px; } } /* a bigger touch target, same visible handle */

/* Gutter shading toward the spine; the back of a leaf mirrors the front */
.face.photo.front::after { background: linear-gradient(90deg, rgba(20, 16, 10, .3) 0%, rgba(20, 16, 10, .1) 2.6%, rgba(20, 16, 10, .03) 7%, transparent 12%); }
.face.photo.back::after { background: linear-gradient(270deg, rgba(20, 16, 10, .3) 0%, rgba(20, 16, 10, .1) 2.6%, rgba(20, 16, 10, .03) 7%, transparent 12%); }
.face.endpaper.front::after { background: linear-gradient(90deg, rgba(0, 0, 0, .5) 0%, rgba(0, 0, 0, .14) 3.5%, transparent 10%), var(--film); }
.face.cover::after { background: var(--film); }
.face.endpaper.back::after { background: linear-gradient(270deg, rgba(0, 0, 0, .5) 0%, rgba(0, 0, 0, .14) 3.5%, transparent 10%), var(--film); }
.face.rear::after { background: var(--film); }
.face.cover::after, .face.rear::after, .face.endpaper::after { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .07), inset 0 0 calc(var(--ph) * .05) rgba(0, 0, 0, .3); }

.title {
  position: absolute;
  z-index: 1;
  left: 13%;
  right: 9%;
  bottom: 9%;
  color: var(--cover-ink);
  font-size: calc(var(--ph) * .07);
  font-weight: 400;
  line-height: .95;
  letter-spacing: -.045em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.note {
  position: absolute;
  z-index: 1000;
  top: 12%;
  left: 12%;
  right: 12%;
  max-height: 76%;
  overflow: hidden;
  color: var(--cover-ink);
  font-size: calc(var(--ph) * .026);
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.note:empty, .caption:empty, .title:empty { display: none; }
.caption { max-width: 62%; overflow: hidden; text-overflow: ellipsis; }
.arranging .leaf:not([inert]) .flat [data-text] {
  display: block;
  min-width: 4em;
  pointer-events: auto;
  cursor: text;
  user-select: text;
  -webkit-user-select: text;
  outline: 1px dashed rgba(0, 153, 255, .95);
  outline-offset: 2px;
}
.arranging .leaf:not([inert]) .flat [data-text]:focus { outline: 2px solid #0099ff; text-overflow: clip; }
.arranging .leaf:not([inert]) .flat [data-text]:empty::before { content: attr(data-placeholder); opacity: .62; }
.caption, .folio {
  position: absolute;
  z-index: 1000;
  bottom: 8%;
  padding: 0 .4em;
  color: rgba(20, 20, 20, .76);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: calc(var(--ph) * .021);
  letter-spacing: .08em;
  text-transform: uppercase;
  white-space: nowrap;
}
.front .caption, .back .folio { left: 9%; }
.front .folio, .back .caption { right: 9%; }

.tools {
  position: fixed;
  z-index: 10;
  left: 0;
  right: 0;
  bottom: clamp(8px, 2vh, 18px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  color: #161616;
  font: 500 12px Inter, system-ui, sans-serif;
  text-align: center;
  pointer-events: none; /* the empty parts of this bar must not block the book */
}
.hint, .status { margin: 0; color: rgba(0, 0, 0, .62); }
.status { color: #161616; }
.status:empty { display: none; }
@media (max-height: 640px) { .hint { display: none; } } /* on a short screen it would cover the book */
.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; }
.controls button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 58px;
  padding: 2px 0;
  border: 0;
  border-radius: 10px;
  background: none;
  color: rgba(0, 0, 0, .68);
  font: inherit;
  font-size: 11px;
  cursor: pointer;
  pointer-events: auto;
}
.controls button[hidden] { display: none; }
.disc {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #fff;
  color: #161616;
  box-shadow: 0 0 0 .5px rgba(0, 0, 0, .14), 0 3px 10px rgba(0, 0, 0, .16);
  transition: transform .15s ease;
}
.disc svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.controls button:hover:not(:disabled) .disc { transform: scale(1.07); }
.controls button:focus-visible { outline: 2px solid #161616; outline-offset: 1px; }
.controls button:disabled { opacity: .4; cursor: default; }
#arrange[aria-pressed="true"] .disc { background: none; color: #fff; box-shadow: inset 0 0 0 2px #fff; }
@media (max-width: 420px) { .controls button { width: 50px; } }
@media (max-width: 640px) {
  .page-tools { flex-wrap: nowrap; top: 5px; left: 6px; right: 4px; gap: 3px; }
  .page-tools button { height: 24px; padding: 0 6px; font-size: 10px; white-space: nowrap; }
}

/* While the book shuts, the swinging sheet's outer side is the front cover */
.leaf.closing .face.front { background: var(--cover-art, none) center / cover no-repeat, var(--tex-board), var(--cover); background-size: cover, calc(var(--ph) * .49), auto; }
.leaf.closing .face.front::before { content: ""; position: absolute; inset: 0; background: var(--spine-front); }
.leaf.closing .face.front > * { display: none !important; } /* photos set their display inline */
.leaf.closing .face.front::after { background: var(--film); box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .07), inset 0 0 calc(var(--ph) * .05) rgba(0, 0, 0, .3); }
