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

:root {
  --gap: 12px;
  --board-size: min(500px, calc(100vw - 32px));
  --cell: calc((var(--board-size) - var(--gap) * 5) / 4);
  --r: 6px;
  --bg: #faf8ef;
  --text: #776e65;
  --board: #bbada0;
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  display: flex; justify-content: center; align-items: center;
  background: var(--bg); color: var(--text);
  -webkit-user-select: none; user-select: none;
  touch-action: none;
}

.container { display: flex; flex-direction: column; width: var(--board-size); gap: 10px; }
.header { display: flex; justify-content: space-between; align-items: center; }
h1 { font-size: clamp(40px, 8vw, 56px); font-weight: 800; letter-spacing: -2px; }
.scores { display: flex; gap: 6px; }

.score-box {
  position: relative;
  background: var(--board);
  color: rgba(238,228,218,0.6);
  border-radius: var(--r);
  padding: 6px 16px;
  text-align: center;
  min-width: 65px;
}
.score-box .label { font-size: 10px; text-transform: uppercase; font-weight: 700; letter-spacing: 0.5px; }
.score-box .value { font-size: 20px; font-weight: 800; color: #fff; line-height: 1.3; }

.score-add {
  position: absolute; left: 50%; top: -8px;
  transform: translateX(-50%);
  font-size: 14px; font-weight: 800; color: var(--text);
  animation: float-up 0.7s ease forwards;
  pointer-events: none; white-space: nowrap; z-index: 200;
}
@keyframes float-up {
  0%   { opacity: 1; top: -8px; }
  100% { opacity: 0; top: -40px; }
}

.controls-row { display: flex; justify-content: space-between; align-items: center; }
.controls-row p { font-size: 14px; }
.controls { display: flex; gap: 6px; }

.btn {
  padding: 8px 16px; font-size: 13px; font-weight: 700;
  background: #8f7a66; color: #f9f6f2;
  border: none; border-radius: var(--r); cursor: pointer;
  transition: background 0.15s;
}
.btn:hover  { background: #9f8a76; }
.btn:active { transform: scale(0.96); }

.board {
  position: relative;
  width: var(--board-size); height: var(--board-size);
  background: var(--board);
  border-radius: calc(var(--r) + 4px);
  padding: var(--gap);
  box-shadow: 0 6px 20px rgba(0,0,0,0.08);
}

.grid-bg {
  display: grid;
  grid-template: repeat(4, var(--cell)) / repeat(4, var(--cell));
  gap: var(--gap);
}
.cell { background: rgba(238,228,218,0.35); border-radius: var(--r); }
.tile-container { position: absolute; top: var(--gap); left: var(--gap); z-index: 2; }

.tile {
  position: absolute;
  width: var(--cell); height: var(--cell);
  border-radius: var(--r);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.4);
  transition: transform 0.12s ease-in-out;
  z-index: 10;
}

.no-transition .tile { transition: none !important; }

.tile-new    { animation: pop-in    0.12s ease; }
.tile-merged { animation: pop-merge 0.18s ease; z-index: 20; }

@keyframes pop-in {
  from { scale: 0; opacity: 0; }
  to   { scale: 1; opacity: 1; }
}
@keyframes pop-merge {
  0%, 100% { scale: 1; }
  50%      { scale: 1.15; }
}

.tile                                            { color: #f9f6f2; }
.tile-2, .tile-4                                 { color: var(--text); }
.tile-128, .tile-256, .tile-512                  { font-size: calc(var(--cell) * 0.33); }
.tile-1024, .tile-2048, .tile-4096, .tile-8192   { font-size: calc(var(--cell) * 0.28); }
.tile-super                                      { font-size: calc(var(--cell) * 0.22); }

.tile-2    { background: #eee4da; }
.tile-4    { background: #ede0c8; }
.tile-8    { background: #f2b179; }
.tile-16   { background: #f59563; }
.tile-32   { background: #f67c5f; }
.tile-64   { background: #f65e3b; }
.tile-128  { background: #edcf72; }
.tile-256  { background: #edcc61; }
.tile-512  { background: #edc850; }
.tile-1024 { background: #edc53f; }
.tile-2048 { background: #edc22e; box-shadow: 0 0 30px 10px rgba(243,215,116,0.35); }
.tile-4096 { background: #3e3933; }
.tile-8192 { background: #2d2926; }
.tile-super { background: #1a1714; }

.game-message {
  display: none;
  position: absolute; inset: 0;
  border-radius: calc(var(--r) + 4px);
  background: rgba(238,228,218,0.73);
  z-index: 100;
  flex-direction: column; align-items: center; justify-content: center;
  gap: 20px;
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade-in 0.35s ease;
}
.game-message.active { display: flex; }
.game-message.win    { background: rgba(237,194,46,0.5); }
.game-message p      { font-size: clamp(30px, 7vw, 48px); font-weight: 800; }
.game-message .actions { display: flex; gap: 8px; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.hint { text-align: center; font-size: 12px; color: #9e948a; }
.keys {
  display: inline-block; background: #eee4da;
  border-radius: 3px; padding: 1px 5px;
  font-size: 11px; font-weight: 600; color: var(--text);
}

@media (max-width: 520px) {
  :root { --gap: 8px; }
  .score-box { padding: 4px 10px; min-width: 55px; }
  .score-box .value { font-size: 16px; }
  .controls-row p { display: none; }
  .hint { display: none; }
}
