:root {
  --bg: #f6f6f4;
  --panel: #ffffff;
  --line: #dcdcd6;
  --text: #1c1c1a;
  --muted: #6d6d66;
  --accent: #2f6fde;
  --user: #e6efff;
  --tool: #f0f0ec;
  --busy: #d98a00;
  --idle: #2e9d5b;
  --wait: #c7402d;
  --off: #a0a09a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --panel: #1f2023;
    --line: #34363b;
    --text: #e6e6e3;
    --muted: #9a9b9f;
    --accent: #6ea0ff;
    --user: #24344f;
    --tool: #26282c;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, textarea, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button[type=submit] { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: .5; cursor: default; }
button.link { border: 0; background: none; color: var(--accent); padding: 4px 6px; }
button.danger { border-color: var(--wait); color: var(--wait); }
input, textarea, select {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 6px;
  padding: 8px 10px;
  width: 100%;
}

.login {
  max-width: 320px;
  margin: 18vh auto 0;
  padding: 0 16px;
  display: grid;
  gap: 12px;
}
.login h1 { margin: 0 0 4px; font-size: 22px; }
.error { color: var(--wait); margin: 0; min-height: 1.4em; }

.app { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
.sidebar {
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar header { display: flex; align-items: center; gap: 8px; padding: 12px 14px 8px; }
.sidebar header .link { margin-left: auto; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--off); }
.conn.on { background: var(--idle); }
.filter { margin: 0 14px 8px; width: auto; }
.new-session {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 24px;
  height: 24px;
  padding: 0;
  border-radius: 50%;
  font-size: 17px;
  line-height: 1;
}
.sessions { overflow-y: auto; flex: 1; padding-bottom: 12px; }

.agent {
  padding: 10px 14px 4px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  display: flex;
  gap: 6px;
  align-items: center;
}
.session {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  gap: 2px 8px;
  align-items: center;
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 0;
  background: none;
  padding: 7px 14px;
}
.session:hover { background: var(--bg); }
.session.active { background: var(--user); }
.session .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session .when { color: var(--muted); font-size: 12px; }
.session .meta {
  grid-column: 2 / 4;
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--off); }
.dot.busy { background: var(--busy); }
.dot.idle { background: var(--idle); }
.dot.waiting { background: var(--wait); }
.empty { padding: 14px; color: var(--muted); }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.back { display: none; }
.titles { min-width: 0; flex: 1; }
.title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.subtitle { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  white-space: nowrap;
}
.badge.busy { border-color: var(--busy); color: var(--busy); }
.badge.idle { border-color: var(--idle); color: var(--idle); }
.badge.waiting { border-color: var(--wait); color: var(--wait); }

.feed { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.item { max-width: 900px; width: 100%; margin: 0 auto; }
.item .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.item.user .body {
  background: var(--user);
  border-radius: 8px;
  padding: 8px 12px;
}
.item.assistant.text .body { padding: 2px 0; }
.item.system .body, .item.error .body { color: var(--wait); font-size: 13px; }
.item.pending .body { opacity: .6; }
.item details {
  background: var(--tool);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 13px;
}
.item summary { cursor: pointer; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item details .body {
  font: 12px/1.4 ui-monospace, Consolas, monospace;
  max-height: 360px;
  overflow: auto;
  margin-top: 4px;
}
/* Markdown в сообщениях */
.item .body.md { white-space: normal; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .6em; white-space: pre-wrap; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { margin: .9em 0 .4em; line-height: 1.25; }
.md h1 { font-size: 1.35em; }
.md h2 { font-size: 1.2em; }
.md h3 { font-size: 1.08em; }
.md h4, .md h5, .md h6 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .6em; padding-left: 1.5em; }
.md li { margin: .15em 0; white-space: pre-wrap; }
.md li > ul, .md li > ol { margin: .15em 0 0; white-space: normal; }
.md code {
  font: .88em/1.4 ui-monospace, Consolas, monospace;
  background: var(--tool);
  border-radius: 4px;
  padding: 1px 5px;
}
.item.user .md code { background: rgba(127, 127, 127, .18); }
.md pre {
  background: var(--tool);
  border-radius: 6px;
  padding: 10px 12px;
  margin: 0 0 .6em;
  overflow-x: auto;
}
.md pre code { background: none; padding: 0; font-size: 12.5px; white-space: pre; }
.md blockquote {
  margin: 0 0 .6em;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--line);
  color: var(--muted);
}
.md hr { border: 0; border-top: 1px solid var(--line); margin: .8em 0; }
.md a { color: var(--accent); }
.md .table-wrap { overflow-x: auto; margin: 0 0 .6em; }
.md table { border-collapse: collapse; font-size: .95em; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; text-align: left; vertical-align: top; }
/* Слова в ячейках не рвутся посередине: слишком широкая таблица прокручивается по горизонтали. */
.md th, .md td { overflow-wrap: break-word; }
.md th { background: var(--tool); font-weight: 600; }
.md th.center, .md td.center { text-align: center; }
.md th.right, .md td.right { text-align: right; }
.md input[type=checkbox] { width: auto; margin: 0 2px 0 0; vertical-align: middle; }

.time { color: var(--muted); font-size: 11px; margin-top: 2px; }

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  align-items: flex-end;
}
.composer textarea { resize: vertical; max-height: 40vh; }
.hint { margin: 0; padding: 0 16px 8px; background: var(--panel); color: var(--muted); font-size: 12px; min-height: 0; }
.hint.error { color: var(--wait); }

/* Окно запуска новой сессии */
.launch {
  width: min(440px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  max-height: 90dvh;
}
.launch::backdrop { background: rgba(0, 0, 0, .4); }
.launch form { display: grid; gap: 12px; padding: 16px; }
.launch h2 { margin: 0; font-size: 17px; }
.launch label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.launch .hint { padding: 0; background: none; }
.launch .actions { display: flex; justify-content: flex-end; gap: 8px; }

html, body { overflow: hidden; }
button, .session { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.feed, .sessions { overscroll-behavior: contain; }
.composer textarea { min-height: 40px; }
.hint:empty { display: none; }
button .icon { display: none; }

/* Телефон: один экран за раз — либо список сессий, либо лента. */
@media (max-width: 760px) {
  body { font-size: 16px; }
  /* Поля ввода не мельче 16px, иначе мобильный браузер масштабирует страницу при фокусе. */
  input, textarea { font-size: 16px; }

  .app { grid-template-columns: 1fr; }
  .app .main { display: none; }
  .app.viewing .main { display: flex; }
  .app.viewing .sidebar { display: none; }

  .sidebar { border-right: 0; }
  .sidebar header {
    padding: calc(10px + env(safe-area-inset-top)) 16px 8px;
    font-size: 18px;
  }
  .sidebar header .link { min-height: 44px; padding: 0 8px; font-size: 15px; }
  .filter { margin: 0 16px 8px; padding: 10px 12px; }
  .new-session { width: 32px; height: 32px; font-size: 20px; }
  .sessions { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .agent { padding: 12px 16px 4px; }
  .session { padding: 10px 16px; min-height: 60px; gap: 3px 10px; border-bottom: 1px solid var(--line); }
  .session .meta, .session .when { font-size: 13px; }

  .top {
    padding: calc(6px + env(safe-area-inset-top)) 10px 6px 4px;
    gap: 6px;
  }
  .back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    font-size: 24px;
    line-height: 1;
  }
  .title { font-size: 15px; }
  .badge { font-size: 11px; padding: 2px 7px; }
  /* На узком экране кнопки действий — круглые значки, подписи остаются в aria-label. */
  button .icon { display: inline; }
  button .label { display: none; }
  #interrupt, .composer button {
    flex: none;
    width: 44px;
    height: 44px;
    min-height: 44px;
    padding: 0;
    border-radius: 50%;
    line-height: 1;
  }
  #interrupt { font-size: 16px; }
  .composer button { font-size: 22px; font-weight: 600; }

  .feed { padding: 12px 12px 16px; gap: 10px; }
  .item.user .body { padding: 8px 11px; }
  .item details { padding: 6px 10px; }
  .item summary { padding: 4px 0; }
  .item details .body { max-height: 50dvh; }
  .md table { font-size: .88em; }
  .md th, .md td { padding: 4px 8px; }
  .md pre { padding: 8px 10px; }
  .md pre code { font-size: 12px; }
  .md ul, .md ol { padding-left: 1.25em; }

  .hint { order: 1; padding: 6px 12px 0; }
  .composer {
    order: 2;
    border-top: 0;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    gap: 6px;
  }
  /* Подсказка стоит над полем ввода; линия сверху рисуется у верхнего из двух блоков. */
  .composer:not(:has(+ .hint:not(:empty))) { border-top: 1px solid var(--line); }
  .hint:not(:empty) { border-top: 1px solid var(--line); }
  .composer textarea { resize: none; padding: 9px 12px; border-radius: 18px; }
}
