* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

:root {
  --bg: #faf8ef;
  --board-bg: #bbada0;
  --empty-cell: rgba(238, 228, 218, 0.35);
  --dark-text: #776e65;
  --light-text: #f9f6f2;
  --btn: #8f7a66;
  --btn-hover: #9f8b77;
}

body {
  font-family: "Segoe UI", "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--dark-text);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

input {
  touch-action: auto;
  -webkit-user-select: text;
  user-select: text;
}

.container {
  width: 100%;
  max-width: 480px;
  padding: 16px;
  height: 100%;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 12px;
}

h1 {
  font-size: 52px;
  font-weight: 700;
  color: #776e65;
}

.subtitle {
  font-size: 14px;
  color: #a59a8f;
  margin-top: 4px;
}

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

.score-box {
  background: var(--board-bg);
  border-radius: 6px;
  padding: 8px 16px;
  text-align: center;
  min-width: 80px;
}

.score-label {
  display: block;
  font-size: 12px;
  color: #eee4da;
  letter-spacing: 1px;
}

.score-value {
  display: block;
  font-size: 22px;
  font-weight: 700;
  color: #fff;
}

.controls {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

.player-area {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.player-area input {
  flex: 1;
  padding: 10px 14px;
  border: 2px solid var(--board-bg);
  border-radius: 6px;
  font-size: 15px;
  font-weight: 600;
  color: var(--dark-text);
  background: #fff;
  outline: none;
  transition: border-color 0.15s;
}

.player-area input:focus {
  border-color: var(--btn);
}

.player-area input::placeholder {
  color: #c2b9ad;
  font-weight: 400;
}

.player-hint {
  font-size: 12px;
  color: #a59a8f;
  white-space: nowrap;
}

.btn {
  flex: 1;
  padding: 12px 0;
  border: none;
  border-radius: 6px;
  background: var(--btn);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn:hover {
  background: var(--btn-hover);
}

.btn:active {
  transform: scale(0.97);
}

.btn-ghost {
  background: transparent;
  color: var(--btn);
  border: 2px solid var(--btn);
}

.btn-ghost:hover {
  background: rgba(143, 122, 102, 0.1);
}

.board-container {
  position: relative;
}

.board {
  background: var(--board-bg);
  border-radius: 10px;
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.cell {
  aspect-ratio: 1;
  background: var(--empty-cell);
  border-radius: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 40px;
  font-weight: 700;
  transition: background 0.15s;
}

.cell[data-value="2"]   { background: #eee4da; color: #776e65; }
.cell[data-value="4"]   { background: #ede0c8; color: #776e65; }
.cell[data-value="8"]   { background: #f2b179; color: #f9f6f2; }
.cell[data-value="16"]  { background: #f59563; color: #f9f6f2; }
.cell[data-value="32"]  { background: #f67c5f; color: #f9f6f2; }
.cell[data-value="64"]  { background: #f65e3b; color: #f9f6f2; }
.cell[data-value="128"] { background: #edcf72; color: #f9f6f2; font-size: 32px; }
.cell[data-value="256"] { background: #edcc61; color: #f9f6f2; font-size: 32px; }
.cell[data-value="512"] { background: #edc850; color: #f9f6f2; font-size: 32px; }
.cell[data-value="1024"] { background: #edc53f; color: #f9f6f2; font-size: 24px; }
.cell[data-value="2048"] { background: #edc22e; color: #f9f6f2; font-size: 24px; }
.cell[data-value="4096"] { background: #3c3a32; color: #f9f6f2; font-size: 24px; }
.cell[data-value="8192"] { background: #3c3a32; color: #f9f6f2; font-size: 24px; }

.cell.pop {
  animation: pop 0.2s ease;
}

.cell.spawn {
  animation: spawn 0.2s ease;
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

@keyframes spawn {
  0% { transform: scale(0); opacity: 0.4; }
  100% { transform: scale(1); opacity: 1; }
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.73);
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

.overlay.hidden {
  display: none;
}

.overlay-content {
  text-align: center;
  padding: 24px;
}

.overlay-content h2 {
  font-size: 42px;
  color: #776e65;
  margin-bottom: 8px;
}

.overlay-content p {
  font-size: 16px;
  margin-bottom: 20px;
}

.overlay-content .btn {
  flex: none;
  padding: 12px 32px;
}

.leaderboard {
  margin-top: 16px;
  background: #eee4da;
  border-radius: 10px;
  padding: 14px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: auto;
  -webkit-overflow-scrolling: touch;
}

.leaderboard h3 {
  font-size: 18px;
  color: #776e65;
  margin-bottom: 12px;
}

.leaderboard ul {
  list-style: none;
}

.leaderboard li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 15px;
  color: #776e65;
}

.leaderboard li:nth-child(odd) {
  background: rgba(255, 255, 255, 0.5);
}

.leaderboard li.me {
  background: var(--btn);
  color: #fff;
}

.leaderboard .rank {
  width: 24px;
  text-align: center;
  font-weight: 700;
  color: #b7ab9d;
}

.leaderboard li.me .rank {
  color: #fff;
}

.leaderboard .rank.top {
  color: #edc22e;
}

.leaderboard .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard .pts {
  font-weight: 700;
  min-width: 60px;
  text-align: right;
}

.leaderboard .empty {
  text-align: center;
  color: #a59a8f;
  font-size: 14px;
  padding: 12px 0;
}

.hint {
  text-align: center;
  font-size: 13px;
  color: #a59a8f;
  margin-top: 16px;
  line-height: 1.6;
}

@media (max-width: 400px) {
  h1 { font-size: 40px; }
  .cell { font-size: 32px; }
  .cell[data-value="128"] { font-size: 26px; }
  .cell[data-value="256"] { font-size: 26px; }
  .cell[data-value="512"] { font-size: 26px; }
  .cell[data-value="1024"] { font-size: 20px; }
  .cell[data-value="2048"] { font-size: 20px; }
  .cell[data-value="4096"] { font-size: 20px; }
  .cell[data-value="8192"] { font-size: 20px; }
}
