:root {
  --paper: #f7f3ea;
  --paper-deep: #eee6d6;
  --ink: #3b372d;
  --muted: #857f70;
  --green: #78a96c;
  --pink: #e69aaa;
  --line: rgba(86, 74, 53, 0.14);
  --flip-duration: 850ms;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  background:
    radial-gradient(1100px 700px at 18% 12%, rgba(255, 255, 255, 0.9), transparent 60%),
    radial-gradient(900px 650px at 85% 88%, rgba(224, 205, 174, 0.45), transparent 62%),
    linear-gradient(160deg, #f3eddf, #e9e0ce);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.35;
  background-image: repeating-linear-gradient(0deg, rgba(120, 105, 80, 0.035) 0 1px, transparent 1px 28px);
}

.app {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: 16px clamp(12px, 3vw, 36px) 18px;
}

/* ---------- top bar ---------- */
.topbar, .bottombar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  background: rgba(255, 253, 247, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0 8px 30px rgba(92, 72, 42, 0.08);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-radius: 16px;
  padding: 10px 16px;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand-icon {
  position: relative;
  display: block;
  width: 32px;
  height: 26px;
  flex: none;
  transform: rotate(-6deg);
}
.brand-icon i {
  position: absolute;
  top: 1px;
  width: 17px;
  height: 24px;
  border-radius: 3px 7px 7px 3px;
  box-shadow: 0 2px 6px rgba(80, 60, 40, 0.18);
}
.brand-icon i:first-child { left: 0; background: var(--green); }
.brand-icon i:last-child { right: 0; background: var(--pink); transform: rotate(7deg); }

.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-family: "STKaiti", "KaiTi", "Songti SC", serif;
  font-size: clamp(17px, 2.2vw, 22px);
  font-weight: 700;
  letter-spacing: 0.12em;
  white-space: nowrap;
}
.brand-text p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.08em;
  white-space: nowrap;
}

.top-actions { display: flex; gap: 8px; flex: none; }

.icon-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.65);
  color: #6b6455;
  cursor: pointer;
  transition: transform 0.18s, background 0.18s, color 0.18s;
}
.icon-btn:hover { background: #fff; color: #2f6f4b; transform: translateY(-1px); }
.icon-btn svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- stage + book ---------- */
.stage {
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
}

.book-wrap {
  position: relative;
  width: min(92vw, 1180px, calc((100vh - 230px) * 1.4188));
  aspect-ratio: 2480 / 1748;
  perspective: 2400px;
  perspective-origin: 50% 45%;
  filter: drop-shadow(0 22px 34px rgba(70, 52, 28, 0.24));
}
@supports (height: 100dvh) {
  .book-wrap { width: min(92vw, 1180px, calc((100dvh - 230px) * 1.4188)); }
}

.book {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
}

.page-stack {
  position: absolute;
  inset: 0;
  z-index: 0;
  transform: translate(7px, 7px);
  border-radius: 3px 11px 11px 3px;
  background: #fdfaf2;
  box-shadow: 3px 3px 0 #f6f0e4, 6px 6px 0 #ece4d4, 9px 9px 16px rgba(70, 52, 28, 0.14);
}
.page-stack::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: repeating-linear-gradient(90deg, rgba(120, 100, 70, 0.07) 0 1px, transparent 1px 5px);
  opacity: 0.5;
}

.underlay, .face {
  position: absolute;
  inset: 0;
  border-radius: 3px 9px 9px 3px;
  overflow: hidden;
  background: #fffdf8;
}
.underlay { z-index: 1; box-shadow: inset 0 0 0 1px rgba(70, 55, 35, 0.08); }
.underlay img, .face img { display: block; width: 100%; height: 100%; object-fit: contain; background: #fffdf8; }

.flip-sheet {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: none;
  transform-origin: left center;
  transform-style: preserve-3d;
  will-change: transform;
  transform: rotateY(0deg);
  box-shadow: 0 8px 24px rgba(60, 45, 25, 0.16);
}
.flip-sheet.is-active { display: block; }

.face {
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.face.back { transform: rotateY(180deg); }

.fold-shade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
}
.flip-sheet.forward .face.front .fold-shade { animation: shadeFrontForward var(--flip-duration) ease-in-out both; }
.flip-sheet.forward .face.back .fold-shade { animation: shadeBackForward var(--flip-duration) ease-in-out both; }
.flip-sheet.backward .face.front .fold-shade { animation: shadeFrontBackward var(--flip-duration) ease-in-out both; }
.flip-sheet.backward .face.back .fold-shade { animation: shadeBackBackward var(--flip-duration) ease-in-out both; }

@keyframes shadeFrontForward { from { opacity: 0; } to { opacity: 0.72; } }
@keyframes shadeBackForward { from { opacity: 0.72; } to { opacity: 0; } }
@keyframes shadeFrontBackward { from { opacity: 0.72; } to { opacity: 0; } }
@keyframes shadeBackBackward { from { opacity: 0; } to { opacity: 0.72; } }

.spine {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 26px;
  z-index: 5;
  pointer-events: none;
  border-radius: 3px 0 0 3px;
  background: linear-gradient(90deg, rgba(46, 33, 18, 0.26), rgba(46, 33, 18, 0.08) 45%, transparent 100%);
}

.zone {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  z-index: 6;
  display: flex;
  align-items: center;
  cursor: pointer;
  outline-offset: -4px;
}
.zone-prev { left: 0; justify-content: flex-start; padding-left: 20px; }
.zone-next { right: 0; justify-content: flex-end; padding-right: 20px; }
body.is-flipping .zone { pointer-events: none; }

.zone-hint {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 11px;
  border-radius: 999px;
  background: rgba(255, 253, 247, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 4px 14px rgba(70, 52, 28, 0.16);
  color: #5c5649;
  font-size: 12px;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
.zone:hover .zone-hint { opacity: 1; transform: translateY(0); }
.zone-hint b { font-size: 26px; line-height: 0.8; font-weight: 400; }

.tap-tip {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.06em;
  text-align: center;
}

/* ---------- bottom bar ---------- */
.bottombar { justify-content: center; }

.nav-btn {
  flex: none;
  min-width: 112px;
  padding: 9px 15px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.7);
  color: #514a3d;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.18s, transform 0.18s, opacity 0.18s;
}
.nav-btn:hover:not(:disabled) { background: #fff; transform: translateY(-1px); }
.nav-btn:disabled { opacity: 0.38; cursor: default; }

.progress-wrap {
  flex: 1;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.progress-track {
  position: relative;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: rgba(105, 88, 60, 0.14);
  cursor: pointer;
  outline-offset: 5px;
}
.progress-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 7.14%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--green), var(--pink));
  transition: width 0.35s ease;
}
.ticks { position: absolute; inset: 0; pointer-events: none; }
.ticks i {
  position: absolute;
  top: 50%;
  width: 3px;
  height: 3px;
  margin: -1.5px 0 0 -1.5px;
  border-radius: 50%;
  background: rgba(80, 66, 45, 0.28);
}
.counter { display: flex; align-items: baseline; gap: 6px; font-variant-numeric: tabular-nums; }
.counter strong { font-size: 19px; font-weight: 700; color: #3f6e4d; }
.counter span { font-size: 12px; color: var(--muted); }
.counter em { margin-left: 8px; font-style: normal; font-size: 12px; color: var(--muted); }

/* ---------- overlays ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + env(safe-area-inset-bottom));
  z-index: 30;
  max-width: min(90vw, 480px);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(49, 44, 36, 0.9);
  color: #fffaf0;
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
  box-shadow: 0 10px 28px rgba(40, 30, 16, 0.24);
  opacity: 0;
  transform: translate(-50%, 8px);
  pointer-events: none;
  transition: opacity 0.22s, transform 0.22s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.loader {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: #f5efe3;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: 0.08em;
  transition: opacity 0.35s;
}
.loader.hidden { opacity: 0; pointer-events: none; }
.loader p { margin: 0; }
.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid rgba(120, 169, 108, 0.25);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

:fullscreen .app { background: rgba(246, 240, 229, 0.96); }
:fullscreen .book-wrap { width: min(96vw, calc((100vh - 200px) * 1.4188)); }

@media (max-width: 680px) {
  .app { padding: 10px 10px 12px; gap: 6px; }
  .topbar, .bottombar { padding: 8px 11px; border-radius: 13px; }
  .brand-text p { display: none; }
  .icon-btn { width: 32px; height: 32px; }
  .book-wrap { width: min(96vw, calc((100vh - 190px) * 1.4188)); }
  @supports (height: 100dvh) {
    .book-wrap { width: min(96vw, calc((100dvh - 190px) * 1.4188)); }
  }
  .spine { width: 16px; }
  .zone-prev { padding-left: 10px; }
  .zone-next { padding-right: 10px; }
  .zone-hint { display: none; }
  .nav-btn { min-width: 76px; padding: 8px 10px; font-size: 12px; }
  .counter em { display: none; }
  .tap-tip { font-size: 11px; }
  :fullscreen .book-wrap { width: min(98vw, calc((100vh - 170px) * 1.4188)); }
}

@media (prefers-reduced-motion: reduce) {
  :root { --flip-duration: 200ms; }
  .progress-fill, .toast, .loader, .zone-hint { transition-duration: 0.01ms; }
}
