:root {
  --bg: #f9f8f5;
  --panel: #f1efe9;
  --surface: #ffffff;
  --border: #e3e0d8;
  --text: #1f1e1c;
  --muted: #7a776f;
  --accent: #c96442;
  --accent-text: #ffffff;
  --user-bubble: #ece9e1;
  --code-bg: #1e1e1e;
  --code-text: #e6e6e6;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1f1d;
    --panel: #181816;
    --surface: #2a2a27;
    --border: #3a3935;
    --text: #ecebe6;
    --muted: #9c998f;
    --accent: #d97757;
    --user-bubble: #34332f;
    --code-bg: #121211;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
button, select, textarea { font: inherit; color: inherit; }

.app { display: flex; height: 100vh; height: 100dvh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 270px; flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 14px 10px;
}
.side-top { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 12px; }
.brand { font-weight: 650; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--accent); font-size: 20px; }
.new-chat {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 10px; padding: 10px 12px; text-align: left; cursor: pointer;
}
.new-chat:hover { border-color: var(--accent); }
.conv-label { font-size: 12px; color: var(--muted); margin: 18px 8px 6px; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  white-space: nowrap; overflow: hidden;
}
.conv:hover { background: var(--border); }
.conv.active { background: var(--border); font-weight: 500; }
.conv .title { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.conv .del { opacity: 0; border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 4px; }
.conv:hover .del { opacity: 1; }
.side-foot { border-top: 1px solid var(--border); padding: 10px 6px 0; }
.provider { font-size: 12px; color: var(--muted); line-height: 1.5; word-break: break-all; }
.provider b { color: var(--text); font-weight: 600; }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #4caf50; margin-right: 6px; }
.dot.warn { background: #e0a030; }

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
}
.model-picker { display: flex; align-items: center; gap: 4px; flex: 1; min-width: 0; }
#modelSelect {
  max-width: 100%; min-width: 0;
  background: transparent; border: 1px solid transparent; border-radius: 8px;
  padding: 6px 8px; font-weight: 600; cursor: pointer;
}
#modelSelect:hover { border-color: var(--border); }
#modelSelect option { background: var(--surface); }
.icon-btn {
  border: 0; background: none; cursor: pointer; border-radius: 8px;
  width: 34px; height: 34px; font-size: 16px; color: var(--muted); flex-shrink: 0;
}
.icon-btn:hover { background: var(--panel); color: var(--text); }
.only-mobile { display: none; }

.system-box { padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
.system-box label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 6px; }
.system-box textarea {
  width: 100%; resize: vertical; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); padding: 8px 10px; outline: none;
}
.hidden { display: none !important; }

/* ---------- Messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.msg { max-width: 760px; margin: 0 auto 22px; line-height: 1.65; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 16px; border-radius: 18px;
  max-width: 85%; white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .bubble { word-wrap: break-word; }
.msg.assistant .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; display: flex; align-items: center; gap: 6px; }
.msg.assistant .meta .logo { font-size: 14px; }
.msg .error { color: #d9534f; background: rgba(217, 83, 79, .1); padding: 8px 12px; border-radius: 8px; margin-top: 6px; font-size: 14px; white-space: pre-wrap; }
.msg-actions { margin-top: 6px; display: flex; gap: 4px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-actions { opacity: 1; }
.msg-actions button { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 12px; padding: 3px 8px; border-radius: 6px; }
.msg-actions button:hover { background: var(--panel); color: var(--text); }

.bubble p { margin: 0 0 .8em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { margin: 1.1em 0 .5em; line-height: 1.3; }
.bubble h1 { font-size: 1.4em; } .bubble h2 { font-size: 1.25em; } .bubble h3 { font-size: 1.1em; }
.bubble ul, .bubble ol { padding-left: 1.4em; margin: 0 0 .8em; }
.bubble li { margin: .2em 0; }
.bubble a { color: var(--accent); }
.bubble blockquote { margin: 0 0 .8em; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.bubble hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.bubble table { border-collapse: collapse; margin: 0 0 .8em; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 5px 10px; }
.bubble code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .9em; background: var(--panel); padding: 1px 5px; border-radius: 5px; }
.codeblock { margin: 0 0 .9em; border-radius: 10px; overflow: hidden; background: var(--code-bg); }
.codeblock .code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; font-size: 12px; color: #aaa; background: rgba(255,255,255,.05); }
.codeblock .code-head button { border: 0; background: none; color: #aaa; cursor: pointer; font-size: 12px; }
.codeblock .code-head button:hover { color: #fff; }
.codeblock pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.codeblock pre code { background: none; padding: 0; color: var(--code-text); font-size: 13px; line-height: 1.55; }

.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

.empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; color: var(--muted); gap: 6px; }
.empty .logo { font-size: 42px; }
.empty h2 { color: var(--text); font-weight: 500; margin: 8px 0 0; font-size: 26px; }

/* ---------- Composer ---------- */
.composer-wrap { padding: 8px 16px 14px; }
.composer {
  max-width: 760px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 10px 10px 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,.04);
}
.composer.drag { border-color: var(--accent); border-style: dashed; background: var(--panel); }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.attach-btn { font-size: 17px; }

/* ---------- Attachments ---------- */
.attach-list { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 2px 0 8px; }
.attach-list:empty { display: none; }
.chip {
  position: relative; display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 6px 10px; font-size: 13px; max-width: 240px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); }
.chip .ico { font-size: 18px; flex-shrink: 0; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .size { color: var(--muted); font-size: 11px; display: block; }
.chip img { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; flex-shrink: 0; }
.chip.img-only { padding: 0; border: 0; background: none; }
.chip.img-only img { width: 64px; height: 64px; border-radius: 10px; border: 1px solid var(--border); }
.chip .rm {
  position: absolute; top: -7px; right: -7px; width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 11px; line-height: 1; padding: 0;
}
.msg.user { flex-direction: column; align-items: flex-end; gap: 6px; }
.msg-attach { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; max-width: 85%; }
.msg-attach .chip.img-only img { width: auto; height: auto; max-width: 260px; max-height: 220px; }
.composer:focus-within { border-color: var(--accent); }
#input { flex: 1; border: 0; outline: none; resize: none; background: transparent; max-height: 220px; line-height: 1.5; padding: 4px 0; }
.send {
  width: 34px; height: 34px; border-radius: 10px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-size: 17px; font-weight: 700; flex-shrink: 0;
}
.send:disabled { opacity: .4; cursor: default; }
.send.stop { font-size: 12px; }
.hint { max-width: 760px; margin: 6px auto 0; text-align: center; font-size: 11px; color: var(--muted); }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgba(0,0,0,.2); }
  .sidebar.open { transform: none; }
  .hint { display: none; }
}
