:root {
  --bg: #1a1a2e;
  --bg-sidebar: #16162a;
  --bg-chat: #1e1e3a;
  --bg-input: #252547;
  --bg-bubble-user: #4338ca;
  --bg-bubble-bot: #252547;
  --bg-bubble-stream: #1c1c38;
  --bg-hover: #2a2a4a;
  --border: #2e2e52;
  --text: #e8e8f0;
  --text-dim: #8888aa;
  --text-bright: #fff;
  --accent: #6366f1;
  --accent-h: #818cf8;
  --green: #22c55e;
  --red: #ef4444;
  --orange: #f59e0b;
  --radius: 12px;
  --radius-s: 8px;
  --sidebar-w: 280px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg); color: var(--text); -webkit-text-size-adjust: 100%;
  overflow: hidden;
}

/* ── LOGIN ─────────────────────────────── */
#login {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; min-height: 100dvh; padding: 20px;
}
.login-card {
  background: var(--bg-sidebar); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 36px 28px; width: 100%; max-width: 360px;
}
.login-card h1 { font-size: 20px; text-align: center; margin-bottom: 2px; }
.login-card .sub { color: var(--text-dim); font-size: 12px; text-align: center; margin-bottom: 20px; }
.login-card .badges { display: flex; justify-content: center; gap: 6px; margin-bottom: 20px; }
.badge { font-size: 10px; color: var(--text-dim); padding: 3px 10px; background: var(--bg-input); border-radius: 20px; display: flex; align-items: center; gap: 4px; }
.badge .d { width: 5px; height: 5px; border-radius: 50%; background: var(--green); }
.fg { margin-bottom: 14px; }
.fg label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }
.fg input {
  width: 100%; padding: 10px 12px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-s); color: var(--text); font-size: 14px; outline: none;
}
.fg input:focus { border-color: var(--accent); }
.btn-go {
  width: 100%; padding: 11px; background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius-s); font-size: 14px; font-weight: 500; cursor: pointer;
}
.btn-go:hover { background: var(--accent-h); }
.login-err { color: var(--red); font-size: 12px; text-align: center; margin-top: 10px; min-height: 16px; }

/* ── MAIN LAYOUT ───────────────────────── */
#app { display: none; height: 100vh; height: 100dvh; }
#app.on { display: flex; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w); background: var(--bg-sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
  transition: transform 0.2s;
}
.sidebar-head {
  padding: 14px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.sidebar-head h2 { font-size: 14px; }
.btn-new {
  padding: 6px 12px; background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius-s); font-size: 12px; cursor: pointer;
}
.btn-new:hover { background: var(--accent-h); }
/* Search */
.search-box { padding: 8px 14px 4px; }
.search-box input {
  width: 100%; padding: 8px 10px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-s); color: var(--text); font-size: 12px; outline: none;
}
.search-box input:focus { border-color: var(--accent); }
.search-box input::placeholder { color: var(--text-dim); }
.search-results { padding: 4px 8px; }
.search-hit {
  padding: 8px 10px; border-radius: var(--radius-s); cursor: pointer;
  margin-bottom: 2px; transition: background .15s;
}
.search-hit:hover { background: var(--bg-hover); }
.search-hit .sh-title { font-size: 12px; font-weight: 600; color: var(--text); margin-bottom: 2px; }
.search-hit .sh-text { font-size: 11px; color: var(--text-dim); line-height: 1.3; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.search-hit .sh-badge { font-size: 9px; color: var(--accent); }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  padding: 10px 12px; border-radius: var(--radius-s); cursor: pointer;
  margin-bottom: 2px; display: flex; align-items: center; justify-content: space-between;
  transition: background 0.15s;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--bg-input); }
.conv-item .title { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-item.has-update { background: rgba(99,102,241,0.15); border-left: 3px solid var(--accent); }
.conv-item.has-update .title { color: var(--accent); font-weight: 600; }
.conv-item .busy-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--orange); flex-shrink: 0; margin-right: 6px; animation: pulse 1.5s infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.3} }
.conv-item .del {
  display: none; background: none; border: none; color: var(--text-dim); cursor: pointer;
  font-size: 16px; padding: 0 4px; flex-shrink: 0;
}
.conv-item:hover .del { display: block; }
.conv-item .del:hover { color: var(--red); }
.sidebar-foot {
  padding: 10px 14px; border-top: 1px solid var(--border);
  display: flex; gap: 6px;
}
.sb { flex: 1; padding: 7px; background: var(--bg-input); border: 1px solid var(--border); border-radius: var(--radius-s); color: var(--text-dim); font-size: 11px; cursor: pointer; text-align: center; }
.sb:hover { color: var(--text); border-color: var(--accent); }
.sb.out:hover { border-color: var(--red); color: var(--red); }

/* Chat area */
.chat-area { flex: 1; display: flex; flex-direction: column; background: var(--bg-chat); min-width: 0; }
.chat-head {
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; flex-shrink: 0;
}
.chat-head .burger { display: none; background: none; border: none; color: var(--text); font-size: 22px; cursor: pointer; }
.chat-head .t { font-size: 14px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .acts { display: flex; gap: 4px; }
.act {
  padding: 5px 10px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-s); color: var(--text-dim); font-size: 11px; cursor: pointer; white-space: nowrap;
}
.act:hover { color: var(--text); border-color: var(--accent); }
.act.fix { border-color: var(--green); color: var(--green); }
.act.fix:hover { background: var(--green); color: #000; }
.act.agent-ctrl { border-color: var(--orange); color: var(--orange); }
.act.agent-ctrl:hover { background: var(--orange); color: #000; }
.act.agent-ctrl.stop { border-color: var(--red); color: var(--red); }
.act.agent-ctrl.stop:hover { background: var(--red); color: #fff; }

/* Paused state indicator */
.m.paused-msg { align-self: center; background: rgba(245,158,11,0.15); border: 1px dashed var(--orange); color: var(--orange); font-size: 12px; max-width: 80%; text-align: center; padding: 8px 14px; }
.m.stopped-msg { align-self: center; background: rgba(239,68,68,0.15); border: 1px dashed var(--red); color: var(--red); font-size: 12px; max-width: 80%; text-align: center; padding: 8px 14px; }
.m.resumed-msg { align-self: center; background: rgba(34,197,94,0.15); border: 1px dashed var(--green); color: var(--green); font-size: 12px; max-width: 80%; text-align: center; padding: 8px 14px; }

/* Messages */
.msgs { flex: 1; overflow-y: auto; padding: 16px; -webkit-overflow-scrolling: touch; }
.msgs-in { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }

.m { max-width: 80%; padding: 10px 14px; border-radius: var(--radius); font-size: 14px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; animation: fi .15s ease; }
@keyframes fi { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }
.m.u { align-self: flex-end; background: var(--bg-bubble-user); border-bottom-right-radius: 4px; }
.m.a { align-self: flex-start; background: var(--bg-bubble-bot); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

/* Stream bubbles */
.stream-box { align-self: stretch; display: flex; flex-direction: column; gap: 6px; }
.stream-bub {
  align-self: flex-start; max-width: 80%; background: var(--bg-bubble-stream);
  border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: var(--radius); padding: 10px 14px; animation: fi .15s ease;
}
.stream-bub .ag { font-size: 11px; font-weight: 600; color: var(--accent); margin-bottom: 3px; }
.stream-bub .tx { font-size: 13px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; max-height: 250px; overflow-y: auto; color: var(--text-dim); }

/* Intermediate */
.m.int { align-self: flex-start; background: var(--bg-bubble-stream); border: 1px solid var(--border); border-left: 3px solid var(--orange); font-size: 13px; max-width: 80%; }

/* Queued message confirmation */
.m.q { align-self: center; background: var(--bg-input); border: 1px dashed var(--border); color: var(--text-dim); font-size: 12px; font-style: italic; max-width: 70%; text-align: center; padding: 8px 14px; }

/* Progress */
.prog {
  align-self: flex-start; max-width: 80%; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px;
}
.prog .pt { font-size: 12px; font-weight: 600; color: var(--accent); margin-bottom: 6px; display: flex; align-items: center; gap: 8px; }
.prog .sp { width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.prog .ps { font-size: 11px; color: var(--text-dim); line-height: 1.5; white-space: pre-wrap; }

/* Empty */
.empty { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: var(--text-dim); text-align: center; padding: 40px 20px; }
.empty h3 { font-size: 16px; color: var(--text); margin-bottom: 6px; }
.empty p { font-size: 12px; line-height: 1.5; }

/* Input */
.inp-area { flex-shrink: 0; padding: 12px 16px; padding-bottom: calc(12px + var(--safe-b)); border-top: 1px solid var(--border); }
.inp-row { max-width: 820px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.inp-row textarea {
  flex: 1; padding: 10px 12px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-s); color: var(--text); font-size: 14px; font-family: inherit;
  line-height: 1.4; resize: none; outline: none; max-height: 120px; min-height: 42px;
}
.inp-row textarea:focus { border-color: var(--accent); }
.btn-send {
  width: 42px; height: 42px; background: var(--accent); border: none; border-radius: var(--radius-s);
  color: #fff; font-size: 16px; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.btn-send:hover { background: var(--accent-h); }
.btn-send:disabled { opacity: .35; cursor: not-allowed; }
.btn-attach {
  width: 42px; height: 42px; display: flex; align-items: center; justify-content: center;
  background: var(--bg-input); border: 1px solid var(--border); border-radius: var(--radius-s);
  color: var(--text-dim); font-size: 18px; cursor: pointer; flex-shrink: 0;
}
.btn-attach:hover { color: var(--text); border-color: var(--accent); }
.file-preview {
  max-width: 820px; margin: 4px auto 0; display: flex; flex-wrap: wrap; gap: 6px;
}
.file-preview:empty { display: none; }
.file-tag {
  display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px;
  background: var(--bg-input); border: 1px solid var(--border); border-radius: 16px;
  font-size: 11px; color: var(--text-dim);
}
.file-tag .fp-img { width: 32px; height: 32px; object-fit: cover; border-radius: 4px; }
.file-tag .x { cursor: pointer; color: var(--text-dim); margin-left: 2px; }
.file-tag .x:hover { color: var(--red); }

/* Drag & drop overlay */
.chat-area.drag-over { outline: 2px dashed var(--accent); outline-offset: -4px; }
.chat-area.drag-over::after {
  content: 'Перетащи файлы сюда'; position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(99,102,241,0.1); color: var(--accent); font-size: 16px; font-weight: 600;
  pointer-events: none; z-index: 10;
}
.chat-area { position: relative; }

/* Settings panel items */
.set-section { margin-bottom: 20px; }
.set-section h3 { font-size: 13px; margin-bottom: 10px; color: var(--text); }
.set-model {
  padding: 12px; background: var(--bg-input); border-radius: var(--radius-s);
  margin-bottom: 8px; border: 1px solid var(--border);
}
.set-model .name { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.set-model .name .dot { width: 8px; height: 8px; border-radius: 50%; }
.set-model .name .dot.on { background: var(--green); }
.set-model .name .dot.off { background: var(--red); }
.set-model .role { font-size: 11px; color: var(--accent); margin-bottom: 4px; }
.set-model .desc { font-size: 11px; color: var(--text-dim); line-height: 1.4; margin-bottom: 6px; }
.set-model .strengths { font-size: 11px; color: var(--text-dim); }
.set-model .strengths span { display: inline-block; background: var(--bg-hover); padding: 2px 8px; border-radius: 10px; margin: 2px 2px; }
.set-model .when { font-size: 10px; color: var(--text-dim); margin-top: 4px; font-style: italic; }
.set-toggle { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.set-toggle label { font-size: 12px; }
.toggle-sw { position: relative; width: 36px; height: 20px; cursor: pointer; }
.toggle-sw input { opacity: 0; width: 0; height: 0; }
.toggle-sw .sl { position: absolute; inset: 0; background: var(--border); border-radius: 10px; transition: .2s; }
.toggle-sw .sl:before { content: ''; position: absolute; width: 16px; height: 16px; left: 2px; bottom: 2px; background: #fff; border-radius: 50%; transition: .2s; }
.toggle-sw input:checked + .sl { background: var(--accent); }
.toggle-sw input:checked + .sl:before { transform: translateX(16px); }
.set-spec { padding: 8px 10px; background: var(--bg-input); border-radius: var(--radius-s); margin-bottom: 4px; display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); }
.set-spec .sname { font-size: 12px; flex: 1; }
.set-spec .sdesc { font-size: 10px; color: var(--text-dim); }
.set-spec .stag { font-size: 9px; padding: 2px 6px; border-radius: 8px; }
.set-spec .stag.multi { background: var(--accent); color: #fff; }
.set-spec .stag.solo { background: var(--border); color: var(--text-dim); }

/* Memory panel */
.mem-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 100; }
.mem-overlay.on { display: block; }
.mem-panel {
  position: fixed; top: 0; right: 0; width: 100%; max-width: 380px; height: 100%;
  background: var(--bg-sidebar); border-left: 1px solid var(--border); z-index: 101;
  display: flex; flex-direction: column;
}
.mem-head { padding: 14px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.mem-head h2 { font-size: 15px; }
.mem-x { background: var(--bg-input); border: 1px solid var(--border); border-radius: 6px; color: var(--text-dim); font-size: 16px; cursor: pointer; width: 28px; height: 28px; display: flex; align-items: center; justify-content: center; }
.mem-body { flex: 1; overflow-y: auto; padding: 14px; }
.mem-cat { margin-bottom: 16px; }
.mem-cat h3 { font-size: 11px; text-transform: uppercase; color: var(--text-dim); margin-bottom: 6px; letter-spacing: .4px; }
.mem-it { display: flex; justify-content: space-between; align-items: flex-start; padding: 8px 10px; background: var(--bg-input); border-radius: var(--radius-s); margin-bottom: 4px; gap: 6px; }
.mem-it .k { font-weight: 600; font-size: 12px; color: var(--accent); }
.mem-it .v { font-size: 12px; color: var(--text-dim); word-break: break-all; }
.mem-it .x { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 14px; }
.mem-it .x:hover { color: var(--red); }
.mem-add { padding: 14px; border-top: 1px solid var(--border); }
.mem-add .r { display: flex; gap: 4px; margin-bottom: 4px; }
.mem-add input, .mem-add select { flex: 1; padding: 8px 10px; background: var(--bg-input); border: 1px solid var(--border); border-radius: 6px; color: var(--text); font-size: 12px; outline: none; }
.mem-add select { flex: .6; }
.mem-add .go { padding: 8px 14px; background: var(--accent); color: #fff; border: none; border-radius: 6px; font-size: 12px; cursor: pointer; }

/* Connection */
.conn { position: fixed; top: 56px; left: 50%; transform: translateX(-50%); padding: 6px 14px; border-radius: 20px; font-size: 11px; z-index: 50; display: none; }
.conn.off { display: block; background: var(--red); color: #fff; }
.conn.re { display: block; background: var(--orange); color: #000; }

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── MOBILE ────────────────────────────── */
@media (max-width: 768px) {
  .sidebar { position: fixed; left: 0; top: 0; height: 100%; z-index: 200; transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .chat-head .burger { display: block; }
  .m, .stream-bub, .m.int, .prog { max-width: 92%; }
  .sidebar-w-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 199; display: none; }
  .sidebar-w-overlay.on { display: block; }
}
