:root {
  --bg: #f4f6fb;
  --bg-elev: #ffffff;
  --bg-soft: #eef1f8;
  --text: #1a2233;
  --text-dim: #5b6577;
  --border: #dde3ee;
  --accent: #1f6feb;
  --accent-soft: #e6effe;
  --good: #1a9e5c;
  --good-soft: #e2f6ec;
  --bad: #d64545;
  --bad-soft: #fdeaea;
  --warn: #b7791f;
  --shadow: 0 6px 22px rgba(20, 35, 70, .08);
  --radius: 14px;
  --maxw: 880px;
}

/* Тёмная палитра. Нужна в двух местах: явный выбор темы и системная настройка
   (см. медиазапрос ниже) — значения должны совпадать. */
:root[data-theme="dark"] {
  --bg: #0f1420;
  --bg-elev: #171d2b;
  --bg-soft: #1d2434;
  --text: #e8edf7;
  --text-dim: #9aa6bd;
  --border: #2a3448;
  --accent: #4d8dff;
  --accent-soft: #1b2b48;
  --good: #3ecf8e;
  --good-soft: #14301f;
  --bad: #ff6b6b;
  --bad-soft: #351a1a;
  --warn: #e0b34d;
  --shadow: 0 8px 26px rgba(0, 0, 0, .35);
}

/* Системная тёмная тема — если пользователь не выбрал светлую вручную. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1420;
    --bg-elev: #171d2b;
    --bg-soft: #1d2434;
    --text: #e8edf7;
    --text-dim: #9aa6bd;
    --border: #2a3448;
    --accent: #4d8dff;
    --accent-soft: #1b2b48;
    --good: #3ecf8e;
    --good-soft: #14301f;
    --bad: #ff6b6b;
    --bad-soft: #351a1a;
    --warn: #e0b34d;
    --shadow: 0 8px 26px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

button { font-family: inherit; }

/* ---------- Верхняя панель ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  color: var(--text);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 10px;
}
.topbar-title:hover { background: var(--bg-soft); }

.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 38px;
  height: 30px;
  padding: 0 8px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 14px;
  letter-spacing: .5px;
}

.topbar-right { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  width: 38px;
  height: 34px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}
.icon-btn:hover { border-color: var(--accent); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
}
.badge .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

/* ---------- Контейнер ---------- */
main {
  flex: 1 0 auto;
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 22px 16px 40px;
}

.footer {
  flex: 0 0 auto;
  text-align: center;
  color: var(--text-dim);
  font-size: 12.5px;
  padding: 14px 16px 26px;
}

h1 { font-size: 22px; margin: 4px 0 6px; }
h2 { font-size: 17px; margin: 26px 0 10px; }
.subtle { color: var(--text-dim); font-size: 14px; }
.hidden { display: none !important; }

/* ---------- Карточки / секции ---------- */
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- Главный экран ---------- */
.hero { margin-bottom: 20px; }
.hero p { margin: 0; color: var(--text-dim); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 4px 0 14px;
}

.seg {
  display: inline-flex;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 3px;
  gap: 3px;
}
.seg button {
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 7px 13px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
.seg button.active {
  background: var(--bg-elev);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .12);
}

.chip {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text-dim);
  padding: 7px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13.5px;
}
.chip:hover { border-color: var(--accent); color: var(--text); }

/* галочка-переключатель в стиле «чипа» */
.checkchip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text-dim);
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13.5px;
  user-select: none;
  white-space: nowrap;
}
.checkchip:hover { border-color: var(--accent); color: var(--text); }
.checkchip input { margin: 0; accent-color: var(--accent); cursor: pointer; }
.checkchip.small { font-size: 12.5px; padding: 5px 10px; }

.advance-hint { margin: 12px 2px 2px; }

/* группы тем */
.theme-group {
  overflow: hidden;
  margin-bottom: 12px;
}
.theme-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
}
.theme-head .theme-name { font-weight: 700; }
.theme-head .count { color: var(--text-dim); font-size: 13px; margin-left: auto; }

.chapter-list { padding: 6px; display: grid; gap: 4px; }
.chapter {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
}
.chapter:hover { background: var(--bg-soft); }
.chapter input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  flex: 0 0 auto;
}
.chapter .ch-title { flex: 1 1 auto; }
.chapter .ch-code {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  padding: 1px 7px;
  border-radius: 7px;
  flex: 0 0 auto;
}
.chapter .ch-count { font-size: 12.5px; color: var(--text-dim); flex: 0 0 auto; min-width: 34px; text-align: right; }

/* ---------- Кнопки ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 11px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { filter: brightness(1.07); }
.btn-block { width: 100%; padding: 14px; font-size: 16px; }
.btn-ghost { background: transparent; }

.startbar {
  position: sticky;
  bottom: 0;
  z-index: 10;
  padding: 12px 0 0;
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}
.startbar .btn-block { box-shadow: var(--shadow); }
.startbar .hint { text-align: center; margin-top: 8px; }
.startbar .hint:empty { display: none; }

/* ---------- Экран вопроса ---------- */
.quiz-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.quiz-head .q-chapter { font-weight: 700; }
.quiz-head .q-theme { color: var(--text-dim); font-size: 13px; }
.quiz-head .q-counter { margin-left: auto; color: var(--text-dim); font-variant-numeric: tabular-nums; }

.progress {
  height: 7px;
  border-radius: 999px;
  background: var(--bg-soft);
  overflow: hidden;
  margin-bottom: 16px;
  border: 1px solid var(--border);
}
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .25s ease; }

.q-card { padding: 18px; margin-bottom: 14px; }
.q-text { font-size: 18px; font-weight: 600; line-height: 1.45; white-space: pre-line; }

.q-image {
  margin: 14px 0 0;
  text-align: center;
}
.q-image img {
  max-width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  cursor: zoom-in;
}

.options { display: grid; gap: 9px; margin-bottom: 14px; }

.option {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 13px 14px;
  border: 1.5px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 12px;
  cursor: pointer;
  font-size: 15.5px;
  line-height: 1.45;
  transition: border-color .12s, background .12s;
}
.option:hover:not(.locked) { border-color: var(--accent); }
.option .num {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--text-dim);
}
.option.selected { border-color: var(--accent); background: var(--accent-soft); }
.option.selected .num { border-color: var(--accent); color: var(--accent); }
.option.locked { cursor: default; }
.option.correct { border-color: var(--good); background: var(--good-soft); }
.option.correct .num { border-color: var(--good); background: var(--good); color: #fff; }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.option.wrong .num { border-color: var(--bad); background: var(--bad); color: #fff; }

.explanation {
  margin-top: 8px;
  padding: 15px 16px;
  border-radius: 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
}
.explanation h3 {
  margin: 0 0 8px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-dim);
}
.explanation p { margin: 0 0 10px; white-space: pre-line; }
.explanation p:last-child { margin-bottom: 0; }

.verdict {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  margin-bottom: 12px;
}
.verdict.skip { color: var(--warn); }

.quiz-nav {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.quiz-nav .spacer { flex: 1 1 auto; }
.quiz-nav .btn { flex: 0 0 auto; }
@media (max-width: 520px) {
  .quiz-nav .btn { flex: 1 1 auto; }
}

/* ---------- Результаты ---------- */
.score-ring {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  padding: 20px;
}
.ring {
  --p: 0;
  width: 116px;
  height: 116px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: conic-gradient(var(--good) calc(var(--p) * 1%), var(--bg-soft) 0);
  display: grid;
  place-items: center;
  position: relative;
}
.ring::after {
  content: "";
  position: absolute;
  inset: 9px;
  border-radius: 50%;
  background: var(--bg-elev);
}
.ring span { position: relative; z-index: 1; font-size: 26px; font-weight: 800; }
.score-text { flex: 1 1 200px; }
.score-text h1 { margin-top: 0; }
.score-text p { margin: 4px 0; color: var(--text-dim); }

.review-item { padding: 15px 16px; margin-bottom: 10px; }
.review-item .rv-head {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 8px;
  color: var(--text-dim);
  font-size: 13px;
}
.review-item .rv-q { font-weight: 600; margin-bottom: 8px; white-space: pre-line; }
.review-item .rv-answer { margin: 4px 0; font-size: 14.5px; white-space: pre-line; }
.review-item .rv-answer.bad { color: var(--bad); }
.review-item .rv-answer.good { color: var(--good); }
.review-item details { margin-top: 8px; }
.review-item summary { cursor: pointer; color: var(--accent); font-size: 14px; }
.review-item .rv-expl { margin-top: 8px; color: var(--text-dim); white-space: pre-line; font-size: 14.5px; }

.empty { text-align: center; color: var(--text-dim); padding: 30px 10px; }

/* ---------- Модальное окно (зум картинки) ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, .8);
  display: grid;
  place-items: center;
  padding: 24px;
  cursor: zoom-out;
}
.modal img { max-width: 100%; max-height: 100%; border-radius: 8px; background: #fff; }

/* ---------- Тосты ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg-elev);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 60;
  opacity: 0;
  transition: opacity .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

@media (max-width: 560px) {
  main { padding: 16px 12px 32px; }
  .q-text { font-size: 16.5px; }
  h1 { font-size: 20px; }
}
