:root {
  --bg: #f5f7fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-soft: #dbeafe;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.2rem; margin: 0; }
.tabs { display: flex; gap: 6px; }
.tab {
  border: 1px solid var(--border); background: var(--bg);
  padding: 8px 18px; border-radius: 8px; cursor: pointer; font-size: 0.95rem;
}
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.panel { display: none; }
.panel.active { display: flex; min-height: calc(100vh - 58px); }

.sidebar {
  width: 290px; flex-shrink: 0;
  background: var(--card); border-right: 1px solid var(--border);
  padding: 12px; overflow-y: auto;
  position: sticky; top: 58px; height: calc(100vh - 58px);
}
.sidebar input[type=search] {
  width: 100%; padding: 8px 10px; margin-bottom: 10px;
  border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem;
}
.topic-list { list-style: none; margin: 0; padding: 0; }
.topic-list li {
  padding: 9px 10px; border-radius: 6px; cursor: pointer;
  display: flex; justify-content: space-between; gap: 8px; font-size: 0.92rem;
}
.topic-list li:hover { background: var(--bg); }
.topic-list li.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.topic-list .count { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }

.content { flex: 1; padding: 20px 28px; max-width: 1000px; }
.content-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
.content-head h2 { margin: 0; }
.toggle { font-size: 0.9rem; color: var(--muted); cursor: pointer; }

h3.subsection {
  margin: 28px 0 10px; padding-bottom: 6px;
  border-bottom: 2px solid var(--primary-soft); color: var(--primary);
}

.qcard {
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 18px; margin: 12px 0;
}
.qtext { font-weight: 600; margin-bottom: 10px; white-space: pre-line; line-height: 1.45; }
.qimgs img { max-width: 100%; height: auto; margin: 4px 0 10px; border-radius: 6px; }
.options { list-style: none; margin: 0; padding: 0; }
.options li {
  padding: 7px 10px; margin: 4px 0; border-radius: 6px; border: 1px solid var(--border);
}
.options li.correct { background: var(--ok-soft); border-color: var(--ok); font-weight: 600; }
.options li.wrong { background: var(--bad-soft); border-color: var(--bad); }
.explain { margin-top: 10px; font-size: 0.9rem; color: var(--muted); line-height: 1.45; }
.hidden-ans .options li.correct { background: none; border-color: var(--border); font-weight: normal; }
.hidden-ans .explain { display: none; }
.hidden-ans { cursor: pointer; }

.empty { color: var(--muted); padding: 40px 0; text-align: center; }

/* Test mode */
.setup { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 20px; max-width: 520px; }
.setup label { display: block; margin: 12px 0 6px; font-weight: 600; }
.setup select { padding: 8px; border-radius: 6px; border: 1px solid var(--border); font-size: 0.95rem; }
.btn {
  background: var(--primary); color: #fff; border: none; border-radius: 8px;
  padding: 10px 20px; font-size: 0.95rem; cursor: pointer;
}
.btn.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }

.test-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; }
.progress { height: 6px; background: var(--border); border-radius: 3px; margin: 10px 0 4px; overflow: hidden; }
.progress div { height: 100%; background: var(--primary); transition: width .2s; }

.choice {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 12px; margin: 6px 0; border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
}
.choice:hover { background: var(--bg); }
.choice.selected { background: var(--primary-soft); border-color: var(--primary); }
.choice input { margin-top: 3px; }

.palette { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 20px; }
.palette button {
  width: 36px; height: 36px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--card); cursor: pointer; font-size: 0.85rem;
}
.palette button.answered { background: var(--primary-soft); border-color: var(--primary); }
.palette button.current { outline: 2px solid var(--primary); font-weight: 700; }

.result-box {
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 20px; text-align: center;
}
.score { font-size: 2.6rem; font-weight: 700; margin: 6px 0; }
.stats { display: flex; justify-content: center; gap: 24px; flex-wrap: wrap; color: var(--muted); }
.stats b { color: var(--text); }
.tag { display: inline-block; font-size: 0.75rem; padding: 2px 8px; border-radius: 10px; margin-left: 6px; }
.tag.ok { background: var(--ok-soft); color: var(--ok); }
.tag.bad { background: var(--bad-soft); color: var(--bad); }
.tag.skip { background: var(--border); color: var(--muted); }

@media (max-width: 760px) {
  .panel.active { flex-direction: column; }
  .sidebar { width: 100%; height: auto; max-height: 40vh; position: static; border-right: none; border-bottom: 1px solid var(--border); }
  .content { padding: 16px; }
}

.note {
  margin-top: 10px; padding: 8px 10px; font-size: 0.88rem; line-height: 1.45;
  background: #fef9c3; border-left: 3px solid #ca8a04; border-radius: 4px; color: #713f12;
}
.hidden-ans .note { display: none; }

.topic-list li.group-head {
  cursor: default; padding: 14px 10px 6px; font-size: 0.72rem; font-weight: 700;
  letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted);
}
.topic-list li.group-head:hover { background: none; }
.topic-list li.group-head:first-child { padding-top: 4px; }
