:root {
  --bg: #f7f5f1;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #b91c1c;
  --primary-dark: #7f1d1d;
  --accent: #1d4ed8;
  --good: #15803d;
  --bad: #b91c1c;
  --warn: #b45309;
  --border: #e5e7eb;
  --shadow: 0 2px 8px rgba(0,0,0,0.06);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --card: #1e293b;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #334155;
    --shadow: 0 2px 12px rgba(0,0,0,0.5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  font-size: 16px;
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 600; }
.iconbtn {
  background: transparent;
  color: #fff;
  border: 0;
  font-size: 22px;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
}
.iconbtn:active { background: rgba(255,255,255,0.15); }

main { padding: 12px; max-width: 720px; margin: 0 auto; }
.screen { display: block; }
.hidden { display: none !important; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card h2 { font-size: 17px; margin: 0 0 12px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }

.year-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 8px;
}
.year-btn {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.year-btn:active, .year-btn.active { border-color: var(--primary); color: var(--primary); }
.year-btn .progress-text { display: block; font-weight: 400; font-size: 12px; color: var(--muted); margin-top: 4px; }

.primary-btn, .secondary-btn, .danger-btn {
  display: inline-block;
  border: 0;
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  width: 100%;
  margin-top: 8px;
}
.primary-btn { background: var(--primary); color: #fff; }
.primary-btn:active { background: var(--primary-dark); }
.secondary-btn { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.secondary-btn:active { background: var(--bg); }
.danger-btn { background: transparent; color: var(--bad); border: 1px solid var(--bad); }

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
}
.stat-cell {
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 6px;
}
.stat-cell .num { display: block; font-size: 22px; font-weight: 700; }
.stat-cell .lbl { display: block; font-size: 12px; color: var(--muted); }

/* Quiz */
.progress {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin-bottom: 8px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  background: var(--primary);
  width: 0;
  transition: width .25s ease;
}
.quiz-meta {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.scenario {
  background: var(--bg);
  border-left: 3px solid var(--accent);
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 15px;
  margin-bottom: 12px;
  white-space: pre-wrap;
}
.question {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 8px;
  white-space: pre-wrap;
}
.badge {
  display: inline-block;
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  margin-bottom: 12px;
}

.options { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  cursor: pointer;
  background: var(--card);
  font-size: 15px;
  user-select: none;
}
.option:active { background: var(--bg); }
.option input { margin-top: 4px; transform: scale(1.2); accent-color: var(--primary); }
.option.selected { border-color: var(--primary); background: rgba(185,28,28,0.05); }
.option.correct { border-color: var(--good); background: rgba(21,128,61,0.08); }
.option.wrong { border-color: var(--bad); background: rgba(185,28,28,0.08); }
.option.disabled { pointer-events: none; opacity: .9; }
.option .key { font-weight: 700; min-width: 1.4em; }

.action-row { display: flex; gap: 8px; }
.action-row > button { flex: 1; }

.result {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 600;
}
.result.correct-result { background: rgba(21,128,61,0.1); color: var(--good); }
.result.wrong-result { background: rgba(185,28,28,0.1); color: var(--bad); }
.result.unknown-result { background: rgba(180,83,9,0.1); color: var(--warn); }

.explanation {
  margin-top: 12px;
  background: var(--bg);
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
}
.explanation h3 { font-size: 14px; margin: 0 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.explanation .src-tag {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  padding: 2px 8px;
  font-size: 11px;
  border-radius: 999px;
  margin-bottom: 8px;
}
.explanation .answer-source {
  display: inline-block;
  background: var(--muted);
  color: #fff;
  padding: 2px 8px;
  font-size: 11px;
  border-radius: 999px;
  margin-bottom: 8px;
  margin-left: 4px;
}
.explanation .answer-note {
  background: rgba(180,83,9,0.1);
  border-left: 3px solid var(--warn);
  padding: 8px 10px;
  border-radius: 4px;
  margin: 8px 0;
  font-size: 13px;
}
.explanation ul { padding-left: 18px; margin: 4px 0; }
.explanation li { margin-bottom: 2px; }

.nav-row { display: flex; gap: 8px; }
.nav-row > button { flex: 1; }

.jump-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 6px;
  margin-bottom: 12px;
}
.jump-cell {
  aspect-ratio: 1;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.jump-cell.correct { background: rgba(21,128,61,0.18); border-color: var(--good); color: var(--good); }
.jump-cell.wrong { background: rgba(185,28,28,0.15); border-color: var(--bad); color: var(--bad); }
.jump-cell.unanswered { color: var(--muted); }
.jump-cell.bookmarked::after {
  content: "★";
  position: absolute;
  top: -2px;
  right: 2px;
  font-size: 10px;
  color: var(--warn);
}
