/* ============================================
   Аналитик продаж — веб-чат
   Темы: светлая (по умолчанию) + тёмная
   Адаптировано из стилей «AI Аналитик» (arendaai): токены дизайна, экран
   входа, сайдбар, лента сообщений, поле ввода — сохранены; всё, что
   относилось к ставкам аренды (карта инструментов, блок «Контекст», отчёты,
   вложения, настройки), убрано. Google Fonts заменены на системный стек —
   внешних ресурсов на странице нет (см. README.md, CSP).
   ============================================ */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

html.theme-switching,
html.theme-switching *,
html.theme-switching *::before,
html.theme-switching *::after {
  transition: none !important;
}

:root,
[data-theme="light"] {
  --bg-deep:       #E2E8F0;
  --bg-primary:    #F1F5F9;
  --bg-secondary:  #FFFFFF;
  --bg-tertiary:   #EEF2F7;
  --bg-hover:      #E9EEF6;
  --bg-active:     #DEE5EF;

  --glass-panel:   rgba(255, 255, 255, 0.74);
  --glass-strong:  rgba(255, 255, 255, 0.93);
  --glass-border:  rgba(255, 255, 255, 0.45);
  --glass-blur:    16px;

  --accent:         #4F46E5;
  --accent-hover:   #4338CA;
  --accent-glow:    rgba(79, 70, 229, 0.16);
  --accent-subtle:  rgba(79, 70, 229, 0.08);
  --accent-wash:    rgba(79, 70, 229, 0.04);

  --user-bubble:             #4F46E5;
  --user-bubble-border:      #4F46E5;
  --user-bubble-text:        #FFFFFF;
  --assistant-bubble:        #F8FAFC;
  --assistant-bubble-border: #E2E8F0;

  --text-primary:   #0F172A;
  --text-secondary: #475569;
  --text-muted:     #5A6678;
  --text-on-accent: #FFFFFF;

  --border:         #E2E8F0;
  --border-light:   #EEF2F7;
  --border-accent:  rgba(79, 70, 229, 0.32);

  --success:        #16A34A;
  --warning:        #B45309;
  --warning-bg:     #FEF3C7;
  --error:          #DC2626;
  --error-bg:       #FEF2F2;
  --info-bg:        #EEF0FE;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 1px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.07), 0 2px 4px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 18px 40px rgba(15, 23, 42, 0.12), 0 6px 12px rgba(15, 23, 42, 0.06);
  --shadow-glow: 0 0 0 4px var(--accent-glow);

  --gradient-bg: linear-gradient(135deg, #F4F6FE 0%, #DDE3F5 100%);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --radius-full: 9999px;

  --sidebar-width: 320px;
  --sidebar-collapsed-width: 64px;
  --header-height: 60px;

  /* Системный стек — никаких внешних шрифтов (CSP: без Google Fonts) */
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", monospace;

  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);

  color-scheme: light;
}

[data-theme="dark"] {
  --bg-deep:       #0A0C12;
  --bg-primary:    #0F1219;
  --bg-secondary:  #161A23;
  --bg-tertiary:   #1C212C;
  --bg-hover:      #242A38;
  --bg-active:     #2C3342;

  --glass-panel:   rgba(22, 26, 35, 0.74);
  --glass-strong:  rgba(22, 26, 35, 0.94);
  --glass-border:  rgba(129, 140, 248, 0.14);
  --glass-blur:    16px;

  --accent:         #818CF8;
  --accent-hover:   #A5B4FC;
  --accent-glow:    rgba(129, 140, 248, 0.24);
  --accent-subtle:  rgba(129, 140, 248, 0.14);
  --accent-wash:    rgba(129, 140, 248, 0.07);

  --user-bubble:             #4F46E5;
  --user-bubble-border:      #6366F1;
  --user-bubble-text:        #EEF0FE;
  --assistant-bubble:        #161A23;
  --assistant-bubble-border: #272D3A;

  --text-primary:   #E6E8EE;
  --text-secondary: #A3ABBA;
  --text-muted:     #8B93A3;
  --text-on-accent: #FFFFFF;

  --border:         #272D3A;
  --border-light:   #1E2330;
  --border-accent:  rgba(129, 140, 248, 0.38);

  --success:        #4ADE80;
  --warning:        #FBBF24;
  --warning-bg:     rgba(251, 191, 36, 0.12);
  --error:          #F87171;
  --error-bg:       rgba(248, 113, 113, 0.12);
  --info-bg:        rgba(129, 140, 248, 0.12);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 1px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.4), 0 2px 4px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 16px 32px rgba(0, 0, 0, 0.5), 0 4px 8px rgba(0, 0, 0, 0.35);
  --shadow-glow: 0 0 0 4px var(--accent-glow);

  --gradient-bg: linear-gradient(135deg, #12151D 0%, #0A0C12 100%);

  color-scheme: dark;
}

/* ============ RESET ============ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; height: 100dvh; overflow: hidden; }

body {
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-deep);
  -webkit-font-smoothing: antialiased;
}

input, textarea, button { font-family: inherit; font-size: inherit; color: inherit; border: none; background: none; }
button { cursor: pointer; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
:focus:not(:focus-visible) { outline: none; }

.hidden { display: none !important; }

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--bg-active); }

/* ============ LOGIN ============ */

#login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-deep);
  z-index: 1000;
}

.login-bg-grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(var(--accent-wash) 1px, transparent 1px),
    linear-gradient(90deg, var(--accent-wash) 1px, transparent 1px);
  background-size: 60px 60px;
  mask-image: radial-gradient(ellipse 60% 60% at 50% 50%, black 20%, transparent 70%);
}

.login-card {
  position: relative;
  width: min(440px, calc(100dvw - 24px));
  padding: 48px 40px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg), var(--shadow-glow);
}

.login-logo { text-align: center; margin-bottom: 36px; }
.login-logo-icon {
  width: 64px; height: 64px; margin: 0 auto 16px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 800; letter-spacing: -0.02em;
  color: var(--text-on-accent); background: var(--accent);
  border-radius: var(--radius-lg); box-shadow: var(--accent-glow);
}
.login-logo h1 { font-size: 26px; font-weight: 700; letter-spacing: -0.5px; color: var(--text-primary); }
.login-logo p { font-size: 15px; color: var(--text-secondary); margin-top: 4px; }

.input-group { margin-bottom: 20px; }
.input-group label {
  display: block; font-size: 14px; font-weight: 600; color: var(--text-secondary);
  margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.5px;
}
.input-group input {
  width: 100%; padding: 12px 16px; background: var(--bg-tertiary);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text-primary); font-size: 16px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.input-group input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.input-group input::placeholder { color: var(--text-muted); }

.login-error { font-size: 15px; color: var(--error); min-height: 20px; margin-bottom: 8px; text-align: center; }

.login-btn {
  width: 100%; padding: 13px; background: var(--accent); color: var(--text-on-accent);
  font-weight: 700; font-size: 16px; border-radius: var(--radius-full);
  transition: background 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
}
.login-btn:hover { background: var(--accent-hover); box-shadow: 0 6px 18px var(--accent-glow); }

/* ============ APP LAYOUT ============ */

#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--gradient-bg);
  gap: 16px;
  padding: 16px;
}

/* ============ SIDEBAR ============ */

#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--glass-panel);
  -webkit-backdrop-filter: blur(var(--glass-blur));
  backdrop-filter: blur(var(--glass-blur));
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-md);
  z-index: 100;
  overflow: hidden;
  transition: width 0.28s var(--ease-out), min-width 0.28s var(--ease-out);
}

.sidebar-top { display: flex; align-items: center; gap: 8px; padding: 14px 14px 8px; }

.new-chat-pill {
  flex: 1; min-width: 0; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 44px; padding: 0 16px; background: var(--accent); color: var(--text-on-accent);
  font-size: 16px; font-weight: 600; border-radius: var(--radius-full);
  box-shadow: 0 4px 14px var(--accent-glow);
  transition: background 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
}
.new-chat-pill:hover { background: var(--accent-hover); box-shadow: 0 6px 18px var(--accent-glow); }
.new-chat-label { white-space: nowrap; }

.sidebar-collapse-btn {
  display: flex; align-items: center; gap: 8px; height: 34px; margin: 2px 14px 4px; padding: 0 12px;
  color: var(--text-muted); font-size: 14.5px; font-weight: 500; border-radius: var(--radius-md);
  transition: background 0.2s var(--ease-out), color 0.2s var(--ease-out);
}
.sidebar-collapse-btn:hover { background: var(--accent-subtle); color: var(--accent); }
.sidebar-collapse-btn svg { flex-shrink: 0; transition: transform 0.28s var(--ease-out); }
.sidebar-collapse-label { white-space: nowrap; }

.sidebar-search { position: relative; padding: 6px 14px 10px; display: flex; align-items: center; }
.sidebar-search-input {
  flex: 1; width: 100%; padding: 10px 14px; font-size: 15px; color: var(--text-primary);
  background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius-md);
  transition: border-color 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
}
.sidebar-search-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.sidebar-search-input::placeholder { color: var(--text-muted); }
.sidebar-search-icon {
  display: none; align-items: center; justify-content: center; width: 40px; height: 40px;
  color: var(--text-muted); border-radius: var(--radius-full); flex-shrink: 0;
}
.sidebar-search-icon:hover { color: var(--accent); background: var(--accent-subtle); }

.session-list { flex: 1; overflow-y: auto; padding: 4px 8px; }

.session-item {
  display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: var(--radius-sm);
  cursor: pointer; transition: background 0.15s; position: relative;
}
.session-item:hover { background: var(--bg-hover); }
.session-item.active { background: var(--accent-subtle); box-shadow: inset 3px 0 0 var(--accent); color: var(--text-primary); }
.session-item.active .session-title { font-weight: 600; color: var(--text-primary); }

.session-icon {
  width: 36px; height: 36px; min-width: 36px; display: flex; align-items: center; justify-content: center;
  background: var(--bg-tertiary); border-radius: var(--radius-sm); font-size: 15px; color: var(--text-muted);
}
.session-icon--busy { color: var(--success); animation: pulse 2s infinite; }
.session-item.active .session-icon { background: var(--accent-subtle); }

.session-info { flex: 1; min-width: 0; }
.session-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-time { font-size: 13px; color: var(--text-muted); align-self: flex-start; white-space: nowrap; }

.session-empty { padding: 40px 20px; text-align: center; color: var(--text-muted); font-size: 15px; line-height: 1.6; }

.sidebar-footer {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 14px; border-top: 1px solid var(--border-light);
}

.sidebar-status {
  display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text-secondary); min-width: 0;
}
.sidebar-status .status-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success); flex-shrink: 0; animation: pulse 2s infinite; }
.status-dot--warn { background: var(--warning); }
.status-dot--error { background: var(--error); }

.logout-btn {
  display: flex; align-items: center; justify-content: center; width: 36px; height: 36px; flex-shrink: 0;
  color: var(--text-muted); border-radius: var(--radius-full);
  transition: background 0.2s var(--ease-out), color 0.2s var(--ease-out);
}
.logout-btn:hover { background: var(--error-bg); color: var(--error); }

@media (min-width: 769px) {
  #sidebar.collapsed { width: var(--sidebar-collapsed-width); min-width: var(--sidebar-collapsed-width); }
  #sidebar.collapsed .sidebar-top { justify-content: center; padding: 14px 8px 8px; }
  #sidebar.collapsed .new-chat-pill { flex: none; width: 44px; height: 44px; padding: 0; }
  #sidebar.collapsed .new-chat-label { display: none; }
  #sidebar.collapsed .sidebar-collapse-btn { justify-content: center; margin: 2px auto 4px; padding: 0; width: 40px; }
  #sidebar.collapsed .sidebar-collapse-label { display: none; }
  #sidebar.collapsed .sidebar-collapse-btn svg { transform: rotate(180deg); }
  #sidebar.collapsed .sidebar-search-input,
  #sidebar.collapsed .session-info,
  #sidebar.collapsed .session-time { display: none; }
  #sidebar.collapsed .sidebar-search { justify-content: center; }
  #sidebar.collapsed .sidebar-search-icon { display: flex; }
  #sidebar.collapsed .session-list { padding: 4px; }
  #sidebar.collapsed .session-item { justify-content: center; padding: 8px; gap: 0; }
  #sidebar.collapsed .session-item.active { box-shadow: none; background: var(--accent-subtle); }
  #sidebar.collapsed .session-item.active .session-icon { box-shadow: inset 0 0 0 1.5px var(--accent); }
  #sidebar.collapsed .sidebar-footer { flex-direction: column; }
  #sidebar.collapsed .sidebar-status { display: none; }
}
@media (max-width: 768px) {
  .sidebar-collapse-btn { display: none; }
}

/* ============ CHAT AREA ============ */

#chat-area {
  flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative;
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(var(--glass-blur));
  backdrop-filter: blur(var(--glass-blur));
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}

.chat-header {
  height: var(--header-height); display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px; border-bottom: 1px solid var(--border-light); z-index: 10;
}
.chat-header-info { display: flex; align-items: center; gap: 8px; min-width: 0; }
.chat-header-info h2 {
  font-size: 18px; font-weight: 700; min-width: 0; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; color: var(--text-primary);
}
.chat-header-actions { display: flex; align-items: center; gap: 6px; }

.icon-btn {
  width: 36px; height: 36px; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary); border: 1px solid transparent; border-radius: var(--radius-full);
  transition: background 0.2s var(--ease-out), color 0.2s var(--ease-out), border-color 0.2s var(--ease-out);
}
.icon-btn--sm { width: 30px; height: 30px; border-color: var(--border); background: var(--bg-secondary); color: var(--text-muted); }
.icon-btn:hover { background: var(--accent-subtle); border-color: var(--border-accent); color: var(--accent); }
.icon-btn.danger-hover:hover { background: var(--error-bg); border-color: var(--error); color: var(--error); }
.icon-btn:disabled, .icon-btn--sm:disabled { opacity: 0.4; cursor: default; pointer-events: none; }

.stop-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 14px;
  background: var(--error-bg); color: var(--error); border: 1px solid var(--error);
  border-radius: var(--radius-full); font-size: 14px; font-weight: 600;
}
.stop-btn:hover { background: var(--error); color: #fff; }

/* ============ MESSAGES ============ */

.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 20px 24px; display: flex; flex-direction: column; gap: 4px; scroll-behavior: smooth; }

.load-older-btn {
  align-self: center; margin-bottom: 12px; padding: 8px 16px; font-size: 14px; font-weight: 600;
  color: var(--text-secondary); background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: var(--radius-full);
}
.load-older-btn:hover { background: var(--accent-subtle); color: var(--accent); border-color: var(--accent); }

.message { display: flex; max-width: 75%; }
.message.user { align-self: flex-end; }
.message.assistant { align-self: flex-start; }

.message.system { align-self: center; max-width: 90%; }
.message.system .system-note {
  font-size: 14px; line-height: 1.5; text-align: center; white-space: pre-line;
  color: var(--text-secondary); background: var(--bg-tertiary); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 14px;
}
.message.system.danger .system-note { color: var(--error); border-color: var(--error); background: var(--error-bg); }

.message-bubble { padding: 10px 16px; border-radius: var(--radius-lg); line-height: 1.6; word-wrap: break-word; overflow-wrap: break-word; position: relative; }

.message.user .message-bubble {
  background: var(--user-bubble); border: 1px solid var(--user-bubble-border); color: var(--user-bubble-text);
  border-bottom-right-radius: 4px; box-shadow: 0 2px 8px var(--accent-glow);
}
.message.user .message-content code { background: rgba(255,255,255,0.18); color: var(--user-bubble-text); }
.message.user .message-content strong, .message.user .message-content em { color: var(--user-bubble-text); }

.message.assistant .message-bubble {
  background: var(--assistant-bubble); border: 1px solid var(--assistant-bubble-border);
  border-bottom-left-radius: 4px; box-shadow: var(--shadow-sm);
}

.message-time { font-size: 13px; color: var(--text-muted); margin-top: 4px; text-align: right; }
.message.assistant .message-time { text-align: left; }
.message.user .message-time { color: var(--user-bubble-text); opacity: 0.72; }

.date-separator { display: flex; align-items: center; justify-content: center; margin: 18px 0 10px; }
.date-separator span {
  font-size: 13px; font-weight: 600; color: var(--text-muted); background: var(--bg-primary);
  border: 1px solid var(--border); border-radius: var(--radius-full); padding: 3px 12px;
}

/* Содержимое сообщения — рендер Markdown (renderMarkdown в app.js экранирует
   HTML ДО разметки; картинки и ссылки из модели не создают <img>/<a>) */
.message-content h3 { font-size: 16px; font-weight: 700; margin: 8px 0 4px; color: var(--text-primary); }
.message-content h4 { font-size: 15px; font-weight: 600; margin: 6px 0 2px; }
.message-content p { margin: 4px 0; }
.message-content strong { font-weight: 600; color: var(--text-primary); }
.message-content em { font-style: italic; color: var(--text-secondary); }
.message-content code { font-family: var(--font-mono); font-size: 14px; background: rgba(0,0,0,0.3); padding: 2px 6px; border-radius: 4px; }
.message-content pre { margin: 8px 0; padding: 12px; background: rgba(0,0,0,0.3); border-radius: var(--radius-sm); overflow-x: auto; }
.message-content pre code { background: none; padding: 0; font-size: 14px; line-height: 1.5; }
.message-content ul, .message-content ol { margin: 6px 0; padding-left: 22px; }
.message-content li { margin: 3px 0; padding-left: 2px; }
.message-content li::marker { color: var(--text-muted); }
.message-content blockquote {
  margin: 8px 0; padding: 4px 12px; border-left: 3px solid var(--border-accent);
  color: var(--text-secondary);
}
.message.user .message-content blockquote { border-left-color: rgba(255,255,255,0.5); color: var(--user-bubble-text); }
.message-content hr { margin: 10px 0; border: none; border-top: 1px solid var(--border); }
.message-content table { border-collapse: collapse; margin: 8px 0; font-size: 14px; width: 100%; }
.message-content th, .message-content td { padding: 6px 10px; border: 1px solid var(--border); text-align: left; }
.message-content th { background: var(--bg-tertiary); font-weight: 600; }
.message.user .message-content table, .message.user .message-content th, .message.user .message-content td { border-color: rgba(255,255,255,0.3); }
.message.user .message-content th { background: rgba(255,255,255,0.12); }

/* ============ ПОТОКОВЫЙ ОТВЕТ (SSE) ============ */
/* Единый визуальный язык для «думает» и «работает с инструментами»:
   бегущие точки + мерцающая (shimmer) подпись. Раньше здесь было мигающее
   opacity-прямоугольное «streaming-cursor» — владелец счёл некрасивым, убрано. */

.message.streaming .message-bubble { padding-bottom: 8px; }

.agent-activity { margin-top: 6px; display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }

.agent-activity__thinking {
  display: inline-flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--text-muted);
}

.agent-spinner {
  flex-shrink: 0; color: var(--accent);
  animation: agentSpin 0.9s linear infinite;
  transform-origin: 50% 50%; will-change: transform;
}

.agent-activity__label,
.status-chip__text {
  background: linear-gradient(90deg, var(--text-muted) 0%, var(--text-primary) 45%, var(--text-muted) 90%);
  background-size: 220% 100%;
  -webkit-background-clip: text; background-clip: text; color: transparent;
  -webkit-text-fill-color: transparent;
  animation: agentShimmer 2.2s linear infinite;
}

.agent-activity__chips { display: flex; flex-direction: column; gap: 4px; }

.status-chip {
  display: inline-flex; align-items: center; gap: 8px; align-self: flex-start;
  padding: 4px 12px; font-size: 13.5px; color: var(--text-muted);
  background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: var(--radius-full);
  transition: opacity 0.35s var(--ease-out), transform 0.35s var(--ease-out);
  animation: statusLineIn 0.25s var(--ease-out);
}
/* Завершённый шаг: сначала видна галочка, затем плавно гаснет (удаление — через 900 мс в app.js). */
.status-chip--done { animation: statusChipDone 0.9s ease forwards; }
@keyframes statusChipDone {
  0%, 45% { opacity: 1; transform: none; }
  100% { opacity: 0; transform: translateY(-3px); }
}
@media (prefers-reduced-motion: reduce) { .status-chip--done { animation: none; opacity: 0.6; } }
.status-chip__check { color: var(--success); font-weight: 700; font-size: 13px; }

@keyframes agentSpin { to { transform: rotate(360deg); } }
@keyframes agentShimmer { 0% { background-position: 220% 0; } 100% { background-position: -220% 0; } }
@keyframes statusLineIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes messageIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.message { animation: messageIn 0.22s var(--ease-out); }

/* ============ EMPTY STATE / СТАРТОВЫЕ ВОПРОСЫ ============ */

.empty-state {
  flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center;
  justify-content: safe center; padding: 40px 40px 24px; text-align: center; overflow-y: auto;
}
.empty-logo {
  display: flex; align-items: center; justify-content: center; font-size: 44px; font-weight: 800;
  letter-spacing: -0.03em; color: var(--accent); margin-bottom: 16px;
  filter: drop-shadow(0 4px 12px var(--accent-glow));
}
.empty-state h2 { font-size: 26px; font-weight: 700; letter-spacing: -0.01em; color: var(--text-primary); margin-bottom: 6px; }
.empty-state p { color: var(--text-secondary); font-size: 15.5px; margin-bottom: 24px; max-width: 480px; }

.starter-questions { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 480px; }
.starter-question {
  display: block; width: 100%; padding: 12px 18px; text-align: left; font-size: 15px; font-weight: 500;
  color: var(--text-secondary); background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: var(--radius-md); transition: background 0.15s var(--ease-out), border-color 0.15s var(--ease-out), color 0.15s var(--ease-out);
}
.starter-question:hover { background: var(--accent-subtle); border-color: var(--accent); color: var(--accent); }

/* ============ ПОЛЕ ВВОДА ============ */

.input-area {
  padding: 12px 24px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
  border-top: 1px solid var(--border-light); width: 100%; box-sizing: border-box;
}
.message-input-box {
  width: 100%; resize: none; padding: 14px 16px; min-height: 56px; max-height: 200px; line-height: 1.5;
  color: var(--text-primary); background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: var(--radius-lg); font-size: 16px; box-sizing: border-box;
  transition: border-color 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
}
.message-input-box:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.message-input-box::placeholder { color: var(--text-muted); }

.limit-error { margin-top: 6px; font-size: 13.5px; color: var(--error); }

/* Ответ этого браузера идёт в другом диалоге — отправка закрыта (updateBusyUi). */
.run-notice {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px;
  margin-bottom: 8px; font-size: 13.5px; color: var(--text-secondary);
}
.run-notice-link {
  padding: 0; font-size: inherit; font-weight: 600; color: var(--accent);
  background: none; border: none; cursor: pointer; text-decoration: underline;
}

/* Обрыв связи с сервером: поток событий переподключается (см. connectStream). */
.conn-banner {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin: 0 auto 8px; padding: 7px 14px; max-width: 520px;
  font-size: 13.5px; color: var(--warning); background: var(--warning-bg);
  border-radius: var(--radius-full);
}
.conn-banner .agent-spinner { width: 15px; height: 15px; }

.input-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }
.char-count { font-size: 13px; color: var(--text-muted); }

.send-btn {
  display: inline-flex; align-items: center; gap: 8px; padding: 11px 26px; background: var(--accent);
  color: #FFFFFF; font-size: 16px; font-weight: 600; border-radius: var(--radius-full);
  transition: background 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out), opacity 0.2s var(--ease-out);
  flex-shrink: 0;
}
.send-btn:disabled { background: var(--bg-hover); color: var(--text-muted); cursor: default; }
.send-btn:not(:disabled):hover { background: var(--accent-hover); box-shadow: 0 6px 18px var(--accent-glow); }

/* ============ ЭКСПОРТ ============ */

.export-menu { position: relative; }
.export-menu > summary { list-style: none; cursor: pointer; }
.export-menu > summary::-webkit-details-marker { display: none; }
.export-menu__panel {
  position: absolute; top: calc(100% + 8px); right: 0; min-width: 230px; padding: 6px;
  background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg); z-index: 30; display: flex; flex-direction: column; gap: 2px;
}
.export-menu__item {
  display: block; width: 100%; padding: 8px 12px; font-size: 15px; text-align: left;
  color: var(--text-primary); border-radius: var(--radius-sm); cursor: pointer;
  transition: background 0.15s var(--ease-out);
}
.export-menu__item:hover { background: var(--accent-subtle); color: var(--accent); }

.print-meta { display: none; }

/* ============ РАЗМЕР ТЕКСТА (настройки → «Чат») ============ */

[data-text-size="large"] .message-content { font-size: 17.5px; }
[data-text-size="large"] .message-input-box { font-size: 17.5px; }

/* ============ SETTINGS MODAL ============ */

.modal {
  position: fixed; inset: 0; z-index: 1500;
  display: none; align-items: center; justify-content: center;
  padding: 32px 16px;
}
.modal.open { display: flex; }
.modal-overlay {
  position: absolute; inset: 0;
  background: rgba(15, 23, 42, 0.45);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  animation: fadeIn 0.2s ease;
}
[data-theme="dark"] .modal-overlay { background: rgba(0, 0, 0, 0.65); }
.modal-window {
  position: relative; width: 100%; max-width: 760px;
  max-height: min(calc(100dvh - 64px), 760px);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: 180px 1fr;
  grid-template-areas: "header header" "nav body";
  overflow: hidden;
  animation: modalIn 0.28s ease;
}
.modal-header {
  grid-area: header; display: flex; align-items: center; justify-content: space-between;
  padding: 18px 24px; border-bottom: 1px solid var(--border-light); background: var(--bg-secondary);
}
.modal-header h1 { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; color: var(--text-primary); }
.modal-close {
  width: 36px; height: 36px; display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary); border-radius: var(--radius-full); transition: background 0.15s, color 0.15s;
}
.modal-close:hover { background: var(--bg-hover); color: var(--text-primary); }
.modal-nav {
  grid-area: nav; padding: 16px 8px 16px 16px; border-right: 1px solid var(--border-light);
  background: var(--bg-tertiary); display: flex; flex-direction: column; gap: 2px; overflow-y: auto;
}
.modal-nav-item {
  padding: 8px 12px; font-size: 15px; color: var(--text-secondary); border-radius: var(--radius-sm);
  text-decoration: none; transition: background 0.15s, color 0.15s;
}
.modal-nav-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.modal-nav-item.active { background: var(--accent-subtle); color: var(--accent); font-weight: 600; }
.modal-body { grid-area: body; overflow-y: auto; padding: 24px 28px 32px; scroll-behavior: smooth; }

.settings-section { margin-bottom: 12px; }
.section-divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
.section-head { margin-bottom: 16px; }
.section-head h2 { font-size: 17px; font-weight: 700; letter-spacing: -0.01em; margin-bottom: 4px; color: var(--text-primary); }
.section-desc { font-size: 14px; color: var(--text-secondary); line-height: 1.5; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; }
.setting-label { display: flex; flex-direction: column; gap: 2px; }
.setting-name { font-size: 15px; font-weight: 500; color: var(--text-primary); }
.setting-hint { font-size: 13.5px; color: var(--text-muted); max-width: 340px; }

.theme-switcher { display: flex; gap: 8px; }
.theme-option {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 14px 8px;
  background: var(--bg-tertiary); border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; color: var(--text-secondary);
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.theme-option:hover { border-color: var(--accent); color: var(--text-primary); }
.theme-option[aria-checked="true"] { border-color: var(--accent); color: var(--text-primary); background: var(--accent-subtle); }
.theme-option--text { padding: 10px 18px; }
.theme-preview { width: 42px; height: 28px; border-radius: 5px; border: 1px solid var(--border); }
.theme-preview-light  { background: linear-gradient(135deg, #E2E8F0 50%, #FFFFFF 50%); border-color: #CBD5E1; }
.theme-preview-dark   { background: linear-gradient(135deg, #0A0C12 50%, #1A1F2A 50%); }
.theme-preview-system { background: linear-gradient(135deg, #F8FAFC 50%, #0A0C12 50%); }

/* iOS-style тумблер (Отправка по Enter) */
.report-switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.report-switch__input { position: absolute; opacity: 0; width: 0; height: 0; }
.report-switch__track {
  position: relative; width: 38px; height: 22px; border-radius: var(--radius-full);
  background: var(--border); transition: background 0.18s var(--ease-out); flex-shrink: 0;
}
.report-switch__knob {
  position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: var(--shadow-sm); transition: transform 0.18s var(--ease-out);
}
.report-switch__input:checked + .report-switch__track { background: var(--accent); }
.report-switch__input:checked + .report-switch__track .report-switch__knob { transform: translateX(16px); }
.report-switch__input:focus-visible + .report-switch__track { box-shadow: 0 0 0 3px var(--accent-glow); }

.about-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.about-row { display: flex; justify-content: space-between; gap: 16px; font-size: 15px; }
.about-row dt { color: var(--text-secondary); }
.about-row dd { color: var(--text-primary); font-weight: 500; }
.about-note { font-size: 14px; color: var(--text-secondary); line-height: 1.5; margin-bottom: 18px; }
.settings-logout-btn {
  padding: 9px 18px; background: var(--error-bg); color: var(--error); border: 1px solid var(--error);
  border-radius: var(--radius-full); font-size: 14.5px; font-weight: 600;
  transition: background 0.15s var(--ease-out), color 0.15s var(--ease-out);
}
.settings-logout-btn:hover { background: var(--error); color: #fff; }

@keyframes modalIn {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 640px) {
  .modal { padding: 0; align-items: stretch; }
  .modal-window {
    max-width: none; max-height: 100dvh; border-radius: 0;
    grid-template-columns: 1fr; grid-template-areas: "header" "nav" "body";
  }
  .modal-nav {
    flex-direction: row; overflow-x: auto; padding: 8px 12px;
    border-right: none; border-bottom: 1px solid var(--border-light); gap: 4px;
  }
  .modal-nav-item { white-space: nowrap; padding: 8px 14px; }
  .modal-body { padding: 16px; }
  .setting-row { flex-direction: column; align-items: stretch; }
  .theme-switcher { justify-content: space-between; }
  .theme-option { flex: 1; }
}

/* ============ TOAST ============ */

.toast-container {
  position: fixed; bottom: 24px; right: 24px; z-index: 2000;
  display: flex; flex-direction: column; gap: 8px; max-width: 360px;
}
.toast {
  display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px;
  background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}
.toast.info { border-left: 3px solid var(--accent); }
.toast.warn { border-left: 3px solid var(--warning); }
.toast.danger { border-left: 3px solid var(--error); }
.toast-body { flex: 1; }
.toast-title { font-weight: 600; font-size: 15px; margin-bottom: 2px; color: var(--text-primary); }
.toast-desc { font-size: 14px; color: var(--text-secondary); line-height: 1.5; }
.toast-close { color: var(--text-muted); padding: 2px; border-radius: 4px; flex-shrink: 0; }
.toast-close:hover { color: var(--text-primary); }

/* ============ MOBILE SIDEBAR ============ */

.sidebar-toggle {
  display: none; position: fixed; top: calc((var(--header-height) - 44px) / 2); left: 12px; z-index: 200;
  width: 44px; height: 44px; align-items: center; justify-content: center;
  background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: var(--radius-sm); color: var(--text-primary);
}
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 90; opacity: 0; transition: opacity 0.3s; }
.sidebar-overlay.visible { display: block; opacity: 1; }

@media (max-width: 768px) {
  .sidebar-toggle { display: flex; top: calc(env(safe-area-inset-top, 0px) + (var(--header-height) - 44px) / 2); }
  #app { padding: 0; gap: 0; }
  #chat-area { border: none; border-radius: 0; box-shadow: none; }
  #sidebar {
    position: fixed; left: 0; top: 0; height: 100vh; height: 100dvh; border-radius: 0; border: none;
    border-right: 1px solid var(--border); transform: translateX(-100%);
    transition: transform 0.3s var(--ease-out); box-shadow: var(--shadow-lg); width: var(--sidebar-width); min-width: var(--sidebar-width);
  }
  #sidebar.open { transform: translateX(0); }
  .message { max-width: 88%; }
  .chat-header { padding-left: 64px; padding-right: 12px; padding-top: env(safe-area-inset-top, 0); }
  .chat-header-info { min-width: 0; flex: 1; }
  .empty-state { padding: 24px 16px; }
  .icon-btn { width: 44px; height: 44px; }
  .icon-btn--sm { width: 38px; height: 38px; }
  .logout-btn { width: 44px; height: 44px; }
  .send-btn { width: auto; height: auto; min-height: 44px; }
  .input-area { padding: 8px 12px 12px; padding-bottom: max(12px, env(safe-area-inset-bottom, 12px)); }
  .toast-container { bottom: 16px; right: 12px; left: 12px; max-width: none; }
}

@media (max-width: 480px) {
  .login-card { padding: 32px 24px; }
  .login-logo h1 { font-size: 22px; }
}

/* ============ ПЕЧАТЬ / «Сохранить в PDF» ============ */

@media print {
  html, body { height: auto !important; overflow: visible !important; }
  body { background: #fff !important; }

  #login-screen, #sidebar, .sidebar-toggle, #sidebar-overlay, #input-area,
  .chat-header-actions, .chat-header-info .icon-btn, #empty-state, #toast-container,
  #settings-modal, .message.system, .message.streaming {
    display: none !important;
  }

  #app { display: block !important; height: auto !important; overflow: visible !important; padding: 0 !important; background: #fff !important; }
  #chat-area {
    display: block !important; height: auto !important; min-height: 0 !important; overflow: visible !important;
    background: #fff !important; border: none !important; border-radius: 0 !important; box-shadow: none !important;
    -webkit-backdrop-filter: none !important; backdrop-filter: none !important;
  }
  .chat-header { height: auto !important; padding: 0 0 4px !important; border-bottom: none; }
  .print-meta { display: block !important; padding: 0 0 10px; font-size: 13px; color: #666; border-bottom: 1px solid #ccc; }
  .messages { display: block !important; height: auto !important; overflow: visible !important; padding: 12px 0 !important; }
  .message { max-width: 100% !important; margin-bottom: 10px; }
  .message.user { margin-left: auto; max-width: 82% !important; justify-content: flex-end; }
  .message-bubble { box-shadow: none !important; border: 1px solid #d5d5d5 !important; }
  .message.user .message-bubble {
    background: #eef0fb !important; -webkit-print-color-adjust: exact; print-color-adjust: exact;
    border: 1px solid #b9c0ee !important; color: #1a1a1a !important;
  }
  .message.user .message-content code { background: #e3e6f7 !important; color: #1a1a1a !important; }
  .message.user .message-content strong, .message.user .message-content em { color: #1a1a1a !important; }
  .message.user .message-time { color: #777 !important; }
  .message-content table { display: table !important; width: 100% !important; table-layout: fixed; overflow: visible !important; word-break: break-word; }
  .message-content pre { white-space: pre-wrap !important; word-break: break-word; overflow: visible !important; }
}
