:root {
  --bg: #212121;
  --bg-elev: #2f2f2f;
  --bg-elev-2: #3a3a3a;
  --bg-sidebar: #171717;
  --text: #ececec;
  --text-muted: #b4b4b4;
  --text-dim: #8e8e8e;
  --border: #3d3d3d;
  --accent: #10a37f;
  --accent-hover: #1a7f64;
  --danger: #ef4444;
  --user-msg: #2f2f2f;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(0,0,0,.35);
  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; background: none; border: none; }
button { cursor: pointer; }
code, pre { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }

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

/* Sidebar */
.sidebar {
  width: 260px;
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid #262626;
  flex-shrink: 0;
  transition: transform .2s ease;
  z-index: 30;
}
.sidebar-top { padding: 12px; }
.new-chat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid #3a3a3a;
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
}
.new-chat-btn:hover { background: #2a2a2a; }
.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-muted);
  text-align: left;
  margin-bottom: 2px;
}
.chat-item:hover { background: #2a2a2a; color: var(--text); }
.chat-item.active { background: #2a2a2a; color: var(--text); }
.chat-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-item-delete {
  opacity: 0;
  color: var(--text-dim);
  padding: 2px;
  border-radius: 4px;
  display: flex;
  flex-shrink: 0;
}
.chat-item:hover .chat-item-delete,
.chat-item.active .chat-item-delete { opacity: 1; }
.chat-item-delete:hover { color: var(--danger); background: rgba(239,68,68,.1); }
.sidebar-bottom { padding: 12px; border-top: 1px solid #262626; }
.sidebar-action {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-muted);
}
.sidebar-action:hover { background: #2a2a2a; color: var(--text); }
.api-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-dim);
}
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #666;
  flex-shrink: 0;
}
.api-status.ok .status-dot { background: #22c55e; }
.api-status.err .status-dot { background: var(--danger); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid #2a2a2a;
  gap: 12px;
}
.model-badge {
  font-size: 14px;
  color: var(--text-muted);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.icon-btn {
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 8px; color: var(--text-muted);
}
.icon-btn:hover { background: var(--bg-elev); color: var(--text); }
.mobile-menu { display: none; }

/* Chat area */
.chat-area { flex: 1; overflow-y: auto; padding: 24px 16px 0; }
.empty-state {
  max-width: 680px;
  margin: 10vh auto 0;
  text-align: center;
  padding: 24px;
}
.empty-icon {
  width: 72px; height: 72px; margin: 0 auto 20px;
  border-radius: 50%; background: var(--bg-elev);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted);
}
.empty-state h1 { font-size: 28px; font-weight: 600; margin-bottom: 8px; }
.empty-state p { color: var(--text-muted); margin-bottom: 28px; }
.empty-hints {
  display: grid;
  grid-template-columns: repeat(2, minmax(0,1fr));
  gap: 10px;
}
.hint {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 14px;
  color: var(--text-muted);
  text-align: left;
}
.hint:hover { background: var(--bg-elev); color: var(--text); }

.messages { max-width: 800px; margin: 0 auto; padding-bottom: 24px; }
.message {
  display: flex;
  gap: 16px;
  padding: 24px 0;
  border-bottom: 1px solid #2a2a2a;
}
.message:last-child { border-bottom: none; }
.avatar {
  width: 32px; height: 32px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; flex-shrink: 0;
}
.message.user .avatar { background: #5436da; }
.message.assistant .avatar { background: var(--accent); }
.message.error .avatar { background: var(--danger); }
.message-body { flex: 1; min-width: 0; }
.message-role { font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.message-content { font-size: 15px; line-height: 1.7; color: var(--text); word-wrap: break-word; }
.message-content p { margin: 0 0 12px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content pre {
  background: #171717;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 14px;
  overflow-x: auto;
  margin: 12px 0;
}
.message-content code { font-size: 0.9em; }
.message-content :not(pre) > code {
  background: #3a3a3a;
  padding: 2px 6px;
  border-radius: 4px;
}
.message-content ul, .message-content ol { margin: 8px 0 12px 24px; }
.message-content li { margin: 4px 0; }
.message-content a { color: #7ab7ff; }
.message-content blockquote {
  border-left: 3px solid #555;
  padding-left: 12px;
  color: var(--text-muted);
  margin: 12px 0;
}
.message-content table { border-collapse: collapse; margin: 12px 0; width: 100%; }
.message-content th, .message-content td { border: 1px solid #444; padding: 8px 10px; text-align: left; }
.message-content th { background: #2a2a2a; }
.typing { color: var(--text-dim); font-style: italic; }
.cursor::after {
  content: '▋';
  animation: blink 1s steps(1) infinite;
  color: var(--text-muted);
  margin-left: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer-wrap {
  padding: 0 16px 16px;
  background: linear-gradient(transparent, var(--bg) 20%);
}
.composer {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid #444;
  border-radius: 24px;
  padding: 10px 10px 10px 18px;
}
.composer:focus-within { border-color: #666; }
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 200px;
  line-height: 1.5;
  font-size: 15px;
  padding: 6px 0;
  outline: none;
}
.composer textarea::placeholder { color: var(--text-dim); }
.send-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--text);
  color: #1a1a1a;
  flex-shrink: 0;
}
.send-btn:hover:not(:disabled) { background: #fff; }
.send-btn:disabled { opacity: .35; cursor: default; }
.stop-btn { background: #555; color: #fff; }
.stop-btn:hover { background: #666; }
.composer-note {
  max-width: 800px;
  margin: 8px auto 0;
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
}
.hidden { display: none !important; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 50;
}
.modal {
  width: 100%; max-width: 560px;
  background: #2a2a2a;
  border: 1px solid #444;
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 20px 0;
}
.modal-header h2 { font-size: 18px; font-weight: 600; }
.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field textarea {
  width: 100%;
  background: #1e1e1e;
  border: 1px solid #444;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
}
.field input:focus, .field textarea:focus { border-color: #777; }
.field small { font-size: 12px; color: var(--text-dim); line-height: 1.4; }
.field small code { background: #3a3a3a; padding: 1px 4px; border-radius: 3px; font-size: 11px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.checkbox-field { flex-direction: row; align-items: center; gap: 10px; }
.checkbox-field input { width: 16px; height: 16px; accent-color: var(--accent); }
.modal-footer {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 0 20px 20px;
}
.btn {
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: #3a3a3a; color: var(--text); }
.btn-secondary:hover { background: #444; }
.btn:disabled { opacity: .5; cursor: default; }
.test-result {
  margin: 0 20px 20px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  display: none;
}
.test-result.ok { display: block; background: rgba(34,197,94,.12); color: #86efac; }
.test-result.err { display: block; background: rgba(239,68,68,.12); color: #fca5a5; }

@media (max-width: 768px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow); }
  .mobile-menu { display: inline-flex; }
  .empty-hints { grid-template-columns: 1fr; }
  .message { gap: 12px; padding: 18px 0; }
  .field-row { grid-template-columns: 1fr; }
}
