:root {
  --bar: #3f86de;
  --bar-dark: #2a68b8;
  --bg: #eef7f4;
  --grid: #c9e3db;
  --ink: #23324a;
  --sub: #5b6b80;
  --ok: #e5393b;
  --ng: #2e5bd8;
  --star: #ffb800;
  --card: #ffffff;
  --shadow: #c6dbd4;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  /* 子どもが 読みやすい 教科書体 / 丸ゴシックを さきに */
  font-family: "UD デジタル 教科書体 NK-R", "UD Digi Kyokasho NK-R", "UD デジタル 教科書体 N-R", "UD Digi Kyokasho N-R",
    "Hiragino Maru Gothic ProN", "BIZ UDPGothic", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
rt { font-size: 0.45em; letter-spacing: 0; }

.screen { position: fixed; inset: 0; display: none; flex-direction: column; }
.screen.on { display: flex; }

/* ============ はじめの画面 ============ */
#home {
  overflow-y: auto;
  padding: calc(20px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 40px 40px;
}
.home-in {
  margin: auto;
  width: min(580px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
#home h1 {
  margin: 0;
  font-size: clamp(38px, 9vw, 64px);
  letter-spacing: 0.04em;
  color: var(--bar-dark);
  text-shadow: 0 4px 0 #fff, 0 6px 14px rgba(0, 0, 0, 0.08);
  text-align: center;
  line-height: 1.2;
}
#home h1 span { color: var(--ok); white-space: nowrap; }
.sub { margin: 0; font-size: clamp(18px, 4.4vw, 24px); color: var(--sub); }

.levels { display: grid; gap: 14px; width: 100%; }
.lv {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 2px 12px;
  padding: 14px 16px 14px 18px;
  border: none;
  border-radius: 24px;
  background: var(--card);
  box-shadow: 0 6px 0 var(--shadow), 0 10px 24px rgba(0, 0, 0, 0.07);
  text-align: left;
  transition: transform 0.08s, box-shadow 0.08s;
}
.lv:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--shadow); }
.lv-stars { grid-row: 1; grid-column: 1; color: var(--star); font-size: 22px; letter-spacing: -2px; }
.lv b { grid-row: 1; grid-column: 2; font-size: clamp(24px, 6vw, 30px); }
.lv small { grid-row: 2; grid-column: 1 / span 2; font-size: clamp(14px, 3.6vw, 17px); color: var(--sub); line-height: 1.4; }
.lv .mini { grid-row: 1 / span 2; grid-column: 3; width: clamp(96px, 26vw, 150px); height: clamp(60px, 15vw, 84px); }
.mini rect, .mini polygon { stroke-width: 7; stroke-linejoin: round; }

.home-total { margin: 4px 0 0; font-size: 20px; text-align: center; }
.home-total b { font-size: 28px; }
.home-total .best { display: inline-block; font-size: 15px; color: var(--sub); }
.home-total .best b { font-size: 15px; }
.star { color: var(--star); }

.opts { display: flex; gap: 12px; }
.toggle {
  min-width: 110px;
  padding: 10px 18px;
  border: 3px solid var(--bar);
  border-radius: 999px;
  background: #fff;
  font-size: 18px;
  color: var(--bar-dark);
}
.toggle::after { content: ' オフ'; }
.toggle[aria-pressed="true"] { background: var(--bar); color: #fff; }
.toggle[aria-pressed="true"]::after { content: ' オン'; }

.ver { margin: 0; font-size: 12px; color: var(--sub); opacity: 0.7; min-height: 1em; }

.update-bar {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  display: none;
  align-items: center;
  gap: 12px;
  padding: 10px 10px 10px 20px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-size: 17px;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  transform: translateX(-50%);
  z-index: 5;
}
.update-bar.on { display: flex; }
.update-bar button {
  padding: 8px 18px;
  border: none;
  border-radius: 999px;
  background: #ff8a3d;
  color: #fff;
  font-size: 17px;
}
.update-bar button:disabled { opacity: 0.6; }

/* ============ あそぶ画面 ============ */
.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 8px calc(12px + env(safe-area-inset-left));
  background: linear-gradient(#5a9be6, var(--bar));
  color: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  z-index: 2;
}
.back {
  padding: 8px 16px;
  border: none;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.22);
  color: #fff;
  font-size: 18px;
  white-space: nowrap;
}
.back:active { background: rgba(255, 255, 255, 0.4); }
.bar-title { flex: 1; text-align: center; font-size: 18px; opacity: 0.95; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.score {
  padding: 4px 14px;
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font-size: 20px;
  white-space: nowrap;
}
.score b { font-size: 24px; }
.score.bump { animation: bump 0.45s ease-out; }

.ask {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 16px;
  padding: 10px calc(16px + env(safe-area-inset-right)) 0 calc(16px + env(safe-area-inset-left));
}
.prompt {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0;
  border: none;
  background: none;
  font-size: clamp(20px, 4.6vw, 32px);
  text-align: left;
}
.hand {
  display: grid;
  place-items: center;
  flex: none;
  width: 1.5em;
  height: 1.5em;
  border-radius: 50%;
  background: var(--bar);
  font-size: 0.8em;
}
.hint {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 4px 12px;
  border-radius: 12px;
  background: #fff;
  font-size: clamp(14px, 3.4vw, 18px);
  box-shadow: 0 2px 0 var(--shadow);
}
.hint.on { display: inline-flex; }
.hint svg { width: 96px; height: 28px; flex: none; }
.hint .h-a { fill: #f7cf2f; stroke: #8f4b14; stroke-width: 6; stroke-linejoin: round; }
.hint text { font-size: 54px; font-weight: bold; fill: var(--ink); text-anchor: middle; dominant-baseline: central; }
#game.done .hint { visibility: hidden; } /* 「つぎへ」と かさならないように */

.stage-wrap { position: relative; flex: 1; min-height: 0; }
#stage { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#stage.enter { animation: enter 0.35s ease-out; }

.grid line { stroke: var(--grid); stroke-width: 1; vector-effect: non-scaling-stroke; }
.body rect, .body polygon { stroke-width: 6; stroke-linejoin: round; }
.body { transform-box: fill-box; transform-origin: center; }
.body.shake { animation: shake 0.5s ease-in-out; }
.body.pop { animation: pop-shape 0.4s ease-out; }

.lab {
  font-size: 54px;
  font-weight: bold;
  fill: #fff;
  stroke: var(--line);
  stroke-width: 14px;
  stroke-linejoin: round;
  paint-order: stroke;
  text-anchor: middle;
  dominant-baseline: central;
  opacity: 0;
  transform-box: fill-box;
  transform-origin: center;
  pointer-events: none;
}
.lab.tri { font-size: 34px; stroke-width: 10px; }
.lab.extra { fill: #fff; stroke: var(--ok); }
.lab.on { opacity: 1; animation: pop 0.3s ease-out; }
.pair {
  stroke: #fff;
  stroke-width: 5;
  stroke-dasharray: 8 9;
  stroke-linecap: round;
  opacity: 0;
}
.pair.on { opacity: 0.9; }

.total {
  font-size: 100px;
  font-weight: bold;
  fill: var(--ink);
  stroke: #fff;
  stroke-width: 18px;
  stroke-linejoin: round;
  paint-order: stroke;
  text-anchor: middle;
  dominant-baseline: central;
  opacity: 0;
  transform-box: fill-box;
  transform-origin: center;
}
.total.big { fill: var(--ok); }
.total.on { opacity: 1; animation: pop 0.4s ease-out; }
.still { animation: none !important; }

.hit { fill: #000; fill-opacity: 0; cursor: pointer; }

.mk { pointer-events: none; }
.mk circle, .mk path { fill: none; stroke-linecap: round; stroke-dasharray: 1; stroke-dashoffset: 0; }
.mk .under { stroke: #fff; stroke-width: 46; }
.mk.ok .over { stroke: var(--ok); stroke-width: 26; }
.mk.ng .over { stroke: var(--ng); stroke-width: 26; }
.mk.anim circle, .mk.anim path { animation: draw 0.45s ease-out; }

.next {
  position: absolute;
  right: calc(16px + env(safe-area-inset-right));
  top: 6px;
  z-index: 3;
  padding: 10px 26px;
  border: none;
  border-radius: 999px;
  background: #ff8a3d;
  color: #fff;
  font-size: clamp(20px, 4.4vw, 26px);
  box-shadow: 0 5px 0 #c4621f;
  opacity: 0;
  transform: translateY(-10px);
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
}
.next.on { opacity: 1; transform: none; pointer-events: auto; }
.next:active { transform: translateY(4px); box-shadow: 0 1px 0 #c4621f; }

/* ============ おいわい ============ */
.fx { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 10; }
.spark {
  position: absolute;
  left: 0;
  top: 0;
  font-size: 30px;
  line-height: 1;
  color: var(--star);
  text-shadow: 0 2px 0 #c98a00;
  animation: fly 0.9s ease-out forwards;
}
.spark.paper { width: 12px; height: 18px; border-radius: 2px; }
.cheer {
  position: absolute;
  left: 50%;
  top: 42%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 18px 36px;
  border-radius: 28px;
  background: #fff;
  box-shadow: 0 8px 0 var(--shadow), 0 16px 40px rgba(0, 0, 0, 0.15);
  white-space: nowrap;
  animation: cheer 1.8s ease-out forwards;
}
.cheer b { font-size: clamp(40px, 10vw, 64px); color: var(--ok); }
.cheer span { font-size: clamp(18px, 4.4vw, 26px); }

@keyframes pop { 0% { transform: scale(0.2); } 70% { transform: scale(1.25); } 100% { transform: scale(1); } }
@keyframes pop-shape { 0% { transform: scale(1); } 40% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.3); } 100% { transform: scale(1); } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-22px) rotate(-3deg); }
  40% { transform: translateX(18px) rotate(3deg); }
  60% { transform: translateX(-12px); }
  80% { transform: translateX(8px); }
}
@keyframes draw { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }
@keyframes enter { from { opacity: 0; transform: translateX(40px); } to { opacity: 1; transform: none; } }
@keyframes fly {
  from { transform: translate(-50%, -50%) translate(var(--x0), var(--y0)) scale(0.4); opacity: 1; }
  to { transform: translate(-50%, -50%) translate(calc(var(--x0) + var(--dx)), calc(var(--y0) + var(--dy))) scale(1.1) rotate(var(--rot)); opacity: 0; }
}
@keyframes cheer {
  0% { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
  15% { transform: translate(-50%, -50%) scale(1.1); opacity: 1; }
  25% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { transform: translate(-50%, -60%) scale(1); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .body.shake, .body.pop, #stage.enter, .score.bump { animation: none; }
}
