:root {
  --navy: #1B2D45; --navy-deep: #0f1c2e; --blue: #3572C4; --gold: #c9922a;
  --bg: #f4f6f9; --panel: #ffffff; --text: #1a2332; --muted: #64748b; --line: #e5e7eb;
  --mine: #3572C4; --mine-text: #ffffff; --theirs: #ffffff; --error: #b42318;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1c2e; --panel: #1a2332; --text: #e8edf4; --muted: #94a3b8; --line: #2a3a52; --theirs: #1B2D45; --error: #f97066; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif; }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; padding: 10px 14px; }
button.primary { background: var(--blue); color: #fff; font-weight: 600; }
button.primary:disabled { opacity: .5; cursor: default; }
button.link { background: none; color: var(--muted); padding: 6px; }
input, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; }
label { display: block; font-size: 14px; color: var(--muted); margin: 12px 0; }
label input { margin-top: 4px; }
.muted { color: var(--muted); margin-top: 0; }
.error { color: var(--error); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }

.screen.center { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; width: 100%; max-width: 380px; border-top: 4px solid var(--gold); }
.card h1 { margin: 8px 0 4px; font-size: 24px; }
.card button { width: 100%; margin-top: 8px; }
.logo { height: 56px; width: auto; display: block; }
.logo.small { height: 36px; }

#app { display: flex; height: 100dvh; }
#sidebar { width: 280px; flex: none; background: var(--navy-deep); color: #e8edf4; display: flex; flex-direction: column; }
.side-head { padding: 16px; display: grid; gap: 12px; border-bottom: 1px solid #22344f; }
#threads { flex: 1; overflow-y: auto; padding: 8px; }
#threads button { display: block; width: 100%; text-align: left; background: none; color: #cbd5e1; padding: 10px 12px; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#threads button.active, #threads button:hover { background: #1B2D45; color: #fff; }
.side-foot { padding: 12px 16px; border-top: 1px solid #22344f; display: flex; justify-content: space-between; align-items: center; font-size: 14px; color: #94a3b8; gap: 8px; }
.side-foot span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-head { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
#chat-head h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu { display: none; font-size: 20px; }
#messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: min(760px, 88%); padding: 10px 14px; border-radius: 14px; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--theirs); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.assistant p { margin: 0 0 8px; } .msg.assistant p:last-child { margin: 0; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 8px; padding-left: 22px; }
.msg.assistant h3, .msg.assistant h4 { margin: 8px 0 4px; font-size: 16px; }
.msg.pending { color: var(--muted); font-style: italic; }
.msg.failed { border-color: var(--error); color: var(--error); }
.files { font-size: 13px; opacity: .85; margin-top: 6px; }
.empty { margin: auto; text-align: center; color: var(--muted); max-width: 420px; }

#composer { border-top: 1px solid var(--line); background: var(--panel); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }
#composer .row { display: flex; gap: 8px; align-items: flex-end; }
#text { resize: none; max-height: 200px; }
.attach { margin: 0; font-size: 22px; padding: 6px; cursor: pointer; color: var(--text); }
.attach input { display: none; }
#attachments { display: flex; flex-wrap: wrap; gap: 6px; }
#attachments:not(:empty) { margin-bottom: 8px; }
.chip { background: var(--bg); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; font-size: 13px; display: flex; gap: 6px; align-items: center; }
.chip button { padding: 0 4px; background: none; color: var(--muted); }

@media (max-width: 760px) {
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; width: 82%; max-width: 300px; }
  #app.menu-open #sidebar { transform: none; }
  #menu { display: block; }
  .msg { max-width: 92%; }
}
