:root {
  --bg: #0a0a0b;
  --surface: #141416;
  --surface-2: #1b1b1f;
  --border: #26262b;
  --border-soft: #1f1f24;
  --text: #e8e8ea;
  --text-dim: #9a9aa3;
  --text-faint: #6a6a74;
  --accent: #ff6a00;
  --accent-2: #ff8c3a;
  --user-bubble: #232328;
  --danger: #e5484d;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; height: 100vh; }

/* ─── Sidebar ─── */
#sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 16px;
  overflow-y: auto;
  z-index: 30;
}

.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.logo { display: flex; align-items: center; gap: 10px; }
.logo-text { display: flex; flex-direction: column; line-height: 1.1; }
.logo-text strong { font-size: 15px; letter-spacing: 0.3px; }
.logo-text span { font-size: 11px; color: var(--accent); font-weight: 600; }
.side-close { display: none; }

.side-section { margin-bottom: 18px; }
.field-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 6px;
}

.key-row { display: flex; gap: 6px; }
.key-row input { flex: 1; }

input[type="password"], input[type="text"], select, textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
select { appearance: none; -webkit-appearance: none; background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="%239a9aa3" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>'); background-repeat: no-repeat; background-position: right 10px center; }
textarea { resize: vertical; font-family: inherit; }
.hint { font-size: 11.5px; color: var(--text-faint); margin-top: 6px; line-height: 1.45; }
.hint code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 11px; }

.section-head-row { display: flex; align-items: center; justify-content: space-between; }
.text-btn { background: none; border: none; color: var(--accent); font-size: 12px; cursor: pointer; padding: 0; }
.text-btn:hover { text-decoration: underline; }
#model-select:disabled { opacity: 0.5; }

.range-row { display: flex; justify-content: space-between; align-items: center; }
.range-row .field-label { margin-bottom: 0; }
.range-val { font-size: 12px; color: var(--text-dim); }
input[type="range"] { width: 100%; accent-color: var(--accent); margin-top: 6px; background: transparent; }

.side-foot { margin-top: auto; }
.endpoint-box {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 11px;
  margin-bottom: 12px;
}
.ep-label { display: block; font-size: 10px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.5px; }
.endpoint-box code { font-size: 12px; color: var(--text-dim); }
.btn-ghost {
  width: 100%;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  padding: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.btn-ghost:hover { border-color: var(--danger); color: var(--danger); }

.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }
#btn-sidebar-close { font-size: 20px; }

#sidebar-backdrop { display: none; }

/* ─── Main ─── */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  min-height: 56px;
}
.topbar-title { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-dim); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); transition: background 0.2s; }
.dot.online { background: #25c2a0; box-shadow: 0 0 8px rgba(37,194,160,0.6); }
.dot.loading { background: var(--accent); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }
.topbar-link { color: var(--accent); font-size: 13px; text-decoration: none; }
.topbar-link:hover { text-decoration: underline; }

#chat-area { flex: 1; overflow-y: auto; padding: 24px 0 8px; }

.welcome {
  max-width: 620px;
  margin: 0 auto;
  text-align: center;
  padding: 60px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.welcome h1 { font-size: 26px; margin: 18px 0 6px; font-weight: 700; }
.welcome p { color: var(--text-dim); font-size: 14px; margin: 0 0 22px; }
.welcome-chips { display: flex; gap: 8px; }
.chip {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 12px;
  color: var(--text-dim);
}

/* Messages */
#messages { max-width: 760px; margin: 0 auto; padding: 0 20px; display: flex; flex-direction: column; gap: 18px; }

.msg { display: flex; gap: 14px; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 8px;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
  margin-top: 2px;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .avatar { background: var(--accent); color: #1a1a1a; }
.msg.assistant .avatar { background: var(--surface-2); border: 1px solid var(--border); color: var(--accent); font-size: 12px; }
.msg.error .avatar { background: var(--danger); color: #fff; }
.msg.error .bubble { border-color: var(--danger); color: #ffb3b6; }

.msg .bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.6;
  background: var(--surface);
  border: 1px solid var(--border);
  overflow-wrap: break-word;
}
.msg.user .bubble { background: var(--user-bubble); border-color: var(--border-2); }
.msg.assistant .bubble { border-color: var(--border); }
.msg.error .bubble { background: rgba(229,72,77,0.08); }

.bubble pre {
  background: #0f0f11 !important;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0;
}
.bubble pre code { font-family: "SF Mono", "JetBrains Mono", Consolas, monospace; font-size: 13px; }
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1,.bubble h2,.bubble h3 { margin: 14px 0 8px; line-height: 1.3; }
.bubble ul, .bubble ol { margin: 8px 0; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble blockquote { border-left: 3px solid var(--accent); margin: 10px 0; padding: 2px 12px; color: var(--text-dim); }
.bubble a { color: var(--accent-2); }
.bubble code:not(pre code) { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.bubble table { border-collapse: collapse; margin: 10px 0; width: 100%; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; font-size: 13px; }
.bubble th { background: var(--surface-2); }

.msg .meta { font-size: 10.5px; color: var(--text-faint); margin-bottom: 4px; display: block; }
.msg.user .bubble .meta { text-align: right; }

.thinking-row { display: flex; gap: 4px; padding: 12px 14px; align-items: center; }
.thinking-row span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: blink 1.2s infinite; }
.thinking-row span:nth-child(2) { animation-delay: 0.2s; }
.thinking-row span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,100% { opacity: 0.2; } 50% { opacity: 1; } }

/* ─── Composer ─── */
#composer {
  padding: 12px 20px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
#composer-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 8px 8px 14px;
  transition: border-color 0.15s;
}
#composer-inner:focus-within { border-color: var(--accent); }
#input {
  flex: 1;
  background: transparent;
  border: none;
  resize: none;
  max-height: 160px;
  padding: 8px 0;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  outline: none;
}
#btn-send {
  width: 38px; height: 38px;
  flex-shrink: 0;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #1a1a1a;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, transform 0.1s;
}
#btn-send:hover { background: var(--accent-2); }
#btn-send:active { transform: scale(0.94); }
#btn-send:disabled { background: var(--surface-2); color: var(--text-faint); cursor: not-allowed; }
.composer-hint { max-width: 760px; margin: 8px auto 0; font-size: 11px; color: var(--text-faint); text-align: center; }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  max-width: 80%;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--danger); color: #ffb3b6; }

/* ─── Mobile ─── */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-105%);
    transition: transform 0.25s ease;
    width: 82vw;
    max-width: 320px;
    z-index: 40;
    box-shadow: 0 0 40px rgba(0,0,0,0.6);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 35;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.25s;
  }
  #sidebar-backdrop.show { opacity: 1; visibility: visible; }
  .side-close { display: inline-flex; }
  #messages { padding: 0 14px; }
  .bubble { max-width: 90%; }
  #composer { padding: 8px 12px 10px; }
  #chat-area { padding-top: 14px; }
  .welcome { padding: 40px 16px; }
}