:root {
  --bg: #eae6de;
  --panel: #f5efe4;
  --text: #23201d;
  --muted: #8f7f70;
  --board: #bbada0;
  --cell: rgba(219, 206, 194, 0.35);
  --tile-light: #eee4da;
  --tile-dark: #eda769;
  --shadow: 0 12px 30px rgba(63, 53, 44, 0.18);
  --gap: clamp(8px, 2vw, 12px);
  --radius: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  font-family: "Space Grotesk", "Avenir Next", "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top right, rgba(255, 255, 255, 0.7), transparent 45%),
    linear-gradient(140deg, #ece5d6 0%, var(--bg) 40%, #ddd1b5 100%);
}

.app {
  width: min(100%, 540px);
  display: grid;
  gap: 14px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
}

.title {
  margin: 0;
  font-size: clamp(2.2rem, 7vw, 3.4rem);
  line-height: 1;
  letter-spacing: -0.04em;
}

.subtitle {
  margin: 8px 0 0;
  color: var(--muted);
}

.score-panel {
  display: flex;
  gap: 10px;
}

.score-card {
  min-width: 86px;
  padding: 9px 12px;
  border-radius: 12px;
  background: var(--panel);
  box-shadow: var(--shadow);
  text-align: center;
}

.score-card span {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.score-card strong {
  display: block;
  margin-top: 4px;
  font-size: 1.4rem;
}

.controls {
  padding: 12px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.7);
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.controls p {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.btn {
  border: 0;
  padding: 10px 14px;
  border-radius: 10px;
  font: inherit;
  font-weight: 700;
  color: #fff;
  background: #9c7860;
  cursor: pointer;
  transition: transform 0.15s ease, filter 0.15s ease;
}

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

.btn:active {
  transform: scale(0.97);
}

.board-wrapper {
  position: relative;
}

.board {
  position: relative;
  width: min(100%, 540px);
  aspect-ratio: 1 / 1;
  padding: var(--gap);
  border-radius: var(--radius);
  background: var(--board);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.board-bg,
.tile-layer {
  position: absolute;
  inset: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
}

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

.tile {
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: clamp(1.2rem, 4.4vw, 2rem);
  color: #695c4e;
  transition: transform 140ms ease;
  box-shadow: inset 0 -2px 2px rgba(0, 0, 0, 0.04);
}

.tile.tile-new {
  animation: pop-in 0.18s ease;
}

.tile.tile-merged {
  animation: merge-pop 0.2s ease;
}

.tile-2 { background: #eee4da; }
.tile-4 { background: #ede0c8; }
.tile-8 { background: #f2b179; color: #fff7eb; }
.tile-16 { background: #f59563; color: #fff7eb; }
.tile-32 { background: #f67c5f; color: #fff7eb; }
.tile-64 { background: #f65e3b; color: #fff7eb; }
.tile-128 { background: #edcf72; color: #fff7eb; font-size: clamp(1rem, 4vw, 1.7rem); }
.tile-256 { background: #edcc61; color: #fff7eb; font-size: clamp(1rem, 4vw, 1.7rem); }
.tile-512 { background: #edc850; color: #fff7eb; font-size: clamp(1rem, 4vw, 1.7rem); }
.tile-1024 { background: #edc53f; color: #fff7eb; font-size: clamp(0.95rem, 3.8vw, 1.5rem); }
.tile-2048,
.tile-super {
  background: #edc22e;
  color: #fff7eb;
  font-size: clamp(0.85rem, 3.5vw, 1.3rem);
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  text-align: center;
  background: rgba(238, 228, 218, 0.82);
}

.overlay h2 {
  margin: 0;
  font-size: clamp(1.6rem, 4vw, 2.2rem);
}

.overlay p {
  margin: 0 0 8px;
}

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

.btn-secondary {
  background: #b39988;
}

.hidden {
  display: none;
}

.board.board-shake {
  animation: shake 0.18s linear;
}

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

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

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

@media (max-width: 580px) {
  .controls {
    flex-direction: column;
    align-items: flex-start;
  }

  .score-card {
    min-width: 75px;
  }
}
