:root {
  --navy: #101a3d;
  --navy-2: #172a63;
  --paper: #fff;
  --ink: #172039;
  --red: #d6232a;
  --magenta: #c21285;
  --cyan: #0da9d8;
  --green: #3bb24a;
  --orange: #fb7d1a;
  --yellow: #ffe500;
  --ui-h: 64px;
  --footer-h: 68px;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  color: #fff;
  background:
    radial-gradient(circle at 50% 18%, rgba(44, 82, 170, .42), transparent 36%),
    linear-gradient(145deg, #071027, var(--navy) 60%, #081431);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

/* ---------- PIN Gate ---------- */
#gate {
  position: fixed; inset: 0; z-index: 999; display: flex; align-items: center; justify-content: center; padding: 24px;
  background:
    radial-gradient(circle at 50% 18%, rgba(44, 82, 170, .42), transparent 36%),
    linear-gradient(145deg, #071027, var(--navy) 60%, #081431);
}
#gate.hidden { display: none; }
.gate-card { width: min(92vw, 360px); background: rgba(10,20,37,.92); border: 1px solid rgba(255,255,255,.13); border-radius: 20px; padding: 34px 26px; text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,.5); color: #fff; }
.gate-card h2 { margin: 0 0 8px; font-size: 19px; letter-spacing: .02em; }
.gate-card p { margin: 0 0 20px; color: #9aa8bd; font-size: 13.5px; line-height: 1.5; }
#gatePin { width: 100%; font-size: 1.1rem; letter-spacing: .16em; text-align: center; padding: .8rem; border-radius: 12px; border: 1px solid rgba(255,255,255,.13); background: rgba(255,255,255,.05); color: #fff; margin-bottom: 14px; }
#gatePin:focus { outline: none; border-color: var(--cyan); box-shadow: 0 0 0 4px rgba(13,169,216,.16); }
#gateBtn { width: 100%; padding: .8rem; border-radius: 999px; border: none; background: linear-gradient(135deg, var(--cyan), var(--magenta)); color: #07101d; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; cursor: pointer; font-size: 13.5px; }
#gateBtn:active { transform: translateY(1px); }
#gateErr { color: #ff8b8b; font-size: .8rem; margin-top: 10px; min-height: 1em; }

/* ---------- Landing ---------- */
#landing {
  position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 28px 20px;
  background:
    radial-gradient(circle at 50% 18%, rgba(44, 82, 170, .42), transparent 36%),
    linear-gradient(145deg, #071027, var(--navy) 60%, #081431);
  text-align: center;
}
#landing.hidden { display: none; }
.landing-inner { max-width: 480px; }
.landing-cover-wrap { width: min(200px, 46vw); margin: 0 auto 22px; border-radius: 12px; overflow: hidden; box-shadow: 0 24px 48px rgba(0,0,0,.5); }
.landing-cover-wrap img { display: block; width: 100%; height: auto; }
.landing-kicker { font-size: 11px; letter-spacing: .32em; color: #b8c8ff; margin-bottom: 14px; }
.landing-title { font-size: clamp(38px, 9vw, 58px); line-height: .98; letter-spacing: -.03em; margin: 0 0 6px; }
.landing-title em { font-family: cursive; font-weight: 500; color: #e4ebff; }
.landing-author { font-size: 13px; color: #cbd7ff; letter-spacing: .08em; margin: 0 0 18px; text-transform: uppercase; }
.landing-desc { font-size: 15px; line-height: 1.65; color: #d7e0ff; margin: 0 0 28px; }
.landing-cta { display: flex; flex-direction: column; gap: 12px; }
.landing-btn {
  height: 52px; border-radius: 14px; border: 1px solid rgba(255,255,255,.18); cursor: pointer;
  font-size: 14.5px; font-weight: 700; letter-spacing: .02em;
}
.landing-btn.primary { background: linear-gradient(120deg, var(--cyan), var(--magenta)); color: #fff; border: none; box-shadow: 0 14px 30px rgba(13,169,216,.28); }
.landing-btn.ghost { background: rgba(255,255,255,.06); color: #fff; }
.landing-btn.hidden { display: none; }
.landing-meter { margin-top: 18px; font-size: 12px; color: #9fb0dd; }

.app-shell { min-height: 100%; display: grid; grid-template-rows: var(--ui-h) 1fr var(--footer-h); }
.app-shell.hidden { display: none; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 14px; padding: max(8px, env(safe-area-inset-top)) 14px 8px;
  background: rgba(6, 13, 35, .72); border-bottom: 1px solid rgba(255,255,255,.12);
  backdrop-filter: blur(18px); z-index: 20;
}
.brand { text-align: center; line-height: 1; }
.brand-kicker { font-size: 9px; letter-spacing: .32em; color: #b8c8ff; margin-bottom: 3px; }
.brand h1 { font-size: clamp(18px, 3.4vw, 25px); margin: 0; letter-spacing: -.04em; }
.brand h1 em { font-family: cursive; font-weight: 500; color: #e4ebff; }
.top-actions { display: flex; gap: 6px; }
.icon-btn {
  width: 42px; height: 42px; display: inline-grid; place-items: center;
  border: 1px solid rgba(255,255,255,.16); border-radius: 12px;
  color: #fff; background: rgba(255,255,255,.08); font-size: 22px; cursor: pointer;
}
.icon-btn:hover, .icon-btn:focus-visible { background: rgba(255,255,255,.16); outline: none; }
.pill-btn {
  height: 42px; padding: 0 16px; display: inline-flex; align-items: center; gap: 7px;
  border: 1px solid rgba(255,255,255,.16); border-radius: 999px;
  color: #fff; background: rgba(255,255,255,.08); font-size: 13px; font-weight: 700; letter-spacing: .02em; cursor: pointer;
}
.pill-btn:hover, .pill-btn:focus-visible { background: rgba(255,255,255,.16); outline: none; }

.reader { min-height: 0; position: relative; display: grid; grid-template-columns: 56px 1fr 56px; align-items: center; }
.book-stage { min-width: 0; min-height: 0; height: 100%; display: grid; place-items: center; padding: 16px 10px 34px; perspective: 2200px; }
.book {
  --book-w: min(1140px, calc(100vw - 150px), calc((100vh - var(--ui-h) - var(--footer-h) - 72px) * 2));
  --book-zoom: 1;
  width: var(--book-w); height: calc(var(--book-w) / 2);
  max-height: calc(100vh - var(--ui-h) - var(--footer-h) - 72px);
  position: relative; display: grid; grid-template-columns: 1fr 1fr;
  filter: drop-shadow(0 26px 45px rgba(0,0,0,.48)); transform-style: preserve-3d;
  transform: scale(var(--book-zoom)); transition: transform .2s ease;
}
.page-slot {
  position: relative; overflow: hidden; background: #f9f9f7;
  box-shadow: inset 0 0 25px rgba(0,0,0,.06);
}
.page-left { border-radius: var(--radius) 0 0 var(--radius); }
.page-right { border-radius: 0 var(--radius) var(--radius) 0; }
.page-slot.blank { background: linear-gradient(90deg, #e8e8e4, #f7f7f3); }
.page-slot img, .sheet-face img { width: 100%; height: 100%; object-fit: contain; display: block; background: #fff; user-select: none; -webkit-user-drag: none; }
.book-gutter {
  z-index: 8; position: absolute; left: 50%; top: 0; bottom: 0; width: 30px; transform: translateX(-50%); pointer-events: none;
  background: linear-gradient(90deg, transparent, rgba(0,0,0,.12) 48%, rgba(255,255,255,.38) 52%, transparent);
  mix-blend-mode: multiply; opacity: .8;
}
.page-slot::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(90deg, rgba(0,0,0,.08), transparent 10%, transparent 90%, rgba(0,0,0,.08)); opacity: .35;
}

.flip-sheet {
  display: none; position: absolute; top: 0; width: 50%; height: 100%; z-index: 12;
  transform-style: preserve-3d; will-change: transform;
}
.flip-sheet.active { display: block; }
.flip-sheet.next { left: 50%; transform-origin: left center; animation: flipNext .72s cubic-bezier(.28,.74,.2,1) forwards; }
.flip-sheet.prev { left: 0; transform-origin: right center; animation: flipPrev .72s cubic-bezier(.28,.74,.2,1) forwards; }
.sheet-face { position: absolute; inset: 0; overflow: hidden; backface-visibility: hidden; background: #fff; }
.sheet-front { border-radius: 0 var(--radius) var(--radius) 0; }
.sheet-back { transform: rotateY(180deg); border-radius: var(--radius) 0 0 var(--radius); }
.flip-sheet.prev .sheet-front { border-radius: var(--radius) 0 0 var(--radius); }
.flip-sheet.prev .sheet-back { border-radius: 0 var(--radius) var(--radius) 0; }
@keyframes flipNext { 0% { transform: rotateY(0); } 100% { transform: rotateY(-180deg); } }
@keyframes flipPrev { 0% { transform: rotateY(0); } 100% { transform: rotateY(180deg); } }

.hint {
  position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
  padding: 7px 12px; border-radius: 99px; background: rgba(0,0,0,.26); color: #cbd7ff;
  font-size: 12px; opacity: .78; white-space: nowrap; transition: opacity .25s;
}
.hint.hide { opacity: 0; }
.nav-edge {
  align-self: stretch; border: 0; background: transparent; color: rgba(255,255,255,.78); cursor: pointer; font-size: 54px;
  transition: background .2s, color .2s;
}
.nav-edge:hover { color: #fff; background: rgba(255,255,255,.05); }
.nav-edge:disabled { opacity: .18; cursor: default; }

.controlbar {
  display: grid; grid-template-columns: minmax(140px, 1fr) auto auto; align-items: center; gap: 16px;
  padding: 8px max(14px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: rgba(6, 13, 35, .82); border-top: 1px solid rgba(255,255,255,.12); backdrop-filter: blur(18px); z-index: 20;
}
.control-btn {
  height: 42px; border-radius: 11px; border: 1px solid rgba(255,255,255,.16); color: #fff; background: rgba(255,255,255,.08); padding: 0 14px; cursor: pointer;
}
.control-btn:hover { background: rgba(255,255,255,.16); }
.control-btn:disabled { opacity: .28; cursor: default; }
.progress-group { display: flex; align-items: center; gap: 12px; min-width: 0; }
.progress-range { flex: 1; min-width: 0; appearance: none; height: 5px; border-radius: 99px; background: rgba(255,255,255,.14); outline: none; }
.progress-range::-webkit-slider-thumb { appearance: none; width: 16px; height: 16px; border-radius: 99px; background: linear-gradient(120deg, var(--cyan), var(--magenta)); border: none; cursor: pointer; }
.progress-range::-moz-range-thumb { width: 16px; height: 16px; border: none; border-radius: 99px; background: linear-gradient(120deg, var(--cyan), var(--magenta)); cursor: pointer; }
.page-stat { flex: 0 0 auto; font-size: 12.5px; color: #cbd7ff; white-space: nowrap; }
.page-control { display: flex; align-items: center; gap: 7px; font-size: 13px; color: #cbd7ff; }
.page-control input {
  width: 62px; height: 38px; border-radius: 10px; border: 1px solid rgba(255,255,255,.16); background: rgba(0,0,0,.22); color: #fff; text-align: center;
}
.footer-actions { display: flex; gap: 8px; }
.footer-actions .control-btn { display: flex; align-items: center; gap: 6px; font-size: 12.5px; white-space: nowrap; }

.drawer {
  position: fixed; z-index: 50; inset: 0 auto 0 0; width: min(390px, 90vw); transform: translateX(-102%);
  display: grid; grid-template-rows: auto 1fr; background: #f7f8fc; color: var(--ink); box-shadow: 24px 0 70px rgba(0,0,0,.5); transition: transform .28s ease;
}
.drawer.open { transform: translateX(0); }
.drawer-wide { width: min(760px, 94vw); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  color: #fff; background: linear-gradient(125deg, var(--navy-2), #293f8f);
}
.drawer-head small { font-size: 10px; letter-spacing: .22em; color: #bac8ff; }
.drawer-head h2 { margin: 3px 0 0; font-size: 22px; }
.toc-list { overflow: auto; padding: 10px 12px 30px; }
.toc-group { margin: 14px 8px 7px; font-size: 10px; letter-spacing: .18em; font-weight: 800; color: #65708d; }
.toc-item {
  width: 100%; display: grid; grid-template-columns: 40px 1fr auto; align-items: center; gap: 10px; padding: 10px;
  border: 0; border-bottom: 1px solid #e0e4ee; background: transparent; color: inherit; text-align: left; cursor: pointer; border-radius: 9px;
}
.toc-item:hover { background: #e9edfa; }
.toc-no { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; color: #fff; font-weight: 800; background: var(--red); }
.toc-title { font-size: 14px; font-weight: 700; }
.toc-page { color: #7c86a1; font-size: 12px; }
.thumb-grid { overflow: auto; padding: 14px; display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 12px; align-content: start; }
.thumb {
  border: 2px solid transparent; border-radius: 10px; padding: 0; overflow: hidden; background: #fff; box-shadow: 0 5px 16px rgba(22,32,57,.14); cursor: pointer; color: var(--ink); position: relative;
}
.thumb:hover, .thumb.current { border-color: var(--magenta); }
.thumb img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.thumb span { position: absolute; right: 5px; bottom: 5px; min-width: 25px; padding: 3px 6px; border-radius: 99px; background: rgba(9,15,40,.82); color: #fff; font-size: 11px; }
.scrim { position: fixed; z-index: 45; inset: 0; background: rgba(0,0,0,.56); backdrop-filter: blur(3px); }

@media (max-width: 760px) {
  :root { --ui-h: 58px; --footer-h: 64px; --radius: 10px; }
  .reader { grid-template-columns: 34px 1fr 34px; }
  .book-stage { padding: 10px 0 30px; }
  .book {
    --book-w: min(calc(100vw - 72px), calc(100vh - var(--ui-h) - var(--footer-h) - 58px));
    width: var(--book-w); height: var(--book-w); grid-template-columns: 1fr;
  }
  .page-left { display: none; }
  .page-right { grid-column: 1; border-radius: var(--radius); }
  .book-gutter { display: none; }
  .flip-sheet { width: 100%; }
  .flip-sheet.next, .flip-sheet.prev { left: 0; }
  .sheet-front, .sheet-back, .flip-sheet.prev .sheet-front, .flip-sheet.prev .sheet-back { border-radius: var(--radius); }
  .nav-edge { font-size: 40px; padding: 0; }
  .hint { max-width: calc(100vw - 90px); overflow: hidden; text-overflow: ellipsis; }
  .controlbar { grid-template-columns: 1fr auto; grid-template-rows: auto auto; row-gap: 8px; column-gap: 10px; padding-top: 6px; }
  .progress-group { grid-column: 1 / -1; grid-row: 1; }
  .page-control { grid-column: 1; grid-row: 2; justify-content: flex-start; }
  .footer-actions { grid-column: 2; grid-row: 2; }
  .footer-actions .control-btn .label { display: none; }
  .footer-actions .control-btn { padding: 0 10px; }
  .brand-kicker { display: none; }
  .landing-cta { max-width: 320px; margin: 0 auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
