/* ==========================================================
   百ます計算アプリ  スタイルシート
   - 小学生 / タブレット利用を想定し、余白とタップ領域を大きく取る
   ========================================================== */

:root {
  --bg: #eef4fb;
  --surface: #ffffff;
  --ink: #1f2933;
  --ink-soft: #5b6b7c;
  --line: #cfdae6;
  --primary: #2f7de1;
  --primary-dark: #1f5fb0;
  --accent: #ff8a3d;
  --ok: #23a06a;
  --ng: #e0483c;
  --shadow: 0 2px 6px rgba(31, 41, 51, .12);
  --radius: 14px;
  --cell: clamp(30px, 7.4vw, 58px);
  --cell-font: clamp(14px, 3.2vw, 24px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

/* ---------- ヘッダ ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
}
.app-header h1 { font-size: 1.1rem; margin: 0; flex: 1; }
.header-user { font-size: .85rem; opacity: .9; }
.icon-btn {
  background: rgba(255, 255, 255, .18);
  border: 0;
  color: #fff;
  font: inherit;
  font-size: .95rem;
  padding: 8px 12px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 40px;
}
.icon-btn:active { background: rgba(255, 255, 255, .35); }

/* ---------- レイアウト ---------- */
main { padding: 16px 14px 48px; max-width: 760px; margin: 0 auto; }
.screen { display: none; }
.screen.active { display: block; }
.screen-wide.active { display: block; }
#main:has(.screen-wide.active) { max-width: 1000px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 16px;
  margin-bottom: 16px;
}
h2 { margin: 0 0 12px; font-size: 1.25rem; }
h3 { margin: 4px 0 10px; font-size: 1.05rem; }
.lead { margin: 0 0 16px; line-height: 1.7; color: var(--ink-soft); }
.hint { color: var(--ink-soft); font-size: .82rem; line-height: 1.6; margin: 6px 0 0; }
.center { text-align: center; }
.error { color: var(--ng); font-weight: 700; margin: 4px 0 8px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 20px 0; }

/* ---------- 入力 ---------- */
.field { display: block; margin-bottom: 18px; }
.field > span:first-child {
  display: block;
  font-weight: 700;
  margin-bottom: 8px;
  font-size: .95rem;
}
.field input[type="text"], .field input[type="tel"] {
  width: 100%;
  font: inherit;
  font-size: 1.1rem;
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: #fff;
  min-height: 48px;
}
.field input:focus { outline: 3px solid rgba(47, 125, 225, .35); border-color: var(--primary); }
.inline-row { display: flex; gap: 8px; align-items: stretch; }
.inline-row input { flex: 1; }
.req, .opt {
  font-style: normal;
  font-size: .7rem;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  vertical-align: middle;
}
.req { background: var(--ng); color: #fff; }
.opt { background: var(--line); color: var(--ink-soft); }

.switch-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
  font-size: .95rem;
}
.switch-row input { width: 26px; height: 26px; }

/* ---------- ボタン ---------- */
.btn {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 12px;
  padding: 12px 20px;
  min-height: 48px;
  cursor: pointer;
  background: var(--line);
  color: var(--ink);
}
.btn:active { transform: translateY(1px); }
.btn-lg { width: 100%; font-size: 1.15rem; padding: 16px; min-height: 60px; }
.btn-sm { min-height: 48px; padding: 10px 14px; font-size: .9rem; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-secondary { background: #e3edf9; color: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--ink-soft); border: 2px solid var(--line); }
.btn-danger { background: var(--ng); color: #fff; }
.row-btns { display: flex; flex-wrap: wrap; gap: 10px; }
.row-btns .btn { flex: 1 1 160px; }
.row-btns.center { justify-content: center; }

/* ---------- 選択肢 ---------- */
.choice-row { display: flex; flex-wrap: wrap; gap: 10px; }
.choice {
  flex: 1 1 70px;
  min-height: 64px;
  font: inherit;
  font-size: 1.5rem;
  font-weight: 700;
  background: #fff;
  border: 3px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px;
}
.choice small { font-size: .72rem; font-weight: 400; color: var(--ink-soft); }
.choice-sm { font-size: .95rem; min-height: 52px; }
.choice[aria-checked="true"] {
  border-color: var(--primary);
  background: #e8f1fd;
  color: var(--primary-dark);
}
.choice[aria-checked="true"] small { color: var(--primary-dark); }

/* ---------- ホームメニュー ---------- */
.menu-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin-bottom: 16px; }
.menu-btn {
  font: inherit;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 24px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  min-height: 130px;
}
.menu-btn:active { background: #f2f7fd; }
.menu-icon { font-size: 2.4rem; line-height: 1; }
.menu-label { font-size: 1.05rem; font-weight: 700; }
.menu-play { background: linear-gradient(160deg, #fff, #e8f1fd); }
.summary { font-size: .95rem; line-height: 1.9; }
.summary b { font-size: 1.15rem; color: var(--primary-dark); }

/* ---------- プレイ画面 ---------- */
.play-bar {
  position: sticky;
  top: calc(56px + env(safe-area-inset-top));
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  margin-bottom: 12px;
}
.play-info { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.badge {
  background: var(--primary);
  color: #fff;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: .8rem;
  font-weight: 700;
}
.badge-quiet { background: #e3edf9; color: var(--primary-dark); }
.timer {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--primary-dark);
}
.play-progress { font-size: .82rem; color: var(--ink-soft); width: 100%; text-align: right; }
.play-actions { display: flex; gap: 10px; margin-top: 16px; }
.play-actions .btn-lg { flex: 2; }
.play-actions .btn-ghost { flex: 1; }

/* ---------- 百ますグリッド ---------- */
.grid-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 4px; }
.grid {
  display: grid;
  grid-template-columns: repeat(11, var(--cell));
  gap: 2px;
  background: var(--line);
  border: 2px solid var(--ink-soft);
  border-radius: 6px;
  padding: 2px;
  width: max-content;
  margin: 0 auto;
}
.gcell {
  width: var(--cell);
  height: var(--cell);
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--cell-font);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.gcell.head { background: #dbe8f7; color: var(--primary-dark); }
.gcell.corner { background: var(--primary); color: #fff; font-size: calc(var(--cell-font) * .9); }
.gcell.disabled { background: repeating-linear-gradient(135deg, #e7ecf2, #e7ecf2 4px, #dde4ec 4px, #dde4ec 8px); }
.gcell input {
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
  text-align: center;
  font: inherit;
  font-size: var(--cell-font);
  font-weight: 700;
  color: var(--ink);
  padding: 0;
  border-radius: 0;
  -moz-appearance: textfield;
}
.gcell input::-webkit-outer-spin-button,
.gcell input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.gcell input:focus { outline: 0; }
.gcell:focus-within { background: #fff3d9; box-shadow: inset 0 0 0 3px var(--accent); }
.gcell.filled { background: #f4f9ff; }
.gcell.filled:focus-within { background: #fff3d9; }

/* 採点表示 */
.grid-review .gcell.ok { background: #e5f6ee; color: var(--ok); }
.grid-review .gcell.ng { background: #fdeceb; color: var(--ng); flex-direction: column; gap: 0; font-size: calc(var(--cell-font) * .8); }
.grid-review .gcell.ng .correct { font-size: calc(var(--cell-font) * .6); color: var(--ink-soft); }

/* ---------- 結果 ---------- */
.result-card { text-align: center; }
.result-meta { color: var(--ink-soft); font-size: .9rem; margin: 0 0 16px; }
.result-main { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-bottom: 12px; }
.result-block {
  flex: 1 1 130px;
  background: #f3f8ff;
  border-radius: 12px;
  padding: 14px 8px;
}
.result-label { display: block; font-size: .8rem; color: var(--ink-soft); margin-bottom: 6px; }
.result-value { display: block; font-size: 1.8rem; font-weight: 700; color: var(--primary-dark); }
.result-diff { font-size: 1.05rem; font-weight: 700; color: var(--accent); min-height: 1.6em; margin: 8px 0 18px; }

/* ---------- 提出用 ---------- */
.submit-sheet {
  background: #fff;
  border: 4px solid var(--primary);
  border-radius: var(--radius);
  padding: 20px 18px;
  margin-bottom: 14px;
}
.submit-head {
  font-size: 1.15rem;
  font-weight: 700;
  text-align: center;
  color: var(--primary-dark);
  border-bottom: 2px dashed var(--line);
  padding-bottom: 10px;
  margin-bottom: 14px;
}
.submit-list { display: grid; grid-template-columns: auto 1fr; gap: 10px 16px; margin: 0; }
.submit-list dt { color: var(--ink-soft); font-size: .9rem; }
.submit-list dd { margin: 0; font-weight: 700; font-size: 1.1rem; text-align: right; }

/* ---------- 履歴 ---------- */
.chart-box { margin-bottom: 20px; }
.chart-box canvas { width: 100%; height: 200px; background: #fbfdff; border: 1px solid var(--line); border-radius: 10px; }
.table-wrap { overflow-x: auto; }
.history-table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.history-table th, .history-table td { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.history-table th { background: #f2f7fd; color: var(--ink-soft); font-size: .8rem; }

/* ---------- 教員モード ---------- */
.share-code {
  font-size: 1.6rem;
  font-weight: 700;
  text-align: center;
  letter-spacing: .06em;
  background: #f3f8ff;
  border: 3px dashed var(--primary);
  border-radius: 12px;
  padding: 18px 10px;
  margin-bottom: 14px;
  word-break: break-all;
}
.qr-box { text-align: center; }
.qr-box #t-qr { display: inline-block; padding: 12px; background: #fff; border: 1px solid var(--line); border-radius: 10px; min-height: 40px; }
.qr-box img, .qr-box canvas { display: block; }

/* ---------- バックアップ / 復元 ---------- */
.btn-touch { min-height: 60px; font-size: 1.05rem; }
.backup-btns { margin-top: 14px; }
.backup-btns .btn { flex: 1 1 200px; }
.backup-msg {
  margin: 14px 0 0;
  padding: 12px 14px;
  border-radius: 10px;
  font-weight: 700;
  line-height: 1.6;
  background: #e5f6ee;
  color: #17724c;
}
.backup-msg.is-error { background: #fdeceb; color: #a92b21; }

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(31, 41, 51, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-box {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(31, 41, 51, .3);
  padding: 24px 20px 20px;
  max-width: 420px;
  width: 100%;
}
.modal-msg { margin: 0 0 20px; font-size: 1.05rem; line-height: 1.7; white-space: pre-line; }

/* ---------- トースト ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: rgba(31, 41, 51, .92);
  color: #fff;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: .9rem;
  z-index: 100;
}

/* ---------- 印刷 ---------- */
@media print {
  .app-header, .play-bar, .play-actions, .row-btns, .btn, .hint, .toast { display: none !important; }
  body { background: #fff; }
  main { max-width: none; }
  .card, .submit-sheet { box-shadow: none; border: 1px solid #999; page-break-inside: avoid; }
  :root { --cell: 46px; --cell-font: 20px; }
}

/* ---------- 大画面 ---------- */
@media (min-width: 720px) {
  :root { --cell: 56px; --cell-font: 24px; }
  .menu-grid { grid-template-columns: repeat(4, 1fr); }
  .app-header h1 { font-size: 1.3rem; }
}
