:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sidebar: #eef1f5;
  --text: #1c2430;
  --muted: #5d6b7c;
  --border: #d9dfe7;
  --accent: #1f4e79;
  --accent-text: #ffffff;
  --accent-soft: #e3edf7;
  --user: #e3edf7;
  --error: #b42318;
  --code: #eef1f5;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161d;
    --panel: #171d25;
    --sidebar: #0d1218;
    --text: #e4e9f0;
    --muted: #93a1b3;
    --border: #2a333f;
    --accent: #5b9bd5;
    --accent-text: #0d1218;
    --accent-soft: #1d2b3a;
    --user: #1d2b3a;
    --error: #f97066;
    --code: #0d1218;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.error { color: var(--error); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

button { cursor: pointer; border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 8px 12px; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button:disabled { opacity: .5; cursor: default; }
button.link { border: 0; background: none; color: var(--muted); padding: 8px 0; text-align: left; }
button.wide { width: 100%; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; min-width: 40px; height: 40px; padding: 0 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); cursor: pointer; font-size: 18px; }
input[type=password], input[type=search], select, textarea {
  border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 9px 11px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; display: grid; gap: 10px; }
.login-card p { margin: 0 0 8px; }

/* Layout */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100dvh; }
.sidebar { background: var(--sidebar); border-right: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; padding: 14px; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; min-height: 40px; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -6px; padding: 0 6px; }
.chat-item { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 8px; }
.chat-item:hover { background: var(--accent-soft); }
.chat-item.active { background: var(--accent-soft); font-weight: 600; }
.chat-item .t { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .d { display: block; font-size: 12px; color: var(--muted); }
.chat-item .s { display: block; font-size: 12.5px; color: var(--muted); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.chat-list .none { color: var(--muted); font-size: 13px; padding: 8px 10px; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
.chat-title { flex: 1; font-size: 16px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar select { max-width: 48%; }

.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 12vh; padding: 0 8px; }
.empty h2 { margin: 0 0 6px; font-size: 20px; }

.msg { margin-bottom: 18px; }
.msg.user .bubble { background: var(--user); border-radius: 14px; padding: 10px 14px; margin-left: auto; width: fit-content; max-width: 88%; white-space: pre-wrap; word-wrap: break-word; }
.msg.user .imgs { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.msg.user .imgs img { width: 88px; height: 88px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; }
.msg.assistant .body { overflow-wrap: anywhere; }
.msg.assistant .body :is(p, ul, ol) { margin: 0 0 10px; }
.msg.assistant .body :is(h1, h2, h3, h4) { font-size: 15.5px; margin: 14px 0 6px; }
.msg.assistant .body code { background: var(--code); padding: 1px 5px; border-radius: 5px; font-size: 13.5px; }
.msg.assistant .body pre { background: var(--code); padding: 10px; border-radius: 8px; overflow-x: auto; }
.msg.assistant .body pre code { padding: 0; }
.msg.assistant .body table { border-collapse: collapse; display: block; overflow-x: auto; margin: 0 0 10px; }
.msg.assistant .body :is(th, td) { border: 1px solid var(--border); padding: 5px 8px; text-align: left; vertical-align: top; }
.status { color: var(--muted); font-size: 13.5px; display: flex; align-items: center; gap: 8px; }
.status::before { content: ""; width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--accent); border-right-color: transparent; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.msg .err { color: var(--error); }
.meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

.sources { margin-top: 8px; font-size: 13px; }
.sources summary { cursor: pointer; color: var(--muted); }
.sources ol { margin: 6px 0 0; padding-left: 22px; }
.sources li { margin-bottom: 4px; }
.sources a { color: var(--accent); text-decoration: none; }
.sources .tag { font-size: 11px; border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; margin-left: 4px; color: var(--muted); }

.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.composer > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea { flex: 1; resize: none; max-height: 40vh; min-height: 40px; }
.send { min-width: 44px; height: 40px; padding: 0 12px; }
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.thumb { position: relative; width: 64px; height: 64px; }
.thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.thumb button { position: absolute; top: -7px; right: -7px; width: 22px; height: 22px; padding: 0; border-radius: 50%; font-size: 12px; line-height: 1; }
.note { font-size: 12.5px; margin: 6px 0 0; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; }

.only-mobile { display: none; }
.scrim { display: none; }
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-flex; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 86vw; max-width: 320px; z-index: 20; transform: translateX(-100%); transition: transform .2s ease; }
  .sidebar.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  .messages { padding: 14px 12px 8px; }
  .topbar, .composer { padding-left: 12px; padding-right: 12px; }
}
