:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --text: #1b2130;
  --muted: #5d667a;
  --border: #e1e5ef;
  --accent: #2f6fed;
  --accent-soft: #e6eeff;
  --good: #1f9d63;
  --good-soft: #e3f6ec;
  --bad: #d64545;
  --bad-soft: #fde8e8;
  --warn: #b7791f;
  --warn-soft: #fdf3e1;
  --case: #7c4dcc;
  --case-soft: #f1ebfc;
  --simple: #0e8a9a;
  --simple-soft: #e1f5f7;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 4px 16px rgba(20, 30, 60, .06);
  --radius: 12px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #11141b;
  --surface: #181c25;
  --surface-2: #202633;
  --text: #e6e9f0;
  --muted: #9aa3b5;
  --border: #2a3140;
  --accent: #6d9bff;
  --accent-soft: #1d2a47;
  --good: #3ecf8e;
  --good-soft: #15312a;
  --bad: #ff7272;
  --bad-soft: #3a1c1f;
  --warn: #f0b95a;
  --warn-soft: #33291a;
  --case: #b393ff;
  --case-soft: #2a2140;
  --simple: #4fd1e0;
  --simple-soft: #16303a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.6 var(--font); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; cursor: pointer; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 16px;
  height: 58px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; color: var(--text); }
.brand:hover { text-decoration: none; }
.logo { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 800; }
.topnav { display: flex; gap: 4px; }
.topnav a { padding: 6px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.topnav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.topnav a.active { background: var(--accent-soft); color: var(--accent); }
.search-wrap { margin-left: auto; }
.search-wrap input {
  width: 240px; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 14px;
}
.icon-btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); width: 36px; height: 36px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }
.only-mobile { display: none; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); min-height: calc(100vh - 58px); }
.layout > * { min-width: 0; }
.lesson-body, .msg { overflow-wrap: anywhere; }
.sidebar {
  position: sticky; top: 58px; height: calc(100vh - 58px); overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px 80px;
}
.view { padding: 32px 40px 120px; max-width: 980px; width: 100%; }

.side-progress { padding: 4px 8px 16px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.side-progress .label { font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }
.bar.good > span { background: var(--good); }

.side-module { margin-bottom: 2px; }
.side-module > button {
  display: flex; width: 100%; gap: 8px; align-items: flex-start; text-align: left; border: 0; background: none;
  color: var(--text); padding: 8px; border-radius: 8px; font-size: 14px; font-weight: 600;
}
.side-module > button:hover { background: var(--surface-2); }
.side-module .num { flex: none; width: 24px; height: 24px; border-radius: 6px; display: grid; place-items: center; font-size: 12px; background: var(--surface-2); color: var(--muted); }
.side-module.done .num { background: var(--good-soft); color: var(--good); }
.side-module.current > button { color: var(--accent); }
.side-lessons { display: none; padding: 2px 0 8px 32px; }
.side-module.open .side-lessons { display: block; }
.side-lessons a { display: flex; gap: 8px; padding: 5px 8px; border-radius: 6px; font-size: 13.5px; color: var(--muted); line-height: 1.35; }
.side-lessons a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.side-lessons a.active { background: var(--accent-soft); color: var(--accent); }
.side-lessons .tick { flex: none; width: 14px; color: var(--good); }

/* ---------- Home ---------- */
.hero {
  background: linear-gradient(135deg, var(--accent), #6a4de0); color: #fff; border-radius: 16px;
  padding: 32px; margin-bottom: 28px; box-shadow: var(--shadow);
}
.hero h1 { margin: 0 0 8px; font-size: 30px; line-height: 1.2; }
.hero p { margin: 0 0 18px; opacity: .92; max-width: 640px; }
.hero .stats { display: flex; gap: 28px; flex-wrap: wrap; margin-bottom: 20px; }
.hero .stat b { display: block; font-size: 26px; }
.hero .stat span { font-size: 13px; opacity: .85; }
.hero .btn { background: #fff; color: #2b3a8f; border-color: #fff; }
.hero .btn.ghost { background: transparent; color: #fff; border-color: rgba(255,255,255,.6); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; color: var(--text);
}
a.card:hover { text-decoration: none; border-color: var(--accent); transform: translateY(-1px); }
.card .kicker { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.card h3 { margin: 0; font-size: 17px; line-height: 1.3; }
.card p { margin: 0; color: var(--muted); font-size: 14px; }
.card .meta { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); margin-top: auto; padding-top: 6px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.section-title { margin: 32px 0 14px; font-size: 20px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px; padding: 9px 16px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font-weight: 600; font-size: 14.5px;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* ---------- Lesson ---------- */
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.lesson-head h1 { margin: 0 0 6px; font-size: 28px; line-height: 1.25; }
.lesson-head .sub { color: var(--muted); margin-bottom: 18px; }
.lesson-body { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px 32px; box-shadow: var(--shadow); }
.lesson-body h3 { font-size: 20px; margin: 28px 0 10px; }
.lesson-body h3:first-child { margin-top: 0; }
.lesson-body h4 { font-size: 17px; margin: 20px 0 8px; }
.lesson-body p { margin: 0 0 12px; }
.lesson-body ul, .lesson-body ol { margin: 0 0 14px; padding-left: 22px; }
.lesson-body li { margin-bottom: 4px; }
.lesson-body table { border-collapse: collapse; width: 100%; margin: 8px 0 18px; font-size: 14.5px; display: block; overflow-x: auto; }
.lesson-body th, .lesson-body td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: top; }
.lesson-body th { background: var(--surface-2); }
.lesson-body blockquote, .lesson-body .quote {
  margin: 14px 0; padding: 12px 18px; border-left: 4px solid var(--accent); background: var(--accent-soft);
  border-radius: 0 8px 8px 0; font-style: italic;
}
.lesson-body code { background: var(--surface-2); padding: 1px 6px; border-radius: 4px; font-size: .92em; }
.lesson-body .simple, .lesson-body .example, .lesson-body .apply, .lesson-body .case, .lesson-body .warn {
  margin: 16px 0; padding: 14px 18px; border-radius: 10px; border: 1px solid transparent;
}
.lesson-body .simple > strong:first-child, .lesson-body .example > strong:first-child, .lesson-body .apply > strong:first-child,
.lesson-body .case > strong:first-child, .lesson-body .warn > strong:first-child {
  display: block; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px;
}
.lesson-body .simple { background: var(--simple-soft); }
.lesson-body .simple > strong:first-child { color: var(--simple); }
.lesson-body .example { background: var(--accent-soft); }
.lesson-body .example > strong:first-child { color: var(--accent); }
.lesson-body .apply { background: var(--good-soft); }
.lesson-body .apply > strong:first-child { color: var(--good); }
.lesson-body .case { background: var(--case-soft); }
.lesson-body .case > strong:first-child { color: var(--case); }
.lesson-body .warn { background: var(--warn-soft); }
.lesson-body .warn > strong:first-child { color: var(--warn); }
.lesson-body div > p:last-child, .lesson-body div > ul:last-child { margin-bottom: 0; }
.lesson-foot { display: flex; justify-content: space-between; gap: 12px; margin-top: 20px; flex-wrap: wrap; }
.ask-strip {
  margin-top: 20px; padding: 16px 18px; border-radius: var(--radius); border: 1px dashed var(--accent);
  background: var(--accent-soft); display: flex; gap: 10px; flex-wrap: wrap; align-items: center;
}
.ask-strip .label { font-weight: 600; margin-right: 6px; }
.chip { border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 6px 12px; border-radius: 99px; font-size: 13.5px; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- Quiz ---------- */
.quiz-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); }
.quiz-top { display: flex; justify-content: space-between; align-items: center; font-size: 14px; color: var(--muted); margin-bottom: 12px; }
.quiz-q { font-size: 19px; font-weight: 600; margin: 8px 0 18px; line-height: 1.45; }
.options { display: grid; gap: 10px; }
.option {
  text-align: left; padding: 12px 16px; border-radius: 10px; border: 1.5px solid var(--border);
  background: var(--surface); color: var(--text); display: flex; gap: 12px; align-items: flex-start; line-height: 1.45;
}
.option:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.option .letter { flex: none; width: 26px; height: 26px; border-radius: 6px; display: grid; place-items: center; background: var(--surface-2); font-weight: 700; font-size: 13px; }
.option.correct { border-color: var(--good); background: var(--good-soft); }
.option.correct .letter { background: var(--good); color: #fff; }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.option.wrong .letter { background: var(--bad); color: #fff; }
.option:disabled { cursor: default; }
.explain { margin-top: 16px; padding: 14px 16px; border-radius: 10px; background: var(--surface-2); }
.explain.good { background: var(--good-soft); }
.explain.bad { background: var(--bad-soft); }
.explain b { display: block; margin-bottom: 4px; }
.result-big { font-size: 44px; font-weight: 800; margin: 6px 0; }
.review-item { border-top: 1px solid var(--border); padding: 14px 0; }
.review-item .q { font-weight: 600; margin-bottom: 4px; }
.review-item .a { font-size: 14px; color: var(--muted); }

/* ---------- Glossary / search ---------- */
.gloss-search { width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; margin-bottom: 18px; }
.gloss-item { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 18px; margin-bottom: 10px; }
.gloss-item b { display: block; margin-bottom: 4px; }
.search-hit { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 18px; margin-bottom: 10px; color: var(--text); }
.search-hit:hover { border-color: var(--accent); text-decoration: none; }
.search-hit small { color: var(--muted); }
.search-hit mark { background: var(--warn-soft); color: inherit; padding: 0 2px; border-radius: 3px; }

/* ---------- Chat ---------- */
.chat { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 92%; padding: 10px 14px; border-radius: 12px; line-height: 1.55; font-size: 15px; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.bot { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.bot p { margin: 0 0 8px; }
.msg.bot p:last-child { margin-bottom: 0; }
.msg.bot ul, .msg.bot ol { margin: 0 0 8px; padding-left: 20px; }
.msg.bot h1, .msg.bot h2, .msg.bot h3, .msg.bot h4 { font-size: 15.5px; margin: 10px 0 6px; }
.msg.bot table { border-collapse: collapse; font-size: 13.5px; margin: 6px 0; display: block; overflow-x: auto; }
.msg.bot th, .msg.bot td { border: 1px solid var(--border); padding: 4px 8px; }
.msg.bot code { background: var(--surface); padding: 1px 5px; border-radius: 4px; }
.msg.bot blockquote { margin: 6px 0; padding-left: 10px; border-left: 3px solid var(--accent); color: var(--muted); }
.msg.err { align-self: stretch; background: var(--bad-soft); color: var(--bad); font-size: 14px; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }
.chat-empty { color: var(--muted); font-size: 14px; text-align: center; margin: auto 10px; }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat-form textarea {
  flex: 1; resize: none; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 14.5px;
}
.quick { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 12px 8px; }
.quick .chip { font-size: 12.5px; padding: 4px 10px; }

.tutor-fab {
  position: fixed; right: 24px; bottom: 24px; z-index: 40; display: flex; align-items: center; gap: 8px;
  padding: 12px 18px; border-radius: 99px; border: 0; background: var(--accent); color: #fff; font-weight: 600; box-shadow: 0 6px 24px rgba(47,111,237,.4);
}
.fab-dot { width: 9px; height: 9px; border-radius: 50%; background: #7dffb5; }
.fab-dot.off { background: #ffb4b4; }
.tutor-drawer {
  position: fixed; right: 0; top: 58px; bottom: 0; width: 440px; max-width: 100vw; z-index: 45;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: -8px 0 32px rgba(0,0,0,.12);
  display: flex; flex-direction: column; transform: translateX(105%); transition: transform .25s ease;
}
.tutor-drawer.open { transform: none; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.drawer-title { font-weight: 700; }
.drawer-sub { font-size: 12.5px; color: var(--muted); max-width: 300px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.drawer-actions { display: flex; gap: 6px; }

.tutor-page { display: flex; flex-direction: column; height: calc(100vh - 58px - 64px); height: calc(100dvh - 58px - 64px); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.notice { padding: 12px 16px; border-radius: 10px; background: var(--warn-soft); color: var(--text); font-size: 14px; margin-bottom: 16px; }

/* ---------- Мобильные элементы ---------- */
.backdrop { position: fixed; inset: 58px 0 0; z-index: 34; background: rgba(10, 14, 25, .45); }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 38; display: none;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a, .tabbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 7px 2px 6px; border: 0; background: none; color: var(--muted);
  font-size: 11px; font-weight: 600; text-decoration: none; min-height: 52px; justify-content: center;
}
.tabbar a:hover, .tabbar button:hover { text-decoration: none; }
.tabbar .ico { font-size: 17px; line-height: 1; }
.tabbar a.active, .tabbar button.active { color: var(--accent); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .search-wrap input { width: 170px; }
}
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .topbar { gap: 10px; padding: 0 12px; }
  .sidebar {
    position: fixed; left: 0; top: 58px; bottom: 0; height: auto; width: min(320px, 88vw); z-index: 35;
    transform: translateX(-105%); transition: transform .25s; box-shadow: var(--shadow);
    padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }
  .sidebar.open { transform: none; }
  .only-mobile { display: inline-block; }
  .tabbar.only-mobile { display: flex; }
  .topnav { display: none; }
  .view { padding: 18px 14px calc(96px + env(safe-area-inset-bottom)); }
  .lesson-body { padding: 18px 16px; }
  .search-wrap input { width: 130px; }
  .tutor-fab { display: none; }
  .tutor-drawer {
    width: 100vw; top: 0; border-left: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tutor-drawer .drawer-head { padding-top: max(14px, env(safe-area-inset-top)); }
  .tutor-page { height: calc(100dvh - 58px - 120px); }
  /* на телефоне палец — не мышь: цели крупнее, строки не липнут друг к другу */
  .side-lessons a { padding: 9px 8px; font-size: 14.5px; }
  .side-module > button { padding: 11px 8px; font-size: 15px; }
  .option { padding: 14px; }
  .chip { padding: 9px 14px; font-size: 14px; }
  .btn { padding: 11px 16px; }
  .lesson-foot .btn-row { flex: 1; }
  .lesson-foot .btn { flex: 1; justify-content: center; }
}
@media (max-width: 520px) {
  .brand span:last-child { display: none; }
  .search-wrap { flex: 1; }
  .search-wrap input { width: 100%; }
  .lesson-head h1 { font-size: 22px; line-height: 1.3; }
  .lesson-body { font-size: 15.5px; padding: 16px 14px; }
  .lesson-body h3 { font-size: 18px; }
  .lesson-body .simple, .lesson-body .example, .lesson-body .apply, .lesson-body .case, .lesson-body .warn { padding: 12px 14px; margin: 14px 0; }
  .hero { padding: 20px 16px; border-radius: 14px; }
  .hero h1 { font-size: 23px; }
  .hero .stats { gap: 14px 22px; }
  .hero .stat b { font-size: 22px; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .quiz-card { padding: 18px 16px; }
  .quiz-q { font-size: 17.5px; }
  .result-big { font-size: 38px; }
  .ask-strip { padding: 14px; }
  .msg { max-width: 96%; }
}
