:root {
  --bg: var(--tg-theme-bg-color, #f2f4f7);
  --bg2: var(--tg-theme-secondary-bg-color, #ffffff);
  --card: var(--tg-theme-section-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #111827);
  --hint: var(--tg-theme-hint-color, #6b7280);
  --link: var(--tg-theme-link-color, #2563eb);
  --accent: var(--tg-theme-button-color, #2563eb);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --sep: var(--tg-theme-section-separator-color, rgba(0,0,0,.06));
  --danger: #ef4444; --warn: #f59e0b; --ok: #10b981; --mastered: #8b5cf6;
  --radius: 20px; --radius-s: 14px;
  --shadow: 0 10px 30px rgba(0,0,0,.08), 0 2px 6px rgba(0,0,0,.04);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  background: var(--bg); color: var(--text);
  font-size: 16px; line-height: 1.4; overflow-x: hidden;
  -webkit-font-smoothing: antialiased; user-select: none; -webkit-user-select: none;
}
#app { min-height: 100vh; min-height: 100dvh; }
.screen { display: none; padding: calc(16px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); min-height: 100dvh; animation: fade .25s ease; }
.screen.active { display: flex; flex-direction: column; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin: 4px 0 2px; }
h2 { font-size: 17px; font-weight: 600; margin: 22px 0 10px; }
.hint { color: var(--hint); font-size: 14px; }
.muted { color: var(--hint); }
button { font: inherit; border: 0; cursor: pointer; }
.btn { display: block; width: 100%; padding: 16px; border-radius: var(--radius-s); background: var(--accent); color: var(--accent-text); font-weight: 600; font-size: 17px; transition: transform .08s, filter .15s; }
.btn:active { transform: scale(.985); filter: brightness(.95); }
.btn.secondary { background: var(--card); color: var(--text); box-shadow: inset 0 0 0 1px var(--sep); }
.btn.ghost { background: transparent; color: var(--link); }
.btn[disabled] { opacity: .5; pointer-events: none; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

/* ---------- loader ---------- */
.loader { margin: auto; display: flex; gap: 8px; }
.loader .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); animation: bounce 1s infinite ease-in-out; }
.loader .dot:nth-child(2) { animation-delay: .15s; } .loader .dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,80%,100% { transform: scale(.6); opacity: .4;} 40% { transform: scale(1); opacity: 1;} }

/* ---------- home ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; }
.streak { display: inline-flex; align-items: center; gap: 6px; background: var(--card); padding: 8px 12px; border-radius: 999px; font-weight: 600; box-shadow: var(--shadow); }
.streak .flame { font-size: 18px; filter: grayscale(1); opacity: .5; }
.streak.on .flame { filter: none; opacity: 1; }
.hero { margin-top: 18px; background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #7c3aed)); color: #fff; border-radius: var(--radius); padding: 22px 20px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.hero::after { content: ""; position: absolute; right: -40px; top: -40px; width: 160px; height: 160px; border-radius: 50%; background: rgba(255,255,255,.12); }
.hero .sub { opacity: .85; font-size: 14px; margin-bottom: 4px; }
.hero .title { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.hero .plan { margin-top: 18px; display: flex; gap: 18px; }
.hero .plan div b { display: block; font-size: 28px; font-weight: 700; line-height: 1; }
.hero .plan div span { font-size: 12px; opacity: .85; }
.hero .btn { margin-top: 18px; background: #fff; color: var(--accent); }
.progress-strip { margin-top: 14px; display: flex; height: 8px; border-radius: 6px; overflow: hidden; background: var(--sep); }
.progress-strip i { display: block; height: 100%; transition: width .5s ease; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 8px; font-size: 13px; color: var(--hint); }
.legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.c-mastered { background: var(--mastered);} .c-known { background: var(--ok);} .c-learning { background: var(--warn);} .c-new { background: var(--sep);}
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.tile { background: var(--card); border-radius: var(--radius-s); padding: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 4px; }
.tile .ico { font-size: 22px; }
.tile b { font-size: 15px; }
.tile span { font-size: 12px; color: var(--hint); }
.tile:active { transform: scale(.98); }
.topics { display: flex; flex-direction: column; gap: 8px; }
.topic { background: var(--card); border-radius: var(--radius-s); padding: 12px 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; }
.topic:active { transform: scale(.99); }
.topic .t { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.topic .t b { font-size: 15px; font-weight: 600; }
.topic .t span { font-size: 12px; color: var(--hint); white-space: nowrap; }
.bar { height: 6px; border-radius: 4px; background: var(--sep); overflow: hidden; display: flex; }
.bar i { height: 100%; display: block; }
.badge { display: inline-block; background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; }

/* ---------- session ---------- */
.session-top { display: flex; align-items: center; gap: 12px; }
.pbar { flex: 1; height: 6px; background: var(--sep); border-radius: 4px; overflow: hidden; }
.pbar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .35s ease; }
.counter { font-size: 13px; color: var(--hint); min-width: 44px; text-align: right; font-variant-numeric: tabular-nums; }
.topic-chip { align-self: flex-start; margin-top: 14px; font-size: 12px; color: var(--hint); background: var(--card); padding: 5px 10px; border-radius: 999px; }
.stage { flex: 1; display: flex; align-items: center; justify-content: center; perspective: 1400px; margin: 14px 0; min-height: 360px; }
/* Переворот: крутится каждая грань отдельно, без overflow:hidden на гранях —
   иначе WebView Telegram (iOS/Android) игнорирует backface-visibility и показывает зеркальный текст. */
.card { position: relative; width: 100%; max-width: 440px; min-height: 380px; transform-style: preserve-3d; -webkit-transform-style: preserve-3d; transition: transform .35s ease, opacity .3s ease; cursor: pointer; will-change: transform; }
.card.dragging { transition: none; }
.face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 26px 22px; display: flex; flex-direction: column; transition: transform .55s cubic-bezier(.2,.8,.2,1), visibility 0s linear .27s; transform-style: preserve-3d; -webkit-transform-style: preserve-3d; }
.face.front { transform: rotateY(0deg) translateZ(1px); z-index: 2; }
.face.back { transform: rotateY(-180deg); visibility: hidden; z-index: 1; }
.card.flipped .face.front { transform: rotateY(180deg); visibility: hidden; z-index: 1; }
.card.flipped .face.back { transform: rotateY(0deg) translateZ(1px); visibility: visible; z-index: 2; }
.face .text::-webkit-scrollbar { display: none; }
.face .label { font-size: 11px; letter-spacing: .12em; text-transform: uppercase; color: var(--hint); font-weight: 600; }
.face .text { flex: 1; display: flex; align-items: center; font-size: 19px; font-weight: 500; line-height: 1.38; letter-spacing: -.005em; overflow-y: auto; margin: 14px 0; white-space: pre-line; }
.face .text.long { font-size: 16px; align-items: flex-start; }
.face .text.answer { font-size: 20px; font-weight: 600; color: var(--accent); }
.face .foot { font-size: 13px; color: var(--hint); text-align: center; }
.stamp { position: absolute; top: 22px; font-size: 14px; font-weight: 800; letter-spacing: .1em; padding: 6px 12px; border: 3px solid; border-radius: 10px; opacity: 0; transform: rotate(-12deg); transition: opacity .15s; }
.stamp.easy { right: 20px; color: var(--ok); border-color: var(--ok); transform: rotate(12deg); }
.stamp.forgot { left: 20px; color: var(--danger); border-color: var(--danger); }
.grades { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; opacity: 0; transform: translateY(10px); transition: all .3s ease; pointer-events: none; }
.grades.show { opacity: 1; transform: none; pointer-events: auto; }
.grade { padding: 14px 8px; border-radius: var(--radius-s); background: var(--card); box-shadow: var(--shadow); font-weight: 600; font-size: 15px; display: flex; flex-direction: column; gap: 3px; align-items: center; color: var(--text); }
.grade small { font-weight: 500; font-size: 11px; color: var(--hint); }
.grade.g0 { box-shadow: var(--shadow), inset 0 -3px 0 var(--danger); }
.grade.g1 { box-shadow: var(--shadow), inset 0 -3px 0 var(--warn); }
.grade.g2 { box-shadow: var(--shadow), inset 0 -3px 0 var(--ok); }
.grade:active { transform: scale(.96); }
.tap-hint { text-align: center; color: var(--hint); font-size: 14px; height: 20px; }

/* ---------- quiz ---------- */
.q-card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px 20px; margin-top: 14px; font-size: 18px; font-weight: 500; line-height: 1.4; white-space: pre-line; }
.options { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.opt { text-align: left; background: var(--card); color: var(--text); border-radius: var(--radius-s); padding: 14px 16px; font-size: 15px; box-shadow: var(--shadow); display: flex; gap: 12px; align-items: flex-start; transition: background .15s, box-shadow .15s, opacity .15s, transform .1s; white-space: pre-line; }
.opt .k { flex: 0 0 26px; height: 26px; border-radius: 50%; background: var(--bg); display: inline-flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; color: var(--hint); }
.opt:active { transform: scale(.985); }
.opt.correct { background: color-mix(in srgb, var(--ok) 16%, var(--card)); box-shadow: inset 0 0 0 2px var(--ok); }
.opt.correct .k { background: var(--ok); color: #fff; }
.opt.wrong { background: color-mix(in srgb, var(--danger) 14%, var(--card)); box-shadow: inset 0 0 0 2px var(--danger); }
.opt.wrong .k { background: var(--danger); color: #fff; }
.opt.locked { pointer-events: none; }
.opt.dim { opacity: .55; }
.q-actions { margin-top: auto; padding-top: 16px; }

/* ---------- summary ---------- */
.summary { text-align: center; margin: auto 0; }
.summary .big { font-size: 64px; line-height: 1; margin: 10px 0; }
.summary h1 { font-size: 24px; }
.stats-row { display: flex; justify-content: center; gap: 24px; margin: 20px 0 28px; }
.stats-row div b { display: block; font-size: 26px; font-weight: 700; }
.stats-row div span { font-size: 12px; color: var(--hint); }

/* ---------- stats ---------- */
.days { display: grid; grid-template-columns: repeat(14, 1fr); gap: 5px; margin-top: 8px; }
.day { aspect-ratio: 1; border-radius: 6px; background: var(--sep); }
.day.l1 { background: color-mix(in srgb, var(--ok) 35%, var(--sep)); } .day.l2 { background: color-mix(in srgb, var(--ok) 65%, var(--sep)); } .day.l3 { background: var(--ok); }
.day.today { outline: 2px solid var(--accent); outline-offset: 1px; }
.section { background: var(--card); border-radius: var(--radius-s); padding: 14px; box-shadow: var(--shadow); }
.kv { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--sep); font-size: 15px; }
.kv:last-child { border-bottom: 0; }
.kv b { font-variant-numeric: tabular-nums; }
.link { color: var(--link); background: none; padding: 12px; font-size: 15px; width: 100%; }
.danger-link { color: var(--danger); }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translate(-50%, 20px); background: rgba(17,24,39,.92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0; transition: all .25s; pointer-events: none; z-index: 50; max-width: 90vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.empty { text-align: center; margin: auto 0; padding: 30px 10px; }
.empty .big { font-size: 56px; }

/* ---------- фолбэк тёмной темы вне Telegram (в Telegram цвета приходят из themeParams) ---------- */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #0f1216);
    --bg2: var(--tg-theme-secondary-bg-color, #181c22);
    --card: var(--tg-theme-section-bg-color, #1b2027);
    --text: var(--tg-theme-text-color, #eef1f5);
    --hint: var(--tg-theme-hint-color, #8b95a5);
    --link: var(--tg-theme-link-color, #6ea8ff);
    --accent: var(--tg-theme-button-color, #3b82f6);
    --sep: var(--tg-theme-section-separator-color, rgba(255,255,255,.08));
    --shadow: 0 10px 30px rgba(0,0,0,.35), 0 2px 6px rgba(0,0,0,.25);
  }
}
.topic, .tile { text-align: left; }
.hero .btn.more { background: rgba(255,255,255,.18); color: #fff; margin-top: 10px; padding: 12px; font-size: 15px; }

/* ---------- ввод ответа в тесте ---------- */
.opt.picked { box-shadow: inset 0 0 0 2px var(--accent); }
.opt.picked .k { background: var(--accent); color: var(--accent-text); }
.answer-box { margin-top: 14px; display: flex; flex-direction: column; gap: 6px; }
.ans-input { width: 100%; font: inherit; font-size: 22px; font-weight: 700; letter-spacing: .25em; text-align: center; padding: 14px; border-radius: var(--radius-s); border: 0; background: var(--card); color: var(--text); box-shadow: var(--shadow), inset 0 0 0 1px var(--sep); outline: none; }
.ans-input:focus { box-shadow: var(--shadow), inset 0 0 0 2px var(--accent); }
.ans-input.ok { box-shadow: var(--shadow), inset 0 0 0 2px var(--ok); color: var(--ok); }
.ans-input.bad { box-shadow: var(--shadow), inset 0 0 0 2px var(--danger); color: var(--danger); }
.ans-hint { font-size: 13px; color: var(--hint); text-align: center; min-height: 18px; }
.ok-text { color: var(--ok); } .bad-text { color: var(--danger); }
.q-card { font-size: 17px; }

/* ---------- без счётчиков ---------- */
.deck-progress { margin-top: 16px; display: flex; align-items: center; gap: 10px; font-size: 13px; opacity: .92; }
.dp-bar { flex: 1; height: 6px; border-radius: 4px; background: rgba(255,255,255,.25); overflow: hidden; }
.dp-bar i { display: block; height: 100%; background: #fff; border-radius: 4px; transition: width .5s ease; }
.hero-note { margin-top: 10px; font-size: 13px; opacity: .85; text-align: center; }
.end-btn { background: none; color: var(--hint); font-size: 14px; padding: 4px 0 4px 8px; }

/* ---------- всё на один экран: тест и карточки не прокручиваются ---------- */
:root { --app-h: 100dvh; }
#quiz.screen, #study.screen { min-height: var(--app-h); padding-bottom: calc(12px + var(--safe-bottom)); --fs: 1; }
#quiz .topic-chip, #study .topic-chip { margin-top: calc(10px * var(--fs)); }
#quiz .q-card { margin-top: calc(10px * var(--fs)); padding: calc(16px * var(--fs)) calc(18px * var(--fs)); font-size: calc(17px * var(--fs)); line-height: 1.32; }
#quiz .options { margin-top: calc(10px * var(--fs)); gap: calc(8px * var(--fs)); }
#quiz .opt { padding: calc(11px * var(--fs)) 14px; font-size: calc(15px * var(--fs)); line-height: 1.3; gap: 10px; }
#quiz .opt .k { flex-basis: calc(24px * var(--fs)); height: calc(24px * var(--fs)); font-size: calc(13px * var(--fs)); }
#quiz .answer-box { margin-top: calc(10px * var(--fs)); gap: 4px; }
#quiz .ans-input { padding: calc(9px * var(--fs)); font-size: calc(20px * var(--fs)); }
#quiz .ans-hint { font-size: calc(12.5px * var(--fs)); }
#quiz .q-actions { margin-top: auto; padding-top: calc(10px * var(--fs)); position: sticky; bottom: 0; padding-bottom: calc(8px + var(--safe-bottom)); margin-bottom: calc(-12px - var(--safe-bottom)); background: linear-gradient(to bottom, transparent, var(--bg) 35%); }
#quiz .btn { padding: calc(14px * var(--fs)); }
#study .stage { flex: 1; min-height: 0; margin: 10px 0; }
#study .card { height: calc(var(--app-h) - 230px); min-height: 260px; max-height: 520px; }
#study .face { padding: 22px 20px; }
#study .face .text { min-height: 0; overflow: hidden; font-size: calc(19px * var(--fs)); }
#study .face .text.answer { font-size: calc(20px * var(--fs)); }
#study .grades { flex: 0 0 auto; }
.topic-all { box-shadow: var(--shadow), inset 0 0 0 1.5px color-mix(in srgb, var(--accent) 45%, transparent); }
.ans-input::placeholder { letter-spacing: 0; font-weight: 500; font-size: 16px; color: var(--hint); }

/* ---------- каталог и тариф ---------- */
.tag { font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 999px; background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); white-space: nowrap; }
.tag.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
.tag.soon { background: var(--sep); color: var(--hint); }
.deck.disabled { opacity: .55; }
.linkbtn { background: none; color: var(--link); font-size: 14px; padding: 0; margin-bottom: 2px; }
.tile-pay { grid-column: 1 / -1; background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 14%, var(--card)), var(--card)); box-shadow: var(--shadow), inset 0 0 0 1.5px color-mix(in srgb, var(--accent) 45%, transparent); }
.note { margin-top: 12px; padding: 12px 14px; border-radius: var(--radius-s); background: color-mix(in srgb, var(--warn) 14%, var(--card)); font-size: 14px; }
.price { text-align: center; font-size: 40px; font-weight: 800; letter-spacing: -.02em; margin: 22px 0 14px; display: flex; flex-direction: column; align-items: center; }
.price small { font-size: 13px; font-weight: 500; color: var(--hint); letter-spacing: 0; margin-top: 2px; }
.hint a { color: var(--link); text-decoration: none; }

/* ---------- пейвол: промокод и почта ---------- */
.field { display: flex; gap: 8px; margin-top: 10px; }
.text-input { flex: 1; font: inherit; font-size: 15px; padding: 12px 14px; border-radius: var(--radius-s); border: 0; background: var(--card); color: var(--text); box-shadow: var(--shadow), inset 0 0 0 1px var(--sep); outline: none; min-width: 0; }
.text-input:focus { box-shadow: var(--shadow), inset 0 0 0 2px var(--accent); }
.btn.small { width: auto; padding: 12px 14px; font-size: 14px; flex: 0 0 auto; }
.price .old { color: var(--hint); font-weight: 500; font-size: 22px; margin-right: 8px; }
