:root {
  --vh: 1vh;
  --bg-0: #edf2ee;
  --bg-1: #dfe7e1;
  --ink-0: #1f2932;
  --ink-1: #4f5d59;
  --line: rgba(120, 133, 128, 0.28);
  --accent: #1d8f4c;
  --accent-strong: #15723b;
  --panel: rgba(255, 255, 255, 0.24);
  --glass-border: rgba(255, 255, 255, 0.3);
  --glass-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
  --radius: 18px;
  --shadow: 0 16px 42px rgba(15, 34, 60, 0.18);
}

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

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

body {
  font-family: "Space Grotesk", "Avenir Next", "Segoe UI", sans-serif;
  color: var(--ink-0);
  background:
    radial-gradient(1200px 700px at 15% 12%, rgba(29, 143, 76, 0.15), transparent 58%),
    linear-gradient(148deg, var(--bg-0), var(--bg-1));
  padding: 16px;
}

.app-shell {
  max-width: 1460px;
  margin: 0 auto;
  min-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.34), rgba(255, 255, 255, 0.1)),
    var(--panel);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  box-shadow: var(--glass-shadow);
  backdrop-filter: blur(14px) saturate(140%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
}

.brand {
  flex: 1;
  min-width: 0;
}

h1 {
  margin: 0;
  font-size: clamp(0.95rem, 1.55vw, 1.36rem);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
  flex: 1;
  min-width: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  white-space: nowrap;
}

.toolbar-left {
  justify-content: flex-start;
}

.toolbar-right {
  justify-content: flex-end;
  margin-left: auto;
}

button,
.page-input {
  font: inherit;
}

.nav-button,
.jump-button {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.18s ease, background-color 0.18s ease, border-color 0.18s ease;
}

.nav-button {
  background: rgba(255, 255, 255, 0.92);
  border-color: var(--line);
  color: var(--ink-0);
}

.jump-button {
  background: var(--accent);
  color: #fff;
}

.nav-button:hover,
.jump-button:hover {
  transform: translateY(-1px);
}

.jump-button:hover {
  background: var(--accent-strong);
}

.nav-button.is-on {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(29, 143, 76, 0.1);
}

.nav-button:disabled,
.jump-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.zoom-text {
  min-width: 40px;
  text-align: center;
  color: var(--ink-1);
  font-size: 0.75rem;
}

.jump-label {
  color: var(--ink-1);
  font-size: 0.75rem;
}

.page-input {
  width: 4.4ch;
  min-width: 4.4ch;
  border-radius: 999px;
  border: 1px solid var(--line);
  padding: 6px 6px;
  text-align: center;
  background: #fff;
  font-size: 0.8rem;
  line-height: 1;
}

.page-input:focus-visible {
  outline: 2px solid rgba(29, 143, 76, 0.3);
  border-color: var(--accent);
}

button:focus-visible {
  outline: 2px solid rgba(29, 143, 76, 0.3);
  outline-offset: 2px;
}

#soundButton .sound-wave {
  font-size: 0.84em;
}

.page-input::-webkit-outer-spin-button,
.page-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.page-input[type="number"] {
  -moz-appearance: textfield;
}

.viewer-layout {
  flex: 0 0 auto;
  min-height: 0;
}

.viewport {
  position: relative;
  height: auto;
  min-height: 0;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(248, 252, 255, 0.58);
  backdrop-filter: blur(6px);
  box-shadow: var(--shadow);
  padding: 14px;
  overflow: hidden;
  touch-action: pan-y;
  user-select: none;
  perspective: 2200px;
}

.page-track {
  width: 100%;
  height: auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 54px;
  transform-origin: center center;
  transition: transform 170ms ease;
}

.edge-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.edge-nav-left {
  left: 8px;
}

.edge-nav-right {
  right: 8px;
}

.edge-button {
  width: 44px;
  height: 44px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 12px;
  background: rgba(33, 47, 66, 0.46);
  color: #fff;
  opacity: 0.2;
  cursor: pointer;
  backdrop-filter: blur(4px);
  font-weight: 700;
  transition: transform 0.18s ease, background-color 0.18s ease, opacity 0.18s ease;
}

.edge-button:hover {
  transform: translateY(-1px);
  background: rgba(22, 37, 58, 0.72);
  opacity: 0.8;
}

.edge-button:active,
.edge-button:focus-visible {
  opacity: 0.8;
}

.edge-button:disabled {
  opacity: 0.12;
  cursor: not-allowed;
  transform: none;
}

.book-scene {
  --book-page-max-height: 920px;
  --ui-stack-height: 210px;
  --scene-h-limit: min(calc((var(--vh) * 100) - var(--ui-stack-height)), var(--book-page-max-height));
  width: 100%;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.mobile-scene {
  position: relative;
  display: block;
  width: min(100%, calc(var(--scene-h-limit) * (1497 / 2048)));
  aspect-ratio: 1497 / 2048;
  height: auto;
  max-height: var(--scene-h-limit);
  perspective: 1800px;
  transform-style: preserve-3d;
  overflow: visible;
}

.zoom-target {
  transform-origin: center center;
  transition: transform 180ms ease;
}

.cover-page {
  position: relative;
  width: min(100%, calc(var(--scene-h-limit) * (1497 / 2048)));
  aspect-ratio: 1497 / 2048;
  height: auto;
  max-height: var(--scene-h-limit);
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid #d4deea;
  box-shadow:
    0 20px 46px rgba(15, 23, 42, 0.24),
    0 3px 6px rgba(15, 23, 42, 0.14);
  background: #fff;
  animation: cover-in 360ms ease;
}

.cover-page img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #fff;
}

.cover-page.to-cover {
  animation: cover-back 360ms ease;
}

.book-spread {
  position: relative;
  width: min(100%, calc(var(--scene-h-limit) * (2994 / 2048)));
  aspect-ratio: 2994 / 2048;
  height: auto;
  max-height: var(--scene-h-limit);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  transform-style: preserve-3d;
  cursor: grab;
  will-change: transform;
}

.book-spread.is-grabbing {
  cursor: grabbing;
}

.spine {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 1px;
  height: 100%;
  z-index: 5;
  background: linear-gradient(
    to bottom,
    rgba(58, 71, 86, 0.2),
    rgba(23, 35, 53, 0.26),
    rgba(58, 71, 86, 0.2)
  );
  box-shadow: 0 0 6px rgba(15, 23, 42, 0.14);
}

.paper {
  position: relative;
  overflow: hidden;
  border: 1px solid #d4deea;
  background: #fff;
  box-shadow: 0 14px 30px rgba(15, 23, 42, 0.18);
}

.paper.left {
  border-radius: 12px 0 0 12px;
  border-right: 0;
  margin-right: -1px;
  box-shadow:
    -12px 16px 30px rgba(15, 23, 42, 0.16),
    -2px 2px 4px rgba(15, 23, 42, 0.1);
}

.paper.cover-under-hidden {
  border-color: transparent;
  box-shadow: none;
  background: transparent;
}

.paper.cover-under-hidden .paper-face,
.paper.cover-under-hidden .page-meta {
  opacity: 0;
}

.paper.right {
  border-radius: 0 12px 12px 0;
  border-left: 0;
  margin-left: -1px;
  box-shadow:
    12px 16px 30px rgba(15, 23, 42, 0.16),
    2px 2px 4px rgba(15, 23, 42, 0.1);
}

.paper-face {
  position: absolute;
  inset: 0;
}

.paper-face img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #fff;
  pointer-events: none;
}

.paper.blank {
  background:
    linear-gradient(125deg, rgba(15, 118, 110, 0.06), rgba(15, 118, 110, 0.02)),
    #fff;
}

.page-meta {
  position: absolute;
  right: 9px;
  bottom: 8px;
  z-index: 6;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.74rem;
  color: #334155;
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid #d8e5ef;
}

.flip-sheet {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  z-index: 10;
  display: none;
  transform-style: preserve-3d;
  will-change: transform;
  pointer-events: none;
  --sheet-shadow: 0;
  --sheet-highlight: 0;
}

.flip-sheet.active {
  display: block;
}

.flip-sheet.next {
  left: 50%;
  transform-origin: left center;
  transform: rotateY(0deg);
}

.flip-sheet.prev {
  left: 0;
  transform-origin: right center;
  transform: rotateY(0deg);
}

.flip-sheet.cover-open {
  left: 50%;
  transform-origin: left center;
  transform: rotateY(0deg);
  z-index: 28;
}

.flip-sheet.animating {
  transition: transform var(--flip-duration, 640ms) cubic-bezier(0.2, 0.62, 0.16, 0.98);
}

.flip-sheet::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to right, rgba(255, 255, 255, 0.44), rgba(255, 255, 255, 0));
  opacity: var(--sheet-highlight);
  pointer-events: none;
}

.flip-sheet::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to left, rgba(15, 23, 42, 0.36), rgba(15, 23, 42, 0.02));
  opacity: var(--sheet-shadow);
  pointer-events: none;
}

.flip-face {
  position: absolute;
  inset: 0;
  backface-visibility: visible;
  -webkit-backface-visibility: visible;
  transform-style: preserve-3d;
  overflow: hidden;
  border: 1px solid #d4deea;
  background: #fff;
}

.flip-face img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #fff;
  pointer-events: none;
  backface-visibility: visible;
  -webkit-backface-visibility: visible;
}

.flip-face.front {
  border-radius: 0 12px 12px 0;
}

.flip-sheet.next .flip-face.front {
  border-left: 0;
}

.flip-sheet.prev .flip-face.front {
  border-radius: 12px 0 0 12px;
  border-right: 0;
}

.flip-sheet.cover-open .flip-face.front {
  border-left: 0;
  border-radius: 0 12px 12px 0;
}

.flip-face.back {
  transform: rotateY(180deg) translateZ(1px);
  border-radius: 12px 0 0 12px;
}

.flip-sheet.next .flip-face.back {
  border-right: 0;
}

.flip-sheet.prev .flip-face.back {
  border-radius: 0 12px 12px 0;
  border-left: 0;
}

.flip-sheet.cover-open .flip-face.back {
  border-right: 0;
  border-radius: 12px 0 0 12px;
}

.mobile-page {
  width: min(100%, calc(var(--scene-h-limit) * (1497 / 2048)));
  aspect-ratio: 1497 / 2048;
  height: auto;
  max-height: var(--scene-h-limit);
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid #d4deea;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.18);
  background: #fff;
  position: relative;
}

.mobile-scene .mobile-page {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-height: none;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform-style: preserve-3d;
  will-change: transform, opacity, filter;
  touch-action: none;
}

.mobile-page.current {
  z-index: 3;
}

.mobile-under-page {
  z-index: 1;
  transform: scale(0.988);
  filter: brightness(0.9);
}

.mobile-page.is-dragging {
  transition: none;
}

@keyframes cover-in {
  from {
    transform: translateY(8px) scale(0.985);
    opacity: 0.72;
  }
  to {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

@keyframes cover-back {
  from {
    transform: translateX(-16px) rotateY(-6deg);
    opacity: 0.85;
  }
  to {
    transform: translateX(0) rotateY(0deg);
    opacity: 1;
  }
}

.mobile-page img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #fff;
}

.mobile-page.slide-next {
  animation: none;
}

.mobile-page.slide-prev {
  animation: none;
}

@keyframes mobile-next {
  from {
    transform: translateX(22px);
    opacity: 0.86;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes mobile-prev {
  from {
    transform: translateX(-22px);
    opacity: 0.86;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

.status-bar {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 0;
  padding: 8px 12px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.34), rgba(255, 255, 255, 0.1)),
    var(--panel);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  color: var(--ink-1);
  font-size: 0.84rem;
  box-shadow: var(--glass-shadow);
  backdrop-filter: blur(14px) saturate(140%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
}

#modeText {
  display: none;
}

@media (max-width: 900px) and (orientation: portrait) {
  body {
    padding: 10px;
  }

  .app-shell {
    min-height: auto;
    gap: 10px;
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar {
    justify-content: space-between;
    flex-wrap: nowrap;
    gap: 8px;
  }

  .toolbar-left,
  .toolbar-right {
    gap: 5px;
  }

  .viewport {
    min-height: 0;
    height: auto;
    padding: 6px;
    perspective: 1800px;
  }

  .page-track {
    padding: 0 6px;
  }

  .edge-button {
    width: 38px;
    height: 38px;
    border-radius: 10px;
  }

  .zoom-text {
    min-width: 42px;
  }

  .mobile-page {
    width: min(100%, calc(100vw - 24px));
    max-height: calc((var(--vh) * 100) - 176px);
  }
}

@media (orientation: landscape) and (max-height: 900px) {
  body {
    padding: 10px;
  }

  .app-shell {
    min-height: calc((var(--vh) * 100) - 20px);
    gap: 8px;
  }

  .topbar {
    padding: 8px 10px;
    border-radius: 14px;
  }

  h1 {
    font-size: clamp(0.86rem, 1.45vw, 1.12rem);
  }

  .toolbar {
    gap: 8px;
  }

  .toolbar-left,
  .toolbar-right {
    gap: 5px;
  }

  .nav-button,
  .jump-button {
    padding: 5px 8px;
    font-size: 0.74rem;
  }

  .page-input {
    padding: 5px 6px;
    font-size: 0.74rem;
  }

  .viewer-layout {
    min-height: 0;
  }

  .viewport {
    padding: 10px;
  }

  .page-track {
    padding: 0 46px;
  }

  .edge-button {
    width: 40px;
    height: 40px;
  }

  .status-bar {
    padding: 6px 10px;
    font-size: 0.76rem;
  }

  .book-scene {
    --ui-stack-height: 92px;
  }
}

@media (orientation: landscape) and (max-height: 700px) {
  .book-scene {
    --ui-stack-height: 78px;
  }
}
