:root {
  --bg: #dff5ff;
  --paper: #ffffff;
  --ink: #2f3a4a;
  --accent: #ff8c42;
  --accent-dark: #c75a16;
  --go: #3cc35f;
  --go-dark: #22843b;
  --sel: #2f8cff;
  --shadow: 0 6px 0 rgba(0, 0, 0, 0.12);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "Rounded Mplus 1c", sans-serif;
  font-weight: 700;
  color: var(--ink);
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 60px 26px at 12% 14%, rgba(255, 255, 255, .9) 98%, transparent),
    radial-gradient(ellipse 80px 30px at 82% 9%, rgba(255, 255, 255, .8) 98%, transparent);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;   /* ながおしメニューを出さない */
  touch-action: manipulation;    /* ダブルタップ拡大を止める */
}
.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }

/* ---------- ボタン共通 ---------- */
button { font: inherit; color: inherit; cursor: pointer; border: 0; }
.icon-btn {
  width: 56px; height: 56px; border-radius: 50%; flex: none;
  background: var(--paper); box-shadow: var(--shadow);
  font-size: 26px; line-height: 1;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.big-btn {
  min-width: 200px; padding: 14px 30px;
  border-radius: 999px; font-size: 26px; font-weight: 900;
  color: #fff; background: var(--accent); box-shadow: 0 6px 0 var(--accent-dark);
}
.big-btn.secondary { color: var(--ink); background: #fff; box-shadow: var(--shadow); }
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--accent-dark); }
.big-btn.secondary:active { box-shadow: 0 2px 0 rgba(0,0,0,.12); }
.big-btn.wiggle { animation: wiggle 1.2s ease-in-out infinite; }
@keyframes wiggle { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-3deg); } 75% { transform: rotate(3deg); } }
.danger-btn {
  padding: 12px 20px; border-radius: 12px; background: #ffe1e1; color: #a12020;
  position: relative; overflow: hidden;
}
.danger-btn.holding::after {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 0;
  background: rgba(161, 32, 32, .25); animation: hold 2s linear forwards;
}
@keyframes hold { to { width: 100%; } }

/* ---------- ホーム ---------- */
.home-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 20px 6px; }
.home-head h1 { margin: 0; font-size: 36px; font-weight: 900; letter-spacing: .04em; }
.stage-grid {
  flex: 1; min-height: 0; display: grid; gap: 12px; padding: 8px 20px 12px;
  grid-template-columns: repeat(5, 1fr); grid-auto-rows: minmax(118px, 1fr);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.stage-btn {
  position: relative; border-radius: 20px; background: var(--paper); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  padding: 6px; min-height: 0;
}
.stage-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.stage-btn.done { background: #fffbe8; }
.stage-btn .no { font-size: clamp(20px, 3vw, 30px); font-weight: 900; line-height: 1; }
.stage-btn .thumb { width: 82%; max-height: 62%; flex: 1 1 auto; min-height: 0; border-radius: 6px; }
.stage-btn .mini { position: absolute; right: -6px; top: -6px; width: 34%; aspect-ratio: 1; }
.stage-btn .mini svg { width: 100%; height: 100%; }
.home-foot { display: flex; gap: 16px; justify-content: center; padding: 10px 20px 16px; }

/* ---------- あそぶ画面 ---------- */
.play-head { display: flex; align-items: center; gap: 14px; padding: 10px 16px 4px; }
.play-title { flex: 1; display: flex; align-items: center; gap: 12px; font-size: 26px; font-weight: 900; min-width: 0; white-space: nowrap; overflow: hidden; }
.play-no:not(:empty) {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 26px;
}
.vehicle-btn {
  width: 110px; height: 60px; border-radius: 18px; background: #fff; box-shadow: var(--shadow); flex: none; padding: 4px;
}
.vehicle-btn svg { width: 100%; height: 100%; }
.vehicle-btn:active { transform: translateY(3px); }

.board-wrap { position: relative; flex: 1; min-height: 0; padding: 4px 12px; display: flex; }
.board-svg {
  width: 100%; height: 100%; touch-action: none;
  filter: drop-shadow(0 6px 0 rgba(0, 0, 0, .12));
}
.gridline { stroke: rgba(255, 255, 255, .45); stroke-width: 2; }

/* 道（縁 → 路面 → センターライン の順に重ねる） */
.r-curb  { fill: none; stroke: #4d5058; stroke-width: 64; }
.r-surf  { fill: none; stroke: #70747d; stroke-width: 54; }
.r-line  { fill: none; stroke: #fff; stroke-width: 4; stroke-dasharray: 12 12; opacity: .85; }
.r-curb-f { fill: #4d5058; }
.r-surf-f { fill: #70747d; }
/* はし */
.b-rail  { fill: none; stroke: #7a4a1f; stroke-width: 68; }
.b-deck  { fill: none; stroke: #d9a066; stroke-width: 54; }
.b-plank { fill: none; stroke: #b97a3c; stroke-width: 54; stroke-dasharray: 3 9; }
.b-rail-f { fill: #7a4a1f; }
.b-deck-f { fill: #d9a066; }
/* トンネルの入口 */
.portal { fill: #2b2118; stroke: #8c8c8c; stroke-width: 5; }

/* しんごう */
.sig .lamp { fill: #3d3d3d; }
.sig:not(.stop) .green { fill: #39e36a; }
.sig.stop .red { fill: #ff3b3b; }
.flag { transform-box: fill-box; transform-origin: left center; animation: flutter 1s ease-in-out infinite; }
@keyframes flutter { 0%, 100% { transform: scaleX(1); } 50% { transform: scaleX(.8); } }

/* パトカー・しょうぼうしゃの ランプ */
.beacon { animation: blink .5s steps(1) infinite; }
.beacon.alt { animation-delay: .25s; }
@keyframes blink { 50% { opacity: .25; } }

/* マスの光（ヒント・だめ） */
.pulse { fill: none; stroke: #ffe14d; stroke-width: 8; animation: pulse 0.8s ease-in-out 3; }
.pulse.nope { stroke: #ff7a7a; animation-iteration-count: 1; }
@keyframes pulse { 0%, 100% { opacity: 0; } 50% { opacity: 1; } }

.bubble {
  position: absolute; z-index: 5; transform: translate(-50%, -110%);
  width: 64px; height: 64px; border-radius: 50%; background: #fff; box-shadow: 0 6px 12px rgba(0,0,0,.2);
  font-size: 44px; font-weight: 900; color: var(--accent); display: flex; align-items: center; justify-content: center;
  animation: bob .8s ease-in-out infinite; pointer-events: none;
}
@keyframes bob { 50% { transform: translate(-50%, -125%); } }

.play-foot { display: flex; align-items: center; gap: 10px; padding: 6px 12px 12px; }
.palette { flex: 1; display: flex; gap: 8px; overflow-x: auto; min-width: 0; padding: 4px 2px 8px; }
.tool-btn {
  flex: none; width: 82px; height: 86px; border-radius: 18px; background: #fff; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 6px 4px;
  border: 4px solid transparent;
}
.tool-btn .ti { width: 46px; height: 46px; display: flex; align-items: center; justify-content: center; font-size: 34px; line-height: 1; }
.tool-btn .ti svg { width: 100%; height: 100%; }
.tool-btn .tl { font-size: 15px; font-weight: 900; white-space: nowrap; }
.tool-btn.active { border-color: var(--sel); background: #eaf3ff; transform: translateY(-3px); }
.tool-btn.clear { width: 92px; }
.go-btn {
  flex: none; height: 86px; padding: 0 30px; border-radius: 999px; font-size: 30px; font-weight: 900;
  color: #fff; background: var(--go); box-shadow: 0 6px 0 var(--go-dark);
  animation: breathe 1.6s ease-in-out infinite;
}
.go-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--go-dark); }
@keyframes breathe { 50% { transform: scale(1.05); } }
.driving .palette, .driving .tool-btn.clear, .driving .go-btn, .driving .vehicle-btn { opacity: .4; pointer-events: none; animation: none; }

/* ---------- オーバーレイ ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(30, 50, 70, .45);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.done-card, .book-card, .settings-card, .vehicle-card {
  background: var(--paper); border-radius: 28px; box-shadow: 0 16px 40px rgba(0,0,0,.25);
  padding: 20px 28px; max-width: 92vw; max-height: 92vh; overflow: auto;
  animation: pop .45s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop { from { transform: scale(.6); opacity: 0; } }
.card-title { margin: 0 0 14px; font-size: 28px; font-weight: 900; text-align: center; }
.done-card { text-align: center; }
.done-title { margin: 0; font-size: 52px; font-weight: 900; color: var(--accent); }
.done-vehicle { width: 220px; height: 132px; margin: 0 auto; animation: wiggle 1.2s ease-in-out infinite; }
.done-vehicle svg { width: 100%; height: 100%; }
.done-label { margin: 6px 0 16px; font-size: 24px; }
.done-btns { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }

.vehicle-list { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.vehicle-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 6px;
  border-radius: 18px; background: #f4f7fb; border: 4px solid transparent; font-size: 17px; font-weight: 900;
}
.vehicle-item svg { width: 120px; height: 72px; }
.vehicle-item.active { border-color: var(--sel); background: #eaf3ff; }

/* キラキラ粒子 */
.particle {
  position: fixed; z-index: 60; pointer-events: none; width: 14px; height: 14px; border-radius: 50%;
  background: var(--c, #ffd54a);
  animation: burst var(--d, 900ms) ease-out forwards;
}
.particle.star { border-radius: 0; clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
@keyframes burst {
  0%   { transform: translate(0, 0) scale(.4) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.1) rotate(220deg); opacity: 0; }
}

/* シール（なぞりと同じ） */
.sticker { position: relative; display: inline-block; width: 120px; height: 120px; }
.sticker svg { width: 100%; height: 100%; filter: drop-shadow(0 4px 6px rgba(0,0,0,.2)); }
.sticker::before, .sticker::after {
  content: ""; position: absolute; width: 22%; height: 22%; pointer-events: none; background: #fff;
  clip-path: polygon(50% 0, 60% 40%, 100% 50%, 60% 60%, 50% 100%, 40% 60%, 0 50%, 40% 40%);
  animation: twinkle 1.6s ease-in-out infinite;
}
.sticker::before { left: 4%; top: 8%; }
.sticker::after  { right: 2%; bottom: 10%; width: 16%; height: 16%; animation-delay: .8s; }
@keyframes twinkle { 0%, 100% { transform: scale(.3) rotate(0deg); opacity: .2; } 50% { transform: scale(1.1) rotate(45deg); opacity: 1; } }
.slot .sticker::before, .slot .sticker::after { display: none; }
.slot .sticker.pasted::before, .slot .sticker.pasted::after { display: block; animation-iteration-count: 3; }
.slot .sticker svg { filter: none; }

/* シールちょう */
.book-card { width: min(94vw, 1000px); display: flex; flex-direction: column; gap: 10px; }
.book-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; gap: 12px; }
.book-head h2 { margin: 0; font-size: 28px; font-weight: 900; }
.book-grid { display: grid; gap: 8px; grid-template-columns: repeat(10, 1fr); touch-action: none; }
.slot {
  aspect-ratio: 1; border-radius: 16px; background: #f4efe3; border: 3px dashed #d9d3c6;
  display: flex; align-items: center; justify-content: center; position: relative;
}
.slot .n { position: absolute; left: 6px; top: 2px; font-size: 16px; color: #9a8b70; }
.slot .sticker { width: 78%; height: 78%; transform: rotate(var(--rot, 0deg)) translate(var(--dx, 0), var(--dy, 0)); }
.slot.filled { border-style: solid; background: #fff; }
.slot.target { border-color: var(--accent); background: #fff3e0; }
.slot.target::after {
  content: ""; position: absolute; inset: -8px; border-radius: 22px; pointer-events: none;
  border: 5px solid rgba(255, 140, 66, .6); animation: glow 1s ease-in-out infinite;
}
@keyframes glow { 0%, 100% { opacity: 0; } 50% { opacity: 1; } }
.slot.nope { animation: shake .4s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.slot .sticker.pasted { animation: paste .5s cubic-bezier(.2, 1.5, .4, 1); }
@keyframes paste { 0% { transform: scale(1.8) rotate(var(--rot, 0deg)); opacity: .6; } 100% { transform: scale(1) rotate(var(--rot, 0deg)) translate(var(--dx, 0), var(--dy, 0)); } }
.book-tray { background: #fff7e0; border-radius: 18px; padding: 10px 14px; }
.tray-label { margin: 0 0 6px; font-size: 20px; text-align: center; }
.tray-list { display: flex; justify-content: center; gap: 14px; touch-action: none; flex-wrap: wrap; }
.tray-item { width: clamp(64px, 9vw, 96px); height: clamp(64px, 9vw, 96px); border-radius: 50%; transition: transform .15s; }
.tray-item.selected { transform: scale(1.18); box-shadow: 0 0 0 5px var(--accent); }
.tray-item.ghost { opacity: .25; }
.book-foot { display: flex; justify-content: center; }
.sticker.drag {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  width: clamp(80px, 11vw, 120px); height: clamp(80px, 11vw, 120px);
  transform: translate3d(var(--x, 0px), var(--y, 0px), 0) translate(-50%, -50%) scale(1.15) rotate(-6deg);
  border-radius: 50%; box-shadow: 0 12px 14px rgba(0, 0, 0, .3);
}
.sticker.drag svg { filter: none; }

/* せってい */
.settings-card { width: min(92vw, 640px); }
.setting { border: 0; padding: 0; margin: 0 0 18px; }
.setting legend { font-size: 18px; font-weight: 900; margin-bottom: 6px; }
.setting label { display: block; padding: 8px 4px; font-size: 17px; }
.setting input { transform: scale(1.4); margin-right: 12px; }
.note { font-size: 14px; color: #6a7a8a; margin: 4px 0 0; line-height: 1.6; }

/* スマホ縦など狭い幅 */
@media (max-width: 700px) {
  .home-head h1 { font-size: 26px; }
  .stage-grid { grid-template-columns: repeat(2, 1fr); grid-auto-rows: minmax(110px, 1fr); overflow-y: auto; }
  .big-btn { min-width: 0; font-size: 20px; padding: 12px 20px; }
  .play-title { font-size: 20px; }
  .vehicle-btn { width: 84px; height: 50px; }
  .play-foot { flex-wrap: wrap; justify-content: center; }
  .palette { flex-basis: 100%; }
  .tool-btn { width: 70px; height: 76px; }
  .tool-btn .ti { width: 38px; height: 38px; }
  .go-btn { height: 70px; font-size: 26px; flex: 1; }
  .vehicle-list { grid-template-columns: repeat(2, 1fr); }
  .book-grid { grid-template-columns: repeat(5, 1fr); }
  .vehicle-item svg { width: 100px; height: 60px; }
}

/* ---------- うっかり防止 ---------- */
/* おとなのせってい: ながおし中は輪がたまる */
.gate { position: relative; }
.gate-btn { position: relative; }
.gate-btn::after {
  content: ""; position: absolute; inset: -5px; border-radius: 50%; pointer-events: none;
  background: conic-gradient(var(--accent) var(--p, 0%), transparent 0);
  -webkit-mask: radial-gradient(circle, transparent 60%, #000 61%); mask: radial-gradient(circle, transparent 60%, #000 61%);
  opacity: 0;
}
.gate-btn.holding::after { opacity: 1; animation: gatefill 1.2s linear forwards; }
@property --p { syntax: '<percentage>'; inherits: false; initial-value: 0%; }
@keyframes gatefill { to { --p: 100%; } }
.gate-hint {
  position: absolute; right: 0; top: 64px; z-index: 10; margin: 0; white-space: nowrap;
  padding: 8px 14px; border-radius: 12px; background: #fff; box-shadow: 0 6px 14px rgba(0,0,0,.15);
  font-size: 14px; color: #6a7a8a;
}
/* やりなおし直後の「もとに もどす」 */
.undo-btn {
  position: fixed; left: 50%; bottom: calc(118px + env(safe-area-inset-bottom)); z-index: 15; transform: translateX(-50%);
  padding: 14px 28px; border-radius: 999px; background: #2f3a4a; color: #fff; font-size: 22px; font-weight: 900;
  box-shadow: 0 8px 18px rgba(0,0,0,.25); animation: undoin .3s ease-out;
}
@keyframes undoin { from { opacity: 0; transform: translateX(-50%) translateY(16px); } }
.settings-card a { color: #1f6fd1; }
