:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --ink: #14181d;
  --ink-2: #5b6570;
  --line: #d9dfe6;
  --accent: #0f7a3d;
  --accent-soft: #e3f3ea;
  --warn: #a3541c;
  --warn-soft: #fdf1e3;
  --bad: #a12622;
  --bad-soft: #fbe9e8;
  --user: #14304a;
  --radius: 12px;
  --tap: 44px;
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #171b21;
    --surface-2: #1f242b;
    --ink: #e8ecf1;
    --ink-2: #98a3b0;
    --line: #2a3138;
    --accent: #4ec27f;
    --accent-soft: #16281f;
    --warn: #e0a063;
    --warn-soft: #2b2117;
    --bad: #e07a76;
    --bad-soft: #2c1918;
    --user: #22384f;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

code { font: 500 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }

/* ---------- topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.mark {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  border-radius: 8px;
  background:
    conic-gradient(from 210deg, #2f6fd0 0 25%, #e8b93c 25% 50%, #d1495b 50% 75%, #3aa66a 75% 100%);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12);
}

.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-size: 15px; letter-spacing: -.01em; }
.brand-text small { color: var(--ink-2); font-size: 12px; }

.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.pill {
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink-2);
  white-space: nowrap;
}
.pill[data-state="ok"] { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.pill[data-state="bad"] { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.pill[data-state="checking"] { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

/* ---------- layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  padding: 16px;
  max-width: 1180px;
  margin: 0 auto;
}

@media (min-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr) 300px; align-items: start; }
}

.chat-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

/* ---------- session bar ---------- */

.session-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
}

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field.grow { flex: 1 1 220px; }
.field.narrow { flex: 0 1 110px; }
.field span { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); }

input, select, textarea, button { font: inherit; color: inherit; }

input, select, textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 9px 11px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
textarea { resize: vertical; min-height: 52px; }

button {
  min-height: var(--tap);
  padding: 9px 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
@media (prefers-color-scheme: dark) { button { color: #08130c; } }

button.ghost {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink-2);
  font-weight: 500;
  font-size: 13.5px;
}
button.ghost.danger { color: var(--bad); border-color: var(--bad-soft); background: var(--bad-soft); }
button[disabled] { opacity: .55; cursor: progress; }

/* ---------- scenarios ---------- */

.scenarios { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  min-height: 36px;
  padding: 7px 11px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink-2);
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- log ---------- */

.log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }

.hint {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--ink-2);
  font-size: 13.5px;
}

.turn { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.bubble {
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.turn.user .bubble { background: var(--user); color: #f2f6fa; border-color: transparent; }
.turn.assistant .bubble { border-left: 3px solid var(--accent); }
.turn.assistant[data-status="clarify"] .bubble { border-left-color: #d8a13c; }
.turn.assistant[data-status="error"] .bubble { border-left-color: var(--bad); }
.turn.assistant[data-status="escalated"] .bubble { border-left-color: var(--warn); }

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  font-size: 12px;
  color: var(--ink-2);
}

.tag {
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-weight: 600;
  letter-spacing: .01em;
}
.tag.status-ok { color: var(--accent); }
.tag.status-clarify { color: #a3721b; }
.tag.status-error { color: var(--bad); }
.tag.status-escalated { color: var(--warn); }

.trace {
  font: 500 11.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--ink-2);
  background: none;
  border: none;
  min-height: 32px;
  padding: 4px 6px;
  text-decoration: underline dotted;
  overflow-wrap: anywhere;
  text-align: left;
}

.answers { display: flex; flex-wrap: wrap; gap: 8px; }
.answers .act {
  min-height: 40px;
  padding: 8px 12px;
  font-size: 13.5px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid transparent;
  font-weight: 600;
}
.links { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.links a { color: var(--accent); }

details.raw { font-size: 12.5px; }
details.raw summary { cursor: pointer; color: var(--ink-2); min-height: 32px; }
details.raw pre {
  margin: 6px 0 0;
  padding: 10px;
  border-radius: 10px;
  background: var(--surface-2);
  overflow-x: auto;
  font: 400 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.pending .bubble { color: var(--ink-2); font-style: italic; }
.pending .bubble::after {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-left: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }
@media (prefers-reduced-motion: reduce) { .pending .bubble::after { animation: none; } }

/* ---------- composer ---------- */

.composer {
  position: sticky;
  bottom: 0;
  padding: 12px;
  margin: 0 -16px -16px;
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.composer-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.composer-row.options { align-items: center; gap: 14px; margin-top: 8px; font-size: 13px; color: var(--ink-2); }
.send { flex: 0 0 auto; min-width: 116px; }
.spacer { flex: 1 1 auto; }
.counter { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.counter.over { color: var(--bad); font-weight: 600; }

.check { display: inline-flex; align-items: center; gap: 8px; min-height: 36px; font-size: 13.5px; cursor: pointer; }
.check input { width: 20px; height: 20px; min-height: 0; padding: 0; accent-color: var(--accent); }

/* ---------- panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  min-width: 0;
}
.panel[hidden] { display: none; }
.panel h2 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-2); }

.radio {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 9px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  cursor: pointer;
  font-size: 14px;
}
.radio input { margin-top: 3px; width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--accent); }
.radio span { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.radio small { color: var(--ink-2); font-size: 12px; }
.radio:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

.scopes { border: 1px solid var(--line); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; gap: 2px; margin: 0; }
.scopes legend { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); padding: 0 4px; }

.note { margin: 0; font-size: 12px; color: var(--ink-2); }

.switch { min-height: var(--tap); }

@media (max-width: 899px) {
  .panel { order: 3; }
  .composer { margin: 0 -16px -16px; }
  /* Поле на всю ширину, channel и «Отправить» — одной строкой ниже. */
  .composer-row .field.grow { flex: 1 1 100%; }
  .composer-row .field.narrow { flex: 0 1 120px; }
  .composer-row .send { flex: 1 1 auto; }
  .composer textarea { min-height: 44px; }
  /* Чипы сценариев — полноценные точки касания на телефоне. */
  .chip { min-height: 44px; }
}
