:root {
  --bg: #faf9f5;
  --sidebar: #f5f4ee;
  --surface: #ffffff;
  --bubble: #f0eee6;
  --hover: rgba(31, 30, 29, .06);
  --active: rgba(31, 30, 29, .09);
  --border: #e6e3d8;
  --border-strong: #d6d2c4;
  --text: #1f1e1d;
  --text-2: #5e5d59;
  --text-3: #8d8b84;
  --accent: #c96442;
  --accent-hover: #b4583a;
  --accent-soft: rgba(201, 100, 66, .12);
  --code-bg: #f3f1ea;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 18px rgba(0,0,0,.06);
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #262624; --sidebar: #1f1e1d; --surface: #30302e; --bubble: #141413;
    --hover: rgba(255,255,255,.06); --active: rgba(255,255,255,.09);
    --border: #3a3936; --border-strong: #4a4945;
    --text: #f2f0e8; --text-2: #b7b5ab; --text-3: #85837b;
    --accent: #d97757; --accent-hover: #e2896b; --accent-soft: rgba(217,119,87,.16);
    --code-bg: #1e1e1c; --danger: #f2b8b5;
    --shadow: 0 1px 2px rgba(0,0,0,.2), 0 4px 18px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #262624; --sidebar: #1f1e1d; --surface: #30302e; --bubble: #141413;
  --hover: rgba(255,255,255,.06); --active: rgba(255,255,255,.09);
  --border: #3a3936; --border-strong: #4a4945;
  --text: #f2f0e8; --text-2: #b7b5ab; --text-3: #85837b;
  --accent: #d97757; --accent-hover: #e2896b; --accent-soft: rgba(217,119,87,.16);
  --code-bg: #1e1e1c; --danger: #f2b8b5;
  --shadow: 0 1px 2px rgba(0,0,0,.2), 0 4px 18px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--sans); -webkit-font-smoothing: antialiased; overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, textarea { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; flex: none; }
.hidden { display: none !important; }

.icon-btn { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; color: var(--text-2); }
.icon-btn:hover { background: var(--hover); color: var(--text); }

/* ---------- layout ---------- */
#app { display: flex; height: 100dvh; }
#sidebar { width: 272px; flex: none; background: var(--sidebar); border-right: 1px solid var(--border); display: flex; flex-direction: column; transition: margin-left .2s ease; z-index: 20; }
body.sb-collapsed #sidebar { margin-left: -272px; }
.sb-top { display: flex; align-items: center; justify-content: space-between; padding: 12px 10px 6px 16px; }
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 19px; font-weight: 600; letter-spacing: -.01em; }
.brand .spark { color: var(--accent); width: 22px; height: 22px; }
.sb-actions { padding: 6px 8px; display: flex; flex-direction: column; gap: 2px; }
.sb-btn { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; color: var(--text); font-size: 14px; text-align: left; width: 100%; }
.sb-btn:hover { background: var(--hover); }
.sb-btn.primary .plus { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: #fff; }
.sb-btn.primary .plus svg { width: 14px; height: 14px; }
.sb-btn.primary { font-weight: 550; color: var(--accent); }
.search { display: flex; align-items: center; gap: 8px; margin: 2px 8px 6px; padding: 6px 10px; border-radius: 9px; border: 1px solid transparent; color: var(--text-3); }
.search:focus-within { border-color: var(--border-strong); background: var(--surface); }
.search input { border: 0; outline: 0; background: none; width: 100%; font-size: 14px; }
#chat-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.group-label { font-size: 12px; color: var(--text-3); padding: 14px 10px 4px; font-weight: 500; }
.chat-item { position: relative; display: flex; align-items: center; border-radius: 8px; }
.chat-item a { flex: 1; min-width: 0; padding: 7px 10px; font-size: 14px; color: var(--text); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item:hover, .chat-item.menu-open { background: var(--hover); }
.chat-item.active { background: var(--active); }
.chat-item .more { opacity: 0; width: 28px; height: 28px; margin-right: 2px; }
.chat-item:hover .more, .chat-item.active .more, .chat-item.menu-open .more { opacity: 1; }
.chat-item input { flex: 1; margin: 2px 4px; padding: 5px 6px; border: 1px solid var(--accent); border-radius: 6px; background: var(--surface); outline: 0; font-size: 14px; }
.sb-empty { color: var(--text-3); font-size: 13px; padding: 16px 10px; }
.sb-user { border-top: 1px solid var(--border); padding: 8px; }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px; width: 100%; text-align: left; }
.user-chip:hover { background: var(--hover); }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--text); color: var(--bg); font-size: 13px; font-weight: 600; flex: none; }
.user-chip .who { flex: 1; min-width: 0; line-height: 1.25; }
.user-chip .who b { display: block; font-weight: 550; font-size: 14px; }
.user-chip .who small { color: var(--text-3); font-size: 12px; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
#topbar { height: 52px; display: flex; align-items: center; gap: 6px; padding: 0 12px; flex: none; }
#topbar .title-btn { display: flex; align-items: center; gap: 4px; max-width: min(520px, 60vw); padding: 5px 8px; border-radius: 8px; font-size: 14px; color: var(--text); }
#topbar .title-btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topbar .title-btn:hover { background: var(--hover); }
#topbar .title-btn svg { width: 14px; height: 14px; color: var(--text-3); }
#open-sb { display: none; }
body.sb-collapsed #open-sb { display: inline-grid; }
.spacer { flex: 1; }

#scroller { flex: 1; overflow-y: auto; overflow-x: hidden; scroll-behavior: auto; }
#thread { max-width: 760px; margin: 0 auto; padding: 8px 24px 40px; }

/* ---------- welcome ---------- */
#welcome { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 0 24px 12vh; }
#welcome h1 { display: flex; align-items: center; gap: 14px; font: 400 clamp(30px, 4.4vw, 42px)/1.15 var(--serif); letter-spacing: -.02em; margin: 0 0 30px; text-align: center; }
#welcome h1 .spark { width: 38px; height: 38px; color: var(--accent); }
#welcome .composer-wrap { width: 100%; max-width: 680px; padding: 0; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 14px; }
.chip { display: flex; align-items: center; gap: 6px; padding: 7px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 13.5px; color: var(--text-2); background: var(--bg); }
.chip:hover { background: var(--hover); color: var(--text); }
.chip svg { width: 15px; height: 15px; }
body.in-chat #welcome { display: none; }
body:not(.in-chat) #scroller, body:not(.in-chat) #dock { display: none; }

/* ---------- composer ---------- */
#dock { flex: none; }
.composer-wrap { max-width: 760px; margin: 0 auto; padding: 0 24px 14px; }
.composer { background: var(--surface); border: 1px solid var(--border); border-radius: 20px; box-shadow: var(--shadow); padding: 12px 12px 10px 16px; transition: border-color .15s; }
.composer:focus-within { border-color: var(--border-strong); }
.composer.drag { border-color: var(--accent); background: var(--accent-soft); }
.composer textarea { display: block; width: 100%; border: 0; outline: 0; background: none; resize: none; font-size: 15.5px; line-height: 1.5; max-height: 40vh; min-height: 24px; padding: 2px 0 0; }
.composer textarea::placeholder { color: var(--text-3); }
.c-row { display: flex; align-items: center; gap: 6px; margin-top: 10px; }
.c-row .icon-btn { border: 1px solid var(--border); width: 32px; height: 32px; }
.model-btn { display: flex; align-items: center; gap: 4px; padding: 6px 8px 6px 10px; border-radius: 8px; font-size: 13.5px; color: var(--text-2); }
.model-btn:hover { background: var(--hover); color: var(--text); }
.model-btn svg { width: 14px; height: 14px; }
.send { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; }
.send:hover { background: var(--accent-hover); }
.send:disabled { opacity: .4; cursor: default; background: var(--accent); }
.send.stop { background: var(--text); color: var(--bg); }
.send svg { width: 16px; height: 16px; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.att { position: relative; display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); padding: 6px 10px; font-size: 12.5px; max-width: 220px; }
.att img { width: 52px; height: 52px; object-fit: cover; border-radius: 6px; margin: -2px -6px; }
.att.img { padding: 4px 8px; }
.att .fname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.att .x { position: absolute; top: -7px; right: -7px; width: 20px; height: 20px; border-radius: 50%; background: var(--text); color: var(--bg); display: grid; place-items: center; }
.att .x svg { width: 11px; height: 11px; }
.disclaimer { text-align: center; font-size: 11.5px; color: var(--text-3); margin-top: 8px; }

/* ---------- messages ---------- */
.msg { margin: 22px 0; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { background: var(--bubble); border-radius: 16px; padding: 10px 16px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15.5px; }
.msg.user .m-images { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.msg.user .m-images img { max-width: 220px; max-height: 220px; border-radius: 12px; border: 1px solid var(--border); cursor: zoom-in; }
.msg.user .m-files { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.msg-actions { display: flex; align-items: center; gap: 2px; margin-top: 4px; opacity: 0; transition: opacity .15s; color: var(--text-3); font-size: 12px; }
.msg:hover .msg-actions, .msg:last-child .msg-actions, .msg-actions:focus-within { opacity: 1; }
.msg-actions .icon-btn { width: 28px; height: 28px; }
.msg-actions .icon-btn svg { width: 15px; height: 15px; }
.msg-actions .meta { margin-left: 8px; }
.edit-box { width: min(100%, 640px); background: var(--surface); border: 1px solid var(--border-strong); border-radius: 16px; padding: 10px 12px; }
.edit-box textarea { width: 100%; border: 0; outline: 0; background: none; resize: none; font-size: 15.5px; line-height: 1.5; }
.edit-box .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.btn { padding: 7px 14px; border-radius: 9px; font-size: 13.5px; font-weight: 500; border: 1px solid var(--border-strong); }
.btn:hover { background: var(--hover); }
.btn.dark { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.dark:hover { opacity: .88; }
.btn.danger { color: var(--danger); }

.msg.assistant .md { font-family: var(--serif); font-size: 16.5px; line-height: 1.65; overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: 0 0 14px; }
.md h1, .md h2, .md h3, .md h4 { font-family: var(--serif); font-weight: 600; line-height: 1.3; margin: 22px 0 10px; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; } .md h4 { font-size: 1em; }
.md ul, .md ol { padding-left: 1.5em; }
.md li { margin: 4px 0; }
.md li > p { margin: 0 0 6px; }
.md a { color: var(--accent); }
.md strong { font-weight: 650; }
.md blockquote { border-left: 3px solid var(--border-strong); padding-left: 14px; color: var(--text-2); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }
.md code { font-family: var(--mono); font-size: .84em; background: var(--code-bg); border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-family: var(--sans); font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 7px 12px; text-align: left; vertical-align: top; }
.md th { background: var(--code-bg); font-weight: 600; }
.codeblock { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; margin: 0 0 14px; background: var(--code-bg); font-family: var(--sans); }
.codeblock .cb-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 6px 14px; font-size: 12px; color: var(--text-3); border-bottom: 1px solid var(--border); }
.codeblock .cb-head button { display: flex; align-items: center; gap: 5px; padding: 3px 7px; border-radius: 6px; font-size: 12px; }
.codeblock .cb-head button:hover { background: var(--hover); color: var(--text); }
.codeblock .cb-head svg { width: 13px; height: 13px; }
.codeblock pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.codeblock pre code { background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.55; }

.thinking { margin: 0 0 14px; border: 1px solid var(--border); border-radius: 12px; font-size: 13.5px; }
.thinking summary { list-style: none; display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; color: var(--text-2); user-select: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary:hover { color: var(--text); }
.thinking summary .chev { width: 14px; height: 14px; transition: transform .15s; margin-left: auto; }
.thinking[open] summary .chev { transform: rotate(180deg); }
.thinking .t-body { padding: 0 14px 12px; color: var(--text-2); white-space: pre-wrap; max-height: 360px; overflow-y: auto; font-size: 13.5px; line-height: 1.55; }
.shimmer { background: linear-gradient(90deg, var(--text-3) 0%, var(--text) 50%, var(--text-3) 100%); background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent; animation: shimmer 1.6s linear infinite; }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }
.typing { display: inline-flex; gap: 5px; padding: 8px 0 4px; vertical-align: middle; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: bob 1.2s ease-in-out infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bob { 0%, 60%, 100% { opacity: .35; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-4px); } }
.err { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); border-radius: 10px; padding: 10px 14px; font-size: 14px; margin-top: 8px; }
.loading-note { color: var(--text-3); font-size: 13px; margin-left: 6px; }

/* ---------- menus & modals ---------- */
.menu { position: fixed; z-index: 100; min-width: 200px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.14); padding: 5px; animation: pop .12s ease; }
@keyframes pop { from { opacity: 0; transform: scale(.97); } }
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; border-radius: 8px; font-size: 14px; text-align: left; }
.menu button:hover { background: var(--hover); }
.menu button.danger { color: var(--danger); }
.menu .sep { height: 1px; background: var(--border); margin: 5px 4px; }
.menu .mlabel { font-size: 12px; color: var(--text-3); padding: 6px 10px 2px; }
.menu.models { width: 300px; }
.menu.models button { align-items: flex-start; }
.menu.models .mm { flex: 1; min-width: 0; }
.menu.models .mm b { display: block; font-weight: 550; }
.menu.models .mm small { color: var(--text-3); font-size: 12.5px; }
.menu .check { color: var(--accent); margin-top: 2px; }

.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 90; display: grid; place-items: center; padding: 16px; animation: fade .15s; }
@keyframes fade { from { opacity: 0; } }
.modal { width: min(520px, 100%); max-height: 85vh; overflow-y: auto; background: var(--bg); border: 1px solid var(--border); border-radius: 18px; box-shadow: 0 20px 60px rgba(0,0,0,.25); padding: 22px 24px; }
.modal h2 { font: 500 22px var(--serif); margin: 0 0 16px; }
.modal h3 { font-size: 14px; margin: 22px 0 8px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 10px; font-size: 13px; color: var(--text-2); }
.field input, .field select { padding: 9px 11px; border: 1px solid var(--border-strong); border-radius: 9px; background: var(--surface); outline: 0; font-size: 14.5px; color: var(--text); }
.field input:focus { border-color: var(--accent); }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { padding: 5px 14px; border-radius: 7px; font-size: 13.5px; color: var(--text-2); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.user-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.user-row .who { flex: 1; }
.user-row small { color: var(--text-3); }
.note { font-size: 13px; color: var(--text-3); }
.ok { color: #2e7d32; font-size: 13px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.inline-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.inline-form .full { grid-column: 1 / -1; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.8); z-index: 120; display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 8px; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 10px; font-size: 14px; z-index: 130; animation: pop .15s; }

/* ---------- login ---------- */
#login { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: var(--bg); z-index: 50; }
#login .link { display: block; margin: 14px auto 0; font-size: 13.5px; color: var(--text-2); }
#login .link:hover { color: var(--accent); text-decoration: underline; }
#login .ok { text-align: left; margin-bottom: 10px; }
#login form { width: min(360px, 100%); text-align: center; }
#login .spark { width: 44px; height: 44px; color: var(--accent); }
#login h1 { font: 400 32px var(--serif); margin: 10px 0 6px; letter-spacing: -.02em; }
#login p { color: var(--text-2); margin: 0 0 26px; }
#login .field { text-align: left; }
#login .btn.dark { width: 100%; padding: 11px; font-size: 15px; margin-top: 6px; }
#login .err { text-align: left; }

/* ---------- mobile ---------- */
#scrim { display: none; }
@media (max-width: 800px) {
  #sidebar { position: fixed; inset: 0 auto 0 0; box-shadow: 0 0 40px rgba(0,0,0,.2); }
  body.sb-collapsed #sidebar { box-shadow: none; }
  body:not(.sb-collapsed) #scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 15; }
  #thread { padding: 4px 16px 28px; }
  .composer-wrap { padding: 0 12px 10px; }
  #welcome { padding: 0 16px 8vh; }
  .msg.user .bubble { max-width: 92%; }
  .msg-actions { opacity: 1; }
  .msg.assistant .md { font-size: 16px; }
  .composer textarea { font-size: 16px; }
}

.badge { display: inline-block; font-size: 11px; font-weight: 600; color: var(--accent); background: var(--accent-soft); border-radius: 6px; padding: 1px 6px; margin-left: 6px; }
.temp-pw { display: flex; align-items: center; gap: 8px; margin-top: 10px; padding: 10px 12px; border: 1px dashed var(--accent); border-radius: 10px; background: var(--accent-soft); font-size: 13.5px; }
.temp-pw code { font: 600 15px var(--mono); flex: 1; }
.menu .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-left: auto; }
