/* Цвета берутся из темы Telegram (--tg-theme-*), вне Telegram - из запасной палитры. */
:root {
  --fb-bg: #f0f1f5;
  --fb-section: #ffffff;
  --fb-text: #10141a;
  --fb-hint: #7b8390;
  --fb-link: #2678e0;
  --fb-button: #2678e0;
  --fb-button-text: #ffffff;
  --fb-danger: #e0453a;
  --fb-separator: rgba(16, 20, 26, 0.08);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #0e1014;
    --fb-section: #191c22;
    --fb-text: #eceef2;
    --fb-hint: #8b929e;
    --fb-link: #6aa8ff;
    --fb-button: #3b87f6;
    --fb-danger: #ff6b61;
    --fb-separator: rgba(255, 255, 255, 0.08);
  }
}

:root {
  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg));
  --section: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-section)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --button: var(--tg-theme-button-color, var(--fb-button));
  --button-text: var(--tg-theme-button-text-color, var(--fb-button-text));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --section-title: var(--tg-theme-section-header-text-color, var(--hint));
  --separator: var(--tg-theme-section-separator-color, var(--fb-separator));
  --positive: #1f9d55;
  --gold: #d9a21b;
  --silver: #9ba4ae;
  --bronze: #b9763a;
  --radius: 14px;
  --safe-bottom: var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
  --tabbar-h: 58px;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

button { font: inherit; color: inherit; }
h1, h2, p { margin: 0; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

.i {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.num { font-variant-numeric: tabular-nums; }

/* ── Кнопки ─────────────────────────────────────────────── */

.button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 50px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-size: 16px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.button:active { opacity: 0.8; }
.button:disabled { opacity: 0.55; }
.button.secondary { background: var(--section); color: var(--link); }
.button.danger { background: var(--section); color: var(--danger); }

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--hint);
  cursor: pointer;
}
.icon-button:active { background: var(--section); }
.spinning .i { animation: spin 0.8s linear infinite; }

/* ── Экраны-состояния ───────────────────────────────────── */

.screen {
  min-height: 100vh;
  min-height: var(--tg-viewport-stable-height, 100vh);
  padding: 24px 20px calc(24px + var(--safe-bottom));
}
.screen.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
}
.screen.center .button { max-width: 320px; margin-top: 12px; }

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--separator);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.empty-icon {
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--section);
  color: var(--button);
}
.empty-icon .i { width: 30px; height: 30px; }
.empty-title { font-size: 20px; font-weight: 700; }
.empty-text { color: var(--hint); max-width: 300px; }

.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 48px 16px;
  color: var(--hint);
  text-align: center;
}
.state .i { width: 36px; height: 36px; stroke-width: 1.6; }
.state .button { max-width: 220px; height: 42px; font-size: 15px; }

/* ── Привязка ───────────────────────────────────────────── */

.link { display: flex; flex-direction: column; max-width: 420px; margin: 0 auto; padding-top: 40px; }
.link-mark {
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  margin: 0 auto 18px;
  border-radius: 18px;
  background: var(--button);
  color: var(--button-text);
}
.link-mark .i { width: 30px; height: 30px; }
.link-title { font-size: 22px; font-weight: 700; text-align: center; }
.link-text { margin-top: 8px; color: var(--hint); text-align: center; }
.link-steps {
  margin: 24px 0 16px;
  padding: 14px 16px 14px 34px;
  border-radius: var(--radius);
  background: var(--section);
}
.link-steps li + li { margin-top: 6px; }
.link-form { display: flex; flex-direction: column; gap: 10px; }
.code-input {
  width: 100%;
  height: 58px;
  border: 1px solid var(--separator);
  border-radius: 12px;
  background: var(--section);
  color: var(--text);
  font: 600 26px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.3em;
  text-align: center;
  text-transform: uppercase;
  outline: none;
}
.code-input::placeholder { color: var(--hint); opacity: 0.5; }
.code-input:focus { border-color: var(--button); }
.form-error { min-height: 20px; color: var(--danger); font-size: 14px; text-align: center; }
.link .button.secondary { margin-top: 6px; }

/* ── Каркас приложения ──────────────────────────────────── */

.content {
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
}

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--section);
  border-top: 1px solid var(--separator);
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: 0;
  background: transparent;
  color: var(--hint);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
}
.tab .i { width: 24px; height: 24px; }
.tab[aria-current="page"] { color: var(--button); }

.page-head { display: flex; align-items: center; justify-content: space-between; min-height: 44px; margin: 4px 4px 12px; }
.page-head h1 { font-size: 24px; font-weight: 700; letter-spacing: -0.01em; }

.section-title {
  margin: 22px 16px 8px;
  color: var(--section-title);
  font-size: 13px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.hint { margin: 10px 16px 0; color: var(--hint); font-size: 13px; }
.hint.center { text-align: center; }

/* ── Главная ────────────────────────────────────────────── */

.hello { display: flex; align-items: center; gap: 12px; margin: 4px 0 14px; }
.avatar {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--button);
  color: var(--button-text);
  font-size: 16px;
  font-weight: 600;
}
.avatar.large { width: 76px; height: 76px; font-size: 28px; }
.hello-text { flex: 1; min-width: 0; }
.hello-name { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hello-sub { color: var(--hint); font-size: 13px; }

.balance {
  padding: 18px 18px 16px;
  border-radius: var(--radius);
  background: var(--section);
}
.balance-label, .balance-bank { display: flex; align-items: center; gap: 6px; color: var(--hint); font-size: 14px; }
.balance-label .i, .balance-bank .i { width: 16px; height: 16px; }
.balance-amount {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
  margin: 6px 0 12px;
  font-size: 36px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.02em;
  word-break: break-all;
}
.balance-cur { color: var(--hint); font-size: 20px; font-weight: 600; letter-spacing: 0; }
.balance-bank { padding-top: 12px; border-top: 1px solid var(--separator); }
.balance-hidden { display: flex; margin-left: auto; }

.ranks { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.rank {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--section);
  text-align: left;
  cursor: pointer;
}
.rank:active { opacity: 0.75; }
.rank-place { font-size: 20px; font-weight: 700; }
.rank-label { color: var(--hint); font-size: 12px; }

/* ── Списки ─────────────────────────────────────────────── */

.list {
  margin: 0;
  padding: 0;
  list-style: none;
  border-radius: var(--radius);
  background: var(--section);
  overflow: hidden;
}
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 10px 16px;
  position: relative;
}
.list > li { position: relative; }
.list > li + li::before {
  content: "";
  z-index: 1;
  position: absolute;
  top: 0;
  right: 0;
  left: 16px;
  border-top: 1px solid var(--separator);
}
.row-button { width: 100%; border: 0; background: transparent; text-align: left; cursor: pointer; }
.row-button:active { background: var(--bg); }
.row-icon { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--bg); color: var(--hint); }
.row-icon .i { width: 18px; height: 18px; }
.row-body { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.row-title { font-size: 15px; }
.row-sub { color: var(--hint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-end { margin-left: auto; color: var(--hint); }
.row-end .i { width: 18px; height: 18px; }

.stat-values { display: flex; flex-direction: column; align-items: flex-end; margin-left: auto; text-align: right; }
.stat-main { font-weight: 600; }
.stat-sub { color: var(--hint); font-size: 13px; }

/* ── Рейтинг ────────────────────────────────────────────── */

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  padding: 2px;
  margin-bottom: 12px;
  border-radius: 10px;
  background: var(--section);
}
.segmented button {
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.segmented button[aria-selected="true"] { background: var(--button); color: var(--button-text); }

.prize-fund { display: flex; align-items: center; gap: 6px; margin: 0 4px 10px; color: var(--hint); font-size: 14px; }
.prize-fund .i { width: 16px; height: 16px; }

.place {
  display: grid;
  place-items: center;
  flex: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  color: var(--hint);
  font-size: 13px;
  font-weight: 700;
}
.place-1, .place-2, .place-3 { color: #fff; }
.place-1 { background: var(--gold); }
.place-2 { background: var(--silver); }
.place-3 { background: var(--bronze); }

.top-name { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; }
.top-name-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-tag {
  flex: none;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--button);
  color: var(--button-text);
  font-size: 11px;
  font-weight: 600;
}
.top-value { display: flex; flex-direction: column; align-items: flex-end; flex: none; text-align: right; font-weight: 600; }
.top-prize { color: var(--positive); font-size: 12px; font-weight: 500; }

.me-bar {
  position: sticky;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 10px);
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding: 12px 16px;
  border: 1px solid var(--separator);
  border-radius: 12px;
  background: var(--section);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
}
.me-bar-label { color: var(--hint); }
.me-bar-value { margin-left: auto; font-weight: 600; }

/* ── История ────────────────────────────────────────────── */

.tx-icon { display: grid; place-items: center; flex: none; width: 36px; height: 36px; border-radius: 50%; }
.tx-icon .i { width: 18px; height: 18px; }
.tx-icon.in { background: rgba(31, 157, 85, 0.13); color: var(--positive); }
.tx-icon.out { background: var(--bg); color: var(--hint); }
.tx-amount { flex: none; font-weight: 600; }
.tx-amount.in { color: var(--positive); }

/* ── Профиль ────────────────────────────────────────────── */

.profile-head { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 16px 0 4px; text-align: center; }
.profile-head .avatar { margin-bottom: 8px; }
.profile-name { font-size: 22px; font-weight: 700; }
.profile-nick { color: var(--hint); }

.achievements { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.achievement {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 10px;
  align-items: center;
  padding: 12px;
  border-radius: 12px;
  background: var(--section);
}
.achievement-icon { grid-row: span 2; display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: var(--bg); }
.achievement-value { font-weight: 600; }
.achievement-label { color: var(--hint); font-size: 12px; }
.achievement.xp .achievement-icon { color: var(--button); }
.achievement.gold .achievement-icon { color: var(--gold); }
.achievement.silver .achievement-icon { color: var(--silver); }
.achievement.bronze .achievement-icon { color: var(--bronze); }

.profile-actions { margin-top: 24px; }

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner, .spinning .i { animation-duration: 2s; }
}
