:root {
  --bg: #14110f;
  --bg-soft: #1e1a17;
  --panel: #23201c;
  --line: #3a342d;
  --grid-bg: #1a1613;
  --cell-bg: #2a251f;
  --text: #ece5da;
  --text-dim: #9c9184;
  --accent: #d8863b;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Noto Sans JP", Meiryo, sans-serif;

  /* タイル色（値ごとに段階変化） */
  --t2: #3c3428;
  --t4: #4a3c2a;
  --t8: #b5702f;
  --t16: #c17d2c;
  --t32: #cf7b3a;
  --t64: #d8863b;
  --t128: #d9a441;
  --t256: #ddb23c;
  --t512: #e0c13a;
  --t1024: #e6ca2f;
  --t2048: #f0d21e;
  --t-super: #7c4bd0;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(1000px 500px at 50% -200px, #241f1a 0%, var(--bg) 60%);
  min-height: 100vh;
}

.app {
  max-width: 460px;
  margin-inline: auto;
  padding: 16px 16px 40px;
  touch-action: none; /* スワイプ時のスクロール抑止 */
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.back {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 13px;
  white-space: nowrap;
}
.back:hover { color: var(--accent); }

.title {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 0.06em;
  flex: 1;
  text-align: right;
}
.title .accent { color: var(--accent); }

/* ---- HUD ---- */
.hud {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.score-box {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.label {
  font-size: 10px;
  color: var(--text-dim);
  letter-spacing: 0.12em;
}

.value {
  font-size: 24px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* ---- auto panel ---- */
.auto-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px 12px;
  margin-bottom: 12px;
}

.auto-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.time-left {
  font-size: 22px;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  transition: color 0.15s ease;
}

.time-left.urgent {
  color: #e5533d;
}

.countdown-track {
  height: 8px;
  background: var(--grid-bg);
  border-radius: 999px;
  overflow: hidden;
}

.countdown-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--accent), #f0d21e);
  border-radius: 999px;
  transform-origin: left center;
}

.countdown-bar.urgent {
  background: linear-gradient(90deg, #e5533d, #f0a81e);
}

/* ---- board ---- */
.board-wrap {
  position: relative;
  margin-bottom: 16px;
}

.board {
  position: relative;
  background: var(--grid-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  aspect-ratio: 1 / 1;
  width: 100%;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.board:active {
  cursor: grabbing;
}

/* 背景の空きマス（静的グリッド） */
.grid-bg {
  position: absolute;
  inset: 10px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
}

.cell {
  background: var(--cell-bg);
  border-radius: 8px;
}

/* タイルを載せる絶対配置レイヤー */
.tiles {
  position: absolute;
  inset: 10px;
  pointer-events: none;
}

/* タイル本体：--r/--c から transform で位置を決め、変化時に滑る */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - 30px) / 4);
  height: calc((100% - 30px) / 4);
  transform: translate(
    calc(var(--c) * (100% + 10px)),
    calc(var(--r) * (100% + 10px))
  );
  transition: transform 130ms ease;
  z-index: 2;
}

.tile.ghost { z-index: 1; } /* 合成で消える側は下に */

/* 見た目・文字・アニメは内側の要素が担当（位置transformと衝突させない） */
.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #14110f;
  background: var(--t2);
  font-size: clamp(20px, 8vw, 34px);
}

.tile-inner.spawn { animation: pop 0.14s ease; }
.tile-inner.merge { animation: merge 0.18s ease; }

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

/* 値ごとの色・文字色 */
.tile[data-v="2"] .tile-inner { background: var(--t2); color: #d8cdbd; }
.tile[data-v="4"] .tile-inner { background: var(--t4); color: #e5d9c6; }
.tile[data-v="8"] .tile-inner { background: var(--t8); }
.tile[data-v="16"] .tile-inner { background: var(--t16); }
.tile[data-v="32"] .tile-inner { background: var(--t32); }
.tile[data-v="64"] .tile-inner { background: var(--t64); }
.tile[data-v="128"] .tile-inner { background: var(--t128); font-size: clamp(18px, 7vw, 30px); }
.tile[data-v="256"] .tile-inner { background: var(--t256); font-size: clamp(18px, 7vw, 30px); }
.tile[data-v="512"] .tile-inner { background: var(--t512); font-size: clamp(18px, 7vw, 30px); }
.tile[data-v="1024"] .tile-inner { background: var(--t1024); font-size: clamp(15px, 6vw, 26px); }
.tile[data-v="2048"] .tile-inner { background: var(--t2048); font-size: clamp(15px, 6vw, 26px); box-shadow: 0 0 18px rgba(240,210,30,0.55); }
.tile.super .tile-inner { background: var(--t-super); color: #fff; font-size: clamp(14px, 5.5vw, 24px); }

/* ---- overlay ---- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(20, 17, 15, 0.86);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 5;
}

.overlay-title {
  margin: 0;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.overlay-score { margin: 0; color: var(--text-dim); font-size: 14px; }
.overlay-score b { color: var(--text); }

.toast {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #14110f;
  font-weight: 800;
  padding: 8px 18px;
  border-radius: 999px;
  z-index: 6;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  transition: opacity 0.3s ease;
}

.hidden { display: none !important; }

/* ---- controls ---- */
.controls {
  display: flex;
  gap: 10px;
}

.btn {
  flex: 1;
  text-align: center;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.btn:hover { border-color: var(--accent); }

.btn-icon {
  flex: 0 0 auto;
  width: 52px;
  padding: 12px 0;
  font-size: 18px;
  line-height: 1;
}

.btn-primary {
  background: var(--accent);
  color: #14110f;
  border-color: var(--accent);
}

/* オーバーレイ内のボタンは縦に伸ばさない（flex:1 の打ち消し） */
.overlay .btn {
  flex: 0 0 auto;
  width: auto;
  min-width: 160px;
  padding: 12px 28px;
}

.hint {
  margin-top: 16px;
  color: var(--text-dim);
  font-size: 12px;
  text-align: center;
  line-height: 1.6;
}

/* ---- about / SEO ---- */
.about {
  margin-top: 26px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.85;
}
.about h2 {
  margin: 0 0 8px;
  font-size: 16px;
  color: var(--text);
  letter-spacing: 0.02em;
}
.about h3 {
  margin: 16px 0 4px;
  font-size: 13px;
  color: var(--accent);
  letter-spacing: 0.04em;
}
.about p { margin: 0; }

/* ---- overlay rank ---- */
.overlay-rank {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  color: #f0d21e;
  letter-spacing: 0.06em;
}

.overlay-name { display: flex; align-items: center; gap: 8px; }
.overlay-name label { font-size: 12px; color: var(--text-dim); }
.overlay-name input {
  background: var(--grid-bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  padding: 7px 10px;
  width: 140px;
  text-align: center;
}
.overlay-name input:focus { outline: none; border-color: var(--accent); }
.overlay-actions { display: flex; gap: 10px; }
.overlay-actions .btn { min-width: 0; padding: 12px 22px; }

/* ---- ranking ---- */
.ranking {
  margin-top: 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px 16px;
}

.ranking-title {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: var(--text-dim);
}

.rank-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rank-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 8px;
  background: var(--grid-bg);
  font-size: 13px;
}

.rank-row.latest {
  border: 1px solid var(--accent);
  background: rgba(216, 134, 59, 0.1);
}

.rank-no {
  width: 2em;
  font-weight: 800;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.rank-row:nth-child(1) .rank-no { color: #f0d21e; }
.rank-row:nth-child(2) .rank-no { color: #cfc6b8; }
.rank-row:nth-child(3) .rank-no { color: #d8863b; }

.rank-name {
  flex: 1;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 8em;
}
.rank-score {
  flex: 1;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.rank-row.world .rank-score { flex: 0 0 auto; }

.rank-tile {
  font-size: 11px;
  color: var(--accent);
  background: var(--grid-bg);
  border: 1px solid var(--line);
  padding: 2px 8px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}

.rank-date {
  font-size: 11px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.rank-empty {
  margin: 0;
  color: var(--text-dim);
  font-size: 12px;
}
