:root, [data-theme="dark"] {
  --bg: #0f172a; --panel: #1e293b; --panel-2: #273449; --border: #334155;
  --text: #e2e8f0; --dim: #94a3b8; --muted: #64748b;
  --accent: #3b82f6; --accent-2: #2563eb; --green: #10b981; --red: #ef4444; --orange: #f59e0b; --purple: #8b5cf6;
  --bubble-user: #2563eb; --bubble-ai: #1e293b; --bubble-admin: #064e3b;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f1f5f9; --panel: #ffffff; --panel-2: #eef2f7; --border: #d5dde8;
    --text: #0f172a; --dim: #475569; --muted: #64748b;
    --bubble-user: #2563eb; --bubble-ai: #ffffff; --bubble-admin: #dcfce7;
  }
}
[data-theme="light"] {
  --bg: #f1f5f9; --panel: #ffffff; --panel-2: #eef2f7; --border: #d5dde8;
  --text: #0f172a; --dim: #475569; --muted: #64748b;
  --bubble-user: #2563eb; --bubble-ai: #ffffff; --bubble-admin: #dcfce7;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; border-radius: 10px; padding: 9px 14px; background: var(--accent); color: #fff; font-weight: 600; }
button:hover:not(:disabled) { background: var(--accent-2); }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
button.ghost:hover:not(:disabled) { background: var(--panel-2); }
button.green { background: var(--green); } button.red { background: var(--red); } button.orange { background: var(--orange); color: #1c1206; }
button.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
input, select, textarea { background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { font-size: 13px; color: var(--dim); display: block; margin: 10px 0 4px; }
.hint { font-size: 12px; color: var(--muted); }

/* ---------- каркас ---------- */
.app { display: grid; grid-template-rows: auto 1fr; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.topbar h1 { font-size: 17px; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .who { font-size: 13px; color: var(--dim); white-space: nowrap; }
.layout { display: grid; grid-template-columns: 290px 1fr; min-height: 0; }
.sidebar { border-right: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.sidebar .side-head { padding: 12px; display: flex; gap: 8px; flex-direction: column; }
.side-list { overflow-y: auto; flex: 1; padding: 0 8px 12px; }
.dlg-item { padding: 10px 12px; border-radius: 10px; cursor: pointer; margin-bottom: 4px; border: 1px solid transparent; }
.dlg-item:hover { background: var(--panel-2); }
.dlg-item.active { background: var(--panel-2); border-color: var(--accent); }
.dlg-item .t { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dlg-item .s { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dlg-item .meta { display: flex; gap: 6px; align-items: center; font-size: 11px; color: var(--muted); margin-top: 2px; flex-wrap: wrap; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 9px; font-size: 11px; font-weight: 700; background: var(--panel-2); color: var(--dim); }
.badge.red { background: var(--red); color: #fff; } .badge.green { background: var(--green); color: #fff; }
.badge.orange { background: var(--orange); color: #1c1206; } .badge.blue { background: var(--accent); color: #fff; }

/* ---------- чат ---------- */
.chat { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; }
.chat-head { padding: 10px 16px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; min-height: 50px; }
.chat-head .title { font-weight: 700; flex: 1; min-width: 160px; }
.msgs { overflow-y: auto; padding: 18px 16px 10px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: min(820px, 92%); padding: 10px 14px; border-radius: 14px; border: 1px solid var(--border); background: var(--bubble-ai); position: relative; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--bubble-user); color: #fff; border-color: transparent; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.ai { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.admin { align-self: flex-start; background: var(--bubble-admin); border-color: var(--green); border-bottom-left-radius: 4px; }
.msg.error { border-color: var(--red); }
.msg.system { align-self: center; background: transparent; border: 0; color: var(--muted); font-size: 12px; padding: 2px; }
.msg .who { font-size: 12px; font-weight: 700; color: var(--dim); margin-bottom: 4px; display: flex; gap: 8px; align-items: center; }
.msg.admin .who { color: var(--green); }
.msg .time { font-weight: 400; color: var(--muted); }
.msg .body p { margin: 0 0 8px; } .msg .body p:last-child { margin-bottom: 0; }
.msg .body h3, .msg .body h4 { margin: 12px 0 6px; font-size: 15px; }
.msg .body ol, .msg .body ul { margin: 4px 0 8px; padding-left: 22px; }
.msg .body li { margin: 2px 0; }
.msg .body code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.msg .body pre { background: var(--panel-2); padding: 10px; border-radius: 8px; overflow-x: auto; }
.msg .body blockquote { border-left: 3px solid var(--accent); margin: 6px 0; padding: 2px 10px; color: var(--dim); }
.msg .body a { color: var(--accent); }
.msg .actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.msg .actions button { padding: 3px 9px; font-size: 12px; background: var(--panel-2); color: var(--text); border: 1px solid var(--border); font-weight: 500; }
.msg .actions button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.msg .edited { font-size: 11px; color: var(--muted); margin-top: 4px; }
.thinking .dots span { display: inline-block; width: 6px; height: 6px; margin-right: 3px; border-radius: 50%; background: var(--dim); animation: blink 1.2s infinite; }
.thinking .dots span:nth-child(2) { animation-delay: .2s; } .thinking .dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.composer { border-top: 1px solid var(--border); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); }
.composer .row { display: flex; gap: 8px; align-items: flex-end; max-width: 980px; margin: 0 auto; }
.composer textarea { resize: none; min-height: 46px; max-height: 220px; }
.composer button { height: 46px; min-width: 52px; }
.banner { margin: 0 auto 8px; max-width: 980px; padding: 8px 12px; border-radius: 10px; font-size: 13px; background: var(--bubble-admin); border: 1px solid var(--green); }

.empty { margin: auto; max-width: 640px; text-align: center; color: var(--dim); padding: 20px; }
.empty h2 { color: var(--text); margin: 0 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 14px; }
.chips button { background: var(--panel); color: var(--text); border: 1px solid var(--border); font-weight: 500; font-size: 13px; text-align: left; }
.chips button:hover { border-color: var(--accent); background: var(--panel-2); }

/* ---------- вход ---------- */
.overlay { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 24px; width: 100%; max-width: 400px; box-shadow: 0 10px 40px rgba(0,0,0,.35); }
.card h2 { margin: 0 0 6px; font-size: 20px; }
.card .err { color: var(--red); font-size: 13px; min-height: 18px; margin-top: 8px; }
.card button { width: 100%; margin-top: 14px; padding: 12px; }

/* ---------- модалки ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: none; align-items: center; justify-content: center; z-index: 90; padding: 16px; }
.modal.open { display: flex; }
.modal .card { max-width: 760px; max-height: 92dvh; overflow-y: auto; }
.modal .card .btns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.modal .card .btns button { width: auto; margin: 0; }

#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px); background: var(--panel); border: 1px solid var(--border); padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none; transition: .25s; z-index: 200; box-shadow: 0 6px 24px rgba(0,0,0,.35); max-width: 90vw; }
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.err { border-color: var(--red); } #toast.ok { border-color: var(--green); }

/* ---------- админка ---------- */
.tabs { display: flex; gap: 4px; }
.tabs button { background: transparent; color: var(--dim); border-radius: 8px; padding: 6px 12px; }
.tabs button.active { background: var(--panel-2); color: var(--text); }
.filters { display: flex; gap: 4px; flex-wrap: wrap; }
.filters button { padding: 4px 10px; font-size: 12px; background: var(--panel-2); color: var(--text); font-weight: 500; }
.filters button.active { background: var(--accent); color: #fff; }
.settings { overflow-y: auto; padding: 16px; }
.settings .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; max-width: 1100px; }
.settings .full { grid-column: 1 / -1; }
.settings h3 { margin: 22px 0 4px; font-size: 15px; grid-column: 1 / -1; }
.settings textarea { min-height: 140px; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.settings textarea#sKnowledge { min-height: 320px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; margin-top: 12px; }
.check input { width: auto; }
.orig { margin-top: 6px; font-size: 12px; color: var(--muted); border-top: 1px dashed var(--border); padding-top: 6px; }
.orig summary { cursor: pointer; }

/* ---------- телефон ---------- */
.only-mobile { display: none; }
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 25% 0 0; z-index: 50; transform: translateX(-105%); transition: transform .2s; box-shadow: 8px 0 30px rgba(0,0,0,.4); }
  .sidebar.open { transform: none; }
  .only-mobile { display: inline-block; }
  .topbar .who { display: none; }
  .msg { max-width: 96%; }
  .settings .grid { grid-template-columns: 1fr; }
}
