:root {
  --bg: #0f1115;
  --card: #181b22;
  --card-2: #1f232c;
  --line: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --accent: #d97757;     /* warm terracotta */
  --accent-2: #c4633f;
  --good: #46b366;
  --bad: #e5534b;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
}

.app {
  max-width: 680px;
  margin: 0 auto;
  padding: 24px 18px 64px;
}

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 14px;
  margin-bottom: 22px;
}
.topbar h1 { font-size: 1.25rem; margin: 0; }
.meta { color: var(--muted); font-size: .9rem; }

.screen { animation: fade .25s ease; }
.hidden { display: none !important; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.lede { color: var(--muted); }

label { display: block; margin: 18px 0 6px; font-weight: 600; }
input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  font-size: 1rem;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 16px;
}
input[type="text"]:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

button { font: inherit; cursor: pointer; }
button.primary {
  width: 100%;
  padding: 13px 16px;
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  transition: background .15s ease;
}
button.primary:hover:not(:disabled) { background: var(--accent-2); }
button.primary:disabled { opacity: .45; cursor: not-allowed; }
button.link {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 10px;
  color: var(--muted);
  background: none;
  border: none;
  text-decoration: underline;
}

.quiz-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.progress { color: var(--muted); font-size: .9rem; font-weight: 600; }
.timer { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--accent); }
.timer-total { color: var(--muted); font-weight: 400; font-size: .8em; }

/* Exam-style progress bar under the header */
.progress-track { height: 6px; background: var(--card-2); border-radius: 6px; overflow: hidden; margin-bottom: 22px; }
.progress-bar { height: 100%; width: 0; background: var(--accent); border-radius: 6px; transition: width .25s ease; }

/* The question pane — one calm card holding the stem + answers, like a real exam screen */
.q-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 26px 28px; }

.domain-tag {
  display: inline-block;
  font-size: .72rem;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 6px;
}
.q-prompt { font-size: 1.22rem; line-height: 1.55; font-weight: 600; margin: 0 0 22px; }

/* Category eyebrow above the question / review item */
.q-category { color: var(--accent); font-size: .78rem; font-weight: 700; letter-spacing: .03em; margin: 0 0 8px; }

/* Answer choices: full-width radio rows that fill on select, exam-style */
.options { display: flex; flex-direction: column; gap: 12px; padding-top: 20px; border-top: 1px solid var(--line); }
.option {
  display: flex;
  gap: 14px;
  align-items: center;
  text-align: left;
  width: 100%;
  padding: 15px 18px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  transition: border-color .12s ease, background .12s ease;
}
.option:hover { border-color: var(--accent); background: var(--card-2); }
.option:hover .opt-radio { border-color: var(--accent); color: var(--text); }
.option.selected { border-color: var(--accent); background: var(--card-2); }
.opt-radio {
  flex: 0 0 auto;
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--line); border-radius: 50%;
  font-weight: 700; font-size: .85rem; color: var(--muted);
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.option.selected .opt-radio { background: var(--accent); border-color: var(--accent); color: #fff; }
.opt-text { flex: 1; line-height: 1.5; }

/* Footer nav bar: report (left) · Next/Finish (right), separated like an exam */
.quiz-foot { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 22px; }
.quiz-foot .primary { width: auto; min-width: 150px; }
.report-link { width: auto; margin: 0; padding: 6px 2px; background: none; border: none; color: var(--muted); font-size: .85rem; text-decoration: underline; cursor: pointer; }
.report-link:hover:not(:disabled) { color: var(--bad); }
.report-link:disabled { color: var(--good); text-decoration: none; cursor: default; }

.score-card { text-align: center; padding: 26px 0 14px; }
.score-big { font-size: 3.2rem; font-weight: 700; color: var(--accent); line-height: 1; }
.score-sub { font-size: 1.4rem; color: var(--muted); font-weight: 600; }
.score-time { color: var(--muted); margin-top: 6px; }
.note { color: var(--muted); font-size: .9rem; }

.result-actions { margin: 18px 0 26px; }

.review { display: flex; flex-direction: column; gap: 14px; }
.review-item {
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 4px solid var(--bad);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.review-item.correct { border-left-color: var(--good); }
.review-q { font-weight: 600; margin: 0 0 8px; }
.review-opt { font-size: .92rem; padding: 3px 0; color: var(--muted); }
.review-opt.is-answer { color: var(--good); font-weight: 600; }
.review-opt.is-given-wrong { color: var(--bad); text-decoration: line-through; }
.review-explain { font-size: .9rem; color: var(--text); margin-top: 8px; }
.review-source { font-size: .78rem; color: var(--muted); margin-top: 4px; }

.board-head { font-size: 1.05rem; margin: 28px 0 12px; border-top: 1px solid var(--line); padding-top: 20px; }
.board { list-style: none; padding: 0; margin: 0; counter-reset: rank; }
.board li {
  display: grid;
  grid-template-columns: 32px 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 11px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 8px;
}
.board .rank { color: var(--muted); font-weight: 700; text-align: center; }
.board .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .pts { color: var(--accent); font-weight: 600; }
.board .ms { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
.board li.me { outline: 2px solid var(--accent); }

.muted { color: var(--muted); font-weight: 400; font-size: .9rem; }
.link-inline { color: var(--accent); text-decoration: none; }
.link-inline:hover { text-decoration: underline; }

/* Inline form controls (admin + stats) */
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 12px 0; }
select, input[type="number"], input[type="date"] {
  padding: 11px 12px;
  font-size: 1rem;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
input[type="number"] { width: 90px; }
button.primary.inline, button.danger { width: auto; padding: 11px 18px; }
button.danger { color: #fff; background: var(--bad); border: none; border-radius: var(--radius); font-weight: 600; }
button.danger:hover { filter: brightness(1.1); }
button.ghost { width: auto; padding: 11px 16px; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
button.ghost:hover { border-color: var(--accent); }

.data-table { width: 100%; border-collapse: collapse; font-size: .86rem; margin-top: 10px; }
.data-table th, .data-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.data-table th { color: var(--muted); font-weight: 600; }
.data-table td.ok { color: var(--good); white-space: nowrap; }
.data-table td.no { color: var(--bad); white-space: nowrap; }

/* Tabs */
.tabs { display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
.tab { padding: 8px 14px; color: var(--muted); background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.tab.active { color: #fff; background: var(--accent); border-color: var(--accent); }
.q-check { width: 16px; height: 16px; vertical-align: middle; accent-color: var(--accent); }

/* Statistics */
.totals { display: flex; gap: 12px; flex-wrap: wrap; margin: 12px 0 6px; }
.stat-card { flex: 1; min-width: 110px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; text-align: center; }
.stat-num { font-size: 1.8rem; font-weight: 700; color: var(--accent); }
.stat-label { color: var(--muted); font-size: .82rem; }

.stat-list { display: flex; flex-direction: column; gap: 8px; }
.stat-row { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat-main { display: flex; gap: 8px; align-items: baseline; margin-bottom: 8px; }
.stat-q { font-size: .95rem; }
.stat-metrics { display: flex; gap: 10px; align-items: center; }
.bar { flex: 1; height: 8px; background: var(--card-2); border-radius: 6px; overflow: hidden; }
.bar-fill { display: block; height: 100%; }
.pct { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 46px; text-align: right; }
.pct-good, .bar-fill.pct-good { color: var(--good); }
.bar-fill.pct-good { background: var(--good); }
.pct-mid, .bar-fill.pct-mid { color: #e0a13a; }
.bar-fill.pct-mid { background: #e0a13a; }
.pct-bad, .bar-fill.pct-bad { color: var(--bad); }
.bar-fill.pct-bad { background: var(--bad); }

/* Per-question answer distribution (A/B/C/D pick counts) */
.opt-dist-wrap { margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.opt-dist { display: flex; align-items: flex-start; gap: 8px; font-size: .82rem; }
.opt-dist .opt-l { font-weight: 700; color: var(--muted); width: 2.2em; flex: 0 0 auto; line-height: 1.4; }
.opt-dist .opt-t { flex: 1; min-width: 0; white-space: normal; overflow-wrap: anywhere; line-height: 1.4; color: var(--muted); }
.opt-dist.is-correct .opt-l, .opt-dist.is-correct .opt-t { color: var(--good); font-weight: 600; }
.opt-dist .minibar { flex: 0 0 70px; height: 6px; margin-top: 5px; background: var(--card-2); border-radius: 4px; overflow: hidden; }
.opt-dist .minibar-fill { display: block; height: 100%; background: var(--accent); }
.opt-dist .minibar-fill.good { background: var(--good); }
.opt-dist .opt-n { flex: 0 0 auto; min-width: 2em; text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }
.error {
  margin-top: 18px;
  padding: 12px 14px;
  background: #2a1416;
  border: 1px solid var(--bad);
  border-radius: var(--radius);
  color: #ffb4ad;
}

/* History day blocks */
.day-block { margin: 20px 0; }
.day-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; border-bottom: 1px solid var(--line); padding-bottom: 6px; margin-bottom: 12px; }
.day-score { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }

/* Consent banner */
.consent {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 50;
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  padding: 12px 18px;
  background: var(--card-2);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: .84rem;
}
.consent span { max-width: 620px; }
.consent-btn {
  flex: 0 0 auto;
  padding: 8px 18px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}
.consent-btn:hover { background: var(--accent-2); }

/* --- Grid.js (admin questions table): restyle the light theme for our dark UI --- */
.gridjs-wrapper, .gridjs-footer, .gridjs-head { background: var(--card); border-color: var(--line); box-shadow: none; }
table.gridjs-table { background: var(--card); }
th.gridjs-th, td.gridjs-td { background: var(--card); color: var(--text); border-color: var(--line); }
th.gridjs-th { color: var(--muted); }
th.gridjs-th-sort:hover, th.gridjs-th-sort:focus { background: var(--card-2); }
.gridjs-tr:hover td.gridjs-td { background: var(--card-2); }
td.gridjs-td a.q-open { color: var(--accent); text-decoration: none; cursor: pointer; }
td.gridjs-td a.q-open:hover { text-decoration: underline; }
input.gridjs-input { background: var(--card-2); color: var(--text); border: 1px solid var(--line); }
.gridjs-pagination, .gridjs-pagination .gridjs-summary { color: var(--muted); }
.gridjs-pagination .gridjs-pages button { background: var(--card); color: var(--text); border: 1px solid var(--line); }
.gridjs-pagination .gridjs-pages button:hover:not(:disabled) { background: var(--card-2); }
.gridjs-pagination .gridjs-pages button.gridjs-currentPage { background: var(--accent); color: #fff; border-color: var(--accent); }
.q-detail { margin-top: 12px; }

/* Upvote ("this question is important") button — statistics + history */
.vote-btn {
  display: inline-flex; align-items: center; gap: 5px;
  width: auto; margin-top: 8px; padding: 4px 10px;
  font-size: .8rem; font-weight: 600;
  color: var(--muted); background: transparent;
  border: 1px solid var(--line); border-radius: 999px;
  cursor: pointer; transition: color .12s ease, border-color .12s ease, background .12s ease;
}
.vote-btn:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); }
.vote-btn.voted { color: var(--accent); border-color: var(--accent); background: var(--card-2); }
.vote-btn:disabled { opacity: .5; cursor: default; }
.vote-count { font-variant-numeric: tabular-nums; }

/* Inline code spans inside question prompts / options / explanations */
code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: .88em;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
}

/* --- Statistics charts (Chart.js) --- */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; margin-bottom: 10px; }
.chart-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.chart-card h3 { margin: 0 0 2px; font-size: 1rem; }
.chart-card .muted { margin: 0 0 12px; font-size: .82rem; }
.chart-box { position: relative; height: 260px; }
