:root {
  color-scheme: light;
  --ink: #17202a;
  --muted: #5f6b76;
  --paper: #fffdf8;
  --panel: rgba(255, 255, 255, 0.88);
  --line: rgba(27, 43, 55, 0.13);
  --board: #324653;
  --board-gap: #263946;
  --accent: #e45d48;
  --accent-dark: #be3f30;
  --teal: #0f8a83;
  --teal-dark: #0a6d67;
  --shadow: 0 20px 52px rgba(23, 32, 42, 0.18);
  --cell-size: clamp(66px, 18vw, 118px);
  --gap: clamp(8px, 2.2vw, 14px);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background:
    linear-gradient(145deg, rgba(15, 138, 131, 0.16), transparent 32rem),
    linear-gradient(315deg, rgba(228, 93, 72, 0.14), transparent 34rem),
    #eef3f1;
  color: var(--ink);
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  padding: 0 16px;
  background: #24313b;
  color: #ffffff;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition:
    background 150ms ease,
    transform 150ms ease,
    box-shadow 150ms ease;
}

button:hover {
  background: #111a22;
  transform: translateY(-1px);
}

button:focus-visible {
  outline: 3px solid rgba(228, 93, 72, 0.32);
  outline-offset: 3px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.44;
  transform: none;
}

.app {
  width: min(1120px, calc(100vw - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 24px 0;
  display: grid;
  grid-template-columns: minmax(310px, 1fr) 280px;
  gap: 18px;
  align-items: center;
}

.game-area,
.side-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.game-area {
  padding: clamp(14px, 2.4vw, 24px);
}

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

.title-block,
.stat,
.progress,
.mini-stats > div {
  min-width: 0;
}

.eyebrow,
h1,
.stat span,
.progress span,
.mini-stats span {
  margin: 0;
}

.eyebrow,
.stat span,
.progress span,
.mini-stats span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

h1 {
  font-size: clamp(3rem, 10vw, 5.7rem);
  line-height: 0.88;
  letter-spacing: 0;
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(82px, 1fr));
  gap: 10px;
}

.stat {
  min-height: 68px;
  border-radius: 8px;
  padding: 10px 12px;
  background: #e6ece8;
  display: grid;
  align-content: center;
  text-align: center;
}

.stat strong {
  font-size: clamp(1.35rem, 3.8vw, 2rem);
  line-height: 1;
}

.board-shell {
  position: relative;
  width: min(calc(var(--cell-size) * 4 + var(--gap) * 5), 100%);
  margin: 0 auto;
  touch-action: none;
}

.board {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  padding: var(--gap);
  background: var(--board);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.08),
    0 16px 34px rgba(23, 32, 42, 0.2);
}

.cell {
  border-radius: 8px;
  background: var(--board-gap);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.tile {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: #fffdf8;
  font-size: clamp(1.55rem, 7.2vw, 3.1rem);
  font-weight: 900;
  line-height: 1;
  box-shadow:
    inset 0 -4px 0 rgba(0, 0, 0, 0.08),
    0 8px 16px rgba(16, 25, 33, 0.18);
  transform-origin: center;
  transition:
    transform 120ms ease,
    filter 120ms ease;
}

.tile[data-value="2"],
.tile[data-value="4"] {
  color: #17202a;
}

.tile[data-value="2"] {
  background: #dbe8e4;
}

.tile[data-value="4"] {
  background: #f4db9f;
}

.tile[data-value="8"] {
  background: #f2a35d;
}

.tile[data-value="16"] {
  background: #e45d48;
}

.tile[data-value="32"] {
  background: #cf4065;
}

.tile[data-value="64"] {
  background: #9550a3;
}

.tile[data-value="128"] {
  background: #4f65b5;
  font-size: clamp(1.35rem, 6.4vw, 2.7rem);
}

.tile[data-value="256"] {
  background: #0f8a83;
  font-size: clamp(1.35rem, 6.4vw, 2.7rem);
}

.tile[data-value="512"] {
  background: #13714c;
  font-size: clamp(1.35rem, 6.4vw, 2.7rem);
}

.tile[data-value="1024"] {
  background: #2f4150;
  font-size: clamp(1.05rem, 5.3vw, 2.2rem);
}

.tile[data-value="2048"] {
  background: linear-gradient(135deg, #f7d85a, #e45d48);
  color: #17202a;
  font-size: clamp(1.05rem, 5.3vw, 2.2rem);
}

.tile[data-value="4096"],
.tile[data-value="8192"],
.tile[data-value="16384"] {
  background: #111a22;
  font-size: clamp(0.9rem, 4.8vw, 1.95rem);
}

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

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

.message {
  position: absolute;
  inset: 0;
  border-radius: 8px;
  background: rgba(238, 243, 241, 0.82);
  backdrop-filter: blur(10px);
  display: grid;
  place-items: center;
  padding: 22px;
  text-align: center;
}

.message[hidden] {
  display: none;
}

.message > div {
  display: grid;
  gap: 12px;
  justify-items: center;
}

.message strong {
  font-size: clamp(2rem, 8vw, 3.2rem);
  line-height: 1;
}

.message span {
  color: var(--muted);
  font-weight: 800;
}

.controls {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr;
  gap: 10px;
  margin-top: 16px;
}

.primary,
.message button {
  background: var(--accent);
}

.primary:hover,
.message button:hover {
  background: var(--accent-dark);
}

.side-panel {
  align-self: stretch;
  min-height: 420px;
  padding: 18px;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 14px;
}

.progress {
  min-height: 126px;
  border-radius: 8px;
  padding: 18px;
  background:
    linear-gradient(135deg, rgba(15, 138, 131, 0.14), transparent),
    #ffffff;
  border: 1px solid var(--line);
  display: grid;
  align-content: center;
  gap: 8px;
}

.progress strong {
  font-size: clamp(2.6rem, 8vw, 4rem);
  line-height: 0.95;
}

.mini-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.mini-stats > div {
  min-height: 86px;
  border-radius: 8px;
  background: #ffffff;
  border: 1px solid var(--line);
  padding: 14px;
  display: grid;
  align-content: center;
  gap: 4px;
}

.mini-stats strong {
  font-size: 2rem;
  line-height: 1;
}

.legend {
  align-self: end;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.legend span {
  min-height: 52px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: #fffdf8;
  font-weight: 900;
}

.legend [data-value="2"] {
  background: #dbe8e4;
  color: #17202a;
}

.legend [data-value="4"] {
  background: #f4db9f;
  color: #17202a;
}

.legend [data-value="8"] {
  background: #f2a35d;
}

.legend [data-value="16"] {
  background: #e45d48;
}

.legend [data-value="32"] {
  background: #cf4065;
}

.legend [data-value="64"] {
  background: #9550a3;
}

@keyframes appear {
  from {
    transform: scale(0.62);
    filter: brightness(1.2);
  }
  to {
    transform: scale(1);
    filter: brightness(1);
  }
}

@keyframes merge {
  0% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.14);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 860px) {
  .app {
    min-height: auto;
    grid-template-columns: 1fr;
    align-items: start;
  }

  .side-panel {
    min-height: auto;
    grid-template-rows: auto auto auto;
  }

  .legend {
    align-self: stretch;
  }
}

@media (max-width: 560px) {
  body {
    background:
      linear-gradient(145deg, rgba(15, 138, 131, 0.16), transparent 22rem),
      #eef3f1;
  }

  .app {
    width: min(100vw - 20px, 480px);
    padding: 10px 0 14px;
    gap: 10px;
  }

  .game-area,
  .side-panel {
    box-shadow: 0 12px 28px rgba(23, 32, 42, 0.14);
  }

  .topbar {
    align-items: end;
    gap: 10px;
  }

  .stats {
    grid-template-columns: repeat(2, minmax(70px, 1fr));
  }

  .stat {
    min-height: 58px;
    padding: 8px;
  }

  .controls {
    grid-template-columns: 1fr 1fr;
  }

  #keepPlayingButton {
    grid-column: 1 / -1;
  }

  .side-panel {
    padding: 14px;
  }

  .progress {
    min-height: 92px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    scroll-behavior: auto !important;
    transition-duration: 1ms !important;
  }
}
