/*
 * Страница чата com_aichat. Чат занимает весь экран и не зависит от стилей шаблона:
 * все нужные свойства элементов задаются здесь явно.
 * Светлая тема по умолчанию, тёмная включается по системной настройке.
 */

.aichat {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface-hover: #ededea;
  --line: #e2e2dd;
  --line-strong: #cfcfc8;
  --ink: #1b1b1d;
  --muted: #6b6b70;
  --user-bg: #1b1b1d;
  --user-ink: #fafaf9;
  --focus: #1b1b1d;
  --danger: #a3261b;
  --danger-bg: #fbecea;
  --danger-line: #f1c9c4;
  --skeleton: #e8e8e4;
  --shadow: 0 1px 2px rgb(0 0 0 / 4%), 0 6px 20px rgb(0 0 0 / 5%);

  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  .aichat {
    --bg: #121214;
    --surface: #1c1c1f;
    --surface-hover: #26262a;
    --line: #2b2b30;
    --line-strong: #3a3a40;
    --ink: #ececee;
    --muted: #9b9ba3;
    --user-bg: #ececee;
    --user-ink: #121214;
    --focus: #ececee;
    --danger: #ffb4a9;
    --danger-bg: #3a1f1c;
    --danger-line: #5c2d27;
    --skeleton: #232327;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

body:has(#aichat) { margin: 0; overflow: hidden; background: #f5f5f3; }
@media (prefers-color-scheme: dark) { body:has(#aichat) { background: #121214; } }

.aichat *, .aichat *::before, .aichat *::after { box-sizing: border-box; }
.aichat h1, .aichat p { margin: 0; }
.aichat button, .aichat textarea {
  margin: 0;
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
  text-transform: none;
}
.aichat button { cursor: pointer; }
.aichat button:disabled { cursor: default; }
.aichat :focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.aichat-column { width: 100%; max-width: 760px; margin: 0 auto; padding: 0 24px; }

/* Шапка */
.aichat-bar { border-bottom: 1px solid var(--line); background: var(--bg); }
.aichat-bar-inner { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 64px; }
.aichat-identity { display: flex; align-items: center; gap: 12px; min-width: 0; }
.aichat-mark {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--bg);
}
.aichat-heading { font-size: 16px; font-weight: 600; line-height: 1.3; color: var(--ink); }
.aichat-subheading { font-size: 13px; color: var(--muted); }
.aichat .aichat-new {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: none;
}
.aichat-new:hover:not(:disabled) { background: var(--surface-hover); }
.aichat-new:disabled { opacity: .5; }

/* Лента */
.aichat-scroll { overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable both-edges; }
.aichat-feed { display: flex; flex-direction: column; padding: 28px 0 20px; }

.aichat-msg { display: flex; flex-direction: column; align-items: flex-start; max-width: min(85%, 600px); margin-top: 4px; }
.aichat-msg.group-start { margin-top: 18px; }
.aichat-msg:first-child { margin-top: 0; }
.aichat-msg.from-user { align-self: flex-end; align-items: flex-end; }

.aichat-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.from-bot .aichat-bubble { border: 1px solid var(--line); background: var(--surface); border-top-left-radius: 6px; }
.from-bot.group-start .aichat-bubble { border-top-left-radius: 18px; }
.from-user .aichat-bubble { background: var(--user-bg); color: var(--user-ink); border-top-right-radius: 6px; }
.from-user.group-start .aichat-bubble { border-top-right-radius: 18px; }
.aichat-bubble strong { font-weight: 600; }
.aichat-bubble code { padding: 1px 5px; border-radius: 5px; background: var(--surface-hover); font-size: .92em; }
.from-user .aichat-bubble code { background: rgb(127 127 127 / 25%); }
.aichat-bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }

.aichat-meta { margin: 5px 6px 0; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.aichat-tag {
  display: inline-block;
  margin-right: 6px;
  padding: 0 5px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .04em;
  line-height: 15px;
}

/* Таблица из SQL-блока */
.aichat-table { margin: 10px -4px 2px; overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; white-space: normal; }
.aichat-table table { width: 100%; border-collapse: collapse; font-size: 14px; }
.aichat-table th, .aichat-table td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; background: transparent; }
.aichat-table th { color: var(--muted); font-size: 12.5px; font-weight: 500; white-space: nowrap; }
.aichat-table td.num, .aichat-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.aichat-table tr:last-child td { border-bottom: 0; }
.aichat-empty { margin-top: 6px; color: var(--muted); }

/* Системные сообщения: закрытие диалога, ошибки сценария */
.aichat-system { align-self: center; max-width: 440px; margin: 24px 0 4px; color: var(--muted); font-size: 13px; line-height: 1.5; text-align: center; text-wrap: balance; }

/* Кнопки сценария под последним сообщением */
.aichat-replies { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.aichat-replies.centered { justify-content: center; }
.aichat-reply {
  min-height: 38px;
  padding: 7px 15px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  font-size: 14px;
  line-height: 1.35;
  text-align: left;
  box-shadow: none;
  transition: background-color .15s, border-color .15s;
}
.aichat-reply:hover:not(:disabled) { border-color: var(--ink); background: var(--surface); }
.aichat-reply:disabled { opacity: .5; }
.aichat-reply.primary { border-color: var(--ink); background: var(--ink); color: var(--bg); font-weight: 500; }
.aichat-reply.quiet { border-style: dashed; color: var(--muted); }
.aichat-reply.quiet:hover:not(:disabled) { color: var(--ink); }

/* Ожидание ответа */
.aichat-typing { display: inline-flex; gap: 4px; padding: 14px 16px; border: 1px solid var(--line); border-radius: 18px; background: var(--surface); }
.aichat-typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: aichat-blink 1.2s infinite both; }
.aichat-typing span:nth-child(2) { animation-delay: .15s; }
.aichat-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes aichat-blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* Загрузка */
.aichat-skeleton { display: flex; flex-direction: column; gap: 8px; width: min(420px, 80%); margin-bottom: 14px; padding: 14px; border: 1px solid var(--line); border-radius: 18px; background: var(--surface); }
.aichat-skeleton.short { width: min(220px, 50%); }
.aichat-skeleton span { height: 10px; border-radius: 5px; background: var(--skeleton); animation: aichat-pulse 1.4s ease-in-out infinite; }
.aichat-skeleton span:nth-child(2) { width: 70%; }
@keyframes aichat-pulse { 50% { opacity: .5; } }
.aichat-loading-error { color: var(--muted); }

/* Нижняя панель: ошибка, поле ввода, примечание */
.aichat-dock { padding: 10px 0 max(14px, env(safe-area-inset-bottom)); background: linear-gradient(to bottom, transparent, var(--bg) 14px); }
.aichat-error {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  padding: 10px 12px 10px 14px;
  border: 1px solid var(--danger-line);
  border-radius: 12px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 14px;
}
.aichat-error[hidden] { display: none; }
.aichat-error-text { flex: 1; }
.aichat-error-retry {
  flex-shrink: 0;
  height: 32px;
  padding: 0 12px;
  border: 1px solid currentColor;
  border-radius: 999px;
  background: transparent;
  font-size: 13px;
  font-weight: 500;
}
.aichat-error-retry[hidden] { display: none; }

.aichat-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0;
  padding: 6px 6px 6px 16px;
  border: 1px solid var(--line-strong);
  border-radius: 22px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color .15s;
}
.aichat-composer:focus-within { border-color: var(--ink); }
.aichat-composer.disabled { box-shadow: none; background: transparent; border-style: dashed; }
.aichat .aichat-composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 38px;
  max-height: 160px;
  padding: 8px 0;
  border: 0;
  border-radius: 0;
  outline: none;
  resize: none;
  background: transparent;
  box-shadow: none;
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
}
.aichat .aichat-composer textarea::placeholder { color: var(--muted); opacity: 1; }
.aichat .aichat-composer textarea:disabled { background: transparent; cursor: default; }
.aichat .aichat-send {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  transition: opacity .15s;
}
.aichat .aichat-send:disabled { background: var(--surface-hover); color: var(--muted); }
.aichat-composer.disabled .aichat-send { visibility: hidden; }
.aichat-note { margin-top: 8px; color: var(--muted); font-size: 12px; text-align: center; }
.aichat-noscript { padding: 24px; text-align: center; }

@media (max-width: 600px) {
  .aichat-column { padding: 0 14px; }
  .aichat-bar-inner { min-height: 56px; }
  .aichat-subheading { display: none; }
  .aichat-new { width: 36px; padding: 0; justify-content: center; }
  .aichat-new span { display: none; }
  .aichat-feed { padding-top: 20px; }
  .aichat-msg { max-width: 90%; }
  .aichat .aichat-composer textarea { font-size: 16px; }
  .aichat-note { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .aichat *, .aichat *::before, .aichat *::after { animation: none !important; transition: none !important; }
}
