:root {
  color-scheme: light;
  --bg: #f6f1e8;
  --panel: #fffaf3;
  --board: #c8b9a6;
  --tile-empty: rgba(255, 255, 255, 0.32);
  --text: #3e3428;
  --accent: #8f5f34;
  --button: #a86f3d;
  --button-hover: #8d5b30;
  --shadow: 0 16px 40px rgba(62, 52, 40, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.8), transparent 38%),
    linear-gradient(180deg, #faf5ee 0%, var(--bg) 100%);
  color: var(--text);
}

.app {
  width: min(92vw, 520px);
  margin: 0 auto;
  padding: 32px 0 40px;
}

.topbar,
.leaderboard-card,
.modal-card {
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

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

.heading h1,
.leaderboard-header h2,
.modal-card h2 {
  margin: 0;
}

.heading p,
.leaderboard-header span,
.score-card span,
.modal-card p {
  margin: 6px 0 0;
  color: #756657;
  font-size: 14px;
}

.actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.score-card {
  min-width: 104px;
  padding: 10px 14px;
  border-radius: 14px;
  text-align: center;
  background: #f1e6d7;
}

.score-card strong,
.modal-card strong {
  display: block;
  margin-top: 4px;
  font-size: 28px;
}

button {
  border: 0;
  border-radius: 14px;
  background: var(--button);
  color: #fff;
  font-size: 15px;
  padding: 12px 18px;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.2s ease;
}

button:hover {
  background: var(--button-hover);
  transform: translateY(-1px);
}

.board-section {
  margin-top: 18px;
  touch-action: none;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
  padding: 10px;
  border-radius: 20px;
  background: var(--board);
  aspect-ratio: 1 / 1;
  overflow: hidden;
  transition: transform 0.14s ease;
}

.board-moving-left {
  transform: translateX(-5px);
}

.board-moving-right {
  transform: translateX(5px);
}

.board-moving-up {
  transform: translateY(-5px);
}

.board-moving-down {
  transform: translateY(5px);
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  min-height: 0;
  border-radius: 16px;
  background: var(--tile-empty);
  font-size: clamp(24px, 5vw, 34px);
  font-weight: 700;
  line-height: 1;
  user-select: none;
  transform: scale(1);
  transition:
    background-color 0.18s ease,
    color 0.18s ease,
    transform 0.18s ease;
}

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

.tile-empty {
  color: transparent;
}

.tile-2 { background: #f5ede3; }
.tile-4 { background: #f0e2ce; }
.tile-8 { background: #efc28c; color: #fff; }
.tile-16 { background: #eaa86b; color: #fff; }
.tile-32 { background: #e38d59; color: #fff; }
.tile-64 { background: #d9754f; color: #fff; }
.tile-128 { background: #d0a650; color: #fff; font-size: clamp(20px, 4.5vw, 28px); }
.tile-256 { background: #c99645; color: #fff; font-size: clamp(20px, 4.5vw, 28px); }
.tile-512 { background: #bb823c; color: #fff; font-size: clamp(20px, 4.5vw, 28px); }
.tile-1024 { background: #a86f3d; color: #fff; font-size: clamp(18px, 4vw, 24px); }
.tile-2048,
.tile-super { background: #8f5f34; color: #fff; font-size: clamp(18px, 4vw, 24px); }

@keyframes tile-pop {
  0% {
    transform: scale(0.9);
  }
  65% {
    transform: scale(1.06);
  }
  100% {
    transform: scale(1);
  }
}

.leaderboard-card {
  margin-top: 18px;
  padding: 18px 20px;
}

.touch-controls {
  margin-top: 18px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.touch-button {
  min-height: 54px;
  font-size: 24px;
  font-weight: 700;
  padding: 0;
}

.touch-up {
  grid-column: 2;
}

.touch-left {
  grid-column: 1;
  grid-row: 2;
}

.touch-right {
  grid-column: 3;
  grid-row: 2;
}

.touch-down {
  grid-column: 2;
  grid-row: 2;
}

.leaderboard-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.leaderboard-list {
  margin: 16px 0 0;
  padding-left: 20px;
}

.leaderboard-list li {
  padding: 6px 0;
}

.leaderboard-empty {
  margin: 16px 0 0;
  color: #756657;
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(39, 31, 24, 0.42);
}

.modal.hidden {
  display: none;
}

.hidden {
  display: none;
}

.modal-card {
  width: min(90vw, 320px);
  padding: 28px 24px;
  text-align: center;
}

@media (max-width: 560px) {
  .topbar,
  .actions,
  .leaderboard-header {
    flex-direction: column;
    align-items: stretch;
  }

  .actions {
    width: 100%;
  }

  button {
    width: 100%;
  }

  .touch-controls button {
    width: auto;
  }
}
