:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #666;
  --border: #ddd;
  --accent: #2563eb;
  --danger: #dc2626;
  --panel: #f7f7f8;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --fg: #e8e8e8;
    --muted: #999;
    --border: #333;
    --accent: #5b8def;
    --danger: #f87171;
    --panel: #202124;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--fg);
}
.hidden { display: none !important; }

.auth-view {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: 280px;
}
.auth-form input, .auth-form button {
  padding: 0.6rem;
  font-size: 1rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--fg);
}
.auth-form button[type="submit"] { background: var(--accent); color: white; border: none; }
.error { color: var(--danger); min-height: 1.2em; font-size: 0.9rem; }

.app-view {
  display: flex;
  height: 100vh;
}
@media (min-width: 900px) {
  .app-view {
    max-width: 1200px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}
.sidebar {
  width: 240px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  flex-shrink: 0;
}

.chats-panel {
  width: 240px;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  flex-shrink: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab-btn {
  flex: 1;
  padding: 0.6rem;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.85rem;
}
.tab-btn.active { color: var(--fg); border-bottom-color: var(--accent); }
.tab-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.memory-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0.5rem 0.75rem; display: flex; flex-direction: column; gap: 0.5rem; }
.memory-item { font-size: 0.82rem; padding: 0.5rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.memory-empty, .memory-error { padding: 0.6rem 0.75rem; font-size: 0.82rem; color: var(--muted); }
.memory-error { color: var(--danger); }

/* Below the breakpoint, drop back to today's single-column layout: bots panel,
   chats/memory panel, and main chat stack full-width instead of sitting side by side. */
@media (max-width: 899px) {
  .app-view { flex-direction: column; height: auto; min-height: 100vh; }
  .sidebar, .chats-panel { width: 100%; border: none; border-bottom: 1px solid var(--border); }
}
.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.sidebar-header button {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
}
.sidebar-section-label {
  padding: 0.6rem 0.75rem 0.3rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.new-chat-inline-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  color: var(--fg);
  cursor: pointer;
  font-size: 0.75rem;
  text-transform: none;
  letter-spacing: normal;
}

.bot-list { flex: 0 1 auto; max-height: 35vh; overflow-y: auto; }
.bot-item {
  padding: 0.6rem 0.75rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.bot-item.active { background: var(--accent); color: white; }
.bot-item .bot-item-meta { font-size: 0.75rem; color: var(--muted); }
.bot-item.active .bot-item-meta { color: rgba(255,255,255,0.8); }

.chats-section { flex: 1; min-height: 0; display: flex; flex-direction: column; border-top: 1px solid var(--border); }
.chat-list { flex: 1; min-height: 0; overflow-y: auto; }
.chat-list-group-label {
  padding: 0.2rem 0.75rem;
  font-size: 0.7rem;
  color: var(--muted);
}
.chat-list-dropzone { min-height: 8px; }
.chat-list-dropzone.drag-over { background: rgba(37, 99, 235, 0.12); outline: 1px dashed var(--accent); outline-offset: -2px; }
.chat-item {
  padding: 0.45rem 0.75rem;
  cursor: grab;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
  gap: 0.4rem;
}
.chat-item.active { background: var(--accent); color: white; }
.chat-item.dragging { opacity: 0.4; }
.chat-item-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item-dismiss {
  background: none;
  border: none;
  color: inherit;
  opacity: 0.6;
  cursor: pointer;
  font-size: 0.9rem;
  flex-shrink: 0;
}
.chat-item-dismiss:hover { opacity: 1; }

.avatar {
  border-radius: 50%;
  object-fit: cover;
  background: var(--panel);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.avatar-sm { width: 64px; height: 64px; }
.avatar-md { width: 56px; height: 56px; }
.avatar-header { width: 88px; height: 88px; }
.avatar-lg { width: 192px; height: 192px; }

.bot-header-title { display: flex; align-items: center; gap: 0.6rem; }

.avatar-fieldset { border: 1px solid var(--border); border-radius: 6px; }
.avatar-editor { display: flex; gap: 1rem; align-items: flex-start; }
.avatar-editor-controls { flex: 1; display: flex; flex-direction: column; gap: 0.5rem; }
.avatar-editor-controls textarea {
  padding: 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
}
.avatar-editor-buttons { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.avatar-editor-buttons button {
  padding: 0.4rem 0.7rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--fg);
  cursor: pointer;
  font-size: 0.85rem;
}
.new-bot-btn {
  margin: 0.75rem;
  padding: 0.5rem;
  border-radius: 6px;
  border: 1px dashed var(--border);
  background: none;
  color: var(--fg);
  cursor: pointer;
}

.main { flex: 1; display: flex; flex-direction: column; }
.empty-state { margin: auto; color: var(--muted); }

.bot-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.bot-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.bot-header-actions { display: flex; align-items: center; gap: 0.5rem; }
.bot-header button, .bot-header select {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  color: var(--fg);
  font-family: inherit;
}

.skill-config-item { border: 1px solid var(--border); border-radius: 6px; padding: 0.5rem 0.6rem; margin-bottom: 0.5rem; }
.skill-config-item .skill-config-name { font-weight: 600; font-size: 0.9rem; }
.skill-config-item .skill-config-desc { font-size: 0.8rem; color: var(--muted); margin: 0.15rem 0 0.5rem; }
.skill-config-fields { display: flex; flex-direction: column; gap: 0.4rem; }
.skill-config-fields input {
  padding: 0.4rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--fg);
}
.skill-config-item button { margin-top: 0.4rem; padding: 0.3rem 0.7rem; border-radius: 6px; border: none; background: var(--accent); color: white; cursor: pointer; font-size: 0.85rem; }

.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.6rem; width: 100%; max-width: 720px; margin: 0 auto; }
.msg { max-width: 70%; padding: 0.5rem 0.75rem; border-radius: 10px; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--accent); color: white; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); }
.msg.error { align-self: center; color: var(--danger); font-size: 0.85rem; }

.message-form { display: flex; gap: 0.5rem; padding: 0.75rem; border-top: 1px solid var(--border); width: 100%; max-width: 720px; margin: 0 auto; }
.message-form input { flex: 1; padding: 0.6rem; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--fg); }
.message-form button { padding: 0.6rem 1.2rem; border-radius: 6px; border: none; background: var(--accent); color: white; cursor: pointer; }

.settings-view { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.9rem; max-width: 560px; }
.settings-view label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.9rem; color: var(--muted); }
.settings-view input, .settings-view select, .settings-view textarea {
  padding: 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
}
.settings-view fieldset { border: 1px solid var(--border); border-radius: 6px; }
.skills-list label { flex-direction: row; align-items: center; gap: 0.5rem; }
.settings-actions { display: flex; justify-content: space-between; }
.settings-actions button { padding: 0.5rem 1rem; border-radius: 6px; cursor: pointer; }
#save-settings-btn { background: var(--accent); color: white; border: none; }
.danger { background: none; border: 1px solid var(--danger); color: var(--danger); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modal-content {
  background: var(--bg);
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
  width: 520px;
  max-height: 80vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}
.modal-header { display: flex; justify-content: space-between; align-items: center; }
.modal-header button { background: none; border: none; font-size: 1.3rem; cursor: pointer; color: var(--muted); }
.hint { font-size: 0.85rem; color: var(--muted); margin: 0.25rem 0 0.75rem; }
.models-list { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 0.75rem; }
.model-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
}
.model-item-meta { color: var(--muted); font-size: 0.8rem; }
.model-item button { background: none; border: none; color: var(--danger); cursor: pointer; }
.new-model-form { display: flex; flex-direction: column; gap: 0.5rem; }
.new-model-form input, .new-model-form select, .new-model-form button {
  padding: 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--fg);
}
.new-model-form button { background: var(--accent); color: white; border: none; cursor: pointer; }
