:root {
  --page-bg: #f4efe6;
  --text: #4b443d;
  --muted: #766d64;
  --board: #9a8f84;
  --cell: rgba(255, 255, 255, 0.24);
  --accent: #e17442;
  --gap: 12px;
  --board-size: min(88vw, 500px);
  --cell-size: calc((var(--board-size) - var(--gap) * 5) / 4);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--page-bg);
}

body {
  min-width: 280px;
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 20% 10%, rgba(255, 255, 255, 0.85), transparent 34rem),
    var(--page-bg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  font: inherit;
}

.game-shell {
  width: min(92vw, 620px);
  margin: 0 auto;
  padding: clamp(24px, 5vw, 48px) 0 32px;
}

.game-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
}

h1 {
  margin: 0;
  color: #504840;
  font-size: clamp(3.5rem, 13vw, 5.5rem);
  font-weight: 850;
  letter-spacing: -0.08em;
  line-height: 0.85;
}

.subtitle {
  max-width: 320px;
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.45;
}

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

.score-box {
  min-width: 82px;
  padding: 9px 12px 10px;
  color: white;
  text-align: center;
  background: #80766d;
  border-radius: 8px;
  box-shadow: 0 2px 0 rgba(70, 58, 48, 0.12);
}

.score-box span {
  display: block;
  margin-bottom: 2px;
  color: #e9e1da;
  font-size: 0.7rem;
  font-weight: 750;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.score-box strong {
  font-size: 1.45rem;
  line-height: 1;
}

.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 24px 0 14px;
}

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

.button {
  min-height: 42px;
  padding: 10px 18px;
  color: white;
  font-weight: 750;
  background: #665b52;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(52, 42, 35, 0.2);
  transition: background-color 120ms ease, transform 120ms ease, box-shadow 120ms ease;
}

.button:hover {
  background: #554b43;
}

.button:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(52, 42, 35, 0.2);
}

.button:focus-visible,
.game-board:focus-visible {
  outline: 3px solid rgba(225, 116, 66, 0.5);
  outline-offset: 3px;
}

.button-secondary {
  color: #554b43;
  background: #eee5db;
}

.button-secondary:hover {
  background: #dfd4c8;
}

.game-board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  margin: 0 auto;
  overflow: hidden;
  touch-action: none;
  background: var(--board);
  border-radius: 14px;
  box-shadow: 0 14px 35px rgba(64, 51, 41, 0.15);
  user-select: none;
  -webkit-user-select: none;
}

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

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

.tile-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.tile {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  display: grid;
  place-items: center;
  width: var(--cell-size);
  height: var(--cell-size);
  color: #554b43;
  font-size: clamp(1.45rem, 7vw, 2.65rem);
  font-weight: 850;
  letter-spacing: -0.04em;
  background: #eee4d9;
  border-radius: 9px;
  box-shadow: 0 3px 8px rgba(64, 51, 41, 0.12);
  transform: translate(
    calc(var(--col) * (var(--cell-size) + var(--gap))),
    calc(var(--row) * (var(--cell-size) + var(--gap)))
  );
  transition: transform 135ms ease-in-out;
  will-change: transform;
}

.tile-new {
  animation: tile-appear 180ms ease-out;
}

.tile-merged {
  animation: tile-merge 210ms ease-out;
}

.tile[data-value="4"] { background: #eadbc8; }
.tile[data-value="8"] { color: #fffaf5; background: #e9a15f; }
.tile[data-value="16"] { color: #fffaf5; background: #e78950; }
.tile[data-value="32"] { color: #fffaf5; background: #df6b47; }
.tile[data-value="64"] { color: #fffaf5; background: #d95037; }
.tile[data-value="128"] { color: #fffaf5; background: #d7ad4b; font-size: clamp(1.25rem, 6vw, 2.25rem); }
.tile[data-value="256"] { color: #fffaf5; background: #c99b3b; font-size: clamp(1.25rem, 6vw, 2.25rem); }
.tile[data-value="512"] { color: #fffaf5; background: #b9892d; font-size: clamp(1.25rem, 6vw, 2.25rem); }
.tile[data-value="1024"] { color: #fffaf5; background: #a97825; font-size: clamp(1rem, 5vw, 1.9rem); }
.tile[data-value="2048"] { color: white; background: #8c651e; font-size: clamp(1rem, 5vw, 1.9rem); }
.tile-super { color: white; background: #4f4943; font-size: clamp(0.9rem, 4.5vw, 1.65rem); }

.game-message {
  position: absolute;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(244, 239, 230, 0.84);
  animation: message-in 180ms ease-out;
}

.game-message[hidden] {
  display: none;
}

.message-card {
  width: min(100%, 360px);
  padding: 28px;
  text-align: center;
  background: rgba(255, 255, 255, 0.94);
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(64, 51, 41, 0.2);
}

.message-card h2 {
  margin: 0 0 8px;
  font-size: clamp(2rem, 8vw, 3rem);
  letter-spacing: -0.04em;
}

.message-card p {
  margin: 0 0 22px;
  color: var(--muted);
  line-height: 1.45;
}

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

.hint {
  max-width: 470px;
  margin: 18px auto 0;
  text-align: center;
  line-height: 1.45;
}

@keyframes tile-appear {
  0% { opacity: 0; scale: 0.45; }
  100% { opacity: 1; scale: 1; }
}

@keyframes tile-merge {
  0%, 100% { scale: 1; }
  50% { scale: 1.14; }
}

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

@media (max-width: 560px) {
  :root {
    --gap: 9px;
    --board-size: min(94vw, 460px);
  }

  .game-shell {
    width: 94vw;
    padding-top: 22px;
  }

  .game-header {
    align-items: center;
  }

  .subtitle {
    display: none;
  }

  .score-box {
    min-width: 68px;
    padding-inline: 8px;
  }

  .score-box strong {
    font-size: 1.2rem;
  }

  .controls {
    margin-top: 18px;
  }

  .controls p {
    font-size: 0.78rem;
  }

  .button {
    min-height: 40px;
    padding: 9px 14px;
  }

  .game-board,
  .grid-cell,
  .tile {
    border-radius: 8px;
  }
}

@media (max-width: 380px) {
  .controls p {
    max-width: 120px;
  }

  .score-panel {
    gap: 5px;
  }

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

@media (prefers-reduced-motion: reduce) {
  .tile,
  .button {
    transition: none;
  }

  .tile-new,
  .tile-merged,
  .game-message {
    animation: none;
  }
}
