/*
 * Flipbook player. Loaded only by the public shortcode page, on top of app.css
 * for the shared tokens.
 *
 * The page is designed to sit inside somebody else's iframe, so it fills its
 * viewport exactly and never scrolls the document — only the stage scrolls, and
 * only when zoomed in.
 */

body.viewer {
  height: 100vh;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  background: var(--info);
}

.fb {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--info);
}

.fb:fullscreen {
  background: var(--info);
}

/* ------------------------------------------------------------------- stage */

.fb__stage {
  position: relative;
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 1rem;
  overflow: hidden;
}

.fb__stage--zoomed {
  overflow: auto;
  place-items: safe center;
}

/* The book takes its size from its leaves, which are sized individually to
   their own pages. --fb-spine is the left leaf's width: with leaves of
   differing widths the spine is not the middle of the book. */
.fb__book {
  --fb-shift: 0px;
  --fb-spine: 50%;

  position: relative;
  display: flex;
  align-items: flex-start;
  width: max-content;
  height: max-content;
  perspective: 2400px;
  transform-style: preserve-3d;
  /* Set by applyShift() to centre a spread that has a blank half. Transitioned
     in step with the page turn so the cover reads as opening. */
  transform: translateX(var(--fb-shift));
  transition: transform 620ms cubic-bezier(0.42, 0, 0.28, 1);
}

.fb__book--single {
  transform: none;
}

@media (prefers-reduced-motion: reduce) {
  .fb__book {
    transition: none;
  }
}

/* Each leaf is one page, sized inline by the player to that page's own
   dimensions. Nothing here may impose a shape on it. */
.fb__leaf {
  position: relative;
  flex: none;
  overflow: hidden;
  background: var(--white);
  box-shadow: 0 2px 12px rgb(0 0 0 / 18%);
}

.fb__leaf--blank {
  background: var(--white);
  box-shadow: none;
}

.fb__book:not(.fb__book--single) .fb__leaf--blank {
  /* Keeps the cover centred against its empty facing page without drawing a
     second sheet of paper next to it. */
  background: transparent;
}

.fb__page {
  position: relative;
  width: 100%;
  height: 100%;
}

.fb__canvas {
  display: block;
}

/* Links are re-created as real elements over the canvas, which is only pixels.
   The layer itself must not swallow clicks meant for the page turn. */
.fb__links {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.fb__link {
  position: absolute;
  padding: 0;
  border: 0;
  background: transparent;
  pointer-events: auto;
  cursor: pointer;
  /* No visible box by default: the document already draws its own link styling.
     A faint wash on hover confirms the target is live. */
  border-radius: 2px;
  transition: background-color 120ms ease;
}

.fb__link:hover {
  background-color: rgb(253 231 3 / 28%);
}

.fb__link:focus-visible {
  outline: 2px solid var(--black);
  outline-offset: 1px;
  background-color: rgb(253 231 3 / 28%);
}

/* The gutter: a soft shadow either side of the spine, as a bound book has. */
.fb__book:not(.fb__book--single) .fb__leaf--left::after,
.fb__book:not(.fb__book--single) .fb__leaf--right::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 26px;
  pointer-events: none;
}

.fb__book:not(.fb__book--single) .fb__leaf--left::after {
  right: 0;
  background: linear-gradient(to right, rgb(0 0 0 / 0%), rgb(0 0 0 / 14%));
}

.fb__book:not(.fb__book--single) .fb__leaf--right::after {
  left: 0;
  background: linear-gradient(to left, rgb(0 0 0 / 0%), rgb(0 0 0 / 14%));
}

/* -------------------------------------------------------------- page turn */

/* The turning sheet; sized inline to the page being turned. */
.fb__turn {
  position: absolute;
  top: 0;
  transform-style: preserve-3d;
  transition: transform 620ms cubic-bezier(0.42, 0, 0.28, 1);
  z-index: 2;
}

/*
 * Both leaves are hinged at the spine and start flat over the half they belong
 * to, then swing across to the other half. The rest position is rotateY(0) in
 * both cases — the direction of travel comes from which half the leaf is
 * positioned over, not from the sign of the starting angle.
 *
 * Turning forward: the leaf sits over the right half and swings left.
 */
.fb__turn--next {
  left: var(--fb-spine);
  transform-origin: left center;
  transform: rotateY(0deg);
}

.fb__turn--next.fb__turn--run {
  transform: rotateY(-180deg);
}

/* Turning back: the leaf sits over the left half and swings right. Starting at
   180deg here would send it left instead, making a backward turn look exactly
   like a forward one. */
.fb__turn--prev {
  right: calc(100% - var(--fb-spine));
  transform-origin: right center;
  transform: rotateY(0deg);
}

.fb__turn--prev.fb__turn--run {
  transform: rotateY(180deg);
}

/* A face keeps its own page size rather than stretching to the sheet, and sits
   against the hinge so the two sides line up along the spine. */
.fb__face {
  position: absolute;
  top: 0;
  backface-visibility: hidden;
  box-shadow: 0 2px 16px rgb(0 0 0 / 22%);
}

.fb__turn--next .fb__face {
  left: 0;
}

.fb__turn--prev .fb__face {
  right: 0;
}

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

@media (prefers-reduced-motion: reduce) {
  .fb__turn {
    transition: none;
  }
}

/* --------------------------------------------------------- edge navigation */

.fb__page-nav {
  position: absolute;
  top: 50%;
  translate: 0 -50%;
  z-index: 3;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgb(255 255 255 / 88%);
  color: var(--black);
  cursor: pointer;
  opacity: 0;
  transition: opacity 150ms ease;
}

.fb__stage:hover .fb__page-nav,
.fb__page-nav:focus-visible {
  opacity: 1;
}

.fb__page-nav--prev {
  left: 0.75rem;
}

.fb__page-nav--next {
  right: 0.75rem;
}

.fb__page-nav:hover {
  background: var(--yellow);
}

.fb__page-nav svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Touch devices have swipe and no hover, so the arrows would just sit there. */
@media (hover: none) {
  .fb__page-nav {
    display: none;
  }
}

/* ------------------------------------------------------------------ status */

.fb__status {
  position: absolute;
  margin: 0;
  padding: 0.6rem 1rem;
  background: var(--white);
  border-left: 4px solid var(--yellow);
  font-size: 0.9rem;
  box-shadow: var(--shadow);
}

.fb__status--error {
  border-left-color: var(--danger);
}

/* --------------------------------------------------------------- tool bar */

.fb__bar {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex: none;
  padding: 0.4rem 0.75rem;
  background: var(--white);
  border-top: 1px solid var(--line);
}

.fb__gap {
  flex: 1;
}

.fb__btn {
  display: grid;
  place-items: center;
  min-width: 34px;
  height: 34px;
  padding: 0 0.4rem;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--black);
  font: inherit;
  cursor: pointer;
}

.fb__btn:hover:not(:disabled) {
  background: var(--yellow);
}

.fb__btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.fb__btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.fb__zoom-label {
  font-size: 0.78rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.fb__counter {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0 0.4rem;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.fb__page-input {
  width: 3.2rem;
  padding: 0.2rem 0.35rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--white);
  font: inherit;
  font-size: 0.85rem;
  text-align: center;
  color: var(--ink);
}

.fb__of {
  color: var(--ink-muted);
}

/* On a narrow embed the zoom controls are the first thing to go: swipe and the
   page counter matter more than a 34px button nobody can hit accurately. */
@media (max-width: 480px) {
  #fb-zoom-out,
  #fb-zoom-in,
  #fb-zoom-reset,
  #fb-first,
  #fb-last {
    display: none;
  }
}
