/* =====================================================================================
 * ИИ-ассистент блога (assets/js/assistant.js). Стили панели перенесены из курса
 * «Градиентный бустинг по косточкам» (shared/web/css/assistant.css); ниже, в конце файла, —
 * «мост» токенов: --accent, --surface, --ink и т. д. берутся из палитры блога (site.css),
 * поэтому светлая и тёмная темы работают без отдельных правил.
 * ===================================================================================== */

/* ---------- 18. ИИ-ассистент урока (shared/web/js/ui/assistant.js) ---------------------- */
:root {
  --assistant-w: 420px;
}
/* Кнопка у выделенного текста */
.assistant-ask {
  position: fixed;
  z-index: 95;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px 7px 10px;
  border: 0;
  border-radius: 99px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 0.86rem;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: var(--shadow), 0 0 0 3px color-mix(in srgb, var(--page) 70%, transparent);
  opacity: 0;
  transform: translateY(4px) scale(0.96);
  transition: opacity 0.14s var(--ease), transform 0.14s var(--ease), background 0.15s;
}
.assistant-ask[hidden] {
  display: none;
}
.assistant-ask.show {
  opacity: 1;
  transform: none;
}
.assistant-ask:hover {
  background: var(--accent-hover);
}
.assistant-ask svg {
  width: 17px;
  height: 17px;
}
.assistant-toggle[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

/* Панель */
.assistant {
  position: fixed;
  z-index: 90;
  top: var(--topbar-h);
  right: 0;
  bottom: 0;
  width: min(var(--assistant-w), 100vw);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 0.94rem;
  line-height: 1.55;
  animation: assistant-in 0.22s var(--ease);
}
.assistant[hidden] {
  display: none;
}
@keyframes assistant-in {
  from {
    transform: translateX(24px);
    opacity: 0;
  }
}
/* На широком экране панель занимает место оглавления и не закрывает урок */
@media (min-width: 1281px) {
  body.assistant-open .layout:not(.home) {
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    margin-right: var(--assistant-w);
  }
  body.assistant-open .toc {
    display: none;
  }
  .assistant {
    box-shadow: none;
  }
}
.assistant-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 10px 10px 14px;
  border-bottom: 1px solid var(--border);
}
.assistant-logo {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
}
.assistant-logo svg {
  width: 19px;
  height: 19px;
}
.assistant-title {
  flex: 1;
  min-width: 0;
  margin-left: 4px;
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}
.assistant-title .sub {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.assistant-title .dot {
  width: 7px;
  height: 7px;
  flex: none;
  border-radius: 50%;
  background: var(--muted);
}
.assistant[data-status="online"] .dot {
  background: var(--good);
}
.assistant[data-status="offline"] .dot {
  background: var(--critical);
}
.assistant-head .icon-btn {
  width: 32px;
  height: 32px;
}
.assistant-head .icon-btn svg {
  width: 18px;
  height: 18px;
}

/* Настройки */
.assistant-settings {
  display: grid;
  gap: 10px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.assistant-settings[hidden] {
  display: none;
}
.assistant-field {
  display: grid;
  gap: 4px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--ink-2);
}
.assistant-field input,
.assistant-field select {
  width: 100%;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-size: 0.9rem;
  font-weight: 400;
}
.assistant-field input[aria-invalid="true"] {
  border-color: var(--critical);
}
.assistant-field small {
  font-weight: 400;
  color: var(--muted);
}
.assistant-settings-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Сервер не запущен */
.assistant-offline {
  margin: 12px 14px 0;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--warn-soft);
  border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent);
  font-size: 0.88rem;
}
.assistant-offline[hidden] {
  display: none;
}
.assistant-offline strong {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--warn-text);
}
.assistant-offline strong svg {
  width: 18px;
  height: 18px;
  flex: none;
}
.assistant-offline p {
  margin: 8px 0;
}
.assistant-offline .cmd {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 6px 6px 10px;
  border-radius: 7px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.assistant-offline .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.assistant-offline .actions .btn {
  text-decoration: none;
}
.assistant-offline .cmd code {
  flex: 1;
  min-width: 0;
  background: none;
  padding: 0;
  white-space: normal;
  overflow-wrap: anywhere;
}

/* Лента сообщений */
.assistant-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  scrollbar-width: thin;
  overscroll-behavior: contain;
}
.assistant-empty {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 28px 12px;
  text-align: center;
  color: var(--ink-2);
}
.assistant-empty[hidden] {
  display: none;
}
.assistant-empty .big {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  margin-bottom: 6px;
  border-radius: 16px;
  background: var(--accent-soft);
  color: var(--accent);
}
.assistant-empty .big svg {
  width: 28px;
  height: 28px;
}
.assistant-empty strong {
  font-size: 1.02rem;
  color: var(--ink);
}
.assistant-empty p {
  margin: 0;
  max-width: 34ch;
  font-size: 0.9rem;
}
.assistant-log {
  display: grid;
  gap: 16px;
}
.assistant-msg {
  min-width: 0;
}
.assistant-msg.user {
  display: grid;
  justify-items: end;
  gap: 6px;
}
.assistant-msg.user .bubble {
  max-width: 92%;
  padding: 8px 12px;
  border-radius: 14px 14px 4px 14px;
  background: var(--accent-soft);
  color: var(--ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.assistant-cite {
  display: grid;
  gap: 2px;
  max-width: 100%;
  min-width: 0;
  margin: 0;
  padding: 6px 10px;
  border-left: 3px solid var(--accent-line);
  border-radius: 0 8px 8px 0;
  background: var(--surface-2);
  font-size: 0.84rem;
  color: var(--ink-2);
}
.assistant-cite .src {
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.assistant-cite .txt {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  line-clamp: 4;
  overflow: hidden;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.assistant-actions {
  display: flex;
  gap: 2px;
  margin-top: 2px;
  opacity: 0.55;
  transition: opacity 0.15s;
}
.assistant-msg.bot:hover .assistant-actions,
.assistant-actions:focus-within {
  opacity: 1;
}
.assistant-actions .icon-btn {
  width: 28px;
  height: 28px;
}
.assistant-actions .icon-btn svg {
  width: 15px;
  height: 15px;
}
.assistant-note {
  margin: 6px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}
.assistant-error {
  display: flex;
  gap: 8px;
  margin: 6px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--critical-soft);
  color: var(--critical-text);
  font-size: 0.86rem;
}
.assistant-error svg {
  width: 17px;
  height: 17px;
  flex: none;
  margin-top: 2px;
}
.assistant-thinking {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 0.86rem;
}
.assistant-thinking .dots {
  display: inline-flex;
  gap: 4px;
}
.assistant-thinking .dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: assistant-dot 1.1s infinite ease-in-out;
}
.assistant-thinking .dots i:nth-child(2) {
  animation-delay: 0.15s;
}
.assistant-thinking .dots i:nth-child(3) {
  animation-delay: 0.3s;
}
@keyframes assistant-dot {
  0%,
  80%,
  100% {
    opacity: 0.25;
    transform: scale(0.8);
  }
  40% {
    opacity: 1;
    transform: none;
  }
}
.assistant-caret {
  display: inline-block;
  width: 7px;
  height: 1em;
  margin-left: 2px;
  vertical-align: text-bottom;
  border-radius: 2px;
  background: var(--accent);
  animation: assistant-dot 1s infinite;
}

/* Ответ (Markdown) */
.md > :first-child {
  margin-top: 0;
}
.md > :last-child {
  margin-bottom: 0;
}
.md p,
.md ul,
.md ol,
.md pre,
.md blockquote,
.md .md-table,
.md .md-formula {
  margin: 0 0 10px;
}
.md h4,
.md h5,
.md h6 {
  margin: 14px 0 6px;
  font-size: 0.98rem;
  line-height: 1.3;
}
.md ul,
.md ol {
  padding-left: 1.35em;
}
.md li {
  margin: 3px 0;
}
.md li > p {
  margin-bottom: 4px;
}
.md :not(pre) > code {
  white-space: normal;
  overflow-wrap: anywhere;
}
.md pre {
  padding: 10px 12px;
  font-size: 0.82rem;
}
.md pre code {
  font-size: inherit;
}
.md blockquote {
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--ink-2);
}
.md hr {
  margin: 12px 0;
  border: 0;
  border-top: 1px solid var(--border);
}
.md .md-table,
.md .md-formula {
  position: relative;
  max-width: 100%;
  overflow-x: auto;
}
.md .md-formula {
  padding: 4px 0;
  text-align: center;
}
.md table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}
.md th,
.md td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.md th {
  font-weight: 650;
  border-bottom-color: var(--border-strong);
}
.md .katex {
  font-size: 1.04em;
}

/* Поле ввода */
.assistant-foot {
  display: grid;
  gap: 8px;
  padding: 10px 14px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.assistant-quote {
  display: flex;
  align-items: flex-start;
  gap: 4px;
}
.assistant-quote[hidden],
.assistant-chips[hidden] {
  display: none;
}
.assistant-quote .assistant-cite {
  flex: 1;
}
.assistant-quote .assistant-cite .txt {
  -webkit-line-clamp: 3;
  line-clamp: 3;
}
.assistant-quote .icon-btn {
  width: 26px;
  height: 26px;
  flex: none;
}
.assistant-quote .icon-btn svg {
  width: 14px;
  height: 14px;
}
.assistant-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.assistant-chip {
  padding: 4px 10px;
  border-radius: 99px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink-2);
  font-size: 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.assistant-chip:hover {
  border-color: var(--accent-line);
  color: var(--accent);
  background: var(--accent-soft);
}
.assistant-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 6px 6px 12px;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  background: var(--page);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.assistant-input:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.assistant-input textarea {
  flex: 1;
  min-width: 0;
  max-height: 160px;
  padding: 5px 0;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  font-size: 0.95rem;
  line-height: 1.45;
}
.assistant-send {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: none;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}
.assistant-send:hover {
  background: var(--accent-hover);
}
.assistant-send[disabled] {
  opacity: 0.35;
  cursor: default;
}
.assistant-send svg {
  width: 18px;
  height: 18px;
}
.assistant-hint {
  margin: 0;
  font-size: 0.72rem;
  line-height: 1.35;
  color: var(--muted);
}
/* Скриншот к вопросу */
.assistant-camera {
  width: 34px;
  height: 34px;
  flex: none;
  margin-left: -6px;
}
.assistant-shot-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 0.8rem;
  color: var(--ink-2);
}
.assistant-shot-preview[hidden] {
  display: none;
}
.assistant-shot-preview img {
  width: 72px;
  height: 48px;
  flex: none;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
}
.assistant-shot-preview span {
  flex: 1;
  min-width: 0;
}
.assistant-shot-preview .icon-btn {
  width: 26px;
  height: 26px;
  flex: none;
}
.assistant-shot-preview .icon-btn svg {
  width: 14px;
  height: 14px;
}
.assistant-shot {
  display: block;
  max-width: 100%;
  max-height: 220px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: #fff;
}
.assistant-shot-gone {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}
.assistant-shot-gone svg {
  width: 15px;
  height: 15px;
}
/* Пока выбирается область снимка, панель и кнопка не должны попасть в кадр */
body.assistant-shooting .assistant,
body.assistant-shooting .assistant-ask,
body.assistant-shooting .toast {
  visibility: hidden;
}
.assistant-region {
  position: fixed;
  inset: 0;
  z-index: 200;
  cursor: crosshair;
  outline: 0;
  touch-action: none;
  user-select: none;
}
.assistant-region-frame {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  max-width: none;
  pointer-events: none;
}
.assistant-capture-video {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 2px;
  height: 2px;
  opacity: 0.01;
  pointer-events: none;
}
.assistant-region-box {
  position: fixed;
  border: 2px solid var(--accent);
  border-radius: 3px;
  background: var(--accent-soft);
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.28);
  pointer-events: none;
}
.assistant-region-box[hidden],
.assistant-region-hint[hidden] {
  display: none;
}
.assistant-region-hint {
  position: fixed;
  left: 50%;
  top: calc(var(--topbar-h) + 14px);
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 9px 16px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--page);
  font-size: 0.88rem;
  text-align: center;
  box-shadow: var(--shadow);
  pointer-events: none;
}
@media (max-width: 640px) {
  .assistant {
    top: 0;
    width: 100vw;
    border-left: 0;
  }
  body.assistant-open {
    overflow: hidden;
  }
}
@media print {
  .assistant,
  .assistant-ask,
  .assistant-toggle {
    display: none !important;
  }
}

/* ---------- Служебное, что нужно панели и вне course.css (самостоятельные страницы extra/) ---------- */
/* База course.css «button, input, textarea { font: inherit; color: inherit }» — без неё поле ввода
   берёт системный чёрный цвет и в тёмной теме текст вопроса не виден. */
.assistant button,
.assistant input,
.assistant select,
.assistant textarea,
.assistant-ask {
  font: inherit;
  color: inherit;
}
.assistant select {
  background: var(--surface);
}
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 120;
  padding: 9px 16px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--page);
  font-size: 0.88rem;
  box-shadow: var(--shadow);
  transform: translate(-50%, 16px);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}
.assistant .btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  line-height: 1.2;
}
.assistant .btn svg {
  width: 16px;
  height: 16px;
  flex: none;
}
.assistant .btn.small {
  padding: 4px 10px;
  font-size: 0.82rem;
  border-radius: 7px;
  gap: 6px;
}
.assistant .btn.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--ink-2);
}
.assistant .btn.ghost:hover {
  background: var(--surface-3);
  color: var(--ink);
}
.assistant .btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}


/* ---------- Мост токенов: палитра блога → токены панели ---------- */
:root {
  --accent-ink: #fff;
  --accent-soft: color-mix(in srgb, var(--accent) 12%, var(--bg));
  --accent-hover: color-mix(in srgb, var(--accent) 85%, var(--fg));
  --accent-line: color-mix(in srgb, var(--accent) 45%, var(--bg));
  --border: var(--line);
  --border-strong: var(--bg3);
  --surface: var(--card);
  --surface-2: var(--bg2);
  --surface-3: var(--bg3);
  --page: var(--bg);
  --ink: var(--fg);
  --ink-2: var(--fg2);
  --muted: var(--fg3);
  --shadow: 0 10px 34px rgba(23, 26, 32, .12);
  --ease: cubic-bezier(.2, .7, .2, 1);
  --topbar-h: 0px;
  --sidebar-w: 0px;
  --radius-sm: 4px;
  --critical: var(--err);
  --critical-text: var(--err);
  --critical-soft: color-mix(in srgb, var(--err) 10%, var(--bg));
  --warn: #B26A00;
  --warn-text: #8A5200;
  --warn-soft: color-mix(in srgb, #E8A33D 14%, var(--bg));
  --good: #1B7F4B;
}
:root[data-theme="dark"] { --accent-ink: #0F1012; --shadow: 0 10px 34px rgba(0, 0, 0, .45); --warn: #E3B25A; --warn-text: #E3B25A; --good: #5AC98A; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --accent-ink: #0F1012; --shadow: 0 10px 34px rgba(0, 0, 0, .45); --warn: #E3B25A; --warn-text: #E3B25A; --good: #5AC98A; }
}

/* Кнопки панели (в курсе они из course.css) */
.assistant .icon-btn, .assistant-toggle {
  display: inline-grid; place-items: center; width: 34px; height: 34px; padding: 0;
  border: 0; border-radius: 6px; background: transparent; color: var(--fg3); cursor: pointer;
  transition: background-color .2s, color .2s;
}
.assistant .icon-btn:hover { background: var(--bg2); color: var(--fg); }
.assistant .icon-btn svg { width: 18px; height: 18px; }
.assistant .btn {
  padding: 7px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--card);
  color: var(--fg); font-size: 13px; font-weight: 600; text-decoration: none; cursor: pointer;
}
.assistant .btn:hover { border-color: var(--fg4); }
.assistant .btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

/* Кнопка в шапке блога — рядом с переключателем темы */
.assistant-toggle svg { width: 18px; height: 18px; }
.assistant-toggle:hover { color: var(--fg); }
.assistant-toggle[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); }

/* Панель открыта на широком экране — страница сдвигается, а не закрывается */
@media (min-width: 1281px) {
  body.assistant-open { padding-right: var(--assistant-w); }
  .assistant { box-shadow: none; }
}
/* Подсказки для запуска сервера */
.assistant-offline ol { margin: 6px 0 0; padding-left: 20px; }
.assistant-offline li { margin: 4px 0; }
.assistant-offline .remote-note { display: block; margin-top: 6px; line-height: 1.5; }
.assistant-offline .os { display: inline-flex; gap: 2px; padding: 2px; border-radius: 6px; background: var(--bg3); margin: 6px 0 2px; }
.assistant-offline .os button { border: 0; border-radius: 4px; padding: 3px 9px; background: transparent; color: var(--fg3); font-size: 12px; font-weight: 600; cursor: pointer; }
.assistant-offline .os button[aria-pressed="true"] { background: var(--card); color: var(--fg); }
