:root {
  --bg: #ffffff;
  --bg-side: #f7f7f8;
  --bg-soft: #f1f2f4;
  --bg-hover: #ebecef;
  --bg-active: #e3e5ea;
  --bubble: #f0f1f4;
  --text: #1d1f23;
  --text-2: #5b606a;
  --text-3: #8a8f99;
  --line: #e4e6ea;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --danger: #c92a2a;
  --code-bg: #f6f8fa;
  --shadow: 0 6px 24px rgba(20, 24, 33, .08), 0 1px 3px rgba(20, 24, 33, .06);
  --radius: 14px;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18191c;
    --bg-side: #111214;
    --bg-soft: #222327;
    --bg-hover: #26282c;
    --bg-active: #2e3035;
    --bubble: #2a2c31;
    --text: #e8e9ec;
    --text-2: #a9adb6;
    --text-3: #7b808a;
    --line: #2c2e33;
    --accent: #7491ff;
    --accent-text: #0e1220;
    --danger: #ff8787;
    --code-bg: #0f1012;
    --shadow: 0 6px 24px rgba(0, 0, 0, .4), 0 1px 3px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- layout ---- */
#app { display: flex; height: 100dvh; overflow: hidden; }
#sidebar {
  width: 272px; flex: none; display: flex; flex-direction: column;
  background: var(--bg-side); border-right: 1px solid var(--line);
}
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
@media (min-width: 821px) { .only-mobile { display: none !important; } }

/* ---- sidebar ---- */
.side-top { display: flex; gap: 6px; padding: 12px 12px 6px; }
.new-chat {
  flex: 1; display: flex; align-items: center; gap: 8px; padding: 9px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-weight: 550;
}
.new-chat:hover { background: var(--bg-hover); }
.search {
  margin: 6px 12px 8px; padding: 8px 11px; border-radius: 9px; border: 1px solid transparent;
  background: var(--bg-soft); outline: none;
}
.search:focus { border-color: var(--line); background: var(--bg); }
.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.group-label { font-size: 12px; font-weight: 600; color: var(--text-3); padding: 14px 8px 4px; }
.chat-item {
  display: flex; align-items: center; gap: 4px; border-radius: 9px; padding: 2px 2px 2px 10px;
  color: var(--text); text-decoration: none; position: relative;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); }
.chat-item .title-wrap { flex: 1; min-width: 0; padding: 6px 0; }
.chat-item .title { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.chat-item .by { display: block; font-size: 11.5px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .live { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; animation: pulse 1.2s infinite; }
.chat-item .item-actions { display: none; }
.chat-item:hover .item-actions, .chat-item.active .item-actions, .chat-item:focus-within .item-actions { display: flex; }
.chat-item input { width: 100%; padding: 3px 6px; border-radius: 6px; border: 1px solid var(--accent); background: var(--bg); outline: none; font-size: 14px; }
.side-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 14px; border-top: 1px solid var(--line); font-size: 14px;
}
.who { display: flex; align-items: center; gap: 8px; min-width: 0; }
.who span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700;
}
.link-btn { background: none; border: 0; color: var(--text-2); padding: 4px; font-size: 13.5px; }
.link-btn:hover { color: var(--text); text-decoration: underline; }
.empty-list { color: var(--text-3); font-size: 13.5px; padding: 16px 10px; }

/* ---- top bar ---- */
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; min-height: 56px; }
.pickers { display: flex; gap: 6px; flex-wrap: wrap; min-width: 0; }
.picker {
  display: flex; align-items: center; gap: 6px; border-radius: 10px; padding: 0 4px 0 10px;
  border: 1px solid var(--line); background: var(--bg);
}
.picker:hover { background: var(--bg-soft); }
.picker-label { font-size: 13px; color: var(--text-3); }
.picker select {
  border: 0; background: transparent; padding: 7px 4px; outline: none; font-weight: 550; font-size: 14px;
  max-width: 200px; cursor: pointer;
}
.picker select option { background: var(--bg); color: var(--text); }
.spacer { flex: 1; }
.icon-btn, .ghost-btn {
  display: inline-flex; align-items: center; gap: 6px; border: 0; background: none; border-radius: 9px;
  padding: 8px; color: var(--text-2);
}
.ghost-btn { padding: 7px 11px; font-size: 14px; }
.icon-btn:hover, .ghost-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ---- messages ---- */
.scroller { flex: 1; overflow-y: auto; overflow-x: hidden; scroll-behavior: auto; }
.messages { max-width: 780px; margin: 0 auto; padding: 8px 20px 24px; }
.empty { max-width: 780px; margin: 0 auto; padding: 18vh 20px 0; text-align: center; }
.empty h1 { font-size: 28px; font-weight: 650; margin: 0 0 8px; letter-spacing: -.01em; }
.empty p { color: var(--text-3); margin: 0; }

.msg { margin: 22px 0; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble {
  background: var(--bubble); border-radius: 18px 18px 6px 18px; padding: 10px 16px; max-width: 85%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg .author { font-size: 12px; color: var(--text-3); margin: 0 6px 4px; }
.msg.user .edit-box { width: min(100%, 640px); }
.edit-box textarea {
  width: 100%; min-height: 80px; padding: 10px 14px; border-radius: 14px; border: 1px solid var(--accent);
  background: var(--bg); resize: vertical; outline: none;
}
.edit-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.primary-btn, .secondary-btn {
  border-radius: 999px; padding: 7px 16px; font-weight: 600; font-size: 14px; border: 1px solid transparent;
}
.primary-btn { background: var(--text); color: var(--bg); }
.primary-btn:hover { opacity: .88; }
.secondary-btn { background: var(--bg); border-color: var(--line); }
.secondary-btn:hover { background: var(--bg-hover); }

.msg.assistant .body { overflow-wrap: anywhere; }
.msg-actions { display: flex; align-items: center; gap: 2px; margin-top: 4px; color: var(--text-3); min-height: 30px; }
.msg.user .msg-actions { opacity: 0; transition: opacity .15s; }
.msg.user:hover .msg-actions, .msg.user:focus-within .msg-actions { opacity: 1; }
.msg-actions button { border: 0; background: none; color: inherit; padding: 6px; border-radius: 7px; display: inline-flex; }
.msg-actions button:hover { background: var(--bg-hover); color: var(--text); }
.msg-actions svg { width: 16px; height: 16px; }
.meta { font-size: 12px; color: var(--text-3); margin-left: 6px; }
.msg .error {
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent); color: var(--danger);
  background: color-mix(in srgb, var(--danger) 8%, transparent); border-radius: 10px; padding: 9px 12px; font-size: 14px; margin-top: 8px;
}
.msg .stopped { font-size: 13px; color: var(--text-3); font-style: italic; margin-top: 6px; }

/* thinking */
.thinking { margin: 0 0 10px; }
.thinking summary {
  list-style: none; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  color: var(--text-2); font-size: 14px; padding: 2px 0; user-select: none;
}
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary svg { width: 14px; height: 14px; transition: transform .15s; }
.thinking[open] summary svg { transform: rotate(90deg); }
.thinking summary:hover { color: var(--text); }
.thinking.bare summary { cursor: default; pointer-events: none; }
.thinking.bare summary svg, .thinking.bare .thought { display: none; }
.thinking .thought {
  margin: 6px 0 4px 6px; padding: 2px 0 2px 14px; border-left: 2px solid var(--line);
  color: var(--text-2); font-size: 14px;
}
.thinking .thought p:first-child { margin-top: 0; }
.thinking .thought p:last-child { margin-bottom: 0; }
.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; } }
@keyframes pulse { 50% { opacity: .35; } }
.cursor::after { content: ""; display: inline-block; width: 8px; height: 8px; margin-left: 4px; border-radius: 50%; background: var(--text); animation: pulse 1s infinite; vertical-align: middle; }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md table, .md pre { margin: 0 0 14px; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 22px 0 10px; font-weight: 650; }
.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 hr { border: 0; border-top: 1px solid var(--line); margin: 20px 0; }
.md blockquote { border-left: 3px solid var(--line); padding: 2px 0 2px 14px; color: var(--text-2); }
.md img { max-width: 100%; }
.md :not(pre) > code {
  font-family: var(--mono); font-size: .88em; background: var(--bg-soft); padding: .15em .4em; border-radius: 5px;
}
.md .table-wrap { overflow-x: auto; margin: 0 0 14px; }
.md table { border-collapse: collapse; font-size: 14px; margin: 0; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--bg-soft); font-weight: 600; }
.code-block { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin: 0 0 14px; background: var(--code-bg); }
.code-head {
  display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 4px 12px;
  font: 12px var(--sans); color: var(--text-3); border-bottom: 1px solid var(--line);
}
.code-head button { border: 0; background: none; color: inherit; font-size: 12px; display: inline-flex; gap: 5px; align-items: center; padding: 4px 6px; border-radius: 6px; }
.code-head button:hover { color: var(--text); background: var(--bg-hover); }
.code-head svg { width: 14px; height: 14px; }
.code-block pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code-block code, .code-block code.hljs { font: 13px/1.55 var(--mono); background: transparent; padding: 0; }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

/* ---- composer ---- */
.composer-wrap { padding: 0 20px calc(10px + env(safe-area-inset-bottom)); }
.composer {
  max-width: 780px; margin: 0 auto; display: flex; flex-direction: column; gap: 6px;
  border: 1px solid var(--line); border-radius: 24px; background: var(--bg); padding: 8px;
  box-shadow: var(--shadow);
}
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.attach {
  width: 36px; height: 36px; border-radius: 50%; border: 0; display: grid; place-items: center; flex: none;
  background: none; color: var(--text-2);
}
.attach:hover:not(:disabled) { background: var(--bg-hover); color: var(--text); }
.attach:disabled { opacity: .35; cursor: default; }
.tray { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 0; }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: var(--bg-soft); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.busy img { opacity: .4; }
.thumb button {
  position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border-radius: 50%; border: 0; padding: 0;
  display: grid; place-items: center; background: rgba(0, 0, 0, .65); color: #fff;
}
.thumb button svg { width: 12px; height: 12px; stroke-width: 2.6; }
.msg-images { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; margin-bottom: 6px; max-width: 85%; }
.msg-images img {
  max-width: 220px; max-height: 220px; border-radius: 14px; border: 1px solid var(--line); display: block;
  cursor: zoom-in; object-fit: cover; background: var(--bg-soft);
}
.msg-images.many img { width: 120px; height: 120px; }
.edit-box .tray { padding: 0 0 6px; justify-content: flex-end; }
.lightbox {
  position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .82); display: grid; place-items: center;
  padding: 16px; cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; box-shadow: var(--shadow); }
.dropzone {
  position: fixed; inset: 0; z-index: 55; display: grid; place-items: center; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 12%, transparent); border: 3px dashed var(--accent);
}
.dropzone div { background: var(--bg); color: var(--text); padding: 12px 20px; border-radius: 12px; font-weight: 600; box-shadow: var(--shadow); }
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.composer textarea {
  flex: 1; border: 0; outline: none; resize: none; background: transparent; padding: 7px 0; min-width: 0;
  max-height: 40vh; line-height: 1.5;
}
.send {
  width: 36px; height: 36px; border-radius: 50%; border: 0; display: grid; place-items: center; flex: none;
  background: var(--text); color: var(--bg);
}
.send:disabled { opacity: .25; cursor: default; }
.send.stop svg { fill: currentColor; stroke: none; }
.fineprint { text-align: center; font-size: 12px; color: var(--text-3); margin: 8px 0 0; }

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 9px; font-size: 14px; box-shadow: var(--shadow);
}

/* ---- login ---- */
.login-body { display: grid; place-items: center; min-height: 100dvh; padding: 16px; background: var(--bg-side); }
.login-card {
  width: 100%; max-width: 380px; background: var(--bg); border: 1px solid var(--line); border-radius: 18px;
  padding: 32px 28px; box-shadow: var(--shadow); text-align: center;
}
.login-card h1 { font-size: 22px; margin: 14px 0 4px; }
.login-sub { color: var(--text-2); margin: 0 0 20px; }
.login-card form { display: grid; gap: 14px; text-align: left; }
.login-card label { display: grid; gap: 6px; font-size: 13.5px; font-weight: 550; color: var(--text-2); }
.login-card input {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); outline: none; font-size: 15px;
}
.login-card input:focus { border-color: var(--accent); }
.login-card .primary-btn { padding: 11px; border-radius: 10px; margin-top: 6px; font-size: 15px; }
.login-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); border-radius: 9px; padding: 8px 10px; font-size: 14px; }

/* ---- small screens ---- */
@media (max-width: 820px) {
  #sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 30; width: min(300px, 86vw);
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  #app.side-open #sidebar { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
  .topbar { padding: 8px 10px; gap: 4px; }
  .picker-label { display: none; }
  .picker select { max-width: 150px; }
  .ghost-btn span { display: none; }
  .messages { padding: 4px 16px 20px; }
  .composer-wrap { padding: 0 12px calc(8px + env(safe-area-inset-bottom)); }
  .msg.user .bubble, .msg-images { max-width: 92%; }
  .msg-images img { max-width: 160px; max-height: 160px; }
  .msg-images.many img { width: 96px; height: 96px; }
  .msg.user .msg-actions { opacity: 1; }
  .empty { padding-top: 12vh; }
  .empty h1 { font-size: 24px; }
}
