/* ── Base ──────────────────────────────────────────────────────────────────── */
:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --border: #2a2d3a;
  --text: #e2e4ef;
  --muted: #6b7280;
  --accent: #7c6df0;
  --accent-h: #9d8fff;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --blue: #60a5fa;
  --radius: 8px;
  --font: 'Segoe UI', system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 14px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  min-height: 100vh;
}

/* ── Header ─────────────────────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.logo {
  font-size: 1.4rem;
}

.title {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

/* ── Badges ─────────────────────────────────────────────────────────────────── */
.badge {
  font-size: 0.72rem;
  padding: 0.2rem 0.6rem;
  border-radius: 12px;
  font-weight: 500;
}

.badge-online {
  background: #162a1e;
  color: var(--green);
}

.badge-offline {
  background: #2a1a1a;
  color: var(--red);
}

.status-badge {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
}

.status-pending {
  background: #1e1e2e;
  color: var(--blue);
}

.status-running {
  background: #1e2a1e;
  color: var(--green);
}

.status-done {
  background: #162a16;
  color: var(--green);
}

.status-failed {
  background: #2a1616;
  color: var(--red);
}

/* ── Layout ─────────────────────────────────────────────────────────────────── */
main {
  padding: 1.25rem 1.5rem;
  max-width: 1400px;
  margin: 0 auto;
}

.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-top: 1rem;
}

.col {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (max-width: 900px) {
  .two-col {
    grid-template-columns: 1fr;
  }
}

/* ── Cards ──────────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.card h2 {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* ── Stats row ──────────────────────────────────────────────────────────────── */
.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
  margin-top: 1rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  text-align: center;
  transition: border-color 0.15s;
}

.stat-card:hover {
  border-color: var(--accent);
}

.stat-value {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

[data-status="pending"] .stat-value {
  color: var(--blue);
}

[data-status="running"] .stat-value {
  color: var(--amber);
}

[data-status="done"] .stat-value {
  color: var(--green);
}

[data-status="failed"] .stat-value {
  color: var(--red);
}

/* ── Forms ──────────────────────────────────────────────────────────────────── */
select,
textarea,
input[type="text"],
input[type="file"] {
  background: #12141c;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.875rem;
  padding: 0.45rem 0.6rem;
  width: 100%;
  outline: none;
  transition: border-color 0.15s;
}

select:focus,
textarea:focus,
input[type="text"]:focus {
  border-color: var(--accent);
}

textarea {
  resize: vertical;
}

#task-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* ── Buttons ─────────────────────────────────────────────────────────────────── */
button {
  cursor: pointer;
  border: none;
  border-radius: 6px;
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.45rem 1rem;
  transition: background 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-h);
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-ghost:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-sm {
  padding: 0.2rem 0.5rem;
  font-size: 0.75rem;
}

.btn-danger {
  background: var(--red);
  color: #fff;
}

.feedback {
  font-size: 0.75rem;
  color: var(--green);
  min-height: 1rem;
  margin-top: 0.25rem;
}

.feedback.error {
  color: var(--red);
}

/* ── Chat ───────────────────────────────────────────────────────────────────── */
.chat-controls {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.chat-controls select {
  flex: 1;
}

#chat-messages {
  background: #12141c;
  border: 1px solid var(--border);
  border-radius: 6px;
  height: 240px;
  overflow-y: auto;
  padding: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.msg {
  display: flex;
  flex-direction: column;
}

.msg-role {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  margin-bottom: 0.15rem;
  color: var(--muted);
}

.msg-text {
  background: var(--surface);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  font-size: 0.82rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user .msg-role {
  color: var(--accent);
}

.msg.user .msg-text {
  background: #1e1b35;
}

.msg.agent .msg-role {
  color: var(--green);
}

.msg.stream .msg-text {
  opacity: 0.8;
}

.chat-input-row {
  display: flex;
  gap: 0.5rem;
}

.chat-input-row input {
  flex: 1;
}

/* ── RAG section ─────────────────────────────────────────────────────────────── */
#rag-sources-list {
  margin-bottom: 0.5rem;
}

.rag-source-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.3rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
}

.rag-source-row:last-child {
  border-bottom: none;
}

.rag-source-name {
  color: var(--text);
}

.rag-source-meta {
  color: var(--muted);
}

.upload-form {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  align-items: center;
}

.upload-form input[type=file] {
  flex: 1;
}

.rag-search-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.rag-search-row input {
  flex: 1;
}

#rag-results {
  font-size: 0.78rem;
  color: var(--muted);
  max-height: 200px;
  overflow-y: auto;
}

.rag-result {
  background: #12141c;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem;
  margin-bottom: 0.4rem;
}

.rag-result-source {
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 0.2rem;
}

.rag-result-score {
  float: right;
  color: var(--muted);
  font-size: 0.7rem;
}

.rag-result-content {
  white-space: pre-wrap;
  line-height: 1.4;
}

/* ── Activity Log ─────────────────────────────────────────────────────────────── */
#activity-log {
  height: 260px;
  overflow-y: auto;
  font-size: 0.75rem;
  color: var(--muted);
  font-family: 'Cascadia Mono', 'Fira Code', monospace;
}

.log-entry {
  padding: 0.25rem 0;
  border-bottom: 1px solid #1c1e2a;
  line-height: 1.5;
}

.log-ts {
  color: #3a3d55;
  margin-right: 0.4rem;
}

.log-icon {
  margin-right: 0.2rem;
}

.log-agent {
  color: #6c7293;
  font-weight: 600;
  margin-right: 0.3rem;
}

.log-hl {
  color: #a0a4c0;
}

.log-start {
  color: var(--blue);
}

.log-start .log-icon {
  color: var(--blue);
}

.log-done {
  color: var(--green);
}

.log-done .log-icon {
  color: var(--green);
}

.log-failed {
  color: var(--red);
}

.log-failed .log-icon {
  color: var(--red);
}

.log-badge {
  display: inline-block;
  font-size: 0.65rem;
  padding: 0.05rem 0.35rem;
  border-radius: 3px;
  margin-left: 0.25rem;
  vertical-align: middle;
}

.badge-dur {
  background: #1a2a1a;
  color: #5eff8a;
}

.badge-tool {
  background: #1a1a2e;
  color: #7eb8ff;
  cursor: help;
}

.badge-rag {
  background: #2a1a2a;
  color: #d49cff;
}

.badge-model {
  background: #1c1e2a;
  color: #6c7293;
}

.log-expand {
  cursor: pointer;
  color: #4a4d6a;
  margin-left: 0.3rem;
  user-select: none;
}

.log-expand:hover {
  color: var(--blue);
}

.log-detail {
  display: none;
  margin-top: 0.2rem;
  padding: 0.3rem 0.5rem;
  background: #0d0e14;
  border-radius: 4px;
  border-left: 2px solid #2a2d45;
}

.log-detail-full {
  display: none;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
}

.log-entry.expanded .log-detail {
  display: block;
}

.log-entry.expanded .log-detail-preview {
  display: none;
}

.log-entry.expanded .log-detail-full {
  display: block;
}

.log-detail-preview {
  white-space: pre-wrap;
  word-break: break-word;
  color: #6c7293;
}

/* ── Task Table ──────────────────────────────────────────────────────────────── */
.task-table-card {
  margin-top: 1rem;
}

.table-scroll {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

thead th {
  text-align: left;
  color: var(--muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 1px solid var(--border);
  padding: 0.4rem 0.6rem;
}

tbody td {
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid #1a1c28;
  vertical-align: middle;
}

tbody tr:hover td {
  background: #141620;
}

.col-id {
  font-family: monospace;
  color: var(--muted);
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-prompt {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-agent {
  color: var(--accent);
  font-weight: 500;
}

/* ── Agent Manager ───────────────────────────────────────────────────────────── */
.agent-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.agent-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.agent-chip {
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 0.78rem;
  cursor: pointer;
  transition: all 0.15s;
}

.agent-chip:hover {
  border-color: var(--accent);
  color: var(--accent-h);
}

.agent-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

#agent-form {
  display: none;
  flex-direction: column;
  gap: 0.6rem;
  margin-top: 0.5rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

#agent-form.open {
  display: flex;
}

#agent-form label {
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#agent-form input,
#agent-form select,
#agent-form textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.45rem 0.6rem;
  font-size: 0.82rem;
  font-family: var(--font);
}

#agent-form input:focus,
#agent-form select:focus,
#agent-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

#agent-form textarea {
  min-height: 120px;
  resize: vertical;
}

.agent-form-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.btn-sm {
  padding: 0.3rem 0.7rem;
  font-size: 0.75rem;
  border-radius: var(--radius);
  cursor: pointer;
  border: none;
  font-weight: 500;
  transition: opacity 0.15s;
}

.btn-sm:hover {
  opacity: 0.85;
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-danger {
  background: var(--red);
  color: #fff;
}

#agent-feedback {
  font-size: 0.75rem;
  min-height: 1.1em;
}

#agent-feedback.error {
  color: var(--red);
}

#agent-feedback:not(.error) {
  color: var(--green);
}

/* ── Sidebar ────────────────────────────────────────────────────────────────── */
.sidebar-toggle {
  position: fixed;
  top: .75rem;
  left: .75rem;
  z-index: 100;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1.3rem;
  padding: .25rem .5rem;
  border-radius: var(--radius);
  cursor: pointer;
  display: none;
}
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 220px;
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding-top: 1rem;
  transform: translateX(-100%);
  transition: transform .2s;
}
.sidebar.open { transform: translateX(0); }
.sidebar-header { padding: 0 1rem .75rem; }
.sidebar-header h2 { font-size: 1.1rem; color: var(--accent); }
.sidebar-nav { list-style: none; }
.sidebar-nav li a {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .6rem 1rem;
  color: var(--muted);
  text-decoration: none;
  font-size: .85rem;
  transition: background .15s, color .15s;
}
.sidebar-nav li a:hover { background: rgba(124,109,240,.1); color: var(--text); }
.sidebar-nav li a.active { color: var(--accent); background: rgba(124,109,240,.12); }
.sidebar-nav .icon { font-size: 1rem; }

@media (min-width: 768px) {
  .sidebar { transform: translateX(0); }
  .sidebar-toggle { display: none; }
  header { margin-left: 220px; }
  main { margin-left: 220px; }
}
@media (max-width: 767px) {
  .sidebar-toggle { display: block; }
}

/* ── Task Cards Grid ────────────────────────────────────────────────────────── */
.task-cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.task-card h2 { font-size: 1rem; margin-bottom: .5rem; }

.task-controls {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  margin-bottom: .5rem;
}
.task-field { display: flex; flex-direction: column; min-width: 120px; }
.task-field label { font-size: .7rem; color: var(--muted); margin-bottom: .15rem; }

.task-actions {
  display: flex;
  gap: .4rem;
  margin-bottom: .5rem;
}

.task-progress-bar {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin-bottom: .5rem;
  overflow: hidden;
}
.task-progress-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .3s;
  border-radius: 2px;
}

.task-log {
  max-height: 150px;
  overflow-y: auto;
  font-size: .72rem;
  font-family: monospace;
  background: #12141c;
  border-radius: var(--radius);
  padding: .4rem;
  margin-bottom: .4rem;
}
.task-log:empty { display: none; }
.task-log-line { padding: .1rem 0; border-bottom: 1px solid rgba(255,255,255,.03); }

.task-results { font-size: .72rem; }
.task-results:empty { display: none; }
.task-results-pre {
  background: #12141c;
  padding: .5rem;
  border-radius: var(--radius);
  overflow-x: auto;
  color: var(--green);
}

.placeholder-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  opacity: .5;
}
.placeholder-msg { color: var(--muted); font-size: .85rem; }