:root {
  --bg: #f4efe6;
  --ink: #3d2b1f;
  --muted: #8a7464;
  --panel: #bbada0;
  --empty: #cdc1b4;
  --card: #efe6d9;
  --accent: #8f7a66;
  --win: #edc22e;
  --shadow: 0 10px 30px rgba(61, 43, 31, 0.12);
  --radius: 12px;
  font-family: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
}

body {
  background:
    radial-gradient(1200px 500px at 10% -10%, #fff8ec 0%, transparent 55%),
    radial-gradient(900px 400px at 100% 0%, #f3d9b4 0%, transparent 50%),
    var(--bg);
  color: var(--ink);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100%;
  padding: 28px 16px 40px;
  touch-action: manipulation;
  user-select: none;
}

.app {
  width: min(480px, 100%);
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 16px;
}

.brand h1 {
  font-size: 48px;
  line-height: 1;
  letter-spacing: -1px;
}

.brand p {
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}

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

.score-box {
  position: relative;
  min-width: 78px;
  background: var(--panel);
  color: #fff;
  border-radius: 8px;
  padding: 8px 12px 10px;
  text-align: center;
  overflow: hidden;
}

.score-box .label {
  display: block;
  font-size: 11px;
  letter-spacing: 1px;
  opacity: 0.85;
  text-transform: uppercase;
}

.score-box span:last-child,
.score-box #score,
.score-box #best {
  font-size: 22px;
  font-weight: 700;
}

.score-add {
  position: absolute;
  left: 50%;
  top: 8px;
  transform: translateX(-50%);
  color: #fff;
  font-weight: 700;
  animation: float-score 700ms ease-out forwards;
  pointer-events: none;
}

.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

button {
  appearance: none;
  border: 0;
  background: var(--accent);
  color: #f9f6f2;
  font: inherit;
  font-weight: 700;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
}

button:hover {
  filter: brightness(1.05);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
  filter: none;
}

.board-wrap {
  position: relative;
  background: var(--panel);
  border-radius: 14px;
  padding: 12px;
  box-shadow: var(--shadow);
  aspect-ratio: 1 / 1;
}

.grid,
.tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 12px;
  width: 100%;
  height: 100%;
}

.tiles {
  position: absolute;
  inset: 12px;
  pointer-events: none;
}

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

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 800;
  color: #776e65;
  transition: transform 90ms ease-out;
  animation: pop-in 140ms ease-out;
}

.tile.merge {
  animation: pop-merge 160ms ease-out;
}

.tile.v2 { background: #eee4da; font-size: 42px; }
.tile.v4 { background: #ede0c8; font-size: 42px; }
.tile.v8 { background: #f2b179; color: #f9f6f2; font-size: 42px; }
.tile.v16 { background: #f59563; color: #f9f6f2; font-size: 38px; }
.tile.v32 { background: #f67c5f; color: #f9f6f2; font-size: 38px; }
.tile.v64 { background: #f65e3b; color: #f9f6f2; font-size: 38px; }
.tile.v128 { background: #edcf72; color: #f9f6f2; font-size: 32px; }
.tile.v256 { background: #edcc61; color: #f9f6f2; font-size: 32px; }
.tile.v512 { background: #edc850; color: #f9f6f2; font-size: 32px; }
.tile.v1024 { background: #edc53f; color: #f9f6f2; font-size: 26px; }
.tile.v2048 { background: #edc22e; color: #f9f6f2; font-size: 26px; }
.tile.v4096,
.tile.v8192,
.tile.v16384,
.tile.v32768,
.tile.v65536 {
  background: #3c3a32;
  color: #f9f6f2;
  font-size: 22px;
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  backdrop-filter: blur(2px);
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  text-align: center;
  padding: 16px;
}

.overlay-card h2 {
  font-size: 36px;
  margin-bottom: 8px;
}

.overlay-card p {
  color: var(--muted);
  margin-bottom: 16px;
}

.overlay-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.hint {
  margin-top: 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

@keyframes pop-in {
  from { transform: scale(0.4); }
  to { transform: scale(1); }
}

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

@keyframes float-score {
  from { opacity: 1; transform: translate(-50%, 0); }
  to { opacity: 0; transform: translate(-50%, -18px); }
}

@media (max-width: 420px) {
  .brand h1 { font-size: 40px; }
  .tile.v2, .tile.v4, .tile.v8 { font-size: 32px; }
  .tile.v16, .tile.v32, .tile.v64 { font-size: 28px; }
  .tile.v128, .tile.v256, .tile.v512 { font-size: 24px; }
  .tile.v1024, .tile.v2048 { font-size: 20px; }
}
