:root {
  --bg: #201717;
  --panel: rgba(255, 255, 255, 0.08);
  --text: #f5f0eb;
  --muted: rgba(245, 240, 235, 0.78);
  --cover: #5b3d31;
  --accent: #caa67a;

  --toolbar-h: 58px;
  --cover-thickness: 8px;
  --spine-width: 4px;
  --frame-radius: 10px;
  --page-gap-padding-x: 4px;
  --page-gap-padding-y: 10px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.08), transparent 36%),
    linear-gradient(180deg, #3a2926, #1c1312 40%, #130d0c 100%);
  overflow: hidden;
}

/* error state */
.error-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.error-card {
  max-width: 720px;
  padding: 24px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(8px);
}

/* app shell */
.app-shell {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
}

/* top bar */
.topbar {
  min-height: var(--toolbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding: max(8px, env(safe-area-inset-top)) 14px 8px;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.12));
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(8px);
}

.title-wrap {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1 1 320px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex: 0 1 auto;
  margin-left: auto;
}

.eyebrow {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.topbar h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.15;
}

#zoomBtn {
  display: none !important;
}

.btn,
.page-indicator {
  min-height: 36px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  padding: 0 12px;
  font-size: 14px;
  text-decoration: none;
}

.btn {
  cursor: pointer;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-wide {
  min-width: 110px;
  text-align: center;
}

.page-indicator {
  min-width: 130px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
}

/* viewer layout */
.viewer-shell {
  min-height: 0;
  height: calc(100dvh - var(--toolbar-h));
  padding: 0;
}

.viewer-stage {
  position: relative;
  min-height: calc(100dvh - var(--toolbar-h));
  height: calc(100dvh - var(--toolbar-h));
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow: hidden;
  padding: 0;
  background: transparent !important;
  border: 0;
  border-radius: 0;
}

/* loading */
.loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-content: center;
  gap: 14px;
  justify-items: center;
  background:
    linear-gradient(rgba(20, 16, 14, 0.64), rgba(20, 16, 14, 0.64)),
    var(--cover-thumb, none);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.loading-overlay.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.loading-dimmer {
  position: absolute;
  inset: 0;
  backdrop-filter: blur(4px);
}

.loading-card {
  position: relative;
  z-index: 2;
  width: min(520px, calc(100% - 32px));
  margin: 0 auto;
  padding: 18px 18px 14px;
  border-radius: 12px;
  background: rgba(24, 20, 18, 0.44);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}

.spinner {
  display: none !important;
}

.loading-copy {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text);
}

.progress-track {
  width: min(320px, 78vw);
  height: 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), #fff0cf);
  transition: width 0.15s linear;
}

.progress-text {
  color: var(--muted);
}

/* book wrapper */
.book-chrome {
  position: absolute;
  inset: 0;
  width: 93%;
  height: 100%;
  margin: 0 auto;
  padding: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.book-shadow {
  position: absolute;
  width: 100%;
  height: calc(100% - 12px);
  border-radius: 34px;
  filter: blur(16px);
  opacity: 0.45;
  background: rgba(0, 0, 0, 0.36);
  transform: translateY(14px);
}

.book-frame {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  min-height: 0;
  padding-top: 10px;
  padding-right: 4px;
  padding-bottom: 6px;
  padding-left: 4px;
  border-radius: var(--frame-radius);
  overflow: visible;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.04), rgba(0, 0, 0, 0.08)),
    linear-gradient(90deg, rgba(255, 255, 255, 0.05), rgba(0, 0, 0, 0.1)),
    var(--cover);
  box-shadow:
    0 18px 40px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    inset 0 0 0 1px rgba(0, 0, 0, 0.18);
}
.book-frame::before,
.book-frame::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: var(--cover-thickness);
  border-radius: inherit;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(0, 0, 0, 0.12)),
    var(--cover);
  z-index: 2;
}

.book-frame::before {
  left: 0;
}

.book-frame::after {
  right: 0;
}

.book-spine {
  position: absolute;
  z-index: 2;
  width: var(--spine-width);
  height: min(86%, 920px);
  left: 50%;
  transform: translateX(-50%);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(0, 0, 0, 0.25)),
    linear-gradient(90deg, rgba(0, 0, 0, 0.28), rgba(255, 255, 255, 0.04), rgba(0, 0, 0, 0.32));
  box-shadow:
    0 0 6px rgba(0, 0, 0, 0.16),
    inset 0 0 2px rgba(255, 255, 255, 0.08);
}

/* pan/zoom + flipbook */
.book-panzoom {
  width: 100%;
  height: 100%;
  overflow: hidden;
  transform-origin: center center;
  will-change: transform;
  touch-action: auto;
}

#book,
.stf__parent {
  position: relative;
  z-index: 5;
}

#book {
  width: 100%;
  height: 100%;
  margin: 0 auto;
}

.page {
  background: #fff;
  overflow: hidden;
}

.page-canvas-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: white;
}

.page canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.page-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #4f4f4f;
  font-size: 14px;
  background: linear-gradient(180deg, #fbfbfb, #efefef);
}

.page-placeholder::after {
  content: "";
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid rgba(0, 0, 0, 0.12);
  border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}

/* page flip stacking and shadows */
.stf__wrapper,
.stf__block,
.stf__item {
  overflow: visible !important;
}

.stf__parent,
#book,
.book-panzoom {
  position: relative;
  z-index: 5;
}

.book-frame,
.book-frame::before,
.book-frame::after,
.book-spine {
  z-index: 2;
}

.stf__itemShadow {
  opacity: 0.72 !important;
  filter: blur(10px);
}

.viewer-stage.zoomed .book-panzoom {
  overflow: hidden;
}

.viewer-stage.zoomed .page {
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06);
}

.viewer-stage.zoomed .stf__block,
.viewer-stage.zoomed .stf__wrapper,
.viewer-stage.zoomed .stf__parent {
  pointer-events: none;
}

/* left-page watermark logo */
.stf__item.-left .stf__block {
  position: relative;
}

.stf__item.-left .stf__block::after {
  content: "";
  position: absolute;
  inset: 50% auto auto 50%;
  width: min(34%, 180px);
  aspect-ratio: 1 / 1;
  transform: translate(-50%, -50%);
  background-image: var(--watermark-logo);
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  opacity: 0.12;
  pointer-events: none;
  z-index: 3;
}

/* animation */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* smaller screens */
@media (max-width: 1023px) {
  :root {
    --toolbar-h: 100px;
  }

.topbar {
  align-items: center;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
}

  .topbar h1 {
    font-size: 16px;
  }

  .controls {
    justify-content: center;
    gap: 8px;
  }

  .btn,
  .page-indicator {
    min-height: 34px;
    padding: 0 10px;
    font-size: 13px;
  }

  .page-indicator {
    min-width: 110px;
  }

  .viewer-shell {
    height: calc(100dvh - var(--toolbar-h));
  }

  .viewer-stage {
    align-items: flex-start;
    justify-content: center;
    padding-top: 2px;
  }

.book-chrome {
  width: 100%;
  height: 100%;
  margin: 0;
  align-items: flex-start;
}

  .book-frame {
    background: transparent !important;
    box-shadow: none !important;
    padding: 0 !important;
    border-radius: 0 !important;
  }

  .book-frame::before,
  .book-frame::after {
    display: none !important;
  }

  .book-spine {
    display: none !important;
  }

  .book-shadow {
    width: 96%;
    height: 90%;
    opacity: 0.18;
    filter: blur(12px);
  }

  .book-panzoom {
    touch-action: none;
    transform-origin: top center;
  }

  .stf__item.-left .stf__block::after {
    opacity: 0.08;
  }
}

@media (max-width: 768px) {
  .controls {
    width: 100%;
    justify-content: center;
    margin-left: 0;
  }
}

/* very small screens */
@media (max-width: 640px) {
  body {
    overflow: hidden;
  }

  .topbar h1 {
    font-size: 16px;
  }

  .controls {
    gap: 8px;
  }

  .btn-wide {
    min-width: 92px;
  }

  .loading-card {
    width: min(92vw, 420px);
    padding: 16px 14px 12px;
  }

  .progress-track {
    width: min(280px, 78vw);
  }
}

/* larger landscape fine-tuning */
@media (min-width: 1024px) {
  .book-frame {
    padding-top: 10px;
    padding-right: 4px;
    padding-bottom: 6px;
    padding-left: 4px;
  }

  .book-frame::before,
  .book-frame::after {
    width: 7px;
  }
}