/* DOGYM.AI — экран ассистента. Палитра и радиусы берутся из web.css,
 * своё здесь только то, чего на сайте ещё не было: лента диалога, композер
 * и типографика Markdown-ответа. Макеты: design/Приветствие.png, design/Ответ ИИ.png,
 * design/ai_chat_fixes/fix_sizes_and_animation.png. */

.ai {
  --ai-user-bubble: #1668a8;
  --ai-spark: #f5841f;

  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  /* Экран фиксирован по высоте, прокручивается лента внутри панели, а не
   * страница: композер и предупреждение всегда на месте, как в макете.
   * Вычитается шапка сайта (web.css .nav) и поля .web-main. */
  height: calc(100dvh - 119px);
  min-height: 420px;
}
@media (max-width: 900px) {
  .ai { height: calc(100dvh - 97px); }
}

.web-main--ai { align-items: center; padding-bottom: 8px; }

/* ---------- Приветствие ---------- */
.ai__welcome { margin: auto auto 24px; text-align: center; }
.ai__title {
  font-family: var(--web-font-title);
  font-size: 34px; line-height: 1.2; font-weight: 800;
  color: var(--web-text); margin: 0 0 12px;
}
.ai__subtitle { color: var(--web-text-2); font-size: 16px; margin: 0; }

/* ---------- Лента диалога ---------- */
.ai__panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--web-surface);
  border: 1px solid var(--web-border);
  border-radius: var(--web-radius);
  margin-bottom: 16px;
  overflow: hidden;
}
/* Композер наезжает на низ ленты на радиус своих углов: рамка общая, без
 * зазора, и под скруглениями видна лента, а не фон страницы. Без композера
 * (гейт согласия при истории) лента остаётся замкнутой. */
.ai__panel:has(~ .ai__composer:not([hidden])) {
  border-radius: var(--web-radius) var(--web-radius) 0 0;
  border-bottom: 0;
  padding-bottom: var(--web-radius);
  margin-bottom: 0;
}
/* display:flex перебивает браузерное [hidden] { display: none } */
.ai__panel[hidden] { display: none; }
.ai__panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 24px; border-bottom: 1px solid var(--web-border); }
.ai__brand { font-family: var(--web-font-title); font-weight: 800; font-size: 20px; color: var(--web-text); }
.ai__log { flex: 1; overflow-y: auto; padding: 20px 24px; display: flex; flex-direction: column; gap: 16px; }

.ai__msg { max-width: 100%; font-size: 15px; line-height: 1.6; }
.ai__msg--user {
  align-self: flex-end;
  max-width: 70%;
  background: var(--ai-user-bubble);
  color: #fff;
  padding: 10px 16px;
  border-radius: var(--web-radius-md);
  white-space: pre-wrap;
}
.ai__msg--assistant { color: var(--web-text-2); }

.ai__typing { align-self: flex-start; }
.ai__sparks { display: flex; align-items: center; gap: 4px; color: var(--ai-spark); font-size: 12px; line-height: 1; }
.ai__sparks > span { animation: ai-spark 1.2s ease-in-out infinite; }
.ai__sparks > span:nth-child(2) { animation-delay: 0.2s; }
.ai__sparks > span:nth-child(3) { animation-delay: 0.4s; }
@keyframes ai-spark {
  0%, 100% { transform: scale(0.45); opacity: 0.5; }
  50% { transform: scale(1); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .ai__sparks > span { animation: none; }
}
.ai__msg--error { color: var(--web-danger); }

/* Ответ печатается: каретка после последнего дорисованного абзаца текста. */
.ai__msg--streaming .ai__block--generated > :last-child::after {
  content: "▍";
  margin-left: 2px;
  color: var(--web-green);
  animation: ai-caret 1s steps(2, start) infinite;
}
@keyframes ai-caret { to { visibility: hidden; } }

/* Обрыв генерации (docs/ai-chat.md §9): многоточие на месте среза, подпись и
   «Продолжить» под тем, что пришло. */
.ai__msg--interrupted .ai__block--generated > :last-child::after { content: " …"; color: var(--web-muted); }
.ai__interrupted {
  display: flex; align-items: center; flex-wrap: wrap; gap: 12px;
  margin-top: 12px; color: var(--web-muted); font-size: 13px;
}
.ai__continue {
  border: 1px solid var(--web-border-strong); border-radius: 999px;
  background: var(--web-surface-2); color: var(--web-text);
  padding: 6px 14px; font: inherit; font-size: 13px; cursor: pointer;
}
.ai__continue:hover { border-color: var(--web-green); color: var(--web-green); }

/* Программы каталога под текстом ответа (маркер инструмента, docs/ai-chat.md §5).
 * Селекторы через блок: иначе их перебивают правила Markdown-списков ниже. */
.ai__block--programs:empty { display: none; }
.ai__block--programs .ai__programs {
  list-style: none; margin: 12px 0; padding: 0;
  display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
}
.ai__block--programs .ai__program {
  margin: 0; padding: 14px 16px;
  display: flex; flex-direction: column; gap: 6px;
  background: var(--web-surface-2);
  border: 1px solid var(--web-border-strong);
  border-radius: var(--web-radius-sm);
}
.ai__program-title { color: var(--web-text); font-weight: 700; font-size: 16px; line-height: 1.3; }
.ai__program-meta { color: var(--web-green); font-size: 13px; }
.ai__program-description {
  font-size: 14px; line-height: 1.5;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 6; line-clamp: 6; overflow: hidden;
}
.ai__program-description--full { display: block; -webkit-line-clamp: none; line-clamp: none; overflow: visible; }
.ai__program-expand {
  align-self: flex-start; border: 0; padding: 0; background: none; cursor: pointer;
  color: var(--web-green); font: inherit; font-size: 13px;
}
.ai__program-expand:hover { color: var(--web-green-strong); }
.ai__block--programs .ai__program-features { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.ai__block--programs .ai__program-features li {
  margin: 0; padding: 2px 10px; border-radius: 999px;
  background: var(--web-surface-3); color: var(--web-text-2); font-size: 12px;
}

/* Доп. вопросы чипами: вариант уходит обычным вопросом (docs/ai-chat.md §14). */
.ai__block--follow_ups:empty { display: none; }
.ai__block--follow_ups { margin-top: 12px; }
.ai__block--follow_ups .ai__follow-ups-lead { margin: 0 0 10px; }

/* Кнопки под ответом по состоянию посетителя (docs/ai-chat.md §14). */
.ai__block--cta:empty { display: none; }
.ai__block--cta { margin-top: 12px; }
.ai__block--cta .ai__offer-text { margin: 0 0 10px; }
.ai__offer-buttons { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 10px; }
.ai__msg--assistant .ai__offer-buttons .ai__chip { display: inline-block; text-decoration: none; }
/* Магазины: бейдж со ссылкой и QR-код над ним — его сканируют с другого устройства, поэтому на телефоне QR нет. */
.ai__msg--assistant .ai__store {
  display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 12px;
  border: 1px solid var(--web-border-strong); border-radius: var(--web-radius-md);
  color: var(--web-text); text-decoration: none;
}
.ai__msg--assistant .ai__store:hover { background: var(--web-surface-2); }
.ai__store-qr { display: block; width: 132px; height: 132px; border-radius: 6px; }
.ai__store-badge { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; }
@media (max-width: 720px), (pointer: coarse) {
  .ai__store-qr { display: none; }
  .ai__msg--assistant .ai__store { padding: 8px 16px; border-radius: 999px; }
}

/* Markdown-ответ: заголовки и списки — обязательная часть формата (§14). */
.ai__msg--assistant h1,
.ai__msg--assistant h2,
.ai__msg--assistant h3 {
  font-family: var(--web-font-title);
  color: var(--web-text);
  font-size: 20px; line-height: 1.3; font-weight: 700;
  margin: 24px 0 8px; padding-top: 16px;
  border-top: 1px solid var(--web-border);
}
.ai__msg--assistant > :first-child { margin-top: 0; padding-top: 0; border-top: 0; }
.ai__msg--assistant p { margin: 0 0 10px; }
.ai__msg--assistant ul, .ai__msg--assistant ol { margin: 0 0 10px; padding-left: 22px; }
.ai__msg--assistant li { margin-bottom: 6px; }
.ai__msg--assistant strong { color: var(--web-text); }
.ai__msg--assistant a { color: var(--web-green); }
.ai__msg--assistant code {
  background: var(--web-surface-2); border-radius: 6px; padding: 1px 5px; font-size: 13px;
}
.ai__msg--assistant pre { background: var(--web-surface-2); border-radius: var(--web-radius-sm); padding: 12px; overflow-x: auto; }
.ai__msg--assistant table { border-collapse: collapse; width: 100%; }
.ai__msg--assistant th, .ai__msg--assistant td { border: 1px solid var(--web-border); padding: 6px 10px; text-align: left; }

/* ---------- Композер ---------- */
.ai__composer {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  background: var(--web-surface-2);
  border: 1px solid var(--web-border-strong);
  border-radius: var(--web-radius);
  padding: 18px 16px 14px;
  /* На приветственном экране auto-поля делят пустоту поровну и держат
   * «приветствие + композер» по центру, а предупреждение — у нижнего края;
   * с открытой лентой её flex:1 съедает свободное место и поля обнуляются. */
  margin-bottom: auto;
}
.ai__panel:not([hidden]) ~ .ai__composer { margin-top: calc(-1 * var(--web-radius)); }
.ai__sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}
/* Пустое поле — одна строка; высоту по мере набора выставляет chat.js, дальше шести строк — прокрутка. */
.ai__input {
  box-sizing: border-box; width: 100%; resize: none; border: 0; background: transparent; outline: none;
  padding: 0 6px; max-height: calc(6 * 1.5em); overflow-y: auto;
  color: var(--web-text); font-family: var(--web-font); font-size: 16px; line-height: 1.5;
}
.ai__input::placeholder { color: var(--web-muted); }
/* Под полем — ряд кнопок по макету: «+» слева, отправка справа. */
.ai__composer-actions { display: flex; align-items: center; justify-content: space-between; margin-top: 10px; }
.ai__send {
  width: 40px; height: 40px; border-radius: 999px; border: 0;
  background: var(--web-surface-3); color: var(--web-text-2);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.ai__send--ready:not(:disabled),
.ai__send:hover:not(:disabled) { background: var(--web-green); color: #fff; }
.ai__send:disabled { opacity: 0.5; cursor: default; }

/* ---------- Предупреждение об ограничениях (макет «Приветствие») ---------- */
.ai__notice { margin: 8px 0 0; text-align: center; color: var(--web-muted); font-size: 13px; }

/* Гейт согласия (docs/ai-chat.md §8): кнопка и текст со ссылками на месте поля ввода. */
.ai__consent {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  text-align: center;
  /* Держит «приветствие + гейт» по центру, как margin-bottom композера. */
  margin-bottom: auto;
}
/* display:flex и display:block перебивают браузерное [hidden] { display: none } */
.ai__consent[hidden], .ai__composer[hidden], .ai__chips[hidden] { display: none; }
.ai__consent-start {
  border: 0; border-radius: 999px; padding: 14px 32px; cursor: pointer;
  background: var(--web-green); color: #fff;
  font-family: var(--web-font); font-size: 16px; font-weight: 700;
}
.ai__consent-start:disabled { opacity: 0.5; cursor: default; }
.ai__consent-text { margin: 0; max-width: 520px; color: var(--web-muted); font-size: 13px; line-height: 1.5; }
.ai__consent-text a { color: var(--web-green); }
.ai__consent-error { margin: 0; color: var(--web-danger); font-size: 13px; }

/* Чат выключен: строка на месте поля ввода. */
.ai__unavailable {
  margin: 0;
  padding: 18px 22px;
  text-align: center;
  background: var(--web-surface-2);
  border: 1px solid var(--web-border-strong);
  border-radius: var(--web-radius);
}

@media (max-width: 720px) {
  .ai__title { font-size: 24px; }
  .ai__subtitle { font-size: 15px; }
  .ai__panel-head { flex-wrap: wrap; padding: 14px 16px; }
  .ai__log { padding: 16px; }
  .ai__msg--user { max-width: 85%; }
  .ai__composer { padding: 14px 12px 12px; }
  .ai__send, .ai__attach { width: 36px; height: 36px; }
}

/* ---------- Популярные запросы (макеты «Приветствие», «Популярные запросы») ---------- */
.ai__chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 10px; }
.ai__chip, .ai__popular-open {
  border: 1px solid; border-radius: 999px; background: transparent;
  font-family: var(--web-font); font-size: 14px; cursor: pointer;
}
.ai__chip { border-color: var(--web-green); color: var(--web-green); line-height: 1.3; padding: 7px 14px; text-align: left; }
.ai__chip:hover { background: var(--web-selected); }
/* На приветствии чипы стоят под композером и забирают его auto-поле себе:
 * группа «приветствие + композер + чипы» остаётся по центру, как в макете. */
.ai__composer:has(+ .ai__chips--welcome) { margin-bottom: 0; }
.ai__chips--welcome { margin: 16px 0 auto; }
.ai__welcome[hidden] ~ .ai__chips--welcome { display: none; }

.ai__popular-open { border-color: var(--web-border-strong); color: var(--web-text); padding: 8px 16px; white-space: nowrap; }
.ai__popular-open:hover { background: var(--web-surface-2); }

.ai__panel-actions { display: flex; align-items: center; gap: 8px; }
.ai__clear {
  border: 0; border-radius: 999px; background: var(--web-surface-3); color: var(--web-text);
  font-family: var(--web-font); font-size: 14px; padding: 8px 16px; white-space: nowrap; cursor: pointer;
}
.ai__clear:hover:not(:disabled) { background: var(--web-surface-2); }
.ai__clear:disabled { opacity: 0.5; cursor: default; }
.ai__popular {
  margin: 0 16px 16px; padding: 16px 20px;
  border: 1px solid var(--web-border-strong); border-radius: var(--web-radius);
}
.ai__popular-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; color: var(--web-text); font-size: 16px; }
.ai__popular-close {
  margin-left: auto; width: 32px; height: 32px; border: 0; border-radius: 50%; background: transparent;
  color: var(--web-text); cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
}
.ai__popular-close:hover { background: var(--web-surface-2); }

@media (max-width: 720px) {
  .ai__chip, .ai__popular-open, .ai__clear { font-size: 13px; padding: 6px 12px; }
  .ai__popular { margin: 0 12px 12px; padding: 14px 16px; }
}

/* ---------- Файлы к вопросу (макет «Написание в чат - текст+картинка+файлы») ---------- */
.ai__attach {
  width: 40px; height: 40px; border-radius: 999px; border: 0;
  background: transparent; color: var(--web-text);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.ai__attach:hover:not(:disabled) { background: var(--web-surface-3); }
.ai__attach:disabled { opacity: 0.5; cursor: default; }
.ai__attach-locked { margin: 8px 6px 0; color: var(--web-text-2); font-size: 13px; }
.ai__attach-locked a { color: var(--web-green); }
.ai__attach-error { margin: 0 0 10px; color: var(--web-danger); font-size: 13px; white-space: pre-line; }

.ai__files { list-style: none; margin: 0 0 14px; padding: 0; display: flex; flex-wrap: wrap; gap: 12px; }
/* display:flex перебивает браузерное [hidden] { display: none } */
.ai__files[hidden] { display: none; }
.ai__file {
  position: relative;
  display: flex; align-items: center; gap: 12px;
  height: 64px; max-width: 260px; padding: 0 34px 0 14px;
  background: var(--web-surface);
  border: 1px solid var(--web-border-strong);
  border-radius: var(--web-radius-sm);
}
.ai__file-icon { flex: none; display: block; }
.ai__file-icon--document {
  width: 22px; height: 28px; border-radius: 3px; background: #2f8fef;
  clip-path: polygon(0 0, 68% 0, 100% 26%, 100% 100%, 0 100%);
}
/* Миниатюра фото, а без самого файла (после перезагрузки) — зелёная плашка. */
.ai__file-icon--image { width: 40px; height: 40px; border-radius: 6px; background: var(--web-green); object-fit: cover; }
.ai__file-info { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.ai__file-name { color: var(--web-text); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai__file-meta { color: var(--web-muted); font-size: 12px; }
.ai__file-remove {
  position: absolute; top: 5px; right: 5px;
  width: 20px; height: 20px; border: 0; border-radius: 50%;
  background: var(--web-text); color: var(--web-surface);
  font-size: 14px; line-height: 1; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
/* В ленте файлы стоят над вопросом, у правого края, как и он. */
.ai__msg-files { align-self: flex-end; max-width: 70%; }
.ai__msg-files .ai__files { margin: 0; justify-content: flex-end; }
