/* ============ Theme tokens ============ */
:root {
  --accent: #0ea5a4;
  --accent-strong: #0f766e;
  --accent-soft: rgba(14, 165, 164, 0.12);
  --bg: #f4f6f8;
  --bg-elev: #ffffff;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.06);
  --good: #16a34a;
  --good-soft: rgba(22, 163, 74, 0.12);
  --bad: #dc2626;
  --bad-soft: rgba(220, 38, 38, 0.10);
  --radius: 14px;
  --radius-sm: 10px;
}
[data-theme="dark"] {
  --accent: #2dd4bf;
  --accent-strong: #5eead4;
  --accent-soft: rgba(45, 212, 191, 0.14);
  --bg: #0b1220;
  --bg-elev: #0f172a;
  --panel: #131c2e;
  --text: #e6edf6;
  --muted: #93a3b8;
  --border: #24314a;
  --border-strong: #33425f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 10px 30px rgba(0, 0, 0, 0.35);
  --good: #34d399;
  --good-soft: rgba(52, 211, 153, 0.14);
  --bad: #f87171;
  --bad-soft: rgba(248, 113, 113, 0.14);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.muted { color: var(--muted); }

/* ============ Top bar ============ */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px clamp(16px, 4vw, 40px);
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 11px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 20px; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  box-shadow: 0 6px 16px var(--accent-soft);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-size: 16px; }
.brand-text span { font-size: 12px; color: var(--muted); }

.icon-btn {
  width: 40px; height: 40px; border-radius: 11px; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  font-size: 18px; display: grid; place-items: center; transition: .15s;
}
.icon-btn:hover { border-color: var(--border-strong); }

main { max-width: 1120px; margin: 0 auto; padding: clamp(16px, 3vw, 32px); }

/* ============ Screens ============ */
.screen { display: none; animation: fade .25s ease; }
.screen.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.hero { margin: 8px 0 22px; }
.hero h1 { font-size: clamp(22px, 3vw, 30px); margin: 0 0 6px; letter-spacing: -0.02em; }
.hero p { margin: 0; }

.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}

.layout { display: grid; grid-template-columns: 1fr 320px; gap: 20px; align-items: start; }

/* ============ Tree ============ */
/* min-width:0 lets the grid items shrink to the track instead of growing to
   fit the widest (deeply-indented) label — which would balloon the page width. */
.tree-panel, .config-panel { min-width: 0; }
.tree-panel { padding: 16px; }
.tree-toolbar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.search {
  width: 100%; padding: 11px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-elev); color: var(--text);
  font-size: 15px; outline: none; transition: .15s;
}
.search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.tree-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.ghost-btn {
  padding: 7px 12px; border-radius: 9px; cursor: pointer; font-size: 13px;
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  transition: .15s;
}
.ghost-btn:hover { color: var(--text); border-color: var(--border-strong); }

.tree { max-height: 62vh; overflow: auto; padding-right: 4px; }
.node { border-radius: var(--radius-sm); }
.node-row {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; border-radius: 9px; cursor: pointer; user-select: none;
}
.node-row:hover { background: var(--accent-soft); }
.node-row.hidden, .node.hidden { display: none; }

.twisty {
  width: 20px; height: 20px; flex: none; display: grid; place-items: center;
  border: none; background: none; color: var(--muted); cursor: pointer;
  font-size: 11px; transition: transform .15s;
}
.twisty.spacer { visibility: hidden; }
.node.collapsed > .children { display: none; }
.node:not(.collapsed) > .node-row .twisty { transform: rotate(90deg); }

.node-row input[type="checkbox"] { width: 17px; height: 17px; flex: none; accent-color: var(--accent); cursor: pointer; }
.node-label { flex: 1; min-width: 0; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.node-count {
  flex: none; font-size: 12px; color: var(--muted);
  background: var(--bg-elev); border: 1px solid var(--border);
  padding: 1px 8px; border-radius: 999px;
}
.children { margin-left: 20px; border-left: 1px solid var(--border); padding-left: 6px; }
.depth-0 > .node-row { font-weight: 650; }
.depth-0 > .node-row .node-label { font-size: 15px; }

/* ============ Config panel ============ */
.config-panel { padding: 18px; position: sticky; top: 80px; display: flex; flex-direction: column; gap: 18px; }
.action-bar { display: flex; flex-direction: column; gap: 14px; }
.selected-summary { display: flex; align-items: baseline; gap: 10px; }
.big-count { font-size: 34px; font-weight: 800; color: var(--accent-strong); letter-spacing: -0.02em; }
.summary-text { display: flex; flex-direction: column; line-height: 1.25; }
.summary-text .small { font-size: 12px; }
.tree-view { width: 100%; }
.subj-filter { width: 100%; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 9px; }
.field-label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.radio-row, .check-row { display: flex; align-items: center; gap: 9px; font-size: 14px; cursor: pointer; }
.radio-row input[type="radio"], .check-row input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }
.num { width: 84px; padding: 6px 9px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); font-size: 14px; }
.num:disabled { opacity: .5; }

.segmented { display: inline-flex; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg { flex: 1; padding: 7px 12px; border: none; background: none; color: var(--muted); border-radius: 8px; cursor: pointer; font-size: 13px; transition: .15s; }
.seg.active { background: var(--accent); color: #fff; font-weight: 600; }

.primary-btn {
  padding: 13px 18px; border: none; border-radius: 12px; cursor: pointer;
  font-size: 15px; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  box-shadow: 0 8px 20px var(--accent-soft); transition: .15s;
}
.primary-btn:hover:not(:disabled) { filter: brightness(1.05); transform: translateY(-1px); }
.primary-btn:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; }
.secondary-btn {
  padding: 12px 18px; border-radius: 12px; cursor: pointer; font-size: 15px; font-weight: 600;
  border: 1px solid var(--accent); background: transparent; color: var(--accent-strong); transition: .15s;
}
.secondary-btn:hover { background: var(--accent-soft); }

/* ============ Quiz ============ */
.quiz-head { position: sticky; top: 62px; z-index: 10; background: var(--bg); padding: 10px 0 12px; }
.progress { height: 8px; background: var(--border); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--accent-strong)); transition: width .3s ease; }
.quiz-meta { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }
.score { font-size: 14px; font-weight: 600; color: var(--accent-strong); }

.quiz-card { padding: clamp(18px, 3vw, 30px); margin-bottom: 18px; }
.q-image { margin: 0 0 16px; }
.q-image img { max-width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.q-text { font-size: clamp(18px, 2.4vw, 22px); margin: 0 0 20px; letter-spacing: -0.01em; line-height: 1.35; white-space: pre-line; }

.answers { display: flex; flex-direction: column; gap: 10px; }
.answer {
  display: flex; align-items: center; gap: 12px; text-align: left; width: 100%;
  padding: 14px 16px; border-radius: 12px; cursor: pointer; font-size: 15px;
  border: 1.5px solid var(--border); background: var(--bg-elev); color: var(--text); transition: .12s;
}
.answer:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.answer:disabled { cursor: default; }
.answer .atext { white-space: pre-line; }
.answer .key {
  flex: none; width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  font-weight: 700; font-size: 14px; background: var(--panel); border: 1px solid var(--border); color: var(--muted);
}
.answer.correct { border-color: var(--good); background: var(--good-soft); }
.answer.correct .key { background: var(--good); color: #fff; border-color: var(--good); }
.answer.wrong { border-color: var(--bad); background: var(--bad-soft); }
.answer.wrong .key { background: var(--bad); color: #fff; border-color: var(--bad); }
.answer.chosen { box-shadow: 0 0 0 3px var(--accent-soft); }

.explain { margin-top: 20px; padding-top: 18px; border-top: 1px dashed var(--border-strong); }
.explain-verdict { font-weight: 700; margin-bottom: 8px; }
.explain-verdict.ok { color: var(--good); }
.explain-verdict.no { color: var(--bad); }
.explain-note { color: var(--text); font-size: 15px; white-space: pre-line; }
.explain .q-image { margin-top: 14px; }

.quiz-foot { display: flex; justify-content: flex-end; }

/* ============ Results ============ */
.results-card { padding: clamp(22px, 4vw, 36px); text-align: center; display: flex; flex-direction: column; align-items: center; gap: 16px; margin-bottom: 22px; }
.score-ring {
  --pct: 0; width: 160px; height: 160px; border-radius: 50%;
  background: conic-gradient(var(--accent) calc(var(--pct) * 1%), var(--border) 0);
  display: grid; place-items: center;
}
.score-ring-inner {
  width: 128px; height: 128px; border-radius: 50%; background: var(--panel);
  display: grid; place-content: center; gap: 2px;
}
.result-pct { font-size: 34px; font-weight: 800; letter-spacing: -0.02em; }
.result-frac { font-size: 14px; }
#result-headline { margin: 0; font-size: 22px; }
.results-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

.review { display: flex; flex-direction: column; gap: 12px; }
.review-item { padding: 16px 18px; }
.review-q { font-weight: 650; margin-bottom: 10px; white-space: pre-line; }
.review-a { display: flex; align-items: center; gap: 8px; font-size: 14px; margin: 4px 0; }
.review-a .key { font-weight: 700; width: 22px; }
.review-a.correct { color: var(--good); }
.review-a.wrong { color: var(--bad); }
.review-note { margin-top: 8px; font-size: 14px; color: var(--muted); border-top: 1px dashed var(--border); padding-top: 8px; white-space: pre-line; }

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

/* ============ Phones (must come last so it overrides the base rules) ============ */
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }

  /* let the tree flow in the page instead of a nested scroll area */
  .tree { max-height: none; overflow: visible; }
  .config-panel { position: static; padding-bottom: 4px; }

  /* count + Start button as a fixed bottom bar, always reachable */
  main { padding-bottom: calc(84px + env(safe-area-inset-bottom, 0px)); }
  .action-bar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    flex-direction: row; align-items: center; gap: 12px;
    padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px));
    background: color-mix(in srgb, var(--bg-elev) 94%, transparent);
    backdrop-filter: saturate(1.4) blur(10px);
    border-top: 1px solid var(--border);
    box-shadow: 0 -6px 22px rgba(15, 23, 42, 0.10);
  }
  .action-bar .selected-summary { flex: none; align-items: center; gap: 8px; }
  .action-bar .big-count { font-size: 26px; }
  .action-bar .summary-text { font-size: 12px; }
  .action-bar .summary-text .muted:first-child { font-size: 13px; }
  .action-bar .summary-text .small { display: none; } /* keep the bar compact */
  .action-bar #start-btn { flex: 1; min-width: 0; padding: 13px 12px; }

  /* tighter nesting so deep subcategory rows still fit narrow screens */
  .children { margin-left: 12px; padding-left: 5px; }

  /* comfortable touch targets */
  .node-row { padding: 11px 8px; }
  .node-row input[type="checkbox"] { width: 20px; height: 20px; }
  .radio-row, .check-row { font-size: 15px; gap: 11px; padding: 3px 0; }
  .radio-row input[type="radio"], .check-row input[type="checkbox"] { width: 20px; height: 20px; }
  .seg { padding: 9px 12px; }
  .ghost-btn { padding: 9px 13px; }

  /* 16px inputs so iOS Safari doesn't zoom the page on focus */
  .search, .num { font-size: 16px; }
  .num { width: 96px; }

  /* full-width primary actions are easier to thumb on a phone */
  .quiz-foot { justify-content: stretch; }
  .quiz-foot .primary-btn { width: 100%; }
  .results-actions { flex-direction: column; }
  .results-actions > button { width: 100%; }
}
