/* ── Reset & Base ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg:          #f6f8fb;
  --surface:     #ffffff;
  --border:      #d8e0ea;
  --primary:     #2f5bd8;
  --primary-h:   #254ab8;
  --on-primary:  #ffffff;
  --brand:       #24324a;
  --brand-mark:  #4567b7;
  --text:        #172033;
  --muted:       #66758a;
  --success:     #166534;
  --success-bg:  #e7f6ec;
  --warn:        #92400e;
  --warn-bg:     #fff4d7;
  --card-shadow: 0 6px 20px rgba(37, 74, 145, .05);
  --radius:      10px;
  /* Компоненты чата (TA-119) — значения перенесены из D:\AI_LEGAL\bopa_rag_dep\frontend,
     образца юнита U09, один в один. */
  --bubble-radius: 14px;
  --user-bg:     #edf3ff;
  --user-border: #c9d8f5;
  --user-text:   #18345f;
  --user-avatar: #eef3ff;
  --ai-bg:       #ffffff;
  --ai-border:   #dce4ee;
  --ai-avatar:   var(--brand-mark);
  --ai-avatar-h: #3b5aa3;
}

/* Тёмная тема переключается классом .dark на <html> (см. applyTheme() в app.js).
   Палитра обязана сидеть на корне: `html, body { background: var(--bg) }` вычисляет
   переменную для каждого элемента, и объявление только на body оставляет <html> —
   а значит и холст за body — на светлом значении. */
:root.dark {
  --bg:          #0a0e1a;
  --surface:     #0f1628;
  --border:      #24304a;
  --primary:     #5b8cff;
  --primary-h:   #7ba3ff;
  /* На тёмной теме --primary светлый, поэтому подпись на нём обязана быть тёмной:
     белая дала бы примерно 3:1 вместо 4.5:1. */
  --on-primary:  #08122b;
  --brand:       #e8eefc;
  --brand-mark:  #5b8cff;
  --text:        #e6ecf7;
  --muted:       #93a3bd;
  --success:     #4ade80;
  --success-bg:  #12301f;
  --warn:        #fbbf24;
  --warn-bg:     #33270a;
  --card-shadow: 0 6px 20px rgba(0, 0, 0, .35);
  --user-bg:     #182442;
  --user-border: #2c3f6b;
  --user-text:   #dbe6ff;
  --user-avatar: #182442;
  --ai-bg:       #0f1628;
  --ai-border:   #24304a;
  --ai-avatar-h: #7ba3ff;
}

html, body { background: var(--bg); }

body {
  font: 15px/1.55 -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  min-height: 100vh;
}

#app {
  max-width: 820px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 100vh;
}

/* ── Header ───────────────────────────────────────────────────────────── */
#top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand { display: flex; flex-direction: column; gap: 2px; }
.brand-mark { font-size: 19px; font-weight: 700; color: var(--brand); letter-spacing: -.2px; }
.brand-sub  { font-size: 13px; color: var(--muted); }

.top-controls { display: flex; align-items: center; gap: 8px; }
.lang-label { font-size: 12px; color: var(--muted); }

/* ── Свёрнутый статус подключений в шапке (TA-119) ──────────────────────
   Переиспользует .conn-state/.conn-active цвета вместо своих — одно и то же
   состояние обязано выглядеть одинаково в детальной карточке ниже и здесь. */
.header-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  text-decoration: none;
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid transparent;
}
.header-chip[data-i18n="stateActive"] { background: var(--success-bg); color: var(--success); }
.header-chip[data-i18n="stateNeedsReauth"] { border-color: var(--warn); }
.header-chip:hover { opacity: .85; }

.lang-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 7px;
  font-size: 13px;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px; height: 30px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.btn-icon:hover { border-color: var(--primary); color: var(--primary); }

/* Иконка темы — чистый CSS от :root.dark, менять в JS нечего. */
.icon-sun  { display: none; }
:root.dark .icon-sun  { display: block; }
:root.dark .icon-moon { display: none; }

/* ── Cards ────────────────────────────────────────────────────────────── */
#main { flex: 1; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 18px 18px 16px;
}

.card-title { font-size: 17px; font-weight: 650; margin-bottom: 6px; }
.card-intro { font-size: 13.5px; color: var(--muted); margin-bottom: 14px; }
.tasks-summary { font-weight: 650; color: var(--text); margin-bottom: 4px; }

/* ── Connections ──────────────────────────────────────────────────────── */
.conn-list { display: flex; flex-direction: column; gap: 10px; }

.conn {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.conn-head { display: flex; align-items: center; gap: 10px; }
.conn-name { font-weight: 600; }

.conn-state {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--warn-bg);
  color: var(--warn);
}
.conn-active .conn-state { background: var(--success-bg); color: var(--success); }

/* Порванная связь — единственное состояние с рамкой: это не информация, а требование
   действия, и оно обязано быть видно без чтения подписи. */
.conn-needs_reauth { border-color: var(--warn); }

.conn-explain { font-size: 13px; color: var(--text); }

.btn-primary {
  align-self: flex-start;
  background: var(--primary);
  color: var(--on-primary);
  border: none;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary:hover { background: var(--primary-h); }

.status-line { font-size: 13px; color: var(--muted); margin-top: 12px; }

/* ── Tasks and agent activity ─────────────────────────────────────────── */
.section-title { font-size: 14px; margin: 20px 0 10px; }
.task-list, .run-list, .confirmation-list { display: flex; flex-direction: column; gap: 10px; }

.task-item, .run-item, .confirmation-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: var(--surface);
}

.task-head, .task-metadata, .task-labels, .confirmation-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.task-head { justify-content: space-between; }
.task-title, .confirmation-question { font-size: 14px; font-weight: 650; }
.task-completed .task-title { color: var(--muted); text-decoration: line-through; }
.task-state, .task-meta, .task-label, .run-state {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11.5px;
  background: var(--warn-bg);
  color: var(--warn);
}
.task-state-done { background: var(--success-bg); color: var(--success); }
.task-description { margin-top: 6px; color: var(--muted); white-space: pre-wrap; }
.task-metadata, .task-labels { margin-top: 8px; }
.task-meta { background: var(--bg); color: var(--muted); }
.task-label { background: var(--primary); color: var(--on-primary); }

.run-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.run-task { font-weight: 550; }
.run-created { color: var(--muted); font-size: 12px; }

/* ── Confirmations ────────────────────────────────────────────────────── */
.confirmation-item { border-left: 3px solid var(--warn); }
.confirmation-consequence {
  margin-top: 9px;
  padding: 9px 11px;
  border-radius: 7px;
  background: var(--warn-bg);
  color: var(--warn);
}
.confirmation-expiry { margin-top: 8px; color: var(--muted); font-size: 12px; }
.confirmation-status { margin-top: 8px; color: var(--muted); font-size: 12px; }
.confirmation-actions { margin-top: 12px; }
.btn-secondary {
  align-self: flex-start;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 13px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-secondary:hover { border-color: var(--primary); color: var(--primary); }
.btn-primary:disabled, .btn-secondary:disabled { cursor: not-allowed; opacity: .55; }

/* ── Чат-композер (TA-119) ────────────────────────────────────────────────
   Компоненты перенесены из образца (D:\AI_LEGAL\bopa_rag_dep\frontend\style.css)
   почти один в один, на уже объявленных выше переменных палитры. */
.chat-section { display: flex; flex-direction: column; gap: 10px; }

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 46vh;
  overflow-y: auto;
  padding-right: 2px;
}

.msg { display: flex; gap: 10px; max-width: 100%; }
.user-msg { flex-direction: row-reverse; }
.user-msg .bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border: 1px solid var(--user-border);
  border-radius: var(--bubble-radius);
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.5;
  max-width: 75%;
  white-space: pre-wrap;
  word-break: break-word;
}
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--user-avatar); color: var(--user-text);
  border: 1px solid var(--user-border);
  font-size: 11px; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.ai-msg { align-items: flex-start; }
.ai-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--ai-avatar); color: var(--on-primary);
  font-size: 10px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.ai-body {
  flex: 1; min-width: 0;
  background: var(--ai-bg); border: 1px solid var(--ai-border);
  border-radius: var(--bubble-radius);
  padding: 10px 14px;
}
.ai-body .answer { font-size: 14px; line-height: 1.6; color: var(--text); white-space: pre-wrap; }

.input-bar { display: flex; align-items: flex-end; gap: 8px; }
#chat-input {
  flex: 1; resize: none; overflow: hidden;
  border: 1.5px solid var(--border); border-radius: 10px;
  padding: 10px 14px; font-size: 14px; font-family: inherit;
  line-height: 1.5; outline: none;
  min-height: 42px; max-height: 120px;
  background: var(--surface); color: var(--text);
}
#chat-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(47, 91, 216, .10); }

.btn-send {
  width: 42px; height: 42px; border-radius: 10px;
  background: var(--ai-avatar); color: var(--on-primary); border: none;
  cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.btn-send:hover { background: var(--ai-avatar-h); }
.btn-send:disabled { opacity: .55; cursor: not-allowed; }

/* ── Footer ───────────────────────────────────────────────────────────── */
#bottom { font-size: 12.5px; color: var(--muted); text-align: center; }

@media (max-width: 560px) {
  .brand-sub { display: none; }
  #app { padding: 14px 12px 28px; }
  .run-item { align-items: flex-start; flex-direction: column; }
  .chat-messages { max-height: 38vh; }
}
