:root {
  --bg: #f6f7f9; --panel: #fff; --text: #1b1f27; --muted: #667085; --line: #e3e6ec;
  --accent: #3b5bdb; --accent-text: #fff; --ok: #137a3f; --warn: #b54708; --bad: #b42318;
  --ok-bg: #e7f6ec; --warn-bg: #fef3e2; --bad-bg: #fde8e6; --user-bubble: #e8edff;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1218; --panel: #171b23; --text: #e8eaf0; --muted: #98a2b3; --line: #2a303b;
    --accent: #6f8cff; --accent-text: #0f1218; --ok: #5fd08a; --warn: #f5b25c; --bad: #ff8a80;
    --ok-bg: #12301f; --warn-bg: #33260f; --bad-bg: #3a1714; --user-bubble: #222b49;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1218; --panel: #171b23; --text: #e8eaf0; --muted: #98a2b3; --line: #2a303b;
  --accent: #6f8cff; --accent-text: #0f1218; --ok: #5fd08a; --warn: #f5b25c; --bad: #ff8a80;
  --ok-bg: #12301f; --warn-bg: #33260f; --bad-bg: #3a1714; --user-bubble: #222b49;
}
:root[data-theme="light"] { color-scheme: light; }
* { box-sizing: border-box; }
/* Big screens: the pages are laid out in px, so on a wide monitor everything looked tiny.
   --z scales the whole page up; the vh rules below divide by it so full-height layouts still fit. */
:root { --z: 1; }
@media (min-width: 1700px) { :root { --z: 1.2; } }
@media (min-width: 2200px) { :root { --z: 1.45; } }
@media (min-width: 2900px) { :root { --z: 1.9; } }
html { zoom: var(--z); }
body { margin: 0; font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); display: flex; min-height: calc(100vh / var(--z)); }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0 0 12px; }
.sub { color: var(--muted); margin: 0 0 20px; }

/* Sidebar (built by app.js) */
.side { width: 220px; flex: none; background: var(--panel); border-right: 1px solid var(--line); padding: 16px 12px; position: sticky; top: 0; height: calc(100vh / var(--z)); overflow-y: auto; }
.side .brand { font-weight: 700; font-size: 16px; padding: 4px 8px 14px; }
.side .group { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 14px 8px 4px; }
.side a { display: block; padding: 7px 8px; border-radius: 6px; color: var(--text); }
.side a:hover { background: var(--bg); }
.side a.active { background: var(--user-bubble); font-weight: 600; }
.side .me { margin-top: 20px; padding: 10px 8px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }

main { flex: 1; padding: 28px 32px; max-width: 1400px; min-width: 0; }
.banner { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 8px 12px; margin-bottom: 20px; font-size: 13px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 18px; margin-bottom: 18px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.stat .n { font-size: 24px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 12px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); color: var(--text); font: inherit; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { margin-bottom: 14px; }
.form-grid { display: grid; gap: 0 14px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

button, .btn { display: inline-block; padding: 8px 14px; border-radius: 7px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit; cursor: pointer; }
button.primary, .btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--bad); }
button.small { padding: 4px 9px; font-size: 12px; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--bg); border: 1px solid var(--line); }
.badge.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.muted { color: var(--muted); }
code { font: 12px ui-monospace, Menlo, monospace; background: var(--bg); padding: 1px 5px; border-radius: 4px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
[hidden] { display: none !important; }

.bar { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; min-width: 120px; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.bar.high > span { background: var(--bad); }

/* Centered auth pages */
body.auth { display: grid; place-items: center; padding: 20px; }
.auth-box { width: 100%; max-width: 380px; }
.auth-box .card { padding: 24px; }

/* Chat */
body.chat { height: calc(100vh / var(--z)); overflow: hidden; }
.chat-list { width: 260px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.chat-list .head { padding: 14px; border-bottom: 1px solid var(--line); }
.chat-list ul { list-style: none; margin: 0; padding: 8px; overflow-y: auto; flex: 1; }
.chat-list li a { display: block; padding: 8px 10px; border-radius: 6px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-list li a.active, .chat-list li a:hover { background: var(--bg); }
.chat-list .foot { padding: 12px 14px; border-top: 1px solid var(--line); font-size: 12px; }
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-top { display: flex; gap: 12px; align-items: center; padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--panel); }
.chat-top select { width: auto; min-width: 200px; }
.msgs { flex: 1; overflow-y: auto; padding: 24px 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 760px; padding: 10px 14px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--user-bubble); border-color: transparent; }
.msg .meta { display: block; margin-top: 6px; font-size: 11px; color: var(--muted); }
.composer { padding: 14px 20px; border-top: 1px solid var(--line); background: var(--panel); }
.composer .box { display: flex; gap: 10px; align-items: flex-end; max-width: 860px; margin: 0 auto; }
.composer textarea { resize: none; min-height: 44px; max-height: 160px; }
.chips { display: flex; gap: 8px; margin: 0 auto 8px; max-width: 860px; flex-wrap: wrap; }

@media (max-width: 760px) {
  body { flex-direction: column; }
  .side { width: 100%; height: auto; position: static; border-right: 0; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: 2px; align-items: center; }
  .side .group, .side .me { display: none; }
  main { padding: 18px 16px; }
  body.chat { flex-direction: column; }
  .chat-list { width: 100%; max-height: 150px; }
}

/* Dialog + checklist (users page) */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); padding: 22px; width: min(520px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.checks { display: grid; gap: 6px; max-height: 160px; overflow-y: auto; border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px; background: var(--bg); }
.checks label { display: flex; gap: 8px; align-items: center; margin: 0; color: var(--text); font-size: 14px; }
.checks input { width: auto; }
.msg-line { padding: 8px 12px; border-radius: 8px; margin-bottom: 14px; font-size: 13px; }
.msg-line.ok { background: var(--ok-bg); color: var(--ok); }
.msg-line.bad { background: var(--bad-bg); color: var(--bad); }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }
.msg.err { color: var(--bad); border-color: var(--bad); }

/* Chat history rows: title plus rename/delete buttons shown on hover or focus */
.chat-list li { display: flex; align-items: center; gap: 2px; }
.chat-list li a { flex: 1; min-width: 0; }
.chat-list .acts { display: none; gap: 2px; padding-right: 6px; }
.chat-list li:hover .acts, .chat-list li:focus-within .acts { display: flex; }
button.icon { border: 0; background: none; padding: 2px 6px; color: var(--muted); font-size: 15px; line-height: 1; cursor: pointer; border-radius: 4px; }
button.icon:hover { background: var(--bg); color: var(--text); }

/* Rendered Markdown (assistant messages) */
.msg.md { white-space: normal; }
.msg.md > :first-child { margin-top: 0; }
.msg.md > :last-child { margin-bottom: 0; }
.msg.md p { margin: 0 0 .7em; }
.msg.md h2, .msg.md h3, .msg.md h4, .msg.md h5, .msg.md h6 { margin: 1em 0 .4em; line-height: 1.3; }
.msg.md h2 { font-size: 1.2em; } .msg.md h3 { font-size: 1.1em; } .msg.md h4, .msg.md h5, .msg.md h6 { font-size: 1em; }
.msg.md ul, .msg.md ol { margin: 0 0 .7em; padding-left: 1.4em; }
.msg.md li { margin: .15em 0; }
.msg.md blockquote { margin: 0 0 .7em; padding: .1em .9em; border-left: 3px solid var(--line); color: var(--muted); }
.msg.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.msg.md a { text-decoration: underline; word-break: break-word; }
.msg.md code.inline { background: var(--bg); border: 1px solid var(--line); padding: 0 .35em; border-radius: 4px; }
.md-table { overflow-x: auto; margin: 0 0 .7em; }
.md-table table { width: auto; border: 1px solid var(--line); }
.md-table th, .md-table td { border: 1px solid var(--line); padding: 5px 10px; }
.md-table th { background: var(--bg); }
.md-code { margin: 0 0 .8em; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--bg); }
.md-code-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 10px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.md-code pre { margin: 0; padding: 10px 12px; overflow-x: auto; }
.md-code pre code { font: 12.5px/1.5 ui-monospace, Menlo, Consolas, monospace; white-space: pre; background: none; padding: 0; }
button.md-copy { padding: 1px 8px; font-size: 12px; border-radius: 5px; }
.tok-kw { color: #8250df; font-weight: 600; } .tok-str { color: #0a7a3d; } .tok-num { color: #b35900; }
.tok-comment { color: #6e7781; font-style: italic; } .tok-fn { color: #0b5fb3; } .tok-key { color: #0b5fb3; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tok-kw { color: #c9a0ff; } :root:not([data-theme="light"]) .tok-str { color: #7ee2a8; } :root:not([data-theme="light"]) .tok-num { color: #ffb36b; }
  :root:not([data-theme="light"]) .tok-comment { color: #8b949e; } :root:not([data-theme="light"]) .tok-fn { color: #79b8ff; } :root:not([data-theme="light"]) .tok-key { color: #79b8ff; }
}
:root[data-theme="dark"] .tok-kw { color: #c9a0ff; } :root[data-theme="dark"] .tok-str { color: #7ee2a8; } :root[data-theme="dark"] .tok-num { color: #ffb36b; }
:root[data-theme="dark"] .tok-comment { color: #8b949e; } :root[data-theme="dark"] .tok-fn { color: #79b8ff; } :root[data-theme="dark"] .tok-key { color: #79b8ff; }
.sources { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--muted); }
.sources .chip { border: 1px solid var(--line); border-radius: 99px; padding: 1px 9px; background: var(--bg); color: var(--text); }

.checkline { display: flex; gap: 8px; align-items: center; margin: 0; color: var(--text); font-size: 14px; }
.checkline input { width: auto; }

/* Light/dark switch (see theme.js) */
.theme-toggle { width: 100%; margin-top: 8px; padding: 6px 10px; font-size: 12.5px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.theme-toggle:hover { color: var(--accent); border-color: var(--accent); }
.theme-toggle.corner { position: fixed; top: 14px; right: 14px; width: auto; margin: 0; z-index: 5; }
