/* ========================================
   TREASURE HUNTER — PREMIUM DARK THEME
   ======================================== */
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Inter:wght@400;500;600;700&display=swap');

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --gold: #ffd700;
  --gold-light: #ffe566;
  --gold-dark: #b8860b;
  --emerald: #2ecc71;
  --emerald-dark: #1a9c54;
  --ruby: #e74c3c;
  --ruby-dark: #c0392b;
  --sapphire: #3498db;
  --purple: #9b59b6;
  --bg-primary: #0b0e17;
  --bg-secondary: #111827;
  --bg-card: #1a1f35;
  --bg-cell: #1e2742;
  --bg-cell-hover: #263150;
  --border-subtle: rgba(255, 255, 255, 0.06);
  --border-gold: rgba(255, 215, 0, 0.3);
  --text-primary: #f0f0f5;
  --text-secondary: #8892a8;
  --text-muted: #5a6478;
  --glow-gold: rgba(255, 215, 0, 0.4);
  --glow-emerald: rgba(46, 204, 113, 0.4);
  --glow-ruby: rgba(231, 76, 60, 0.5);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 28px;
  --shadow-lg: 0 20px 60px rgba(0,0,0,0.5);
  --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

html { font-size: 16px; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Noto Sans Thai', 'Inter', system-ui, sans-serif;
  display: flex;
  justify-content: center;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ========== PARTICLES ========== */
#particles {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

/* ========== GAME CONTAINER ========== */
.game-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 460px;
  padding: 24px 20px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

/* ========== HEADER ========== */
.game-header { text-align: center; }

.game-title {
  font-family: 'Orbitron', monospace;
  font-weight: 900;
  font-size: 1.8rem;
  background: linear-gradient(135deg, var(--gold-light), var(--gold), var(--gold-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  filter: drop-shadow(0 0 20px var(--glow-gold));
  letter-spacing: 1px;
}
.title-icon { -webkit-text-fill-color: initial; }

.game-subtitle {
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 500;
  letter-spacing: 0.5px;
}

/* ========== STATS BAR ========== */
.stats-bar {
  width: 100%;
  display: flex;
  gap: 10px;
}
.stat {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 12px 8px;
  text-align: center;
  transition: var(--transition);
}
.stat.highlight {
  border-color: var(--border-gold);
  background: linear-gradient(145deg, rgba(255,215,0,0.08), rgba(255,215,0,0.02));
  box-shadow: 0 0 30px rgba(255,215,0,0.06);
}
.stat-label {
  display: block;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-weight: 600;
}
.stat-value {
  font-family: 'Orbitron', monospace;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
}
.stat.highlight .stat-value {
  color: var(--gold);
  text-shadow: 0 0 12px var(--glow-gold);
}

/* ========== STAGE PROGRESS ========== */
.stage-progress {
  display: flex;
  align-items: center;
  gap: 0;
  width: 100%;
  justify-content: center;
}
.stage-pip {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 2px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: all 0.4s ease;
  position: relative;
}
.stage-pip .pip-num {
  font-family: 'Orbitron', monospace;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  line-height: 1;
}
.stage-pip .pip-reward {
  font-size: 0.55rem;
  font-weight: 600;
  color: var(--text-muted);
  opacity: 0.7;
  margin-top: 1px;
}
.stage-pip.active {
  border-color: var(--gold);
  background: linear-gradient(145deg, rgba(255,215,0,0.15), rgba(255,215,0,0.05));
  box-shadow: 0 0 20px var(--glow-gold), 0 0 40px rgba(255,215,0,0.1);
  transform: scale(1.1);
}
.stage-pip.active .pip-num { color: var(--gold); }
.stage-pip.active .pip-reward { color: var(--gold-light); opacity: 1; }

.stage-pip.completed {
  border-color: var(--emerald);
  background: linear-gradient(145deg, rgba(46,204,113,0.15), rgba(46,204,113,0.05));
  box-shadow: 0 0 15px var(--glow-emerald);
}
.stage-pip.completed .pip-num { color: var(--emerald); }
.stage-pip.completed .pip-reward { color: var(--emerald); opacity: 1; }
.stage-pip.completed::after {
  content: '✓';
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  background: var(--emerald);
  border-radius: 50%;
  font-size: 0.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
}

.stage-connector {
  width: 24px;
  height: 2px;
  background: var(--border-subtle);
  transition: background 0.4s;
}
.stage-connector.done {
  background: var(--emerald);
  box-shadow: 0 0 6px var(--glow-emerald);
}

/* ========== DANGER METER ========== */
.danger-meter {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 10px 16px;
}
.danger-label {
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  color: var(--text-secondary);
}
.danger-bar {
  flex: 1;
  height: 8px;
  background: rgba(255,255,255,0.06);
  border-radius: 10px;
  overflow: hidden;
}
.danger-fill {
  height: 100%;
  border-radius: 10px;
  background: linear-gradient(90deg, var(--emerald), #f39c12, var(--ruby));
  transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  width: 33%;
}
.danger-text {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  font-family: 'Orbitron', monospace;
  white-space: nowrap;
}

/* ========== GAME GRID ========== */
.grid-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  perspective: 800px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  width: 300px;
  max-width: 100%;
}

/* --- Cell --- */
.cell {
  aspect-ratio: 1;
  border-radius: var(--radius-md);
  background: var(--bg-cell);
  border: 2px solid var(--border-subtle);
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.25s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}
.cell::before {
  content: '';
  width: 58px;
  height: 58px;
  background: url('assets/chest-closed.svg') center/contain no-repeat;
  opacity: 0.6;
  transition: all 0.3s;
}
.cell:hover:not(.revealed):not(.disabled) {
  background: var(--bg-cell-hover);
  border-color: rgba(255,255,255,0.15);
  transform: translateY(-3px) scale(1.03);
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.cell:hover:not(.revealed):not(.disabled)::before {
  opacity: 0.85;
  transform: scale(1.05);
}
.cell:active:not(.revealed):not(.disabled) {
  transform: translateY(0) scale(0.97);
}

/* Shimmer on unrevealed cells */
.cell:not(.revealed)::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    transparent 40%,
    rgba(255,255,255,0.03) 50%,
    transparent 60%
  );
  animation: cellShimmer 3s ease-in-out infinite;
}
@keyframes cellShimmer {
  0%, 100% { transform: translateX(-30%) translateY(-30%); }
  50% { transform: translateX(30%) translateY(30%); }
}

/* --- Revealed states --- */
.cell.revealed {
  cursor: default;
  transform: rotateY(360deg);
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1), 
              background 0.3s, border-color 0.3s, box-shadow 0.3s;
}
.cell.revealed::before { content: none; }
.cell.revealed::after { display: none; }

.cell.revealed .cell-content {
  opacity: 1;
  transform: scale(1);
}
.cell-content {
  font-size: 2.2rem;
  opacity: 0;
  transform: scale(0);
  transition: all 0.35s 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  line-height: 1;
}
.cell-content .cell-icon {
  width: 54px;
  height: 54px;
  object-fit: contain;
  filter: drop-shadow(0 2px 6px rgba(0,0,0,0.3));
  pointer-events: none;
}
.cell-content .cell-label {
  font-size: 0.55rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-family: 'Noto Sans Thai', 'Inter', sans-serif;
}

/* Icon size tweaks per type */
.cell.bomb .cell-icon { filter: drop-shadow(0 0 8px rgba(231,76,60,0.5)); }
.cell.common .cell-icon { filter: drop-shadow(0 0 8px rgba(52,152,219,0.4)); }
.cell.gold .cell-icon {
  width: 60px;
  height: 60px;
  filter: drop-shadow(0 0 12px rgba(255,215,0,0.5));
}

/* Bomb */
.cell.bomb {
  background: linear-gradient(145deg, rgba(231,76,60,0.25), rgba(192,57,43,0.1));
  border-color: var(--ruby);
  box-shadow: 0 0 30px var(--glow-ruby), inset 0 0 30px rgba(231,76,60,0.08);
  animation: bombShake 0.5s ease;
}
.cell.bomb .cell-label { color: var(--ruby); }

@keyframes bombShake {
  0%, 100% { transform: translateX(0) rotateY(360deg); }
  20% { transform: translateX(-8px) rotateY(360deg); }
  40% { transform: translateX(8px) rotateY(360deg); }
  60% { transform: translateX(-5px) rotateY(360deg); }
  80% { transform: translateX(5px) rotateY(360deg); }
}

/* Common Treasure */
.cell.common {
  background: linear-gradient(145deg, rgba(52,152,219,0.2), rgba(41,128,185,0.08));
  border-color: var(--sapphire);
  box-shadow: 0 0 25px rgba(52,152,219,0.3), inset 0 0 20px rgba(52,152,219,0.05);
}
.cell.common .cell-label { color: var(--sapphire); }

/* Gold Treasure */
.cell.gold {
  background: linear-gradient(145deg, rgba(255,215,0,0.25), rgba(184,134,11,0.1));
  border-color: var(--gold);
  box-shadow: 0 0 30px var(--glow-gold), inset 0 0 30px rgba(255,215,0,0.05);
  animation: goldGlow 2s ease-in-out infinite;
}
.cell.gold .cell-label { color: var(--gold); }

@keyframes goldGlow {
  0%, 100% { box-shadow: 0 0 30px var(--glow-gold), inset 0 0 30px rgba(255,215,0,0.05); }
  50% { box-shadow: 0 0 50px var(--glow-gold), 0 0 80px rgba(255,215,0,0.15), inset 0 0 30px rgba(255,215,0,0.1); }
}

/* Disabled (after pick, other cells dim) */
.cell.disabled {
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
}
.cell.disabled.revealed { opacity: 1; }

/* ========== INSTRUCTION ========== */
.instruction {
  font-size: 0.9rem;
  color: var(--text-secondary);
  text-align: center;
  font-weight: 500;
  min-height: 1.6em;
  transition: var(--transition);
}
.instruction.treasure-found { color: var(--emerald); }
.instruction.gold-found { color: var(--gold); }
.instruction.bomb-hit { color: var(--ruby); }

/* ========== ACTION BUTTONS ========== */
.action-buttons {
  display: flex;
  gap: 12px;
  width: 100%;
  transition: var(--transition);
}
.action-buttons.hidden {
  display: none;
}

.btn {
  flex: 1;
  padding: 14px 20px;
  border: none;
  border-radius: var(--radius-md);
  font-family: 'Inter', sans-serif;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.25s ease;
  position: relative;
  overflow: hidden;
  letter-spacing: 0.5px;
}
.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255,255,255,0.1), transparent);
  pointer-events: none;
}

.btn-cashout {
  background: linear-gradient(135deg, var(--emerald), var(--emerald-dark));
  color: #fff;
  box-shadow: 0 4px 20px rgba(46,204,113,0.3);
}
.btn-cashout:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(46,204,113,0.4);
}
.btn-cashout:active { transform: translateY(0); }

.btn-continue {
  background: linear-gradient(135deg, #f39c12, #e67e22);
  color: #fff;
  box-shadow: 0 4px 20px rgba(243,156,18,0.3);
}
.btn-continue:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(243,156,18,0.4);
}
.btn-continue:active { transform: translateY(0); }

/* ========== OVERLAY ========== */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: overlayIn 0.4s ease;
}
.overlay.hidden { display: none; }

@keyframes overlayIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.overlay-content {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xl);
  padding: 40px 32px;
  text-align: center;
  max-width: 380px;
  width: 100%;
  animation: overlayContentIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: var(--shadow-lg);
}
@keyframes overlayContentIn {
  from { opacity: 0; transform: scale(0.85) translateY(30px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.overlay-icon {
  font-size: 4rem;
  margin-bottom: 16px;
  display: block;
  animation: iconBounce 0.6s 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
@keyframes iconBounce {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

.overlay-title {
  font-family: 'Orbitron', monospace;
  font-size: 1.5rem;
  font-weight: 900;
  margin-bottom: 8px;
  letter-spacing: 1px;
}
.overlay-title.win {
  background: linear-gradient(135deg, var(--gold-light), var(--gold));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.overlay-title.lose { color: var(--ruby); }

.overlay-subtitle {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 20px;
  line-height: 1.5;
}

.overlay-amount {
  font-family: 'Orbitron', monospace;
  font-size: 2.4rem;
  font-weight: 900;
  margin: 16px 0;
}
.overlay-amount.win {
  color: var(--gold);
  text-shadow: 0 0 30px var(--glow-gold);
}
.overlay-amount.lose {
  color: var(--ruby);
  text-shadow: 0 0 20px var(--glow-ruby);
}

.btn-play-again {
  margin-top: 20px;
  padding: 14px 40px;
  border: none;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--gold), var(--gold-dark));
  color: var(--bg-primary);
  font-family: 'Orbitron', monospace;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.25s ease;
  letter-spacing: 1px;
  box-shadow: 0 4px 20px rgba(255,215,0,0.3);
}
.btn-play-again:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(255,215,0,0.45);
}

/* ========== CONFETTI CANVAS ========== */
#confetti-canvas {
  position: fixed;
  inset: 0;
  z-index: 200;
  pointer-events: none;
}

/* ========== REVEAL ALL ANIMATION ========== */
.cell.reveal-all {
  transition-delay: var(--reveal-delay, 0s);
}

/* ========== RESPONSIVE ========== */
@media (max-width: 400px) {
  .game-container { padding: 16px 12px 32px; gap: 14px; }
  .game-title { font-size: 1.4rem; }
  .grid { width: 260px; gap: 10px; }
  .stat-value { font-size: 0.95rem; }
  .stage-pip { width: 42px; height: 42px; }
  .stage-pip .pip-num { font-size: 0.75rem; }
  .stage-connector { width: 16px; }
}

/* ========== EARNINGS POP ========== */
@keyframes earningsPop {
  0% { transform: scale(1); }
  50% { transform: scale(1.25); }
  100% { transform: scale(1); }
}
.earnings-pop {
  animation: earningsPop 0.4s ease;
}

/* ========== FLOATING REWARD ========== */
.floating-reward {
  position: absolute;
  pointer-events: none;
  font-family: 'Orbitron', monospace;
  font-size: 1.1rem;
  font-weight: 900;
  z-index: 50;
  animation: floatUp 1.2s ease-out forwards;
}
.floating-reward.common-float { color: var(--sapphire); text-shadow: 0 0 10px rgba(52,152,219,0.5); }
.floating-reward.gold-float { color: var(--gold); text-shadow: 0 0 15px var(--glow-gold); }

@keyframes floatUp {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-60px) scale(1.3); }
}

/* ========== GRID ENTER ANIMATION ========== */
.cell.entering {
  animation: cellEnter 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) both;
  animation-delay: var(--enter-delay, 0s);
}
@keyframes cellEnter {
  from { opacity: 0; transform: scale(0.5) rotateY(90deg); }
  to { opacity: 1; transform: scale(1) rotateY(0); }
}

/* ========== RULES BUTTON ========== */
.btn-rules {
  background: transparent;
  border: 1px solid var(--border-gold);
  color: var(--gold);
  font-family: 'Noto Sans Thai', 'Inter', sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 10px 28px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.25s ease;
  letter-spacing: 0.5px;
}
.btn-rules:hover {
  background: rgba(255, 215, 0, 0.08);
  border-color: var(--gold);
  box-shadow: 0 0 20px rgba(255,215,0,0.15);
  transform: translateY(-1px);
}

/* ========== RULES MODAL ========== */
.rules-content {
  max-height: 80vh;
  overflow-y: auto;
  text-align: left;
}
.rules-content .overlay-icon { text-align: center; }
.rules-content .overlay-title { text-align: center; margin-bottom: 20px; }

.rules-section {
  margin-bottom: 20px;
  padding: 16px;
  background: rgba(255,255,255,0.03);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}
.rules-section h3 {
  font-family: 'Noto Sans Thai', 'Inter', sans-serif;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--gold);
  margin-bottom: 10px;
}
.rules-section ul {
  list-style: none;
  padding: 0;
}
.rules-section ul li {
  font-family: 'Noto Sans Thai', 'Inter', sans-serif;
  font-size: 0.82rem;
  color: var(--text-secondary);
  padding: 6px 0;
  padding-left: 16px;
  position: relative;
  line-height: 1.6;
}
.rules-section ul li::before {
  content: '▸';
  position: absolute;
  left: 0;
  color: var(--gold);
  font-size: 0.7rem;
  top: 8px;
}
.rules-section ul li b {
  color: var(--text-primary);
}

/* Rules Table */
.rules-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-family: 'Noto Sans Thai', 'Inter', sans-serif;
}
.rules-table th {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 8px 6px;
  border-bottom: 1px solid var(--border-subtle);
  text-align: center;
  font-weight: 600;
}
.rules-table td {
  font-size: 0.8rem;
  color: var(--text-secondary);
  padding: 8px 6px;
  text-align: center;
  border-bottom: 1px solid rgba(255,255,255,0.03);
}
.rules-table tr.rules-highlight td {
  color: var(--gold);
  font-weight: 700;
  background: rgba(255,215,0,0.05);
}

.rules-content .btn-play-again {
  display: block;
  width: 100%;
  margin-top: 16px;
  text-align: center;
  font-family: 'Noto Sans Thai', 'Inter', sans-serif;
}
