:root {
  --bg: #f2f4f8;
  --card: #ffffff;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #16a34a;
  --green-bg: #ecfdf5;
  --red: #dc2626;
  --red-bg: #fef2f2;
  --text: #1f2937;
  --text-2: #6b7280;
  --text-3: #9ca3af;
  --border: #e5e7eb;
  --radius: 16px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI",
    Roboto, "Helvetica Neue", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

.app { max-width: 780px; margin: 0 auto; min-height: 100vh; padding-bottom: env(safe-area-inset-bottom); }

.view { display: none; padding: 16px; }
.view.active { display: block; }

/* ---------- Header ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: rgba(255, 255, 255, .86);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar .back {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 10px;
  background: #f3f4f6; font-size: 18px; color: var(--text);
  flex: 0 0 auto;
}
.topbar .title { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .spacer { flex: 1; }
.topbar .score {
  font-size: 13px; color: var(--text-2); white-space: nowrap;
}
.topbar .score b { color: var(--green); font-variant-numeric: tabular-nums; }
.topbar .score b.wrong { color: var(--red); }

.progress-wrap { position: sticky; top: 61px; z-index: 19; background: #fff; border-bottom: 1px solid var(--border); padding: 8px 16px 10px; }
.progress-bar { height: 6px; background: #eef0f4; border-radius: 999px; overflow: hidden; }
.progress-inner { height: 100%; background: var(--primary); border-radius: 999px; width: 0; transition: width .25s ease; }
.progress-num { font-size: 12px; color: var(--text-2); margin-top: 4px; font-variant-numeric: tabular-nums; }

/* ---------- Home ---------- */
.hero { padding: 26px 8px 8px; }
.hero h1 { font-size: 26px; font-weight: 800; letter-spacing: -.5px; }
.hero .sub { color: var(--text-2); font-size: 14px; margin-top: 4px; }
.hero .stats { margin-top: 14px; display: flex; gap: 8px; flex-wrap: wrap; }
.hero .stats .pill { background: #fff; border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; font-size: 12.5px; color: var(--text-2); box-shadow: var(--shadow); }
.hero .stats .pill b { color: var(--text); font-variant-numeric: tabular-nums; }

.section-title { font-size: 16px; font-weight: 700; margin: 26px 6px 12px; display: flex; align-items: center; gap: 8px; }
.section-title .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--primary); }

.set-list { display: flex; flex-direction: column; gap: 12px; }

.set-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); transition: transform .12s ease, border-color .12s ease;
}
.set-card:active { transform: scale(.985); border-color: var(--primary); }
.set-card .row1 { display: flex; align-items: center; gap: 10px; }
.set-card .badge {
  flex: 0 0 auto; width: 44px; height: 44px; border-radius: 12px; background: #eef2ff; color: var(--primary);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 15px;
}
.set-card .name { font-weight: 700; font-size: 15.5px; line-height: 1.4; }
.set-card .count { font-size: 12.5px; color: var(--text-2); margin-top: 3px; font-variant-numeric: tabular-nums; }
.set-card .type-tags { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 6px; }

.tag { display: inline-block; padding: 3px 9px; border-radius: 8px; font-size: 12px; background: #f3f4f6; color: var(--text-2); }
.tag.single { background: #eff6ff; color: #2563eb; }
.tag.multi  { background: #fdf4ff; color: #a21caf; }
.tag.judge  { background: #ecfdf5; color: #16a34a; }
.tag.short, .tag.case, .tag.writing { background: #fffbeb; color: #b45309; }

.toolbar-row { display: flex; gap: 10px; margin: 0 4px 8px; }
.toolbar-row button {
  flex: 1; padding: 12px; border-radius: 12px; font-size: 14px; font-weight: 600;
  background: #fff; border: 1px solid var(--border); box-shadow: var(--shadow);
}
.toolbar-row button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }

/* ---------- Mode panel (bottom sheet) ---------- */
.sheet-mask {
  position: fixed; inset: 0; background: rgba(17, 24, 39, .45); z-index: 40;
  display: none; align-items: flex-end; justify-content: center;
}
.sheet-mask.open { display: flex; }
.sheet {
  width: 100%; max-width: 780px; background: #fff; border-radius: 20px 20px 0 0;
  padding: 12px 16px calc(20px + env(safe-area-inset-bottom)); animation: slideUp .22s ease;
  max-height: 86vh; overflow-y: auto;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: .5; } to { transform: translateY(0); opacity: 1; } }
.sheet .grip { width: 40px; height: 4px; background: #e5e7eb; border-radius: 999px; margin: 4px auto 14px; }
.sheet h3 { font-size: 17px; font-weight: 700; text-align: center; margin-bottom: 4px; }
.sheet .sheet-sub { text-align: center; color: var(--text-2); font-size: 13px; margin-bottom: 16px; }

.mode-btn {
  width: 100%; text-align: left; padding: 15px 16px; border-radius: 12px; margin-bottom: 10px;
  background: #f8fafc; border: 1px solid var(--border); font-size: 15px; font-weight: 600;
  display: flex; align-items: center; gap: 12px; transition: background .12s ease;
}
.mode-btn:active { background: #eef2ff; }
.mode-btn .ico { font-size: 20px; width: 24px; text-align: center; }
.mode-btn .desc { font-weight: 400; font-size: 12.5px; color: var(--text-2); margin-top: 2px; }

.type-chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 12px; }
.type-chips .chip {
  padding: 9px 14px; border-radius: 999px; background: #fff; border: 1px solid var(--border);
  font-size: 13.5px; font-weight: 600; display: flex; align-items: center; gap: 6px;
}
.type-chips .chip span { color: var(--text-2); font-weight: 400; }
.type-chips .chip:active { border-color: var(--primary); color: var(--primary); }

/* ---------- Quiz ---------- */
.question-meta { display: flex; align-items: center; gap: 8px; margin: 4px 2px 12px; flex-wrap: wrap; }
.qtype { font-size: 12.5px; font-weight: 600; color: var(--primary); background: #eff6ff; padding: 3px 10px; border-radius: 8px; }
.qmodule { font-size: 12.5px; color: var(--text-2); background: #f3f4f6; padding: 3px 10px; border-radius: 8px; }
.qno { font-size: 12.5px; color: var(--text-3); margin-left: auto; font-variant-numeric: tabular-nums; }

.stem {
  font-size: 17px; font-weight: 600; line-height: 1.75; padding: 16px; background: var(--card);
  border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow);
  white-space: pre-wrap; word-break: break-word;
}

.hint { font-size: 13px; color: var(--text-2); margin: 14px 4px 8px; }

.opt-list { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.opt {
  display: flex; align-items: flex-start; gap: 12px; padding: 14px 14px;
  background: var(--card); border: 1.5px solid var(--border); border-radius: 14px;
  font-size: 15.5px; line-height: 1.55; text-align: left; transition: border-color .12s ease, background .12s ease;
  white-space: pre-wrap; word-break: break-word;
}
.opt .letter {
  flex: 0 0 auto; width: 26px; height: 26px; border-radius: 8px; background: #f3f4f6;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13.5px;
  color: var(--text-2); margin-top: 1px;
}
.opt:active { border-color: var(--primary); background: #f8faff; }
.opt.selected { border-color: var(--primary); background: #eff6ff; }
.opt.selected .letter { background: var(--primary); color: #fff; }
.opt.correct { border-color: var(--green); background: var(--green-bg); }
.opt.correct .letter { background: var(--green); color: #fff; }
.opt.wrong { border-color: var(--red); background: var(--red-bg); }
.opt.wrong .letter { background: var(--red); color: #fff; }
.opt.disabled { pointer-events: none; opacity: .92; }

.judge-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 4px; }
.judge-btn {
  padding: 26px 12px; border-radius: 14px; font-size: 20px; font-weight: 700;
  background: var(--card); border: 1.5px solid var(--border);
}
.judge-btn.true { color: var(--green); }
.judge-btn.false { color: var(--red); }
.judge-btn.correct { border-color: var(--green); background: var(--green-bg); }
.judge-btn.wrong { border-color: var(--red); background: var(--red-bg); }

.submit-row { margin-top: 16px; }
.btn-block {
  width: 100%; padding: 15px; border-radius: 13px; font-size: 16px; font-weight: 700;
  background: var(--primary); color: #fff; transition: opacity .12s ease;
}
.btn-block:disabled { opacity: .4; cursor: not-allowed; }
.btn-block.ghost { background: #fff; color: var(--primary); border: 1.5px solid var(--primary); }

/* ---------- Feedback ---------- */
.feedback { margin-top: 16px; border-radius: var(--radius); border: 1px solid var(--border); overflow: hidden; box-shadow: var(--shadow); }
.feedback .head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; font-weight: 800; font-size: 16px; }
.feedback .head .emo { font-size: 22px; }
.feedback.right .head { background: var(--green-bg); color: var(--green); border-bottom: 1px solid #d1fae5; }
.feedback.wrong .head { background: var(--red-bg); color: var(--red); border-bottom: 1px solid #fee2e2; }
.feedback .body { padding: 14px 16px; background: #fff; font-size: 14.5px; }
.feedback .body .line { margin-bottom: 10px; }
.feedback .body .line:last-child { margin-bottom: 0; }
.feedback .body .label { font-weight: 700; color: var(--text-2); font-size: 12.5px; display: block; margin-bottom: 2px; }
.feedback .body .value { white-space: pre-wrap; word-break: break-word; }
.feedback .body .answer-correct { color: var(--green); font-weight: 700; }
.feedback .body .answer-wrong { color: var(--red); font-weight: 700; }

.ref-answer { margin-top: 16px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.ref-answer .rh { padding: 12px 16px; font-weight: 700; font-size: 14px; background: #fffbeb; color: #b45309; border-bottom: 1px solid #fef3c7; }
.ref-answer .rb { padding: 14px 16px; font-size: 14.5px; white-space: pre-wrap; word-break: break-word; line-height: 1.7; }
.self-assess { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.self-assess button { padding: 14px; border-radius: 12px; font-weight: 700; font-size: 15px; }
.self-assess .ok { background: var(--green); color: #fff; }
.self-assess .no { background: var(--red); color: #fff; }

/* ---------- Result ---------- */
.result-card { background: var(--card); border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow); padding: 28px 20px; text-align: center; margin-top: 8px; }
.result-ring { width: 120px; height: 120px; margin: 0 auto 16px; border-radius: 50%; position: relative; display: flex; align-items: center; justify-content: center; }
.result-ring .pct { font-size: 26px; font-weight: 800; }
.result-ring .sub { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 8px 0 20px; }
.result-grid .cell { background: #f8fafc; border-radius: 12px; padding: 12px; }
.result-grid .cell .num { font-size: 22px; font-weight: 800; }
.result-grid .cell .lab { font-size: 12px; color: var(--text-2); }
.result-grid .cell.g .num { color: var(--green); }
.result-grid .cell.r .num { color: var(--red); }

.wrong-list { margin-top: 6px; display: flex; flex-direction: column; gap: 10px; }
.wrong-item { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 14px; box-shadow: var(--shadow); }
.wrong-item .w-top { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.wrong-item .w-stem { font-size: 14px; white-space: pre-wrap; word-break: break-word; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; margin-bottom: 8px; }
.wrong-item .w-ans { font-size: 13px; color: var(--text-2); }
.wrong-item .w-ans b { color: var(--green); }

/* ---------- Utility ---------- */
.hidden { display: none !important; }
.empty { text-align: center; color: var(--text-2); padding: 48px 20px; font-size: 14.5px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }