/* ==========================================================================
   Book reader: page canvas, thumbnails rail, toolbar, text layer, search
   ========================================================================== */

.reader {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  gap: 0;
  height: calc(100vh - var(--header-h));
  background: var(--paper-2);
  overflow: hidden;
}

/* --- rail --------------------------------------------------------------- */
.rail {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-height: 0;
  z-index: 3;
}
.rail-head {
  padding: 13px 15px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.rail-head .rh-title { font-weight: 700; font-size: .92rem; line-height: 1.25; }
.rail-head .rh-sub { font-size: .74rem; color: var(--ink-3); }
.rail-tabs {
  display: flex; gap: 2px; padding: 8px 10px 0;
  border-bottom: 1px solid var(--border);
}
.rail-tabs button {
  flex: 1; border: 0; background: transparent; padding: 8px 6px;
  font-size: .78rem; font-weight: 700; color: var(--ink-3);
  border-bottom: 2.5px solid transparent; border-radius: 0;
  transition: color .15s var(--ease), border-color .15s var(--ease);
}
.rail-tabs button:hover { color: var(--ink); }
.rail-tabs button[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }

.rail-body { flex: 1; overflow-y: auto; min-height: 0; padding: 10px; }
.rail-pane { display: none; }
.rail-pane.active { display: block; }

/* thumbnail strip */
.thumb {
  display: flex; align-items: center; gap: 10px;
  padding: 7px; border-radius: var(--r-sm);
  border: 1.5px solid transparent;
  cursor: pointer; background: transparent; width: 100%; text-align: left;
  transition: background .14s var(--ease), border-color .14s var(--ease);
}
.thumb:hover { background: var(--surface-2); }
.thumb.is-current { border-color: var(--brand); background: var(--brand-soft); }
.thumb img {
  width: 52px; height: 70px; object-fit: cover; object-position: top;
  border-radius: 5px; border: 1px solid var(--border); flex: none;
  background: var(--surface-2);
}
.thumb .tn { font-size: .8rem; font-weight: 700; color: var(--ink-2); }
.thumb .ts { font-size: .7rem; color: var(--ink-4); }
.thumb .tn .dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--ok); margin-left: 5px; vertical-align: middle;
}

/* contents (TOC) list */
.toc-list { display: flex; flex-direction: column; gap: 2px; }
.toc-item {
  display: flex; gap: 10px; align-items: baseline;
  padding: 8px 10px; border-radius: var(--r-sm);
  text-decoration: none; color: var(--ink-2);
  border-left: 2.5px solid transparent;
  transition: background .14s var(--ease);
}
.toc-item:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.toc-item.is-active { background: var(--brand-soft); color: var(--brand); border-left-color: var(--brand); font-weight: 700; }
.toc-item .tnum {
  flex: none; min-width: 2.1em; font-size: .74rem; font-weight: 700;
  color: var(--ink-4); font-variant-numeric: tabular-nums;
}
.toc-item .ttl { font-size: .86rem; line-height: 1.35; }
.toc-item .tpg { margin-left: auto; font-size: .72rem; color: var(--ink-4); font-variant-numeric: tabular-nums; }

/* search inside the book */
.rail-search { padding: 10px; border-bottom: 1px solid var(--border); }
.rail-search input {
  width: 100%; padding: 8px 11px; border-radius: var(--r-sm);
  border: 1.5px solid var(--border); background: var(--surface-2); font-size: .84rem;
}
.rail-search input:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.hit {
  display: block; width: 100%; text-align: left;
  padding: 9px 11px; border-radius: var(--r-sm); border: 1px solid transparent;
  background: transparent; margin-bottom: 3px;
  transition: background .14s var(--ease), border-color .14s var(--ease);
}
.hit:hover { background: var(--surface-2); border-color: var(--border); }
.hit .hpg { font-size: .7rem; font-weight: 700; color: var(--accent); }
.hit .htx { font-size: .8rem; color: var(--ink-2); line-height: 1.4; }
.hit mark { background: var(--gold-soft); color: inherit; padding: 0 2px; border-radius: 3px; font-weight: 700; }

/* --- canvas ------------------------------------------------------------- */
.canvas {
  position: relative; overflow: hidden;
  background:
    radial-gradient(circle at 50% 0%, color-mix(in srgb, var(--border) 40%, transparent), transparent 70%),
    var(--paper-2);
  display: flex; flex-direction: column;
  min-width: 0;
}

.canvas-viewport {
  flex: 1; position: relative; overflow: hidden;
  cursor: grab;
  touch-action: none;
  min-height: 0;
}
.canvas-viewport.is-panning { cursor: grabbing; }
.canvas-viewport.mode-text { cursor: text; }

.page-stack {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  will-change: transform;
}
.page-stack img {
  max-width: 100%; max-height: 100%;
  width: auto; height: auto;
  object-fit: contain;
  box-shadow: var(--sh-4);
  background: #fff;
  border-radius: 3px;
  user-select: none;
  -webkit-user-drag: none;
}
.page-stack.two-up { display: flex; gap: 14px; align-items: center; justify-content: center; }
.page-stack.two-up img { max-width: 48.5%; }

/* overlay for OCR text (searchable, selectable, copyable) */
.text-layer {
  position: absolute; pointer-events: none;
  opacity: 0; transition: opacity .2s var(--ease);
}
.text-layer.on { opacity: 1; pointer-events: auto; }
.text-layer .tl-page {
  position: absolute; background: color-mix(in srgb, var(--surface) 97%, transparent);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0;
  box-shadow: var(--sh-4);
  overflow: auto;
  white-space: pre-wrap;
  font-size: .82rem; line-height: 1.5;
  color: var(--ink);
}
.text-layer .tl-page h5 {
  position: sticky; top: 0; margin: 0; padding: 7px 12px;
  background: var(--surface-2); border-bottom: 1px solid var(--border);
  font-size: .7rem; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3);
}
.text-layer .tl-page pre {
  margin: 0; padding: 12px 14px; font-family: var(--sans); font-size: .84rem;
  white-space: pre-wrap; word-break: break-word;
}

/* highlight ring shown while flashing a search hit */
.flash { animation: flashPulse 1.15s var(--ease) 2; }
@keyframes flashPulse {
  0%, 100% { box-shadow: var(--sh-4); }
  50% { box-shadow: 0 0 0 5px var(--gold), var(--sh-4); }
}

/* page loading spinner */
.page-loading {
  position: absolute; inset: 0; display: grid; place-items: center;
  pointer-events: none;
}
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* edge click zones for page turning */
.edge {
  position: absolute; top: 0; bottom: 0; width: 9%;
  min-width: 54px; max-width: 130px;
  border: 0; background: transparent; z-index: 2;
  display: grid; place-items: center;
  color: var(--ink); opacity: 0;
  transition: opacity .18s var(--ease);
}
.edge:hover { opacity: 1; background: linear-gradient(var(--dir, 90deg),
  color-mix(in srgb, var(--ink) 14%, transparent), transparent); }
.edge-prev { left: 0; --dir: 90deg; }
.edge-next { right: 0; --dir: 270deg; }
.edge .eic {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--border);
  display: grid; place-items: center; box-shadow: var(--sh-2);
}
.edge:disabled { display: none; }

/* --- toolbar ------------------------------------------------------------ */
.reader-toolbar {
  flex: none;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.tb-group { display: flex; align-items: center; gap: 4px; }
.tb-sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
.tb-page {
  display: flex; align-items: center; gap: 6px;
  font-size: .84rem; font-weight: 600; color: var(--ink-2);
}
.tb-page input {
  width: 54px; padding: 6px 8px; text-align: center;
  border-radius: var(--r-sm); border: 1.5px solid var(--border);
  background: var(--surface); font-variant-numeric: tabular-nums;
}
.tb-page input:focus { outline: none; border-color: var(--accent); }
.zoom-label { font-size: .76rem; color: var(--ink-3); min-width: 44px; text-align: center; font-variant-numeric: tabular-nums; }

/* mobile: rail collapses to a sheet */
.rail-toggle { display: none; }

@media (max-width: 1000px) {
  .reader { grid-template-columns: 1fr; }
  .rail {
    position: fixed; top: var(--header-h); bottom: 0; left: 0;
    width: min(320px, 86vw);
    transform: translateX(-103%);
    transition: transform .26s var(--ease-out);
    box-shadow: var(--sh-4);
  }
  .rail.open { transform: none; }
  .rail-toggle { display: inline-flex; }
}

@media (max-width: 640px) {
  .reader-toolbar { padding: 6px 8px; gap: 5px; }
  .tb-sep { display: none; }
  .zoom-label { display: none; }
}

/* --- unit page grid (the "book view" per unit) -------------------------- */
.page-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
}
.pg-cell {
  position: relative; display: block; padding: 0; border: 1.5px solid var(--border);
  border-radius: var(--r-sm); overflow: hidden; background: var(--surface);
  box-shadow: var(--sh-1); text-align: left;
  transition: transform .16s var(--ease), box-shadow .2s var(--ease), border-color .16s var(--ease);
}
.pg-cell:hover { transform: translateY(-3px); box-shadow: var(--sh-3); border-color: var(--border-2); }
.pg-cell img { width: 100%; aspect-ratio: 1 / 1.38; object-fit: cover; object-position: top; }
.pg-cell .pc-n {
  position: absolute; left: 6px; top: 6px;
  padding: 1px 7px; border-radius: var(--r-pill);
  background: color-mix(in srgb, var(--ink) 78%, transparent);
  color: #fff; font-size: .68rem; font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.pg-cell .pc-s {
  position: absolute; right: 6px; top: 6px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--ok); color: #fff; display: grid; place-items: center;
  font-size: .66rem; box-shadow: var(--sh-1);
}
.pg-cell .pc-t {
  display: block; padding: 6px 8px 7px; font-size: .7rem; line-height: 1.3;
  color: var(--ink-2); border-top: 1px solid var(--border);
  max-height: 3.4em; overflow: hidden;
}

/* --- book/level cards --------------------------------------------------- */
.level-card { display: flex; flex-direction: column; }
.level-card .lc-top {
  padding: 18px; display: flex; gap: 14px; align-items: flex-start;
  background: linear-gradient(135deg, var(--surface-2), var(--surface));
  border-bottom: 1px solid var(--border);
}
.level-card .lc-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; flex: 1; }
.level-card .lc-units { display: flex; flex-wrap: wrap; gap: 6px; }
.unit-dot {
  width: 27px; height: 27px; border-radius: 8px;
  display: grid; place-items: center;
  font-size: .7rem; font-weight: 700;
  background: var(--surface-2); color: var(--ink-3);
  border: 1px solid var(--border);
  text-decoration: none;
  transition: background .14s var(--ease), color .14s var(--ease), transform .14s var(--ease);
}
.unit-dot:hover { background: var(--brand-soft); color: var(--brand); border-color: var(--brand-tint); transform: translateY(-1px); text-decoration: none; }
.unit-dot.done { background: var(--ok-soft); color: var(--ok); border-color: var(--ok-soft); }

/* --- unit header hero --------------------------------------------------- */
.unit-hero {
  display: grid; grid-template-columns: 1fr auto; gap: 22px;
  padding: 24px; border-radius: var(--r-lg);
  background: linear-gradient(135deg, var(--brand-soft), var(--surface) 60%);
  border: 1px solid var(--border);
  margin-bottom: 22px;
}
.unit-hero h1 { margin-bottom: 6px; }
.unit-hero .uh-sub { color: var(--ink-3); font-size: .88rem; }
.unit-hero .uh-side { display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
@media (max-width: 760px) {
  .unit-hero { grid-template-columns: 1fr; }
  .unit-hero .uh-side { align-items: flex-start; }
}

/* --- section accordion -------------------------------------------------- */
.sections { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.sec-card {
  padding: 15px 17px; border-radius: var(--r-md);
  background: var(--surface); border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 6px;
}
.sec-card .sc-kind {
  font-size: .68rem; font-weight: 700; letter-spacing: .07em;
  text-transform: uppercase; color: var(--accent);
}
.sec-card .sc-title { font-weight: 700; font-size: .95rem; line-height: 1.3; }
.sec-card .sc-note { font-size: .82rem; color: var(--ink-3); line-height: 1.45; }
.sec-card .sc-actions { margin-top: auto; padding-top: 8px; display: flex; gap: 6px; }

/* --- audio panel -------------------------------------------------------- */
.audio-panel {
  position: fixed; right: 20px; bottom: 20px; z-index: 250;
  width: min(390px, calc(100vw - 40px));
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); box-shadow: var(--sh-4);
  overflow: hidden;
  display: flex; flex-direction: column;
  max-height: min(70vh, 560px);
  transform-origin: bottom right;
}
.audio-panel.min { max-height: 58px; }
.audio-panel .ap-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.audio-panel.min .ap-head { border-bottom: 0; }
.audio-panel .ap-title { flex: 1; min-width: 0; }
.audio-panel .ap-title b { display: block; font-size: .84rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.audio-panel .ap-title span { font-size: .7rem; color: var(--ink-3); }
.audio-panel .ap-body { overflow-y: auto; padding: 8px; }
.audio-panel.min .ap-body { display: none; }
.audio-panel audio { width: 100%; margin-top: 6px; }
.track {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: var(--r-sm); width: 100%;
  border: 1px solid transparent; background: transparent; text-align: left;
  transition: background .14s var(--ease);
}
.track:hover { background: var(--surface-2); }
.track.is-current { background: var(--accent-soft); border-color: var(--accent); }
.track .tnum { font-size: .72rem; font-weight: 700; color: var(--ink-4); min-width: 2em; font-variant-numeric: tabular-nums; }
.track .tname { font-size: .82rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.track .tkind { font-size: .64rem; font-weight: 700; text-transform: uppercase; color: var(--ink-4); }
