:root {
  --bg: #f6f3ec;
  --surface: #fffdf8;
  --surface-2: #efeae0;
  --ink: #1d2230;
  --ink-2: #4a5163;
  --ink-3: #7b8192;
  --line: #e2dccf;
  --accent: #1f5c7a;
  --accent-ink: #ffffff;
  --accent-soft: #dde9ee;
  --good: #2f7a4f;
  --good-soft: #e1f0e6;
  --warn: #a8661b;
  --warn-soft: #f7ead6;
  --bad: #a83a32;
  --bad-soft: #f6e0dd;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(29, 34, 48, .06), 0 4px 16px rgba(29, 34, 48, .05);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tabbar-h: 62px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171d; --surface: #1c2028; --surface-2: #252a34; --ink: #e9e6df; --ink-2: #b9bcc6; --ink-3: #868b98;
    --line: #2e3440; --accent: #6fb1d1; --accent-ink: #0f1a20; --accent-soft: #1e3340;
    --good: #6cc08f; --good-soft: #1d3326; --warn: #e0a85c; --warn-soft: #3a2d1b; --bad: #e88a80; --bad-soft: #3d2220;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--ink); font: 16px/1.5 var(--sans);
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
}
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.45rem; letter-spacing: -.01em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
small, .muted { color: var(--ink-3); }
.muted-2 { color: var(--ink-2); }

/* каркас */
#app { max-width: 720px; margin: 0 auto; padding: 16px 16px 0; }
.topbar { display: flex; align-items: center; gap: 10px; margin: 4px 0 14px; }
.topbar .back { font-size: 1.4rem; line-height: 1; padding: 6px 8px 6px 0; color: var(--ink-2); }
.topbar h1 { margin: 0; flex: 1; font-size: 1.25rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.card.flat { box-shadow: none; }
.card.soft { background: var(--surface-2); border-color: transparent; box-shadow: none; }
.card h2 { margin-bottom: .6rem; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.grow { flex: 1; min-width: 0; }
.center { text-align: center; }
.hidden { display: none !important; }
hr { border: none; border-top: 1px solid var(--line); margin: 14px 0; }

/* кнопки и поля */
button, .btn {
  font: inherit; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 11px 16px; border-radius: 12px; cursor: pointer; min-height: 44px; display: inline-flex;
  align-items: center; justify-content: center; gap: 6px; text-align: center;
}
button:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); padding-left: 8px; padding-right: 8px; }
.btn-small { min-height: 34px; padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.btn-block { width: 100%; }
.btn-danger { color: var(--bad); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > button { flex: 1 1 auto; }

input[type=text], input[type=password], input[type=number], select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); width: 100%;
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
}
textarea { min-height: 120px; resize: vertical; line-height: 1.5; }
textarea.answer { font-size: 1.02rem; min-height: 140px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label { display: block; font-size: .9rem; color: var(--ink-2); margin-bottom: 4px; }
.field + .field { margin-top: 12px; }
.check { display: flex; gap: 8px; align-items: center; font-size: .92rem; color: var(--ink-2); }
.check input { width: 18px; height: 18px; }

/* таб-бар */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: var(--surface);
  border-top: 1px solid var(--line); display: flex; justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom); height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--ink-3); font-size: .72rem; }
.tabbar a .ico { font-size: 1.25rem; line-height: 1; }
.tabbar a.active { color: var(--accent); font-weight: 600; }
.tabbar .badge { position: absolute; margin-left: 22px; margin-top: -26px; background: var(--bad); color: #fff;
  font-size: .65rem; border-radius: 10px; padding: 1px 5px; }

/* чипы, метки */
.chip { display: inline-block; font-size: .75rem; padding: 2px 8px; border-radius: 20px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.chip.good { background: var(--good-soft); color: var(--good); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.dots { letter-spacing: 1px; color: var(--accent); font-size: .8rem; }

/* прогресс */
.bar { height: 6px; border-radius: 6px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 6px; }
.score { font-weight: 700; font-size: 1.6rem; line-height: 1; font-variant-numeric: tabular-nums; }
.score.good { color: var(--good); } .score.warn { color: var(--warn); } .score.bad { color: var(--bad); }
.breakdown { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: .88rem; margin-top: 8px; }
.breakdown .bar { grid-column: 1 / -1; margin-bottom: 4px; }

/* шаги статьи */
.stepper { display: flex; gap: 6px; margin-bottom: 14px; }
.stepper button { flex: 1; min-height: 38px; font-size: .85rem; padding: 6px 4px; border-radius: 10px; color: var(--ink-3); }
.stepper button.done { color: var(--good); }
.stepper button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

/* исходный текст */
.source { font-family: var(--serif); font-size: 1.14rem; line-height: 1.65; color: var(--ink); }
.source .w { cursor: pointer; border-radius: 4px; }
.source .w:active, .source .w.sel { background: var(--accent-soft); }
.source .w.known { text-decoration: underline dotted var(--accent); text-underline-offset: 3px; }
.source .para-gap { display: block; height: .7em; }
.prev-context { font-family: var(--serif); color: var(--ink-3); font-size: .95rem; margin-bottom: 10px; border-left: 3px solid var(--line); padding-left: 10px; }

/* всплывающий глоссарий */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 40; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; background: var(--surface);
  border-radius: 18px 18px 0 0; padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgba(0,0,0,.15);
  max-width: 720px; margin: 0 auto; max-height: 75vh; overflow: auto; }
.sheet .term { font-family: var(--serif); font-size: 1.3rem; font-weight: 600; }

/* обратная связь */
.feedback .praise { color: var(--good); }
.hint { background: var(--warn-soft); color: var(--ink); border-radius: 12px; padding: 12px 14px; margin: 8px 0; }
.hint b { color: var(--warn); }
.err { border-left: 3px solid var(--bad); padding: 6px 0 6px 12px; margin: 10px 0; }
.err .cat { font-size: .75rem; color: var(--bad); text-transform: uppercase; letter-spacing: .03em; }
.err .pair { font-size: .95rem; }
.err .pair s { color: var(--bad); text-decoration-thickness: 1px; }
.err .pair ins { color: var(--good); text-decoration: none; font-weight: 600; }
.reference { background: var(--good-soft); border-radius: 12px; padding: 12px 14px; }
.improved { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; font-family: var(--serif); font-size: 1.05rem; }
details { margin: 8px 0; }
details > summary { cursor: pointer; color: var(--accent); font-size: .95rem; padding: 6px 0; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "▸ "; }
details[open] > summary::before { content: "▾ "; }

/* дискуссия */
.bubble { border-radius: 16px; padding: 12px 14px; margin: 10px 0; max-width: 92%; }
.bubble.q { background: var(--surface); border: 1px solid var(--line); font-family: var(--serif); font-size: 1.06rem; }
.bubble.a { background: var(--accent-soft); margin-left: auto; }
.bubble .who { font-family: var(--sans); font-size: .75rem; color: var(--ink-3); margin-bottom: 4px; display: flex; justify-content: space-between; gap: 8px; }

/* голос */
.voice-bar { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; align-items: center; }
.voice-bar button { min-height: 38px; padding: 6px 12px; font-size: .9rem; border-radius: 20px; }
.voice-bar button.rec { background: var(--bad); color: #fff; border-color: var(--bad); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .65; } }
.voice-note { font-size: .8rem; color: var(--ink-3); }

/* списки */
.list-item { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: none; }
.list-item .title { font-weight: 600; }
.vocab-term { font-family: var(--serif); font-weight: 600; font-size: 1.05rem; }

/* карточки повторения */
.flash { text-align: center; padding: 28px 16px; }
.flash .front { font-family: var(--serif); font-size: 1.6rem; font-weight: 600; margin-bottom: 6px; }
.flash .ctx { font-family: var(--serif); color: var(--ink-3); font-size: .95rem; margin-top: 10px; }
.flash .back { font-size: 1.2rem; margin-top: 14px; }
.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 12px; }
.rate button { flex-direction: column; font-size: .85rem; padding: 8px 4px; gap: 0; }
.rate button small { font-size: .7rem; }
.rate .r1 { color: var(--bad); } .rate .r2 { color: var(--warn); } .rate .r3 { color: var(--good); } .rate .r4 { color: var(--accent); }

/* тест */
.mcq { padding: 12px 0; border-bottom: 1px solid var(--line); }
.mcq .q { font-family: var(--serif); font-size: 1.05rem; margin-bottom: 8px; }
.mcq .opts { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.mcq .opts button { min-height: 40px; font-size: .92rem; padding: 6px 8px; }
.mcq .opts button.picked { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* графики */
.spark { width: 100%; height: 70px; display: block; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 12px; }
.stat .v { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { font-size: .8rem; color: var(--ink-3); }
.hbar { display: grid; grid-template-columns: minmax(0, 1fr) 40px; gap: 4px 10px; align-items: center; font-size: .88rem; }
.hbar .bar { grid-column: 1 / -1; margin-bottom: 6px; }
.hbar .bar > i { background: var(--bad); opacity: .7; }

/* тосты и загрузка */
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 20px + env(safe-area-inset-bottom));
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 60; max-width: 90vw; font-size: .92rem;
  box-shadow: 0 6px 24px rgba(0,0,0,.2); }
.toast.error { background: var(--bad); color: #fff; }
.spinner { display: inline-block; width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent;
  border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-block { padding: 30px 0; text-align: center; color: var(--ink-3); }

/* вход */
.login { max-width: 360px; margin: 18vh auto 0; }
.login h1 { font-family: var(--serif); font-size: 1.6rem; }

.tabs { display: flex; gap: 4px; background: var(--surface-2); border-radius: 12px; padding: 4px; margin-bottom: 12px; }
.tabs button { flex: 1; border: none; background: transparent; min-height: 38px; font-size: .9rem; border-radius: 9px; color: var(--ink-2); }
.tabs button.active { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: var(--shadow); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .92rem; }
.kv dt { color: var(--ink-3); } .kv dd { margin: 0; }

@media (min-width: 760px) {
  body { padding-bottom: 0; }
  .tabbar { position: sticky; top: 0; bottom: auto; height: 56px; border-top: none; border-bottom: 1px solid var(--line);
    justify-content: center; gap: 8px; padding-bottom: 0; }
  .tabbar a { flex: 0 0 auto; flex-direction: row; gap: 6px; padding: 0 14px; font-size: .9rem; }
  .toast { bottom: 24px; }
}
