:root {
  --bg: #050505;
  --text: #f2eee6;
  --muted: #a7a29a;
  --line: rgba(255, 255, 255, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Arial, sans-serif;
}

body {
  overflow: hidden;
}

.topbar {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(5, 5, 5, 0.92);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 50;
}

.brand {
  font-size: 13px;
  letter-spacing: 0.04em;
  text-transform: none;
  color: var(--muted);
  max-width: 60vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.controls {
  display: flex;
  gap: 10px;
}

.toggle-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  padding: 8px 12px;
  cursor: pointer;
}

.toggle-btn:hover {
  color: var(--text);
  border-color: rgba(255, 255, 255, 0.2);
}

.toggle-btn.active {
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}

.app-main {
  position: relative;
  height: calc(100vh - 60px);
}

.loader-screen {
  position: absolute;
  inset: 0;
  background: #000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: opacity 500ms ease, visibility 500ms ease;
}

.loader-screen.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

#three-container {
  position: relative;
  width: 100%;
  height: 60%;
  flex-shrink: 0;
}

.loader-text {
  position: relative;
  z-index: 2;
  margin-top: 12px;
  color: var(--muted);
  font-size: 14px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.reader-shell {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 16px;
  transition: opacity 500ms ease;
}

.reader-shell.hidden {
  opacity: 0;
  pointer-events: none;
}

/* BOOK STAGE with real page-turn effect */

.book-stage {
  position: relative;
  display: none;
  justify-content: center;
  align-items: center;
  max-width: 100%;
  max-height: calc(100vh - 140px);
  perspective: 2600px;
}

.spread {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0;
}

.spread.single-page {
  justify-content: center;
}

.page-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
}

.page-slot canvas,
.flip-page canvas {
  display: block;
  max-height: calc(100vh - 140px);
  width: auto;
  height: auto;
}

/* The flipping page: a live copy of the outgoing page,
   positioned exactly over its slot. It only rotates a quarter
   turn away from the viewer, staying fully opaque the whole time.
   backface-visibility hides it automatically once it passes ~90deg,
   so it never needs an opacity fade and never looks "transparent". */
.flip-page {
  position: absolute;
  top: 50%;
  height: auto;
  display: flex;
  align-items: center;
  transform-style: preserve-3d;
  transform: translateY(-50%) rotateY(0deg);
  transition: transform 360ms cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
  visibility: hidden;
  z-index: 5;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.flip-page canvas {
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.flip-page.visible {
  visibility: visible;
}

.flip-page.origin-left {
  transform-origin: left center;
}

.flip-page.origin-right {
  transform-origin: right center;
}

.flip-page.rotated-forward {
  transform: translateY(-50%) rotateY(-92deg);
}

.flip-page.rotated-backward {
  transform: translateY(-50%) rotateY(92deg);
}

.flip-page::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.28), transparent 35%);
  pointer-events: none;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* SCROLL VIEW */

.scroll-view {
  width: 100%;
  height: calc(100vh - 140px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.scroll-view.hidden {
  display: none;
}

.scroll-page {
  line-height: 0;
}

.scroll-page canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

/* NAV */

.nav-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.nav-button {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.nav-button:hover {
  background: rgba(255, 255, 255, 0.06);
}

.nav-button:disabled {
  opacity: 0.3;
  cursor: default;
}

.page-indicator {
  font-size: 13px;
  color: var(--muted);
  min-width: 80px;
  text-align: center;
}

@media (max-width: 800px) {
  .topbar {
    flex-wrap: wrap;
    height: auto;
    padding: 10px 12px;
    gap: 8px;
  }

  .brand {
    max-width: 55vw;
    font-size: 11px;
  }

  .toggle-btn {
    font-size: 11px;
    padding: 6px 10px;
  }
}