/* Стили чата. Все цвета/шрифты/размеры — через токены --c-*. */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: var(--c-font-family);
  font-size: var(--c-font-size);
  line-height: 1.45;
  color: var(--c-text);
  background: var(--c-bg);
}
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.chat {
  display: flex; flex-direction: column;
  height: 100%; min-height: 0; overflow: hidden;
  background: var(--c-bg);
  position: relative;
}
.chat[data-mode="inline"] { min-height: 0; height: 100%; }

/* Шапка */
.chat__header {
  display: flex; align-items: center; gap: var(--c-space);
  padding: var(--c-space) calc(var(--c-space) * 1.25);
  background: var(--c-header-bg); color: var(--c-header-text);
  border-bottom: 3px solid var(--c-header-line);
  flex-shrink: 0; z-index: 2;
}
.chat__logo { height: 32px; width: auto; }
.chat__title { flex: 1; min-width: 0; }
.chat__title h1 { margin: 0; font-size: 1.1em; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat__org { font-size: 0.85em; opacity: 0.85; }
.chat__tools { display: flex; align-items: center; gap: calc(var(--c-space) / 2); flex-wrap: wrap; justify-content: flex-end; }
.chat__header .btn--icon { color: inherit; }
.chat__header .btn--icon:hover { color: inherit; border-color: currentColor; }
.chat__header .btn--icon svg { width: 20px; height: 20px; }

/* Свёрнутый чат (встраивание со сворачиванием): в iframe видна только шапка. */
.chat.is-collapsed .chat__messages,
.chat.is-collapsed .composer,
.chat.is-collapsed .authbar,
.chat.is-collapsed .typing,
.chat.is-collapsed .chat__farewell { display: none; }
.chat.is-collapsed .chat__header { cursor: pointer; }

/* Статус */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 0.8em; padding: 2px 8px; border-radius: 999px; border: 1px solid currentColor; }
.status__dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status--ok { color: var(--c-status-ok); }
.status--degraded { color: var(--c-status-warn); }
.status--error { color: var(--c-status-error); }

/* Сообщения */
.chat__messages {
  flex: 1; min-height: 0; overflow-x: hidden; overflow-y: auto;
  padding: var(--c-space) calc(var(--c-space) * 1.25);
  display: flex; flex-direction: column; gap: var(--c-space);
  scroll-behavior: auto; overflow-anchor: none;
}
.chat.is-unavailable .composer,
.chat.is-unavailable .authbar,
.chat.is-unavailable .typing,
.chat.is-unavailable .composer__help-btn,
.chat.is-unavailable #logoutBtn { display: none !important; }
.chat.is-unavailable .chat__messages {
  justify-content: center; align-items: center;
}
.chat__stub {
  max-width: 28em; margin: 0 auto; text-align: center;
  color: var(--c-text-muted); font-size: 1.05em; line-height: 1.45;
  padding: calc(var(--c-space) * 1.5) var(--c-space);
}
.msg { display: flex; flex-direction: column; max-width: 85%; flex-shrink: 0; }
.msg--user { align-self: flex-end; align-items: flex-end; }
.msg--bot { align-self: flex-start; align-items: flex-start; }
.msg__bubble {
  padding: calc(var(--c-space) * 0.75) var(--c-space);
  border-radius: var(--c-radius);
  border: 1px solid var(--c-border);
  box-shadow: var(--c-shadow);
  overflow-wrap: anywhere;
}
.msg--user .msg__bubble { background: var(--c-user-bg); color: var(--c-user-text); border-color: transparent; }
.msg--bot .msg__bubble { background: var(--c-bot-bg); color: var(--c-bot-text); }
.msg--bot.msg--warn .msg__bubble { border-left: 4px solid var(--c-status-warn); }
.msg--bot.msg--error .msg__bubble { border-left: 4px solid var(--c-status-error); }
.msg--bot.msg--kb .msg__bubble { border-left: 4px solid var(--c-status-ok); }
.msg__bubble p { margin: 0 0 0.5em; }
.msg__bubble p:last-child { margin-bottom: 0; }
.msg__bubble ul, .msg__bubble ol { margin: 0.25em 0 0.5em; padding-left: 1.4em; }
.msg__bubble code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.9em; background: rgba(0,0,0,0.06); padding: 0 4px; border-radius: 4px; }
.msg__bubble pre { white-space: pre-wrap; margin: 0.25em 0; }
.msg__meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 4px; font-size: 0.8em; color: var(--c-text-muted); }
.msg__time { opacity: 0.8; }

/* Вложения */
.attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.attachments__btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: var(--c-radius);
  border: 1px solid var(--c-accent); color: var(--c-accent); background: var(--c-surface);
  text-decoration: none; font-size: 0.9em;
}
.attachments__btn:hover { background: var(--c-accent); color: var(--c-accent-text); }

/* Подсказки вопросов (assist / suggest) — внутри .msg__bubble */
.msg__bubble .suggestions {
  display: flex; flex-direction: column; gap: 6px;
  margin-top: 10px; width: 100%;
}
.msg__bubble .suggestions__btn {
  display: block; width: 100%; text-align: left;
  white-space: normal; line-height: 1.35;
  padding: 8px 12px;
  border: 1px solid var(--c-accent); color: var(--c-accent);
  background: transparent;
  border-radius: var(--c-radius); font: inherit; cursor: pointer;
}
.msg__bubble .suggestions__btn:hover:not(:disabled) {
  background: var(--c-accent); color: var(--c-accent-text);
}
.msg__bubble .suggestions__btn:disabled { opacity: 0.55; cursor: not-allowed; }
.msg__bubble .suggestions__none {
  display: block; width: 100%; text-align: left;
  white-space: normal; line-height: 1.35;
  padding: 8px 12px; margin-top: 2px;
  border: 1px solid var(--c-status-error); color: var(--c-status-error);
  background: transparent;
  border-radius: var(--c-radius); font: inherit; cursor: pointer;
}
.msg__bubble .suggestions__none:hover:not(:disabled) {
  background: var(--c-status-error); color: #fff;
  border-color: var(--c-status-error);
}
.msg__bubble .suggestions__none:disabled { opacity: 0.55; cursor: not-allowed; }
.msg__bubble .msg__specialist-icon {
  display: inline-flex; vertical-align: -0.25em; margin-left: 0.35em;
  color: var(--c-text-muted); pointer-events: none;
}
.msg__bubble .msg__specialist-icon svg { width: 1.15em; height: 1.15em; display: block; }
.msg__bubble .suggestions--used .suggestions__btn,
.msg__bubble .suggestions--used .suggestions__none { pointer-events: none; }

/* Кнопки */
.btn {
  font: inherit; cursor: pointer; border-radius: var(--c-radius);
  padding: 8px 14px; border: 1px solid var(--c-border); background: var(--c-surface); color: var(--c-text);
  transition: background-color 0.15s, color 0.15s, border-color 0.15s;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn--primary { background: var(--c-accent); color: var(--c-accent-text); border-color: var(--c-accent); }
.btn--primary:hover:not(:disabled) { background: var(--c-accent-hover); border-color: var(--c-accent-hover); }
.btn--secondary { border-color: var(--c-accent); color: var(--c-accent); }
.btn--secondary:hover { background: var(--c-accent); color: var(--c-accent-text); }
.btn--ghost { background: transparent; }
.btn--icon { padding: 4px; font-size: 0.95em; line-height: 1; border-color: transparent; background: transparent; color: var(--c-text-muted); }
.btn--icon:hover { border-color: var(--c-border); color: var(--c-text); }
.btn--icon[aria-pressed="true"] { color: var(--c-accent); border-color: var(--c-accent); }
.btn--icon svg { display: block; }
.btn--icon[aria-pressed="true"] svg { fill: currentColor; }
:focus-visible { outline: 2px solid var(--c-focus); outline-offset: 2px; }

/* Оценка */
.feedback { display: inline-flex; gap: 4px; align-items: center; }
.feedback-thanks {
  font-size: 0.85em; color: var(--c-text-muted); opacity: 0.85;
  margin-left: 4px; white-space: nowrap;
  animation: feedback-thanks-in 0.25s ease;
}
@keyframes feedback-thanks-in { from { opacity: 0; } to { opacity: 0.85; } }
@media (prefers-reduced-motion: reduce) {
  .feedback-thanks { animation: none; }
}

/* Формы */
.input, textarea.input, select.input {
  font: inherit; color: var(--c-text); background: var(--c-surface);
  border: 1px solid var(--c-border); border-radius: var(--c-radius); padding: 8px 10px; width: 100%;
}
.field { display: grid; gap: 4px; }
.field > span, .field legend { font-size: 0.9em; color: var(--c-text-muted); }
.field--radio { border: 0; padding: 0; margin: 0; }
.field--radio label { margin-right: 14px; }
.form-error { color: var(--c-status-error); font-size: 0.9em; }
.req { color: var(--c-status-error); }

.escform {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  padding: var(--c-space);
  overflow: hidden;
}
.escform__backdrop {
  position: absolute; inset: 0;
  background: var(--c-text); opacity: 0.2;
}
.escform__panel {
  position: relative; z-index: 1;
  width: min(420px, 100%);
  max-height: 100%;
  padding: calc(var(--c-space) * 1.25);
  display: grid; gap: var(--c-space);
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--c-surface); color: var(--c-text);
  border: 1px solid var(--c-border); border-radius: var(--c-radius); box-shadow: var(--c-shadow);
  box-sizing: border-box;
}
.escform__head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.escform__title { font-weight: 600; padding-top: 4px; }
.escform__close { flex: 0 0 auto; }
.escform__close svg { width: 18px; height: 18px; }
.escform__actions { display: flex; justify-content: flex-end; gap: 8px; }
.escform--static {
  position: relative; inset: auto; z-index: 0;
  padding: 0 calc(var(--c-space) * 1.25) var(--c-space);
}
.escform--static .escform__backdrop { display: none; }

/* Ввод */
.authbar {
  padding: var(--c-space) calc(var(--c-space) * 1.25);
  background: var(--c-surface); border-top: 1px solid var(--c-border);
  flex-shrink: 0;
  display: flex; justify-content: center;
}
.authbar[hidden] { display: none; }
.authbar__btn { width: 100%; max-width: 280px; }
.composer {
  padding: var(--c-space) calc(var(--c-space) * 1.25);
  background: var(--c-surface); border-top: 1px solid var(--c-border);
  flex-shrink: 0;
}
.composer[hidden] { display: none; }
.composer__box {
  display: flex; align-items: flex-end; gap: 4px;
  border: 1px solid var(--c-border); border-radius: var(--c-radius);
  background: var(--c-surface); padding: 4px;
}
.composer__box:focus-within { outline: 2px solid var(--c-focus); outline-offset: 2px; }
.composer__input {
  flex: 1; resize: none; overflow-x: hidden; overflow-y: hidden; border: 0;
  min-height: calc(1.45em + 8px);
  max-height: calc(1.45em * 4 + 8px);
  line-height: 1.45;
  font: inherit; color: var(--c-text); background: transparent;
  border-radius: var(--c-radius); padding: 4px 8px;
}
.composer__input:focus, .composer__input:focus-visible { outline: none; }
.composer__help-btn {
  flex: 0 0 auto; width: calc(1.45em + 8px); height: calc(1.45em + 8px); padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--c-radius);
}
.composer__help-btn[hidden] { display: none; }
.composer__help-btn svg { width: 18px; height: 18px; display: block; }
.composer__send {
  flex: 0 0 auto; width: calc(1.45em + 8px); height: calc(1.45em + 8px); padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--c-accent-text); background: var(--c-accent); border-color: var(--c-accent);
  border-radius: var(--c-radius);
}
.composer__send:hover:not(:disabled) {
  background: var(--c-accent-hover); border-color: var(--c-accent-hover);
}
.composer__send svg { width: 18px; height: 18px; display: block; }

/* «Печатает…» */
.typing { padding: 0 calc(var(--c-space) * 1.25) 6px; color: var(--c-text-muted); font-size: 0.9em; display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.typing__dots i { display: inline-block; width: 6px; height: 6px; margin-right: 3px; border-radius: 50%; background: currentColor; animation: blink 1.2s infinite; }
.typing__dots i:nth-child(2) { animation-delay: 0.2s; }
.typing__dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }

.chat.is-leaving .chat__messages,
.chat.is-leaving .composer,
.chat.is-leaving .authbar,
.chat.is-leaving .typing {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}
.chat__farewell {
  position: absolute; inset: 0; z-index: 5;
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--c-space) * 2);
  text-align: center; color: var(--c-text);
  font-size: 1.15em; line-height: 1.45;
  opacity: 0; transform: translateY(10px);
  transition: opacity 0.45s ease, transform 0.45s ease;
  pointer-events: none;
}
.chat__farewell.is-visible { opacity: 1; transform: none; }
.chat.is-entering .chat__messages,
.chat.is-entering .composer,
.chat.is-entering .authbar { animation: chat-fade-in 0.5s ease; }
@keyframes chat-fade-in { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 480px) {
  .msg { max-width: 95%; }
  .chat__header { padding: 8px 10px; }
  .chat__tools .status { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .chat__messages { scroll-behavior: auto; }
  .typing__dots i { animation: none; }
  .btn { transition: none; }
  .chat.is-leaving .chat__messages,
  .chat.is-leaving .composer,
  .chat.is-leaving .authbar,
  .chat.is-leaving .typing,
  .chat__farewell,
  .chat.is-entering .chat__messages,
  .chat.is-entering .composer,
  .chat.is-entering .authbar { transition: none; animation: none; transform: none; }
}
