:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1c2333;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-2: #6366f1;
  --green: #16a34a;
  --red: #dc2626;
  --border: #e5e7eb;
  --radius: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.wrap {
  max-width: 640px;
  margin: 0 auto;
  padding: 32px 16px 48px;
}

header { text-align: center; margin-bottom: 20px; }
h1 { margin: 0 0 4px; font-size: 26px; letter-spacing: 1px; }
.sub { margin: 0; color: var(--muted); font-size: 13px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.06);
  padding: 20px;
  margin-bottom: 16px;
}

/* ---------- settings ---------- */
.settings { display: flex; flex-direction: column; gap: 14px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.setting > label { font-size: 14px; color: var(--muted); }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button {
  border: none; background: var(--card); padding: 8px 14px;
  font-size: 14px; cursor: pointer; color: var(--text);
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent); color: #fff; }

.check { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; user-select: none; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }

/* ---------- tabs ---------- */
.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
}
.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 15px;
  padding: 10px 0;
  border-radius: 12px;
  cursor: pointer;
}
.tab.active { background: var(--card); color: var(--text); font-weight: 700; box-shadow: 0 1px 3px rgba(16,24,40,.1); }
.tab:disabled { opacity: .5; cursor: default; }

/* ---------- challenge ---------- */
#chIdle { text-align: center; padding: 8px 0; }
#chIdle h3 { margin: 12px 0 8px; font-size: 20px; }
#chIdle p { margin: 0 0 12px; color: var(--muted); font-size: 14px; line-height: 1.7; }
.ch-illus { font-size: 44px; line-height: 1; }
.ch-actions { margin-top: 18px; }
.btn.big { padding: 12px 34px; font-size: 18px; border-radius: 12px; }
.btn.sm { padding: 5px 12px; font-size: 13px; }

.ch-run-head { text-align: center; margin-bottom: 6px; }
.ch-timer {
  font-family: "Cambria Math", Georgia, serif;
  font-size: 52px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: 2px;
  font-variant-numeric: tabular-nums;
}
.ch-timer.warn { color: var(--red); }
.ch-progress { margin-top: 6px; color: var(--muted); font-size: 14px; }
.ch-progress b { color: var(--accent); font-size: 18px; }

.cfb { margin-top: 14px; text-align: center; font-size: 18px; font-weight: 700; min-height: 26px; }
.cfb.ok { color: var(--green); }
.cfb.no { color: var(--red); }

.result-grid { display: flex; justify-content: center; gap: 28px; margin-top: 16px; }
.res { text-align: center; }
.res span { display: block; font-size: 34px; font-weight: 700; color: var(--accent); }
.res label { font-size: 12px; color: var(--muted); }
.result-note { margin-top: 14px; color: var(--muted); font-size: 13px; }
#chResult h3 { text-align: center; margin: 4px 0 0; font-size: 22px; }
#chResult .ch-actions { text-align: center; }

/* ---------- history ---------- */
.history-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.history-head h2 { margin: 0; font-size: 18px; }

.history-filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.history-filters select {
  flex: 1;
  min-width: 110px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.history-list { overflow-x: auto; }
.history-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.history-table th, .history-table td { padding: 9px 8px; text-align: center; white-space: nowrap; }
.history-table th { color: var(--muted); font-weight: 500; border-bottom: 1px solid var(--border); }
.history-table td { border-bottom: 1px solid var(--border); }
.history-table tr:last-child td { border-bottom: none; }
.history-table .rank { color: var(--muted); }
.history-table .rank.top { color: #d97706; font-weight: 700; }
.history-table .cell-correct { font-weight: 700; color: var(--accent); }
.history-table .cell-acc { color: var(--green); font-weight: 600; }
.history-table .cell-level { color: var(--muted); }

.history-empty { text-align: center; color: var(--muted); padding: 28px 0; font-size: 14px; }

/* ---------- play ---------- */
.matrix-wrap { text-align: center; padding: 12px 0 4px; }
.type-tag {
  display: inline-block; font-size: 12px; color: var(--muted);
  background: var(--bg); border-radius: 999px; padding: 3px 12px; margin-bottom: 12px;
}

.matrix {
  position: relative;
  display: inline-grid;
  gap: 12px 26px;
  padding: 12px 28px;
}
.matrix .cell {
  font-family: "Cambria Math", Georgia, "Times New Roman", serif;
  font-size: 30px;
  text-align: center;
  min-width: 2ch;
}
.matrix::before,
.matrix::after {
  content: "";
  position: absolute;
  top: -8px; bottom: -8px;
  width: 11px;
  border: 3px solid var(--text);
}
.matrix::before { left: 0; border-right: 0; border-radius: 16px 0 0 16px; }
.matrix::after  { right: 0; border-left: 0; border-radius: 0 16px 16px 0; }

.answer-bar { display: flex; gap: 10px; justify-content: center; margin-top: 24px; }
#answer {
  width: 190px;
  font-size: 22px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  text-align: center;
  font-family: "Cambria Math", Georgia, serif;
  color: var(--text);
}
#answer:focus { outline: 2px solid var(--accent-2); border-color: transparent; }
#answer:disabled { background: var(--bg); color: var(--muted); }

.btn { border: none; border-radius: 10px; padding: 10px 22px; font-size: 16px; cursor: pointer; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:disabled { opacity: 0.5; cursor: default; }
.btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); }
.hidden { display: none !important; }

.hint { margin-top: 10px; font-size: 12px; color: var(--muted); }

.feedback { margin-top: 18px; text-align: center; font-size: 18px; font-weight: 700; min-height: 26px; }
.feedback.ok { color: var(--green); }
.feedback.no { color: var(--red); }

.solution {
  margin-top: 10px;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  line-height: 2;
  font-family: "Cambria Math", Georgia, serif;
}
.time { margin-top: 6px; text-align: center; font-size: 12px; color: var(--muted); min-height: 16px; }

/* ---------- stats ---------- */
.stats { display: flex; justify-content: space-around; text-align: center; padding: 16px 8px; }
.stat span { display: block; font-size: 26px; font-weight: 700; }
.stat label { font-size: 12px; color: var(--muted); }

footer { text-align: center; color: var(--muted); font-size: 13px; line-height: 1.7; padding: 0 8px; }

@media (max-width: 420px) {
  .matrix .cell { font-size: 24px; }
  .matrix { gap: 10px 18px; padding: 10px 22px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --card: #181b23;
    --text: #e6e8ef;
    --muted: #9aa1b0;
    --border: #262a36;
    --accent: #6366f1;
    --accent-2: #818cf8;
  }
  .type-tag { background: #0f1117; }
  #answer:disabled { background: #0f1117; }
  .seg button { background: var(--card); }
}
