/* ---------- Theme variables ---------- */
:root {
  --bar-h: 52px;
  --foot-h: 44px;
  --page-aspect: 0.72;            /* width / height of a page */
}

body[data-theme="light"] {
  --desk: #e9e6df;
  --desk-deep: #dcd8ce;
  --paper: #fdfdfa;
  --paper-edge: #e4e0d4;
  --ink: #23211d;
  --ink-soft: #6d685e;
  --bar-bg: rgba(250, 249, 246, 0.88);
  --bar-line: rgba(0, 0, 0, 0.10);
  --accent: #a8632a;
  --shadow-strong: rgba(30, 25, 15, 0.35);
}
body[data-theme="sepia"] {
  --desk: #e3d5b8;
  --desk-deep: #d5c5a4;
  --paper: #f7eed9;
  --paper-edge: #e2d4b4;
  --ink: #3c2f1e;
  --ink-soft: #7d6a4d;
  --bar-bg: rgba(244, 234, 213, 0.9);
  --bar-line: rgba(80, 60, 20, 0.14);
  --accent: #8d5a22;
  --shadow-strong: rgba(60, 40, 10, 0.35);
}
body[data-theme="dark"] {
  --desk: #101012;
  --desk-deep: #0a0a0b;
  --paper: #19181a;
  --paper-edge: #232225;
  --ink: #d9d6ce;
  --ink-soft: #8f8b82;
  --bar-bg: rgba(22, 22, 24, 0.88);
  --bar-line: rgba(255, 255, 255, 0.09);
  --accent: #d29a5b;
  --shadow-strong: rgba(0, 0, 0, 0.75);
}

/* ---------- Base layout ---------- */
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(120% 100% at 50% 0%, var(--desk) 0%, var(--desk-deep) 100%);
  color: var(--ink);
  transition: background 0.4s ease;
}
#app { display: flex; flex-direction: column; height: 100dvh; }

/* ---------- Top bar ---------- */
#topbar {
  height: var(--bar-h);
  flex: 0 0 var(--bar-h);
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
  padding: 0 10px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--bar-line);
  z-index: 30;
}
.bar-group { display: flex; align-items: center; gap: 2px; }
.bar-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px;
  border: none; border-radius: 9px;
  background: transparent;
  color: var(--ink);
  font-size: 15px; font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}
.bar-btn:hover { background: rgba(128, 128, 128, 0.15); }
.bar-btn svg { width: 20px; height: 20px; fill: currentColor; }
.text-btn { font-size: 14px; letter-spacing: -0.5px; }

.bar-title {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; align-items: center;
  line-height: 1.25;
}
#titleMain {
  font-weight: 650; font-size: 14px;
  max-width: 100%;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#titleChapter {
  font-size: 11.5px; color: var(--ink-soft);
  max-width: 100%;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- Stage & book ---------- */
#stage {
  flex: 1;
  position: relative;
  display: flex; align-items: center; justify-content: center;
  perspective: 2400px;
  perspective-origin: 50% 45%;
  --avail-h: calc(100dvh - var(--bar-h) - var(--foot-h) - 36px);
}
#bookBox {
  position: relative;
  width: min(92vw, 720px, calc(var(--avail-h) * var(--page-aspect)));
  height: min(var(--avail-h), calc(min(92vw, 720px) / var(--page-aspect)));
  background: var(--paper);
  border-radius: 3px 6px 6px 3px;
  box-shadow:
    0 1px 2px rgba(0,0,0,0.18),
    0 12px 40px -8px var(--shadow-strong),
    0 30px 70px -18px var(--shadow-strong);
  transform-style: preserve-3d;
}

/* stacked page edges on the right + bottom, book thickness illusion */
.page-edges {
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
}
.page-edges::before {              /* right-hand page stack */
  content: "";
  position: absolute;
  top: 2px; bottom: 2px; right: -7px; width: 7px;
  background:
    repeating-linear-gradient(to right,
      var(--paper) 0px, var(--paper) 1px,
      var(--paper-edge) 2px);
  border-radius: 0 3px 3px 0;
  box-shadow: 1px 1px 3px rgba(0,0,0,0.25);
}
.page-edges::after {               /* spine on the left */
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: -5px; width: 5px;
  background: linear-gradient(to right, rgba(0,0,0,0.35), rgba(0,0,0,0.12));
  border-radius: 3px 0 0 3px;
}

/* ---------- Flippable sheets ---------- */
.sheet {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  transform-origin: left center;
  border-radius: inherit;
  z-index: 2;
  will-change: transform;
}
.face {
  position: absolute; inset: 0;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  background: var(--paper);
  border-radius: inherit;
  overflow: hidden;
}
.face.back {
  transform: rotateY(180deg);
  background:
    linear-gradient(to left, rgba(0,0,0,0.16), rgba(0,0,0,0.03) 22%, rgba(0,0,0,0) 55%),
    var(--paper);
}
.rend { position: absolute; inset: 0; }
.rend iframe { border: none; }

/* soft gutter shadow near the spine, on every page */
.spine-shade {
  position: absolute; inset: 0;
  pointer-events: none;
  background: linear-gradient(to right, rgba(0,0,0,0.10), rgba(0,0,0,0.02) 5%, rgba(0,0,0,0) 10%);
}

/* darkening on the lifting page, driven by JS */
.sheet-shade {
  position: absolute; inset: 0;
  pointer-events: none;
  opacity: 0;
  border-radius: inherit;
  background: linear-gradient(to right, rgba(0,0,0,0.38), rgba(0,0,0,0.06) 55%, rgba(0,0,0,0) 80%);
}

/* shadow the flipping page casts on the one beneath */
#underShadow {
  position: absolute; inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: 4;
  border-radius: inherit;
  background: linear-gradient(to right, rgba(0,0,0,0.42), rgba(0,0,0,0.10) 40%, rgba(0,0,0,0) 72%);
}

/* ---------- Flip zones ---------- */
.flip-zone {
  position: absolute; top: 0; bottom: 0;
  width: 18%;
  min-width: 44px;
  z-index: 10;
  display: flex; align-items: center;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.flip-zone.left  { left: 0;  justify-content: flex-start; }
.flip-zone.right { right: 0; justify-content: flex-end; }
.flip-zone span {
  font-size: 34px; line-height: 1;
  color: var(--ink);
  opacity: 0;
  padding: 0 6px;
  transition: opacity 0.2s;
}
@media (hover: hover) {
  .flip-zone:hover span { opacity: 0.35; }
}

/* ---------- Loading ---------- */
#loading {
  position: absolute; inset: 0;
  z-index: 20;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 14px;
  background: var(--paper);
  border-radius: inherit;
  color: var(--ink-soft);
  font-size: 14px;
}
.spinner {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 3px solid var(--bar-line);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Bottom bar ---------- */
#bottombar {
  height: var(--foot-h);
  flex: 0 0 var(--foot-h);
  display: flex; align-items: center;
  gap: 12px;
  padding: 0 16px calc(env(safe-area-inset-bottom, 0px) / 2);
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--bar-line);
  z-index: 30;
}
.progress-label {
  font-size: 11.5px; color: var(--ink-soft);
  min-width: 34px;
  font-variant-numeric: tabular-nums;
}
#progressPage { text-align: right; min-width: 70px; }
#progressSlider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  border-radius: 2px;
  background: var(--bar-line);
  outline: none;
  cursor: pointer;
}
#progressSlider:disabled { cursor: default; opacity: 0.4; }
#progressSlider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent);
  border: none;
}
#progressSlider::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent);
  border: none;
}

/* ---------- TOC drawer ---------- */
#drawerOverlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.25s;
}
#drawerOverlay.open { opacity: 1; }
#tocDrawer {
  position: fixed; top: 0; bottom: 0; left: 0;
  width: min(320px, 85vw);
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  border-right: 1px solid var(--bar-line);
  z-index: 41;
  display: flex; flex-direction: column;
  transform: translateX(-102%);
  transition: transform 0.28s cubic-bezier(0.3, 0.8, 0.3, 1);
  box-shadow: 12px 0 40px rgba(0,0,0,0.25);
}
#tocDrawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 10px 12px 18px;
  border-bottom: 1px solid var(--bar-line);
}
.drawer-head h2 { margin: 0; font-size: 16px; }
.drawer-head .bar-btn { font-size: 22px; }
#tocList { flex: 1; overflow-y: auto; padding: 6px 0 20px; }
#tocList a {
  display: block;
  padding: 11px 18px;
  font-size: 13.5px;
  color: var(--ink);
  text-decoration: none;
  border-left: 3px solid transparent;
  cursor: pointer;
}
#tocList a.sub { padding-left: 34px; font-size: 12.5px; color: var(--ink-soft); }
#tocList a:hover { background: rgba(128,128,128,0.12); }
#tocList a.current {
  border-left-color: var(--accent);
  background: rgba(128,128,128,0.10);
  font-weight: 600;
}

/* ---------- Settings panel ---------- */
#settingsPanel {
  position: fixed;
  top: calc(var(--bar-h) + 8px);
  right: 10px;
  width: 230px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  border: 1px solid var(--bar-line);
  border-radius: 14px;
  box-shadow: 0 16px 50px rgba(0,0,0,0.3);
  padding: 14px 16px;
  z-index: 42;
}
#settingsPanel h3 {
  margin: 10px 0 8px;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px;
  color: var(--ink-soft);
}
#settingsPanel h3:first-child { margin-top: 0; }
.theme-chips { display: flex; gap: 6px; }
.chip {
  flex: 1;
  padding: 7px 0;
  border: 1px solid var(--bar-line);
  border-radius: 9px;
  background: transparent;
  color: var(--ink);
  font-size: 12.5px;
  cursor: pointer;
}
.chip.active { border-color: var(--accent); color: var(--accent); font-weight: 650; }
.opt-row {
  display: flex; align-items: center; gap: 9px;
  font-size: 13.5px;
  padding: 6px 0;
  cursor: pointer;
}
.opt-row input { accent-color: var(--accent); }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%; bottom: calc(var(--foot-h) + 18px);
  transform: translateX(-50%);
  background: rgba(20, 20, 22, 0.85);
  color: #f0efe9;
  font-size: 13px;
  padding: 9px 18px;
  border-radius: 20px;
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
}
#toast.show { opacity: 1; }

/* ---------- Embed mode ---------- */
body.embed #shelfBtn { display: none; }

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  #stage { --avail-h: calc(100dvh - var(--bar-h) - var(--foot-h)); }
  #bookBox {
    width: 100vw;
    height: var(--avail-h);
    border-radius: 0;
    box-shadow: none;
  }
  .page-edges::before, .page-edges::after { display: none; }
  #titleMain { font-size: 13px; }
  .bar-btn { width: 34px; height: 34px; }
  #fsBtn { display: none; }
}

[hidden] { display: none !important; }
