/* 2048 网页版 - 移动优先样式（spec3 §三）
   双主题由 html[data-theme] 切换；布局参考桌面版玻璃卡片 + 胶囊风格。 */

@font-face {
  font-family: "MiSans";
  src: url("../fonts/MiSans.ttf") format("truetype");
  font-weight: 100 900;
  font-display: swap;
}

:root {
  --font: "MiSans", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  --glow-c1: #32409c;
  --glow-c2: #1785a8;
  --glow-c3: #6a4bbf;
  --glow-c4: #4b3a86;
  --glow-opacity: 0.5;
}

html[data-theme="dark"] {
  --bg-top: #141923;
  --bg-bottom: #20263a;
  --card-bg: rgba(255, 255, 255, 0.06);
  --card-border: rgba(255, 255, 255, 0.12);
  --text-main: #e6e9f2;
  --text-dim: #98a0b3;
  --accent-a: #22d3ee;
  --accent-b: #7c6cf0;
  --on-accent: #ffffff;
  --cell-bg: rgba(255, 255, 255, 0.09);
  --cell-border: rgba(255, 255, 255, 0.16);
  --overlay-bg: rgba(20, 25, 35, 0.82);
  --tile-shadow: rgba(0, 0, 0, 0.38);
  --tile-hi: rgba(255, 255, 255, 0.16);
  --card-shadow: rgba(0, 0, 0, 0.32);
  --tile-2: linear-gradient(to bottom right, #333848, #3d4356);
  --tile-4: linear-gradient(to bottom right, #3f465c, #4b536b);
  --tile-8: linear-gradient(to bottom right, #a06a35, #b97f3d);
  --tile-16: linear-gradient(to bottom right, #a05f34, #c06e3c);
  --tile-32: linear-gradient(to bottom right, #a05533, #c05a36);
  --tile-64: linear-gradient(to bottom right, #a04a2e, #bf4f2d);
  --tile-128: linear-gradient(to bottom right, #a88a2e, #c2a81f);
  --tile-256: linear-gradient(to bottom right, #b89a20, #d2b51a);
  --tile-512: linear-gradient(to bottom right, #c8a914, #e2c20f);
  --tile-1024: linear-gradient(to bottom right, #d8b80c, #f2cf0a);
  --tile-2048: linear-gradient(to bottom right, #f5cf3f, #ffdb3d);
  --tile-super: linear-gradient(to bottom right, #eef0f5, #d8dce8);
  --tile-2048-fg: #141923;
  --max-glow: rgba(255, 193, 77, 0.85);
  --spawn-glow: rgba(103, 232, 249, 0.65);
}

html[data-theme="light"] {
  --bg-top: #f7f8fc;
  --bg-bottom: #e9edf6;
  --card-bg: rgba(255, 255, 255, 0.62);
  --card-border: rgba(255, 255, 255, 0.9);
  --text-main: #4a4f5c;
  --text-dim: #9aa0ab;
  --accent-a: #ff9f43;
  --accent-b: #ff6b6b;
  --on-accent: #ffffff;
  --cell-bg: rgba(205, 193, 180, 0.46);
  --cell-border: rgba(120, 100, 60, 0.14);
  --overlay-bg: rgba(247, 248, 252, 0.82);
  --tile-shadow: rgba(140, 105, 55, 0.30);
  --tile-hi: rgba(255, 255, 255, 0.60);
  --card-shadow: rgba(90, 70, 40, 0.18);
  --glow-c1: #ffc98f;
  --glow-c2: #a7c8ff;
  --glow-c3: #ffb6d9;
  --glow-c4: #b8e6c9;
  --glow-opacity: 0.38;
  --tile-2: linear-gradient(to bottom right, #fbf7ec, #f0e7d5);
  --tile-4: linear-gradient(to bottom right, #f9f0dc, #eadcb9);
  --tile-8: linear-gradient(to bottom right, #ffd9b8, #f9a86b);
  --tile-16: linear-gradient(to bottom right, #ffc09a, #f98a55);
  --tile-32: linear-gradient(to bottom right, #ffab8a, #f97145);
  --tile-64: linear-gradient(to bottom right, #ff9673, #f6543a);
  --tile-128: linear-gradient(to bottom right, #ffe9a8, #f6c94e);
  --tile-256: linear-gradient(to bottom right, #ffe59a, #f2bc3a);
  --tile-512: linear-gradient(to bottom right, #ffe18a, #eda72a);
  --tile-1024: linear-gradient(to bottom right, #ffd97d, #e8941e);
  --tile-2048: linear-gradient(to bottom right, #ffdf8a, #f7c522);
  --tile-super: linear-gradient(to bottom right, #454a56, #2a2e38);
  --tile-2048-fg: #ffffff;
  --max-glow: rgba(255, 210, 122, 0.8);
  --spawn-glow: rgba(255, 159, 67, 0.55);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  background: linear-gradient(to bottom, var(--bg-top), var(--bg-bottom));
  color: var(--text-main);
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 12px;
  touch-action: manipulation;
}

.hidden { display: none !important; }

.screen { width: 100%; max-width: 460px; }

/* ============ 通用按钮 ============ */
.btn {
  border: none;
  border-radius: 999px;
  font-family: var(--font);
  font-weight: bold;
  font-size: 15px;
  padding: 12px 26px;
  cursor: pointer;
  color: var(--text-main);
  background: var(--card-bg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
  transition: transform 0.08s ease, box-shadow 0.2s ease;
}
.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: 0.45; }
.btn.primary {
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  color: var(--on-accent);
  box-shadow: 0 8px 20px rgba(124, 108, 240, 0.35);
}
.btn.big {
  font-size: 17px;
  padding: 14px 52px;
  min-width: 240px;
}
.btn.ghost {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
}

.icon-btn {
  border: none;
  border-radius: 999px;
  width: 38px;
  height: 38px;
  font-size: 20px;
  background: var(--card-bg);
  color: var(--text-main);
  cursor: pointer;
}

/* ============ 主菜单 ============ */
.menu-header { text-align: center; margin: 18px 0 8px; }
.menu-title {
  margin: 0;
  font-size: 56px;
  font-weight: bold;
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 4px 12px rgba(124, 108, 240, 0.35));
}
.menu-subtitle { color: var(--text-dim); font-size: 14px; margin: 6px 0 0; }

.menu-body { display: flex; flex-direction: column; align-items: center; gap: 16px; margin-top: 14px; }

.best-card {
  width: 100%;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  box-shadow: 0 6px 16px -8px var(--card-shadow), inset 0 1px 0 var(--tile-hi);
}
.best-caption { font-size: 12px; color: var(--text-dim); }
.best-value {
  font-size: 30px; font-weight: bold;
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.history-caption { font-size: 13px; font-weight: bold; color: var(--text-dim); margin-top: 4px; }
.history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.history-entry {
  font-size: 13px;
  background: var(--cell-bg);
  border-radius: 8px;
  padding: 4px 16px;
  text-align: center;
}

.size-select { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.section-label { font-size: 13px; font-weight: bold; color: var(--text-dim); }
.size-row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

.size-btn {
  border: none;
  border-radius: 14px;
  min-width: 56px;
  padding: 10px 12px 8px;
  background: var(--card-bg);
  color: var(--text-main);
  font-family: var(--font);
  font-weight: bold;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.size-btn.selected {
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  color: var(--on-accent);
  box-shadow: 0 6px 14px rgba(124, 108, 240, 0.4);
}
.classic-badge { font-size: 10px; font-weight: normal; opacity: 0.95; }

.author {
  text-align: center;
  font-size: 13px;
  color: var(--text-dim);
  margin: 18px 0 6px;
}

/* ============ 游戏界面 ============ */
.game-header { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.game-titles { display: flex; flex-direction: column; line-height: 1.15; }
.game-title {
  margin: 0;
  font-size: 30px;
  font-weight: bold;
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.game-author { font-size: 11px; color: var(--text-dim); }
.score-cards { margin-left: auto; display: flex; gap: 8px; }
.score-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 6px 14px;
  min-width: 70px;
  text-align: center;
  box-shadow: 0 4px 12px -6px var(--card-shadow), inset 0 1px 0 var(--tile-hi);
}
.card-caption { display: block; font-size: 11px; color: var(--text-dim); }
.card-value { font-size: 19px; font-weight: bold; }

.play-hint { text-align: center; font-size: 12px; color: var(--text-dim); margin: 6px 0 10px; }

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: linear-gradient(to bottom right, rgba(255,255,255,0.09), rgba(255,255,255,0.04));
  border: 1px solid var(--card-border);
  border-radius: 18px;
  box-shadow:
    0 18px 34px rgba(0, 0, 0, 0.25),
    inset 0 2px 10px rgba(0, 0, 0, 0.20);
  overflow: hidden;
  touch-action: none; /* 关键：棋盘区域禁止浏览器滚动，交给滑动手势（spec3 §三） */
}

#board { position: absolute; inset: 3.5%; }

.cells, .tiles { position: absolute; inset: 0; }
.cell {
  position: absolute;
  border-radius: 14%;
  background: var(--cell-bg);
  border: 1px solid var(--cell-border);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.12);
}
.tile {
  position: absolute;
  border-radius: 14%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: var(--text-main);
  font-size: clamp(16px, 7vw, 34px);
  will-change: transform;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  box-shadow:
    0 6px 14px -4px var(--tile-shadow),
    0 2px 5px -2px var(--tile-shadow),
    inset 0 1px 0 var(--tile-hi),
    inset 0 -2px 6px rgba(0, 0, 0, 0.08);
}
.tile.t2   { background: var(--tile-2); }
.tile.t4   { background: var(--tile-4); }
.tile.t8   { background: var(--tile-8); }
.tile.t16  { background: var(--tile-16); }
.tile.t32  { background: var(--tile-32); }
.tile.t64  { background: var(--tile-64); }
.tile.t128 { background: var(--tile-128); }
.tile.t256 { background: var(--tile-256); }
.tile.t512 { background: var(--tile-512); }
.tile.t1024{ background: var(--tile-1024); }
.tile.t2048{ background: var(--tile-2048); color: var(--tile-2048-fg); text-shadow: none; }
.tile.t4096, .tile.t8192, .tile.t16384, .tile.t32768 { background: var(--tile-super); }
.tile.t128, .tile.t256, .tile.t512, .tile.t1024 {
  font-size: clamp(13px, 5.5vw, 27px);
}
.tile.t4096, .tile.t8192, .tile.t16384, .tile.t32768 {
  font-size: clamp(10px, 4.5vw, 22px);
  color: #141923;
}
.tile-max {
  box-shadow: 0 0 18px var(--max-glow), 0 0 30px var(--max-glow);
  animation: pop 240ms ease-out, maxBreathe 2.4s ease-in-out infinite alternate;
}
@keyframes maxBreathe {
  0% { box-shadow: 0 0 12px var(--max-glow), 0 0 22px var(--max-glow); }
  100% { box-shadow: 0 0 22px var(--max-glow), 0 0 44px var(--max-glow); }
}
.tile.pop { animation: pop 240ms ease-out; }
@keyframes pop {
  0% { transform: scale(0.4); opacity: 0; }
  55% { transform: scale(1.12); opacity: 1; }
  75% { transform: scale(0.96); }
  100% { transform: scale(1); }
}
.tile.spawn { animation: spawnGlow 420ms ease-out; }
@keyframes spawnGlow {
  0% { transform: scale(0.55); opacity: 0; box-shadow: 0 0 0 0 var(--spawn-glow); }
  45% { transform: scale(1.08); opacity: 1; box-shadow: 0 0 16px 3px var(--spawn-glow); }
  70% { transform: scale(0.98); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 var(--spawn-glow); }
}
.tile.moving { z-index: 5; }

#confettiCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 30;
}

.overlay {
  position: absolute;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  backdrop-filter: blur(3px);
}
.panel {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 20px;
  padding: 20px 26px;
  margin: 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: 320px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  animation: panelIn 220ms ease-out;
}
@keyframes panelIn {
  0% { transform: scale(0.92); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
.panel-title { margin: 0; font-size: 26px; font-weight: bold; }
.panel-score {
  font-size: 26px; font-weight: bold;
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.panel-subtitle { margin: 0; font-size: 14px; color: var(--text-dim); }
.new-best { font-size: 14px; font-weight: bold; color: var(--accent-a); }
.panel-btns { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 4px; }

.pulse { animation: pulse 600ms ease 3; }
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.18); }
}

.game-controls {
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: center;
  margin-top: 14px;
}

/* ============ 统计栏（spec.md FR-19：步数/用时/统计入口） ============ */
.stats-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 8px auto 12px;
  max-width: 360px;
}
.stat-chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  padding: 5px 14px;
}
.stat-value { font-size: 15px; font-weight: bold; font-variant-numeric: tabular-nums; }

/* ============ 菱形方向键（spec.md FR-18 / spec2 §3.1 桌面控制） ============ */
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(3, 44px);
  gap: 2px;
}
.dpad-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--card-border);
  background: linear-gradient(to bottom right, var(--accent-a), var(--accent-b));
  color: var(--on-accent);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.dpad-btn:active { transform: scale(0.92); }
.dpad .up    { grid-area: 1 / 2; }
.dpad .left  { grid-area: 2 / 1; }
.dpad .right { grid-area: 2 / 3; }
.dpad .down  { grid-area: 3 / 2; }

/* ============ 统计面板 ============ */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.stat-box {
  background: var(--cell-bg);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.stat-big {
  font-size: 22px;
  font-weight: bold;
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}

/* ============ 设置模态 ============ */
.modal {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-panel {
  width: 100%;
  max-width: 340px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 20px;
  padding: 22px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.modal-panel h2 { margin: 0; text-align: center; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.setting-caption { font-size: 15px; font-weight: bold; }
.seg { display: flex; gap: 6px; }
.seg-btn {
  border: 1px solid var(--card-border);
  border-radius: 999px;
  padding: 7px 18px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: bold;
  background: var(--cell-bg);
  color: var(--text-main);
  cursor: pointer;
}
.seg-btn.selected {
  background: linear-gradient(to right, var(--accent-a), var(--accent-b));
  color: var(--on-accent);
  border-color: transparent;
}

@media (min-height: 760px) and (min-width: 500px) {
  .screen { max-width: 500px; }
}

/* ============ 背景光晕（对齐桌面版 GlowBackground，spec2 §4.2） ============ */
.glow {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}
.glow i {
  position: absolute;
  border-radius: 50%;
  background: radial-gradient(circle, var(--glow-c1) 0%, rgba(0, 0, 0, 0) 55%);
  opacity: var(--glow-opacity);
  filter: blur(90px);
  animation: glowDrift 13s ease-in-out infinite alternate;
}
.glow i:nth-child(2) {
  background: radial-gradient(circle, var(--glow-c2) 0%, rgba(0, 0, 0, 0) 55%);
  animation-duration: 11s;
  animation-delay: 1.2s;
  animation-direction: alternate-reverse;
}
.glow i:nth-child(3) {
  background: radial-gradient(circle, var(--glow-c3) 0%, rgba(0, 0, 0, 0) 55%);
  animation-duration: 9.5s;
  animation-delay: 2.1s;
}
.glow i:nth-child(4) {
  background: radial-gradient(circle, var(--glow-c4) 0%, rgba(0, 0, 0, 0) 55%);
  animation-duration: 10.5s;
  animation-delay: 3s;
  animation-direction: alternate-reverse;
}
.glow i:nth-child(1) { left: 10%; top: 16%; width: 480px; height: 480px; margin: -240px 0 0 -240px; }
.glow i:nth-child(2) { left: 86%; top: 20%; width: 560px; height: 560px; margin: -280px 0 0 -280px; }
.glow i:nth-child(3) { left: 82%; top: 84%; width: 460px; height: 460px; margin: -230px 0 0 -230px; }
.glow i:nth-child(4) { left: 16%; top: 86%; width: 520px; height: 520px; margin: -260px 0 0 -260px; }

@keyframes glowDrift {
  from { transform: translate(0, 0); }
  to { transform: translate(48px, 38px); }
}

.screen, .modal { position: relative; z-index: 1; }