:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --pane: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --line: #e3e7ec;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --mine: #e7f0fe;
  --selected: #eef3fb;
  --warn-bg: #fff4dc;
  --warn: #7a5200;
  --ok: #1a8f4c;
  --off: #a0a8b3;
  --error: #c0392b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181f;
    --pane: #1b212a;
    --text: #e6eaf0;
    --muted: #93a0b0;
    --line: #2c3440;
    --accent: #4c8df6;
    --accent-text: #0e1420;
    --mine: #24344d;
    --selected: #25303f;
    --warn-bg: #3a2f17;
    --warn: #f0c778;
    --ok: #4cc27f;
    --off: #6b7686;
    --error: #ef7b6e;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, "Segoe UI", Roboto, Arial, sans-serif; }
button { font: inherit; cursor: pointer; }
button[type="submit"] { background: var(--accent); color: var(--accent-text); border: 0; border-radius: 8px; padding: 9px 18px; }
button:disabled { opacity: .55; cursor: default; }
.link { background: none; border: 0; color: var(--muted); padding: 2px 4px; }
.link:hover { color: var(--text); }
.muted { color: var(--muted); }
.error { color: var(--error); margin: 0; }

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--pane); border: 1px solid var(--line); border-radius: 12px; padding: 28px; display: grid; gap: 14px; }
.login-card h1 { margin: 0; font-size: 24px; }
.login-card .muted { margin: -10px 0 4px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
input, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%; }
textarea { resize: none; }

.app { height: 100%; display: grid; grid-template-columns: 280px minmax(0, 1fr) 260px; }
.app.no-bases { grid-template-columns: 280px minmax(0, 1fr); }
.app.no-bases .bases { display: none; }
.topics, .bases { background: var(--pane); display: flex; flex-direction: column; min-height: 0; }
.topics { border-right: 1px solid var(--line); }
.bases { border-left: 1px solid var(--line); }
.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.pane-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 13px 16px; border-bottom: 1px solid var(--line); min-height: 50px; }
.title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.list { overflow-y: auto; padding: 6px; flex: 1; }
.group { font-size: 12px; color: var(--muted); padding: 10px 10px 4px; }
.item { display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 8px; padding: 8px 10px; color: var(--text); }
.item:hover { background: var(--selected); }
.item.active { background: var(--selected); }
.item-title { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { display: block; font-size: 12px; color: var(--muted); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--off); margin-right: 6px; }
.dot.on { background: var(--ok); }

.notice { background: var(--warn-bg); color: var(--warn); padding: 8px 16px; font-size: 14px; }
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.message { max-width: 780px; padding: 10px 14px; border-radius: 12px; overflow-wrap: anywhere; }
.message.user { align-self: flex-end; background: var(--mine); white-space: pre-wrap; }
.message.assistant { align-self: flex-start; background: var(--pane); border: 1px solid var(--line); }
.message.system { align-self: center; color: var(--muted); font-size: 14px; }
.message.working { align-self: flex-start; color: var(--muted); }
.message p { margin: 0 0 8px; }
.message p:last-child { margin-bottom: 0; }
.message ul { margin: 0 0 8px; padding-left: 20px; }
.table-wrap { overflow-x: auto; margin-bottom: 8px; }
.message table { border-collapse: collapse; font-size: 14px; }
.message th, .message td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }

.ask { display: flex; gap: 10px; align-items: flex-end; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--pane); }

@media (max-width: 800px) {
  .app, .app.no-bases { grid-template-columns: minmax(0, 1fr); grid-auto-rows: minmax(0, auto); height: auto; min-height: 100%; }
  .topics, .bases { border: 0; border-bottom: 1px solid var(--line); max-height: 30vh; }
  .chat { min-height: 60vh; }
}

.password-block { display: grid; gap: 14px; }
.qr-block { display: grid; gap: 10px; justify-items: center; text-align: center; }
.qr-image { width: 220px; height: 220px; background: #ffffff; border-radius: 8px; padding: 6px; }
.qr-image svg { width: 100%; height: 100%; display: block; }
.bind-dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--pane); color: var(--text); padding: 24px; text-align: center; }
.bind-dialog h2 { margin: 0 0 12px; font-size: 18px; }
.bind-dialog .qr-image { margin: 0 auto; }
.bind-dialog form { margin-top: 12px; }
