:root {
  --bg: #16151a;
  --bg-raised: #1c1b22;
  --bg-hover: #24232b;
  --border: #2c2b34;
  --text: #ffffff;
  --text-dim: #9d9aa6;
  --text-faint: #6b6875;
  --accent: #20b2aa;
  --accent-dim: #388883;
  --accent-text: #000000;
  --danger: #d97066;
  --ok: #7fb88a;
  --radius: 10px;
  --radius-sm: 6px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
}

* { box-sizing: border-box; }

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

button, input, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

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

/* ---------- sidebar ---------- */

#sidebar {
  width: 280px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-raised);
  border-right: 1px solid var(--border);
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 14px 8px;
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; letter-spacing: 0.2px; }
.brand-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.sidebar-actions { display: flex; flex-direction: column; gap: 8px; padding: 8px 14px 12px; }

.btn-block {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--accent-dim);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}
.btn-block:hover { filter: brightness(1.08); }
.btn-block.btn-secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.btn-block.btn-secondary:hover { background: var(--bg-hover); }

.invites-pill {
  margin: 0 14px 10px;
  padding: 7px 10px;
  border-radius: 999px;
  border: 1px solid var(--accent-dim);
  background: rgba(32, 178, 170, 0.12);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.invites-pill:hover { background: rgba(32, 178, 170, 0.2); }

#chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
}

.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-faint);
  padding: 12px 8px 6px;
}

.list { display: flex; flex-direction: column; gap: 1px; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-dim);
  position: relative;
}
.chat-item:hover { background: var(--bg-hover); color: var(--text); }
.chat-item.active { background: var(--bg-hover); color: var(--text); }
.chat-item.active::before {
  content: "";
  position: absolute;
  left: -8px; top: 8px; bottom: 8px;
  width: 3px;
  background: var(--accent);
  border-radius: 2px;
}
.chat-item .title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13.5px;
}
.chat-item .del {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 13px;
}
.chat-item:hover .del { opacity: 1; }
.chat-item .del:hover { color: var(--danger); background: var(--bg); }

.empty-list-hint {
  padding: 8px;
  font-size: 12.5px;
  color: var(--text-faint);
}

.sidebar-bottom { padding: 10px 14px; border-top: 1px solid var(--border); }
.ollama-status { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text-faint); }
.ollama-status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); flex-shrink: 0; }
.ollama-status.connected .dot { background: var(--ok); }
.ollama-status.disconnected .dot { background: var(--danger); }

/* ---------- main chat area ---------- */

#chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
}

.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
#chat-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  outline: none;
  border-radius: var(--radius-sm);
  padding: 2px 6px;
  margin-left: -6px;
}
#chat-title[contenteditable="true"] { background: var(--bg-raised); box-shadow: 0 0 0 1px var(--accent-dim); }
.chat-header-actions { display: flex; gap: 6px; flex-shrink: 0; }

.text-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  cursor: pointer;
}
.text-btn:hover { background: var(--bg-hover); color: var(--text); }
.text-btn.danger:hover { color: var(--danger); border-color: var(--danger); }

.members-bar {
  padding: 8px 24px;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-dim);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.member-chip {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  padding: 3px 9px;
  border-radius: 999px;
}

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

.msg { display: flex; flex-direction: column; max-width: 70%; }
.msg.role-user { align-self: flex-end; align-items: flex-end; }
.msg.role-assistant { align-self: flex-start; align-items: flex-start; }

.msg .author {
  font-size: 11.5px;
  color: var(--text-faint);
  margin-bottom: 4px;
  padding: 0 4px;
}

.bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.5;
  white-space: normal;
  word-wrap: break-word;
}
.msg.role-user .bubble {
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
}
.msg.role-assistant .bubble {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.role-assistant .bubble.pending::after {
  content: "";
  display: inline-block;
  width: 6px; height: 6px;
  margin-left: 4px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite;
}
@keyframes blink { 0%, 100% { opacity: 0.2; } 50% { opacity: 1; } }

.bubble code {
  background: rgba(0,0,0,0.25);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.9em;
}
.bubble pre.code-block {
  background: rgba(0,0,0,0.35);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 6px 0;
}
.bubble pre.code-block code { background: none; padding: 0; }
.bubble a { color: inherit; text-decoration: underline; }

.bubble.error {
  background: rgba(217, 112, 102, 0.12);
  border: 1px solid var(--danger);
  color: var(--danger);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 14px 24px 20px;
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1;
  resize: none;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  max-height: 200px;
  line-height: 1.4;
}
.composer textarea:focus { outline: none; border-color: var(--accent-dim); }
.send-btn {
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 50%;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
}
.send-btn:hover { filter: brightness(1.08); }
.send-btn:disabled { opacity: 0.4; cursor: default; }

/* ---------- modals ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 9, 12, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 440px;
  max-height: 86vh;
  overflow-y: auto;
  padding: 22px;
}
.modal h2 { margin: 0 0 4px; font-size: 17px; }
.modal .sub { color: var(--text-dim); font-size: 13px; margin: 0 0 16px; line-height: 1.5; }
.modal label { display: block; font-size: 12.5px; color: var(--text-dim); margin: 14px 0 6px; }
.modal label:first-of-type { margin-top: 0; }
.modal input[type=text], .modal input[type=email], .modal input[type=password], .modal textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  color: var(--text);
}
.modal input:focus, .modal textarea:focus { outline: none; border-color: var(--accent-dim); }
.modal .row { display: flex; gap: 8px; margin-top: 18px; }
.modal .row.end { justify-content: flex-end; }
.modal .msg-line { font-size: 13px; margin-top: 10px; }
.modal .msg-line.error { color: var(--danger); }
.modal .msg-line.ok { color: var(--ok); }

.btn {
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
  font-size: 13.5px;
}
.btn:hover { background: var(--bg-hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent-dim); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; color: var(--text-dim); }
.btn.ghost:hover { color: var(--text); background: var(--bg-hover); }

.token-box {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 11px;
  word-break: break-all;
  user-select: all;
  color: var(--accent);
}

.invite-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.invite-item:last-child { border-bottom: none; }
.invite-item .who { font-size: 13.5px; }
.invite-item .meta { font-size: 12px; color: var(--text-faint); }
.invite-actions { display: flex; gap: 6px; flex-shrink: 0; }

.warn-box {
  background: rgba(226, 163, 85, 0.1);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 12.5px;
  color: var(--text-dim);
  line-height: 1.5;
  margin-top: 14px;
}

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

@media (max-width: 720px) {
  #sidebar { position: fixed; z-index: 40; height: 100%; transform: translateX(-100%); transition: transform 0.2s; }
  #sidebar.open { transform: translateX(0); }
  .msg { max-width: 88%; }
}
