/* JoyReader — giao diện đọc sách */
:root {
  --bg: #f7f3ea;
  --bg-deep: #efe8d8;
  --surface: #fffdf7;
  --surface-2: #f0e9db;
  --ink: #26323a;
  --muted: #6d7a83;
  --line: rgba(38, 50, 58, 0.12);
  --line-strong: rgba(38, 50, 58, 0.22);
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --accent-soft: #d9efe9;
  --gold: #b45309;
  --danger: #b91c1c;
  --shadow: 0 18px 40px rgba(40, 45, 40, 0.12);
  --shadow-soft: 0 6px 18px rgba(40, 45, 40, 0.08);
  --radius: 20px;
  --radius-sm: 12px;
  --topbar: 66px;
  --font-ui: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-hanzi: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", "Source Han Sans SC", sans-serif;
  --border: var(--line-strong);
  --bg-soft: var(--surface-2);
  --gutter: clamp(14px, 2.4vw, 40px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101517;
    --bg-deep: #0b0f11;
    --surface: #171d20;
    --surface-2: #20272b;
    --ink: #e9e4d8;
    --muted: #9aa7ad;
    --line: rgba(233, 228, 216, 0.12);
    --line-strong: rgba(233, 228, 216, 0.24);
    --accent: #2dd4bf;
    --accent-strong: #5eead4;
    --accent-soft: rgba(45, 212, 191, 0.14);
    --gold: #fbbf24;
    --shadow: 0 18px 46px rgba(0, 0, 0, 0.45);
    --shadow-soft: 0 6px 18px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* Trình phát audio ẩn nhưng vẫn nằm trong DOM (cần cho iOS/Safari). */
#jr-audio {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

html, body {
  margin: 0;
  padding: 0;
}

body {
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(15, 118, 110, 0.09), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(180, 83, 9, 0.07), transparent 55%),
    var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overflow-x: hidden;
}

button { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
img { max-width: 100%; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--topbar);
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 clamp(14px, 3vw, 34px);
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(16px) saturate(1.2);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}
.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: linear-gradient(145deg, var(--accent), var(--accent-strong));
  color: #fff;
  font-family: var(--font-hanzi);
  font-size: 22px;
  font-weight: 700;
  box-shadow: 0 6px 14px rgba(15, 118, 110, 0.3);
}
.brand-name {
  font-weight: 750;
  font-size: 1.02rem;
  letter-spacing: 0.01em;
  display: flex;
  flex-direction: column;
  line-height: 1.05;
}
.brand-sub {
  font-weight: 500;
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.04em;
}

.searchbox {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 560px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  height: 42px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-soft);
}
.searchbox svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.9;
  stroke-linecap: round;
  flex: 0 0 auto;
}
.searchbox input {
  flex: 1;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-size: 0.95rem;
  min-width: 0;
}
.search-clear {
  border: 0;
  background: var(--surface-2);
  color: var(--muted);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  line-height: 1;
}

.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

/* ---------- common controls ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 38px;
  padding: 0 15px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); border-color: var(--accent); }
.btn:active { transform: translateY(0); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover { background: var(--accent-strong); }
.btn.ghost { background: transparent; }
.btn.small { height: 32px; padding: 0 12px; font-size: 0.8rem; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.icon-btn {
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.icon-btn[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}

.app { max-width: 1320px; margin: 0 auto; padding: 26px clamp(14px, 3vw, 34px) 70px; }
.app.reading { max-width: none; padding: 10px clamp(10px, 2vw, 22px) 14px; }
.reading .searchbox { display: none; }
.chip-row { display: inline-flex; flex-wrap: wrap; gap: 6px; }

/* ---------- hero ---------- */
.hero {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(280px, 1fr);
  gap: 26px;
  align-items: end;
  margin-bottom: 30px;
}
.hero h1 {
  margin: 0 0 10px;
  font-size: clamp(1.7rem, 3.4vw, 2.6rem);
  line-height: 1.1;
  letter-spacing: -0.02em;
}
.hero h1 em {
  font-style: normal;
  color: var(--accent);
}
.hero p { margin: 0; color: var(--muted); max-width: 56ch; line-height: 1.6; font-size: 0.98rem; }

.stats-strip {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.stat {
  min-width: 108px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-soft);
}
.stat b { display: block; font-size: 1.3rem; letter-spacing: -0.01em; }
.stat span { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }

/* ---------- continue reading ---------- */
.section-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin: 0 0 14px;
}
.section-head h2 { margin: 0; font-size: 1.15rem; letter-spacing: -0.01em; }
.section-head .hint { color: var(--muted); font-size: 0.84rem; }
.section-head .spacer { flex: 1; }

.continue-row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(240px, 300px);
  gap: 14px;
  overflow-x: auto;
  padding: 4px 2px 14px;
  scroll-snap-type: x mandatory;
  margin-bottom: 18px;
}
.continue-row::-webkit-scrollbar { height: 8px; }
.continue-row::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 99px; }

.mini-book {
  scroll-snap-align: start;
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.mini-book:hover { border-color: var(--accent); transform: translateY(-2px); }
.mini-cover {
  width: 74px;
  aspect-ratio: 3 / 4;
  border-radius: 9px;
  overflow: hidden;
  background: var(--surface-2);
}
.mini-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.mini-body { min-width: 0; }
.mini-body h3 { margin: 0 0 3px; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-body p { margin: 0 0 8px; font-size: 0.78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-progress { height: 5px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.mini-progress i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #34d399); }

/* ---------- filters ---------- */
.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 12px 0 16px;
  border-top: 1px solid var(--line);
}
.filter-label { font-size: 0.76rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.1em; margin-right: 2px; }
.chip {
  height: 32px;
  padding: 0 13px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.83rem;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); }
.chip[aria-pressed="true"] {
  background: var(--ink);
  color: var(--bg);
  border-color: var(--ink);
}
.chip .count { opacity: 0.6; margin-left: 4px; font-size: 0.72rem; }
.filters .spacer { flex: 1; }
.select {
  height: 32px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 0 12px;
  font-size: 0.83rem;
}

.result-line { color: var(--muted); font-size: 0.84rem; margin: 0 0 14px; }

/* ---------- book grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(158px, 1fr));
  gap: clamp(12px, 1.8vw, 20px);
}
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
  display: flex;
  flex-direction: column;
}
.card:hover { transform: translateY(-4px); box-shadow: var(--shadow); border-color: var(--line-strong); }
.cover {
  position: relative;
  aspect-ratio: 3 / 4;
  background: linear-gradient(160deg, var(--surface-2), var(--bg-deep));
  overflow: hidden;
}
.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.35s ease;
}
.card:hover .cover img { transform: scale(1.04); }
.badge {
  position: absolute;
  top: 8px;
  left: 8px;
  min-width: 24px;
  height: 24px;
  padding: 0 7px;
  border-radius: 8px;
  background: rgba(10, 20, 20, 0.72);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  display: grid;
  place-items: center;
  backdrop-filter: blur(4px);
}
.badge.orig { background: rgba(180, 83, 9, 0.9); }
.card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 3px; flex: 1; }
.card-body h3 {
  margin: 0;
  font-size: 0.95rem;
  font-family: var(--font-hanzi);
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-body .en {
  margin: 0;
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-meta {
  margin-top: auto;
  padding-top: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 0.72rem;
  color: var(--muted);
}
.card-progress { height: 4px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 7px; }
.card-progress i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #34d399); }

.load-more { display: flex; justify-content: center; padding: 30px 0 0; }

/* ---------- reader ---------- */
.reader {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 392px;
  gap: 16px;
  height: calc(100dvh - var(--topbar) - 26px);
  min-height: 480px;
}
.stage-pane { display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; }

.book-line {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 2px;
  min-height: 34px;
}
.book-line-text { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.15; }
.book-line-text b {
  font-family: var(--font-hanzi);
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-line-text span {
  font-size: 0.74rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stage-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 22px;
  background:
    radial-gradient(70% 70% at 50% 40%, rgba(255, 255, 255, 0.5), transparent),
    repeating-linear-gradient(45deg, var(--surface-2) 0 2px, transparent 2px 6px),
    var(--bg-deep);
  overflow: hidden;
  padding: 14px;
}
@media (prefers-color-scheme: dark) {
  .stage-wrap {
    background:
      radial-gradient(70% 70% at 50% 40%, rgba(255, 255, 255, 0.04), transparent),
      repeating-linear-gradient(45deg, var(--surface-2) 0 2px, transparent 2px 6px),
      var(--bg-deep);
  }
}
.stage {
  position: relative;
  width: 60%;
  max-width: 100%;
  border-radius: 10px;
  overflow: hidden;
  background: #0b0d0e;
  box-shadow: 0 22px 50px rgba(0, 0, 0, 0.32);
  transition: opacity 0.2s ease;
  touch-action: pan-y;
}
.stage.loading { opacity: 0.35; }
.stage img.layer {
  position: absolute;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}
.stage-karaoke {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
}
.image-word-box {
  position: absolute;
  border-radius: 4px;
  background: rgba(250, 204, 21, 0.28);
  box-shadow: inset 0 -0.5em 0 rgba(250, 204, 21, 0.42), 0 0 0 1.5px rgba(180, 83, 9, 0.35);
  pointer-events: none;
  transition: left 0.08s linear, top 0.08s linear, width 0.08s linear, height 0.08s linear;
}
.stage .stage-error {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 6px;
  text-align: center;
  color: #cbd5d1;
  font-size: 0.9rem;
  padding: 20px;
  line-height: 1.5;
}
.stage .stage-error small { display: block; opacity: 0.72; font-size: 0.76rem; }
.hotspot {
  position: absolute;
  z-index: 5;
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s ease, box-shadow 0.15s ease;
}
.hotspot:hover, .hotspot:focus-visible {
  background: rgba(45, 212, 191, 0.18);
  box-shadow: 0 0 0 2px rgba(45, 212, 191, 0.75);
  outline: none;
}
.stage-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 64px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(8px);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 1.15rem;
  z-index: 3;
}
.stage-arrow:hover { border-color: var(--accent); color: var(--accent); }
.stage-arrow.prev { left: 10px; }
.stage-arrow.next { right: 10px; }
.stage-arrow:disabled { opacity: 0.25; cursor: default; }

.reader-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-soft);
  flex-wrap: wrap;
}
.reader-bar .page-pos { font-size: 0.84rem; color: var(--muted); white-space: nowrap; }
.reader-bar .page-pos b { color: var(--ink); font-variant-numeric: tabular-nums; }
.reader-bar input[type="range"] { flex: 1; min-width: 90px; accent-color: var(--accent); }
.reader-bar .spacer { flex: 1; }
.audio-status {
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 64px;
  text-align: center;
  white-space: nowrap;
}

.page-select {
  height: 32px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 0 8px;
  font-size: 0.82rem;
}

/* panel */
.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow-soft);
  overflow: hidden;
}
.panel-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  flex-wrap: nowrap;
}
.panel-head .grow { flex: 1; }
.panel-title {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.01em;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.panel-tools { display: flex; gap: 6px; align-items: center; flex: 0 0 auto; }
.tool {
  height: 30px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.76rem;
  cursor: pointer;
}
.tool[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-strong); }
.tool:hover { border-color: var(--accent); }

.book-search {
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}
.book-search input {
  flex: 1;
  min-width: 0;
  height: 32px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  padding: 0 10px;
  font-size: 0.85rem;
  outline: none;
}
.book-search input:focus { border-color: var(--accent); }

.sentences {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overscroll-behavior: contain;
}
.sentence {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 9px;
  padding: 10px;
  border-radius: 14px;
  border: 1px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.sentence:hover { background: var(--surface-2); }
.sentence.has-audio { cursor: pointer; }
.sentence.active { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.sentence .play {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  color: var(--accent-strong);
  flex: 0 0 auto;
}
.sentence .play:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.sentence .play.playing { background: var(--accent); color: #fff; }
.sentence .play:disabled { opacity: 0.3; cursor: default; }

.words { display: flex; flex-wrap: wrap; align-items: flex-end; column-gap: 2px; row-gap: 8px; }
.word { display: inline-flex; flex-direction: column; align-items: center; line-height: 1.12; }
.word.latin .hz { font-family: var(--font-ui); font-size: 1.12rem; }
.word.gap-left { margin-left: 0.3em; }
.word .py {
  font-size: 0.66rem;
  color: var(--muted);
  min-height: 1.2em;
  line-height: 1.25;
  letter-spacing: 0.01em;
  white-space: nowrap;
  transition: opacity 0.2s ease;
}
.word .hz { font-size: 1.24rem; line-height: 1.2; font-family: var(--font-hanzi); }
.word.karaoke .hz {
  background: var(--accent);
  color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 8px rgba(15, 118, 110, 0.35);
  transition: background 0.1s linear, color 0.1s linear;
}
.word.karaoke .py { color: var(--accent-strong); }
@media (prefers-color-scheme: dark) {
  .word.karaoke .hz { color: #06302c; }
}
.word .hz.space { min-width: 0.45em; }
.sentence.no-pinyin .word .py { display: none; }
.sentence .trans {
  margin: 6px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
  display: none;
}
.sentence.show-trans .trans { display: block; }

.sentences-empty {
  padding: 26px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.6;
}
.sentences-empty b { color: var(--ink); }

.book-search-results {
  padding: 6px 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 220px;
  overflow: auto;
  border-top: 1px solid var(--line);
}
.search-hit {
  text-align: left;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 0.82rem;
  line-height: 1.4;
}
.search-hit:hover { border-color: var(--accent); }
.search-hit .hit-page { color: var(--muted); font-size: 0.72rem; display: block; margin-bottom: 2px; }
.search-hit mark { background: var(--accent-soft); color: var(--accent-strong); border-radius: 3px; padding: 0 1px; }

/* page drawer */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 12, 12, 0.45);
  backdrop-filter: blur(3px);
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 20px;
}
.drawer {
  width: min(720px, 100%);
  max-height: min(76dvh, 640px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.drawer-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.drawer-head h2 { margin: 0; font-size: 1rem; }
.drawer-head .spacer { flex: 1; }
.drawer-grid {
  padding: 14px 18px 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
  overflow: auto;
}
.page-btn {
  height: 46px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 1px;
}
.page-btn small { font-size: 0.62rem; color: var(--muted); }
.page-btn:hover { border-color: var(--accent); }
.page-btn.current { background: var(--accent); border-color: var(--accent); color: #fff; }
.page-btn.current small { color: rgba(255, 255, 255, 0.75); }

/* ---------- misc ---------- */
.boot { padding: 90px 0; text-align: center; color: var(--muted); }
.boot-spinner {
  width: 34px; height: 34px; margin: 0 auto 14px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { padding: 80px 20px; text-align: center; color: var(--muted); }
.empty-mark {
  font-family: var(--font-hanzi);
  font-size: 3rem;
  color: var(--line-strong);
  margin-bottom: 10px;
}
.empty h2 { margin: 0 0 6px; color: var(--ink); font-size: 1.1rem; }
.empty p { margin: 0; font-size: 0.9rem; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 90;
  max-width: min(520px, calc(100vw - 30px));
  padding: 11px 18px;
  border-radius: 999px;
  background: rgba(22, 30, 30, 0.92);
  color: #f4f1e8;
  font-size: 0.86rem;
  box-shadow: var(--shadow);
  text-align: center;
}
.toast.error { background: rgba(150, 30, 30, 0.95); }

.skeleton {
  background: linear-gradient(100deg, var(--surface-2) 30%, var(--surface) 50%, var(--surface-2) 70%);
  background-size: 220% 100%;
  animation: shimmer 1.2s infinite;
  border-radius: 8px;
}
@keyframes shimmer { to { background-position: -220% 0; } }

.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.tag {
  font-size: 0.68rem;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

/* ---------- responsive ---------- */
@media (max-width: 1024px) {
  .reader { grid-template-columns: minmax(0, 1fr) 340px; }
  .hero { grid-template-columns: 1fr; align-items: start; }
  .stats-strip { justify-content: flex-start; }
}

@media (max-width: 860px) {
  :root { --topbar: 58px; }
  .brand-name { display: none; }
  .searchbox { height: 38px; }
  .app { padding-top: 18px; }
  .app.reading { padding: 8px 8px calc(14px + env(safe-area-inset-bottom, 0px)); }

  .reader {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto;
    height: auto;
    gap: 10px;
  }
  .stage-pane { min-height: 0; }
  .stage-wrap { flex: none; height: auto; min-height: 0; padding: 8px; }
  .stage-arrow { width: 38px; height: 54px; }
  .panel { max-height: none; }
  .sentences { max-height: 46dvh; }
  .panel-head { padding: 8px 10px; flex-wrap: wrap; }
  .reader-bar { padding: 6px 8px; gap: 6px; }
  .reader-bar input[type="range"] { min-width: 60px; }
  .reader-bar .page-pos { order: -1; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }
  .continue-row { grid-auto-columns: minmax(220px, 78vw); }
  .hero h1 { font-size: 1.55rem; }
  .stat { min-width: 88px; padding: 10px 12px; }
  .stat b { font-size: 1.1rem; }
}

@media (max-width: 520px) {
  .topbar { gap: 10px; padding: 0 12px; }
  .brand-mark { width: 34px; height: 34px; font-size: 19px; }
  .searchbox { padding: 0 10px; }
  .searchbox input { font-size: 0.88rem; }
  .btn.small { padding: 0 10px; }
  .stage-wrap { height: 46dvh; min-height: 260px; }
  .panel-tools { gap: 4px; }
  .tool { padding: 0 8px; font-size: 0.72rem; }
}

/* ---------- layout: sidebar + content ---------- */
.layout { display: grid; grid-template-columns: 236px minmax(0, 1fr); gap: 26px; align-items: start; }
.sidebar {
  position: sticky;
  top: calc(var(--topbar) + 16px);
  max-height: calc(100dvh - var(--topbar) - 32px);
  overflow-y: auto;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(245, 158, 11, 0.13), var(--surface) 46%);
  box-shadow: var(--shadow-soft);
}
.side-head { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); padding: 6px 10px 8px; }
.side-item { display: flex; flex-direction: column; gap: 1px; padding: 8px 12px; border-radius: 12px; margin: 2px 0; color: var(--ink); transition: background 0.15s ease, color 0.15s ease; }
.side-item:hover { background: var(--surface-2); }
.side-item.active { background: linear-gradient(135deg, #f59e0b, #d97706); color: #fff; box-shadow: 0 8px 18px rgba(217, 119, 6, 0.24); }
.side-zh { font-family: var(--font-hanzi); font-size: 0.98rem; font-weight: 700; line-height: 1.2; }
.side-vi { font-size: 0.68rem; opacity: 0.7; }
.side-item.active .side-vi { opacity: 0.88; }
.side-foot { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.content { min-width: 0; }
.standalone-content { max-width: 1220px; margin: 0 auto; width: 100%; }

.channel-hero {
  padding: 24px 26px;
  border-radius: 24px;
  background: linear-gradient(120deg, rgba(15, 118, 110, 0.12), rgba(245, 158, 11, 0.16)), var(--surface);
  border: 1px solid var(--line);
  margin-bottom: 24px;
}
.channel-hero .crumb { font-size: 0.72rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.channel-hero h1 { margin: 4px 0 6px; font-family: var(--font-hanzi); font-size: clamp(1.5rem, 2.6vw, 2.1rem); }
.channel-hero h1 em { font-style: normal; font-family: var(--font-ui); font-size: 0.55em; color: var(--muted); margin-left: 8px; }
.channel-hero p { margin: 0; color: var(--muted); font-size: 0.85rem; }

.class-section { margin-bottom: 28px; }
.class-head { display: flex; align-items: flex-end; gap: 12px; margin-bottom: 10px; padding: 0 2px; }
.class-title h2 { margin: 0; font-family: var(--font-hanzi); font-size: 1.12rem; }
.class-title span { display: block; font-size: 0.75rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 52vw; }
.class-qty { font-size: 0.74rem; color: var(--muted); white-space: nowrap; }
.class-head .more { margin-left: auto; color: var(--accent-strong); font-size: 0.82rem; font-weight: 600; white-space: nowrap; }
.book-row { display: grid; grid-auto-flow: column; grid-auto-columns: 150px; gap: 14px; overflow-x: auto; padding: 2px 2px 12px; scroll-snap-type: x proximity; }
.book-row::-webkit-scrollbar { height: 8px; }
.book-row::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 99px; }
.book-row .card { scroll-snap-align: start; }
.book-row .card-body h3 { font-size: 0.9rem; }

.breadcrumb { display: flex; gap: 8px; flex-wrap: wrap; font-size: 0.82rem; color: var(--muted); margin-bottom: 14px; }
.breadcrumb a { color: var(--accent-strong); }
.class-hero h1 { margin: 0 0 4px; font-family: var(--font-hanzi); }
.class-hero p { margin: 0 0 20px; color: var(--muted); }

/* ---------- account ---------- */
.account-wrap { max-width: 560px; margin: 20px auto; }
.auth-card { background: var(--surface); border: 1px solid var(--line); border-radius: 24px; padding: 28px; box-shadow: var(--shadow); }
.auth-card .crumb { font-size: 0.72rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.auth-card h1 { margin: 4px 0 8px; }
.auth-card > p { color: var(--muted); line-height: 1.55; margin: 0 0 18px; font-size: 0.92rem; }
.auth-tabs { display: flex; gap: 6px; padding: 4px; border-radius: 12px; background: var(--surface-2); margin-bottom: 18px; }
.auth-tabs button { flex: 1; height: 34px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 600; cursor: pointer; }
.auth-tabs button.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-soft); }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 0.82rem; color: var(--muted); margin-bottom: 12px; }
.auth-card input { height: 42px; border-radius: 11px; border: 1px solid var(--line-strong); background: var(--bg); color: var(--ink); padding: 0 12px; font-size: 0.95rem; outline: none; }
.auth-card input:focus { border-color: var(--accent); }
.auth-submit { width: 100%; height: 44px; margin-top: 4px; }
.auth-error { color: var(--danger); font-size: 0.82rem; min-height: 1.2em; margin: 10px 0 0; }
.auth-note { color: var(--muted); font-size: 0.75rem; margin: 14px 0 0; }

.account-hero { display: flex; align-items: center; gap: 16px; margin-bottom: 20px; }
.account-hero-text { flex: 1; min-width: 0; }
.account-hero h1 { margin: 0 0 4px; font-size: 1.5rem; }
.account-hero p { margin: 0; color: var(--muted); font-size: 0.86rem; }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: inline-grid; place-items: center; background: var(--accent); color: #fff; font-weight: 700; font-size: 0.85rem; }
.avatar.xl { width: 60px; height: 60px; font-size: 1.4rem; flex: 0 0 auto; }
.account-btn { padding-left: 6px; }
.account-name { max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat-card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 14px 16px; }
.stat-card b { display: block; font-size: 1.2rem; margin-bottom: 2px; }
.stat-card span { color: var(--muted); font-size: 0.74rem; }

.dashboard-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 14px; margin-bottom: 18px; }
.panel-card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 16px; margin-bottom: 18px; box-shadow: var(--shadow-soft); }
.panel-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.panel-card-head h2 { margin: 0; font-size: 1rem; }
.panel-card-head span { font-size: 0.75rem; color: var(--muted); }
.chart { display: flex; align-items: flex-end; gap: 6px; height: 150px; padding-top: 8px; }
.chart-col { flex: 1; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; }
.chart-bar { width: 100%; max-width: 26px; border-radius: 6px 6px 2px 2px; background: linear-gradient(180deg, var(--accent), #34d399); min-height: 1px; }
.chart-col span { font-size: 0.62rem; color: var(--muted); }
.levels { display: flex; flex-direction: column; gap: 10px; }
.level-row { display: grid; grid-template-columns: 32px 1fr auto; align-items: center; gap: 8px; }
.level-badge { height: 26px; border-radius: 7px; display: grid; place-items: center; background: var(--surface-2); font-size: 0.75rem; font-weight: 700; }
.level-track { height: 7px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.level-track i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #34d399); }
.level-text { font-size: 0.7rem; color: var(--muted); white-space: nowrap; }

.account-books { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; }
.account-book { display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 10px; padding: 8px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); }
.account-book img { width: 52px; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 6px; }
.account-book b { display: block; font-size: 0.85rem; font-family: var(--font-hanzi); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-book span { font-size: 0.72rem; color: var(--muted); }
.account-progress { height: 4px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.account-progress i { display: block; height: 100%; background: var(--accent); }
.done-mark { display: inline-block; color: var(--accent-strong); background: var(--accent-soft); border-radius: 50%; padding: 0 5px; font-size: 0.7rem; }

.activity { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.activity li { display: flex; align-items: center; gap: 10px; padding: 8px 4px; border-bottom: 1px solid var(--line); font-size: 0.82rem; }
.activity li:last-child { border-bottom: 0; }
.event-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: 0 0 auto; }
.activity b { font-weight: 600; }
.activity span { color: var(--muted); margin-left: 4px; }
.activity time { margin-left: auto; color: var(--muted); font-size: 0.72rem; white-space: nowrap; }
.muted { color: var(--muted); font-size: 0.85rem; }

@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; gap: 14px; }
  .sidebar {
    position: static;
    max-height: none;
    display: flex;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    padding: 8px;
    border-radius: 16px;
  }
  .side-head, .side-vi, .side-foot { display: none; }
  .side-item { flex: 0 0 auto; margin: 0; padding: 8px 12px; flex-direction: row; align-items: center; }
  .side-zh { font-size: 0.86rem; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dashboard-grid { grid-template-columns: 1fr; }
  .account-hero { flex-wrap: wrap; }
  .account-hero .btn { margin-left: auto; }
  .class-title span { max-width: 60vw; }
  .book-row { grid-auto-columns: 132px; }
  .account-wrap { margin: 8px auto; }
}

@media (max-width: 520px) {
  .channel-hero { padding: 18px 18px; }
  .book-row { grid-auto-columns: 124px; }
  .auth-card { padding: 20px; }
  .stat-card b { font-size: 1.05rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------- channel level toolbar & badges ---------- */
.channel-hero-row {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.channel-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  margin-bottom: 22px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-soft);
}
.channel-toolbar .chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1 1 auto;
}
.class-level-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 26px;
  padding: 0 10px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #fff;
  font-family: var(--font-ui);
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-right: 8px;
  vertical-align: middle;
  box-shadow: 0 3px 8px rgba(15, 118, 110, 0.25);
}

/* ---------- admin & management UI ---------- */
.admin-wrap {
  max-width: 1220px;
  margin: 0 auto;
}
.admin-btn {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}
.admin-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 22px 26px;
  margin-bottom: 20px;
  border-radius: 22px;
  background: linear-gradient(120deg, rgba(15, 118, 110, 0.11), rgba(245, 158, 11, 0.14)), var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-soft);
}
.admin-head .crumb {
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent-strong);
  font-weight: 700;
}
.admin-head h1 {
  margin: 4px 0 4px;
  font-size: 1.55rem;
}
.admin-head p {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
}
.admin-tabs {
  display: inline-flex;
  gap: 6px;
  padding: 5px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  flex-wrap: wrap;
  box-shadow: var(--shadow-soft);
}
.admin-tab,
.admin-tabs button {
  height: 38px;
  padding: 0 18px;
  border-radius: 10px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  transition: all 0.15s ease;
}
.admin-tab:hover,
.admin-tabs button:hover {
  color: var(--ink);
  background: var(--surface-2);
}
.admin-tab.active,
.admin-tabs button.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #fff;
  box-shadow: 0 4px 12px rgba(15, 118, 110, 0.25);
}
.admin-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  margin-bottom: 16px;
  box-shadow: var(--shadow-soft);
}
.admin-input,
.admin-toolbar input[type="search"],
.admin-toolbar input[type="text"],
.admin-toolbar select,
.crawler-card input[type="text"],
.crawler-card input[type="search"] {
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--bg);
  color: var(--ink);
  padding: 0 12px;
  font-family: inherit;
  font-size: 0.88rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.admin-toolbar input[type="search"],
.admin-toolbar input[type="text"] {
  min-width: 240px;
}
.admin-input:focus,
.admin-toolbar input[type="search"]:focus,
.admin-toolbar input[type="text"]:focus,
.admin-toolbar select:focus,
.crawler-card input[type="text"]:focus,
.crawler-card input[type="search"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.admin-table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  overflow-x: auto;
  box-shadow: var(--shadow-soft);
}
.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}
.admin-table th,
.admin-table td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
.admin-table th {
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  background: var(--surface-2);
  white-space: nowrap;
}
.admin-table tr:last-child td {
  border-bottom: 0;
}
.admin-table tr:hover td {
  background: color-mix(in srgb, var(--surface-2) 45%, transparent);
}
.admin-actions,
.row-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.btn.danger {
  border-color: rgba(185, 28, 28, 0.35);
  color: var(--danger);
}
.btn.danger:hover {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 600;
  white-space: nowrap;
}
.status-pill.ok {
  background: var(--accent-soft);
  color: var(--accent-strong);
}
.status-pill.warn {
  background: rgba(245, 158, 11, 0.16);
  color: var(--gold);
}
.status-pill.err {
  background: rgba(185, 28, 28, 0.14);
  color: var(--danger);
}
.role-badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 7px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  background: var(--surface-2);
  color: var(--muted);
}
.role-badge.admin {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff;
}
.role-badge.user {
  background: var(--surface-2);
  color: var(--muted);
}
.book-Id-cell {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}
.admin-book-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
}
.admin-book-info img {
  width: 38px;
  height: 50px;
  object-fit: cover;
  border-radius: 6px;
  background: var(--surface-2);
  flex: 0 0 auto;
}
.admin-book-info b {
  display: block;
  font-family: var(--font-hanzi);
  font-size: 0.92rem;
}
.admin-book-info small {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
}

/* ---------- crawler & scanner panel ---------- */
.crawler-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(310px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.crawler-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px 20px;
  box-shadow: var(--shadow-soft);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.crawler-card h3 {
  margin: 0;
  font-size: 0.98rem;
  display: flex;
  align-items: center;
  gap: 8px;
}
.crawler-card p {
  margin: 0;
  font-size: 0.83rem;
  color: var(--muted);
  line-height: 1.5;
}
.crawler-progress-box {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px 18px;
  margin-bottom: 18px;
  box-shadow: var(--shadow-soft);
}
.crawler-progress-bar {
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  margin: 10px 0;
}
.crawler-progress-bar i,
.crawler-progress-fill {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), #34d399);
  transition: width 0.25s ease;
}
.crawler-log {
  max-height: 210px;
  overflow-y: auto;
  background: var(--bg-deep);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--ink);
  white-space: pre-wrap;
}

/* ---------- modal (Create / Edit User & Book) ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(10, 15, 18, 0.55);
  backdrop-filter: blur(5px);
  display: grid;
  place-items: center;
  padding: 16px;
}
.modal-card {
  width: min(560px, 100%);
  max-height: 92dvh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 24px 28px;
  box-shadow: var(--shadow);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 14px;
  margin-bottom: 18px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 {
  margin: 0;
  font-size: 1.22rem;
}
.modal-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.form-grid,
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.form-field,
.modal-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.83rem;
  font-weight: 600;
  color: var(--muted);
}
.form-field.full {
  grid-column: 1 / -1;
}
.form-field input,
.form-field select,
.form-field textarea,
.modal-form input:not([type="checkbox"]),
.modal-form select,
.modal-form textarea {
  width: 100%;
  min-height: 42px;
  border-radius: 11px;
  border: 1px solid var(--line-strong);
  background: var(--bg);
  color: var(--ink);
  padding: 9px 13px;
  font: inherit;
  font-weight: 400;
  font-size: 0.92rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.modal-form textarea {
  min-height: 76px;
  resize: vertical;
}
.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus,
.modal-form input:not([type="checkbox"]):focus,
.modal-form select:focus,
.modal-form textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.modal-foot,
.modal-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

/* =====================================================================
   Giao diện v2 — full màn hình, modal mới, lọc bài đã học, mobile
   ===================================================================== */

/* ---------- full chiều rộng màn hình ---------- */
.app { max-width: none; padding: 24px var(--gutter) 70px; }
.topbar { padding: 0 var(--gutter); }
.searchbox { max-width: 640px; }
.layout { grid-template-columns: 250px minmax(0, 1fr); gap: clamp(18px, 2vw, 32px); }
.standalone-content,
.admin-wrap { max-width: none; }
.grid { grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); }
.book-row { grid-auto-columns: 158px; }
@media (min-width: 1700px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(184px, 1fr)); }
  .book-row { grid-auto-columns: 172px; }
}

.class-hero {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 10px 12px;
  margin-bottom: 20px;
}
.class-hero > div:first-child { min-width: 0; }
.class-hero p { margin: 0; }
.class-hero .spacer { flex: 1; }
.channel-toolbar .spacer { flex: 1; }

/* ---------- công tắc: Chưa học / Tất cả ---------- */
.learned-toggle {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  gap: 2px;
  flex: 0 0 auto;
}
.learned-toggle button {
  height: 30px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 650;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}
.learned-toggle button:hover { color: var(--ink); }
.learned-toggle button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--accent-strong);
  box-shadow: 0 2px 8px rgba(15, 40, 40, 0.12);
}
.learned-toggle button[data-learned-mode="hide"][aria-pressed="true"] {
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #fff;
}
.learned-toggle button[data-learned-mode="hide"]::before { content: "✓ "; }
.side-learned { display: none; }
.side-learned .learned-toggle { display: flex; margin: 0 4px; }
.side-learned .learned-toggle button { flex: 1; padding: 0 8px; }

.all-done-notice {
  grid-column: 1 / -1;
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  border: 1px dashed var(--line-strong);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.88rem;
}

.class-done-stat {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 0.74rem;
  font-weight: 600;
  white-space: nowrap;
}

/* ---------- nút tick "Học xong" trên thẻ sách ---------- */
.card { position: relative; }
.card.is-leaving { opacity: 0; transform: scale(0.94); transition: opacity 0.25s ease, transform 0.25s ease; pointer-events: none; }
.card.is-finished .cover img { filter: saturate(0.75); }
.card-done-btn {
  position: absolute;
  right: 8px;
  bottom: 8px;
  z-index: 3;
  height: 28px;
  padding: 0 10px 0 4px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.55);
  background: rgba(15, 23, 32, 0.6);
  backdrop-filter: blur(6px);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.15s ease, transform 0.15s ease, background 0.15s ease;
}
.card-done-btn .tick {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  border: 1.5px solid rgba(255, 255, 255, 0.85);
  font-size: 0.7rem;
  line-height: 1;
  color: transparent;
}
.card:hover .card-done-btn,
.card:focus-within .card-done-btn { opacity: 1; transform: none; }
.card-done-btn:hover { background: rgba(4, 120, 87, 0.85); }
.card-done-btn:hover .tick { color: #fff; }
.card-done-btn.is-done {
  opacity: 1;
  transform: none;
  background: linear-gradient(135deg, #059669, #047857);
  border-color: rgba(255, 255, 255, 0.7);
  box-shadow: 0 3px 10px rgba(4, 120, 87, 0.4);
}
.card-done-btn.is-done .tick { background: #fff; border-color: #fff; color: #047857; }
@media (hover: none) {
  .card-done-btn { opacity: 1; transform: none; }
}

/* ---------- trang quản trị ---------- */
.admin-head { padding: 22px clamp(18px, 2vw, 30px); }
.admin-head > div:first-child { min-width: 0; }
.admin-head .spacer { display: none; }
.stat-grid { gap: clamp(10px, 1.2vw, 16px); }
.admin-wrap .stat-card {
  position: relative;
  overflow: hidden;
  padding: 16px 18px;
  box-shadow: var(--shadow-soft);
}
.admin-wrap .stat-card::after {
  content: "";
  position: absolute;
  inset: auto -20px -30px auto;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  background: var(--accent-soft);
  opacity: 0.7;
}
.admin-wrap .stat-card b { font-size: 1.45rem; position: relative; z-index: 1; }
.admin-wrap .stat-card span { position: relative; z-index: 1; font-size: 0.8rem; }
.admin-toolbar .spacer { flex: 1; }
.admin-table td { font-size: 0.86rem; }
.user-cell { display: flex; align-items: center; gap: 10px; min-width: 160px; }
.user-cell .avatar { flex: 0 0 auto; width: 34px; height: 34px; }
.cell-note { max-width: 220px; font-size: 0.82rem; }
.crawler-card input[type="text"],
.crawler-card input[type="search"] { background: var(--bg); border-color: var(--line-strong) !important; }

/* ---------- modal v2 ---------- */
body.modal-open { overflow: hidden; }
.modal-backdrop { animation: fadeIn 0.15s ease; }
.modal-card {
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: popIn 0.18s ease;
}
.modal-card.wide { width: min(680px, 100%); }
.modal-head {
  margin: 0;
  padding: 20px 24px 16px;
  align-items: flex-start;
  justify-content: flex-start;
  gap: 14px;
  background: linear-gradient(120deg, rgba(15, 118, 110, 0.10), rgba(245, 158, 11, 0.10)), var(--surface);
}
.modal-icon {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: linear-gradient(145deg, var(--accent), var(--accent-strong));
  color: #fff;
  font-size: 1.15rem;
  box-shadow: 0 6px 14px rgba(15, 118, 110, 0.28);
}
.modal-title { flex: 1; min-width: 0; }
.modal-title h2 { margin: 2px 0 2px; font-size: 1.2rem; }
.modal-title p { margin: 0; color: var(--muted); font-size: 0.83rem; }
.modal-close { flex: 0 0 auto; border-radius: 50%; }
.modal-body { padding: 18px 24px 22px; overflow-y: auto; overflow-x: hidden; flex: 1 1 auto; min-height: 0; }
.modal-actions {
  position: sticky;
  bottom: -22px;
  margin: 4px -24px -22px;
  padding: 14px 24px;
  background: var(--surface);
}
.modal-actions .btn { min-width: 110px; height: 40px; }

.form-section {
  margin: 0;
  padding: 0;
  border: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}
.form-section legend {
  padding: 0;
  margin-bottom: 10px;
  font-size: 0.72rem;
  font-weight: 750;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent-strong);
}
.form-section + .form-section { padding-top: 14px; border-top: 1px dashed var(--line); }
.modal-form .form-field small,
.form-hint { font-size: 0.74rem; font-weight: 400; color: var(--muted); }
.form-hint.warn { color: var(--danger); font-weight: 600; }

.user-summary {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 14px;
  padding: 14px 16px;
  border-radius: 16px;
  background: var(--bg);
  border: 1px solid var(--line);
}
.user-summary-text { flex: 1; min-width: 140px; display: flex; flex-direction: column; }
.user-summary-text b { font-size: 1.02rem; }
.user-summary-text span { font-size: 0.8rem; color: var(--muted); }
.user-summary-stats { display: flex; gap: 6px; }
.user-summary-stats div {
  min-width: 74px;
  padding: 6px 10px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  text-align: center;
}
.user-summary-stats b { display: block; font-size: 0.95rem; }
.user-summary-stats span { font-size: 0.68rem; color: var(--muted); }

.input-group {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  background: var(--bg);
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input-group:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.modal-form .input-group input:not([type="checkbox"]) {
  border: 0;
  border-radius: 0;
  box-shadow: none !important;
  flex: 1;
  min-width: 0;
}
.input-addon {
  border: 0;
  border-left: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  padding: 0 14px;
  font-size: 0.8rem;
  font-weight: 650;
  cursor: pointer;
}
.input-addon:hover { background: var(--accent-soft); color: var(--accent-strong); }

.choice-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal-form .choice-card {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border: 1.5px solid var(--line-strong);
  border-radius: 14px;
  background: var(--bg);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}
.modal-form .choice-card input[type="radio"] {
  accent-color: var(--accent);
  width: 18px;
  height: 18px;
  min-height: 0;
  padding: 0;
  margin: 0;
  flex: 0 0 auto;
  box-shadow: none;
}
.choice-body { display: flex; flex-direction: column; gap: 2px; }
.choice-body b { color: var(--ink); font-size: 0.9rem; }
.choice-body small { font-weight: 400; font-size: 0.74rem; color: var(--muted); }
.modal-form .choice-card:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.modal-form .switch-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  cursor: pointer;
}
.switch-text { display: flex; flex-direction: column; gap: 2px; }
.switch-text b { color: var(--ink); font-size: 0.9rem; }
.switch-text small { font-weight: 400; font-size: 0.74rem; }
.switch { position: relative; width: 46px; height: 26px; flex: 0 0 auto; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch i {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--line-strong);
  transition: background 0.18s ease;
}
.switch i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.18s ease;
}
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(20px); }
.switch input:focus-visible + i { box-shadow: 0 0 0 3px var(--accent-soft); }

.expiry-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.modal-form .expiry-row input[type="date"] { width: auto; flex: 0 0 190px; }
.quick-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.quick-chips .chip { height: 30px; padding: 0 11px; font-size: 0.78rem; }
.quick-chips .chip:hover { background: var(--accent-soft); color: var(--accent-strong); }

@keyframes fadeIn { from { opacity: 0; } }
@keyframes popIn { from { opacity: 0; transform: translateY(10px) scale(0.98); } }

/* ---------- máy tính bảng ---------- */
@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .side-learned { display: block; padding: 0; margin: 0 4px 0 0; border: 0; flex: 0 0 auto; }
  .learned-toggle.in-toolbar { display: none; }
  .side-learned .side-head { display: none; }
  .side-learned .learned-toggle { margin: 0; }
  .sidebar { scrollbar-width: none; }
  .sidebar::-webkit-scrollbar { display: none; }
  .side-item.active { box-shadow: 0 4px 10px rgba(217, 119, 6, 0.24); }
}

/* ---------- điện thoại ---------- */
@media (max-width: 720px) {
  .app { padding: 14px 12px calc(60px + env(safe-area-inset-bottom, 0px)); }
  .topbar { padding: 0 12px; gap: 10px; }
  .account-name { display: none; }
  .top-actions .lbl { display: none; }
  .top-actions .btn.small:has(.ico) { width: 34px; padding: 0; }
  .account-btn { padding: 0 3px; }
  .top-actions { gap: 6px; }

  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .book-row { grid-auto-columns: 138px; gap: 10px; }
  .card { border-radius: 16px; }
  .card-body { padding: 8px 10px 10px; }
  .card-done-btn { height: 26px; right: 6px; bottom: 6px; }
  .card-done-btn .lbl { display: none; }
  .card-done-btn { padding: 0 3px; }
  .card-done-btn.is-done .lbl { display: inline; }
  .card-done-btn.is-done { padding: 0 8px 0 3px; }

  .channel-hero { padding: 16px 16px; border-radius: 18px; margin-bottom: 14px; }
  .channel-toolbar { padding: 10px; gap: 8px; margin-bottom: 16px; }
  .channel-toolbar .chip-row { flex-wrap: nowrap; overflow-x: auto; width: 100%; scrollbar-width: none; }
  .channel-toolbar .chip-row .chip { flex: 0 0 auto; }
  .channel-toolbar .spacer { display: none; }
  .channel-toolbar .select { flex: 1 1 auto; min-width: 0; }
  .class-head { flex-wrap: wrap; gap: 6px 10px; }
  .class-head .class-done-stat { order: 3; }
  .filters .spacer, .class-hero .spacer { display: none; }

  /* quản trị */
  .admin-head { padding: 16px; border-radius: 18px; }
  .admin-head h1 { font-size: 1.2rem; }
  .admin-head p { font-size: 0.8rem; }
  .admin-tabs { display: flex; width: 100%; flex-wrap: nowrap; }
  .admin-tabs button { flex: 1; justify-content: center; padding: 0 8px; font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .admin-wrap .stat-card { padding: 12px 14px; }
  .admin-wrap .stat-card b { font-size: 1.1rem; }
  .admin-toolbar { padding: 12px; }
  .admin-toolbar input[type="search"],
  .admin-toolbar input[type="text"] { min-width: 0; width: 100%; flex: 1 1 100%; }
  .admin-toolbar select { flex: 1 1 calc(50% - 5px); min-width: 0; }
  .admin-toolbar .spacer { display: none; }
  .admin-toolbar .btn { flex: 1 1 auto; }
  .crawler-grid { grid-template-columns: 1fr; }

  /* bảng → thẻ */
  .admin-table-wrap { background: transparent; border: 0; box-shadow: none; overflow: visible; }
  .admin-table, .admin-table tbody { display: block; }
  .admin-table thead { display: none; }
  .admin-table tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 12px;
    padding: 14px;
    margin-bottom: 12px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: var(--shadow-soft);
  }
  .admin-table tr:hover td { background: transparent; }
  .admin-table td { display: block; padding: 0; border: 0; min-width: 0; }
  .admin-table td[data-label]:not([data-label=""])::before {
    content: attr(data-label);
    display: block;
    font-size: 0.66rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin-bottom: 3px;
  }
  .admin-table td[colspan] { grid-column: 1 / -1; }
  .admin-table .cell-main,
  .admin-table .cell-note,
  .admin-table .cell-actions { grid-column: 1 / -1; max-width: none; }
  .admin-table .cell-main { order: -1; }
  .admin-table .cell-main::before { display: none !important; }
  .admin-table .cell-id { position: absolute; right: 14px; top: 14px; font-size: 0.78rem; color: var(--muted); }
  .admin-table .cell-id::before { display: none !important; }
  .admin-table tr { position: relative; }
  .admin-table .cell-cover { display: none; }
  .admin-table .cell-actions { padding-top: 10px; border-top: 1px solid var(--line); }
  .admin-table .row-actions { display: flex; }
  .admin-table .row-actions .btn { flex: 1 1 auto; }

  /* modal dạng bottom sheet */
  .modal-backdrop { place-items: end stretch; padding: 0; }
  .modal-card, .modal-card.wide {
    width: 100%;
    max-height: 94dvh;
    border-radius: 22px 22px 0 0;
    animation: sheetUp 0.22s ease;
  }
  .modal-head { padding: 16px 16px 12px; }
  .modal-icon { width: 36px; height: 36px; border-radius: 11px; font-size: 1rem; }
  .modal-body { padding: 14px 16px calc(16px + env(safe-area-inset-bottom, 0px)); }
  .modal-actions { margin: 4px -16px calc(-16px - env(safe-area-inset-bottom, 0px)); padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px)); bottom: calc(-16px - env(safe-area-inset-bottom, 0px)); }
  .modal-actions .btn { flex: 1; }
  .form-row, .form-grid, .choice-grid { grid-template-columns: 1fr; }
  .user-summary-stats { width: 100%; }
  .user-summary-stats div { flex: 1; min-width: 0; }
  .modal-form .expiry-row input[type="date"] { flex: 1 1 100%; width: 100%; }
}

@keyframes sheetUp { from { transform: translateY(40px); opacity: 0; } }

@media (max-width: 420px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .book-row { grid-auto-columns: 42vw; }
}
