:root {
  --bg: #16261f;
  --bg2: #1f3a2e;
  --panel: #24392f;
  --text: #f1f5ee;
  --muted: #9fb3a6;
  --accent: #f5b83d;
  --accent-ink: #2a1e00;
  --ground: #8a8f86;
  --ground-line: #7b8077;
  --truck: #3fae5a;
  --star: #ffd257;
  --cell: 52px;
  --gap: 4px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(120% 80% at 50% 0%, var(--bg2), var(--bg));
  color: var(--text);
  font-family: "Hiragino Sans", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  overscroll-behavior: none;
  user-select: none; -webkit-user-select: none;
}
#app {
  max-width: 480px; margin: 0 auto; min-height: 100%;
  padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  position: relative;
}
.screen { display: none; flex-direction: column; align-items: stretch; gap: 14px; }
.screen.active { display: flex; }

/* ---- ステージ選択 ---- */
.title-area { text-align: center; padding-top: 18px; }
.title-badge {
  display: inline-block; font-size: 12px; letter-spacing: .08em;
  background: rgba(245,184,61,.15); color: var(--accent);
  padding: 4px 10px; border-radius: 999px;
}
h1 { font-size: 40px; margin: 10px 0 6px; letter-spacing: .04em; text-shadow: 0 3px 0 #0d1813; }
.lead { color: var(--muted); margin: 0; font-size: 14px; line-height: 1.7; }
.progress-line { text-align: center; color: var(--star); font-weight: 700; }
.stage-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.stage-btn {
  aspect-ratio: 1; border: none; border-radius: 12px;
  background: var(--panel); color: var(--text);
  font-size: 18px; font-weight: 700; font-family: inherit;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  box-shadow: 0 3px 0 #0f1c16; cursor: pointer;
}
.stage-btn .s { font-size: 10px; color: var(--star); letter-spacing: 1px; }
.stage-btn.locked { opacity: .35; cursor: default; }
.stage-btn.cleared { background: #2e5440; }

/* ---- ボタン ---- */
.btn {
  border: none; border-radius: 12px; padding: 14px 10px;
  font-size: 15px; font-weight: 700; font-family: inherit;
  background: var(--panel); color: var(--text);
  box-shadow: 0 3px 0 #0f1c16; cursor: pointer; flex: 1;
}
.btn:active, .stage-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #0f1c16; }
.btn.accent { background: var(--accent); color: var(--accent-ink); box-shadow: 0 3px 0 #9c6d10; }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); text-decoration: underline; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; border: none;
  background: var(--panel); color: var(--text); font-size: 26px; line-height: 1; cursor: pointer;
}

/* ---- プレイ画面 ---- */
.topbar { display: flex; align-items: center; gap: 10px; }
.stage-label { font-weight: 800; font-size: 20px; letter-spacing: .06em; }
.spacer { flex: 1; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--panel); border-radius: 12px; padding: 8px; text-align: center; }
.stat-k { font-size: 11px; color: var(--muted); }
.stat-v { font-size: 22px; font-weight: 800; }

.board-wrap { display: flex; justify-content: center; padding: 6px 0 10px; overflow: hidden; }
.board {
  position: relative;
  width: calc(var(--cell) * 6 + var(--gap) * 7);
  height: calc(var(--cell) * 6 + var(--gap) * 7);
  background-color: var(--ground);
  background-image:
    linear-gradient(var(--ground-line) 2px, transparent 2px),
    linear-gradient(90deg, var(--ground-line) 2px, transparent 2px);
  background-size: calc(var(--cell) + var(--gap)) calc(var(--cell) + var(--gap));
  background-position: calc(var(--gap) / 2 - 1px) calc(var(--gap) / 2 - 1px);
  border-radius: 10px;
  border: 6px solid #4b5148;
  box-shadow: 0 6px 0 #0d1813, inset 0 0 0 2px rgba(0,0,0,.15);
  touch-action: none;
}
.exit {
  position: absolute; right: -14px; width: 14px;
  height: calc(var(--cell) + var(--gap));
  background: repeating-linear-gradient(45deg, #f5b83d 0 6px, #222 6px 12px);
  border-radius: 0 6px 6px 0;
}
.exit span {
  position: absolute; left: 18px; top: 50%; transform: translateY(-50%);
  font-size: 10px; font-weight: 800; color: var(--accent); letter-spacing: .1em;
  writing-mode: vertical-rl;
}

.piece {
  position: absolute; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: rgba(255,255,255,.92); font-size: 13px;
  box-shadow: 0 3px 0 rgba(0,0,0,.35), inset 0 0 0 2px rgba(255,255,255,.12);
  cursor: grab; touch-action: none;
  transition: left .12s ease, top .12s ease;
}
.piece.dragging { transition: none; z-index: 5; filter: brightness(1.12); cursor: grabbing; }
.piece.hint { animation: hint 0.6s ease-in-out 3; }
@keyframes hint { 50% { filter: brightness(1.6); box-shadow: 0 0 0 4px var(--accent); } }

/* コンテナ（ストライプ付き） */
.piece.box {
  background-image: repeating-linear-gradient(var(--stripe-dir), rgba(0,0,0,.14) 0 3px, transparent 3px 9px);
}
.piece.h { --stripe-dir: 90deg; }
.piece.v { --stripe-dir: 0deg; }
.piece .lbl {
  background: rgba(0,0,0,.28); padding: 2px 6px; border-radius: 6px; font-size: 12px;
  writing-mode: horizontal-tb;
}
.piece.v .lbl { writing-mode: vertical-rl; padding: 6px 2px; }

/* 種類ごとの色 */
.k-can   { background-color: #c2483f; }
.k-paper { background-color: #3a6fb0; }
.k-pet   { background-color: #2f9aa3; }
.k-metal { background-color: #6c6f78; }
.k-wood  { background-color: #9a6a3a; }
.k-glass { background-color: #7a5bb0; }
.k-drum  { background-color: #d07a2a; border-radius: 14px; }

/* 収集車 */
.piece.truck { background: transparent; box-shadow: none; padding: 0; }
.piece.truck svg { width: 100%; height: 100%; display: block; filter: drop-shadow(0 3px 0 rgba(0,0,0,.35)); }
.piece.truck.leaving { transition: left .5s ease-in; }

.controls { display: flex; gap: 8px; }

/* ---- モーダル ---- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: none; align-items: center; justify-content: center; padding: 16px; z-index: 20;
}
.modal.show { display: flex; }
.modal-card {
  width: 100%; max-width: 360px; background: var(--bg2); border-radius: 18px;
  padding: 22px 18px 18px; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,.4);
  animation: pop .25s ease-out;
}
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.clear-title { font-size: 24px; font-weight: 800; margin-bottom: 8px; }
.clear-stars { font-size: 40px; color: var(--star); letter-spacing: 6px; min-height: 52px; }
.clear-stars .off { color: rgba(255,255,255,.18); }
.clear-text { color: var(--muted); margin: 6px 0 16px; line-height: 1.6; font-size: 14px; }
.modal-actions { display: flex; gap: 8px; }
.howto { text-align: left; line-height: 1.9; padding-left: 20px; margin: 4px 0 16px; font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: rgba(0,0,0,.8); color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 13px; z-index: 30; pointer-events: none;
}
