:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f0f0f2;
  --sidebar: #efeff2;
  --text: #1d1d1f;
  --muted: #6b6b73;
  --border: #dedee3;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --user-bubble: #e8effd;
  --danger: #c62828;
  --ok: #2e7d32;
  --warn-bg: #fff4e5;
  --warn-text: #8a4b00;
  --code-bg: #f3f3f6;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141416;
    --surface: #1c1c1f;
    --surface-2: #26262a;
    --sidebar: #101012;
    --text: #ececf1;
    --muted: #9a9aa3;
    --border: #33333a;
    --accent: #5b8cff;
    --accent-text: #0b0b0d;
    --user-bubble: #23304d;
    --danger: #ff6b6b;
    --ok: #6fcf7a;
    --warn-bg: #3a2a12;
    --warn-text: #ffc77a;
    --code-bg: #0f0f11;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #141416;
  --surface: #1c1c1f;
  --surface-2: #26262a;
  --sidebar: #101012;
  --text: #ececf1;
  --muted: #9a9aa3;
  --border: #33333a;
  --accent: #5b8cff;
  --accent-text: #0b0b0d;
  --user-bubble: #23304d;
  --danger: #ff6b6b;
  --ok: #6fcf7a;
  --warn-bg: #3a2a12;
  --warn-text: #ffc77a;
  --code-bg: #0f0f11;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
button, select, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; border-radius: 8px; border: 1px solid transparent; padding: 7px 14px; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
button.ghost { background: transparent; border-color: var(--border); }
button.icon { background: transparent; padding: 8px; color: var(--muted); }
button.icon:hover:not(:disabled) { color: var(--text); background: var(--surface-2); }
button.wide { width: 100%; text-align: left; }
.muted { color: var(--muted); font-size: 13px; }

/* Sidebar */
.sidebar {
  width: 270px; flex-shrink: 0;
  display: flex; flex-direction: column; gap: 10px;
  padding: 12px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 4px; }
.brand { font-weight: 650; }
.conversation-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; }
.conv {
  display: flex; align-items: center; gap: 4px;
  border-radius: 8px; padding: 0 4px 0 0;
}
.conv:hover, .conv.active { background: var(--surface-2); }
.conv .open {
  flex: 1; min-width: 0; text-align: left; background: transparent; padding: 7px 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv.active .open { font-weight: 600; }
.conv .delete { visibility: hidden; padding: 2px 8px; background: transparent; color: var(--muted); }
.conv:hover .delete, .conv.active .delete { visibility: visible; }
.conv .delete:hover { color: var(--danger); }
.conv-group { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 10px 4px; }
.sidebar-foot { display: flex; flex-direction: column; gap: 6px; }
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .4); z-index: 9; }
.only-mobile { display: none; }

/* Main column */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.model-picker { display: flex; align-items: center; gap: 8px; }
.model-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.model-picker select {
  padding: 7px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); font-weight: 600; max-width: 62vw;
}
.pill {
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.pill.ok { color: var(--ok); }
.pill.busy { color: var(--warn-text); background: var(--warn-bg); }
.pill.err { color: var(--danger); }

/* Messages */
.messages {
  flex: 1; overflow-y: auto;
  padding: 24px 16px;
  display: flex; flex-direction: column; gap: 18px;
}
.messages > * { width: 100%; max-width: 820px; margin: 0 auto; }
.empty { text-align: center; color: var(--muted); margin-top: 10vh; }
.empty h1 { color: var(--text); font-size: 26px; margin-bottom: 8px; }
.empty p { max-width: 620px; margin: 0 auto; }

.msg { display: flex; flex-direction: column; gap: 6px; }
.msg .who { font-size: 12px; color: var(--muted); }
.msg.user { align-items: flex-end; }
.msg.user .body {
  background: var(--user-bubble); padding: 10px 14px; border-radius: var(--radius);
  max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .body { overflow-wrap: anywhere; }
.msg.assistant .body p { margin: 0 0 10px; }
.msg.assistant .body p:last-child { margin-bottom: 0; }
.msg.assistant .body ul, .msg.assistant .body ol { margin: 0 0 10px; padding-left: 22px; }
.msg.assistant .body h2, .msg.assistant .body h3, .msg.assistant .body h4,
.msg.assistant .body h5 { margin: 14px 0 6px; line-height: 1.3; }
.msg .error { color: var(--danger); }
.msg .meta { font-size: 12px; color: var(--muted); }
.msg .body a { color: var(--accent); overflow-wrap: anywhere; }
.msg code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px;
  background: var(--code-bg); padding: 1px 5px; border-radius: 5px;
}
.table-wrap { overflow-x: auto; margin: 0 0 10px; }
.table-wrap table { border-collapse: collapse; font-size: 14px; min-width: 60%; }
.table-wrap th, .table-wrap td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.table-wrap th { background: var(--surface-2); font-weight: 600; }
.codeblock { margin: 0 0 10px; background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; }
.codeblock .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(--border);
}
.codeblock .code-head button { padding: 2px 8px; font-size: 12px; background: transparent; border-color: var(--border); }
.codeblock pre { margin: 0; padding: 12px; overflow-x: auto; }
.codeblock pre code { background: none; padding: 0; }

details.thinking { font-size: 13px; color: var(--muted); border-left: 3px solid var(--border); padding: 2px 0 2px 10px; }
details.thinking summary { cursor: pointer; }
details.thinking div { white-space: pre-wrap; margin-top: 6px; }

.steps { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.steps:empty { display: none; }
.steps .step.failed { color: var(--danger); }
.sources { font-size: 13px; border-top: 1px solid var(--border); padding-top: 8px; margin-top: 4px; }
.sources summary { cursor: pointer; color: var(--muted); }
.sources ol { margin: 6px 0 0; padding-left: 22px; }
.sources a { color: var(--accent); text-decoration: none; overflow-wrap: anywhere; }
.sources a:hover { text-decoration: underline; }
.sources .host { color: var(--muted); margin-left: 6px; }

.downloads { display: flex; flex-direction: column; gap: 8px; }
.downloads:empty { display: none; }
.download-preview { max-width: min(512px, 100%); max-height: 512px; border-radius: 10px; border: 1px solid var(--border); display: block; }
.download-card {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  max-width: 520px;
}
.download-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.download-size { font-size: 12px; color: var(--muted); }
.download-card button { padding: 5px 12px; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.thumbs img { width: 96px; height: 96px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.file-tag {
  display: inline-flex; gap: 6px; align-items: center; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 3px 8px;
}
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer { padding: 12px 16px 16px; background: linear-gradient(to top, var(--bg) 75%, transparent); }
.composer > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.composer-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 8px;
}
.composer-row:focus-within { border-color: var(--accent); }
.composer-row.dragover { border-style: dashed; border-color: var(--accent); }
#input { flex: 1; resize: none; border: 0; outline: 0; background: transparent; max-height: 40vh; padding: 7px 4px; }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.attachment {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 4px 6px 4px 4px;
  font-size: 13px; max-width: 300px;
}
.attachment img { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; }
.attachment .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-left: 4px; }
.attachment button { padding: 0 6px; background: transparent; color: var(--muted); font-size: 16px; }
.notice { margin-bottom: 8px; padding: 8px 12px; border-radius: 10px; font-size: 13px; background: var(--warn-bg); color: var(--warn-text); }

/* Dialogs */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); width: min(480px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog h2 { margin-top: 0; }
dialog input {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2);
}
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 14px; }
.memory-list { list-style: none; padding: 0; margin: 12px 0; max-height: 45vh; overflow-y: auto; }
.memory-list li {
  display: flex; align-items: flex-start; gap: 8px; justify-content: space-between;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; font-size: 14px;
}
.memory-list li.empty-note { justify-content: center; color: var(--muted); border-style: dashed; }
.memory-list li button { padding: 0 6px; background: transparent; color: var(--muted); flex-shrink: 0; }
.memory-list li button:hover { color: var(--danger); }
.memory-form { display: flex; gap: 8px; }

@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 10; width: min(300px, 85vw);
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sidebar.open { transform: none; }
  .topbar { padding: 8px 12px; }
  .model-picker select { max-width: 55vw; }
  .messages { padding: 16px 12px; }
  .composer { padding: 8px 12px 12px; }
  .msg.user .body { max-width: 92%; }
}
