:root {
  color-scheme: dark;
  --surface: #17191c;
  --surface-2: #202327;
  --chrome: rgba(22, 24, 27, 0.92);
  --chrome-border: rgba(255, 255, 255, 0.1);
  --text: #f3f0ea;
  --muted: #bbb5aa;
  --paper: #f8f6f0;
  --paper-edge: rgba(24, 22, 18, 0.16);
  --focus: #d7b978;
  --toolbar-height: 58px;
  --bottom-height: 62px;
  --page-inset: clamp(6px, 0.8vw, 12px);
  --book-width: min(96vw, calc((100dvh - var(--toolbar-height) - var(--bottom-height) - 28px) * 1.48), 1320px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 8%, rgba(255,255,255,0.08), transparent 34%),
    linear-gradient(145deg, var(--surface-2), var(--surface) 54%, #111315);
  color: var(--text);
}

button, input { font: inherit; }

button {
  min-height: 40px;
  border: 1px solid var(--chrome-border);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.055);
  color: var(--text);
  padding: 0.45rem 0.72rem;
  cursor: pointer;
  white-space: nowrap;
}

button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.1); }
button:focus-visible, input:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus), transparent 55%);
  outline-offset: 2px;
  border-color: var(--focus);
}
button:disabled { cursor: not-allowed; opacity: 0.36; }

.app {
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: grid;
  grid-template-rows: var(--toolbar-height) minmax(0, 1fr) var(--bottom-height);
}

.top-bar, .bottom-bar {
  min-height: 0;
  background: var(--chrome);
  border-color: var(--chrome-border);
  display: grid;
  align-items: center;
  gap: 0.65rem;
  padding: 0.45rem clamp(0.6rem, 1.6vw, 1.15rem);
  backdrop-filter: blur(18px);
  z-index: 10;
}

.top-bar {
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  border-bottom: 1px solid var(--chrome-border);
}

.bottom-bar {
  grid-template-columns: repeat(2, auto) minmax(11rem, auto) repeat(2, auto);
  justify-content: center;
  border-top: 1px solid var(--chrome-border);
}

.toolbar-zone { display: flex; align-items: center; gap: 0.45rem; min-width: 0; }
.toolbar-left { justify-content: flex-start; }
.toolbar-center { flex-direction: column; justify-content: center; gap: 0.08rem; text-align: center; }
.toolbar-right { justify-content: flex-end; }

h1 {
  margin: 0;
  max-width: 32vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(0.85rem, 1.35vw, 1.08rem);
  font-weight: 650;
  letter-spacing: 0.01em;
}

#page-status {
  margin: 0;
  color: var(--muted);
  font-size: clamp(0.74rem, 1.15vw, 0.92rem);
  line-height: 1.15;
}

.reader {
  position: relative;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: clamp(8px, 1.1vw, 14px);
  perspective: 2600px;
}

.book-frame {
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  place-items: center;
}

.book {
  --zoom: 1;
  position: relative;
  width: var(--book-width);
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1.42 / 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  transform: scale(var(--zoom));
  transform-origin: center center;
  transform-style: preserve-3d;
  transition: transform 160ms ease;
  isolation: isolate;
  contain: layout style;
}

.book.cover-mode,
.book.single-mode {
  width: min(48vw, calc((100dvh - var(--toolbar-height) - var(--bottom-height) - 28px) * 0.71), 650px);
  aspect-ratio: 0.71 / 1;
  grid-template-columns: minmax(0, 1fr);
}

.book-shadow {
  position: absolute;
  inset: 3.5% -1.5% -4.5%;
  border-radius: 18px;
  background:
    linear-gradient(90deg, rgba(0,0,0,0.2), transparent 16%, transparent 84%, rgba(0,0,0,0.2)),
    rgba(0,0,0,0.34);
  filter: blur(20px);
  transform: translate3d(0, 18px, -8px);
  z-index: 0;
}

.book::before {
  content: "";
  position: absolute;
  inset: -7px -9px -9px;
  border-radius: 16px;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.09), transparent 1.3%, transparent 98.7%, rgba(255,255,255,0.09)),
    linear-gradient(#dfd8c9, #cfc5b3);
  box-shadow: 0 20px 56px rgba(0,0,0,0.34);
  z-index: 0;
}

.book-page {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: stretch;
  overflow: hidden;
  padding: var(--page-inset);
  background: var(--paper);
  border: 1px solid rgba(255,255,255,0.24);
  box-shadow:
    inset 0 0 22px rgba(44, 34, 22, 0.045),
    0 1px 0 rgba(255,255,255,0.32);
  z-index: 1;
}

.left-page { border-radius: 10px 2px 2px 10px; transform-origin: right center; will-change: transform; }
.right-page { border-radius: 2px 10px 10px 2px; transform-origin: left center; will-change: transform; }

.left-page::after,
.right-page::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: clamp(18px, 5.5%, 44px);
  pointer-events: none;
  z-index: 3;
}
.left-page::after {
  right: 0;
  background: linear-gradient(to left, rgba(42, 34, 24, 0.11), rgba(42, 34, 24, 0.025) 38%, transparent);
}
.right-page::before {
  left: 0;
  background: linear-gradient(to right, rgba(42, 34, 24, 0.10), rgba(42, 34, 24, 0.022) 38%, transparent);
}

.book.spread-mode::after {
  content: "";
  position: absolute;
  top: 0.35%;
  bottom: 0.35%;
  left: 50%;
  width: clamp(10px, 1.4vw, 20px);
  transform: translateX(-50%);
  pointer-events: none;
  z-index: 4;
  background:
    linear-gradient(90deg,
      transparent,
      rgba(40, 34, 26, 0.075) 34%,
      rgba(255,255,255,0.20) 49%,
      rgba(40, 34, 26, 0.07) 66%,
      transparent);
  opacity: 0.86;
}

.book.cover-mode .left-page,
.book.cover-mode .right-page.empty,
.book.single-mode .left-page.empty,
.book.single-mode .right-page.empty { display: none; }
.book.cover-mode .right-page,
.book.single-mode .right-page:not(.empty) { border-radius: 10px; }
.book-page.empty { visibility: visible; }
.book-page.empty img { display: none; }

.book-page img,
.turn-face img {
  width: 100%;
  height: 100%;
  min-height: 0;
  object-fit: contain;
  background: transparent;
  user-select: none;
  touch-action: pan-y pinch-zoom;
  display: block;
}

.turn-page {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  display: none;
  transform-style: preserve-3d;
  z-index: 7;
  pointer-events: none;
  will-change: transform;
}

.turn-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: stretch;
  overflow: hidden;
  padding: var(--page-inset);
  background: var(--paper);
  border: 1px solid rgba(255,255,255,0.26);
  box-shadow: 0 16px 46px rgba(0,0,0,0.28), inset 0 0 26px rgba(43,33,24,0.055);
  backface-visibility: hidden;
  transform: translateZ(0);
}
.turn-back { transform: rotateY(180deg) translateZ(0); }
.turn-face::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(90deg, transparent 10%, rgba(55,43,31,0.14) 72%, rgba(255,255,255,0.16));
  opacity: 0;
  animation: sheet-shade 520ms cubic-bezier(.25,.46,.2,1) both;
}

.book.turning-next .turn-page {
  display: block;
  right: 0;
  transform-origin: left center;
  animation: turn-next 520ms cubic-bezier(.25,.46,.2,1) both;
}
.book.turning-next .turn-face { border-radius: 2px 10px 10px 2px; }
.book.turning-prev .turn-page {
  display: block;
  left: 0;
  transform-origin: right center;
  animation: turn-prev 520ms cubic-bezier(.25,.46,.2,1) both;
}
.book.turning-prev .turn-face { border-radius: 10px 2px 2px 10px; }
.book.turning-next .right-page::after,
.book.turning-prev .left-page::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  background: radial-gradient(ellipse at center, rgba(0,0,0,0.14), transparent 68%);
  animation: cast-shadow 520ms cubic-bezier(.25,.46,.2,1) both;
}

@keyframes turn-next {
  0% { transform: rotateY(0deg); }
  100% { transform: rotateY(-180deg); }
}
@keyframes turn-prev {
  0% { transform: rotateY(0deg); }
  100% { transform: rotateY(180deg); }
}
@keyframes sheet-shade {
  0% { opacity: 0.08; }
  50% { opacity: 0.42; }
  100% { opacity: 0.12; }
}
@keyframes cast-shadow {
  0%, 100% { opacity: 0; transform: scaleX(0.4); }
  50% { opacity: 0.42; transform: scaleX(1); }
}

.page-hotspot {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 18%;
  min-height: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  opacity: 0;
  z-index: 8;
}
.page-hotspot.left { left: 0; }
.page-hotspot.right { right: 0; }

.page-jump {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  color: var(--muted);
  white-space: nowrap;
}

#page-input {
  width: 4.7rem;
  min-height: 40px;
  border: 1px solid var(--chrome-border);
  border-radius: 999px;
  background: rgba(255,255,255,0.06);
  color: var(--text);
  padding: 0.35rem 0.65rem;
  text-align: center;
}

body.fullscreen-reading .top-bar,
body.fullscreen-reading .bottom-bar { background: rgba(12,13,15,0.72); }

@media (max-width: 820px) {
  :root {
    --toolbar-height: 54px;
    --bottom-height: 58px;
    --page-inset: 6px;
    --book-width: min(94vw, calc((100dvh - var(--toolbar-height) - var(--bottom-height) - 24px) * 0.71), 620px);
  }
  .top-bar { grid-template-columns: auto 1fr auto; gap: 0.35rem; }
  .toolbar-left button span,
  .toolbar-right button span,
  .bottom-bar button span { display: none; }
  h1 { display: none; }
  .book,
  .book.cover-mode,
  .book.single-mode {
    width: var(--book-width);
    aspect-ratio: 0.71 / 1;
    grid-template-columns: minmax(0, 1fr);
  }
  .book .left-page { display: none; }
  .book .right-page { border-radius: 10px; }
  .book.spread-mode::after { display: none; }
  .turn-page { width: 100%; }
  .book.turning-next .turn-face,
  .book.turning-prev .turn-face { border-radius: 10px; }
  .book.turning-next .turn-page { right: 0; }
  .book.turning-prev .turn-page { left: 0; }
  .bottom-bar { grid-template-columns: repeat(2, auto) minmax(7rem, auto) repeat(2, auto); gap: 0.35rem; }
  .page-jump span:first-child { display: none; }
}

@media (max-width: 520px) {
  button { min-height: 38px; padding-inline: 0.55rem; }
  .top-bar, .bottom-bar { padding-inline: 0.45rem; }
  #page-input { width: 3.9rem; }
  .toolbar-left { gap: 0.25rem; }
  .toolbar-right { gap: 0.25rem; }
}

@media (prefers-reduced-motion: reduce) {
  .book,
  .turn-page,
  .turn-face::after,
  .book.turning-next .right-page::after,
  .book.turning-prev .left-page::before {
    transition: none !important;
    animation: none !important;
  }
}
