:root {
  --bg: #14161a;
  --panel: #1a1d23;
  --panel-2: #20242c;
  --border: #2a2e37;
  --text: #e6e8ec;
  --text-muted: #888e9c;
  --accent: #4fd1c5;
  --accent-dim: #2c9c92;
  --danger: #e2685f;
  --radius: 10px;
  --mono: "SFMono-Regular", "JetBrains Mono", Consolas, "Liberation Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Main / center content ---------- */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.thread-title {
  font-family: var(--mono);
  font-size: 13px;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-family: var(--mono);
  font-size: 12px;
  max-width: 240px;
  display: none;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  max-width: 320px;
}
.empty-state-icon {
  font-size: 28px;
  color: var(--accent-dim);
  margin-bottom: 8px;
}

.msg {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  gap: 12px;
}

.msg-role {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  margin-top: 2px;
}
.msg.user .msg-role {
  background: var(--panel-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.msg.assistant .msg-role {
  background: rgba(79, 209, 197, 0.12);
  color: var(--accent);
  border: 1px solid rgba(79, 209, 197, 0.25);
}

.msg-body {
  flex: 1;
  line-height: 1.6;
  word-wrap: break-word;
  padding-top: 3px;
}
.msg-body.plain { white-space: pre-wrap; }
.msg.user .msg-body { color: var(--text); }
.msg.assistant .msg-body { color: var(--text); }

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

/* ---------- Markdown content ---------- */

.msg-body :first-child { margin-top: 0; }
.msg-body :last-child { margin-bottom: 0; }

.msg-body h1, .msg-body h2, .msg-body h3,
.msg-body h4, .msg-body h5, .msg-body h6 {
  margin: 18px 0 8px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
}
.msg-body h1 { font-size: 1.4em; }
.msg-body h2 { font-size: 1.25em; }
.msg-body h3 { font-size: 1.1em; }
.msg-body h4, .msg-body h5, .msg-body h6 { font-size: 1em; }

.msg-body p { margin: 8px 0; }

.msg-body ul, .msg-body ol {
  margin: 8px 0;
  padding-left: 24px;
}
.msg-body li { margin: 3px 0; }
.msg-body li > p { margin: 2px 0; }

.msg-body blockquote {
  margin: 10px 0;
  padding: 4px 14px;
  border-left: 3px solid var(--accent-dim);
  color: var(--text-muted);
  background: rgba(79, 209, 197, 0.05);
}
.msg-body blockquote p { margin: 4px 0; }

.msg-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

.msg-body a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: rgba(79, 209, 197, 0.4);
}
.msg-body a:hover { text-decoration-color: var(--accent); }

.msg-body img {
  max-width: 100%;
  border-radius: 6px;
  margin: 6px 0;
}

.msg-body table {
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}
.msg-body th, .msg-body td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.msg-body th {
  background: var(--panel-2);
  font-weight: 600;
}

/* Inline code (not inside a <pre>) */
.msg-body :not(pre) > code {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: var(--mono);
  font-size: 0.88em;
  color: var(--accent);
}

/* Fenced code blocks, wrapped by decorateCodeBlocks() in app.js */
.msg-body pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  background: transparent;
}
.msg-body pre code {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  background: none;
  border: none;
  padding: 0;
  color: inherit;
}

.code-block {
  margin: 10px 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #0d0f13;
}

.code-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 11px;
}
.code-lang {
  color: var(--text-muted);
  text-transform: lowercase;
  letter-spacing: 0.03em;
}
.code-copy-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 5px;
  padding: 3px 9px;
  font-family: var(--mono);
  font-size: 11px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.code-copy-btn:hover { color: var(--accent); border-color: var(--accent-dim); }

/* ---------- Composer ---------- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 16px 24px 22px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer-input {
  flex: 1;
  max-width: 760px;
  margin: 0 auto;
  resize: none;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-family: var(--sans);
  font-size: 14.5px;
  line-height: 1.5;
  max-height: 200px;
  outline: none;
}
.composer-input:focus { border-color: var(--accent-dim); }

.send-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--accent);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}
.send-btn:hover { background: rgba(79, 209, 197, 0.12); border-color: var(--accent-dim); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Sidebar (right) ---------- */

.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
}

.sidebar-title {
  font-family: var(--mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.new-btn {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--accent);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.new-btn:hover { background: rgba(79, 209, 197, 0.12); }

.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.thread-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-muted);
  position: relative;
}
.thread-item:hover { background: var(--panel-2); }
.thread-item.active {
  background: rgba(79, 209, 197, 0.1);
  color: var(--text);
}
.thread-item.active .thread-name { color: var(--text); }

.thread-info { flex: 1; min-width: 0; }

.thread-name {
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.thread-preview {
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.thread-delete {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: 5px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}
.thread-item:hover .thread-delete { display: flex; }
.thread-delete:hover { background: rgba(226, 104, 95, 0.15); color: var(--danger); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 11px;
}

.conn-status { color: var(--text-muted); }
.conn-status.ok { color: var(--accent); }
.conn-status.err { color: var(--danger); }

/* ---------- Scrollbars ---------- */

.messages::-webkit-scrollbar, .thread-list::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb, .thread-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* ---------- Responsive ---------- */

@media (max-width: 760px) {
  .app { flex-direction: column-reverse; }
  .sidebar {
    width: 100%;
    height: 40vh;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
