/* ===== 游戏页通用样式 ===== */
:root {
  --bg: #0b0f1a;
  --bg-2: #111830;
  --surface: rgba(255, 255, 255, 0.04);
  --surface-2: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.10);
  --text: #eef2ff;
  --muted: #9aa6c7;
  --accent: #6c8cff;
  --accent-2: #22d3ee;
  --danger: #fb7185;
  --shadow: 0 20px 50px rgba(3, 7, 18, 0.55);
  --radius: 18px;
}

html[data-theme="light"] {
  --bg: #eef1fb;
  --bg-2: #dfe6ff;
  --surface: rgba(255, 255, 255, 0.78);
  --surface-2: rgba(255, 255, 255, 0.95);
  --border: rgba(20, 30, 70, 0.12);
  --text: #131a33;
  --muted: #5b678a;
  --shadow: 0 18px 40px rgba(30, 45, 90, 0.16);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: var(--bg);
  transition: background .35s ease, color .35s ease;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(900px 520px at 50% -160px, rgba(108, 140, 255, .30), transparent 70%),
    radial-gradient(600px 400px at 90% 100%, rgba(34, 211, 238, .16), transparent 70%);
  pointer-events: none;
}

/* 顶栏 */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1080px;
  margin: 0 auto;
  padding: 18px 24px;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
  transition: color .2s, background .2s, transform .2s;
}
.back-link:hover { color: var(--text); background: var(--surface-2); transform: translateX(-3px); }

.topbar-title { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; }
.topbar-title .emoji { font-size: 22px; }

.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  transition: transform .2s, background .2s;
}
.icon-btn:hover { transform: translateY(-2px); background: var(--surface-2); }

/* 主体 */
.game-shell {
  max-width: 1080px;
  margin: 0 auto;
  padding: 8px 24px 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.hud {
  display: flex;
  align-items: stretch;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.hud-item {
  min-width: 104px;
  padding: 10px 18px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  text-align: center;
}
.hud-item .label { display: block; font-size: 11px; color: var(--muted); letter-spacing: 1px; text-transform: uppercase; }
.hud-item .value { display: block; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.hud-item.accent .value { color: var(--accent-2); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 26px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  color: #fff;
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  box-shadow: 0 10px 24px rgba(108, 140, 255, .32);
  cursor: pointer;
  transition: transform .18s, box-shadow .18s, filter .2s;
}
.btn:hover { transform: translateY(-2px); filter: brightness(1.06); }
.btn:active { transform: translateY(0); }
.btn-ghost {
  color: var(--text);
  background: var(--surface);
  border-color: var(--border);
  box-shadow: none;
}
.btn-ghost:hover { background: var(--surface-2); }

.hint { margin: 0; color: var(--muted); font-size: 13px; text-align: center; }

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

/* 画布容器 */
.stage {
  position: relative;
  width: 100%;
  max-width: 560px;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 12px;
  touch-action: none;
  background: #0a0e1a;
}
html[data-theme="light"] canvas { background: #131a33; }

/* 遮罩层 */
.overlay {
  position: absolute;
  inset: 12px;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border-radius: 12px;
  background: rgba(6, 10, 22, .82);
  backdrop-filter: blur(4px);
  text-align: center;
  padding: 20px;
}
.overlay.show { display: flex; animation: fadeIn .25s ease; }
.overlay h2 { margin: 0; font-size: 26px; }
.overlay p { margin: 0; color: #b9c3e6; font-size: 14px; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* 触屏方向键 */
.dpad {
  display: none;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(2, 52px);
  gap: 8px;
  justify-content: center;
}
.dpad button {
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.dpad button:active { background: rgba(108, 140, 255, .3); }
.dpad .up { grid-column: 2; grid-row: 1; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }

@media (hover: none) and (pointer: coarse) {
  .dpad.show { display: grid; }
}

/* ===== 记忆翻牌 ===== */
.memory-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  width: 100%;
  max-width: 480px;
}
.memory-card {
  position: relative;
  aspect-ratio: 1 / 1;
  border: none;
  padding: 0;
  border-radius: 16px;
  background: transparent;
  cursor: pointer;
  perspective: 800px;
  font-size: 34px;
}
.memory-inner {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.4, .2, .2, 1);
}
.memory-card.flipped .memory-inner,
.memory-card.matched .memory-inner { transform: rotateY(180deg); }

.memory-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 16px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border: 1px solid var(--border);
}
.memory-back {
  background: linear-gradient(135deg, rgba(108, 140, 255, .55), rgba(34, 211, 238, .35));
  font-size: 26px;
}
.memory-front {
  transform: rotateY(180deg);
  background: var(--surface-2);
}
.memory-card.matched .memory-front {
  background: rgba(34, 211, 238, .22);
  border-color: rgba(34, 211, 238, .6);
  animation: pop .4s ease;
}
@keyframes pop {
  0% { transform: rotateY(180deg) scale(1); }
  50% { transform: rotateY(180deg) scale(1.12); }
  100% { transform: rotateY(180deg) scale(1); }
}

/* ===== 2048 ===== */
.puzzle-wrap {
  position: relative;
  width: 100%;
  max-width: 420px;
  padding: 12px;
  border-radius: 18px;
  background: #8f7a66;
  box-shadow: var(--shadow);
  touch-action: none;
}
.puzzle-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 10px;
  border-radius: 12px;
  background: #bbada0;
}
.puzzle-cell {
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  background: rgba(238, 228, 218, .35);
}
.puzzle-tile {
  position: absolute;
  top: 0;
  left: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-weight: 700;
  color: #6b5a48;
  background: #eee4da;
  transition: transform .12s ease-in-out, opacity .12s ease-in-out;
  will-change: transform;
}
.puzzle-tile.gone { opacity: 0; }
.puzzle-tile.merged { animation: tileFlash .2s ease-in-out; }
.puzzle-tile.new { animation: tileAppear .18s ease-out; }
@keyframes tileFlash {
  0% { filter: brightness(1); }
  50% { filter: brightness(1.55); }
  100% { filter: brightness(1); }
}
@keyframes tileAppear {
  0% { opacity: 0; filter: brightness(1.4); }
  100% { opacity: 1; filter: brightness(1); }
}

@media (max-width: 620px) {
  .topbar { padding: 14px 16px; }
  .topbar-title { font-size: 16px; }
  .game-shell { padding: 4px 16px 40px; }
  .hud-item { min-width: 84px; padding: 8px 14px; }
  .hud-item .value { font-size: 19px; }
}
