:root {
  --bg-page: #faf8ef;
  --bg-board: #bbada0;
  --bg-cell: rgba(238, 228, 218, 0.35);
  --text-main: #776e65;
  --text-light: #f9f6f2;
  --panel-bg: #bbada0;
  --gap: clamp(8px, 1.8vw, 14px);
  --board-size: min(90vw, 520px);
  --cell-size: calc((var(--board-size) - (5 * var(--gap))) / 4);
  --radius: 10px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg-page);
  color: var(--text-main);
  display: grid;
  place-items: center;
  padding: 18px;
}

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

.top-bar {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: flex-start;
  margin-bottom: 10px;
}

.title {
  margin: 0;
  font-size: clamp(2.2rem, 7vw, 4.3rem);
  line-height: 1;
  color: #776e65;
}

.subtitle {
  margin: 8px 0 0;
  font-size: 0.96rem;
}

.controls {
  display: grid;
  justify-items: end;
  gap: 8px;
}

.score-panels {
  display: flex;
  gap: 8px;
}

.score-box {
  background: var(--panel-bg);
  border-radius: 7px;
  min-width: 82px;
  text-align: center;
  padding: 8px 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.score-label {
  display: block;
  color: #eee4da;
  text-transform: uppercase;
  font-size: 0.68rem;
  letter-spacing: 0.04em;
}

.score-box strong {
  color: var(--text-light);
  font-size: 1.45rem;
  line-height: 1.1;
}

.restart-button {
  border: 0;
  background: #8f7a66;
  color: var(--text-light);
  border-radius: 7px;
  padding: 0.65rem 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.18s ease, transform 0.18s ease;
}

.restart-button:hover,
.restart-button:focus-visible {
  filter: brightness(1.06);
}

.restart-button:active {
  transform: translateY(1px);
}

.restart-button:focus-visible,
.overlay-button:focus-visible {
  outline: 3px solid #f6c343;
  outline-offset: 2px;
}

.instructions {
  margin: 0 0 14px;
  font-size: 0.95rem;
}

.board-area {
  display: grid;
  justify-items: center;
}

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  background: var(--bg-board);
  border-radius: var(--radius);
  padding: var(--gap);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 8px 25px rgba(119, 110, 101, 0.16);
}

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

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

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

.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.3rem, 6vw, 2.6rem);
  transition: top 120ms ease-in-out, left 120ms ease-in-out, transform 120ms ease-in-out;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.08);
}

.tile.new {
  animation: pop-in 160ms ease;
}

.tile.merged {
  animation: merge-pop 200ms ease;
}

@keyframes pop-in {
  0% {
    transform: scale(0);
  }
  75% {
    transform: scale(1.06);
  }
  100% {
    transform: scale(1);
  }
}

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

.overlay {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 14px;
  background: rgba(238, 228, 218, 0.78);
  text-align: center;
  z-index: 10;
}

.overlay.hidden,
.overlay-button.hidden {
  display: none;
}

.overlay-message {
  margin: 0;
  font-size: clamp(2rem, 7vw, 3.3rem);
  font-weight: 800;
  color: #776e65;
}

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

.overlay-button {
  border: 0;
  border-radius: 7px;
  background: #8f7a66;
  color: var(--text-light);
  padding: 0.66rem 1rem;
  font-weight: 700;
  cursor: pointer;
}

.tile-2 {
  background: #eee4da;
  color: #776e65;
}

.tile-4 {
  background: #ede0c8;
  color: #776e65;
}

.tile-8 {
  background: #f2b179;
  color: var(--text-light);
}

.tile-16 {
  background: #f59563;
  color: var(--text-light);
}

.tile-32 {
  background: #f67c5f;
  color: var(--text-light);
}

.tile-64 {
  background: #f65e3b;
  color: var(--text-light);
}

.tile-128 {
  background: #edcf72;
  color: var(--text-light);
  font-size: clamp(1.1rem, 5vw, 2.1rem);
}

.tile-256 {
  background: #edcc61;
  color: var(--text-light);
  font-size: clamp(1.1rem, 5vw, 2.1rem);
}

.tile-512 {
  background: #edc850;
  color: var(--text-light);
  font-size: clamp(1.1rem, 5vw, 2.1rem);
}

.tile-1024 {
  background: #edc53f;
  color: var(--text-light);
  font-size: clamp(0.95rem, 4.2vw, 1.7rem);
}

.tile-2048 {
  background: #edc22e;
  color: var(--text-light);
  font-size: clamp(0.95rem, 4.2vw, 1.7rem);
}

.tile-super {
  background: #3c3a32;
  color: var(--text-light);
  font-size: clamp(0.95rem, 3.8vw, 1.4rem);
}

@media (max-width: 560px) {
  :root {
    --board-size: min(92vw, 420px);
  }

  .top-bar {
    align-items: stretch;
  }

  .subtitle,
  .instructions {
    font-size: 0.9rem;
  }
}

@media (max-width: 380px) {
  .controls {
    width: 100%;
  }

  .score-box {
    min-width: 74px;
  }

  .restart-button {
    width: 100%;
  }
}
