:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #6b7785;
  --line: #e4e8ec;
  --accent: #3b82f6;
  --accent-d: #2563eb;
  --accent-soft: #e8f0fe;
  --t2: #0ea5e9;
  --t2-soft: #e3f5fd;
  --t3: #8b5cf6;
  --t3-soft: #f1ecfe;
  --t4: #f59e0b;
  --t4-d: #b45309;
  --t4-soft: #fef3c7;
  --ok: #16a34a;
  --ok-soft: #e8f7ee;
  --wrong: #ef4444;
  --wrong-soft: #fdeaea;
  --shadow: 0 1px 3px rgba(16,24,40,.08), 0 6px 20px rgba(16,24,40,.06);
  --radius: 16px;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* Boot error banner */
.boot-error {
  margin: 12px 16px 0;
  padding: 12px 16px;
  border: 1px solid #f3c2c2;
  background: var(--wrong-soft);
  color: #a12626;
  border-radius: 12px;
  font-size: 13.5px;
  line-height: 1.6;
}
.boot-error b { color: #8a1f1f; }
.boot-error code {
  background: #fff; border: 1px solid #f0cfcf;
  border-radius: 6px; padding: 1px 6px; font-size: 12.5px;
}

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-text h1 { margin: 0; font-size: 18px; letter-spacing: .3px; }
.brand-text p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.mode-switch { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 999px; padding: 3px; flex-wrap: wrap; }
.mode-btn {
  border: 0; background: transparent; cursor: pointer;
  padding: 8px 14px; border-radius: 999px; font-size: 13.5px; color: var(--muted); font-weight: 600;
  transition: all .15s; white-space: nowrap;
}
.mode-btn.active { background: var(--accent); color: #fff; box-shadow: var(--shadow); }
.mode-btn[data-mode="T2"].active { background: var(--t2); }
.mode-btn[data-mode="T3"].active { background: var(--t3); }
.mode-btn[data-mode="T4"].active { background: var(--t4); }

/* Layout */
.layout { display: grid; grid-template-columns: 320px 1fr; height: calc(100vh - 61px); }
.sidebar { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.search-box { padding: 14px; border-bottom: 1px solid var(--line); }
.search-box input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px;
  font-size: 13.5px; outline: none; font-family: inherit; color: var(--ink); background: var(--bg);
}
.search-box input:focus { border-color: var(--accent); background: #fff; }
.tree { flex: 1; overflow: auto; padding: 8px 0 16px; }
.tree-foot { padding: 10px 14px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }

.tnode { user-select: none; }
.tdate {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 14px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--ink);
  border-left: 3px solid transparent;
}
.tdate:hover { background: var(--bg); }
.tdate .caret { font-size: 10px; color: var(--muted); width: 10px; }
.tdate .cnt { margin-left: auto; font-size: 11.5px; color: var(--muted); font-weight: 500; }
.tdate .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.tdate[data-mode="T2"] .dot { background: var(--t2); }
.tdate[data-mode="T3"] .dot { background: var(--t3); }
.tdate[data-mode="T4"] .dot { background: var(--t4); }

.titem {
  display: block; padding: 7px 14px 7px 30px; font-size: 13px; color: #46505c;
  cursor: pointer; border-left: 3px solid transparent; line-height: 1.45;
}
.titem:hover { background: var(--bg); color: var(--ink); }
.titem.active { background: var(--accent-soft); color: var(--accent-d); border-left-color: var(--accent); font-weight: 600; }
.titem[data-mode="T2"].active { background: var(--t2-soft); color: #0369a1; border-left-color: var(--t2); }
.titem[data-mode="T3"].active { background: var(--t3-soft); color: #6d28d9; border-left-color: var(--t3); }
.titem[data-mode="T4"].active { background: var(--t4-soft); color: var(--t4-d); border-left-color: var(--t4); }
.titem .badge { font-size: 10.5px; color: var(--muted); margin-left: 6px; }
.tgroup.collapsed .tchildren { display: none; }

/* Stage */
.stage { overflow: auto; padding: 18px 24px 60px; }
.breadcrumb { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 22px;
  max-width: 980px;
}
.scene-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.tag {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 700;
  background: var(--accent-soft); color: var(--accent-d); letter-spacing: .3px;
}
.tag[data-mode="T2"] { background: var(--t2-soft); color: #0369a1; }
.tag[data-mode="T3"] { background: var(--t3-soft); color: #6d28d9; }
.tag[data-mode="T4"] { background: var(--t4-soft); color: var(--t4-d); }
.scenario { font-size: 16.5px; font-weight: 600; }
.qcount { margin-left: auto; font-size: 12.5px; color: var(--muted); }

/* Player */
.player-card {
  display: flex; align-items: center; gap: 14px;
  margin: 16px 0 4px; padding: 12px 16px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px;
}
.play-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 16px; line-height: 1;
  display: flex; align-items: center; justify-content: center; flex: 0 0 auto;
  transition: transform .12s, background .15s;
}
.play-btn:hover { background: var(--accent-d); }
.play-btn:active { transform: scale(.94); }
.player-main { flex: 1; min-width: 0; }
.progress { height: 6px; background: #dfe4e9; border-radius: 999px; cursor: pointer; position: relative; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); border-radius: 999px; transition: width .08s linear; }
.player-meta { display: flex; justify-content: space-between; margin-top: 6px; font-size: 11.5px; color: var(--muted); }
.player-tools { display: flex; gap: 6px; flex: 0 0 auto; }
.btn-mini {
  border: 1px solid var(--line); background: #fff; border-radius: 9px; cursor: pointer;
  padding: 6px 10px; font-size: 12.5px; color: var(--ink); font-family: inherit; font-weight: 600;
}
.btn-mini:hover { border-color: var(--accent); color: var(--accent); }
.player-card.audio-missing { opacity: .65; }

/* Transcript / audio text */
.transcript-wrap { margin: 12px 0 0; }
.toggle-btn {
  border: 1px dashed var(--line); background: #fff; border-radius: 10px; cursor: pointer;
  padding: 8px 14px; font-size: 13px; color: var(--muted); font-family: inherit; font-weight: 600;
  display: inline-flex; align-items: center; gap: 7px;
}
.toggle-btn:hover { border-color: var(--accent); color: var(--accent); }
.toggle-btn .ic { font-size: 12px; }
.transcript-box {
  margin-top: 10px; padding: 14px 16px; border-radius: 12px;
  background: #f8fafc; border: 1px solid var(--line);
  font-size: 14px; line-height: 1.85; color: #2f3944;
}
.transcript-box .tb-label { display: block; font-size: 11.5px; color: var(--muted); font-weight: 700; letter-spacing: .4px; margin-bottom: 7px; }

/* Questions */
#articleBody { margin-top: 16px; }
.q-block { padding: 14px 0; border-top: 1px solid var(--line); }
.q-block:first-child { border-top: 0; padding-top: 6px; }
.q-stem-row { display: flex; align-items: flex-start; gap: 9px; margin-bottom: 10px; }
.q-no {
  flex: 0 0 auto; min-width: 26px; height: 22px; padding: 0 6px;
  border-radius: 6px; background: var(--accent-soft); color: var(--accent-d);
  font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
}
.q-block[data-mode="T2"] .q-no { background: var(--t2-soft); color: #0369a1; }
.q-block[data-mode="T3"] .q-no { background: var(--t3-soft); color: #6d28d9; }
.q-block[data-mode="T4"] .q-no { background: var(--t4-soft); color: var(--t4-d); }
.q-stem { font-size: 15px; line-height: 1.6; font-weight: 600; flex: 1; }

.audio-line {
  margin: 0 0 10px 35px; padding: 8px 12px; border-radius: 9px;
  background: var(--accent-soft); border-left: 3px solid var(--accent);
  font-size: 14px; color: #1e3a5f; line-height: 1.7;
  display: flex; align-items: center; gap: 10px;
}
.audio-line .q-play {
  flex: 0 0 auto; width: 26px; height: 26px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 10px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.audio-line .q-play:hover { background: var(--accent-d); }
.audio-line .audio-text { flex: 1; }

.opts { display: grid; gap: 7px; margin-left: 35px; }
.opt {
  display: flex; align-items: flex-start; gap: 10px; cursor: pointer;
  padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; font-size: 14px; line-height: 1.55; transition: all .12s;
}
.opt:hover { border-color: var(--accent); background: #fbfdff; }
.opt input { margin: 2px 0 0; accent-color: var(--accent); flex: 0 0 auto; }
.opt .key { font-weight: 700; color: var(--muted); flex: 0 0 auto; min-width: 15px; }
.opt.sel { border-color: var(--accent); background: var(--accent-soft); }
.opt.correct { border-color: var(--ok); background: var(--ok-soft); }
.opt.correct .key { color: var(--ok); }
.opt.wrong { border-color: var(--wrong); background: var(--wrong-soft); }
.opt.wrong .key { color: var(--wrong); }
.opt.locked { cursor: default; }
.q-ansline { margin: 9px 0 0 35px; font-size: 13px; color: var(--muted); }
.q-ansline b { color: var(--ok); }

/* Per-question countdown (T1) - sits directly under its question */
.q-timer {
  display: inline-flex; align-items: center; gap: 9px;
  margin: 10px 0 2px 35px; padding: 5px 12px 5px 6px;
  border: 1px solid var(--line); border-radius: 999px;
  background: #fbfcfe; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.q-timer:hover { border-color: var(--accent); background: var(--accent-soft); }
.qt-ring { transform: rotate(-90deg); flex: 0 0 auto; }
.qt-bg { fill: none; stroke: #e6eaee; stroke-width: 4; }
.qt-fg {
  fill: none; stroke: var(--accent); stroke-width: 4; stroke-linecap: round;
  transition: stroke-dashoffset .9s linear, stroke .3s;
}
.qt-fg.warn { stroke: var(--wrong); }
.qt-body { display: flex; flex-direction: column; line-height: 1.25; }
.qt-num { font-size: 13.5px; font-weight: 700; font-variant-numeric: tabular-nums; }
.qt-num.warn { color: var(--wrong); }
.qt-status { font-size: 11px; color: var(--muted); white-space: nowrap; }
/* counting down right now */
.q-timer.qt-running { border-color: var(--accent); background: var(--accent-soft); }
/* closed: the student moved on to the next question, or the clock ran out */
.q-timer.qt-closed { background: #f6f7f9; opacity: .72; }
.q-timer.qt-closed .qt-num { font-weight: 600; color: var(--muted); }
.q-timer.qt-closed .qt-fg { stroke: #c3cad2; }

/* Countdown */
.countdown-wrap { position: relative; width: 112px; height: 112px; margin: 20px auto 6px; }
.ring { transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: #e6eaee; stroke-width: 8; }
.ring-fg { fill: none; stroke: var(--accent); stroke-width: 8; stroke-linecap: round; transition: stroke-dashoffset .9s linear, stroke .3s; }
.ring-fg.warn { stroke: var(--wrong); }
.ring-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.cd-num { font-size: 26px; font-weight: 700; letter-spacing: .5px; font-variant-numeric: tabular-nums; }
.cd-num.warn { color: var(--wrong); }
.cd-status { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* Actions */
.actions { display: flex; gap: 10px; justify-content: center; margin: 14px 0 4px; flex-wrap: wrap; }
.btn-primary {
  border: 0; background: var(--accent); color: #fff; font-family: inherit; font-weight: 700;
  padding: 11px 26px; border-radius: 11px; font-size: 14.5px; cursor: pointer; box-shadow: var(--shadow);
}
.btn-primary:hover { background: var(--accent-d); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink); font-family: inherit; font-weight: 600;
  padding: 10px 18px; border-radius: 11px; font-size: 14px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.nav { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.nav-pos { font-size: 12.5px; color: var(--muted); }
.hint { text-align: center; font-size: 12px; color: var(--muted); margin: 12px 0 0; line-height: 1.6; }

/* Result */
.result { margin-top: 16px; padding: 14px 16px; border-radius: 12px; background: #f8fafc; border: 1px solid var(--line); font-size: 13.5px; line-height: 1.8; }
.result .r-head {
  font-weight: 700; margin-bottom: 6px;
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
}
.result .r-score {
  font-size: 15px; color: var(--accent-d); font-variant-numeric: tabular-nums;
}

/* Empty state */
.empty { padding: 40px 20px; text-align: center; color: var(--muted); font-size: 14px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { max-height: 42vh; }
  .stage { padding: 16px 14px 50px; }
  .opt, .audio-line, .q-ansline { margin-left: 0; }
  .opts { margin-left: 0; }
}

/* ===== Mobile drawer controls (hidden on desktop) ===== */
.menu-btn { display: none; }
.drawer-close { display: none; }
.sidebar-backdrop { display: none; }

/* ===== Phone layout (<= 760px) ===== */
@media (max-width: 760px) {
  body.drawer-open { overflow: hidden; }

  .topbar {
    padding: 10px 12px;
    gap: 8px;
    align-items: center;
  }
  .brand { gap: 9px; min-width: 0; flex: 1 1 auto; }
  .brand-text { min-width: 0; }
  .brand-text h1 {
    font-size: 15px;
    line-height: 1.25;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .brand-text p {
    font-size: 10.5px;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* hamburger / 目录 button */
  .menu-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    flex: 0 0 auto;
    cursor: pointer;
    border: 1px solid var(--line);
    background: var(--bg);
    color: var(--ink);
    border-radius: 10px;
    padding: 8px 10px;
    font-family: inherit;
    font-size: 12.5px;
    font-weight: 700;
    /*
     * 触控友好：最小 44px 高度（iOS HIG / Material 推荐）。
     * 用 min-height 而非固定 height，避免 font 放大时裁切。
     */
    min-height: 44px;
  }
  .menu-btn:active { transform: scale(.97); }
  .menu-btn .menu-ic { font-size: 15px; line-height: 1; }

  /* mode switch: 2x2 grid keeps all four types visible on a phone */
  .mode-switch {
    width: 100%;
    flex-wrap: wrap;
    gap: 4px;
    padding: 4px;
    border-radius: 14px;
  }
  .mode-btn {
    flex: 1 1 calc(50% - 4px);
    padding: 9px 8px;
    font-size: 12.5px;
    min-height: 40px;
  }

  /* explicit close button inside the drawer */
  .search-box { display: flex; align-items: center; gap: 8px; }
  .search-box input { flex: 1 1 auto; min-width: 0; }
  .drawer-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 10px;
    color: var(--muted);
    font-size: 16px;
    cursor: pointer;
  }

  /* single column; sidebar becomes an off-canvas drawer */
  .layout {
    display: block;
    height: auto;
    min-height: calc(100vh - 58px);
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 340px);
    max-height: none;
    z-index: 60;
    border-right: 0;
    transform: translateX(-102%);
    transition: transform .26s ease;
    box-shadow: 0 10px 40px rgba(16, 24, 40, .28);
    padding-top: env(safe-area-inset-top);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(15, 23, 42, .42);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  .sidebar-backdrop.show { opacity: 1; pointer-events: auto; }

  .stage {
    padding: 12px 12px calc(44px + env(safe-area-inset-bottom));
  }
  .breadcrumb { font-size: 11.5px; margin-bottom: 8px; }
  .card { padding: 14px 14px 18px; border-radius: 14px; }
  .scene-head { gap: 8px; padding-bottom: 11px; }
  .scenario { font-size: 15px; }
  .qcount { font-size: 11.5px; }

  .player-card { gap: 10px; padding: 10px 12px; margin: 12px 0 4px; }
  .play-btn { width: 42px; height: 42px; }
  .player-tools { gap: 5px; }
  .btn-mini { padding: 7px 9px; font-size: 12px; min-height: 36px; }

  .transcript-box { font-size: 13.5px; line-height: 1.8; padding: 12px 13px; }
  .q-stem { font-size: 14.5px; }
  .q-block { padding: 12px 0; }
  .audio-line { font-size: 13.5px; margin-left: 0; }
  .opts { margin-left: 0; gap: 8px; }
  .opt { font-size: 14px; padding: 11px 12px; }
  .q-ansline { margin-left: 0; }
  .q-timer { margin-left: 0; }

  /* shrink the countdown ring so it fits narrow screens */
  .countdown-wrap { width: 96px; height: 96px; margin: 16px auto 4px; }
  .countdown-wrap .ring { width: 96px; height: 96px; }
  .cd-num { font-size: 23px; }

  .actions { gap: 8px; }
  .actions .btn,
  .actions .btn-primary {
    flex: 1 1 auto;
    min-width: 44%;
    padding: 12px 14px;
    min-height: 46px;
  }
  .nav { gap: 8px; flex-wrap: wrap; }
  .nav .btn { padding: 10px 12px; font-size: 13px; min-height: 44px; }
  .nav-pos { font-size: 11.5px; }
  .hint { font-size: 11px; }

  /* prevent long English words from forcing horizontal scroll */
  .stage,
  .card,
  .q-stem,
  .opt,
  .transcript-box { overflow-wrap: anywhere; }
}

/* Very narrow phones (<= 380px): trim the brand further. */
@media (max-width: 380px) {
  .brand-text p { display: none; }
  .brand-text h1 { font-size: 14px; }
  .menu-btn { padding: 8px 9px; }
  .menu-btn .menu-tx { display: none; }
}
