/* === Theme Variables === */
:root {
  --bg: #1a1a2e;
  --bg-gradient: linear-gradient(160deg, #1a1a2e, #16213e);
  --surface: #2a2a3e;
  --surface-light: #3a3a52;
  --card-back: linear-gradient(145deg, #2d2d44, #1e1e32);
  --card-border: #4a4a6a;
  --card-front: #f5f0e8;
  --card-matched: rgba(100, 220, 100, 0.2);
  --text: #e8e8f0;
  --text-dim: #8888aa;
  --accent: #ffd700;
  --accent-grad: linear-gradient(180deg, #ffe44d, #f0a500);
  --accent2: #ff6b6b;
  --btn-bg: linear-gradient(180deg, #ffd700, #e6a800);
  --btn-text: #1a1a2e;
  --btn-hover: linear-gradient(180deg, #ffe44d, #ffd700);
  --shadow: rgba(0,0,0,0.4);
  --glow-match: 0 0 20px rgba(100, 220, 100, 0.5);
  --glow-mismatch: 0 0 20px rgba(255, 80, 80, 0.5);
  --font-title: Georgia, Cambria, serif;
  --font-hud: Inter, system-ui, sans-serif;
}

[data-theme="ocean"] {
  --bg: #0a3a4a;
  --bg-gradient: linear-gradient(160deg, #0a3a4a, #0d4a5e);
  --surface: #1a4a5a;
  --surface-light: #2a5a6a;
  --card-back: linear-gradient(145deg, #1a5a6e, #0d3a4a);
  --card-border: #4a8a9a;
  --card-front: #e8f4f8;
  --card-matched: rgba(64, 200, 220, 0.25);
  --text: #d0eaf0;
  --text-dim: #70a0b0;
  --accent: #40d0e0;
  --accent-grad: linear-gradient(180deg, #60e0f0, #20a0b0);
  --accent2: #ff6b6b;
  --btn-bg: linear-gradient(180deg, #40d0e0, #20a0b0);
  --btn-text: #0a2a30;
  --btn-hover: linear-gradient(180deg, #60e0f0, #40d0e0);
  --shadow: rgba(0,40,50,0.4);
  --glow-match: 0 0 20px rgba(64, 200, 220, 0.6);
  --glow-mismatch: 0 0 20px rgba(255, 80, 80, 0.5);
}

[data-theme="cosmic"] {
  --bg: #1a0a2e;
  --bg-gradient: linear-gradient(160deg, #1a0a2e, #2a1050);
  --surface: #2a1840;
  --surface-light: #3a2060;
  --card-back: linear-gradient(145deg, #2a1840, #1a0a2e);
  --card-border: #6a3a9a;
  --card-front: #f0e8f8;
  --card-matched: rgba(160, 80, 220, 0.25);
  --text: #e0d0f0;
  --text-dim: #8870aa;
  --accent: #c060f0;
  --accent-grad: linear-gradient(180deg, #d080ff, #a040d0);
  --accent2: #ff6b9d;
  --btn-bg: linear-gradient(180deg, #c060f0, #9040c0);
  --btn-text: #1a0a2e;
  --btn-hover: linear-gradient(180deg, #d080ff, #c060f0);
  --shadow: rgba(30,0,60,0.4);
  --glow-match: 0 0 20px rgba(160, 80, 220, 0.6);
  --glow-mismatch: 0 0 20px rgba(255, 80, 80, 0.5);
}

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

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-hud);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

#game-root {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-gradient);
}

/* === Scenes === */
.scene {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
}
.scene.active { display: flex; }
.scene.overlay {
  z-index: 100;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(4px);
}

/* === Menu === */
.menu-frame {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 32px 40px;
  max-width: 480px;
  width: 90%;
}

.game-title {
  font-family: var(--font-title);
  font-size: 48px;
  color: var(--accent);
  text-shadow: 0 3px 0 rgba(0,0,0,0.3), 0 6px 20px rgba(0,0,0,0.4);
  letter-spacing: 1px;
  text-align: center;
}

.menu-subtitle {
  font-size: 16px;
  color: var(--text-dim);
  margin-top: -8px;
}

.menu-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.menu-label {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
}

.board-selector, .theme-selector {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

.board-btn {
  min-width: 100px;
  padding: 12px 16px;
  font-size: 18px;
  font-weight: 800;
  line-height: 1.2;
  text-align: center;
}

.btn-sub {
  display: block;
  font-size: 11px;
  font-weight: 600;
  opacity: 0.7;
  margin-top: 2px;
}

.theme-btn {
  padding: 8px 20px;
  border-radius: 999px;
  border: 2px solid var(--card-border);
  background: transparent;
  color: var(--text);
  font-family: var(--font-hud);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}
.theme-btn.active {
  background: var(--accent-grad);
  color: var(--btn-text);
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(0,0,0,0.3);
}

.start-btn {
  font-size: 22px;
  padding: 14px 56px;
  margin-top: 8px;
}

.best-results {
  margin-top: 8px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--surface);
  width: 100%;
  text-align: center;
}
.best-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
#best-list {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.6;
}

/* === Play HUD === */
.play-hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  z-index: 10;
  pointer-events: none;
}
.play-hud > * { pointer-events: auto; }

.hud-left, .hud-right, .hud-center {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 10px;
  background: var(--surface);
  border: 2px solid var(--card-border);
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  box-shadow: 0 2px 8px var(--shadow);
}

.hud-icon {
  font-size: 16px;
  opacity: 0.7;
}

.combo-chip {
  background: var(--accent-grad);
  color: var(--btn-text);
  border-color: var(--accent);
  font-weight: 800;
}

.hud-btn {
  padding: 6px 12px;
  font-size: 18px;
  min-width: 36px;
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: var(--surface-light);
  color: var(--text);
  border: 2px solid var(--card-border);
  box-shadow: 0 2px 8px var(--shadow);
}

/* === Game Board === */
.game-board {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 60px 12px 12px;
  max-width: 700px;
  margin: 0 auto;
}

#game-canvas {
  width: 100%;
  height: auto;
  border-radius: 16px;
  cursor: pointer;
  touch-action: manipulation;
}

/* === Buttons === */
.game-button {
  border: 3px solid var(--card-border);
  border-radius: 14px;
  background: var(--btn-bg);
  color: var(--btn-text);
  box-shadow: 0 4px 0 var(--card-border), 0 8px 20px var(--shadow);
  font-family: var(--font-hud);
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s;
}
.game-button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--card-border), 0 4px 12px var(--shadow);
}
.game-button.secondary-btn {
  background: var(--surface-light);
  color: var(--text);
  box-shadow: 0 4px 0 var(--card-border), 0 8px 20px var(--shadow);
}

/* === Win Overlay === */
.win-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 32px 40px;
  border-radius: 24px;
  background: var(--surface);
  border: 3px solid var(--card-border);
  box-shadow: 0 8px 0 var(--card-border), 0 24px 50px rgba(0,0,0,0.4);
  z-index: 10;
  min-width: 280px;
}

.win-title {
  font-family: var(--font-title);
  font-size: 36px;
  color: var(--accent);
}

.win-stats {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  border-radius: 8px;
  background: rgba(0,0,0,0.15);
}
.stat-label { font-size: 14px; color: var(--text-dim); font-weight: 600; }
.stat-value { font-size: 18px; font-weight: 800; color: var(--accent); }
.best-row { background: rgba(255,215,0,0.1); }
.best-row .stat-value { color: var(--accent); }

.win-buttons {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}
.win-buttons .game-button {
  padding: 12px 28px;
  font-size: 16px;
}

#confetti-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}

/* === Responsive === */
@media (max-width: 520px) {
  .game-title { font-size: 34px; }
  .menu-frame { padding: 20px; gap: 14px; }
  .board-btn { min-width: 80px; padding: 10px 12px; font-size: 16px; }
  .win-panel { padding: 24px; min-width: auto; width: 85%; }
  .game-board { padding: 56px 6px 6px; }
  .play-hud { padding: 6px 8px; }
  .hud-chip { padding: 4px 8px; font-size: 13px; }
}

@media (min-width: 1000px) {
  .game-board { max-width: 800px; }
}
