:root {
  color-scheme: dark;
  --bg: #0b0f14;
  --panel: #151c25;
  --line: #223042;
  --text: #e6edf6;
  --muted: #8b9bb0;
  --accent: #4cc2ff;
  --gap: clamp(8px, 2.2vmin, 14px);
  --radius: 10px;
  --move: 110ms;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px 40px;
  background: radial-gradient(circle at 50% -10%, #16324a 0%, var(--bg) 55%);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(92vw, 480px);
}

.head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: clamp(30px, 8vmin, 40px);
  letter-spacing: 1px;
}

.sub {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

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

.score {
  min-width: 72px;
  padding: 6px 10px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.score span {
  display: block;
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--muted);
}

.score strong {
  font-size: 18px;
  font-variant-numeric: tabular-nums;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 16px 0;
}

.hint {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.btn {
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: var(--accent);
  color: #04121c;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.btn:hover {
  filter: brightness(1.08);
}

.btn.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--line);
}

.board {
  position: relative;
  aspect-ratio: 1 / 1;
  padding: var(--gap);
  background: #1b2431;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
}

.cells {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
  height: 100%;
}

.cells div {
  background: rgba(255, 255, 255, 0.045);
  border-radius: var(--radius);
}

.tiles {
  position: absolute;
  inset: var(--gap);
}

/* 位置完全由 --row / --col 决定，滑动靠 transform 过渡 */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  transform: translate(
    calc(var(--col) * (100% + var(--gap))),
    calc(var(--row) * (100% + var(--gap)))
  );
  transition: transform var(--move) ease-in-out;
  will-change: transform;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: clamp(20px, 6.2vmin, 40px);
  background: #eee4da;
  color: #776e65;
}

.tile[data-value="2"] .tile-inner { background: #eee4da; color: #776e65; }
.tile[data-value="4"] .tile-inner { background: #ede0c8; color: #776e65; }
.tile[data-value="8"] .tile-inner { background: #f2b179; color: #fdf6ef; }
.tile[data-value="16"] .tile-inner { background: #f59563; color: #fdf6ef; }
.tile[data-value="32"] .tile-inner { background: #f67c5f; color: #fdf6ef; }
.tile[data-value="64"] .tile-inner { background: #f65e3b; color: #fdf6ef; }
.tile[data-value="128"] .tile-inner { background: #edcf72; color: #fdf6ef; }
.tile[data-value="256"] .tile-inner { background: #edcc61; color: #fdf6ef; }
.tile[data-value="512"] .tile-inner { background: #edc850; color: #fdf6ef; }
.tile[data-value="1024"] .tile-inner { background: #edc53f; color: #fdf6ef; }
.tile[data-value="2048"] .tile-inner {
  background: #edc22e;
  color: #fff;
  box-shadow: 0 0 24px rgba(237, 194, 46, 0.45);
}
.tile[data-value="4096"] .tile-inner,
.tile[data-value="8192"] .tile-inner,
.tile[data-value="16384"] .tile-inner,
.tile[data-value="32768"] .tile-inner,
.tile[data-value="65536"] .tile-inner {
  background: #3c3a32;
  color: #f9f6f2;
}

/* 位数多的数字缩小一点，避免溢出 */
.tile[data-digits="3"] .tile-inner { font-size: clamp(17px, 5.2vmin, 33px); }
.tile[data-digits="4"] .tile-inner { font-size: clamp(14px, 4.2vmin, 26px); }
.tile[data-digits="5"] .tile-inner { font-size: clamp(12px, 3.4vmin, 21px); }

.tile-new .tile-inner {
  animation: appear var(--move) ease-out;
}

@keyframes appear {
  from { transform: scale(0.4); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.tile-pop .tile-inner {
  animation: pop 150ms ease-out;
}

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

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

.overlay[hidden] {
  display: none;
}

.overlay-title {
  margin: 0;
  font-size: clamp(20px, 5vmin, 26px);
  font-weight: 700;
}

.overlay-actions {
  display: flex;
  gap: 10px;
}

.foot {
  margin-top: 16px;
  font-size: 13px;
}

.foot a {
  color: var(--muted);
  text-decoration: none;
}

.foot a:hover {
  color: var(--accent);
}
