:root {
  --bg: #101418;
  --panel: #1b2128;
  --line: #2e3742;
  --text: #eef2f5;
  --muted: #9aa6b2;
  --accent: #22c55e;
  --accent-text: #04210f;
  --key: #26303a;
  --key-fn: #3a4652;
  --ok: #ff4d4d;
  --ng: #4da3ff;
  --num-color: #22ff22;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", Meiryo, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body { overflow: hidden; }

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

button {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  border: none;
  border-radius: 12px;
  min-height: 44px;
  min-width: 44px;
}

.screen {
  display: none;
  position: fixed;
  inset: 0;
  overflow-y: auto;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
}
.screen.active { display: flex; }

/* ---------- 設定 ---------- */
.settings-wrap, .result-wrap {
  width: 100%;
  max-width: 560px;
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
h1 { font-size: 1.5rem; margin: 0 0 4px; text-align: center; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field.row { flex-direction: row; align-items: center; gap: 12px; }
.field label { font-size: 0.9rem; color: var(--muted); }

select, input[type=number] {
  min-height: 48px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 1.15rem;
  width: 100%;
}
input[type=range] { width: 100%; min-height: 44px; accent-color: var(--accent); }
input[type=color] {
  width: 64px; height: 44px; padding: 0; border: 1px solid var(--line);
  border-radius: 10px; background: var(--panel);
}

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.summary {
  margin: 0;
  padding: 12px;
  border-radius: 10px;
  background: var(--panel);
  text-align: center;
  font-size: 1.05rem;
}
.warn { margin: 0; color: #ffcc66; font-size: 0.9rem; }
.note { margin: 0; color: var(--muted); font-size: 0.8rem; text-align: center; }

.advanced {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 12px;
}
.advanced summary {
  min-height: 44px; display: flex; align-items: center; cursor: pointer; color: var(--muted);
}
.advanced[open] { padding-bottom: 12px; }
.advanced[open] .field + .field { margin-top: 12px; }

.primary { background: var(--accent); color: var(--accent-text); font-weight: 700; }
.secondary { background: var(--key-fn); }
.small { background: var(--key-fn); padding: 0 14px; font-size: 0.95rem; }
.big { min-height: 60px; font-size: 1.35rem; }

/* ---------- 出題 ---------- */
.screen.flash {
  background: #000;
  padding: 0;
  overflow: hidden;
  align-items: center;
  justify-content: center;
  cursor: none;
}
.flash-num {
  color: var(--num-color);
  font-family: Arial, "Helvetica Neue", Helvetica, Roboto, sans-serif;
  font-weight: 700;
  font-variant-numeric: tabular-nums lining-nums;
  font-feature-settings: "tnum" 1, "lnum" 1;
  line-height: 1;
  white-space: nowrap;
  letter-spacing: 0;
  visibility: hidden;
  user-select: none;
  -webkit-user-select: none;
  will-change: contents;
}
.flash-dot {
  position: absolute;
  width: 28vmin; height: 28vmin;
  border-radius: 50%;
  background: var(--num-color);
  visibility: hidden;
}
.abort {
  position: absolute;
  top: calc(8px + var(--safe-t));
  right: calc(8px + var(--safe-r));
  width: 52px; height: 52px;
  background: rgba(255,255,255,0.08);
  color: #777;
  font-size: 1.8rem;
  line-height: 1;
  border-radius: 50%;
  cursor: pointer;
}
.debug-live {
  position: absolute;
  left: calc(8px + var(--safe-l));
  bottom: calc(8px + var(--safe-b));
  color: #aaa;
  font: 12px/1.4 ui-monospace, Menlo, Consolas, monospace;
  white-space: pre;
  pointer-events: none;
}

/* ---------- 解答 ---------- */
.answer-wrap {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100%;
}
.answer-top { display: flex; flex-direction: column; gap: 8px; align-items: stretch; }
.answer-bottom { margin-top: auto; }
.qno-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.qno { color: var(--muted); }
.input {
  min-height: 72px;
  padding: 8px 16px;
  border-radius: 12px;
  background: #000;
  border: 1px solid var(--line);
  color: var(--num-color);
  font-family: Arial, "Helvetica Neue", Helvetica, Roboto, sans-serif;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: 3rem;
  line-height: 56px;
  text-align: right;
  overflow: hidden;
}
.input:empty::before { content: "答えを入力"; color: #555; font-size: 1.2rem; font-weight: 400; }

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.keypad button {
  background: var(--key);
  font-size: 1.9rem;
  font-weight: 700;
  min-height: 64px;
  height: clamp(56px, 9vh, 84px);
}
.keypad button:active { filter: brightness(1.4); }
.keypad .fn { background: var(--key-fn); font-size: 1.5rem; }
.keypad .enter {
  grid-column: 1 / -1;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1.5rem;
}
.after { display: flex; flex-direction: column; gap: 10px; }

.judge { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.mark { font-size: 6rem; line-height: 1; font-weight: 700; }
.mark.ok { color: var(--ok); }
.mark.ng { color: var(--ng); }
.correct-line { font-size: 1.2rem; text-align: center; }
.correct-line b { font-variant-numeric: tabular-nums; font-size: 1.6rem; }
.nums {
  width: 100%;
  max-height: 30vh;
  overflow-y: auto;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  font-variant-numeric: tabular-nums;
  font-size: 1rem;
  line-height: 1.6;
  text-align: center;
  word-break: break-all;
}

.debug-report {
  margin: 0;
  max-height: 30vh;
  overflow: auto;
  padding: 8px;
  background: #000;
  color: #9f9;
  border-radius: 8px;
  font: 11px/1.4 ui-monospace, Menlo, Consolas, monospace;
}

/* ---------- 結果 ---------- */
.verdict { font-size: 2.6rem; font-weight: 700; text-align: center; }
.verdict.pass { color: var(--ok); }
.verdict.semi { color: #ffb84d; }
.verdict.fail { color: var(--ng); }
.score { font-size: 1.6rem; text-align: center; }
.result-cond { color: var(--muted); text-align: center; font-size: 0.9rem; }
.result-list {
  margin: 0;
  padding: 8px 12px 8px 2.6em;
  background: var(--panel);
  border-radius: 10px;
  max-height: 40vh;
  overflow-y: auto;
  font-variant-numeric: tabular-nums;
  line-height: 1.8;
}
.result-list .ok { color: var(--ok); }
.result-list .ng { color: var(--ng); }
.result-buttons { display: flex; flex-direction: column; gap: 10px; }

/* スマホ横向き：解答画面を左右2列に */
@media (orientation: landscape) and (max-height: 560px) {
  .screen { padding-top: calc(8px + var(--safe-t)); padding-bottom: calc(8px + var(--safe-b)); }
  .answer-wrap { max-width: none; flex-direction: row; gap: 16px; }
  .answer-top { flex: 1; justify-content: center; }
  .answer-bottom { flex: 1; margin-top: 0; display: flex; flex-direction: column; justify-content: center; }
  .keypad button { height: calc((100dvh - 16px - var(--safe-t) - var(--safe-b) - 32px) / 5); min-height: 44px; font-size: 1.5rem; }
  .mark { font-size: 4rem; }
  .grid2 { grid-template-columns: repeat(4, 1fr); }
}
