/* Стиль в духе Quizlet. В Telegram цвета подстраиваются под тему пользователя. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #282e3e);
  --hint: var(--tg-theme-hint-color, #586380);
  --link: var(--tg-theme-link-color, #4255ff);
  --accent: var(--tg-theme-button-color, #4255ff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --page: color-mix(in srgb, var(--text) 3%, var(--bg));     /* фон страницы под плитками */
  --line: color-mix(in srgb, var(--text) 9%, var(--bg));     /* рамки */
  --track: color-mix(in srgb, var(--text) 14%, var(--bg));   /* пустой прогресс */
  --soft: color-mix(in srgb, var(--text) 5%, var(--bg));
  --accent-soft: color-mix(in srgb, var(--accent) 12%, var(--bg));
  --ok: #59c68a;
  --ok-strong: #2b7d5b;
  --ok-line: #18ae79;
  --ok-bg: color-mix(in srgb, #18ae79 11%, var(--bg));
  --danger: #ff725b;
  --danger-strong: #e0503a;
  --bad-bg: color-mix(in srgb, #ff725b 11%, var(--bg));
  --orange: #ff983a;
  --yellow: #ffcd1f;
  --radius: 12px;
  --shadow: 0 2px 10px color-mix(in srgb, var(--text) 7%, transparent);
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) { --bg: #0a092d; --text: #f6f7fb; --hint: #a8b1d0; --link: #8a95ff; --accent: #4255ff; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh; overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; }
#app { max-width: 640px; margin: 0 auto; padding: 16px 20px calc(110px + env(safe-area-inset-bottom)); }

h1 { font-size: 26px; font-weight: 700; margin: 6px 0 6px; line-height: 1.25; }
h2 { font-size: 16px; font-weight: 700; margin: 28px 0 12px; }
p.sub { margin: 0 0 18px; color: var(--hint); font-size: 15px; }
.muted { color: var(--hint); font-size: 15px; }
.big { font-size: 52px; line-height: 1.1; }
.list { display: flex; flex-direction: column; gap: 12px; }
.spacer { flex: 1; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%;
  border: 0; border-radius: var(--radius); padding: 15px 16px; font-weight: 600;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
.btn svg { width: 20px; height: 20px; }
.btn:disabled { opacity: .5; }
.btn.ghost { background: var(--bg); color: var(--text); border: 2px solid var(--line); }
.btn.danger { background: transparent; color: var(--danger-strong); font-weight: 500; }
.btn-row { display: flex; flex-direction: column; gap: 10px; margin-top: 22px; }
.fixed-bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg); border-top: 2px solid var(--line);
}
.fixed-bottom > * { max-width: 600px; margin: 0 auto; }
body.tg-main .fixed-bottom { display: none; } /* в Telegram используем родную MainButton */
.continue-bar { display: flex; align-items: center; gap: 12px; }
.continue-bar span { flex: 1; color: var(--hint); font-size: 14px; }
.continue-bar .btn { width: auto; padding: 13px 26px; }
@media (hover: none) { .continue-bar span { visibility: hidden; } }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; padding: 8px; border-radius: 8px; cursor: pointer; color: var(--hint);
}
.icon-btn:active { background: var(--soft); }
.icon-btn svg { width: 26px; height: 26px; }
.icon-btn.small { padding: 4px; }
.icon-btn.small svg { width: 20px; height: 20px; }
.round-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; border-radius: 50%;
  border: 2px solid var(--line); background: var(--bg); color: var(--text); cursor: pointer;
}
.round-btn svg { width: 22px; height: 22px; }
.round-btn:disabled { opacity: .35; }
.back { display: inline-flex; align-items: center; gap: 2px; color: var(--hint); text-decoration: none; font-size: 15px; margin: 0 0 6px -6px; }
.back svg { width: 20px; height: 20px; }

/* ===== Главная ===== */
.brand { display: flex; align-items: center; gap: 6px; font-weight: 800; font-size: 15px; letter-spacing: .02em; color: var(--accent); margin-bottom: 2px; }
.brand svg { width: 20px; height: 20px; color: var(--yellow); fill: var(--yellow); }
.hero {
  display: flex; align-items: center; gap: 16px; width: 100%; text-align: left; cursor: pointer; margin-top: 14px;
  border: 0; border-radius: 16px; padding: 18px; color: #fff;
  background: linear-gradient(135deg, #4255ff, #7b5cff);
  box-shadow: 0 8px 24px color-mix(in srgb, #4255ff 30%, transparent);
}
.hero-ico { flex: none; width: 52px; height: 52px; border-radius: 14px; background: rgba(255,255,255,.18); display: flex; align-items: center; justify-content: center; }
.hero-ico svg { width: 30px; height: 30px; color: var(--yellow); }
.hero b { display: block; font-size: 18px; margin-bottom: 2px; }
.hero span span { font-size: 14px; opacity: .9; }
.hero:active { transform: scale(.99); }

.set-tile {
  display: block; width: 100%; text-align: left; cursor: pointer;
  background: var(--bg); border: 2px solid var(--line); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow);
}
.set-tile:active { border-color: var(--accent); }
.set-title { font-weight: 700; font-size: 18px; margin-bottom: 8px; }
.set-meta { display: flex; align-items: center; gap: 10px; }
.pill-badge { background: var(--soft); border-radius: 999px; padding: 2px 10px; font-size: 13px; font-weight: 600; color: var(--hint); }
.bar { height: 6px; background: var(--track); border-radius: 3px; overflow: hidden; margin-top: 12px; }
.bar > i { display: block; height: 100%; width: 0; background: var(--ok); border-radius: 3px; transition: width .4s; }
.empty { text-align: center; padding: 40px 8px; }

/* ===== Создание ===== */
.upload {
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; cursor: pointer;
  padding: 34px 16px; border-radius: 16px; border: 2px dashed var(--track); background: var(--page); transition: border-color .15s, background .15s;
}
.upload.over, .upload:active { border-color: var(--accent); background: var(--accent-soft); }
.upload.has { border-style: solid; border-color: var(--accent); background: var(--accent-soft); }
.upload-ico { width: 56px; height: 56px; border-radius: 16px; background: var(--bg); display: flex; align-items: center; justify-content: center; color: var(--accent); box-shadow: var(--shadow); margin-bottom: 6px; }
.upload-ico svg { width: 28px; height: 28px; }
.upload-title { font-weight: 700; font-size: 17px; word-break: break-word; }
.upload input { display: none; }
.or { display: flex; align-items: center; gap: 12px; color: var(--hint); font-size: 14px; margin: 18px 0; }
.or::before, .or::after { content: ""; flex: 1; height: 2px; background: var(--line); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 2px solid var(--line); background: var(--bg); border-radius: 999px;
  padding: 8px 18px; font-size: 15px; font-weight: 600; cursor: pointer;
}
.chip.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

input[type=text], textarea {
  width: 100%; border: 2px solid var(--line); border-radius: var(--radius); padding: 12px 14px; font: inherit; font-size: 16px;
  background: var(--bg); color: var(--text); outline: none;
}
textarea { min-height: 110px; resize: vertical; }
input:focus, textarea:focus { border-color: var(--accent); }
.field-label { display: block; font-size: 14px; font-weight: 600; color: var(--hint); margin: 12px 0 6px; }
.notice { font-size: 14px; padding: 12px 14px; border-radius: var(--radius); background: var(--soft); color: var(--hint); margin-top: 18px; }

.loading { text-align: center; padding: 80px 8px; }
.magic {
  width: 76px; height: 76px; margin: 0 auto 18px; border-radius: 22px; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #4255ff, #7b5cff); color: var(--yellow); animation: pulse 1.6s ease-in-out infinite;
}
.magic svg { width: 40px; height: 40px; }
@keyframes pulse { 50% { transform: scale(1.08) rotate(6deg); } }
.loading-title { margin: 0 0 16px; font-size: 19px; }
.loading-bar { max-width: 280px; margin: 0 auto 14px; height: 8px; border-radius: 4px; }
.loading-bar > i { background: var(--accent); transition: width .3s linear; }

/* ===== Модуль ===== */
.set-head { display: flex; align-items: flex-start; gap: 4px; }
.set-head h1 { flex: 1; }
.set-head .icon-btn { margin-top: 8px; }

.preview { margin: 4px 0 6px; }
.preview .flip-inner { min-height: 220px; }
.preview .face { background: var(--bg); }
.preview-nav { display: flex; align-items: center; justify-content: center; gap: 22px; margin-top: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }

.mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }
.mode-tile {
  display: flex; align-items: center; gap: 12px; text-align: left; cursor: pointer;
  border: 2px solid var(--line); background: var(--bg); border-radius: var(--radius); padding: 14px; font-weight: 700;
  box-shadow: var(--shadow);
}
.mode-tile svg { width: 26px; height: 26px; color: var(--accent); flex: none; }
.mode-tile:active { border-color: var(--accent); }

.progress-rows { border: 2px solid var(--line); border-radius: var(--radius); padding: 4px 16px; }
.prow { display: flex; align-items: center; gap: 10px; padding: 10px 0; }
.prow + .prow { border-top: 2px solid var(--line); }
.prow b { margin-left: auto; }
.dot { width: 12px; height: 12px; border-radius: 50%; }
.dot.grey { background: var(--track); } .dot.orange { background: var(--orange); } .dot.green { background: var(--ok); }

.term-row { position: relative; border: 2px solid var(--line); border-radius: var(--radius); padding: 14px 44px 14px 16px; background: var(--bg); }
.term-q { font-weight: 600; padding-bottom: 10px; margin-bottom: 10px; border-bottom: 2px solid var(--line); }
.term-a { font-size: 16px; }
.term-edit { position: absolute; top: 8px; right: 6px; }
.add-card { margin-top: 14px; }

/* ===== Общая шапка тренировки ===== */
.st-head { display: flex; align-items: center; gap: 4px; margin: -6px -8px 14px; min-height: 44px; }
.st-center { flex: 1; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.mode-btn svg:first-child { color: var(--accent); }
.mode-btn svg:last-child { width: 22px; height: 22px; color: var(--text); }

/* Прогресс «Заучивания»: кружок со счётчиком едет по 4 сегментам */
.lp { position: relative; height: 44px; margin: 0 0 30px; }
.lp-track { position: absolute; left: 4px; right: 22px; top: 50%; transform: translateY(-50%); display: flex; gap: 8px; }
.lp-seg { flex: 1; height: 12px; border-radius: 6px; background: var(--track); overflow: hidden; }
.lp-seg > i { display: block; height: 100%; width: 0; background: var(--ok); border-radius: 6px; transition: width .5s ease; }
.lp-cur, .lp-total {
  position: absolute; top: 0; width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 17px;
}
.lp-cur { left: 0; background: var(--ok-strong); color: #fff; box-shadow: 0 0 0 3px var(--bg); transition: left .5s ease; z-index: 1; }
.lp-total { right: 0; background: var(--track); color: var(--text); }
.lp-total.done { background: var(--ok-strong); color: #fff; }

#stage { animation: slide .28s ease-out; }
@keyframes slide { from { transform: translateX(14px); opacity: 0; } }

.q-label { display: flex; align-items: center; gap: 6px; color: var(--hint); font-weight: 600; font-size: 16px; }
.q-text { font-size: 24px; line-height: 1.35; margin: 26px 0 48px; word-wrap: break-word; }
.a-label { color: var(--hint); font-weight: 600; font-size: 16px; margin-bottom: 18px; }
.a-label.ok { color: var(--ok-line); }
.a-label.bad { color: var(--danger-strong); }

.answers { display: grid; gap: 14px; }
.answers.years { grid-template-columns: 1fr 1fr; }
.ans {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 62px; text-align: left; cursor: pointer;
  background: var(--bg); border: 2px solid var(--line); border-radius: var(--radius); padding: 16px 20px;
  font-size: 18px; line-height: 1.45; transition: border-color .15s, background .15s, opacity .15s;
}
@media (hover: hover) { .ans:not(:disabled):hover { border-color: var(--track); } }
.ans:not(:disabled):active { border-color: var(--accent); }
.answers.years .ans { justify-content: center; font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.ans .mark { flex: none; width: 22px; height: 22px; }
.ans .mark:empty { display: none; }
.ans.picked { border-color: var(--accent); background: var(--accent-soft); }
.ans.right { border-color: var(--ok-line); background: var(--ok-bg); }
.ans.right .mark, .ans.missed .mark { color: var(--ok-line); }
.ans.missed { border-color: var(--ok-line); border-style: dashed; }
.ans.wrong { border-color: var(--danger); background: var(--bad-bg); }
.ans.wrong .mark { color: var(--danger); }
.ans.dim { opacity: .45; }
.ans:disabled { cursor: default; }
.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

.dont-know { text-align: right; margin-top: 18px; }
.dont-know button { background: none; border: 0; color: var(--link); font-weight: 600; font-size: 16px; cursor: pointer; padding: 6px 0; }
.explain { margin-top: 18px; padding: 14px 16px; border-radius: var(--radius); background: var(--soft); font-size: 16px; }

.checkpoint { text-align: center; }
.checkpoint .list { text-align: left; margin-top: 20px; }
.cp-row { display: flex; gap: 12px; align-items: flex-start; border: 2px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.cp-row .q { font-weight: 600; }
.cp-mark { flex: none; width: 26px; height: 26px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; margin-top: 1px; }
.cp-mark svg { width: 16px; height: 16px; }
.cp-mark.ok { background: var(--ok-line); } .cp-mark.bad { background: var(--danger); }

/* ===== Переворачивающиеся карточки ===== */
.flip { perspective: 1200px; cursor: pointer; touch-action: pan-y; user-select: none; -webkit-user-select: none; transition: transform .22s ease, opacity .22s ease; }
.flip-inner { position: relative; min-height: 340px; transition: transform .45s ease; transform-style: preserve-3d; }
.flip.on .flip-inner { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border: 2px solid var(--line); border-radius: 16px; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 44px 22px;
  box-shadow: 0 4px 18px color-mix(in srgb, var(--text) 9%, transparent); transition: border-color .15s;
}
.face.back { transform: rotateY(180deg); }
.face .q-label { position: absolute; top: 14px; left: 18px; }
.face-text { font-size: 22px; line-height: 1.4; word-wrap: break-word; overflow-wrap: anywhere; }
.face-note { margin-top: 14px; }
.face .tap { position: absolute; bottom: 14px; color: var(--hint); font-size: 13px; }
.flip[data-hint="know"] .face { border-color: var(--ok-line); }
.flip[data-hint="still"] .face { border-color: var(--orange); }

.fc-bar { height: 4px; background: var(--track); border-radius: 2px; margin: -4px 0 16px; overflow: hidden; }
.fc-bar > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.fc-counters { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; font-size: 14px; }
.fc-bubble { display: inline-flex; align-items: center; justify-content: center; min-width: 34px; height: 30px; padding: 0 10px; border-radius: 15px; font-weight: 700; font-size: 15px; border: 2px solid; }
.fc-bubble.orange { color: var(--orange); border-color: var(--orange); background: color-mix(in srgb, var(--orange) 10%, var(--bg)); }
.fc-bubble.green { color: var(--ok-line); border-color: var(--ok-line); background: var(--ok-bg); }
.fc-actions { display: flex; align-items: center; justify-content: center; gap: 28px; margin-top: 22px; }
.round-btn.big { width: 64px; height: 64px; }
.round-btn.big svg { width: 28px; height: 28px; }
.round-btn.bad { color: var(--orange); border-color: color-mix(in srgb, var(--orange) 45%, var(--bg)); }
.round-btn.ok { color: var(--ok-line); border-color: color-mix(in srgb, var(--ok-line) 45%, var(--bg)); }
.fc-summary { display: flex; justify-content: center; gap: 24px; margin-top: 12px; }
.fc-summary div { display: flex; align-items: center; gap: 8px; font-weight: 600; }

/* ===== Итоги ===== */
.score { text-align: center; padding: 14px 0 4px; }
.score h1 { margin-top: 12px; }
.ring {
  --p: 0; width: 120px; height: 120px; border-radius: 50%; margin: 0 auto; flex: none;
  background: conic-gradient(var(--ok-line) calc(var(--p) * 1%), var(--track) 0);
  display: flex; align-items: center; justify-content: center;
}
.ring span { width: 94px; height: 94px; border-radius: 50%; background: var(--bg); display: flex; align-items: center; justify-content: center; font-size: 26px; font-weight: 800; }
.mistake { border: 2px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.mistake .q { font-weight: 600; padding-bottom: 8px; margin-bottom: 8px; border-bottom: 2px solid var(--line); }
.mistake .a { font-size: 16px; }

/* ===== Тест ===== */
.test-list { display: flex; flex-direction: column; gap: 18px; }
.tq { border: 2px solid var(--line); border-radius: 16px; padding: 18px; background: var(--bg); box-shadow: var(--shadow); scroll-margin-top: 16px; }
.tq-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.tq-text { font-size: 21px; line-height: 1.4; margin-bottom: 26px; }
.tq .ans { font-size: 17px; min-height: 56px; padding: 14px 16px; }
.tq.answered { border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.tq.is-ok { border-color: color-mix(in srgb, var(--ok-line) 45%, var(--line)); }
.tq.is-bad { border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
.test-end { text-align: center; padding: 26px 8px 8px; }
.test-end .btn { margin-top: 14px; }
.test-result { display: flex; align-items: center; gap: 22px; padding: 8px 0; }
.res-row { display: flex; justify-content: space-between; gap: 18px; padding: 8px 14px; border-radius: 10px; margin-top: 6px; font-weight: 600; }
.res-row.ok { background: var(--ok-bg); color: var(--ok-line); }
.res-row.bad { background: var(--bad-bg); color: var(--danger-strong); }
@media (max-width: 400px) { .test-result { flex-direction: column; text-align: center; } }

/* ===== Подбор ===== */
.match-intro { text-align: center; padding: 50px 8px; }
.timer { font-size: 18px; }
.match-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.tile {
  display: flex; align-items: center; justify-content: center; text-align: center; min-height: 110px; padding: 10px; cursor: pointer;
  border: 2px solid var(--line); border-radius: var(--radius); background: var(--bg); box-shadow: var(--shadow);
  font-size: 14px; line-height: 1.3; transition: transform .2s, opacity .25s, border-color .15s, background .15s;
}
.tile span { display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.tile.q { font-weight: 700; }
.tile.sel { border-color: var(--accent); background: var(--accent-soft); }
.tile.ok { border-color: var(--ok-line); background: var(--ok-bg); }
.tile.bad { border-color: var(--danger); background: var(--bad-bg); }
.tile.gone { opacity: 0; transform: scale(.85); pointer-events: none; }

/* ===== Шторка, меню, всплывашки ===== */
.overlay { position: fixed; inset: 0; background: rgba(10, 9, 45, .45); z-index: 20; animation: fade .2s; }
.overlay.clear { background: transparent; }
@keyframes fade { from { opacity: 0; } }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-width: 640px; margin: 0 auto; max-height: 90vh; overflow: auto;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  animation: up .25s ease-out;
}
@keyframes up { from { transform: translateY(100%); } }
.sheet h3 { margin: 0 0 12px; font-size: 20px; }
.toggle-row { display: flex; align-items: center; gap: 12px; padding: 14px 0; border-bottom: 2px solid var(--line); cursor: pointer; }
.toggle-row span { flex: 1; }
.toggle-row input { display: none; }
.switch { width: 46px; height: 28px; border-radius: 14px; background: var(--track); position: relative; transition: background .2s; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: left .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.toggle-row input:checked + .switch { background: var(--accent); }
.toggle-row input:checked + .switch::after { left: 21px; }
.menu {
  position: absolute; z-index: 21; min-width: 220px; background: var(--bg); border: 2px solid var(--line);
  border-radius: 12px; padding: 6px; box-shadow: 0 8px 24px rgba(0,0,0,.12); animation: fade .15s;
}
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; background: none; border: 0; padding: 10px 12px; border-radius: 8px; cursor: pointer; text-align: left; font-weight: 600; }
.menu button svg { width: 22px; height: 22px; color: var(--accent); }
.menu button.on { background: var(--soft); }
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(96px + env(safe-area-inset-bottom)); max-width: 560px; margin: 0 auto;
  background: #282e3e; color: #fff; padding: 12px 14px; border-radius: 10px; font-size: 15px; z-index: 30;
}
