/* The flat-laid book: cover board, two pages, spine and flip layer. */

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 22px 50px;
  perspective: 2600px;
  overflow: hidden;
}

.book {
  /* Open spread of two A5 pages (148 x 210mm each), ratio kept exact via fitBook() in app.js */
  position: relative;
  display: flex;
  width: min(1440px, 100%);
  aspect-ratio: 148 / 105;
  max-height: 100%;
  padding: 18px;
  gap: 0;
  border-radius: var(--book-radius);
  background:
    linear-gradient(145deg, var(--cover), var(--cover-2));
  box-shadow:
    0 2px 0 var(--cover-stitch) inset,
    0 30px 60px -20px rgba(0, 0, 0, 0.75),
    0 0 0 1px var(--cover-edge);
  transform-style: preserve-3d;
}

.book::before {
  /* stitched border on the cover */
  content: "";
  position: absolute;
  inset: 8px;
  border: 1px dashed var(--cover-stitch);
  border-radius: calc(var(--book-radius) - 2px);
  pointer-events: none;
}

.page {
  position: relative;
  flex: 1 1 50%;
  min-width: 0;
  background: linear-gradient(180deg, var(--paper), var(--paper-2));
  box-shadow: 0 0 0 1px var(--paper-edge);
}

.page--left {
  border-radius: var(--page-radius) 0 0 var(--page-radius);
}

.page--right {
  border-radius: 0 var(--page-radius) var(--page-radius) 0;
}

.spine {
  width: 0;
  z-index: 2;
}

.page__inner {
  position: absolute;
  inset: 0;
  padding: calc(var(--pf, 1) * 22px) calc(var(--pf, 1) * 26px);
  overflow: hidden;
}

.page--left .page__inner {
  padding-right: calc(var(--pf, 1) * 38px);
}

.page--right .page__inner {
  padding-left: calc(var(--pf, 1) * 38px);
}

.page__folio,
.page__signature {
  position: absolute;
  bottom: 10px;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.page--left .page__folio { left: 30px; }
.page--right .page__folio { right: 30px; }

.page--left .page__signature { right: 42px; }
.page--right .page__signature { left: 42px; }

.page__signature {
  font-family: var(--font-hand);
  font-size: 14px;
  letter-spacing: 0.04em;
  text-transform: none;
  color: var(--ink-soft);
}

.book.is-readonly .page__inner {
  filter: saturate(0.92);
}

/* ---- Cover: back board, spine, front board — board sized -------- */
/* The boards are the book's covers, not its paper, so they fill the whole
   .book box (paper inset included). A closed board then measures exactly
   half of the open book, and the object keeps its size across leaves. */
.cover {
  position: absolute;
  inset: 0;
  z-index: 9;
  display: flex;
  cursor: pointer;
  transform-origin: left center;
  animation: cover-close 480ms cubic-bezier(0.4, 0, 0.2, 1);
}

.cover[hidden] {
  display: none;
}

.cover__panel {
  position: relative;
  flex: 1 1 50%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: var(--cover);
  box-shadow: inset 0 0 0 1px var(--cover-edge);
}

.cover__panel::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 1px dashed var(--cover-stitch);
  border-radius: 4px;
  pointer-events: none;
}

.cover__panel--back {
  border-radius: var(--book-radius) 0 0 var(--book-radius);
}

.cover__panel--front {
  border-radius: 0 var(--book-radius) var(--book-radius) 0;
}

.cover__spine {
  flex: none;
  width: 26px;
  background: var(--cover-2);
  box-shadow: inset 1px 0 0 var(--cover-edge), inset -1px 0 0 var(--cover-edge);
}

.cover__emboss {
  font-family: var(--font-head);
  font-size: 13px;
  letter-spacing: 0.4em;
  /* trailing letter-space would otherwise push the text off centre */
  text-indent: 0.4em;
  text-transform: uppercase;
  color: var(--cover-ink);
  opacity: 0.6;
}

.cover__plate {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: 100%;
  padding: 34px 56px;
  color: var(--cover-ink);
  border: 1px solid var(--cover-stitch);
  border-radius: 4px;
  text-align: center;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
}

.cover__title {
  font-family: var(--font-head);
  font-size: 34px;
  letter-spacing: 0.14em;
  text-indent: 0.14em;
  text-transform: uppercase;
}

.cover__rule {
  width: 110px;
  height: 1px;
  background: var(--cover-stitch);
}

.cover__owner {
  font: inherit;
  font-family: var(--font-hand);
  font-size: 22px;
  color: inherit;
  background: none;
  border: none;
  border-bottom: 1px dashed transparent;
  cursor: text;
  padding: 2px 6px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cover__owner:hover {
  border-bottom-color: var(--cover-stitch);
}

.cover__hint {
  position: absolute;
  bottom: 34px;
  font-size: 11px;
  letter-spacing: 0.2em;
  text-indent: 0.2em;
  text-transform: uppercase;
  color: var(--cover-ink);
  opacity: 0.8;
}

@keyframes cover-close {
  from { transform: rotateY(-92deg); opacity: 0.2; }
  to { transform: rotateY(0deg); opacity: 1; }
}

/* The back board hinges on the spine too, and from behind the spine is on
   the right, so it swings the other way. */
.stage[data-leaf="cover-back"] .cover {
  transform-origin: right center;
  animation-name: cover-close-back;
}

@keyframes cover-close-back {
  from { transform: rotateY(92deg); opacity: 0.2; }
  to { transform: rotateY(0deg); opacity: 1; }
}

/* While a board is showing it is the whole object. The spread's own board,
   stitching and paper sit behind it and would stand still while it turns. */
.stage[data-leaf="cover-front"] .book,
.stage[data-leaf="cover-back"] .book {
  background: none;
  box-shadow: none;
}

.stage[data-leaf="cover-front"] .book::before,
.stage[data-leaf="cover-back"] .book::before,
.stage[data-leaf="cover-front"] .page,
.stage[data-leaf="cover-back"] .page {
  visibility: hidden;
}

.stage[data-leaf="cover-front"] .cover,
.stage[data-leaf="cover-back"] .cover {
  border-radius: var(--book-radius);
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.75);
}

/* A closed book is one board. Side by side the two boards read as an open
   book with a cover on each leaf, which no book ever is, so the wide layout
   shows the board you asked for and nothing behind it. */
@media (min-width: 901px) {
  .stage[data-leaf="cover-front"] .cover,
  .stage[data-leaf="cover-back"] .cover {
    left: 0;
    right: 0;
    width: 50%;
    margin-inline: auto;
  }

  .stage[data-leaf="cover-front"] .cover__panel--back,
  .stage[data-leaf="cover-back"] .cover__panel--front,
  .stage[data-leaf="cover-front"] .cover__spine,
  .stage[data-leaf="cover-back"] .cover__spine {
    display: none;
  }

  .stage[data-leaf="cover-front"] .cover__panel,
  .stage[data-leaf="cover-back"] .cover__panel {
    border-radius: var(--book-radius);
  }
}

/* ---- Page flip ------------------------------------------------ */
.flip-layer {
  position: absolute;
  inset: 18px;
  pointer-events: none;
  z-index: 6;
  perspective: 2600px;
}

.flip-layer:empty {
  display: none;
}

.flip-shim {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  overflow: hidden;
  background: linear-gradient(180deg, var(--paper), var(--paper-2));
}

.flip-shim--left { left: 0; }
.flip-shim--right { right: 0; }

.flip-sheet {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  transform-style: preserve-3d;
  will-change: transform;
}

.flip-sheet--forward {
  right: 0;
  transform-origin: left center;
  animation: flip-forward var(--flip-ms, 620ms) cubic-bezier(0.5, 0, 0.3, 1) forwards;
}

.flip-sheet--back {
  left: 0;
  transform-origin: right center;
  animation: flip-back var(--flip-ms, 620ms) cubic-bezier(0.5, 0, 0.3, 1) forwards;
}

.flip-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  backface-visibility: hidden;
  background: linear-gradient(180deg, var(--paper), var(--paper-2));
  box-shadow: 0 0 0 1px var(--paper-edge);
}

.flip-face--back {
  transform: rotateY(180deg);
}

.flip-face::after {
  /* curl shading that follows the turn */
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(to left, rgba(0, 0, 0, 0.22), transparent 45%);
  opacity: 0.9;
}

.flip-face--back::after {
  background: linear-gradient(to right, rgba(0, 0, 0, 0.22), transparent 45%);
}

@keyframes flip-forward {
  from { transform: rotateY(0deg); }
  to { transform: rotateY(-180deg); }
}

@keyframes flip-back {
  from { transform: rotateY(0deg); }
  to { transform: rotateY(180deg); }
}

@media (prefers-reduced-motion: reduce) {
  .flip-sheet--forward,
  .flip-sheet--back {
    animation-duration: 1ms;
  }
}

/* Leaf navigation: dots under the book on every screen size. On narrow
   screens each dot is a single leaf; on desktop, a whole spread. */
.page-nav {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 4px 2px;
}

.page-nav__btn {
  flex: none;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
  color: var(--ui-fg);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: opacity 0.12s ease;
}

.page-nav__btn:active {
  opacity: 0.6;
}

.page-nav__btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.page-nav__mid {
  flex: none;
  min-width: 0;
  display: flex;
  align-items: center;
}

.page-nav__label {
  display: none;
}

.page-nav__dots {
  display: flex;
  align-items: center;
  gap: 12px;
}

.page-nav__dot {
  position: relative;
  flex: none;
  width: 6px;
  height: 6px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--ui-border);
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}

.page-nav__dot::before {
  /* the dot is a 6px mark but an 18x24px target */
  content: "";
  position: absolute;
  inset: -9px -6px;
}

.page-nav__dot.is-active {
  background: var(--accent);
  transform: scale(1.4);
}

/* Narrow screens: flip through one leaf at a time instead of a two-page
   spread — cover, left page, right page, unplanned, back cover. */
@media (max-width: 900px) {
  .flip-layer {
    display: none;
  }

  .stage {
    flex-direction: column;
    padding: 14px 18px;
  }

  .page {
    border-radius: var(--page-radius);
  }

  .page--left .page__inner,
  .page--right .page__inner {
    padding: calc(var(--pf, 1) * 20px);
    overflow: auto;
  }

  .cover__panel {
    flex: 1 1 100%;
    padding: 0 16px;
    border-radius: var(--book-radius);
  }

  .cover__plate {
    width: 100%;
    padding: 26px 18px;
  }

  .cover__title {
    font-size: clamp(22px, 7.5vw, 34px);
  }

  .cover__rule {
    width: min(110px, 60%);
  }

  .cover__owner {
    font-size: clamp(18px, 5.5vw, 22px);
  }

  .cover__hint {
    bottom: 22px;
  }

  /* Only the active leaf is rendered; every other leaf is pulled out of flow. */
  .stage[data-leaf] .page,
  .stage[data-leaf] .cover__panel {
    display: none;
  }

  .stage[data-leaf="left"] .page--left,
  .stage[data-leaf="unplanned"] .page--left {
    display: flex;
    flex: 1 1 100%;
  }

  .stage[data-leaf="right"] .page--right {
    display: flex;
    flex: 1 1 100%;
  }

  .stage[data-leaf="cover-front"] .cover__panel--front,
  .stage[data-leaf="cover-back"] .cover__panel--back {
    display: flex;
  }

  .stage[data-leaf="cover-front"] .cover__spine,
  .stage[data-leaf="cover-back"] .cover__spine {
    display: none;
  }
}

