/* ===== CSS Variables ===== */
:root {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-card-hover: #252545;
  --primary: #6c63ff;
  --primary-light: #8b83ff;
  --secondary: #ff6584;
  --accent: #00d2ff;
  --text: #e8e8e8;
  --text-dim: #8888aa;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 8px 32px rgba(0,0,0,0.3);
  --header-h: 56px;
  --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#app { width: 100%; height: 100%; display: flex; flex-direction: column; }

/* ===== Header ===== */
header {
  height: var(--header-h);
  background: rgba(15, 15, 26, 0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(108, 99, 255, 0.15);
  z-index: 100;
  flex-shrink: 0;
}
.header-inner {
  max-width: 1200px; margin: 0 auto; height: 100%;
  display: flex; align-items: center; padding: 0 12px; gap: 8px;
}
.header-inner h1 {
  font-size: 20px; font-weight: 700;
  background: linear-gradient(135deg, var(--primary-light), var(--accent));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.header-spacer { flex: 1; }
.btn-icon {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(108, 99, 255, 0.08);
  border: 1px solid rgba(108, 99, 255, 0.15);
  border-radius: 10px;
  color: var(--primary-light);
  cursor: pointer; transition: var(--transition);
  font-size: 12px; flex-shrink: 0;
}
.btn-icon:hover { background: rgba(108, 99, 255, 0.2); }
.btn-lang { font-size: 11px; font-weight: 700; width: auto; padding: 0 10px; }
.avatar-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(108, 99, 255, 0.12);
  border: 1px solid rgba(108, 99, 255, 0.2);
  border-radius: 50%; cursor: pointer;
  font-size: 20px; transition: var(--transition); flex-shrink: 0;
}
.avatar-btn:hover { border-color: var(--primary-light); transform: scale(1.1); }

/* ===== Menu ===== */
.menu {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 0 16px 32px; -webkit-overflow-scrolling: touch;
}
.hero { text-align: center; padding: 28px 0 20px; }
.hero h2 {
  font-size: 26px; font-weight: 800;
  background: linear-gradient(135deg, #fff, var(--primary-light));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
  margin-bottom: 6px;
}
.hero p { color: var(--text-dim); font-size: 14px; }

/* ===== Game Grid ===== */
.game-grid {
  max-width: 1000px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px;
}
@media (min-width: 640px) { .game-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); } }

.game-card {
  background: var(--bg-card);
  border: 1px solid rgba(108, 99, 255, 0.1);
  border-radius: var(--radius);
  padding: 18px 14px; text-align: center;
  cursor: pointer; transition: var(--transition);
  position: relative; overflow: hidden;
}
.game-card::before {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(135deg, rgba(108,99,255,0.05), rgba(0,210,255,0.05));
  opacity: 0; transition: var(--transition);
}
.game-card:hover {
  transform: translateY(-4px);
  border-color: rgba(108, 99, 255, 0.3);
  box-shadow: 0 12px 40px rgba(108, 99, 255, 0.15);
}
.game-card:hover::before { opacity: 1; }
.game-card:active { transform: scale(0.97); }
.game-card .emoji { font-size: 44px; line-height: 1.2; margin-bottom: 10px; display: block; position: relative; }
.game-card .name { font-size: 14px; font-weight: 600; color: var(--text); position: relative; }
.game-card .desc { font-size: 11px; color: var(--text-dim); margin-top: 4px; position: relative; }

/* ===== Game Container ===== */
.game-container {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  overflow: hidden; position: relative; padding: 8px;
}
.game-wrapper {
  width: 100%; max-width: 500px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  position: relative;
}

/* ===== Game Shared ===== */
.game-score-bar {
  width: 100%; display: flex; justify-content: space-between; align-items: center;
  padding: 6px 14px; background: var(--bg-card);
  border-radius: var(--radius-sm); border: 1px solid rgba(108, 99, 255, 0.1);
  gap: 8px; flex-wrap: wrap;
}
.game-score-bar .label { font-size: 11px; color: var(--text-dim); }
.game-score-bar .value { font-size: 18px; font-weight: 700; color: var(--primary-light); }
.game-btn {
  padding: 8px 20px; border-radius: var(--radius-sm); border: none;
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: var(--transition); color: #fff; background: var(--primary);
}
.game-btn:hover { background: var(--primary-light); transform: scale(1.05); }
.game-btn:active { transform: scale(0.97); }
.game-btn.secondary { background: transparent; border: 1px solid rgba(108, 99, 255, 0.3); color: var(--primary-light); }
.game-btn.secondary:hover { background: rgba(108, 99, 255, 0.1); }
.game-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.game-over-overlay {
  position: absolute; inset: 0; background: rgba(15, 15, 26, 0.88);
  backdrop-filter: blur(8px); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; z-index: 10;
}
.game-over-overlay h2 { font-size: 26px; font-weight: 800; color: var(--secondary); }
.game-over-overlay .final-score {
  font-size: 44px; font-weight: 800;
  background: linear-gradient(135deg, var(--primary-light), var(--accent));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}

/* ===== Canvas ===== */
canvas.game-canvas {
  border-radius: var(--radius-sm); border: 1px solid rgba(108, 99, 255, 0.15);
  background: var(--bg-card); max-width: 100%; touch-action: none;
}

/* ===== Modal ===== */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px); z-index: 200;
}
.modal {
  position: fixed; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: calc(100% - 32px); max-width: 400px; max-height: 85vh;
  background: var(--bg-card); border: 1px solid rgba(108, 99, 255, 0.2);
  border-radius: var(--radius); z-index: 201;
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid rgba(108, 99, 255, 0.1);
}
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-close {
  width: 32px; height: 32px; border: none; background: none;
  color: var(--text-dim); font-size: 22px; cursor: pointer;
  border-radius: 8px; transition: var(--transition);
}
.modal-close:hover { background: rgba(255,255,255,0.05); color: var(--text); }
.modal-body { padding: 16px 18px; overflow-y: auto; flex: 1; }
.modal-footer { padding: 12px 18px; display: flex; justify-content: center; gap: 8px; }

.modal-label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; font-weight: 600; }
.modal-input {
  width: 100%; padding: 10px 14px; border-radius: var(--radius-sm);
  border: 1px solid rgba(108, 99, 255, 0.2); background: rgba(0,0,0,0.2);
  color: var(--text); font-size: 15px; outline: none; transition: var(--transition);
}
.modal-input:focus { border-color: var(--primary-light); }

/* Avatar Grid */
.avatar-grid {
  display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px;
}
.avatar-option {
  width: 100%; aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  font-size: 22px; border-radius: 8px; cursor: pointer;
  border: 2px solid transparent; transition: var(--transition);
  background: rgba(255,255,255,0.03);
}
.avatar-option:hover { background: rgba(108, 99, 255, 0.15); }
.avatar-option.selected { border-color: var(--primary-light); background: rgba(108, 99, 255, 0.2); }

/* Help Content */
.help-content { font-size: 14px; line-height: 1.7; color: var(--text); }
.help-content b { color: var(--primary-light); }

/* Leaderboard Table */
.lb-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.lb-table th { color: var(--text-dim); font-size: 11px; font-weight: 600; padding: 6px 8px; text-align: left; border-bottom: 1px solid rgba(108,99,255,0.1); }
.lb-table td { padding: 8px; border-bottom: 1px solid rgba(255,255,255,0.03); }
.lb-rank { font-weight: 700; text-align: center; width: 40px; }
.lb-avatar { margin-right: 6px; }
.lb-score { font-weight: 700; color: var(--primary-light); }
.lb-date { color: var(--text-dim); font-size: 11px; }

/* ===== 2048 ===== */
.grid-2048 {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 8px;
  background: #1e1e38; border-radius: var(--radius); width: 100%; max-width: 400px; aspect-ratio: 1;
}
.tile-2048 {
  border-radius: var(--radius-sm); display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: clamp(18px, 5vw, 32px); transition: all 0.15s ease; aspect-ratio: 1;
}
.tile-2048.empty { background: rgba(255,255,255,0.03); }
.tile-2048.t2 { background: #3d3d5c; color: #c8c8e0; }
.tile-2048.t4 { background: #4a4a6a; color: #d0d0e8; }
.tile-2048.t8 { background: #e8854a; color: #fff; }
.tile-2048.t16 { background: #e86e3a; color: #fff; }
.tile-2048.t32 { background: #e85d3a; color: #fff; }
.tile-2048.t64 { background: #e84a2a; color: #fff; }
.tile-2048.t128 { background: #edcf72; color: #fff; font-size: clamp(16px, 4.5vw, 28px); }
.tile-2048.t256 { background: #edcc61; color: #fff; font-size: clamp(16px, 4.5vw, 28px); }
.tile-2048.t512 { background: #edc850; color: #fff; font-size: clamp(16px, 4.5vw, 28px); }
.tile-2048.t1024 { background: #edc53f; color: #fff; font-size: clamp(14px, 4vw, 24px); }
.tile-2048.t2048 { background: #6c63ff; color: #fff; font-size: clamp(14px, 4vw, 24px); }
.tile-2048.tsuper { background: linear-gradient(135deg, #6c63ff, #00d2ff); color: #fff; font-size: clamp(12px, 3.5vw, 20px); }

/* ===== Sudoku ===== */
.grid-sudoku {
  display: grid; grid-template-columns: repeat(9, 1fr); gap: 1px; padding: 2px;
  background: rgba(108, 99, 255, 0.2); border-radius: var(--radius-sm);
  width: 100%; max-width: 400px; aspect-ratio: 1;
}
.cell-sudoku {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-card); font-size: clamp(14px, 4vw, 22px); font-weight: 600;
  cursor: pointer; aspect-ratio: 1; transition: var(--transition);
  color: var(--text); border: none; outline: none;
}
.cell-sudoku.fixed { color: var(--text-dim); cursor: default; }
.cell-sudoku.selected { background: rgba(108, 99, 255, 0.35) !important; box-shadow: inset 0 0 0 2px var(--primary-light); }
.cell-sudoku.error { color: var(--secondary) !important; }
.cell-sudoku.highlight { background: rgba(108, 99, 255, 0.1); }
.cell-sudoku:nth-child(3n) { border-right: 2px solid rgba(108, 99, 255, 0.3); }
.cell-sudoku:nth-child(9n) { border-right: none; }
.cell-sudoku:nth-child(n+19):nth-child(-n+27),
.cell-sudoku:nth-child(n+46):nth-child(-n+54) { border-bottom: 2px solid rgba(108, 99, 255, 0.3); }
.sudoku-numpad { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }
.sudoku-numpad button {
  width: 36px; height: 36px; border-radius: 8px;
  border: 1px solid rgba(108, 99, 255, 0.2); background: var(--bg-card);
  color: var(--text); font-size: 15px; font-weight: 600;
  cursor: pointer; transition: var(--transition);
}
.sudoku-numpad button:hover { background: rgba(108, 99, 255, 0.15); }
.sudoku-numpad button:active { transform: scale(0.9); }
.sudoku-errors { display: flex; gap: 4px; align-items: center; }
.sudoku-errors .dot { width: 10px; height: 10px; border-radius: 50%; background: rgba(255,255,255,0.1); }
.sudoku-errors .dot.used { background: var(--secondary); }

/* ===== Board Game ===== */
.board-info {
  width: 100%; display: flex; justify-content: center; align-items: center;
  gap: 12px; padding: 6px; font-size: 14px; font-weight: 600;
}
.board-info .player { padding: 4px 14px; border-radius: 20px; transition: var(--transition); }
.board-info .player.active { background: rgba(108, 99, 255, 0.2); border: 1px solid rgba(108, 99, 255, 0.4); }
.board-info .player:not(.active) { color: var(--text-dim); }

/* ===== Don't Tap White ===== */
.dtw-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; width: 100%; max-width: 340px; overflow: hidden; border-radius: var(--radius-sm); }
.dtw-cell { aspect-ratio: 0.7; border-radius: 4px; cursor: pointer; transition: background 0.1s; }
.dtw-cell.white { background: rgba(255, 255, 255, 0.08); }
.dtw-cell.black { background: #e8e8e8; }
.dtw-cell.black:hover { background: #fff; }
.dtw-cell.tapped { background: rgba(108, 99, 255, 0.4); }
.dtw-cell.wrong { background: var(--secondary); }

/* ===== Dragon Tiger Card Game ===== */
.dt-hand {
  display: flex; gap: 6px; flex-wrap: wrap; justify-content: center;
  padding: 8px; min-height: 90px;
}
.dt-card-btn {
  width: 52px; height: 72px; border-radius: 8px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; cursor: pointer;
  transition: var(--transition); border: 2px solid;
  background: rgba(0,0,0,0.2); gap: 2px;
}
.dt-card-btn:hover:not(:disabled) { transform: translateY(-4px); }
.dt-card-btn:disabled { opacity: 0.3; cursor: not-allowed; transform: none; }
.dt-card-btn.dragon-card { border-color: #e74c3c; color: #e74c3c; }
.dt-card-btn.tiger-card { border-color: #3498db; color: #3498db; }
.dt-card-btn.morph { border-color: #f39c12; color: #f39c12; }
.dt-card-btn.selected { transform: translateY(-8px); box-shadow: 0 8px 20px rgba(108,99,255,0.3); border-color: var(--primary-light); }
.dt-played {
  width: 64px; height: 88px; border-radius: 10px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 700; border: 2px solid rgba(255,255,255,0.15);
}
.dt-played.facedown { background: linear-gradient(135deg, var(--primary), #4a45b0); font-size: 20px; }
.dt-played.dragon-reveal { background: #2a1515; border-color: #e74c3c; color: #e74c3c; }
.dt-played.tiger-reveal { background: #15202a; border-color: #3498db; color: #3498db; }
.dt-arena {
  display: flex; align-items: center; justify-content: center; gap: 20px;
  padding: 12px; min-height: 110px;
}
.dt-arena .vs-text { font-size: 20px; font-weight: 800; color: var(--secondary); }
.dt-score-row {
  display: flex; justify-content: space-around; width: 100%;
  padding: 6px 12px; font-size: 13px;
}
.dt-score-row span { font-weight: 700; }

/* ===== Klotski ===== */
.klotski-board { position: relative; background: #1e1e38; border-radius: var(--radius); border: 2px solid rgba(108, 99, 255, 0.2); }
.klotski-piece {
  position: absolute; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; cursor: pointer;
  transition: left 0.2s ease, top 0.2s ease; border: 2px solid rgba(255,255,255,0.1);
}
.klotski-piece.cao { background: linear-gradient(135deg, #e74c3c, #c0392b); color: #fff; font-size: 16px; }
.klotski-piece.general { background: linear-gradient(135deg, #f39c12, #e67e22); color: #fff; }
.klotski-piece.hbar { background: linear-gradient(135deg, #3498db, #2980b9); color: #fff; }
.klotski-piece.vbar { background: linear-gradient(135deg, #2ecc71, #27ae60); color: #fff; }
.klotski-piece.small { background: linear-gradient(135deg, #9b59b6, #8e44ad); color: #fff; }

/* ===== Link Match 连连看 ===== */
.link-board {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 3px;
  padding: 4px; background: #1e1e38; border-radius: var(--radius);
  width: 100%; max-width: 420px; position: relative;
}
.link-cell {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  font-size: clamp(18px, 4vw, 28px); border-radius: 6px;
  background: var(--bg-card); cursor: pointer; transition: var(--transition);
  border: 2px solid transparent;
}
.link-cell:hover { background: rgba(108, 99, 255, 0.15); }
.link-selected { border-color: var(--primary-light) !important; background: rgba(108, 99, 255, 0.2) !important; }
.link-path {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 5;
}

/* ===== Sokoban 推箱子 ===== */
.sokoban-board { position: relative; background: #1e1e38; border-radius: var(--radius); border: 2px solid rgba(108, 99, 255, 0.2); }
.sk-cell {
  position: absolute; display: flex; align-items: center; justify-content: center;
  font-size: clamp(16px, 4vw, 26px);
}
.sk-wall { background: #3a3a5c; border-radius: 3px; }
.sk-target { color: rgba(108, 99, 255, 0.4); font-size: clamp(12px, 3vw, 18px); }
.sk-box { background: rgba(230, 126, 34, 0.3); border: 2px solid #e67e22; border-radius: 6px; }
.sk-box-done { background: rgba(46, 204, 113, 0.3); border: 2px solid #2ecc71; border-radius: 6px; }
.sk-player { z-index: 2; }

/* ===== Minesweeper 扫雷 ===== */
.ms-board {
  display: grid; grid-template-columns: repeat(10, 1fr); gap: 2px;
  padding: 4px; background: #1e1e38; border-radius: var(--radius);
  width: 100%; max-width: 400px;
}
.ms-cell {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  background: rgba(108, 99, 255, 0.12); border-radius: 4px;
  font-size: clamp(12px, 3vw, 16px); font-weight: 700; cursor: pointer;
  transition: var(--transition); border: 1px solid rgba(108, 99, 255, 0.08);
}
.ms-cell:hover:not(.ms-revealed) { background: rgba(108, 99, 255, 0.25); }
.ms-revealed { background: rgba(255,255,255,0.04); cursor: default; border-color: transparent; }
.ms-mine { background: rgba(231, 76, 60, 0.2) !important; }

/* ===== D-Pad (mobile controls) ===== */
.mobile-dpad {
  display: flex; flex-direction: column; align-items: center; gap: 4px; margin-top: 6px;
}
.dpad-mid { display: flex; gap: 42px; }
.dpad-btn {
  width: 44px; height: 44px; border-radius: 12px;
  background: rgba(108, 99, 255, 0.12); border: 1px solid rgba(108, 99, 255, 0.2);
  color: var(--primary-light); font-size: 16px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.dpad-btn:hover { background: rgba(108, 99, 255, 0.25); }
.dpad-btn:active { transform: scale(0.9); background: rgba(108, 99, 255, 0.35); }

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
@keyframes popIn { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }
.fade-in { animation: fadeIn 0.4s ease forwards; }
.pop-in { animation: popIn 0.3s ease forwards; }
.game-card { animation: fadeIn 0.4s ease forwards; opacity: 0; }
.game-card:nth-child(1) { animation-delay: 0s; }
.game-card:nth-child(2) { animation-delay: 0.04s; }
.game-card:nth-child(3) { animation-delay: 0.08s; }
.game-card:nth-child(4) { animation-delay: 0.12s; }
.game-card:nth-child(5) { animation-delay: 0.16s; }
.game-card:nth-child(6) { animation-delay: 0.2s; }
.game-card:nth-child(7) { animation-delay: 0.24s; }
.game-card:nth-child(8) { animation-delay: 0.28s; }
.game-card:nth-child(9) { animation-delay: 0.32s; }
.game-card:nth-child(10) { animation-delay: 0.36s; }
.game-card:nth-child(11) { animation-delay: 0.4s; }
.game-card:nth-child(12) { animation-delay: 0.44s; }
.game-card:nth-child(13) { animation-delay: 0.48s; }
.game-card:nth-child(14) { animation-delay: 0.52s; }
.game-card:nth-child(15) { animation-delay: 0.56s; }
.game-card:nth-child(16) { animation-delay: 0.60s; }

/* ===== Scrollbar ===== */
.menu::-webkit-scrollbar { width: 6px; }
.menu::-webkit-scrollbar-track { background: transparent; }
.menu::-webkit-scrollbar-thumb { background: rgba(108,99,255,0.2); border-radius: 3px; }

/* ===== Mobile ===== */
@media (max-width: 480px) {
  .game-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .game-card { padding: 14px 10px; }
  .game-card .emoji { font-size: 34px; }
  .hero h2 { font-size: 22px; }
  .hero { padding: 18px 0 14px; }
  .avatar-grid { grid-template-columns: repeat(7, 1fr); }
}
@media (max-width: 360px) {
  .game-grid { grid-template-columns: repeat(2, 1fr); }
}
