:root {
  --bg: #0d0f15;
  --bg-2: #141821;
  --bg-3: #1b2030;
  --bg-4: #232a3d;
  --border: #2a3145;
  --border-2: #353e57;
  --text: #e7e9f0;
  --text-dim: #9aa4ba;
  --text-faint: #6b7488;
  --accent: #7c93ff;
  --accent-2: #6276f0;
  --accent-soft: rgba(124,147,255,0.14);
  --user-bubble: #1f2940;
  --think-bg: #16140f;
  --think-border: #3a3320;
  --think-accent: #d9b25a;
  --danger: #ff6b6b;
  --ok: #46d39a;
  --code-bg: #0a0c12;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "SF Mono", "Cascadia Code", "JetBrains Mono", Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font-family: var(--font); }

/* ---------------- Login ---------------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 18px; padding: 34px 30px; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.login-card h1 { font-size: 23px; margin: 0 0 4px; }
.login-card p.sub { color: var(--text-dim); margin: 0 0 24px; font-size: 14px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
input[type="text"], input[type="password"], select, textarea {
  width: 100%; background: var(--bg-3); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 11px 12px; font-size: 15px; font-family: var(--font); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.btn { width: 100%; background: var(--accent); color: #fff; border: none; border-radius: 10px; padding: 12px; font-size: 15px; font-weight: 600; cursor: pointer; }
.btn:hover { background: var(--accent-2); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.msg-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* ---------------- Chat layout ---------------- */
.app { display: flex; height: 100vh; overflow: hidden; }

.sidebar { width: 276px; background: var(--bg-2); border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar-head { padding: 14px; }
.new-chat { width: 100%; background: var(--accent); color: #fff; border: none; border-radius: 10px; padding: 11px; cursor: pointer; font-size: 14px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 8px; }
.new-chat:hover { background: var(--accent-2); }
.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.conv-item { display: flex; align-items: center; justify-content: space-between; padding: 9px 10px; border-radius: 9px; cursor: pointer; font-size: 14px; color: var(--text-dim); margin-bottom: 2px; gap: 6px; }
.conv-item:hover { background: var(--bg-3); color: var(--text); }
.conv-item.active { background: var(--bg-3); color: var(--text); }
.conv-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-item .del { opacity: 0; border: none; background: none; color: var(--text-dim); cursor: pointer; font-size: 14px; padding: 2px 4px; border-radius: 6px; }
.conv-item .del:hover { background: var(--bg-4); color: var(--danger); }
.conv-item:hover .del { opacity: 1; }
.sidebar-foot { padding: 12px 14px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-dim); display: flex; align-items: center; gap: 8px; }
.sidebar-foot .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 15px; padding: 6px; border-radius: 8px; display: inline-flex; }
.icon-btn:hover { background: var(--bg-3); color: var(--text); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 11px 16px; border-bottom: 1px solid var(--border); background: var(--bg-2); }
.topbar .menu-btn { display: none; background: none; border: none; color: var(--text); font-size: 22px; cursor: pointer; }
.topbar select { width: auto; max-width: 340px; padding: 8px 10px; font-size: 14px; }
.topbar .status { margin-left: auto; font-size: 12px; color: var(--text-dim); white-space: nowrap; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); margin-right: 5px; vertical-align: middle; }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--danger); }

.messages { flex: 1; overflow-y: auto; padding: 22px 0 8px; scroll-behavior: smooth; }
.msg-row { padding: 10px 20px; }
.msg-inner { max-width: 800px; margin: 0 auto; display: flex; gap: 14px; }
.avatar { width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; }
.avatar.user { background: var(--user-bubble); color: var(--accent); }
.avatar.assistant { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; }
.col { flex: 1; min-width: 0; }
.bubble { line-height: 1.65; font-size: 15px; word-wrap: break-word; overflow-wrap: anywhere; padding-top: 3px; }
.bubble p { margin: 0 0 12px; } .bubble p:last-child { margin-bottom: 0; }
.bubble h1,.bubble h2,.bubble h3 { margin: 16px 0 8px; 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 { margin: 0 0 12px; padding-left: 22px; } .bubble li { margin: 3px 0; }
.bubble blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--border-2); color: var(--text-dim); }
.bubble a { color: var(--accent); }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.bubble table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.bubble th,.bubble td { border: 1px solid var(--border-2); padding: 6px 10px; text-align: left; }
.bubble th { background: var(--bg-3); }
.bubble code.inline { background: var(--bg-3); padding: 1px 6px; border-radius: 6px; font-family: var(--mono); font-size: .88em; }

.codeblock { position: relative; margin: 0 0 12px; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--code-bg); }
.codeblock .cb-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 12px; background: var(--bg-3); font-size: 12px; color: var(--text-dim); }
.codeblock .cb-copy { background: none; border: 1px solid var(--border-2); color: var(--text-dim); border-radius: 6px; padding: 2px 9px; cursor: pointer; font-size: 11px; }
.codeblock .cb-copy:hover { color: var(--text); border-color: var(--accent); }
.codeblock pre { margin: 0; padding: 13px 14px; overflow-x: auto; }
.codeblock code { font-family: var(--mono); font-size: 13px; line-height: 1.55; }
.tok-key { color: #c792ea; } .tok-str { color: #c3e88d; } .tok-num { color: #f78c6c; }
.tok-com { color: #5c6680; font-style: italic; } .tok-fn { color: #82aaff; }

/* Thinking block */
.think { margin: 2px 0 12px; border: 1px solid var(--think-border); background: var(--think-bg); border-radius: 10px; overflow: hidden; }
.think-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; font-size: 13px; color: var(--think-accent); user-select: none; }
.think-head .chev { transition: transform .15s; font-size: 11px; }
.think.collapsed .chev { transform: rotate(-90deg); }
.think-head .spark { font-size: 13px; }
.think-body { padding: 0 14px 12px 14px; font-size: 13.5px; line-height: 1.6; color: #cdbb90; white-space: pre-wrap; max-height: 320px; overflow-y: auto; }
.think.collapsed .think-body { display: none; }
.think-timer { margin-left: auto; font-size: 11px; color: #8a7c52; }
.pulse { animation: pulse 1.3s ease-in-out infinite; }
@keyframes pulse { 0%,100%{opacity:.55} 50%{opacity:1} }

.empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-dim); text-align: center; padding: 20px; }
.empty h2 { color: var(--text); margin-bottom: 6px; font-size: 22px; }

.typing { display: inline-flex; gap: 4px; align-items: center; padding-top: 6px; }
.typing span { width: 7px; height: 7px; background: var(--text-dim); border-radius: 50%; animation: blink 1.2s infinite both; }
.typing span:nth-child(2){animation-delay:.2s} .typing span:nth-child(3){animation-delay:.4s}
@keyframes blink { 0%,80%,100%{opacity:.2} 40%{opacity:1} }

.msg-actions { display: flex; gap: 6px; margin-top: 7px; opacity: 0; transition: opacity .12s; }
.msg-row:hover .msg-actions { opacity: 1; }
.msg-actions button { background: none; border: 1px solid var(--border); color: var(--text-dim); border-radius: 7px; font-size: 11px; padding: 3px 9px; cursor: pointer; }
.msg-actions button:hover { color: var(--text); border-color: var(--accent); }

.attach-row { display: flex; flex-wrap: wrap; gap: 8px; max-width: 800px; margin: 0 auto 8px; padding: 0 4px; }
.attach-chip { display: flex; align-items: center; gap: 8px; background: var(--bg-3); border: 1px solid var(--border); border-radius: 9px; padding: 6px 8px; font-size: 12px; color: var(--text-dim); max-width: 220px; }
.attach-chip img { width: 34px; height: 34px; object-fit: cover; border-radius: 6px; }
.attach-chip .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip .rm { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 14px; }
.attach-chip .rm:hover { color: var(--danger); }

.composer { border-top: 1px solid var(--border); padding: 12px 16px 16px; background: var(--bg-2); }
.composer.dragover { background: var(--accent-soft); }
.composer-inner { max-width: 800px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; background: var(--bg-3); border: 1px solid var(--border); border-radius: 16px; padding: 8px 8px 8px 10px; }
.composer-inner:focus-within { border-color: var(--border-2); }
.composer textarea { resize: none; max-height: 220px; min-height: 24px; line-height: 1.5; border: none; background: transparent; padding: 8px 4px; }
.composer textarea:focus { border: none; }
.attach-btn { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 19px; padding: 6px; border-radius: 8px; flex-shrink: 0; align-self: flex-end; }
.attach-btn:hover { color: var(--text); background: var(--bg-4); }
.send-btn { background: var(--accent); border: none; color: #fff; border-radius: 11px; width: 40px; height: 40px; flex-shrink: 0; cursor: pointer; font-size: 17px; align-self: flex-end; display: flex; align-items: center; justify-content: center; }
.send-btn:hover { background: var(--accent-2); }
.send-btn:disabled { opacity: .45; cursor: not-allowed; }
.send-btn.stop { background: var(--danger); }
.hint { max-width: 800px; margin: 8px auto 0; font-size: 11px; color: var(--text-faint); text-align: center; }

/* Settings modal */
.modal-scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50; align-items: center; justify-content: center; padding: 20px; }
.modal-scrim.show { display: flex; }
.modal { width: 100%; max-width: 460px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 16px; padding: 22px; }
.modal h3 { margin: 0 0 16px; }
.modal label { display: block; font-size: 13px; color: var(--text-dim); margin: 14px 0 6px; }
.modal textarea { min-height: 90px; resize: vertical; }
.range-row { display: flex; align-items: center; gap: 12px; }
.range-row input[type=range] { flex: 1; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.modal-actions .btn { width: auto; padding: 9px 18px; }
.btn.ghost { background: var(--bg-3); color: var(--text); }
.btn.ghost:hover { background: var(--bg-4); }

@media (max-width: 820px) {
  .sidebar { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  .sidebar.open { transform: translateX(0); }
  .topbar .menu-btn { display: block; }
  .scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }
  .scrim.show { display: block; }
  .topbar select { max-width: 180px; }
  .msg-inner, .composer-inner, .hint, .attach-row { max-width: 100%; }
}

/* ===== ChatGPT-style additions ===== */
.new-chat .plus { font-size: 16px; }
.search-wrap { position: relative; margin-top: 10px; }
.search-wrap .search-ico { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--text-faint); font-size: 14px; }
.conv-search { padding: 8px 10px 8px 30px; font-size: 13px; border-radius: 9px; }
.avatar-sm { width: 26px; height: 26px; border-radius: 50%; background: linear-gradient(135deg,var(--accent),var(--accent-2)); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex-shrink: 0; }

/* Composer redesign */
.composer-inner { flex-direction: column; align-items: stretch; gap: 4px; padding: 8px 10px; }
.composer textarea { width: 100%; }
.composer-tools { display: flex; align-items: center; gap: 8px; }
.tool-spacer { flex: 1; }
.tool-btn { display: inline-flex; align-items: center; gap: 6px; background: transparent; border: 1px solid var(--border); color: var(--text-dim); border-radius: 18px; padding: 6px 12px; font-size: 13px; cursor: pointer; }
.tool-btn:hover { color: var(--text); border-color: var(--border-2); }
.tool-btn.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* Welcome / suggestions */
.welcome { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; text-align: center; }
.welcome .logo { width: 56px; height: 56px; border-radius: 16px; background: linear-gradient(135deg,var(--accent),var(--accent-2)); display: flex; align-items: center; justify-content: center; font-size: 28px; margin-bottom: 18px; }
.welcome h2 { font-size: 26px; margin: 0 0 6px; color: var(--text); }
.welcome p.sub { color: var(--text-dim); margin: 0 0 26px; }
.suggestions { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 12px; width: 100%; max-width: 620px; }
.sugg { text-align: left; background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; cursor: pointer; transition: border-color .12s, transform .05s; }
.sugg:hover { border-color: var(--accent); transform: translateY(-1px); }
.sugg .st { font-weight: 600; font-size: 14px; color: var(--text); margin-bottom: 3px; }
.sugg .sd { font-size: 12.5px; color: var(--text-dim); }

/* Generation stats under answers */
.gen-stats { font-size: 11px; color: var(--text-faint); margin-top: 6px; }

/* Scroll to bottom */
.scroll-btn { position: absolute; left: 50%; transform: translateX(-50%); bottom: 132px; width: 36px; height: 36px; border-radius: 50%; background: var(--bg-3); color: var(--text); border: 1px solid var(--border-2); cursor: pointer; font-size: 16px; box-shadow: 0 4px 14px rgba(0,0,0,.35); z-index: 5; }
.scroll-btn:hover { background: var(--bg-4); }
.main { position: relative; }

/* Settings checkbox row */
.check-row { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); margin-top: 16px; }
.check-row input { width: auto; }

/* inline rename input */
.conv-item .rename-input { flex: 1; background: var(--bg-4); border: 1px solid var(--accent); color: var(--text); border-radius: 6px; padding: 2px 6px; font-size: 13px; }

@media (max-width: 820px) {
  .suggestions { grid-template-columns: 1fr; }
  .scroll-btn { bottom: 150px; }
}

/* Model load progress bar */
.load-bar { position: relative; height: 26px; background: var(--bg-3); border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.load-bar .lb-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0%; background: linear-gradient(90deg,var(--accent),var(--accent-2)); transition: width .2s ease; }
.load-bar .lb-text { position: relative; z-index: 1; font-size: 12px; color: var(--text); padding-left: 14px; font-weight: 600; }

/* MCP tool activity in messages */
.tool-act { margin: 2px 0 12px; border: 1px solid var(--border); background: var(--bg-2); border-radius: 10px; overflow: hidden; }
.tool-act-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; font-size: 13px; color: var(--accent); user-select: none; }
.tool-act-head .chev { transition: transform .15s; font-size: 11px; }
.tool-act.collapsed .chev { transform: rotate(-90deg); }
.tool-act-body { padding: 0 12px 10px; font-size: 12.5px; }
.tool-act.collapsed .tool-act-body { display: none; }
.tool-call { margin: 6px 0; }
.tool-call .tc-name { font-family: var(--mono); color: var(--text); font-size: 12px; }
.tool-call .tc-args, .tool-call .tc-res { font-family: var(--mono); font-size: 11.5px; color: var(--text-dim); white-space: pre-wrap; word-break: break-word; background: var(--code-bg); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; margin-top: 4px; max-height: 160px; overflow: auto; }

/* MCP manager in settings */
.modal-note { font-size: 12px; color: var(--text-dim); margin: 0 0 10px; }
.mcp-list { display: flex; flex-direction: column; gap: 8px; }
.mcp-srv { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.mcp-srv-top { display: flex; align-items: center; gap: 10px; }
.mcp-srv .nm { font-weight: 600; font-size: 14px; flex: 1; }
.mcp-srv .badge { font-size: 11px; padding: 1px 7px; border-radius: 10px; border: 1px solid var(--border-2); color: var(--text-dim); }
.mcp-srv .badge.on { color: var(--ok); border-color: var(--ok); }
.mcp-srv .badge.err { color: var(--danger); border-color: var(--danger); }
.mcp-srv .desc { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.mcp-srv .toolnames { font-size: 11px; color: var(--text-faint); margin-top: 4px; font-family: var(--mono); }
.mcp-srv .rm { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 13px; }
.mcp-srv .rm:hover { color: var(--danger); }
.switch { position: relative; width: 38px; height: 21px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .sl { position: absolute; inset: 0; background: var(--bg-4); border-radius: 21px; transition: .15s; cursor: pointer; }
.switch .sl:before { content: ""; position: absolute; width: 15px; height: 15px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + .sl { background: var(--accent); }
.switch input:checked + .sl:before { transform: translateX(17px); }
.mcp-add { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
.mcp-add summary { cursor: pointer; font-size: 13px; color: var(--accent); }
.mcp-add label { display:block; font-size:12px; color:var(--text-dim); margin:10px 0 4px; }

/* Code block buttons + artifact preview */
.codeblock .cb-btns { display: flex; gap: 6px; }
.codeblock .cb-preview { background: var(--accent-soft); border: 1px solid var(--accent); color: var(--accent); border-radius: 6px; padding: 2px 9px; cursor: pointer; font-size: 11px; }
.codeblock .cb-preview:hover { background: var(--accent); color: #fff; }
.artifact-scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 60; padding: 24px; }
.artifact-scrim.show { display: flex; align-items: center; justify-content: center; }
.artifact { width: 100%; max-width: 1100px; height: 100%; max-height: 88vh; background: var(--bg-2); border: 1px solid var(--border); border-radius: 14px; display: flex; flex-direction: column; overflow: hidden; }
.artifact-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--bg-3); }
.artifact-head .atitle { font-weight: 600; font-size: 14px; }
.artifact-head .abtn { background: var(--bg-4); border: 1px solid var(--border-2); color: var(--text); border-radius: 8px; padding: 5px 12px; cursor: pointer; font-size: 12px; margin-left: 8px; }
.artifact-head .abtn:hover { border-color: var(--accent); }
.artifact iframe { flex: 1; width: 100%; border: none; background: #fff; }
