/* ============================================================
   404ページ用「循環2048」ゲーム
   - 4×4 / 白→赤 16段階（虹）/ 進捗リング表記 / 循環
   - スライド数カウンター・カラーチップ付き
   - スマホは 100svh 内に収める（スクロールなし）
   - 外部ライブラリなし・バニラ
   ============================================================ */

.g404 {
  --board-max: 420px;
  --board-svh: 56svh; /* 盤面の高さ上限（スクロール防止） */
  --radius: 10px;
  --tile-radius: 8px;
  --anim: 80ms;      /* スライドのキビキビ感 */
  --ease: cubic-bezier(.2, .8, .2, 1);

  box-sizing: border-box;
  width: 100%;
  max-width: var(--board-max);
  min-height: 100svh;
  margin: 0 auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-family: -apple-system, "Helvetica Neue", "Hiragino Kaku Gothic ProN",
    "Yu Gothic", "Meiryo", sans-serif;
  color: #222;
  text-align: center;
  user-select: none;
  -webkit-user-select: none;
}

.g404 * { box-sizing: border-box; }

.g404__msg { margin: 0; }
.g404__code { font-size: 44px; font-weight: 700; letter-spacing: 2px; line-height: 1; }
.g404__lead { margin: 6px 0 0; font-size: 14px; color: #555; }
.g404__home {
  display: inline-block;
  margin-top: 10px;
  font-size: 13px;
  color: #222;
  text-decoration: none;
  border-bottom: 1px solid currentColor;
}
.g404__home:hover { opacity: .6; }

.g404__hint { margin: 0; font-size: 11px; color: #999; }

/* ---- ゲーム全体（カウンター＋盤面＋チップの縦積み） ---- */
.game {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(100%, var(--board-svh));
  margin: 0;
}

/* スライド数カウンター */
.game__meter {
  font-size: 13px;
  color: #666;
  line-height: 1;
}
.game__meter-val {
  font-weight: 700;
  color: #222;
  font-variant-numeric: tabular-nums;
}

/* ---- 盤面 ---- */
.game__board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #c9c9c9;
  border-radius: var(--radius);
  touch-action: none; /* スワイプ時のスクロール抑制 */
}

.grid-bg,
.tile-container {
  position: absolute;
  inset: 0;
}

.grid-bg {
  display: grid;
  grid-template-columns: repeat(var(--size, 4), 1fr);
  grid-template-rows: repeat(var(--size, 4), 1fr);
  gap: var(--gap, 10px);
  padding: var(--gap, 10px);
}
.grid-bg .cell {
  border-radius: var(--tile-radius);
  background: #b5b5b5;
}

.tile-container { padding: 0; }

/* ---- カラーチップ（段階0→15の色遷移） ---- */
.game__chips {
  display: flex;
  gap: 3px;
  width: 100%;
}
.chip {
  flex: 1 1 0;
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, .14);
}

/* ---- タイル ---- */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  transition: transform var(--anim) var(--ease);
  will-change: transform;
}
.tile__inner {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: var(--tile-radius);
  border: 1px solid rgba(0, 0, 0, .16);
  overflow: hidden;
  /* background（段階色）は JS でインライン指定 */
}
/* 外周の進捗リング（段階ぶん時計回りに満ちる。background は JS で conic-gradient 指定） */
.tile__ring {
  position: absolute;
  inset: 0;
}
/* 中央のフェイス（段階色。リングぶんだけ内側に収める） */
.tile__face {
  position: absolute;
  inset: 13%;
  border-radius: calc(var(--tile-radius) - 3px);
  /* background（段階色）は JS でインライン指定 */
}

/* 出現・合体の弾み（inner側で。位置transformと競合させない） */
.tile--new .tile__inner { animation: g404-appear 110ms ease-out; }
.tile--merged .tile__inner { animation: g404-pop 110ms ease-out; }

@keyframes g404-appear {
  from { transform: scale(0); }
  to   { transform: scale(1); }
}
@keyframes g404-pop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.16); }
  100% { transform: scale(1); }
}

/* ---- ゲームメッセージ（詰み） ---- */
.game-message {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(255, 255, 255, .65);
  border-radius: var(--radius);
  z-index: 5;
  animation: g404-fade 260ms ease;
}
.game-message.is-visible { display: flex; }
.game-message__title { margin: 0; font-size: 18px; font-weight: 700; color: #222; }
.game-message__moves { margin: 0; font-size: 14px; color: #333; font-variant-numeric: tabular-nums; }
@keyframes g404-fade { from { opacity: 0; } to { opacity: 1; } }

/* ---- ボタン ---- */
.g404 .btn {
  appearance: none;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  color: #222;
  background: #fff;
  border: 1px solid #222;
  border-radius: 999px;
  padding: 8px 20px;
  transition: background .15s ease, color .15s ease;
}
.g404 .btn:hover { background: #222; color: #fff; }

.g404__actions { margin: 0; }

/* ---- 背の低いビューポート向けの詰め ---- */
@media (max-height: 680px) {
  .g404 { --board-svh: 52svh; gap: 8px; padding: 8px; }
  .g404__code { font-size: 34px; }
  .g404__lead { margin: 4px 0 0; font-size: 12px; }
  .g404__home { margin-top: 6px; }
}
