:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --text: #1d2321;
  --muted: #5f6b66;
  --line: #d9ddd8;
  --accent: #0f6b5c;
  --accent-text: #ffffff;
  --good: #1a7f4b;
  --good-bg: #e2f4ea;
  --bad: #b3322c;
  --bad-bg: #fbe7e5;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151a18;
    --surface: #1f2623;
    --text: #e8ece9;
    --muted: #9ba8a2;
    --line: #36403c;
    --accent: #4fbfa8;
    --accent-text: #0d1f1b;
    --good: #66d19a;
    --good-bg: #1c3a2b;
    --bad: #f0928b;
    --bad-bg: #472321;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
  line-height: 1.6;
}

main {
  max-width: 860px;
  margin: 0 auto;
  padding: 20px 16px 64px;
}

h1 { font-size: 1.5rem; margin: 8px 0 4px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
p { margin: 0 0 8px; }

.muted { color: var(--muted); font-size: .875rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  margin-top: 16px;
}

button, select {
  font: inherit;
  color: inherit;
}

button {
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}

button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button.link {
  border: none;
  background: none;
  color: var(--muted);
  padding-inline: 4px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.stat b { display: block; font-size: 1.5rem; font-variant-numeric: tabular-nums; }

.form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.form label { display: block; font-size: .875rem; color: var(--muted); }

.form select {
  display: block;
  width: 100%;
  min-height: 44px;
  margin-top: 4px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.exam-list { display: grid; gap: 8px; }

.exam-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
}

.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 8px 0;
  background: var(--bg);
}

.bar .timer { font-variant-numeric: tabular-nums; font-weight: 600; }
.bar .timer.low { color: var(--bad); }

/* 問題画像は白地の前提で作られているので、ダークモードでも白い台紙に載せる */
.sheet {
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  overflow-x: auto;
}

.sheet img { display: block; width: 100%; height: auto; }
/* 全幅に縮めると文字が読めない幅では、等倍寄りで表示して横スクロールさせる */
@media (max-width: 640px) {
  .sheet img { width: 640px; }
}

.sheet.zoomed img { width: 200%; min-width: 1100px; max-width: none; }

.sheet .q-text { margin: 0; color: #1d2321; white-space: pre-wrap; }
.sheet .q-text + img { margin-top: 10px; }
.sheet .q-text { font-size: 1rem; line-height: 1.8; overflow-wrap: anywhere; }

.q-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.choices { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.choices button {
  flex: 1 1 52px;
  max-width: 96px;
  min-height: 48px;
  font-size: 1.05rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.sub-form { margin-top: 12px; }

.sub-row {
  display: grid;
  grid-template-columns: 2em 1fr;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.sub-row .kana { font-weight: 700; text-align: center; }
.sub-row .choices { margin-top: 0; gap: 4px; }
.sub-row .choices button { flex: 1 1 44px; min-height: 44px; padding: 4px; max-width: 64px; font-size: 1rem; }

.sub-row .choices.ten {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 64px));
}

.choices.labeled { flex-direction: column; }

.choices.labeled button {
  flex: none;
  max-width: none;
  font-weight: 400;
  text-align: left;
}

.choices button.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.choices button.correct {
  background: var(--good-bg);
  border-color: var(--good);
  color: var(--good);
  opacity: 1;
}

.choices button.wrong {
  background: var(--bad-bg);
  border-color: var(--bad);
  color: var(--bad);
  opacity: 1;
}

.verdict { margin-top: 12px; font-weight: 700; }
.verdict.good { color: var(--good); }
.verdict.bad { color: var(--bad); }

.explanation { white-space: pre-wrap; }
.error { white-space: pre-wrap; color: var(--bad); }

.nav { display: flex; justify-content: space-between; gap: 8px; margin-top: 16px; }
.nav .primary { margin-left: auto; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  gap: 6px;
  margin-top: 12px;
}

.grid button { min-height: 40px; padding: 2px; font-size: .85rem; font-variant-numeric: tabular-nums; }
.grid button.current { border: 2px solid var(--accent); }
.grid button.done { background: var(--good-bg); color: var(--good); }
.grid button.ok { background: var(--good-bg); color: var(--good); }
.grid button.ng { background: var(--bad-bg); color: var(--bad); }

.score { font-size: 2.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.pass { color: var(--good); }
.fail { color: var(--bad); }

footer { margin-top: 32px; }
