/* ============ LLM Chat UI — dark, ChatGPT-like ============ */
:root {
  --bg: #14161a;
  --bg-soft: #1b1e24;
  --bg-elev: #22262e;
  --border: #2c313a;
  --text: #e8eaed;
  --text-dim: #9aa3af;
  --accent: #10b981;
  --accent-soft: rgba(16, 185, 129, 0.14);
  --danger: #ef4444;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
}
.hidden { display: none !important; }

.app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 264px;
  min-width: 264px;
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.18s ease;
}
.sidebar.collapsed { margin-left: -264px; }
.sidebar-head { padding: 12px; border-bottom: 1px solid var(--border); }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 9px 10px; margin-bottom: 2px;
  border: none; border-radius: 8px;
  background: transparent; color: var(--text-dim);
  font: inherit; text-align: left; cursor: pointer;
}
.conv-item:hover { background: var(--bg-elev); color: var(--text); }
.conv-item.active { background: var(--accent-soft); color: var(--text); }
.conv-item .t {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item .del {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 14px; padding: 2px 4px; border-radius: 4px; opacity: 0;
}
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: var(--danger); background: rgba(239,68,68,0.12); }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.server-status { font-size: 12px; color: var(--text-dim); display: flex; align-items: center; gap: 6px; }
.server-status::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: #6b7280; flex: none;
}
.server-status.ok::before { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.server-status.bad::before { background: var(--danger); box-shadow: 0 0 6px var(--danger); }

/* ---------- Main / topbar ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 56px; flex: none;
  display: flex; align-items: center; gap: 10px;
  padding: 0 14px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: 6px; }
#modelSelect {
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; font: inherit; font-size: 14px; max-width: 220px;
}

/* ---------- Chat area ---------- */
.chat { flex: 1; overflow-y: auto; padding: 24px 0 8px; scroll-behavior: smooth; }

.empty { max-width: 640px; margin: 6vh auto 0; padding: 0 24px; text-align: center; }
.empty-logo { font-size: 44px; }
.empty h1 { font-size: 26px; margin: 12px 0 6px; }
.empty-sub { color: var(--text-dim); margin: 0 0 22px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--text-dim); border-radius: 999px; padding: 8px 14px;
  font: inherit; font-size: 14px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--text); }

.msg { display: flex; gap: 12px; max-width: 860px; margin: 0 auto 22px; padding: 0 24px; }
.msg.user { flex-direction: row-reverse; }
.avatar {
  width: 34px; height: 34px; flex: none; border-radius: 8px;
  display: flex; align-items: center; justify-content: center; font-size: 17px;
}
.msg.user .avatar { background: var(--accent-soft); }
.msg.assistant .avatar { background: var(--bg-elev); border: 1px solid var(--border); }
.bubble {
  min-width: 0; max-width: 78%;
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--bg-soft); border: 1px solid var(--border);
  overflow-wrap: break-word;
}
.msg.user .bubble { background: var(--accent-soft); border-color: transparent; }
.bubble.error { border-color: var(--danger); }
.bubble.error .errline { color: var(--danger); font-size: 13px; margin-top: 4px; }

/* assistant markdown */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { margin: 14px 0 8px; line-height: 1.3; }
.bubble h1 { font-size: 1.35em; } .bubble h2 { font-size: 1.22em; } .bubble h3 { font-size: 1.1em; }
.bubble ul, .bubble ol { margin: 6px 0 10px; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble blockquote {
  margin: 8px 0; padding: 4px 12px; border-left: 3px solid var(--accent);
  color: var(--text-dim); background: var(--bg-elev); border-radius: 0 8px 8px 0;
}
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.bubble a { color: #5eead4; }
.bubble code {
  font-family: var(--mono); font-size: 0.88em;
  background: var(--bg-elev); padding: 1px 5px; border-radius: 5px;
}
.bubble pre {
  position: relative; background: #0f1115; border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; margin: 10px 0;
  overflow-x: auto;
}
.bubble pre code { background: none; padding: 0; font-size: 0.85em; line-height: 1.5; display: block; }
.code-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: -4px 0 8px; font-size: 11px; color: var(--text-dim);
  font-family: var(--mono); text-transform: lowercase;
}
.copy-code {
  position: absolute; top: 8px; right: 8px;
  background: var(--bg-elev); color: var(--text-dim);
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 11px; padding: 3px 8px; cursor: pointer;
}
.copy-code:hover { color: var(--text); border-color: var(--accent); }
.caret { display: inline-block; width: 8px; height: 16px; background: var(--accent); vertical-align: text-bottom; margin-left: 2px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.thinking { color: var(--text-dim); font-size: 14px; padding: 12px 2px; }
.thinking span { animation: dots 1.2s infinite; }
.thinking span:nth-child(2) { animation-delay: 0.2s; }
.thinking span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dots { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.msg-meta { display: flex; gap: 10px; align-items: center; margin-top: 6px; font-size: 12px; color: var(--text-dim); }
.msg-meta .stats { opacity: 0; transition: opacity 0.3s; }
.msg-meta .stats.show { opacity: 1; }
.msg-meta button {
  background: none; border: none; color: var(--text-dim);
  font-size: 12px; cursor: pointer; padding: 0;
}
.msg-meta button:hover { color: var(--text); }
.msg-meta .copied { color: var(--accent); }

/* ---------- Input bar ---------- */
.inputbar { flex: none; padding: 10px 24px 14px; }
.input-shell {
  max-width: 860px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: 14px; padding: 10px 12px;
  transition: border-color 0.15s;
}
.input-shell:focus-within { border-color: var(--accent); }
#input {
  flex: 1; resize: none; border: none; outline: none; background: none;
  color: var(--text); font: inherit; line-height: 1.45;
  max-height: 160px; padding: 4px 2px;
}
#input::placeholder { color: var(--text-dim); }
.send, .stop-btn {
  flex: none; width: 38px; height: 38px; border-radius: 10px;
  border: none; cursor: pointer; font-size: 16px;
}
.btn.primary { background: var(--accent); color: #06281c; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.primary:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost {
  background: none; border: 1px solid var(--border); color: var(--text-dim);
  border-radius: 8px; padding: 7px 12px; cursor: pointer; font: inherit; font-size: 14px;
}
.btn.ghost:hover { color: var(--text); border-color: var(--text-dim); }
.btn.block { width: 100%; }
.btn.icon-only { padding: 6px 9px; }
.hint { max-width: 860px; margin: 8px auto 0; text-align: center; font-size: 11.5px; color: var(--text-dim); }
.foot-links { max-width: 860px; margin: 5px auto 0; text-align: center; font-size: 12px; color: var(--text-dim); }
.foot-links a { color: var(--text-dim); text-decoration: underline; cursor: pointer; }
.foot-links a:hover { color: var(--text); }
.model-badge {
  font-size: 13px; color: var(--text-dim);
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 12px; white-space: nowrap;
}

/* ---------- Modal (disclaimers) ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 60; }
.modal {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(560px, 92vw); max-height: 80vh;
  background: var(--bg-soft); border: 1px solid var(--border); border-radius: 14px;
  z-index: 70; display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px 18px; overflow-y: auto; }
.modal-body ul, .modal-body ol { margin: 8px 0; padding-left: 22px; }
.modal-body li { margin: 4px 0; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 13px; color: var(--text-dim); display: flex; justify-content: space-between; }
.field small { color: var(--text-dim); font-size: 11.5px; }
.field input[type="text"], .field input[type="number"], .field textarea {
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font: inherit; font-size: 14px;
}
.field input[type="range"] { accent-color: var(--accent); }
.field textarea { resize: vertical; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 18px; font-size: 14px; z-index: 100;
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
}

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #333a44; border-radius: 6px; border: 2px solid var(--bg); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .sidebar { position: fixed; z-index: 30; height: 100%; box-shadow: 8px 0 30px rgba(0,0,0,0.4); }
  .bubble { max-width: 88%; }
  .msg { padding: 0 12px; }
}
