:root {
  --bg: #f6f4ef;
  --card: #ffffff;
  --text: #22252a;
  --muted: #6b7078;
  --border: #dcd8cf;
  --accent: #2f6fde;
  --accent-text: #ffffff;
  --minus: #d6453d;
  --ok: #1f9d55;
  --ng: #d6453d;
  --flash-bg: #111418;
  --flash-text: #ffffff;
  --flash-minus: #ff7a6e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --card: #20232a;
    --text: #eceef2;
    --muted: #9aa0aa;
    --border: #353a43;
    --accent: #5b8ff0;
    --minus: #ff7a6e;
    --ok: #4cc47f;
    --ng: #ff7a6e;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 18px;
  -webkit-text-size-adjust: 100%;
}

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

#app {
  min-height: 100%;
}

.screen {
  display: none;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 24px 16px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.screen.active { display: flex; }

h1 {
  margin: 0 0 20px;
  font-size: 1.8rem;
  letter-spacing: 0.05em;
}
h2 { margin: 0 0 16px; font-size: 1.5rem; }

.card {
  width: 100%;
  max-width: 480px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.card.center { align-items: center; text-align: center; }

/* 設定フォーム */
.field {
  display: grid;
  grid-template-columns: 3em 1fr auto;
  align-items: center;
  gap: 8px 12px;
}
.field > label, .field > .label {
  font-weight: 600;
}
.field-time .segmented { grid-column: 2 / 4; }
.field-time .stepper { grid-column: 2; }
.field-time .hint, .field-size .hint { grid-column: 2 / 4; }
.field .segmented:only-of-type { grid-column: 2 / 4; }

.unit { color: var(--muted); }
.hint { margin: 0; font-size: 0.85rem; color: var(--muted); }

.stepper {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  max-width: 220px;
}
.stepper input {
  flex: 1;
  min-width: 0;
  width: 4em;
  border: none;
  background: transparent;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 600;
  padding: 8px 0;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper .step {
  width: 48px;
  border: none;
  background: transparent;
  font-size: 1.4rem;
  cursor: pointer;
  touch-action: manipulation;
}
.stepper .step:active { background: var(--border); }

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.segmented label {
  flex: 1;
  position: relative;
}
.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.segmented span {
  display: block;
  white-space: nowrap;
  text-align: center;
  padding: 10px 6px;
  cursor: pointer;
  font-size: 0.95rem;
}
.segmented label + label span { border-left: 1px solid var(--border); }
.segmented input:checked + span {
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.segmented input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: -3px; }

/* ボタン */
button { cursor: pointer; touch-action: manipulation; }
.primary, .secondary, .ghost {
  border-radius: 12px;
  padding: 12px 20px;
  border: 1px solid transparent;
}
.primary {
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}
.primary.big { font-size: 1.25rem; padding: 16px 24px; width: 100%; }
.secondary {
  background: transparent;
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}
.ghost {
  background: transparent;
  color: var(--muted);
}
.row {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

/* 出題画面 */
#screen-flash {
  background: var(--flash-bg);
  position: relative;
  user-select: none;
  -webkit-user-select: none;
}
.flash-progress {
  position: absolute;
  top: 24px;
  left: 16px;
  right: 100px;
  margin: 0;
  color: #8a9099;
  font-weight: 600;
}
.flash {
  color: var(--flash-text);
  font-family: "SF Mono", "Menlo", "Consolas", "Roboto Mono", monospace;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: var(--flash-size, clamp(64px, 22vw, 240px));
  line-height: 1;
  white-space: nowrap;
  text-align: center;
}
.flash.minus { color: var(--flash-minus); }

/* 始まる前のゲージ */
.ready {
  position: absolute;
  inset: 0;
  margin: auto;
  width: min(60vmin, 480px);
  height: min(60vmin, 480px);
  display: flex;
  align-items: center;
  justify-content: center;
}
.ready[hidden] { display: none; }
.ready svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}
.ready circle {
  fill: none;
  stroke-width: 5;
}
.ready-track { stroke: rgba(255, 255, 255, 0.12); }
.ready-progress {
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
}
.ready.running .ready-progress {
  animation: ready-fill var(--ready-seconds, 3s) linear forwards;
}
@keyframes ready-fill {
  to { stroke-dashoffset: 0; }
}
.ready-label {
  color: var(--flash-text);
  font-weight: 700;
  font-size: min(9vmin, 72px);
  letter-spacing: 0.08em;
}
.flash.measure {
  position: absolute;
  left: -9999px;
  top: 0;
  visibility: hidden;
  font-size: 100px;
}
#abort {
  position: absolute;
  top: 16px;
  right: 16px;
  color: #8a9099;
}

/* 解答画面 */
#screen-answer { padding-top: 16px; padding-bottom: 16px; }
.answer-card { max-width: 520px; gap: 14px; }
.answer-display {
  width: 100%;
  min-height: 1.5em;
  font-size: clamp(2.4rem, 8vh, 4rem);
  line-height: 1.4;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: right;
  padding: 0 16px;
  border: 2px solid var(--accent);
  border-radius: 12px;
  overflow: hidden;
  white-space: nowrap;
}
.answer-display .placeholder {
  display: block;
  text-align: center;
  color: var(--muted);
  font-size: 0.5em;
  font-weight: 600;
  line-height: 2.8;
}
.keypad {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.keypad button {
  height: clamp(56px, 10vh, 88px);
  font-size: clamp(1.6rem, 5vh, 2.4rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg);
  -webkit-user-select: none;
  user-select: none;
}
.keypad button:active { background: var(--border); }
.keypad button.fn { font-size: clamp(1.1rem, 3.2vh, 1.5rem); color: var(--muted); }

/* 結果画面 */
.result-mark {
  font-size: 2.6rem;
  line-height: 1;
  font-weight: 800;
}
.result-mark.ok { color: var(--ok); }
.result-mark.ng { color: var(--ng); }
.result-your { margin: 0; color: var(--muted); }
.result-answer { margin: 0; font-size: 1.2rem; }
.result-answer strong {
  font-size: 2.4rem;
  font-variant-numeric: tabular-nums;
  margin-left: 8px;
}
details { width: 100%; }
summary { cursor: pointer; color: var(--accent); }
.number-list {
  list-style: none;
  margin: 12px auto 0;
  padding: 0;
  max-width: 240px;
  font-variant-numeric: tabular-nums;
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
  font-size: 1.15rem;
}
.number-list li {
  display: flex;
  justify-content: space-between;
  padding: 4px 8px;
  border-bottom: 1px dashed var(--border);
}
.number-list li .idx { color: var(--muted); font-size: 0.85rem; }
.number-list li.minus .val { color: var(--minus); }
.number-list li.total { border-bottom: none; border-top: 2px solid var(--text); font-weight: 700; }
.score { margin: 0; color: var(--muted); font-size: 0.9rem; }

/* 練習／検定 */
.field-group {
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.field-group[hidden] { display: none; }
.segmented.kind span { font-size: 1.05rem; padding: 12px; }
.segmented label { display: flex; min-width: 0; }
.segmented.three span { font-size: 0.8rem; padding: 8px 4px; }
.segmented span {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  white-space: normal;
  word-break: keep-all;
}

select {
  font: inherit;
  font-size: 1.15rem;
  font-weight: 600;
  color: inherit;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  grid-column: 2 / 4;
}
select option, select optgroup { color: #22252a; }

.level-spec {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--bg);
}
.level-spec > div { display: contents; }
.level-spec .k { color: var(--muted); }
.level-spec .v { font-weight: 600; }

.answer-progress {
  margin: 0;
  color: var(--muted);
  font-weight: 600;
}

.result-mark.semi { color: #d08a1e; }

.exam-list {
  width: 100%;
  max-width: 360px;
  text-align: left;
}
.exam-list details {
  border-bottom: 1px dashed var(--border);
}
.exam-list summary {
  display: grid;
  grid-template-columns: 4em 1.5em 1fr auto;
  gap: 8px;
  padding: 8px 4px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  list-style: none;
}
.exam-list summary::-webkit-details-marker { display: none; }
.exam-list .no { color: var(--muted); }
.exam-list .ok .mark { color: var(--ok); font-weight: 700; }
.exam-list .ng .mark { color: var(--ng); font-weight: 700; }
.exam-list .ans { text-align: right; font-weight: 700; }
.exam-list .yours { color: var(--ng); font-size: 0.9rem; }
.exam-list .number-list { margin: 0 auto 12px; }
.exam-list .number-list li { justify-content: flex-end; }
