:root {
  --paper: #f2f5f9;
  --surface: #ffffff;
  --ink: #1d2b3a;
  --ink-soft: #5a6b7c;
  --line: #c9d3e0;
  --accent: #1f4e79;
  --accent-ink: #ffffff;
  --disabled-bg: #e8edf3;
  --focus: #e8a317;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #131b24;
    --surface: #1c2733;
    --ink: #e8eef5;
    --ink-soft: #9fb0c1;
    --line: #34465a;
    --accent: #7db2e0;
    --accent-ink: #0f1a25;
    --disabled-bg: #212e3b;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Hiragino Maru Gothic ProN", "BIZ UDPGothic", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 1rem;
  line-height: 1.7;
}

.app { max-width: 640px; margin: 0 auto; padding: 24px 16px 48px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.screen { display: none; }
.screen.is-active { display: block; }

/* ホームの主役：2028 とその素因数分解 */
.hero { padding: 28px 0 24px; border-bottom: 2px solid var(--ink); margin-bottom: 28px; }
.hero-year {
  margin: 0; font-size: clamp(4.5rem, 22vw, 7.5rem); font-weight: 700;
  line-height: 1; letter-spacing: 0.02em; font-variant-numeric: tabular-nums;
}
.hero-factor { margin: 8px 0 0; font-size: clamp(1.5rem, 7vw, 2.25rem); color: var(--accent); font-weight: 700; }
.hero-era { margin: 4px 0 0; color: var(--ink-soft); }

.section-title { font-size: 1.125rem; margin: 0 0 12px; }

.category-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.category {
  width: 100%; min-height: 56px; padding: 10px 16px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  font: inherit; color: var(--ink); text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  cursor: pointer;
}
.category:hover:not(:disabled) { border-color: var(--accent); }
.category:disabled { background: var(--disabled-bg); color: var(--ink-soft); cursor: not-allowed; }
.category-name { font-weight: 700; }
.category-note { font-size: 0.875rem; }

.btn {
  min-height: 48px; padding: 8px 20px; margin-top: 16px;
  font: inherit; font-weight: 700; color: var(--accent-ink);
  background: var(--accent); border: 0; border-radius: 8px; cursor: pointer;
}

.hint { margin-top: 20px; color: var(--ink-soft); font-size: 0.875rem; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* 出題画面 */
.progress { margin: 0 0 8px; color: var(--ink-soft); font-size: 0.875rem; }
.question { margin: 0 0 20px; font-size: 1.25rem; font-weight: 700; line-height: 1.6; }

.answer-form { display: grid; gap: 12px; }
.answer-input {
  width: 100%; min-height: 56px; padding: 8px 14px;
  font: inherit; font-size: 1.5rem; color: var(--ink);
  background: var(--surface); border: 2px solid var(--line); border-radius: 8px;
}
.answer-input:focus { border-color: var(--accent); }
.insert-row { display: flex; gap: 8px; }
.btn-sub {
  min-width: 56px; min-height: 48px; font: inherit; font-size: 1.25rem; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; cursor: pointer;
}
.answer-form .btn { margin-top: 0; }
.yesno-row { display: flex; gap: 12px; }
.btn.yesno { flex: 1; margin-top: 0; min-height: 72px; font-size: 2rem; }
.btn:disabled, .btn-sub:disabled, .answer-input:disabled { opacity: 0.5; cursor: not-allowed; }

.feedback { margin-top: 20px; }
.feedback.is-correct, .feedback.is-wrong, .feedback.is-invalid { padding: 12px 16px; border-radius: 8px; border-left: 6px solid; background: var(--surface); }
.feedback.is-correct { border-color: #2e7d32; }
.feedback.is-wrong { border-color: #c8452d; }
.feedback.is-invalid { border-color: var(--focus); }
.feedback p { margin: 0 0 6px; }
.verdict { font-size: 1.25rem; font-weight: 700; }
.feedback .btn { margin-top: 8px; }

.summary-score { font-size: 1.75rem; font-weight: 700; margin: 8px 0 0; }

/* 解説 */
.btn-sub.wide { width: 100%; margin-top: 8px; font-size: 1rem; }
.feedback .btn-sub.wide { margin-top: 10px; }
.explain { margin-top: 12px; padding-top: 4px; border-top: 1px solid var(--line); }
.explain[hidden] { display: none; }
.explain-step { margin: 14px 0; }
.explain-title { margin: 0 0 6px; font-size: 1rem; color: var(--accent); }
.explain-step p { margin: 0 0 4px; }
.math-line { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tip-list, .plain-list { margin: 0; padding-left: 1.2em; }
.tip-list li, .plain-list li { margin-bottom: 4px; }
.tip-list { padding: 8px 12px 8px 1.6em; background: var(--paper); border-left: 4px solid var(--focus); border-radius: 4px; }
.plain-list { font-variant-numeric: tabular-nums; }

/* わり算のはしご（連除法） */
.ladder { display: inline-grid; grid-template-columns: auto auto; margin: 4px 0; font-size: 1.375rem; font-variant-numeric: tabular-nums; }
.ladder-d { padding: 2px 10px 2px 0; text-align: right; }
.ladder-v { padding: 2px 6px 2px 12px; border-left: 2px solid var(--ink); border-top: 2px solid var(--ink); }
.ladder-v.is-last { border-top: 0; }

/* 約数の一覧・表 */
.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chips li { padding: 2px 10px; background: var(--paper); border: 1px solid var(--line); border-radius: 6px; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }
.explain-table { border-collapse: collapse; font-variant-numeric: tabular-nums; }
.explain-table th, .explain-table td { padding: 4px 10px; border: 1px solid var(--line); text-align: right; white-space: nowrap; }
.explain-table thead th, .explain-table tbody th { background: var(--paper); color: var(--accent); }

/* 曜日ボタン */
.weekday-row { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.btn.weekday { margin-top: 0; min-height: 56px; padding: 8px 0; font-size: 1.25rem; }

/* 線分図 */
.bars { display: grid; gap: 8px; margin: 4px 0; }
.bar-row { display: flex; align-items: stretch; gap: 8px; }
.bar-label { flex: 0 0 6.5em; align-self: center; font-size: 0.875rem; color: var(--ink-soft); }
.bar-track { display: flex; flex: 1; min-width: 0; }
.bar-seg { flex-basis: 0; min-width: 0; padding: 6px 4px; text-align: center; font-size: 0.9rem; font-variant-numeric: tabular-nums; background: var(--paper); border: 2px solid var(--accent); }
.bar-seg + .bar-seg { border-left: 0; }
.bar-seg.is-diff { background: var(--surface); border-style: dashed; border-left: 2px dashed var(--accent); }
.bars-note { margin: 6px 0 0; font-size: 0.875rem; color: var(--ink-soft); }

/* ホーム：模擬テスト */
.section-gap { margin-top: 28px; }

/* 成績 */
.stat-list { list-style: none; margin: 0 0 16px; padding: 0; display: grid; gap: 14px; }
.stat-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: baseline; }
.stat-name { font-weight: 700; }
.stat-score { font-size: 0.875rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.stat-bar { grid-column: 1 / -1; height: 10px; background: var(--disabled-bg); border-radius: 5px; overflow: hidden; }
.stat-bar > span { display: block; height: 100%; background: var(--accent); }
.stat-note { margin: 12px 0 0; color: var(--ink-soft); font-size: 0.875rem; }

/* 年の切り替え・印刷リンク */
.year-pick { display: block; margin-top: 16px; font-weight: 700; }
.year-pick select { display: block; width: 100%; min-height: 48px; margin-top: 4px; padding: 6px 10px; font: inherit; color: var(--ink); background: var(--surface); border: 2px solid var(--line); border-radius: 8px; }
.year-facts { margin-top: 12px; color: var(--ink-soft); font-size: 0.9375rem; }
.print-links { margin: 0; display: grid; gap: 8px; }
.print-link { display: block; text-align: center; text-decoration: none; padding-top: 10px; }

/* 電卓風キーパッド */
.keypad { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; max-width: 360px; width: 100%; margin: 0 auto; }
.key { min-height: 60px; font: inherit; font-size: 1.625rem; font-weight: 700; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; cursor: pointer; touch-action: manipulation; }
.key:active { background: var(--disabled-bg); }
.key-op { color: var(--accent); }
.key-wide { grid-column: 1 / -1; min-height: 48px; font-size: 1rem; color: var(--ink-soft); }
.key:disabled { opacity: 0.5; cursor: not-allowed; }

.keypad[hidden], .keypad-toggle[hidden] { display: none; }
.keypad-toggle { max-width: 360px; margin: 0 auto; }

.keypad-4 { grid-template-columns: repeat(4, 1fr); }
.key-eq { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }

/* 出題画面の下のボタン */
.quiz-actions { display: flex; gap: 8px; margin-top: 24px; }
.quiz-actions > button { flex: 1 1 0; min-width: 0; min-height: 52px; margin-top: 0; padding: 8px 6px; font: inherit; font-size: 0.9375rem; font-weight: 700; }
.quiz-actions > button[hidden] { display: none; }
.quiz-actions .btn-sub { width: auto; }
.feedback.is-shown { padding: 12px 16px; border-radius: 8px; border-left: 6px solid var(--accent); background: var(--surface); }

/* この年の特徴（折りたたみ） */
.year-features { margin-top: 16px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.year-features > summary { min-height: 48px; padding: 10px 16px; display: flex; align-items: center; font-weight: 700; cursor: pointer; }
.year-features[open] > summary { border-bottom: 1px solid var(--line); }
.year-features > .hero-factor, .year-features > .year-facts { margin-left: 16px; margin-right: 16px; }
.year-features > .year-facts { margin-top: 8px; margin-bottom: 12px; }

/* 折りたたみの ▼ マーク（開くと上向きになる） */
.year-features > summary { list-style: none; justify-content: space-between; gap: 12px; }
.year-features > summary::-webkit-details-marker { display: none; }
.year-features > summary::after { content: "▼"; flex: 0 0 auto; font-size: 0.875rem; color: var(--accent); transition: transform 0.15s; }
.year-features[open] > summary::after { transform: rotate(180deg); }
@media (prefers-reduced-motion: reduce) { .year-features > summary::after { transition: none; } }

.near-title { margin: 8px 16px 0; font-weight: 700; font-size: 0.9375rem; }
.year-features > .year-facts + .near-title + .year-facts { margin-top: 4px; }

/* キーパッドに重ねて出す注意書き（ツールチップ）。下のキーはそのまま押せる */
.keypad-tip {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  z-index: 5; width: max-content; max-width: 88%; padding: 12px 16px;
  font-size: 1rem; font-weight: 700; line-height: 1.5; text-align: center;
  color: var(--paper); background: var(--ink); border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35); pointer-events: none;
}

/* 難しさフィルタ（「問題をえらぶ」の横） */
.section-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px 12px; margin-bottom: 12px; }
.section-head .section-title { margin: 0; }
.level-filter { display: flex; gap: 4px; }
.level-btn { min-width: 44px; min-height: 44px; padding: 4px 10px; font: inherit; font-size: 0.875rem; font-weight: 700; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; cursor: pointer; touch-action: manipulation; }
.level-btn[aria-pressed="true"] { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }

/* 分数（上下に積んだ形）。問題文・解説・印刷で共通 */
.frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; margin: 0 0.15em; line-height: 1.2; text-align: center; font-variant-numeric: tabular-nums; }
.frac > .num { padding: 0 0.2em; border-bottom: 0.08em solid currentColor; }
.frac > .den { padding: 0 0.2em; }

/* 画面左上：上の階層（アプリ一覧）にもどる、家の形の丸いボタン */
.home-link { display: inline-flex; align-items: center; justify-content: center; width: 48px; height: 48px; margin: -8px 0 -12px; color: var(--accent); background: var(--surface); border: 2px solid var(--accent); border-radius: 50%; text-decoration: none; touch-action: manipulation; }
.home-link:hover { background: var(--paper); }
.home-link svg { display: block; }
