/* 2048 — styles taken from the validated prototype
 * (~/workspace/prototypes/2048-prototype.html, v0.04) and the portal
 * 2048 page design language. One token set, flat controls, no external
 * fonts/images. The game fills its iframe; no prototype shell here.
 */
:root {
  --card: #faf8ef;
  --board: #bbada0;
  --cell-empty: #cdc1b4;
  --ink: #776e65;
  --ink-soft: #8a7f72;
  --btn-main: #8f7a66;
  --btn-main-hover: #7d6955;
  --pill-border: #e3e3e3;
  --you-bg: #f7e7c3;
  --you-border: #edcf72;
  --overlay-bg: rgba(250, 248, 239, 0.88);
  --shadow: 0 10px 30px rgba(119, 110, 101, 0.14);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--card);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100dvh;
  padding: 16px;
}
button { cursor: pointer; font: inherit; }
.hidden { display: none !important; }

/* ---------- 游戏卡 ---------- */
.game-card {
  position: relative;
  background: var(--card);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 20px;
  width: 100%;
  max-width: 640px;
  container-type: inline-size;
}
.game-card.leaderboard {
  display: flex;
  flex-direction: column;
  /* No fixed min-height: JS pins this card to the play card's measured
     height on open (see syncLeaderboardHeight), so both views match. */
}

/* ---------- 游玩页顶部栏 ---------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}
.scores { display: flex; gap: 8px; }
.pill {
  position: relative;
  background: var(--board);
  border-radius: 8px;
  padding: 6px 16px 8px;
  min-width: 84px;
  text-align: center;
}
.pill-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: #eee4da;
  line-height: 1.3;
}
.pill-num {
  display: block;
  font-size: 24px;
  font-weight: 800;
  color: #fff;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}
.gain {
  position: absolute;
  left: 50%;
  top: 2px;
  transform: translateX(-50%);
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  opacity: 0;
  pointer-events: none;
}
.gain.show { animation: gainFloat 0.7s ease-out; }
@keyframes gainFloat {
  0% { opacity: 0; transform: translate(-50%, 6px); }
  25% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -16px); }
}
.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
  border-radius: 999px;
  padding: 9px 22px;
  min-height: 44px;
  line-height: 1.2;
  transition: background 0.15s ease, transform 0.05s ease;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn-light { background: #fff; color: var(--ink); border: 1px solid var(--pill-border); }
.btn-light:hover { background: #f6f1e7; }
.btn-main { background: var(--btn-main); color: #fff; }
.btn-main:hover { background: var(--btn-main-hover); }

/* ---------- 棋盘（容器与格子都锁定 1:1） ---------- */
.board {
  background: var(--board);
  border-radius: 16px;
  padding: clamp(8px, 2cqw, 12px);
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: clamp(8px, 2cqw, 12px);
  width: 100%;
  aspect-ratio: 1 / 1;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.tile {
  background: var(--cell-empty);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  color: var(--ink);
  font-size: clamp(20px, 9.5cqw, 60px);
  font-variant-numeric: tabular-nums;
  aspect-ratio: 1 / 1;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.tile.d3 { font-size: clamp(17px, 7.8cqw, 48px); }
.tile.d4 { font-size: clamp(14px, 6.4cqw, 40px); }
.tile.v2 { background: #eee4da; }
.tile.v4 { background: #ede0c8; }
.tile.v8 { background: #f2b179; color: #f9f6f2; }
.tile.v16 { background: #f59563; color: #f9f6f2; }
.tile.v32 { background: #f67c5f; color: #f9f6f2; }
.tile.v64 { background: #f65e3b; color: #f9f6f2; }
.tile.v128 { background: #edcf72; color: #f9f6f2; }
.tile.v256 { background: #edcc61; color: #f9f6f2; }
.tile.v512 { background: #edc850; color: #f9f6f2; }
.tile.v1024 { background: #edc53f; color: #f9f6f2; }
.tile.v2048 { background: #edc22e; color: #f9f6f2; }
.tile.vsuper { background: #3c3a32; color: #f9f6f2; }
.tile.spawn { animation: spawn 0.18s ease-out; }
.tile.merge { animation: merge 0.18s ease-in-out; }
@keyframes spawn { 0% { transform: scale(0); } 100% { transform: scale(1); } }
@keyframes merge { 0% { transform: scale(1); } 50% { transform: scale(1.18); } 100% { transform: scale(1); } }

/* ---------- 结束遮罩（唯一一种） ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 5;
  border-radius: 20px;
}
.overlay-box { text-align: center; max-width: 420px; width: 100%; position: relative; z-index: 2; }
.overlay-title { margin: 0 0 6px; font-size: 38px; font-weight: 800; color: var(--ink); letter-spacing: 0.02em; }
.overlay-sub { margin: 0 0 22px; font-size: 15px; color: var(--ink-soft); line-height: 1.6; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 24px; }
.stat { background: #fff; border: 1px solid var(--pill-border); border-radius: 12px; padding: 10px 4px 8px; }
.stat-label { display: block; font-size: 11px; color: #a2937d; letter-spacing: 0.08em; margin-bottom: 2px; }
.stat-num { display: block; font-size: 20px; font-weight: 800; color: var(--ink); font-variant-numeric: tabular-nums; }
.overlay-btns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.overlay-btns .btn { padding: 12px 26px; font-size: 16px; }
.confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; z-index: 1; }
.confetti i {
  position: absolute; top: -14px; width: 9px; height: 14px;
  border-radius: 2px; opacity: 0; animation: fall linear forwards;
}
@keyframes fall {
  0% { opacity: 1; transform: translateY(0) rotate(0deg); }
  90% { opacity: 1; }
  100% { opacity: 0; transform: translateY(560px) rotate(560deg); }
}

/* ---------- 排行榜 ---------- */
.rank-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.back-btn {
  appearance: none; background: #fff; border: 1px solid var(--pill-border);
  color: var(--ink); font-size: 14px; font-weight: 700; border-radius: 999px;
  padding: 8px 18px; min-height: 44px; cursor: pointer;
}
.back-btn:hover { background: #f6f1e7; }
.rank-title { margin: 0; font-size: 24px; font-weight: 800; color: var(--ink); }
.demo-tag {
  margin-left: auto; flex: none; background: #fff; border: 1px solid var(--you-border);
  color: var(--ink); font-size: 12px; font-weight: 700; border-radius: 999px; padding: 4px 10px;
}
.rank-list {
  flex: 1; min-height: 0; overflow-y: auto;
  border: 1px solid #eee7d8; border-radius: 16px; background: #fff;
}
.rank-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid #f3eee1; font-size: 15px; color: var(--ink);
}
.rank-row:last-child { border-bottom: none; }
.rank-no {
  width: 30px; height: 30px; border-radius: 50%; background: #f3eee1; color: var(--ink-soft);
  font-size: 13px; font-weight: 700; display: flex; align-items: center; justify-content: center;
  flex: none; font-variant-numeric: tabular-nums;
}
.rank-row.top .rank-no { color: #fff; }
.rank-row.top .rank-name { font-weight: 800; }
.rank-row.top .rank-score { font-weight: 800; }
.rank-row.r1 .rank-no { background: #edc22e; }
.rank-row.r2 .rank-no { background: #b9b3a8; }
.rank-row.r3 .rank-no { background: #d99a5b; }
.rank-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-score { font-weight: 700; font-variant-numeric: tabular-nums; }
.rank-row.you { background: var(--you-bg); box-shadow: inset 0 0 0 1.5px var(--you-border); }
.rank-row.you .rank-no { background: var(--btn-main); color: #fff; }
.rank-row.you .rank-name { font-weight: 800; }
.rank-row.you .rank-score { font-weight: 800; }
.rank-empty { padding: 24px 16px; text-align: center; color: var(--ink-soft); font-size: 14px; line-height: 1.6; }
.rank-footer {
  flex: none; display: flex; align-items: center; gap: 12px;
  background: var(--you-bg); border: 1.5px solid var(--you-border);
  border-radius: 16px; padding: 12px 18px; margin-top: 12px;
}
@media (max-width: 460px) {
  body { padding: 10px; }
  .game-card { padding: 14px; }
  .pill { min-width: 72px; padding: 5px 10px 7px; }
  .pill-num { font-size: 20px; }
  .btn { padding: 8px 16px; font-size: 14px; }
  .overlay-title { font-size: 30px; }
}
@media (prefers-reduced-motion: reduce) {
  .tile.spawn, .tile.merge, .gain.show { animation: none; }
  .confetti { display: none; }
}
