*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Special Elite", system-ui, serif;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.book {
  --bg: #000;
  --fg: #fff;
  --muted: rgba(255, 255, 255, 0.45);
  --border: rgba(255, 255, 255, 0.2);
  --spine: rgba(255, 255, 255, 0.14);
  --flip-duration: 0.7s;
  --chrome-footer: clamp(3rem, 8vh, 3.75rem);
  --chrome-nav: clamp(3.25rem, 9vh, 4rem);
  --text-body: clamp(0.95rem, 1.1vw + 0.65rem, 1.6rem);
  --text-label: clamp(0.75rem, 0.7vw + 0.55rem, 1rem);
  --text-title: clamp(2rem, 5.5vw + 1rem, 6rem);
  --text-cover-tagline: clamp(0.85rem, 1vw + 0.5rem, 1.35rem);
  --text-nav-indicator: clamp(1rem, 1.2vw + 0.6rem, 1.25rem);
  --text-nav-btn: clamp(1.5rem, 3vw + 0.75rem, 2rem);
  --text-footer: clamp(0.5rem, 1vw + 0.35rem, 0.7rem);
  --text-jump: clamp(0.75rem, 0.9vw + 0.45rem, 0.85rem);
  --pad-panel-x: clamp(2rem, 8vw, 7rem);
  --pad-panel-y: clamp(2rem, 7vh, 5.5rem);
  --pad-cover-x: clamp(3.5rem, 14vw, 11rem);
  --content-inset: min(80%, 36ch);

  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  /* transition: background-color 0.4s ease, color 0.4s ease; */
}

.book[data-theme="white"] {
  --bg: #fff;
  --fg: #000;
  --muted: rgba(0, 0, 0, 0.45);
  --border: rgba(0, 0, 0, 0.2);
  --spine: rgba(0, 0, 0, 0.12);
}

.book[data-theme="black"] {
  --bg: #000;
  --fg: #fff;
  --muted: rgba(255, 255, 255, 0.45);
  --border: rgba(255, 255, 255, 0.2);
  --spine: rgba(255, 255, 255, 0.14);
}

.book__stage {
  position: absolute;
  inset: 0;
  bottom: calc(var(--chrome-footer) + var(--chrome-nav));
  perspective: 2200px;
  perspective-origin: 50% 50%;
}

/* Vertical spine (desktop): between left image and right text */
.book__stage::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1px;
  height: auto;
  right: auto;
  transform: translateX(-50%);
  background: var(--spine);
  z-index: 6;
  pointer-events: none;
}

.spread {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  transform-style: preserve-3d;
}

.spread--current {
  z-index: 2;
}

.spread--incoming {
  z-index: 1;
  pointer-events: none;
  visibility: hidden;
  opacity: 0;
}

.spread.is-visible {
  visibility: visible;
  opacity: 1;
}

.spread--underneath {
  visibility: visible;
  opacity: 1;
}

/* During flip: keep the static page side visible, hide the side that is turning */
.spread--flip-forward .panel--right {
  visibility: hidden;
}

.spread--flip-backward .panel--left,
.spread--flip-backward .panel--right {
  visibility: hidden;
}

.book__flip-leaf {
  position: absolute;
  top: 0;
  height: 100%;
  width: 50%;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transform-style: preserve-3d;
  transform: rotateY(0deg);
  transition: transform var(--flip-duration) ease-in;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  background: var(--bg);
  color: var(--fg);
  isolation: isolate;
  contain: layout paint;
  pointer-events: none;
}

.book__flip-leaf .panel {
  width: 100%;
  height: 100%;
  transform: none;
  will-change: auto;
}

.book__flip-leaf .panel--right {
  padding: var(--pad-panel-y) var(--pad-panel-x);
}

.book__flip-leaf .panel--right .panel__label,
.book__flip-leaf .panel--right .panel__text {
  width: var(--content-inset);
  max-width: 80%;
}

.book__flip-leaf .panel__image,
.book__flip-leaf .panel__label,
.book__flip-leaf .panel__text,
.book__flip-leaf .panel__title {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.book__flip-leaf--right {
  right: 0;
  transform-origin: left center;
}

.book__flip-leaf--left {
  left: 0;
  transform-origin: right center;
}

.panel {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
}

.book--flip-forward .book__flip-leaf--right {
  transform: rotateY(-90deg);
}

.book--flip-backward .book__flip-leaf--left {
  transform: rotateY(90deg);
}

.panel--left {
  border-right: none;
}

.spread--cover .panel--left,
.spread--cover .panel--right {
  padding-left: var(--pad-cover-x);
  padding-right: var(--pad-cover-x);
}

.spread--cover .panel__title {
  padding-left: 0;
  padding-right: 0;
}

.panel--right {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--pad-panel-y) var(--pad-panel-x);
  overflow-y: auto;
}

.panel--right .panel__label,
.panel--right .panel__text {
  width: var(--content-inset);
  max-width: 80%;
}

.panel__image {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}

.panel__title {
  font-family: "Mrs Saint Delafield", cursive;
  font-size: var(--text-title);
  line-height: 1.1;
  text-align: center;
  width: 100%;
  padding: clamp(1.25rem, 4vw, 2rem);
}

.panel__cover-tagline {
  display: block;
  font-family: "Special Elite", system-ui, serif;
  font-size: var(--text-cover-tagline);
  line-height: 1.55;
  letter-spacing: 0.02em;
  margin-top: 0;
}

.panel__cover-tagline em {
  font-style: normal;
}

.panel__label {
  font-family: "Special Elite", system-ui, serif;
  font-size: var(--text-label);
  letter-spacing: 0.05em;
  margin-bottom: 1rem;
  opacity: 0.85;
}

.panel__text {
  font-family: "Special Elite", system-ui, serif;
  font-size: var(--text-body);
  line-height: 1.65;
  width: 100%;
}

.panel__text p {
  margin-bottom: 0.75em;
}

.panel__text p:last-child {
  margin-bottom: 0;
}

.panel__text em {
  font-style: italic;
}

.panel__text a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.type-word {
  opacity: 0;
}

.type-word.is-visible {
  opacity: 1;
}

.panel--text-pending .panel__label,
.panel--text-pending .panel--typing {
  visibility: hidden;
}

.panel--typing .type-word {
  transition: opacity 0.05s ease;
}

.panel--typing-done .type-word {
  opacity: 1;
}

.panel__placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  width: min(80%, 320px);
  aspect-ratio: 4 / 5;
  border: 2px dashed var(--border);
  border-radius: 4px;
  padding: 1.5rem;
  text-align: center;
}

.panel__placeholder-title {
  font-family: "Mrs Saint Delafield", cursive;
  font-size: clamp(1.75rem, 4vw, 2.5rem);
}

.panel__placeholder-note {
  font-family: "Special Elite", system-ui, serif;
  font-size: clamp(1rem, 2.5vw, 1.35rem);
  color: var(--muted);
}

.book__footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  padding: 0.35rem 1rem 0.55rem;
  text-align: center;
  font-size: var(--text-footer);
  line-height: 1.45;
  color: var(--muted);
}

.book__footer p + p {
  margin-top: 0.15rem;
}

.book__footer a {
  color: var(--muted);
  text-decoration: none;
  /* text-underline-offset: 1px; */
}

.book__footer a:hover {
  opacity: 0.8;
}

.book__nav {
  position: absolute;
  bottom: var(--chrome-footer);
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
  padding: 1rem 1.5rem;
  z-index: 100;
}

.book__nav-btn {
  font-family: inherit;
  font-size: var(--text-nav-btn);
  line-height: 1;
  width: 2.75rem;
  height: 2.75rem;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  transition: opacity 0.2s, background 0.2s;
}

.book__nav-btn:hover:not(:disabled) {
  background: var(--border);
}

.book__nav-btn:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.book__jump {
  position: relative;
  z-index: 1;
}

.book__jump:hover,
.book__jump:focus-within,
.book__jump--open {
  z-index: 200;
}

.book__page-indicator {
  font-family: inherit;
  font-size: var(--text-nav-indicator);
  min-width: 6ch;
  padding: 0.35rem 0.75rem;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.book__page-indicator:hover,
.book__jump:focus-within .book__page-indicator {
  border-color: var(--border);
  background: rgba(127, 127, 127, 0.12);
}

.book__jump-menu {
  position: absolute;
  bottom: calc(100% + 0.5rem);
  top: auto;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  min-width: 11rem;
  max-width: min(18rem, 90vw);
  max-height: min(16rem, 45vh);
  padding: 0.35rem 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  background-color: var(--bg);
  color: var(--fg);
  box-shadow: 0 -6px 28px rgba(0, 0, 0, 0.45);
  visibility: hidden;
  pointer-events: none;
  transition: visibility 0.15s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  opacity: 1;
}

.book[data-theme="black"] .book__jump-menu {
  background-color: #000;
}

.book[data-theme="white"] .book__jump-menu {
  background-color: #fff;
}

.book__jump:hover .book__jump-menu,
.book__jump:focus-within .book__jump-menu,
.book__jump--open .book__jump-menu {
  visibility: visible;
  pointer-events: auto;
}

.book__jump-item {
  display: block;
  width: 100%;
  padding: 0.45rem 0.85rem;
  border: none;
  background-color: var(--bg);
  color: var(--fg);
  font-family: inherit;
  font-size: var(--text-jump);
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book[data-theme="black"] .book__jump-item {
  background-color: #000;
}

.book[data-theme="white"] .book__jump-item {
  background-color: #fff;
}

.book[data-theme="black"] .book__jump-item:hover,
.book[data-theme="black"] .book__jump-item.is-active {
  background-color: #1a1a1a;
}

.book[data-theme="white"] .book__jump-item:hover,
.book[data-theme="white"] .book__jump-item.is-active {
  background-color: #e8e8e8;
}

.book__jump-item.is-active {
  font-weight: bold;
}

.book__jump-list {
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.book__tap-zones {
  position: absolute;
  inset: 0;
  bottom: calc(var(--chrome-footer) + var(--chrome-nav));
  display: grid;
  grid-template-columns: 1fr 1fr;
  z-index: 5;
  pointer-events: none;
}

.book__tap {
  pointer-events: auto;
  border: none;
  background: transparent;
  cursor: pointer;
}

.book.is-flipping {
  pointer-events: none;
}

.book.is-flipping .book__nav-btn,
.book.is-flipping .book__tap {
  pointer-events: none;
}

@media (max-width: 768px) {
  .book {
    --text-body: clamp(0.9rem, 2.8vw + 0.5rem, 1.35rem);
    --text-label: clamp(0.7rem, 2.2vw + 0.4rem, 0.95rem);
    --text-title: clamp(1.75rem, 9vw + 0.5rem, 3.5rem);
    --text-cover-tagline: clamp(0.8rem, 2.6vw + 0.4rem, 1.15rem);
    --text-nav-indicator: clamp(0.95rem, 2.8vw + 0.45rem, 1.15rem);
    --text-nav-btn: clamp(1.35rem, 5vw + 0.5rem, 1.75rem);
    --pad-panel-x: clamp(1.25rem, 6vw, 2.5rem);
    --pad-panel-y: clamp(1.25rem, 4vh, 2rem);
    --pad-cover-x: clamp(2rem, 10vw, 4rem);
    --content-inset: min(92%, 34ch);
    --chrome-footer: clamp(2.75rem, 10vh, 3.5rem);
    --chrome-nav: clamp(3rem, 11vh, 3.75rem);
  }

  .book__stage {
    perspective: none;
  }

  /* Horizontal spine (mobile): between top image and bottom text */
  .book__stage::after {
    left: 0;
    right: 0;
    top: 50%;
    bottom: auto;
    width: auto;
    height: 1px;
    transform: translateY(-50%);
  }

  .spread {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
    transform: none !important;
  }

  .panel--left {
    border-right: none;
    border-bottom: none;
  }

  .book__tap-zones {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }

  .book--flip-forward .book__flip-leaf--right,
  .book--flip-backward .book__flip-leaf--left {
    transition: none;
    transform: none;
  }

  .book.is-flipping .book__flip-leaf {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .book__stage {
    perspective: none;
  }

  .book--flip-forward .book__flip-leaf--right,
  .book--flip-backward .book__flip-leaf--left {
    transition: none;
    transform: none;
  }

  .book {
    transition: none;
  }
}
