/* ── CSS 变量 ─────────────────────────────────── */
:root {
  --board-size: clamp(280px, 90vw, 480px);
  --gap: clamp(8px, 2vw, 12px);
  --pad: clamp(8px, 2vw, 12px);
  --cell-size: calc((var(--board-size) - var(--pad) * 2 - var(--gap) * 3) / 4);
  --radius: 6px;
  --bg: #faf8ef;
  --board-bg: #bbada0;
  --cell-bg: rgba(238, 228, 218, 0.35);
  --text-dark: #776e65;
  --text-light: #f9f6f2;
  --score-bg: #bbada0;
  --btn-bg: #8f7a66;
  --btn-hover: #9f8b77;
}

/* ── Reset & 基础 ─────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  transition: background 0.9s ease;
  color: var(--text-dark);
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: none; /* 全局禁止默认滚动，游戏内滑动接管 */
}

/* ── 布局容器 ─────────────────────────────────── */
.container {
  width: var(--board-size);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px 0;
}

/* ── 头部 ─────────────────────────────────────── */
header { display: flex; flex-direction: column; gap: 8px; }

.title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 {
  font-size: clamp(2.4rem, 10vw, 3.5rem);
  font-weight: 700;
  color: var(--text-dark);
  line-height: 1;
}

.scores { display: flex; gap: 8px; }

.score-box {
  background: var(--score-bg);
  border-radius: var(--radius);
  padding: 6px 14px;
  min-width: 60px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.score-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  color: #eee4da;
  letter-spacing: 0.05em;
}

.score-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: #fff;
}

.action-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.hint { font-size: 0.85rem; color: var(--text-dark); }
.hint strong { color: var(--text-dark); }

button {
  background: var(--btn-bg);
  color: #f9f6f2;
  border: none;
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}
button:hover { background: var(--btn-hover); }
button:active { transform: scale(0.97); }

/* ── 棋盘 ─────────────────────────────────────── */
.board-wrapper {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  flex-shrink: 0;
}

.grid-bg {
  position: absolute;
  inset: 0;
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: var(--pad);
  display: grid;
  grid-template-columns: repeat(4, var(--cell-size));
  grid-template-rows: repeat(4, var(--cell-size));
  gap: var(--gap);
}

.grid-cell {
  background: var(--cell-bg);
  border-radius: calc(var(--radius) - 2px);
}

/* ── 瓦片层 ───────────────────────────────────── */
.tile-layer {
  position: absolute;
  inset: 0;
}

.tile {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: calc(var(--radius) - 2px);
  font-weight: 700;
  font-size: clamp(1rem, 5vw, 2.2rem);
  width: var(--cell-size);
  height: var(--cell-size);
  /* transition: top 0.1s ease, left 0.1s ease; */
}

/* 新瓦片弹出动画 */
@keyframes tile-appear {
  0%   { transform: scale(0);   opacity: 0; }
  70%  { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1);   opacity: 1; }
}

/* 合并瓦片弹跳动画 */
@keyframes tile-pop {
  0%   { transform: scale(1);   }
  40%  { transform: scale(1.2); }
  100% { transform: scale(1);   }
}

.tile-new    { animation: tile-appear 0.15s ease both; }
.tile-merged { animation: tile-pop    0.12s ease both; }

/* ── 瓦片配色（经典 2048 色彩方案）─────────────── */
.t2    { background: #eee4da; color: #776e65; }
.t4    { background: #ede0c8; color: #776e65; }
.t8    { background: #f2b179; color: #f9f6f2; }
.t16   { background: #f59563; color: #f9f6f2; }
.t32   { background: #f67c5f; color: #f9f6f2; }
.t64   { background: #f65e3b; color: #f9f6f2; }
.t128  { background: #edcf72; color: #f9f6f2; font-size: clamp(0.85rem, 4vw, 1.8rem); }
.t256  { background: #edcc61; color: #f9f6f2; font-size: clamp(0.85rem, 4vw, 1.8rem); }
.t512  { background: #edc850; color: #f9f6f2; font-size: clamp(0.85rem, 4vw, 1.8rem); }
.t1024 { background: #edc53f; color: #f9f6f2; font-size: clamp(0.75rem, 3.5vw, 1.5rem); }
.t2048 { background: #edc22e; color: #f9f6f2; font-size: clamp(0.75rem, 3.5vw, 1.5rem); }
.tsuper {
  background: #3c3a32; color: #f9f6f2;
  font-size: clamp(0.6rem, 3vw, 1.2rem);
}

/* ── 覆盖层 ───────────────────────────────────── */
.overlay {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: rgba(238, 228, 218, 0.73);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fade-in 0.3s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.overlay.hidden { display: none; }

.overlay-card {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.overlay-msg {
  font-size: clamp(1.4rem, 6vw, 2.2rem);
  font-weight: 700;
  color: var(--text-dark);
}

#btn-overlay {
  font-size: 1rem;
  padding: 10px 24px;
}

/* ── 页脚 ─────────────────────────────────────── */
footer p {
  text-align: center;
  font-size: 0.75rem;
  color: #a09088;
}

/* ── 按钮组 ───────────────────────────────────── */
.btn-group {
  display: flex;
  gap: 6px;
  align-items: center;
}

#btn-mute {
  padding: 8px 10px;
  font-size: 1rem;
  line-height: 1;
}

#btn-ai {
  background: var(--btn-bg);
}

#btn-ai.ai-active {
  background: #5a9f5a;
}
#btn-ai.ai-active:hover {
  background: #6ab56a;
}

/* ── AI 状态栏 ────────────────────────────────── */
.ai-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-dark);
  background: rgba(187, 173, 160, 0.25);
  border-radius: var(--radius);
  padding: 4px 10px;
}

.ai-hint-dir {
  font-size: 1.2rem;
  font-weight: 700;
  color: #5a9f5a;
  letter-spacing: 0.05em;
}

/* ── 粒子爆炸 ─────────────────────────────────── */
@keyframes particle-burst {
  0%   { transform: translate(0, 0) scale(1); opacity: 0.9; }
  100% { transform: translate(var(--tx), var(--ty)) scale(0); opacity: 0; }
}

.particle {
  position: absolute;
  width: 7px;
  height: 7px;
  margin: -3px 0 0 -3px;
  border-radius: 50%;
  pointer-events: none;
  animation: particle-burst 0.42s ease-out both;
  z-index: 25;
}

/* ── 飘字动画 ─────────────────────────────────── */
@keyframes score-float {
  0%   { transform: translateY(0); opacity: 1; }
  70%  { opacity: 1; }
  100% { transform: translateY(-52px); opacity: 0; }
}

.score-popup {
  position: absolute;
  font-weight: 700;
  font-size: 1rem;
  color: var(--text-dark);
  pointer-events: none;
  animation: score-float 0.75s ease-out both;
  z-index: 30;
  white-space: nowrap;
  text-shadow: 0 1px 3px rgba(255,255,255,0.6);
}

/* ═══════════════════════════════════════════════════
   新增样式：AI配置、排行榜、帮助
   ═══════════════════════════════════════════════════ */

/* ── AI 控制栏 ─────────────────────────────────── */
.ai-control-panel {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 8px 0;
}

.ai-badge {
  font-size: 0.4em;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 4px 10px;
  border-radius: 12px;
  vertical-align: middle;
  margin-left: 8px;
}

.btn-ai {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  font-weight: 600;
}

.btn-ai:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-ai.active {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(245, 87, 108, 0.4); }
  50% { box-shadow: 0 0 0 10px rgba(245, 87, 108, 0); }
}

.btn-config, .btn-mute, .btn-rank {
  padding: 10px;
  font-size: 1.1rem;
}

.btn-mute.muted {
  opacity: 0.5;
}

.ai-status {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(102, 126, 234, 0.1);
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.8rem;
  color: #667eea;
}

.ai-dot {
  width: 8px;
  height: 8px;
  background: #667eea;
  border-radius: 50%;
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ── AI 配置面板 ──────────────────────────────── */
.config-panel, .rank-panel {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
}

.config-panel h3, .rank-panel h3 {
  margin-bottom: 16px;
  font-size: 1.1rem;
  color: var(--text-dark);
}

.config-item {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.config-item label {
  flex: 0 0 100px;
  font-size: 0.9rem;
  color: var(--text-dark);
}

.config-item input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  height: 6px;
  background: #ddd;
  border-radius: 3px;
  outline: none;
}

.config-item input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.2s;
}

.config-item input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.config-value {
  flex: 0 0 40px;
  text-align: center;
  font-weight: 600;
  color: #667eea;
}

.config-actions {
  display: flex;
  gap: 12px;
  margin-top: 20px;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  flex: 1;
}

.btn-secondary {
  background: #e0e0e0 !important;
  color: var(--text-dark) !important;
  flex: 1;
}

/* ── 排行榜 ───────────────────────────────────── */
.rank-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.rank-tab {
  flex: 1;
  background: #f0f0f0;
  color: var(--text-dark);
  padding: 10px;
  font-size: 0.85rem;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.rank-tab.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.rank-list {
  max-height: 300px;
  overflow-y: auto;
}

.rank-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid #eee;
}

.rank-number {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  border-radius: 50%;
  font-weight: 700;
}

.rank-item:nth-child(1) .rank-number {
  background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
  color: white;
}

.rank-item:nth-child(2) .rank-number {
  background: linear-gradient(135deg, #C0C0C0 0%, #A0A0A0 100%);
  color: white;
}

.rank-item:nth-child(3) .rank-number {
  background: linear-gradient(135deg, #CD7F32 0%, #B87333 100%);
  color: white;
}

.rank-score {
  flex: 1;
  font-weight: 600;
  font-size: 1.1rem;
}

.rank-time {
  font-size: 0.75rem;
  color: #999;
}

/* ── 弹窗 ─────────────────────────────────────── */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 16px;
  padding: 28px;
  max-width: 90%;
  width: 400px;
  max-height: 80vh;
  overflow-y: auto;
}

.help-section {
  margin-bottom: 16px;
}

.help-section h4 {
  color: #667eea;
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.help-section p {
  font-size: 0.85rem;
  color: #666;
  margin-bottom: 4px;
}

.btn-close {
  width: 100%;
  margin-top: 20px;
  padding: 12px;
  background: #f0f0f0;
  color: var(--text-dark);
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

/* ── 页脚 ─────────────────────────────────────── */
.footer-links {
  text-align: center;
  margin-top: 8px;
}

.footer-links a {
  color: #667eea;
  text-decoration: none;
  font-size: 0.85rem;
}

.footer-links a:hover {
  text-decoration: underline;
}

.footer-links span {
  margin: 0 8px;
  color: #ccc;
}

/* ── 通用隐藏类 ────────────────────────────────── */
.hidden { display: none !important; }

