:root {
  --tile-size: 44px;
  --bg-gradient-1: #667eea;
  --bg-gradient-2: #764ba2;
  --accent: #ffd166;
  --accent-dark: #d69f00;
  --wall: #4a5f7a;
  --floor: #f5e6d3;
  --panel: rgba(0, 0, 0, 0.25);
  --btn-glass: rgba(255, 255, 255, 0.22);
  --btn-glass-hover: rgba(255, 255, 255, 0.4);
}

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Yu Gothic UI", "Hiragino Sans", "Meiryo", sans-serif;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  background: linear-gradient(135deg, var(--bg-gradient-1) 0%, var(--bg-gradient-2) 100%);
  color: #fff;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

#app {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  max-width: 500px;
  position: relative;
  display: flex;
  flex-direction: column;
}

.screen {
  display: none;
  flex: 1;
  width: 100%;
  padding: 12px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
}

.screen.active {
  display: flex;
}

/* タイトル画面 */
.title {
  font-size: clamp(2.5rem, 12vw, 4rem);
  font-weight: 900;
  text-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  letter-spacing: 0.15em;
}

.subtitle {
  font-size: 1.15rem;
  text-align: center;
  line-height: 1.6;
  opacity: 0.95;
}

.inline-icon {
  width: 1.4em;
  height: 1.4em;
  vertical-align: middle;
  margin: 0 2px;
  display: inline-block;
}

/* ボタン */
.btn-primary, .btn-secondary {
  border: none;
  border-radius: 16px;
  font-weight: bold;
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.05s, background 0.1s;
}

.btn-primary {
  padding: 16px 32px;
  font-size: 1.25rem;
  background: var(--accent);
  color: #333;
  min-width: 200px;
  min-height: 56px;
  box-shadow: 0 4px 0 var(--accent-dark), 0 8px 16px rgba(0, 0, 0, 0.2);
}

.btn-primary:active {
  box-shadow: 0 1px 0 var(--accent-dark), 0 3px 6px rgba(0, 0, 0, 0.15);
  transform: translateY(3px);
}

.btn-secondary {
  padding: 10px 14px;
  font-size: 0.95rem;
  background: var(--btn-glass);
  color: #fff;
  min-height: 42px;
  min-width: 96px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.28);
}

.btn-secondary:active {
  background: var(--btn-glass-hover);
  transform: scale(0.97);
}

/* ゲーム画面 */
#screen-game {
  padding: 8px;
  justify-content: space-between;
  gap: 8px;
}

/* ステージ番号と手数は中央寄せの2段構成に */
.game-header {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 14px;
  background: var(--panel);
  border-radius: 12px;
  font-size: 0.95rem;
  gap: 2px;
}

.stage-info,
.moves-info {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.label {
  font-size: 0.78rem;
  opacity: 0.75;
  margin-right: 2px;
}

.separator {
  opacity: 0.5;
  margin: 0 6px;
}

#stage-num, #moves-num, #best-num {
  font-size: 1.2rem;
  font-weight: bold;
}

.stage-name {
  font-size: 0.95rem;
  opacity: 0.9;
  margin-left: 8px;
}

.moves-info {
  font-size: 0.85rem;
  opacity: 0.85;
}

.moves-info #moves-num,
.moves-info #best-num {
  font-size: 1rem;
}

/* マップは cell だけで描く。板の枠・背景・影は消してマップシルエットを irregular に */
.board {
  display: grid;
  gap: 0;
  background: transparent;
  padding: 0;
  border-radius: 0;
  box-shadow: none;
  position: relative;
  border: none;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-size) * 0.7);
  line-height: 1;
  color: #333;
  position: relative;
  min-width: 0;
  min-height: 0;
}

/* 市松模様の床（濃淡2色）— 見やすさアップ */
.cell.floor-a { background: #f5e6d3; }
.cell.floor-b { background: #e8d5b8; }

/* ゴール位置は黄色く目立たせる（床と同化しないよう） */
.cell.goal {
  background: #ffde7a;
}

/* 壁は「床に接する辺だけ 8px の border」として描画。斜めだけ隣接の corner wall は cell 全体を塗る */
.cell.wall {
  background: transparent;
  border: 0 solid var(--wall);
}
.cell.wall.wall-t { border-top-width: 8px; }
.cell.wall.wall-b { border-bottom-width: 8px; }
.cell.wall.wall-l { border-left-width: 8px; }
.cell.wall.wall-r { border-right-width: 8px; }
.cell.wall.wall-corner {
  background: var(--wall);
}

/* 荷物が置かれてないゴールにだけ星（SVG）を表示 */
.cell.goal:not(.box)::before {
  content: '';
  position: absolute;
  inset: 20%;
  background-image: url("../assets/goal.svg");
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: drop-shadow(0 1px 2px rgba(120, 80, 0, 0.4));
  pointer-events: none;
}

/* 荷物がゴール上に乗ったら緑背景（達成の視覚フィードバック） */
.cell.box.goal {
  background: #7ed87e;
  box-shadow: inset 0 0 0 2px rgba(20, 120, 20, 0.35);
}

/* プレイヤーと箱は board に絶対配置される sprite。移動は transform の transition で滑らかに */
.player-sprite,
.box-sprite {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--tile-size);
  height: var(--tile-size);
  transition: transform 0.24s ease-out;
  pointer-events: none;
}

.player-sprite {
  z-index: 3;
}

/* 荷物 sprite は上から見た四角いコンテナ（SVG） */
.box-sprite {
  z-index: 2;
}

.box-sprite::before {
  content: '';
  position: absolute;
  inset: 6%;
  background-image: url("../assets/box.svg");
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

/* ゴール達成時は色相を回して緑寄りにハイライト */
.box-sprite.on-goal::before {
  filter: hue-rotate(55deg) saturate(1.3) brightness(0.95);
}

/* 内側 div で idle bounce アニメ、その ::before で SVG 差し替えと mirror */
.player-inner {
  position: absolute;
  inset: 0;
  animation: player-idle 1.4s infinite ease-in-out;
}

.player-inner::before {
  content: '';
  position: absolute;
  inset: 0;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.face-down  .player-inner::before { background-image: url("../assets/player_front.svg"); }
.face-up    .player-inner::before { background-image: url("../assets/player_back.svg"); }
.face-right .player-inner::before { background-image: url("../assets/player_side.svg"); }
.face-left  .player-inner::before {
  background-image: url("../assets/player_side.svg");
  transform: scaleX(-1);
}

@keyframes player-idle {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-2px); }
}

/* クリア直前の cheer アニメ: 正面向きで2回ジャンプ + 金色ハイライト */
.player-inner.cheer {
  animation: player-cheer 1s ease-in-out;
  filter: drop-shadow(0 0 6px gold);
}

@keyframes player-cheer {
  0%   { transform: translateY(0); }
  20%  { transform: translateY(-12px); }
  40%  { transform: translateY(0); }
  60%  { transform: translateY(-12px); }
  80%  { transform: translateY(0); }
  100% { transform: translateY(0); }
}

/* 下部パネル: 左に操作ボタン群、右に Dパッド（他社アプリのレイアウト参考） */
.bottom-panel {
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  gap: 12px;
  width: 100%;
}

/* 「一つもどる」を上、サブアクションを下に。誤爆しづらいよう間隔を空ける */
.side-controls {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 12px;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 55%;
}

/* メイン操作は「一つもどる」— 目立つ黄色ボタン */
.btn-main {
  width: 100%;
  padding: 14px 12px;
  font-size: 1.05rem;
  background: var(--accent);
  color: #333;
  border: none;
  border-radius: 14px;
  font-weight: bold;
  min-height: 54px;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--accent-dark), 0 5px 10px rgba(0, 0, 0, 0.15);
  font-family: inherit;
  transition: transform 0.05s;
}

.btn-main:active {
  box-shadow: 0 1px 0 var(--accent-dark);
  transform: translateY(2px);
}

/* サブ操作: 誤爆しづらいよう小さめ、下段に横並び */
.sub-actions {
  display: flex;
  gap: 6px;
}

.btn-mini {
  flex: 1;
  padding: 6px 6px;
  font-size: 0.7rem;
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  min-height: 34px;
  min-width: 0;
  cursor: pointer;
  font-family: inherit;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: transform 0.05s, background 0.1s;
}

.btn-mini:active {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(0.96);
}

/* Dパッド: 右下、指の届きやすい範囲 */
.dpad {
  display: grid;
  grid-template-columns: 52px 52px 52px;
  grid-template-rows: 52px 52px 52px;
  gap: 6px;
  flex: 0 0 auto;
}

/* Dパッドを立体的に。gradient で上下ハイライト、box-shadow で厚み感 */
.dpad-btn {
  border: none;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.12));
  color: white;
  font-size: 1.4rem;
  border-radius: 14px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  cursor: pointer;
  box-shadow:
    0 4px 0 rgba(0, 0, 0, 0.28),
    0 8px 14px rgba(0, 0, 0, 0.2),
    inset 0 1px 0 rgba(255, 255, 255, 0.55),
    inset 0 -2px 0 rgba(0, 0, 0, 0.12);
  font-family: inherit;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
  transition: transform 0.05s, box-shadow 0.05s, background 0.05s;
}

.dpad-btn:active {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.42), rgba(255, 255, 255, 0.2));
  box-shadow:
    0 1px 0 rgba(0, 0, 0, 0.28),
    0 3px 6px rgba(0, 0, 0, 0.2),
    inset 0 1px 0 rgba(255, 255, 255, 0.45),
    inset 0 -1px 0 rgba(0, 0, 0, 0.1);
  transform: translateY(3px);
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

/* クリア画面 */
.clear-star {
  font-size: 5rem;
  animation: pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes pop {
  0% { transform: scale(0); }
  100% { transform: scale(1); }
}

#screen-clear h2 {
  font-size: 2.8rem;
  color: var(--accent);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.clear-stats {
  font-size: 1.15rem;
  line-height: 1.9;
  text-align: center;
}

.clear-stats span {
  font-weight: bold;
  color: var(--accent);
  margin-left: 6px;
}

/* 「つぎのステージ」と「タイトルへ」を離して誤爆防止、タイトルへは控えめに */
.clear-buttons {
  display: flex;
  flex-direction: column;
  gap: 44px;
  align-items: center;
  margin-top: 8px;
}

.btn-tertiary {
  padding: 6px 14px;
  font-size: 0.85rem;
  background: transparent;
  color: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  min-height: 32px;
  min-width: 90px;
  cursor: pointer;
  font-family: inherit;
}

.btn-tertiary:active {
  background: rgba(255, 255, 255, 0.15);
}

/* ==================== ステージセレクト画面 ==================== */
#screen-select {
  justify-content: flex-start;
  gap: 10px;
  padding: 12px 8px;
}

.select-header {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 0 4px;
}

.select-header h2 {
  font-size: 1.3rem;
  flex: 1;
  text-align: center;
  color: var(--accent);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.select-progress {
  font-size: 0.8rem;
  opacity: 0.9;
  padding: 4px 10px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.select-progress #select-cleared {
  color: var(--accent);
  font-weight: bold;
}

/* ワールドタブ（★1〜★5） */
.world-tabs {
  display: flex;
  gap: 6px;
  width: 100%;
  overflow-x: auto;
  padding: 4px 2px 6px;
  scrollbar-width: none;
}

.world-tabs::-webkit-scrollbar { display: none; }

.world-tab {
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.22);
  color: rgba(255, 255, 255, 0.85);
  border-radius: 10px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 62px;
  font-size: 0.7rem;
  transition: background 0.1s, transform 0.05s;
}

.world-tab .world-stars {
  color: var(--accent);
  font-size: 0.78rem;
  letter-spacing: -1px;
}

.world-tab.active {
  background: var(--accent);
  color: #333;
  border-color: var(--accent);
  font-weight: bold;
  transform: translateY(-1px);
}

.world-tab.active .world-stars {
  color: #7a4d00;
}

/* ステージカードの grid */
.stage-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  width: 100%;
  overflow-y: auto;
  padding: 4px;
  flex: 1;
  align-content: start;
}

.stage-card {
  position: relative;
  aspect-ratio: 1;
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: #fff;
  border-radius: 12px;
  cursor: pointer;
  padding: 5px 5px 3px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: space-between;
  gap: 3px;
  font-family: inherit;
  overflow: hidden;
  transition: transform 0.05s, background 0.1s;
}

.stage-card:not(.locked):active {
  transform: scale(0.96);
}

.stage-card.locked {
  opacity: 0.35;
  cursor: not-allowed;
  background: rgba(255, 255, 255, 0.06);
}

.stage-card.cleared {
  background: rgba(255, 209, 102, 0.25);
  border-color: rgba(255, 209, 102, 0.55);
}

.stage-card.cleared::after {
  content: '★';
  position: absolute;
  top: 2px;
  right: 6px;
  color: var(--accent);
  font-size: 0.85rem;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.4);
}

.stage-num-small {
  font-size: 0.95rem;
  font-weight: bold;
  text-align: center;
  color: #fff;
  order: 1;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
  flex-shrink: 0;
}

.stage-lock {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  opacity: 0.5;
  order: 2;
}

/* 面プレビュー: 簡易 grid で色マスを敷き詰める */
.preview-grid {
  flex: 1;
  display: grid;
  gap: 1px;
  aspect-ratio: 1;
  width: 100%;
  max-width: 100%;
  align-self: center;
  justify-self: center;
  order: 2;
}

.preview-cell {
  background: #f5e6d3;
  min-width: 0;
  min-height: 0;
}

.preview-cell.pv-wall   { background: #4a5f7a; }
.preview-cell.pv-goal   { background: #ffde7a; }
.preview-cell.pv-box    { background: #c78848; }
.preview-cell.pv-player { background: #e74c3c; }
.preview-cell.pv-goal.pv-box    { background: #7ed87e; }
.preview-cell.pv-goal.pv-player { background: #ffb84a; }

/* ==================== クリアパネル ====================
   マップを残したまま bottom-panel を置き換えて表示する。
   スクショが撮れるように、盤面は消さない。 */
.clear-panel {
  display: none;
  width: 100%;
  padding: 14px 12px;
  background: linear-gradient(135deg, rgba(255, 219, 128, 0.96) 0%, rgba(255, 180, 74, 0.96) 100%);
  color: #333;
  border-radius: 16px;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  flex-direction: column;
  align-items: center;
  gap: 8px;
  animation: clear-pop 0.35s ease-out;
}

.clear-panel.active {
  display: flex;
}

@keyframes clear-pop {
  0%   { transform: scale(0.85); opacity: 0; }
  60%  { transform: scale(1.03); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.clear-panel .clear-header {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
}

.clear-panel .clear-star-mini {
  font-size: 1.6rem;
  line-height: 1;
}

.clear-panel .clear-title {
  font-size: 1.5rem;
  color: #7a4d00;
  margin: 0;
  text-shadow: none;
  letter-spacing: 0.05em;
}

.clear-panel .clear-stats {
  font-size: 0.95rem;
  color: #444;
  margin: 0;
  line-height: 1.4;
  text-align: center;
}

.clear-panel .clear-stats span {
  font-weight: bold;
  color: #7a4d00;
  margin-left: 3px;
}

/* .clear-buttons は縦積みが基本だが、パネル内では横並びに override */
.clear-panel .clear-buttons {
  display: flex;
  flex-direction: row;
  gap: 10px;
  align-items: center;
  margin-top: 4px;
  padding: 0;
}

.clear-panel .btn-primary {
  min-width: 140px;
  min-height: 46px;
  padding: 10px 18px;
  font-size: 1rem;
}

.clear-panel .btn-tertiary {
  color: rgba(0, 0, 0, 0.65);
  border-color: rgba(0, 0, 0, 0.28);
  background: rgba(255, 255, 255, 0.4);
}

.clear-panel .btn-tertiary:active {
  background: rgba(255, 255, 255, 0.6);
}

/* 到達不可な余白 cell は完全透明 (page bg が透ける) → マップシルエットが irregular に */
.cell.outside {
  background: transparent;
}
