:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f6);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #111214);
  --hint: var(--tg-theme-hint-color, #8a8d93);
  --accent: var(--tg-theme-button-color, #2f7cf6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, var(--accent));
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --ok: #2fa86b;
  --line: color-mix(in srgb, var(--hint) 22%, transparent);
  --soft: color-mix(in srgb, var(--accent) 12%, transparent);
  --radius: 16px;
  --tabbar: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #0f1012; --card: #1a1b1f; --text: #f2f3f5; --hint: #8b8e96;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--link); text-decoration: none; }

.screen {
  padding: 16px 16px calc(var(--tabbar) + var(--safe-bottom) + 24px);
  max-width: 640px; margin: 0 auto; min-height: 100vh;
}
.fade-in { animation: fade .22s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- заголовки ---------- */
.title { font-size: 28px; font-weight: 700; letter-spacing: -.02em; margin: 4px 4px 16px; }
.subtitle { color: var(--hint); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; margin: 22px 16px 8px; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.center { text-align: center; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

/* ---------- карточки и списки ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px; min-height: 48px;
  position: relative; width: 100%; background: none; border: 0; text-align: left;
}
.item + .item::before {
  content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 1px solid var(--line);
}
.item.tap:active { background: var(--soft); }
.item .grow { flex: 1; min-width: 0; }
.item .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { color: var(--hint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .value { color: var(--hint); font-variant-numeric: tabular-nums; white-space: nowrap; }
.item .chev { color: var(--hint); font-size: 18px; }
.empty { color: var(--hint); text-align: center; padding: 28px 16px; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 15px; overflow: hidden;
}
.avatar.lg { width: 84px; height: 84px; font-size: 32px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.place { width: 24px; text-align: center; color: var(--hint); font-variant-numeric: tabular-nums; font-size: 14px; }

/* ---------- профиль ---------- */
.hero { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 0 18px; }
.hero h1 { margin: 6px 0 0; font-size: 24px; font-weight: 700; letter-spacing: -.01em; text-align: center; }
.hero .tag { color: var(--hint); }
.badges { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin-top: 4px; }
.badge { background: var(--soft); color: var(--accent); border-radius: 999px; padding: 3px 10px; font-size: 13px; font-weight: 500; }
.badge.gold { background: color-mix(in srgb, #e3a008 16%, transparent); color: #c48a00; }
.badge.danger { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.motto { color: var(--hint); font-style: italic; text-align: center; margin-top: 2px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 14px 12px; }
.stat b { display: block; font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat span { color: var(--hint); font-size: 12px; }

.ach { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.ach div { aspect-ratio: 1; border-radius: 14px; display: grid; place-items: center; font-size: 24px; background: var(--bg); }
.ach .off { filter: grayscale(1); opacity: .3; }

/* ---------- кошелёк ---------- */
.balance { padding: 20px; }
.balance .big { font-size: 38px; font-weight: 700; letter-spacing: -.03em; font-variant-numeric: tabular-nums; }
.balance .cur { display: flex; gap: 18px; margin-top: 10px; color: var(--hint); font-variant-numeric: tabular-nums; }
.balance .cur b { color: var(--text); font-weight: 600; }
.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.action {
  background: var(--card); border: 0; border-radius: var(--radius); padding: 14px 4px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 12px;
}
.action i { font-style: normal; font-size: 22px; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: var(--soft); }
.action:active { transform: scale(.97); }
.action[disabled] { opacity: .5; }
.delta-plus { color: var(--ok); }
.delta-minus { color: var(--danger); }

/* ---------- формы ---------- */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; color: var(--hint); font-size: 13px; margin: 0 4px 6px; }
.input, .select, .textarea {
  width: 100%; border: 0; outline: none; background: var(--bg); border-radius: 12px; padding: 12px 14px;
  -webkit-appearance: none; appearance: none;
}
.card .input, .card .select, .card .textarea, .sheet .input, .sheet .select, .sheet .textarea { background: var(--bg); }
.textarea { min-height: 96px; resize: vertical; }
.input:focus, .textarea:focus { box-shadow: 0 0 0 2px var(--accent) inset; }
.inline { display: flex; gap: 8px; }
.inline > * { flex: 1; }

.btn {
  border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 600; width: 100%;
  background: var(--accent); color: var(--accent-text); transition: transform .08s, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn[disabled] { opacity: .45; }
.btn.ghost { background: var(--soft); color: var(--accent); }
.btn.danger { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.btn.sm { padding: 7px 12px; font-size: 13px; width: auto; border-radius: 10px; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

.seg { display: flex; background: var(--card); border-radius: 12px; padding: 3px; margin-bottom: 12px; overflow-x: auto; scrollbar-width: none; }
.seg::-webkit-scrollbar { display: none; }
.seg button { flex: 1 0 auto; border: 0; background: none; padding: 8px 12px; border-radius: 9px; color: var(--hint); font-weight: 500; font-size: 14px; white-space: nowrap; }
.seg button.on { background: var(--accent); color: var(--accent-text); }

.chips { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px 12px; padding: 0 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 0; background: var(--card); padding: 8px 14px; border-radius: 999px; font-size: 14px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.on { background: var(--accent); color: var(--accent-text); }

/* переключатель */
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background .2s; }
.switch span::after {
  content: ""; position: absolute; width: 27px; height: 27px; left: 2px; top: 2px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.18); transition: transform .2s;
}
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:disabled + span { opacity: .5; }
.num-input { width: 84px; text-align: right; padding: 8px 10px; }

/* ---------- графики ---------- */
.chart { width: 100%; height: 120px; display: block; }
.chart.tall { height: 180px; }
.bars rect { fill: color-mix(in srgb, var(--accent) 35%, transparent); }
.bars rect.last { fill: var(--accent); }
.line-path { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.line-area { fill: color-mix(in srgb, var(--accent) 14%, transparent); }

/* ---------- игры ---------- */
.games-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.game-tile {
  border: 0; background: var(--card); border-radius: var(--radius); padding: 16px; text-align: left;
  display: flex; flex-direction: column; gap: 4px; min-height: 112px;
}
.game-tile i { font-style: normal; font-size: 30px; margin-bottom: 4px; }
.game-tile b { font-size: 16px; }
.game-tile span { color: var(--hint); font-size: 13px; }
.game-tile:active { transform: scale(.98); }

.crash-stage { position: relative; height: 220px; border-radius: 14px; background: var(--bg); overflow: hidden; margin-bottom: 14px; }
.crash-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.crash-mult {
  position: absolute; inset: 0; display: grid; place-items: center; font-size: 52px; font-weight: 750;
  letter-spacing: -.03em; font-variant-numeric: tabular-nums;
}
.crash-mult.lost { color: var(--danger); }
.crash-mult.won { color: var(--ok); }

.reels { display: flex; justify-content: center; gap: 10px; margin: 8px 0 16px; }
.reel { width: 76px; height: 86px; border-radius: 16px; background: var(--bg); display: grid; place-items: center; font-size: 40px; }
.reel.spin { animation: spin .12s linear infinite; }
@keyframes spin { 0% { transform: translateY(-3px); } 50% { transform: translateY(3px); } 100% { transform: translateY(-3px); } }
.coin { width: 110px; height: 110px; margin: 6px auto 16px; border-radius: 50%; display: grid; place-items: center; font-size: 50px; background: var(--bg); }
.coin.flip { animation: flip .5s ease-in-out infinite; }
@keyframes flip { to { transform: rotateY(360deg); } }
.result { text-align: center; font-weight: 600; min-height: 22px; margin-bottom: 12px; }

.price-big { font-size: 34px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.book { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.book .side { background: var(--bg); border-radius: 12px; padding: 10px 12px; font-variant-numeric: tabular-nums; font-size: 14px; }
.book .side div { display: flex; justify-content: space-between; padding: 2px 0; }
.ask { color: var(--danger); }
.bid { color: var(--ok); }

/* ---------- нижняя панель ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; z-index: 10;
  height: calc(var(--tabbar) + var(--safe-bottom)); padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--card) 88%, transparent); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  flex: 1; border: 0; background: none; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; color: var(--hint); font-size: 11px; font-weight: 500;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--accent); }
.tabbar.hidden { display: none; }

/* ---------- шторка и уведомление ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); animation: fadebg .2s; }
@keyframes fadebg { from { opacity: 0; } }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 88vh; overflow-y: auto; background: var(--bg);
  border-radius: 20px 20px 0 0; padding: 8px 16px calc(20px + var(--safe-bottom)); animation: up .26s cubic-bezier(.2,.8,.2,1);
  max-width: 640px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(100%); } }
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 12px; }
.sheet h2 { margin: 4px 4px 14px; font-size: 20px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + var(--safe-bottom) + 14px); transform: translateX(-50%);
  background: rgba(30,30,32,.92); color: #fff; padding: 11px 16px; border-radius: 14px; z-index: 30;
  max-width: calc(100% - 32px); font-size: 14px; white-space: pre-line; text-align: center; animation: fade .2s;
}
.toast.err { background: color-mix(in srgb, var(--danger) 92%, black); }

.skeleton { height: 64px; border-radius: var(--radius); background: var(--card); margin-bottom: 12px; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
pre.text { white-space: pre-wrap; font: inherit; margin: 0; }

/* ---------- вкладка владельца ---------- */
pre.code {
  margin: 0; padding: 12px; border-radius: 12px; background: var(--bg); overflow-x: auto;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre; max-height: 60vh;
}
pre.code.wrap { white-space: pre-wrap; word-break: break-word; }
.textarea.mono, .input.mono { font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.kv { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 12px; }
.kv .stat b { font-size: 18px; }
.err-text { color: var(--danger); }
.tabbar button.owner-tab { display: none; }
.tabbar.owner button.owner-tab { display: flex; }
