:root {
  --bg: #0f1117;
  --surface: #171a23;
  --surface-2: #1f2330;
  --border: #2a2f3e;
  --text: #e6e8ee;
  --muted: #9aa3b2;
  --accent: #d9772b;
  --accent-soft: #3a2a1a;
  --ok: #3fb950;
  --bad: #f85149;
  --radius: 10px;
  --maxw: 920px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.container { width: 100%; max-width: var(--maxw); margin: 0 auto; padding: 0 16px; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.7rem; line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.3rem; margin: 2rem 0 .75rem; }
h3 { font-size: 1.1rem; margin: 0; }
h4 { font-size: 1rem; margin: 1.25rem 0 .5rem; color: var(--text); }
p { margin: .5rem 0; }
.muted { color: var(--muted); }
.lead { color: var(--muted); font-size: 1.05rem; }
.eyebrow, .card-eyebrow { color: var(--accent); font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }

/* ---------- header / nav ---------- */
.site-header {
  position: sticky; top: 0; z-index: 20;
  background: rgba(15,17,23,.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.header-inner { display: flex; align-items: center; gap: 12px; min-height: 56px; }
.brand { font-weight: 700; color: var(--text); font-size: 1.05rem; }
.brand-ru { color: var(--accent); font-size: .8rem; vertical-align: super; }
.site-nav { margin-left: auto; display: flex; flex-wrap: wrap; gap: 4px; }
.nav-link {
  display: inline-flex; align-items: center; min-height: 44px;
  padding: 0 10px; border-radius: 8px; color: var(--muted); font-size: .92rem;
}
.nav-link:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav-link.active { color: var(--text); background: var(--accent-soft); }

.nav-toggle {
  display: none; margin-left: auto;
  width: 44px; height: 44px; padding: 10px;
  background: transparent; border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
  flex-direction: column; justify-content: space-between;
}
.nav-toggle span { display: block; height: 2px; background: var(--text); border-radius: 2px; }

main.container { padding-top: 24px; padding-bottom: 48px; }

/* ---------- hero / home ---------- */
.hero { padding: 8px 0 16px; }
.metrics { display: flex; flex-wrap: wrap; gap: 12px; margin: 20px 0; }
.metric { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; flex: 1 1 120px; }
.metric-value { display: block; font-size: 1.5rem; font-weight: 700; color: var(--accent); }
.metric-label { color: var(--muted); font-size: .85rem; }
.cta { display: flex; flex-wrap: wrap; gap: 10px; }

/* Атрибут hidden должен прятать элемент даже при заданном display (напр. .btn). */
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 18px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover { text-decoration: none; border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #15110b; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.card { display: flex; flex-direction: column; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card:hover { border-color: var(--accent); text-decoration: none; }
.card-title { font-weight: 700; color: var(--text); }
.card-meta { color: var(--muted); font-size: .85rem; }

.steps { padding-left: 1.2rem; }
.steps li { margin: .4rem 0; }

/* ---------- domain / task ---------- */
.domain-head { border-bottom: 1px solid var(--border); padding-bottom: 16px; margin-bottom: 8px; }
.toc { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; margin: 16px 0 24px; }
.toc h2 { margin: 0 0 .5rem; font-size: 1rem; }
.toc ol { margin: 0; padding-left: 1.2rem; }
.toc li { margin: .25rem 0; }

.task { border-top: 1px solid var(--border); padding: 24px 0; }
.task-head { display: flex; align-items: flex-start; gap: 10px; }
.task-check { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.task-toggle { width: 20px; height: 20px; accent-color: var(--accent); }
.task-id { font-weight: 700; color: var(--accent); white-space: nowrap; }
.task-title { flex: 1; }
.task.done { opacity: .72; }
.task.done .task-title { text-decoration: line-through; }

.lesson :is(ul, ol) { padding-left: 1.3rem; }
.lesson code, .question code, td code, .explanation code {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 5px;
  padding: 1px 5px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .88em;
}
.block-title { color: var(--accent); }

/* ---------- code ---------- */
.code { margin: 16px 0; }
.code figcaption { color: var(--muted); font-size: .85rem; margin-bottom: 4px; }
.code-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
pre { margin: 0; padding: 14px 16px; }
pre code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85rem; line-height: 1.5; white-space: pre; }

/* ---------- diagrams (mermaid) ---------- */
/* Блок до рендера показывает исходный текст моноширинно; после рендера mermaid
   заменяет его на SVG. Центрируем и даём горизонтальный скролл на узких экранах. */
.code.diagram pre.mermaid { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; text-align: center; }
.code.diagram pre.mermaid svg { max-width: 100%; height: auto; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 12px 0; border: 1px solid var(--border); border-radius: var(--radius); }
.data-table { border-collapse: collapse; width: 100%; min-width: 520px; }
.data-table caption { text-align: left; color: var(--muted); padding: 8px 12px; font-size: .85rem; }
.data-table th, .data-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.data-table th { background: var(--surface-2); font-size: .85rem; white-space: nowrap; }
.data-table tr:last-child td { border-bottom: none; }

/* ---------- implementation ---------- */
.impl-list { padding-left: 1.3rem; }
.impl-list li { margin: .35rem 0; }

/* ---------- questions ---------- */
.qbank { margin-top: 16px; }
.question { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin: 12px 0; }
.question-scenario { color: var(--muted); font-style: italic; }
.question-stem { font-weight: 600; }
.options { list-style: none; margin: 10px 0; padding: 0; }
.option { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; margin: 6px 0; min-height: 44px; }
.option-label { font-weight: 700; color: var(--muted); }
.option.correct { border-color: var(--ok); background: rgba(63,185,80,.1); }
.option.correct .option-label { color: var(--ok); }
.reveal-btn { margin-top: 4px; }
.reveal-btn { min-height: 44px; padding: 0 16px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); border-radius: 8px; cursor: pointer; font: inherit; font-weight: 600; }
.reveal-btn:hover { border-color: var(--accent); }
.explanation { margin-top: 12px; padding: 12px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 8px 8px 0; }
.answer-line { margin-top: 0; }

/* ---------- progress ---------- */
.progress { margin: 10px 0; }
.progress-bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .25s ease; }
.progress.big .progress-bar { height: 12px; }
.progress-label { font-size: .9rem; color: var(--muted); margin: 6px 0 0; }
.progress-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.domain-progress-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.dp-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.dp-title { display: block; font-weight: 600; color: var(--text); margin-bottom: 4px; }

.exam-history table { width: 100%; }
.history-item { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; padding: 12px; border: 1px solid var(--border); border-radius: 8px; margin: 8px 0; background: var(--surface); }
.badge { padding: 2px 10px; border-radius: 99px; font-size: .82rem; font-weight: 700; }
.badge.pass { background: rgba(63,185,80,.15); color: var(--ok); }
.badge.fail { background: rgba(248,81,73,.15); color: var(--bad); }

/* ---------- exam runner ---------- */
.exam-facts { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(160px,1fr)); gap: 10px; }
.exam-facts li { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.exam-bar { display: flex; justify-content: space-between; align-items: center; position: sticky; top: 56px; background: var(--bg); padding: 10px 0; z-index: 10; }
.exam-counter { color: var(--muted); }
.exam-timer { font-variant-numeric: tabular-nums; font-weight: 700; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; }
.exam-timer.danger { color: var(--bad); border-color: var(--bad); }
.exam-controls { display: flex; gap: 10px; margin: 16px 0; flex-wrap: wrap; }
.exam-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 6px; margin-top: 8px; }
.exam-grid button { min-height: 40px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 6px; cursor: pointer; font: inherit; }
.exam-grid button.answered { border-color: var(--accent); background: var(--accent-soft); }
.exam-grid button.current { outline: 2px solid var(--accent); }
.exam .options .option { cursor: pointer; }
.exam .options .option.chosen { border-color: var(--accent); background: var(--accent-soft); }
.exam .options .option.correct { border-color: var(--ok); background: rgba(63,185,80,.1); }
.exam .options .option.wrong { border-color: var(--bad); background: rgba(248,81,73,.1); }
.score-circle { font-size: 2.6rem; font-weight: 800; }
.score-circle.pass { color: var(--ok); }
.score-circle.fail { color: var(--bad); }
.result-review .question { margin: 10px 0; }

/* ---------- footer ---------- */
.site-footer { border-top: 1px solid var(--border); padding: 20px 0; color: var(--muted); font-size: .9rem; }
.site-footer p { margin: 4px 0; }

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .nav-toggle { display: flex; }
  .site-nav {
    position: fixed; inset: 56px 0 auto 0;
    flex-direction: column; gap: 0;
    background: var(--surface); border-bottom: 1px solid var(--border);
    padding: 8px; margin: 0;
    max-height: calc(100vh - 56px); overflow-y: auto;
    transform: translateY(-120%); transition: transform .2s ease;
  }
  .site-nav.open { transform: translateY(0); }
  .nav-link { width: 100%; }
  h1 { font-size: 1.45rem; }
  .exam-bar { top: 0; }
}

/* ---------- AI-ассистент ---------- */
.assistant-fab {
  position: fixed; right: 18px; bottom: 18px; z-index: 50;
  width: 56px; height: 56px; border-radius: 50%;
  border: 1px solid var(--accent); background: var(--accent); color: #15110b;
  font-weight: 800; font-size: 1rem; cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .35);
}
.assistant-fab:hover { filter: brightness(1.05); }
body.assistant-open .assistant-fab { display: none; }

.assistant-panel {
  position: fixed; right: 18px; bottom: 18px; z-index: 60;
  display: none; flex-direction: column; /* по умолчанию закрыт; открытие — .is-open */
  width: 380px; max-width: calc(100vw - 36px); height: 560px; max-height: calc(100vh - 36px);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .5);
}
.assistant-panel.is-open { display: flex; }
.assistant-resize {
  position: absolute; top: 0; left: 0; z-index: 5;
  width: 18px; height: 18px; cursor: nwse-resize;
  background:
    linear-gradient(135deg, var(--accent) 0 2px, transparent 2px 4px, var(--accent) 4px 6px, transparent 6px) no-repeat;
  background-size: 12px 12px; background-position: 4px 4px; opacity: .55;
}
.assistant-resize:hover { opacity: 1; }
.assistant-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.assistant-title { font-weight: 700; font-size: .98rem; }
.assistant-sub { color: var(--muted); font-size: .76rem; margin-top: 2px; }
.assistant-actions { display: flex; align-items: center; gap: 2px; flex: none; }
.assistant-icon {
  border: none; background: none; color: var(--muted);
  width: 32px; height: 32px; border-radius: 8px;
  font-size: 1.4rem; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.assistant-icon:hover { color: var(--text); background: var(--surface); }

.assistant-log {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.assistant-msg { display: flex; }
.assistant-user { justify-content: flex-end; }
.assistant-bubble {
  max-width: 86%; padding: 9px 12px; border-radius: 12px;
  font-size: .9rem; line-height: 1.5; white-space: pre-wrap; word-wrap: break-word;
  background: var(--surface-2); border: 1px solid var(--border);
}
.assistant-user .assistant-bubble {
  background: var(--accent-soft); border-color: var(--accent);
}
.assistant-bubble.assistant-hint { color: var(--muted); }
.assistant-bubble.assistant-error { border-color: var(--bad); color: var(--bad); }

/* отрендеренный markdown в ответе ассистента */
.assistant-bubble.assistant-md { white-space: normal; }
.assistant-md > :first-child { margin-top: 0; }
.assistant-md > :last-child { margin-bottom: 0; }
.assistant-md p { margin: 0 0 8px; }
.assistant-md ul, .assistant-md ol { margin: 0 0 8px; padding-left: 20px; }
.assistant-md li { margin: 2px 0; }
.assistant-md h1, .assistant-md h2, .assistant-md h3, .assistant-md h4 {
  font-size: .95rem; margin: 8px 0 4px;
}
.assistant-md strong { font-weight: 700; }
.assistant-md a { color: var(--accent); }
.assistant-md code {
  background: var(--bg); border: 1px solid var(--border); border-radius: 4px;
  padding: 0 4px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .85em;
}
.assistant-md pre {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; margin: 0 0 8px; overflow-x: auto;
}
.assistant-md pre code { background: none; border: none; padding: 0; font-size: .82rem; }
.assistant-bubble.assistant-typing::after {
  content: "▋"; color: var(--accent); animation: assistant-blink 1s step-end infinite;
}
@keyframes assistant-blink { 50% { opacity: 0; } }

.assistant-sources-row { margin-top: -6px; } /* прижать чипы к ответу сверху */
.assistant-sources {
  display: flex; flex-wrap: wrap; gap: 6px;
}
.assistant-chip {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--accent);
  border-radius: 99px; padding: 4px 10px; font-size: .76rem; cursor: pointer;
}
.assistant-chip:hover { border-color: var(--accent); }

.assistant-form {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px; border-top: 1px solid var(--border); background: var(--surface);
}
.assistant-input {
  flex: 1; resize: none; max-height: 120px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px;
  font: inherit; font-size: .9rem; line-height: 1.4;
}
.assistant-input:focus { outline: none; border-color: var(--accent); }
.assistant-send {
  flex: none; width: 44px; height: 44px; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--accent); color: #15110b;
  font-size: 1.2rem; font-weight: 800; cursor: pointer;
}
.assistant-send:disabled { opacity: .5; cursor: default; }

@media (max-width: 760px) {
  /* На узких экранах — полноэкранный bottom-sheet; !important перебивает
     инлайновый размер, выставленный resize на десктопе в этой же вкладке. */
  .assistant-panel {
    right: 0; left: 0; bottom: 0; top: auto;
    width: 100% !important; max-width: 100% !important;
    height: 88dvh !important; max-height: 88dvh !important;
    border-radius: var(--radius) var(--radius) 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .assistant-resize { display: none; }
  .assistant-fab { right: 14px; bottom: 14px; }
}

/* ============ Мультикурс: хаб, лендинги, уроки ============ */

/* Карточки курсов на главной */
.course-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.course-card {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
}
.course-card:hover { border-color: var(--accent); text-decoration: none; }
.course-card-eyebrow { color: var(--accent); font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.course-card-title { font-size: 1.25rem; font-weight: 800; color: var(--text); }
.course-card-tagline { color: var(--muted); font-size: .9rem; }
.course-card-metrics { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0; }
.course-card-metrics .metric { flex: 1 1 90px; padding: 8px 10px; }
.course-card-metrics .metric-value { font-size: 1.15rem; }

/* Список уроков модуля (Курс 2) */
.lesson-list { list-style: none; padding: 0; margin: 16px 0 0; display: flex; flex-direction: column; gap: 8px; }
.lesson-list-item { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.lesson-list-item.done { border-color: var(--accent); }
.lesson-list-link { display: flex; align-items: center; gap: 12px; padding: 14px 16px; }
.lesson-list-link:hover { text-decoration: none; }
.lesson-list-id { color: var(--accent); font-weight: 700; min-width: 2.5rem; }
.lesson-list-title { color: var(--text); font-weight: 600; }
.lesson-list-item.done .lesson-list-id::after { content: " ✓"; }

/* Страница урока (Курс 2) */
.lesson-page-head { margin-bottom: 16px; }
.lesson-page-check { display: inline-flex; align-items: center; gap: 8px; margin: 8px 0; color: var(--muted); font-size: .9rem; cursor: pointer; }
.lesson-nav { display: flex; justify-content: space-between; gap: 12px; margin-top: 32px; flex-wrap: wrap; }

/* Блок «Дальше почитать» */
.further { margin-top: 20px; padding: 14px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.further-list { margin: 8px 0 0; padding-left: 20px; }
.further-list li { margin: 4px 0; }

/* ============ Счётчик токенов тьютора ============ */
.assistant-usage {
  display: flex; align-items: flex-start; gap: 8px; flex-wrap: wrap;
  padding: 8px 10px; border-top: 1px solid var(--border);
  background: var(--surface-2); font-size: .76rem; color: var(--muted);
}
.assistant-usage-summary {
  flex: 1; min-width: 0; text-align: left; background: none; border: none;
  color: var(--muted); cursor: pointer; padding: 6px 4px; min-height: 32px;
  font: inherit; font-size: .76rem;
}
.assistant-usage-summary:hover { color: var(--text); }
.assistant-usage-line { display: block; }
.assistant-usage-reset {
  flex: none; background: none; border: 1px solid var(--border); color: var(--muted);
  border-radius: 99px; padding: 4px 10px; cursor: pointer; font-size: .72rem; min-height: 32px;
}
.assistant-usage-reset:hover { border-color: var(--accent); color: var(--accent); }
.assistant-usage-detail {
  flex-basis: 100%; color: var(--muted); font-size: .72rem;
  padding: 4px; border-top: 1px dashed var(--border); margin-top: 2px;
  word-break: break-word;
}

@media (max-width: 760px) {
  .course-cards { grid-template-columns: 1fr; }
  /* Тап-зоны ≥44px на мобильном */
  .assistant-usage-summary, .assistant-usage-reset { min-height: 44px; }
  .lesson-list-link { padding: 16px; min-height: 44px; }
}
