:root {
  --bg: #ffffff; --panel: #f4f5f7; --text: #1b1d21; --muted: #6b7280;
  --border: #d9dce1; --accent: #2457d6; --user: #e8eefc; --danger: #b42318;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #16181d; --panel: #1e2128; --text: #e6e8ec; --muted: #9aa1ad;
          --border: #30343c; --accent: #6b95ff; --user: #243252; --danger: #ff8a7a; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.5 system-ui, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
button, select, textarea, input { font: inherit; color: inherit; }
button { background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 6px 14px; cursor: pointer; }
button.subtle { background: none; color: var(--muted); border: 1px solid var(--border); }
button:disabled { opacity: .5; cursor: default; }
select, input, textarea { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.error { color: var(--danger); margin: 4px 16px; min-height: 1em; }

#login { height: 100%; display: grid; place-items: center; }
#login form { display: flex; flex-direction: column; gap: 10px; width: 280px; }
#login h1 { margin: 0 0 4px; font-size: 20px; }

#app { height: 100%; display: flex; }
aside { width: 260px; background: var(--panel); border-right: 1px solid var(--border);
        display: flex; flex-direction: column; padding: 10px; gap: 8px; }
#list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#list li { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
#list li:hover { background: var(--border); }
#list li.active { background: var(--user); }
#list .info { flex: 1; min-width: 0; }
#list .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#list .meta { font-size: 12px; color: var(--muted); }
#list .empty { color: var(--muted); cursor: default; }

main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
header { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
header h2 { flex: 1; margin: 0; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; }
#messages { flex: 1; overflow-y: auto; overflow-anchor: none; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 760px; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border); }
.msg.user { background: var(--user); align-self: flex-end; border-color: transparent; }
.msg .who { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.hint { margin: auto; color: var(--muted); }
#composer { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
#composer textarea { flex: 1; resize: none; }

@media (max-width: 700px) {
  aside { width: 150px; }
  header { flex-wrap: wrap; }
}

button.icon { background: none; color: var(--muted); padding: 0 6px; }
button.icon:hover { color: var(--text); }
.msg .imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg .imgs img { max-width: 240px; max-height: 240px; border-radius: 6px; cursor: zoom-in; background: var(--panel); }
#attachments { display: flex; gap: 8px; padding: 8px 16px 0; flex-wrap: wrap; }
#attachments:empty { display: none; }
.thumb { position: relative; }
.thumb img { height: 64px; border-radius: 6px; border: 1px solid var(--border); display: block; }
.thumb button { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; padding: 0;
                border-radius: 50%; line-height: 18px; background: var(--text); color: var(--bg); }

/* Rendered markdown (assistant messages) */
.msg .body.md { white-space: normal; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote, .md table { margin: 0 0 .75em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1em 0 .4em; line-height: 1.25; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3, .md h4 { font-size: 1.05em; }
.md ul, .md ol { padding-left: 1.5em; }
.md a { color: var(--accent); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.md blockquote { padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md code { font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--panel); padding: 1px 5px; border-radius: 4px; }
.md pre { position: relative; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md pre .copy { position: absolute; top: 6px; right: 6px; padding: 2px 8px; font-size: 12px; background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
.md table { display: block; overflow-x: auto; border-collapse: collapse; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 10px; text-align: left; }
.md th { background: var(--panel); }

/* Views */
#chat-view, #prompts-view { flex: 1; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
header { flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 13px; }

/* System prompts view */
#prompts-body { flex: 1; display: flex; gap: 16px; padding: 16px; min-height: 0; }
#prompts-side { width: 220px; display: flex; flex-direction: column; gap: 8px; }
#prompt-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
#prompt-list li { padding: 6px 8px; border-radius: 6px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#prompt-list li:hover { background: var(--panel); }
#prompt-list li.active { background: var(--user); }
#prompt-list li.empty { color: var(--muted); cursor: default; }
#prompt-form { flex: 1; display: flex; flex-direction: column; gap: 10px; min-width: 0; max-width: 800px; }
#prompt-form label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }
#prompt-form label.grow { flex: 1; min-height: 0; }
#prompt-form textarea { flex: 1; min-height: 200px; resize: vertical; font: 14px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--text); }
#prompt-form input { color: var(--text); }
#prompt-form .row { display: flex; align-items: center; gap: 8px; }
@media (max-width: 700px) { #prompts-body { flex-direction: column; } #prompts-side { width: auto; } }