:root {
  --bg: #0a0a0a;
  --bg2: #141414;
  --bg3: #1e1e1e;
  --border: #2a2a2a;
  --fg: #f0f0f0;
  --muted: #9a9a9a;
  --faint: #6a6a6a;
  --accent: #ffffff;
  --red: #ff6b6b;
  --yellow: #ffd166;
  --radius: 8px;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; background: var(--bg); color: var(--fg); font-family: var(--sans); -webkit-font-smoothing: antialiased; }
body { display: flex; flex-direction: column; }
.hidden { display: none !important; }

.view { flex: 1; display: flex; min-height: 0; }

/* ===== Кнопки ===== */
.btn {
  font-family: var(--sans); border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--bg3); color: var(--fg);
  padding: 9px 14px; font-size: 13px; font-weight: 600; cursor: pointer;
  transition: filter .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn:hover { filter: brightness(1.2); }
.btn-primary { background: var(--fg); color: #0a0a0a; border-color: var(--fg); }
.btn-danger { background: transparent; color: var(--red); border-color: #5a2a2a; }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; margin-top: 6px; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* ===== Алерты ===== */
.alert { border-radius: var(--radius); padding: 10px 12px; font-size: 13px; margin-bottom: 12px; }
.alert-error { background: rgba(255,107,107,.08); border: 1px solid #5a2a2a; color: var(--red); }

/* ===== Логин ===== */
#view-login { align-items: center; justify-content: center; padding: 24px; }
.login-box { width: 100%; max-width: 380px; background: var(--bg2); border: 1px solid var(--border); border-radius: 12px; padding: 28px 24px; }
.logo { font-family: var(--mono); font-size: 20px; font-weight: 700; }
.login-sub { color: var(--muted); font-size: 13px; margin: 8px 0 20px; }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input, .composer textarea, select {
  width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--fg); padding: 10px 12px; font-size: 14px; font-family: var(--sans); outline: none;
}
.field input:focus, .composer textarea:focus, select:focus { border-color: var(--muted); }
.divider { display: flex; align-items: center; gap: 10px; color: var(--faint); font-size: 11px; margin: 16px 0; text-transform: uppercase; letter-spacing: 1px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.host-card { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; margin-bottom: 14px; }
.host-card-title { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: 1px; margin-bottom: 6px; }
.host-card-user { font-size: 14px; margin-bottom: 4px; }
.host-card-status { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); }

/* ===== Топбар ===== */
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; background: var(--bg2); border-bottom: 1px solid var(--border); }
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.logo-mini { font-family: var(--mono); color: var(--fg); font-weight: 700; }
.conn-user { font-size: 13px; color: var(--muted); }

/* ===== Подключения ===== */
#view-conn { flex-direction: column; }
.conn-wrap { flex: 1; overflow-y: auto; padding: 20px 16px; max-width: 560px; width: 100%; margin: 0 auto; }
.conn-title { font-size: 18px; font-family: var(--mono); margin-bottom: 6px; }
.conn-hint { font-size: 12px; color: var(--muted); margin-bottom: 18px; }
.host-list { display: flex; flex-direction: column; gap: 10px; }
.host-item { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; cursor: pointer; transition: border-color .15s, background .15s; }
.host-item:hover { border-color: var(--muted); background: var(--bg3); }
.host-item .h-user { font-size: 14px; font-weight: 600; }
.host-item .h-meta { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.host-item .h-actions { margin-top: 10px; display: flex; gap: 8px; }
.host-offline { opacity: .55; }
.h-connect {
  flex: 1; background: var(--fg); color: #0a0a0a; border: none;
  border-radius: 8px; padding: 10px 14px; font-size: 13px; font-weight: 700;
  cursor: pointer; touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.h-connect:active { transform: scale(0.97); }
.h-del {
  background: transparent; border: 1px solid #3a2020; color: #f88;
  border-radius: 8px; padding: 10px 14px; font-size: 13px; font-weight: 600;
  cursor: pointer; -webkit-tap-highlight-color: rgba(255,107,107,.2);
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.h-del:hover { background: rgba(255,107,107,.1); }
.h-del:active { background: rgba(255,107,107,.2); transform: scale(0.97); }
.invite-row { margin-top: 24px; display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 13px; color: var(--muted); }
.pwa-install {
  margin-top: 18px; display: flex; align-items: center; justify-content: center; gap: 8px;
  font-size: 14px; padding: 12px;
}
.pwa-install.hidden { display: none; }
.empty { color: var(--faint); text-align: center; padding: 30px 10px; font-size: 13px; }
.empty-hint { color: var(--muted); font-size: 14px; padding: 60px 20px; }

/* ===== Статус-точка ===== */
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot-on { background: #4ade80; box-shadow: 0 0 6px #4ade80; }
.dot-off { background: var(--faint); }
.dot-red { background: var(--red); }

/* ===== Чат: layout ===== */
#view-chat { position: relative; }
#sidebar {
  flex: 0 0 320px; border-right: 1px solid var(--border); background: var(--bg2);
  display: flex; flex-direction: column; min-width: 0;
}
#chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* Бургер (мобильный) / назад (десктоп) */
.btn-burger { display: none; font-size: 20px; padding: 2px 10px; }
.btn-back-side { display: inline-flex; }

/* Оверлей сайдбара */
.sidebar-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 90;
}
.sidebar-overlay.hidden { display: none; }

.side-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.side-host-wrap { flex: 1; min-width: 0; }
.side-host { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-status { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); margin-top: 2px; }

.proj-row { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.proj-row select { width: auto; flex: 1; padding: 5px 8px; font-size: 12px; background: var(--bg); }

.session-list { flex: 1; overflow-y: auto; padding: 8px; }
.session-item {
  padding: 12px 14px; border-radius: var(--radius);
  cursor: pointer; border: 1px solid var(--border);
  margin-bottom: 6px; background: var(--bg2);
  -webkit-tap-highlight-color: rgba(255,255,255,.05);
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
  transition: background .12s, border-color .12s, transform .05s;
}
.session-item:hover { background: var(--bg3); border-color: var(--muted); }
.session-item:active { background: var(--bg3); transform: scale(0.98); }
.session-item.active { background: var(--bg3); border-color: var(--fg); box-shadow: 0 0 0 1px var(--fg) inset; }
.session-item .s-title { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .s-preview { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.session-item .s-meta { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.session-item .s-time { font-size: 10px; color: var(--faint); font-family: var(--mono); }
.session-item .s-del { background: transparent; border: none; color: var(--faint); cursor: pointer; font-size: 12px; padding: 2px 6px; }
.session-item .s-del:hover { color: var(--red); }
.side-new { margin: 8px; width: calc(100% - 16px); }

/* ===== Чат-хедер ===== */
.chat-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--bg2); min-height: 48px; }
.chat-title-wrap { flex: 1; min-width: 0; }
.chat-title { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); }
.chat-sub { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.chat-head-right { display: flex; align-items: center; gap: 10px; }

.chat-messages { flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 14px; }

/* ===== Сообщения ===== */
.msg { max-width: 92%; display: flex; flex-direction: column; gap: 4px; }
.msg-user { align-self: flex-end; }
.msg-assistant { align-self: flex-start; }
.msg-bubble { padding: 9px 12px; border-radius: var(--radius); font-size: 13.5px; line-height: 1.55; white-space: pre-wrap; word-wrap: break-word; }
.msg-user .msg-bubble { background: var(--fg); color: #0a0a0a; border-bottom-right-radius: 2px; font-weight: 500; }
.msg-assistant .msg-bubble { background: var(--bg2); border: 1px solid var(--border); border-bottom-left-radius: 2px; }
.msg-label { font-size: 10px; color: var(--faint); font-family: var(--mono); padding: 0 4px; }
.msg-error { align-self: flex-start; width: 100%; }
.msg-error .msg-bubble { background: rgba(255,107,107,.06); border: 1px solid #5a2a2a; color: var(--red); }
.msg-tool .msg-bubble { background: var(--bg); color: var(--muted); font-family: var(--mono); font-size: 12px; }

/* ===== Живой tool ===== */
.msg-tool { align-self: flex-start; }
.tool-head {
  font-size: 11px; color: var(--faint); font-family: var(--mono);
  text-transform: uppercase; letter-spacing: .5px; margin-bottom: 4px;
}
.tool-body {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 10px; font-size: 12px; color: var(--muted); font-family: var(--mono);
  white-space: pre-wrap; word-wrap: break-word;
}
.tool-body.collapsed { display: none; }

/* ===== Рассуждение ===== */
.msg-reasoning { align-self: flex-start; width: 100%; max-width: 100%; }
.msg-reasoning .reasoning-head {
  font-size: 11px; color: var(--faint); font-family: var(--mono);
  text-transform: uppercase; letter-spacing: .5px; margin-bottom: 4px;
}
.msg-reasoning .reasoning-body {
  background: rgba(255,255,255,.03); border: 1px dashed var(--border);
  border-radius: var(--radius); padding: 8px 10px; font-size: 12px;
  color: var(--muted); white-space: pre-wrap; word-wrap: break-word;
  max-height: 200px; overflow-y: auto;
}

/* ===== Живой блок ===== */
.msg-live .msg-bubble { min-width: 40px; }
.msg-live .msg-bubble:empty::after {
  content: '▍'; animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.thinking { color: var(--muted); font-size: 12px; font-family: var(--mono); display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.thinking-bubble { color: var(--muted); font-size: 12px; font-family: var(--mono); font-style: italic; }
.thinking .dots::after { content: ''; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* markdown */
.msg-bubble pre { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; overflow-x: auto; margin: 6px 0; font-size: 12.5px; }
.msg-bubble code { font-family: var(--mono); font-size: 12px; }
.msg-bubble p { margin: 0 0 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { padding-left: 18px; margin: 6px 0; }
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 { margin: 10px 0 6px; font-size: 1.1em; }
.msg-bubble a { color: #9ecbff; }
.msg-bubble blockquote { border-left: 3px solid var(--border); padding-left: 10px; color: var(--muted); margin: 6px 0; }

/* ===== Панель сессии (agent / model / variant) ===== */
.session-bar {
  display: flex; gap: 6px; padding: 8px 10px; border-top: 1px solid var(--border);
  background: var(--bg2);
}
#sel-agent { flex: 1.4; min-width: 0; }
#sel-variant { flex: 1; min-width: 0; }
.picker { flex: 2.4; min-width: 0; }
.session-bar select {
  flex: 1; min-width: 0; background: var(--bg); border: 1px solid var(--border);
  color: var(--fg); font-size: 12px; padding: 6px 8px; border-radius: var(--radius);
  min-height: 30px;
}
.session-bar select option { background: var(--bg2); color: var(--fg); }
.session-bar select:focus { border-color: var(--muted); outline: none; }

/* ===== Кастомный dropdown моделей ===== */
.picker { position: relative; min-width: 0; }
.picker-btn {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 6px;
  background: var(--bg); border: 1px solid var(--border); color: var(--fg);
  font-size: 12px; padding: 6px 8px; border-radius: var(--radius); cursor: pointer;
  -webkit-tap-highlight-color: rgba(255,255,255,.05); touch-action: manipulation;
}
.picker-btn:hover { border-color: var(--muted); }
.picker-label { display: flex; align-items: center; gap: 6px; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--fg); }
.picker-label span { overflow: hidden; text-overflow: ellipsis; color: var(--fg); }
.picker-label svg { flex-shrink: 0; color: var(--muted); }
.picker-arrow { color: var(--faint); font-size: 10px; flex-shrink: 0; }
.picker-menu {
  position: absolute; z-index: 50; left: 0; right: 0; bottom: calc(100% + 4px);
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  max-height: 260px; overflow-y: auto; box-shadow: 0 -4px 20px rgba(0,0,0,.5);
}
.picker-menu.hidden { display: none; }
.picker-group {
  padding: 6px 10px 2px; font-size: 10px; color: var(--faint);
  text-transform: uppercase; letter-spacing: .5px; font-family: var(--mono);
  position: sticky; top: 0; background: var(--bg2);
}
.picker-item {
  display: flex; align-items: center; gap: 7px; padding: 7px 10px;
  font-size: 12px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.picker-item:hover { background: var(--bg3); }
.picker-item.active { background: var(--bg3); box-shadow: 0 0 0 1px var(--muted) inset; }
.picker-item svg { flex-shrink: 0; color: var(--muted); }
.picker-item span { overflow: hidden; text-overflow: ellipsis; }

.plan-hint {
  background: rgba(255,209,102,.08); border-top: 1px solid #5a4a20;
  color: var(--yellow); font-size: 12px; padding: 8px 12px; text-align: center;
}
.plan-hint.hidden { display: none; }

/* ===== Композер ===== */
.composer { border-top: 1px solid var(--border); background: var(--bg2); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }
.composer textarea { resize: none; font-family: var(--sans); line-height: 1.4; max-height: 140px; }
.composer-actions { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.hint { font-size: 11px; color: var(--faint); font-family: var(--mono); }

/* ===== Тоггл ===== */
.toggle-wrap { display: inline-flex; align-items: center; cursor: pointer; }
.toggle-wrap input { display: none; }
.toggle { width: 32px; height: 18px; border-radius: 9px; background: var(--bg3); border: 1px solid var(--border); position: relative; transition: background .15s; }
.toggle::after { content: ''; position: absolute; top: 1px; left: 1px; width: 14px; height: 14px; border-radius: 50%; background: var(--muted); transition: left .15s, background .15s; }
.toggle-wrap input:checked + .toggle { background: var(--muted); border-color: var(--muted); }
.toggle-wrap input:checked + .toggle::after { left: 15px; background: #0a0a0a; }

/* ===== Overlay загрузки ===== */
.loading-overlay {
  position: fixed; inset: 0; background: rgba(10,10,10,.85);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.loading-overlay.hidden { display: none; }
.loading-box { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.spinner-big {
  width: 34px; height: 34px; border: 3px solid rgba(240,240,240,.2);
  border-top-color: #f0f0f0; border-radius: 50%; animation: spin .7s linear infinite;
}
.loading-text { font-size: 13px; color: var(--muted); font-family: var(--mono); }

/* ===== Спиннер в чате (сбоку) ===== */
.chat-loading { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 30px 10px; color: var(--muted); font-size: 13px; }
.chat-loading .spinner { width: 18px; height: 18px; border-width: 2px; border-color: rgba(240,240,240,.2); border-top-color: #f0f0f0; }

/* ===== Модалка ===== */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.7); display: flex; align-items: flex-end; justify-content: center; z-index: 100; }
@media (min-width: 640px) { .modal-backdrop { align-items: center; } }
.modal { width: 100%; max-width: 480px; background: var(--bg2); border: 1px solid var(--border); border-top-left-radius: 12px; border-top-right-radius: 12px; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
@media (min-width: 640px) { .modal { border-radius: 12px; } }
.modal-title { font-weight: 700; font-size: 14px; margin-bottom: 10px; }
.modal-desc { font-size: 13px; color: var(--muted); background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; font-family: var(--mono); white-space: pre-wrap; word-break: break-word; max-height: 180px; overflow-y: auto; margin-bottom: 14px; }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; }

/* ===== Мобилка: сайдбар выезжает, как в mgp chat ===== */
@media (max-width: 899px) {
  #view-chat { flex-direction: row; }
  #sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 95;
    width: 280px; max-width: 85vw; height: 100%;
    border-right: 1px solid var(--border); border-bottom: none;
    transform: translateX(-100%);
    transition: transform .3s ease;
    box-shadow: 0 0 30px rgba(0,0,0,.6);
  }
  #sidebar.open { transform: translateX(0); }
  #chat-main { flex: 1; width: 100%; }
  .btn-burger { display: inline-flex; }
  .btn-back-side { display: none; }
  .hide-mobile { display: none !important; }

  /* панель настроек — горизонтальная прокрутка, чтобы всё влезало */
  .session-bar { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .session-bar select { flex: 0 0 auto; min-width: 90px; }
  .picker { flex: 0 0 auto; min-width: 150px; }

  /* крупнее композер для телефона */
  .composer textarea { font-size: 15px; padding: 12px; }
  #btn-send { padding: 12px 18px; font-size: 14px; }

  /* крупнее сессии */
  .session-item { padding: 14px 16px; }
  .session-item .s-title { font-size: 14px; }
  .chat-messages { padding: 14px 10px; }
  .msg-bubble { font-size: 14.5px; padding: 10px 13px; }
}

/* Десктоп: сайдбар статичный, бургер скрыт */
@media (min-width: 900px) {
  .btn-burger { display: none; }
  .btn-back-side { display: inline-flex; }
  .sidebar-overlay { display: none !important; }
}
