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

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-card: #16213e;
  --bg-card-hover: #1f3460;
  --accent: #e94560;
  --accent-glow: rgba(233, 69, 96, 0.4);
  --gold: #f5c518;
  --gold-glow: rgba(245, 197, 24, 0.4);
  --text-primary: #eee;
  --text-secondary: #999;
  --text-dim: #555;
  --border: #2a2a4a;
  --success: #27ae60;
  --radius: 12px;
  --radius-sm: 8px;
  --nav-height: 64px;
  --header-height: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tg-safe-top: 0px;
  --tg-safe-bottom: 0px;
  --tg-content-top: 0px;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

/* ─── Scrollbar ───────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ─── Modal ───────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(8px);
  padding: 24px;
}
.modal-overlay.hidden { display: none; }

.modal-box {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 24px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}
.modal-icon { font-size: 48px; margin-bottom: 16px; }
.modal-box h2 { font-size: 20px; margin-bottom: 12px; color: var(--gold); }
.modal-box p { font-size: 14px; color: var(--text-secondary); margin-bottom: 24px; line-height: 1.6; }

/* ─── Buttons ─────────────────────────────────────────────────────────── */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 32px;
  background: linear-gradient(135deg, var(--accent), #c0392b);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.2s;
  min-height: 44px;
}
.btn-primary:active {
  transform: scale(0.96);
  box-shadow: 0 0 20px var(--accent-glow);
}

.icon-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 20px;
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.icon-btn:active { background: var(--bg-card); }

.back-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  padding: 8px 16px;
  cursor: pointer;
  margin-bottom: 12px;
  transition: background 0.15s;
}
.back-btn:active { background: var(--bg-card); }

/* ─── Language Picker ─────────────────────────────────────────────────── */
.lang-picker {
  position: fixed;
  top: calc(var(--header-height) + var(--tg-content-top) + var(--tg-safe-top) + 4px);
  right: 12px;
  z-index: 1000;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}
.lang-picker.hidden { display: none; }
.lang-option {
  display: block;
  width: 100%;
  padding: 12px 20px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 15px;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
}
.lang-option:active { background: var(--bg-card); }
.lang-option + .lang-option { border-top: 1px solid var(--border); }

/* ─── Header ──────────────────────────────────────────────────────────── */
#app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  padding-top: calc(var(--tg-safe-top) + var(--tg-content-top));
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 16px;
  padding-right: 12px;
  z-index: 100;
  box-sizing: content-box;
}
.header-left { display: flex; align-items: center; gap: 8px; }
#header-logo { font-size: 24px; }
#header-title { font-size: 18px; font-weight: 700; background: linear-gradient(135deg, var(--gold), var(--accent)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }

.header-right { display: flex; align-items: center; gap: 10px; }
#balance-display {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-card);
  padding: 6px 14px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 15px;
  border: 1px solid var(--border);
}
.coin-icon { font-size: 16px; }
#balance-value { color: var(--gold); }

/* ─── Main Content ────────────────────────────────────────────────────── */
#main-content {
  position: fixed;
  top: calc(var(--header-height) + var(--tg-safe-top) + var(--tg-content-top));
  left: 0;
  right: 0;
  bottom: calc(var(--nav-height) + var(--tg-safe-bottom));
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
}

.tab-content { display: none; }
.tab-content.active { display: block; }

.section-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
  color: var(--text-primary);
}

/* ─── Games Grid ──────────────────────────────────────────────────────── */
.games-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.game-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 12px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.2s, box-shadow 0.2s;
  position: relative;
  overflow: hidden;
}
.game-card.playable:active {
  transform: scale(0.97);
  border-color: var(--accent);
  box-shadow: 0 0 20px var(--accent-glow);
}
.game-card.locked {
  opacity: 0.4;
  cursor: default;
  pointer-events: none;
}
.game-card-img { font-size: 48px; margin-bottom: 8px; }
.game-card-name { font-size: 13px; font-weight: 600; color: var(--text-secondary); }

/* ─── Earn ─────────────────────────────────────────────────────────────── */
.earn-container { display: flex; flex-direction: column; gap: 12px; }
.earn-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.earn-icon { font-size: 36px; }
.earn-label { flex: 1; font-size: 15px; font-weight: 500; }
.earn-reward { font-size: 14px; color: var(--gold); font-weight: 600; margin-right: 12px; }
.earn-btn { padding: 8px 20px; font-size: 14px; min-height: 36px; }

/* ─── Leaders ─────────────────────────────────────────────────────────── */
.leaders-tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.leader-tab {
  flex: 1;
  padding: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.leader-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.leaders-list { display: flex; flex-direction: column; gap: 6px; }
.leader-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.leader-rank {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  border-radius: 50%;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  flex-shrink: 0;
}
.leader-rank.top1 { background: var(--gold); color: #000; }
.leader-rank.top2 { background: #aaa; color: #000; }
.leader-rank.top3 { background: #cd7f32; color: #fff; }
.leader-name { flex: 1; font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leader-score { font-size: 14px; font-weight: 700; color: var(--gold); }

/* ─── Bottom Nav ──────────────────────────────────────────────────────── */
#bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  padding-bottom: var(--tg-safe-bottom);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 100;
  box-sizing: content-box;
}
.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
  padding: 8px 16px;
  transition: color 0.15s;
  min-height: 44px;
}
.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 22px; }
.nav-label { font-weight: 500; }

/* ─── Game Screen ─────────────────────────────────────────────────────── */
.game-screen {
  padding: 0 !important;
}
.game-screen .back-btn {
  margin: 12px 16px;
}

/* ─── Animations ──────────────────────────────────────────────────────── */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.fade-in {
  animation: fadeIn 0.3s ease forwards;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}
.pulse { animation: pulse 0.5s ease; }

@keyframes coinPop {
  0% { transform: scale(0.5); opacity: 0; }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); opacity: 1; }
}
.coin-pop { animation: coinPop 0.4s ease; }

/* ─── Toast ───────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  top: calc(var(--header-height) + var(--tg-safe-top) + var(--tg-content-top) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--success);
  color: #fff;
  padding: 10px 24px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  z-index: 5000;
  pointer-events: none;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 1.7s forwards;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(-10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}
@keyframes toastOut {
  from { opacity: 1; }
  to { opacity: 0; transform: translateX(-50%) translateY(-10px); }
}