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

:root {
  --bg-top: #faf6f0;
  --bg-bottom: #f1e9df;
  --text: #3e352b;
  --muted: #9b8d7b;
  --board-bg: #ddcbb5;
  --empty-cell: rgba(150, 122, 90, 0.14);
  --gap: clamp(8px, 2.6vw, 13px);
  --radius: 12px;
  --board-size: clamp(250px, min(90vw, 58vh), 460px);
  --tile-size: calc((var(--board-size) - 5 * var(--gap)) / 4);
  --move-ms: 110ms;
}

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

body {
  margin: 0;
  font-family: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1100px 620px at 12% -12%, #fffaf3 0%, rgba(255, 250, 243, 0) 62%),
    radial-gradient(900px 520px at 102% 2%, #f6eee1 0%, rgba(246, 238, 225, 0) 58%),
    linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  width: min(100%, 520px);
  margin: 0 auto;
  padding: clamp(14px, 4vw, 26px) clamp(12px, 4vw, 18px) 30px;
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 2.4vw, 14px);
}

/* ---------- Header ---------- */

.header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.brand {
  min-width: 0;
}

.brand h1 {
  margin: 0;
  font-size: clamp(38px, 10.5vw, 52px);
  line-height: 0.95;
  font-weight: 800;
  letter-spacing: -0.055em;
  background: linear-gradient(135deg, #4b4034 10%, #927a5d 95%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tagline {
  margin: 7px 0 0;
  font-size: 12.5px;
  letter-spacing: 0.01em;
  color: var(--muted);
}

.scores {
  display: flex;
  gap: 8px;
}

.score {
  min-width: clamp(64px, 18vw, 82px);
  padding: 6px 10px;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.92);
  text-align: center;
  box-shadow: 0 8px 20px -14px rgba(80, 60, 40, 0.6), inset 0 1px 0 #fff;
}

.score-label {
  display: block;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.16em;
  color: #aa9c88;
}

.score-value {
  display: block;
  margin-top: 1px;
  font-size: clamp(18px, 4.6vw, 22px);
  font-weight: 800;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.score.bump .score-value {
  animation: score-bump 240ms cubic-bezier(0.3, 0.9, 0.4, 1.3);
}

@keyframes score-bump {
  0% { transform: scale(1); }
  42% { transform: scale(1.16); }
  100% { transform: scale(1); }
}

/* ---------- Toolbar ---------- */

.toolbar {
  display: flex;
  gap: 8px;
}

.btn {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid #e3d7c6;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.9);
  color: #5c4e3f;
  font: inherit;
  font-size: 13.5px;
  font-weight: 650;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: 0 5px 14px -10px rgba(90, 70, 50, 0.65);
  transition: transform 0.16s ease, background 0.16s ease, box-shadow 0.16s ease, color 0.16s ease;
}

.btn svg {
  width: 16px;
  height: 16px;
  flex: none;
}

.btn:hover {
  transform: translateY(-1px);
  background: #fff;
  box-shadow: 0 9px 20px -12px rgba(90, 70, 50, 0.6);
}

.btn:active {
  transform: translateY(0) scale(0.98);
}

.btn:focus-visible {
  outline: 2px solid #b08b5a;
  outline-offset: 2px;
}

.btn.primary {
  background: #3e352b;
  border-color: #3e352b;
  color: #fdfaf5;
  box-shadow: 0 8px 18px -12px rgba(45, 34, 22, 0.9);
}

.btn.primary:hover {
  background: #4b4137;
}

.btn[disabled] {
  opacity: 0.38;
  cursor: default;
  transform: none;
  box-shadow: none;
}

.btn[aria-pressed="true"] {
  background: #e9b923;
  border-color: #dfad12;
  color: #3b2f0d;
  box-shadow: 0 8px 18px -12px rgba(190, 145, 20, 0.9);
}

/* ---------- Board ---------- */

.board-wrap {
  display: flex;
  justify-content: center;
}

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  border-radius: 18px;
  background: var(--board-bg);
  box-shadow:
    0 26px 50px -26px rgba(95, 74, 52, 0.6),
    inset 0 1px 0 rgba(255, 255, 255, 0.65),
    inset 0 -2px 0 rgba(0, 0, 0, 0.03);
  touch-action: none;
}

.board.nudge {
  animation: board-nudge 180ms ease;
}

@keyframes board-nudge {
  0%, 100% { transform: translateX(0); }
  30% { transform: translateX(-4px); }
  65% { transform: translateX(4px); }
}

.cells {
  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: calc(var(--radius) - 2px);
  background: var(--empty-cell);
  box-shadow: inset 0 1px 2px rgba(120, 95, 70, 0.07);
}

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

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--tile-size);
  height: var(--tile-size);
  z-index: 1;
  transform: translate(
    calc(var(--col) * (100% + var(--gap))),
    calc(var(--row) * (100% + var(--gap)))
  );
  transition: transform var(--move-ms) cubic-bezier(0.22, 0.68, 0.38, 1);
  will-change: transform;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  font-size: calc(var(--tile-size) * 0.42);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  color: #6d5f51;
  background: #eee4da;
  box-shadow:
    0 6px 14px -9px rgba(90, 68, 45, 0.65),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
  transform-origin: center;
  backface-visibility: hidden;
}

.tile-inner.d3 { font-size: calc(var(--tile-size) * 0.355); }
.tile-inner.d4 { font-size: calc(var(--tile-size) * 0.295); }
.tile-inner.d5 { font-size: calc(var(--tile-size) * 0.235); }

/* Tile palette */
.v2 { background: #f2e9df; color: #6d5f51; }
.v4 { background: #efe4d0; color: #6d5f51; }
.v8 { background: #f3b27a; color: #fff9f2; }
.v16 { background: #f0955f; color: #fff9f2; }
.v32 { background: #f47a5a; color: #fff9f2; }
.v64 { background: #f25c3a; color: #fff9f2; }
.v128 { background: #efd27d; color: #5c4a1e; }
.v256 { background: #edc96a; color: #5c4a1e; }
.v512 { background: #eac452; color: #4d3d14; }
.v1024 { background: #e7bc3f; color: #453611; }

.v2048 {
  background: linear-gradient(135deg, #f8d953, #e9b923);
  color: #3b2d08;
  box-shadow:
    0 0 0 2px rgba(233, 185, 35, 0.35),
    0 12px 26px -12px rgba(205, 150, 20, 0.9),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.v-super {
  background: linear-gradient(135deg, #4c4339, #2e2823);
  color: #f5e7c6;
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.08),
    0 12px 26px -14px rgba(40, 30, 20, 0.95);
}

/* Tile animations */
@keyframes tile-appear {
  from { transform: scale(0.2); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes tile-pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.17); }
  100% { transform: scale(1); }
}

@keyframes tile-restore {
  from { transform: scale(0.78); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes tile-erase {
  from { transform: scale(1); opacity: 1; }
  to { transform: scale(0.35) rotate(4deg); opacity: 0; }
}

.tile.new .tile-inner {
  animation: tile-appear 180ms cubic-bezier(0.2, 0.9, 0.35, 1.15) both;
}

.tile.merged .tile-inner {
  animation: tile-pop 190ms cubic-bezier(0.25, 0.9, 0.35, 1.25) both;
}

.tile.restore .tile-inner {
  animation: tile-restore 220ms cubic-bezier(0.2, 0.9, 0.35, 1.1) both;
}

.tile.erasing .tile-inner {
  animation: tile-erase 150ms ease-in forwards;
}

/* Erase mode */
.board.erase-mode {
  box-shadow:
    0 0 0 3px rgba(233, 185, 35, 0.55),
    0 26px 50px -26px rgba(95, 74, 52, 0.6),
    inset 0 1px 0 rgba(255, 255, 255, 0.65);
}

.board.erase-mode .tile-inner {
  cursor: pointer;
}

.board.erase-mode .tile-inner:hover {
  filter: brightness(1.07);
  outline: 2px dashed rgba(62, 53, 43, 0.55);
  outline-offset: -4px;
}

/* ---------- Overlay ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  border-radius: 18px;
  background: rgba(248, 244, 238, 0.82);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.overlay.show {
  display: flex;
  animation: overlay-in 220ms ease both;
}

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

.overlay-card {
  text-align: center;
  max-width: 300px;
  animation: card-in 300ms cubic-bezier(0.2, 0.9, 0.3, 1.1) both;
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(8px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: clamp(19px, 5.4vw, 24px);
  letter-spacing: -0.02em;
  color: #3d342b;
}

.overlay-card p {
  margin: 0 0 18px;
  font-size: 13px;
  line-height: 1.65;
  color: #8a7c6b;
}

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

.overlay-actions .btn {
  height: 42px;
  font-size: 13.5px;
}

/* ---------- Misc ---------- */

.hint {
  margin: 0;
  min-height: 19px;
  text-align: center;
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--muted);
  transition: color 0.2s ease;
}

.hint.active {
  color: #a87b1a;
  font-weight: 600;
}

.toast {
  position: fixed;
  left: 50%;
  top: max(18px, env(safe-area-inset-top));
  z-index: 20;
  padding: 10px 18px;
  border-radius: 999px;
  background: #3e352b;
  color: #fdfaf5;
  font-size: 13.5px;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 14px 32px -14px rgba(40, 30, 20, 0.8);
  opacity: 0;
  transform: translate(-50%, -14px);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 359px) {
  .tagline {
    display: none;
  }

  .hint {
    font-size: 11.5px;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --move-ms: 60ms;
  }

  .tile.new .tile-inner,
  .tile.merged .tile-inner,
  .tile.restore .tile-inner {
    animation-duration: 100ms;
  }

  .board.nudge {
    animation: none;
  }
}
