:root {
  --bg: #f7fafa;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0ea5a4;
  --primary-hover: #0b8a89;
  --primary-ghost: #ccfbf1;
  --correct: #16a34a;
  --correct-bg: #dcfce7;
  --correct-border: #86efac;
  --wrong: #dc2626;
  --wrong-bg: #fee2e2;
  --wrong-border: #fca5a5;
  --shadow: 0 1px 3px rgba(15,23,42,0.06), 0 1px 2px rgba(15,23,42,0.04);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111827;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #1f2937;
    --primary: #14b8a6;
    --primary-hover: #2dd4bf;
    --primary-ghost: #0f3a39;
    --correct: #4ade80;
    --correct-bg: #052e1a;
    --correct-border: #166534;
    --wrong: #f87171;
    --wrong-bg: #3b0b0b;
    --wrong-border: #991b1b;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  padding-bottom: env(safe-area-inset-bottom);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 0.5em 0; line-height: 1.25; }
p { margin: 0 0 0.8em 0; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 28px; line-height: 1; }
.brand h1 { font-size: 18px; font-weight: 700; margin: 0; }
.brand .subtitle { font-size: 12px; color: var(--muted); margin: 0; }
.score {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 18px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--primary-ghost);
  color: var(--primary);
}

/* Layout */
main {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}
.screen { animation: fade 0.25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

/* Start screen */
.lead { color: var(--muted); }
.modes {
  display: grid;
  gap: 10px;
  margin: 16px 0;
}
.mode-btn {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
  text-align: left;
  padding: 16px 18px;
  font: inherit;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s, transform 0.05s;
  color: var(--text);
}
.mode-btn:hover:not(:disabled) { border-color: var(--primary); }
.mode-btn:active:not(:disabled) { transform: scale(0.995); }
.mode-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.mode-btn.primary { border-color: var(--primary); background: var(--primary-ghost); }
.mode-title { font-weight: 700; font-size: 17px; }
.mode-desc { color: var(--muted); font-size: 14px; }

.filters { margin-top: 16px; }
.filters summary {
  cursor: pointer;
  font-size: 14px;
  color: var(--muted);
  padding: 6px 0;
  user-select: none;
}
.category-list { display: grid; gap: 6px; margin-top: 10px; }
.category-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.category-list label:hover { background: var(--bg); }
.category-list input { accent-color: var(--primary); }
.category-list .count { margin-left: auto; color: var(--muted); font-size: 12px; }

/* Progress */
.progress {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 8px;
}
.progress-bar {
  height: 100%;
  background: var(--primary);
  width: 0;
  transition: width 0.3s ease;
}
.progress-text {
  text-align: right;
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 12px 0;
}

/* Question card */
.question { margin-top: 4px; }
.category-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--primary);
  background: var(--primary-ghost);
  padding: 3px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.question h2 {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.45;
  margin-bottom: 16px;
}
.q-lead {
  margin: 0 0 12px 0;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.45;
}
.q-flow {
  list-style: none;
  counter-reset: step;
  padding: 0;
  margin: 0 0 16px 0;
  display: grid;
  gap: 0;
  position: relative;
}
.q-flow li {
  counter-increment: step;
  padding: 12px 14px 12px 48px;
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 22px;
  font-size: 15px;
  line-height: 1.4;
}
.q-flow li::before {
  content: counter(step);
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.q-flow li:not(:last-child)::after {
  content: "↓";
  position: absolute;
  left: 50%;
  bottom: -22px;
  transform: translateX(-50%);
  font-size: 18px;
  color: var(--primary);
  height: 22px;
  display: flex;
  align-items: center;
  font-weight: 700;
}
.q-flow-missing {
  border-style: dashed !important;
  border-color: var(--primary) !important;
  color: var(--primary);
  font-weight: 600;
  background: var(--primary-ghost) !important;
}

.q-image {
  margin: 0 0 16px 0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  justify-content: center;
}
.q-image img {
  max-width: 100%;
  height: auto;
  display: block;
}

.options {
  display: grid;
  gap: 10px;
}
.option {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  line-height: 1.45;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.option:hover:not(:disabled) { border-color: var(--primary); }
.option:disabled { cursor: default; }
.option .letter {
  flex: 0 0 28px;
  font-weight: 700;
  color: var(--primary);
}
.option .text { flex: 1; }
.option.is-correct {
  border-color: var(--correct-border);
  background: var(--correct-bg);
}
.option.is-correct .letter { color: var(--correct); }
.option.is-wrong {
  border-color: var(--wrong-border);
  background: var(--wrong-bg);
}
.option.is-wrong .letter { color: var(--wrong); }

.explanation {
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--bg);
  border-left: 3px solid var(--primary);
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.55;
}
.explanation h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 600;
}
.explanation p { margin: 0; }

.actions { margin-top: 16px; display: flex; justify-content: flex-end; }
.btn-next, .btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 22px;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
  -webkit-tap-highlight-color: transparent;
}
.btn-next:hover, .btn-primary:hover { background: var(--primary-hover); }
.btn-next:active, .btn-primary:active { transform: scale(0.98); }

/* Result */
.result-card { text-align: center; }
.result-score {
  font-size: 56px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  margin: 12px 0;
  color: var(--primary);
  line-height: 1;
}
.score-sep { color: var(--muted); font-weight: 400; margin: 0 6px; }
.result-percent {
  font-size: 20px;
  font-weight: 600;
  color: var(--muted);
}
.result-msg { font-size: 16px; color: var(--text); margin-bottom: 20px; }
.result-actions {
  display: grid;
  gap: 10px;
}

/* Footer */
.footer {
  text-align: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  font-size: 12px;
  color: var(--muted);
}
.footer p { margin: 4px 0; }

/* Responsive */
@media (min-width: 520px) {
  .modes { grid-template-columns: 1fr 1fr; }
  .result-actions { grid-template-columns: auto auto; justify-content: center; }
  .question h2 { font-size: 19px; }
}
@media (min-width: 720px) {
  main { padding: 24px; }
  .card { padding: 28px; }
}
