:root {
  --bg:        var(--tg-theme-bg-color, #0b1020);
  --bg2:       var(--tg-theme-secondary-bg-color, #161b2e);
  --bg3:       #1f2640;
  --fg:        var(--tg-theme-text-color, #f1f5f9);
  --muted:     var(--tg-theme-hint-color, #94a3b8);
  --link:      var(--tg-theme-link-color, #60a5fa);
  --accent:    var(--tg-theme-button-color, #6366f1);
  --accent2:   #8b5cf6;
  --accent-fg: var(--tg-theme-button-text-color, #ffffff);
  --danger:    #ef4444;
  --warn:      #f59e0b;
  --ok:        #10b981;
  --gold:      #fbbf24;
  --radius:    18px;
  --pad:       16px;
  --safe-top:  env(safe-area-inset-top, 0);
  --safe-bot:  env(safe-area-inset-bottom, 0);
  --nav-h:     64px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display",
               "Helvetica Neue", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
  padding-top: var(--safe-top);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

/* === Topbar === */
.topbar {
  padding: 10px var(--pad) 8px;
  background: linear-gradient(180deg, var(--bg2) 0%, transparent 100%);
  flex-shrink: 0;
}
.user { display: flex; align-items: center; gap: 12px; }
.user img {
  width: 44px; height: 44px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg2);
  border: 2px solid var(--accent);
}
.user img:not([src]), .user img[src=""] {
  display: inline-block;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
}
.user-info { flex: 1; min-width: 0; }
.user-name {
  font-size: 16px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-meta {
  display: flex; gap: 10px; font-size: 12px; color: var(--muted);
  margin-top: 2px;
  align-items: center;
}
.user-meta .lv {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff;
  padding: 2px 8px; border-radius: 8px; font-weight: 700;
  font-size: 11px;
}
.user-meta .coin::before { content: "🪙 "; }
.user-meta .coin {
  background: rgba(251,191,36,0.15);
  color: var(--gold);
  padding: 2px 8px; border-radius: 8px; font-weight: 700;
  font-size: 11px;
}
.xp-bar {
  margin-top: 6px;
  height: 4px;
  background: rgba(255,255,255,0.1);
  border-radius: 4px;
  overflow: hidden;
}
.xp-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--gold));
  width: 0%;
  transition: width .5s ease;
}

/* === Screens === */
#screens {
  flex: 1;
  position: relative;
  overflow: hidden;
}
.screen {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: var(--pad) var(--pad) calc(var(--nav-h) + var(--safe-bot) + 12px);
  display: none;
  -webkit-overflow-scrolling: touch;
  animation: screenIn .25s ease;
}
.screen.active { display: block; }
@keyframes screenIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

#screen-game {
  /* Игровой экран — без нижнего бара */
  padding-bottom: calc(var(--pad) + var(--safe-bot));
}

.screen-header {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 16px;
}
.screen-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
  flex: 1;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.screen-header .balance {
  background: rgba(251,191,36,0.15);
  color: var(--gold);
  padding: 8px 14px; border-radius: 14px;
  font-weight: 700;
}
.coin-icon { margin-right: 4px; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--bg2);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  font-weight: 600;
  flex-shrink: 0;
  transition: transform .15s, background .2s;
}
.icon-btn:active { transform: scale(0.92); background: var(--accent); }

/* === Главная: hero + сетка === */
.hero-title {
  font-size: 32px;
  font-weight: 800;
  text-align: center;
  margin: 16px 0 22px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6, #ec4899);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  letter-spacing: -0.5px;
}

.game-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}
.game-card {
  position: relative;
  height: 110px;
  border-radius: var(--radius);
  background: var(--bg2);
  overflow: hidden;
  cursor: pointer;
  transition: transform .2s, box-shadow .2s;
  display: flex;
  align-items: center;
  padding: 0 18px;
  gap: 16px;
}
.game-card:active {
  transform: scale(0.98);
}
.game-card .gcard-bg {
  position: absolute; inset: 0;
  background-size: cover;
  background-position: center;
  opacity: 0.85;
}
.game-card::after {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(90deg, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.15) 100%);
}
.game-card .gcard-emoji {
  position: relative;
  z-index: 1;
  font-size: 48px;
  filter: drop-shadow(0 4px 10px rgba(0,0,0,0.5));
  flex-shrink: 0;
}
.game-card .gcard-info {
  position: relative;
  z-index: 1;
  flex: 1;
  min-width: 0;
}
.game-card .gcard-info .gcard-name {
  font-weight: 800;
  font-size: 19px;
  margin-bottom: 4px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}
.game-card .gcard-info .gcard-best {
  font-size: 13px;
  color: rgba(255,255,255,0.85);
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}
.game-card .gcard-arrow {
  position: relative;
  z-index: 1;
  font-size: 20px;
  opacity: 0.7;
}

/* === Bottom nav (5 разделов) === */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: var(--bg2);
  border-top: 1px solid rgba(255,255,255,0.06);
  padding-bottom: var(--safe-bot);
  z-index: 50;
}
.bottom-nav.hidden { display: none; }
.nav-tab {
  height: var(--nav-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 600;
  font-style: normal;
  position: relative;
  transition: color .2s;
}
.nav-tab span { font-size: 22px; line-height: 1; }
.nav-tab i { font-style: normal; }
.nav-tab.active {
  color: var(--accent);
}
.nav-tab.active::before {
  content: "";
  position: absolute;
  top: 0;
  left: 25%; right: 25%;
  height: 3px;
  background: var(--accent);
  border-radius: 0 0 3px 3px;
}
.nav-tab:active span { transform: scale(0.85); }

/* === Экран игры === */
.game-header {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 10px;
}
.game-header .game-title {
  flex: 1;
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.game-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}
.game-stats > div {
  background: var(--bg2);
  border-radius: 12px;
  padding: 8px 10px;
  text-align: center;
}
.game-stats .stats-label {
  display: block;
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.game-stats b { font-size: 18px; }

.game-area {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg2);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  min-height: 260px;
  padding: 6px;
}
.game-area canvas {
  display: block;
  max-width: 100%;
  height: auto;
  touch-action: none;
}

.game-controls {
  margin-top: 8px;
  display: flex; flex-wrap: wrap;
  gap: 8px; justify-content: center;
  align-items: center;
}
.game-controls .level-select {
  background: var(--bg2);
  border-radius: 12px;
  padding: 8px 12px;
  color: var(--fg);
  font-size: 13px;
  font-weight: 600;
}
.touch-hint {
  text-align: center;
  width: 100%;
  font-size: 12px;
  color: var(--muted);
  padding: 4px 0;
}

/* === Tabs === */
.tabs {
  display: flex; gap: 6px;
  margin-bottom: 14px;
  background: var(--bg2);
  padding: 4px;
  border-radius: 12px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex-shrink: 0;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
  transition: background .2s, color .2s;
}
.tab.active {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff;
}

/* === Лидерборд === */
.lb-list { display: flex; flex-direction: column; gap: 8px; }
.lb-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg2);
  padding: 10px 12px;
  border-radius: 14px;
}
.lb-row.me { outline: 2px solid var(--accent); }
.lb-row .rank {
  font-weight: 700; font-size: 18px;
  width: 32px; text-align: center;
}
.lb-row .rank.top1 { color: var(--gold); }
.lb-row .rank.top2 { color: #d1d5db; }
.lb-row .rank.top3 { color: #d97706; }
.lb-row img {
  width: 36px; height: 36px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg);
}
.lb-row .name { flex: 1; font-weight: 600; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-row .pts { font-weight: 700; font-size: 16px; }

/* === Магазин === */
.shop-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.shop-card {
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 12px;
  display: flex; flex-direction: column;
  gap: 6px;
}
.shop-card .preview {
  height: 80px;
  border-radius: 12px;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 4px;
  overflow: hidden;
  position: relative;
}
.shop-card .name { font-weight: 600; font-size: 14px; }
.shop-card .price {
  display: flex; justify-content: space-between; align-items: center;
  margin-top: auto;
}
.shop-card .price b { color: var(--gold); }
.shop-card button {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--accent-fg);
  padding: 6px 12px;
  border-radius: 10px;
  font-size: 13px; font-weight: 600;
}
.shop-card button:disabled {
  background: var(--bg);
  color: var(--muted);
}
.shop-card.owned { outline: 2px solid var(--ok); }

/* === Достижения === */
.ach-list { display: flex; flex-direction: column; gap: 8px; }
.ach-row {
  background: var(--bg2);
  padding: 12px;
  border-radius: 14px;
  display: flex; align-items: center; gap: 12px;
}
.ach-row .icon {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  filter: grayscale(1);
  flex-shrink: 0;
}
.ach-row.earned .icon { filter: none; background: var(--gold); color: #000; }
.ach-row .name { font-weight: 600; }
.ach-row .desc { font-size: 12px; color: var(--muted); }
.ach-row .reward {
  margin-left: auto; font-weight: 700; color: var(--gold);
  flex-shrink: 0;
}

/* === Настройки === */
.settings-list {
  display: flex; flex-direction: column; gap: 8px;
}
.setting-row {
  background: var(--bg2);
  padding: 14px 16px;
  border-radius: 14px;
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 500;
}
.setting-row select {
  background: var(--bg);
  color: var(--fg);
  border: none;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
}
.setting-row input[type=checkbox] {
  width: 44px; height: 26px;
  appearance: none;
  background: var(--bg);
  border-radius: 14px;
  position: relative;
  transition: background .2s;
  cursor: pointer;
}
.setting-row input[type=checkbox]::before {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  background: white;
  border-radius: 50%;
  transition: left .2s;
}
.setting-row input[type=checkbox]:checked { background: var(--accent); }
.setting-row input[type=checkbox]:checked::before { left: 21px; }
.about { text-align: center; color: var(--muted); padding: 24px 0 12px; }
.about .muted { font-size: 12px; }

/* === Toast === */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-bot) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  background: var(--bg3);
  border: 1px solid var(--accent);
  color: var(--fg);
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 100;
  pointer-events: none;
  transition: transform .3s ease;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* === Модалка === */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card {
  background: linear-gradient(180deg, var(--bg2) 0%, var(--bg3) 100%);
  border-radius: var(--radius);
  padding: 24px 22px;
  max-width: 380px; width: 100%;
  text-align: center;
  animation: modalIn .35s cubic-bezier(0.16, 1, 0.3, 1);
  position: relative;
  overflow: hidden;
}
@keyframes modalIn {
  from { transform: scale(0.85) translateY(10px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}
.modal-emoji {
  font-size: 56px;
  margin-bottom: 6px;
  animation: bounceIn .6s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes bounceIn {
  0%   { transform: scale(0); }
  60%  { transform: scale(1.15); }
  100% { transform: scale(1); }
}
.modal-card h2 { margin: 0 0 12px; font-size: 22px; }
.modal-text {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
  margin: 0 0 18px;
}
.end-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}
.end-stats > div {
  background: rgba(255,255,255,0.05);
  padding: 10px;
  border-radius: 10px;
}
.end-stats span { display: block; font-size: 11px; color: var(--muted); }
.end-stats b { font-size: 20px; }
.end-ach {
  margin-bottom: 16px;
  font-size: 14px;
  color: var(--gold);
}
.end-ach .ach-line { padding: 4px 0; }
.modal-buttons { display: flex; gap: 10px; }
.modal-buttons button {
  flex: 1;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--accent-fg);
  padding: 12px;
  border-radius: 12px;
  font-weight: 600; font-size: 15px;
  transition: transform .15s;
}
.modal-buttons button:active { transform: scale(0.96); }
.modal-buttons button.secondary {
  background: var(--bg);
  color: var(--fg);
}

/* === Level-up === */
.level-up-modal .modal-card {
  background: linear-gradient(180deg, #1e1b4b 0%, #4c1d95 100%);
}
.level-up-num {
  font-size: 64px;
  font-weight: 900;
  margin: 4px 0 8px;
  background: linear-gradient(135deg, var(--gold), #f97316);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 16px rgba(251,191,36,0.4));
}
.confetti-burst {
  position: absolute; inset: 0;
  pointer-events: none;
}
.confetti-piece {
  position: absolute;
  width: 8px; height: 14px;
  border-radius: 2px;
  animation: confettiFall 1.6s linear forwards;
}
@keyframes confettiFall {
  0%   { transform: translate3d(var(--cx,0), -10px, 0) rotate(0); opacity: 1; }
  100% { transform: translate3d(calc(var(--cx,0) + var(--dx,0)), 100vh, 0) rotate(var(--rot,360deg)); opacity: 0; }
}

.muted { color: var(--muted); }
.scroll-x { overflow-x: auto; }
