/* ═══════════ Тема: берём цвета Telegram, у каждого есть запасной вариант ═══════════ */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg-soft: var(--tg-theme-secondary-bg-color, #f2f4f7);
  --text: var(--tg-theme-text-color, #1b1f27);
  --hint: var(--tg-theme-hint-color, #7a8190);
  --accent: var(--tg-theme-button-color, #3a7bd5);
  --on-accent: var(--tg-theme-button-text-color, #ffffff);
  --line: rgba(127, 127, 127, .22);
  --gold: #f5b301;
  --gold-soft: rgba(245, 179, 1, .14);
  --good: #2eaa6b;
  --bad: #e5484d;
  --radius: 18px;
  --shadow: 0 2px 10px rgba(0, 0, 0, .08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none;
  background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased;
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
b { font-weight: 650; }

#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; transition: filter .3s; }
#app.blurred { filter: blur(6px); pointer-events: none; }
.screen { display: flex; flex-direction: column; flex: 1; min-height: 0; }

/* ═══════════ Верхняя панель ═══════════ */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-shrink: 0;
  padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--bg); box-shadow: 0 1px 0 var(--line);
}
.topbar-left { display: flex; align-items: center; gap: 8px; }
.topbar-title { flex: 1; text-align: center; font-weight: 650; font-size: 16px; }
.topbar-spacer { width: 40px; }

.icon-btn {
  width: 40px; height: 40px; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%; background: var(--bg-soft); color: var(--text);
}
.icon-btn.small { width: 32px; height: 32px; font-size: 13px; }
.icon-btn:active { opacity: .6; }

.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 6px 11px; border-radius: 999px;
  background: var(--bg-soft); font-weight: 650; font-size: 14px;
}
.chip.streak.active { background: var(--gold-soft); color: #b77f00; }

.profile { display: flex; align-items: center; gap: 8px; padding: 0; min-width: 0; }
.level-pill { padding: 4px 10px; border-radius: 999px; background: var(--bg-soft); font-size: 13px; white-space: nowrap; }
.progress { width: 56px; height: 8px; border-radius: 99px; background: var(--bg-soft); overflow: hidden; }
.progress-fill { display: block; height: 100%; width: 0; border-radius: 99px; background: var(--accent); transition: width .4s ease; }
.progress-text { min-width: 36px; font-size: 11px; color: var(--hint); font-variant-numeric: tabular-nums; text-align: left; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; overflow: hidden; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-weight: 700;
  background: var(--accent); color: var(--on-accent);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ═══════════ Чат ═══════════ */
.chat {
  flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 8px; padding: 14px 12px 8px;
}
.msg {
  max-width: 84%; padding: 9px 13px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere;
  white-space: pre-wrap; animation: pop .2s ease;
}
.msg.user { align-self: flex-end; background: var(--accent); color: var(--on-accent); border-bottom-right-radius: 6px; }
.msg.bot { align-self: flex-start; background: var(--bg-soft); border-bottom-left-radius: 6px; }
.msg.system { align-self: center; max-width: 92%; text-align: center; font-size: 13px; color: var(--hint); background: transparent; }
.msg.photo { padding: 4px; }
.msg.photo img { display: block; max-width: 100%; max-height: 320px; border-radius: 14px; }
.msg a { color: var(--accent); }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.typing { display: inline-flex; gap: 4px; align-items: center; padding: 4px 2px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--hint); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* Приветствие */
.welcome {
  align-self: stretch; margin: auto 0; padding: 26px 18px; border-radius: 24px; text-align: center;
  background: var(--bg-soft); display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.welcome .big { font-size: 46px; line-height: 1; }
.welcome h2 { margin: 0; font-size: 19px; }
.welcome p { margin: 0 0 6px; color: var(--hint); max-width: 300px; }
.quick { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

/* Кнопки */
.btn {
  padding: 10px 16px; border-radius: 999px; font-weight: 600; font-size: 14px;
  background: var(--accent); color: var(--on-accent);
}
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; }
.btn.soft { background: var(--bg); color: var(--text); box-shadow: var(--shadow); }
.btn.gold { background: var(--gold); color: #2a2000; }

/* Карточка разбора */
.review {
  align-self: stretch; max-width: 100%; padding: 12px; border-radius: 22px; background: var(--bg-soft);
  display: flex; flex-direction: column; gap: 10px; animation: pop .25s ease;
}
.review img { width: 100%; border-radius: 14px; display: block; }
.score-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.score {
  display: inline-flex; align-items: baseline; gap: 2px; padding: 6px 14px; border-radius: 999px;
  background: var(--accent); color: var(--on-accent); font-weight: 700; font-size: 20px;
}
.score small { font-size: 13px; font-weight: 600; opacity: .85; }
.stats { font-size: 13px; color: var(--hint); }
.verdict { white-space: pre-wrap; padding: 0 2px; }
.review .btn { align-self: flex-start; }

/* События */
.event {
  align-self: stretch; text-align: center; padding: 20px 16px; border-radius: 22px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; animation: pop .4s cubic-bezier(.2, 1.3, .4, 1);
  background: var(--bg-soft);
}
.event .emoji { font-size: 48px; line-height: 1.1; }
.event .title { font-weight: 750; font-size: 17px; }
.event .text { color: var(--hint); font-size: 14px; max-width: 280px; }
.event.level_up { background: rgba(46, 170, 107, .14); } .event.level_up .title { color: var(--good); }
.event.streak_lost { background: rgba(229, 72, 77, .12); } .event.streak_lost .title { color: var(--bad); }
.event.streak_saved { background: rgba(58, 123, 213, .14); } .event.streak_saved .title { color: var(--accent); }
.event.premium { background: var(--gold-soft); } .event.premium .title { color: #b77f00; }

/* Paywall и задание */
.card {
  align-self: stretch; padding: 16px; border-radius: 22px; background: var(--gold-soft);
  display: flex; flex-direction: column; gap: 8px; text-align: center; align-items: center; animation: pop .2s ease;
}
.card h3 { margin: 0; font-size: 16px; } .card p { margin: 0; color: var(--hint); font-size: 14px; }

/* ═══════════ Ввод ═══════════ */
.composer {
  display: flex; align-items: center; gap: 8px; flex-shrink: 0;
  padding: 8px 12px; padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: var(--bg); box-shadow: 0 -1px 0 var(--line);
}
.text-input {
  flex: 1; min-width: 0; height: 42px; padding: 0 16px; border: 0; outline: 0; border-radius: 999px;
  background: var(--bg-soft); font-size: 15px;
}
.text-input::placeholder { color: var(--hint); }
.text-input:disabled { opacity: .6; }
.send-btn { background: var(--accent); color: var(--on-accent); }
.send-btn:disabled { opacity: .5; }

.preview-bar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--bg); box-shadow: 0 -1px 0 var(--line);
}
.preview-bar img { width: 52px; height: 52px; object-fit: cover; border-radius: 12px; }
.preview-note { flex: 1; font-size: 13px; color: var(--hint); }

/* ═══════════ Меню ═══════════ */
.backdrop {
  position: fixed; inset: 0; z-index: 25; background: rgba(0, 0, 0, .35); opacity: 0; pointer-events: none; transition: opacity .2s;
}
.backdrop.show { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: 80%; max-width: 330px;
  background: var(--bg); box-shadow: var(--shadow); transform: translateX(-102%); transition: transform .25s ease;
  padding: calc(18px + env(safe-area-inset-top)) 14px 18px; overflow-y: auto;
}
.drawer.open { transform: none; }
.drawer-nav { display: flex; flex-direction: column; gap: 8px; }
.drawer-title { font-size: 22px; font-weight: 750; margin: 4px 6px 10px; }
.drawer-item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; padding: 14px 16px;
  border-radius: 16px; background: var(--bg-soft); font-size: 15px; font-weight: 550;
}
.drawer-item span { font-size: 20px; }
.drawer-item:active { opacity: .7; }
.drawer-item.gold { background: var(--gold); color: #2a2000; font-weight: 700; }
.drawer-premium { margin-top: 18px; display: flex; flex-direction: column; gap: 8px; }
.perks { margin: 4px 0 0; padding: 12px 14px 12px 30px; border-radius: 16px; background: var(--gold-soft); font-size: 13px; color: var(--hint); }
.perks li { margin: 3px 0; }

/* ═══════════ Профиль (нижняя шторка) ═══════════
   Шторку убираем из потока, пока она закрыта, чтобы она не перекрывала страницу. */
.modal-backdrop { z-index: 40; }
.sheet {
  position: fixed; z-index: 41; left: 0; right: 0; bottom: 0; background: var(--bg);
  border-radius: 26px 26px 0 0; padding: 8px 18px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -6px 24px rgba(0, 0, 0, .15); transform: translateY(105%); transition: transform .3s cubic-bezier(.2, .9, .3, 1);
  visibility: hidden;
}
.sheet.show { transform: none; visibility: visible; }
.sheet-handle { width: 38px; height: 4px; border-radius: 4px; background: var(--line); margin: 0 auto 10px; }
.sheet-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.sheet-title { font-size: 18px; font-weight: 750; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; box-shadow: 0 1px 0 var(--line); }
.row:last-child { box-shadow: none; }
.row > span { color: var(--hint); }
.task-info { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.task-info b { font-weight: 500; word-break: break-word; }
.premium-note { font-size: 12px; color: var(--hint); margin-top: 8px; }

/* ═══════════ Рейтинг ═══════════ */
.lb-content { flex: 1; overflow-y: auto; padding: 14px 14px 24px; display: flex; flex-direction: column; gap: 12px; }
.lb-timer { align-self: center; font-size: 13px; color: var(--hint); padding: 6px 14px; border-radius: 999px; background: var(--bg-soft); }
.lb-card { border-radius: 22px; background: var(--bg-soft); overflow: hidden; }
.lb-line { display: grid; grid-template-columns: 44px 1fr auto; align-items: center; gap: 6px; padding: 13px 16px; box-shadow: 0 1px 0 var(--line); }
.lb-line:last-child { box-shadow: none; }
.lb-line.me { background: var(--gold-soft); }
.lb-rank { text-align: center; font-weight: 700; font-size: 17px; }
.lb-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-count { font-weight: 700; font-variant-numeric: tabular-nums; }
.lb-note { text-align: center; color: var(--hint); font-size: 14px; padding: 6px 12px; }

/* ═══════════ Приз ═══════════ */
.game-overlay {
  position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 24px; padding: 0 12px; background: rgba(0, 0, 0, .45); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px);
}
.game-title { font-size: 20px; font-weight: 750; text-align: center; color: #fff; text-shadow: 0 2px 8px rgba(0, 0, 0, .35); }
.cards { display: flex; gap: 10px; perspective: 1000px; justify-content: center; width: 100%; }
.prize-card { width: 108px; height: 156px; position: relative; transform-style: preserve-3d; transition: transform .9s cubic-bezier(.4, .2, .2, 1); }
.prize-card.flipped { transform: rotateY(180deg); }
.prize-card.winner { transform: rotateY(180deg) scale(1.12) translateY(-8px); z-index: 2; }
.prize-card.winner .face { box-shadow: 0 0 28px 6px rgba(245, 179, 1, .8); }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; border-radius: 18px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 8px; text-align: center;
  box-shadow: var(--shadow);
}
.face.back { background: linear-gradient(145deg, var(--accent), #6b5bd6); color: #fff; font-size: 46px; font-weight: 800; }
.face.front { background: var(--bg); transform: rotateY(180deg); font-size: 12px; font-weight: 650; line-height: 1.2; }
.face.front .emoji { font-size: 54px; line-height: 1; }

/* ═══════════ Прочее ═══════════ */
.toast {
  position: fixed; left: 50%; bottom: 96px; z-index: 1000; max-width: 84vw; padding: 10px 16px; border-radius: 14px; text-align: center;
  background: var(--text); color: var(--bg); font-size: 14px; opacity: 0; pointer-events: none;
  transform: translate(-50%, 16px); transition: all .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.confetti-layer { position: fixed; inset: 0; z-index: 9999; pointer-events: none; overflow: hidden; }
.confetti { position: absolute; bottom: -40px; animation: rise linear forwards; }
@keyframes rise { to { transform: translateY(-110vh) rotate(25deg) scale(1.4); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
