/* ── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --bg-card: #1c2128;
  --border: #30363d;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --accent: #58a6ff;
  --accent-dim: #1f6feb;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --blue: #58a6ff;
  --purple: #bc8cff;
  --orange: #d18616;
  --font-mono: 'Consolas', 'Monaco', 'Courier New', monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  --radius: 6px;
  --sidebar-width: 160px;
  --header-height: 48px;
}

html, body {
  height: 100%;
  font-family: var(--font-sans);
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg-primary);
  line-height: 1.5;
}

/* ── Login Screen ────────────────────────────────────────── */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg-primary);
}

.login-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 340px;
  text-align: center;
}

.login-card h1 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
  color: var(--accent);
}

.login-card p {
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
  font-size: 0.875rem;
}

.login-card input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-card button {
  width: 100%;
  padding: 0.5rem;
  background: var(--accent-dim);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
}

.login-card button:hover { background: var(--accent); }

.login-error {
  margin-top: 1rem;
  color: var(--red);
  font-size: 0.8rem;
}

/* ── Dashboard Layout ────────────────────────────────────── */
.dashboard {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-height);
  padding: 0 1rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

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

.header h1 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--accent);
}

.header-mission {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  background: var(--red);
}

.status-dot.connected { background: var(--green); }

#header-status-text {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 0.5rem;
  gap: 2px;
  flex-shrink: 0;
}

.nav-btn {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: transparent;
  border: none;
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.875rem;
  text-align: left;
  cursor: pointer;
}

.nav-btn:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.nav-btn.active { background: var(--bg-tertiary); color: var(--accent); font-weight: 500; }

/* ── Main Content ────────────────────────────────────────── */
.main {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
}

.panel { animation: fadeIn 0.15s ease-in; }
.panel h2 { font-size: 1.1rem; margin-bottom: 1rem; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ── Status Panel ────────────────────────────────────────── */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.status-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.status-card label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.25rem;
}

.status-value {
  font-size: 1.25rem;
  font-weight: 600;
  font-family: var(--font-mono);
}

/* ── Toolbar ─────────────────────────────────────────────── */
.panel-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.panel-toolbar h2 { margin-bottom: 0; }

.toolbar-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.toolbar-controls select,
.toolbar-controls input[type="text"],
.toolbar-controls input[type="number"] {
  padding: 0.35rem 0.5rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.8rem;
}

.toolbar-controls select:focus,
.toolbar-controls input:focus {
  outline: none;
  border-color: var(--accent);
}

.checkbox-label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
}

/* ── Log/Event Container ─────────────────────────────────── */
.log-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-y: auto;
  max-height: calc(100vh - 200px);
  font-family: var(--font-mono);
  font-size: 0.8rem;
}

.log-entry {
  display: flex;
  gap: 0.5rem;
  padding: 0.3rem 0.75rem;
  border-bottom: 1px solid var(--border);
  align-items: baseline;
}

.log-entry:last-child { border-bottom: none; }
.log-entry:hover { background: var(--bg-tertiary); }

.log-ts {
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.log-level {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  text-transform: uppercase;
  flex-shrink: 0;
  min-width: 52px;
  text-align: center;
}

.log-level.DEBUG { background: var(--bg-tertiary); color: var(--text-muted); }
.log-level.INFO { background: #0d419d33; color: var(--blue); }
.log-level.WARNING { background: #9e6a0033; color: var(--yellow); }
.log-level.ERROR { background: #8b131733; color: var(--red); }

.log-msg {
  color: var(--text-primary);
  word-break: break-word;
  flex: 1;
}

/* ── Event entries ───────────────────────────────────────── */
.event-name {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 3px;
  background: var(--bg-tertiary);
  color: var(--purple);
  flex-shrink: 0;
}

/* ── Expandable JSON ─────────────────────────────────────── */
.expandable {
  cursor: pointer;
  user-select: none;
}

.expandable::before {
  content: '\25b6';
  display: inline-block;
  margin-right: 0.35rem;
  font-size: 0.65rem;
  transition: transform 0.15s;
}

.expandable.expanded::before { transform: rotate(90deg); }

.json-block {
  display: none;
  padding: 0.5rem 0.75rem;
  background: var(--bg-primary);
  border-radius: var(--radius);
  margin: 0.35rem 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.75rem;
  line-height: 1.6;
}

.json-block.visible { display: block; }

/* JSON syntax colors */
.json-key { color: var(--blue); }
.json-string { color: var(--green); }
.json-number { color: var(--orange); }
.json-bool { color: var(--purple); }
.json-null { color: var(--text-muted); }

/* ── Command Console ─────────────────────────────────────── */
.command-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 1rem;
  min-height: 400px;
}

.command-selector select {
  width: 100%;
  padding: 0.35rem 0.5rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

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

.cmd-item {
  padding: 0.4rem 0.6rem;
  background: transparent;
  border: none;
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.8rem;
  text-align: left;
  cursor: pointer;
  font-family: var(--font-mono);
}

.cmd-item:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.cmd-item.active { background: var(--bg-tertiary); color: var(--accent); }

.command-form-area {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.cmd-description {
  color: var(--text-secondary);
  font-size: 0.8rem;
  margin-bottom: 1rem;
}

.form-group {
  margin-bottom: 0.75rem;
}

.form-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 0.25rem;
}

.form-group label .param-type {
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.7rem;
}

.form-group label .param-required {
  color: var(--red);
  margin-left: 0.2rem;
}

.param-enum {
  color: var(--text-muted);
  font-size: 0.75em;
}

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 0.4rem 0.5rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.8rem;
  font-family: var(--font-mono);
}

.form-group textarea {
  min-height: 60px;
  resize: vertical;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.btn-primary {
  padding: 0.5rem 1.5rem;
  background: var(--accent-dim);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
}

.btn-primary:hover { background: var(--accent); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-small {
  padding: 0.25rem 0.75rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.8rem;
}

.btn-small:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.cmd-result {
  margin-top: 1rem;
  padding: 0.75rem;
  background: var(--bg-primary);
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 0.8rem;
}

.cmd-result.success { border-left: 3px solid var(--green); }
.cmd-result.error { border-left: 3px solid var(--red); }

/* ── Results entries ─────────────────────────────────────── */
.result-status {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 3px;
  flex-shrink: 0;
}

.result-status.ok { background: #23863533; color: var(--green); }
.result-status.error { background: #8b131733; color: var(--red); }

.result-cmd-id {
  color: var(--text-muted);
  font-size: 0.75rem;
  flex-shrink: 0;
  min-width: 40px;
}

/* ── Modules Panel ───────────────────────────────────────── */
.module-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.75rem;
}

.module-header {
  padding: 0.75rem 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  user-select: none;
}

.module-header:hover { background: var(--bg-tertiary); }

.module-header .arrow {
  font-size: 0.65rem;
  transition: transform 0.15s;
  color: var(--text-muted);
}

.module-header.expanded .arrow { transform: rotate(90deg); }

.module-name {
  font-weight: 600;
  font-family: var(--font-mono);
}

.module-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.module-tag {
  font-size: 0.65rem;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.module-body {
  display: none;
  padding: 0 1rem 1rem;
}

.module-body.visible { display: block; }

.module-section-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0.75rem 0 0.5rem;
}

.module-item {
  padding: 0.5rem 0.75rem;
  background: var(--bg-primary);
  border-radius: var(--radius);
  margin-bottom: 0.35rem;
}

.module-item-name {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--accent);
  font-size: 0.85rem;
}

.module-item-desc {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 0.15rem;
}

.module-item-params {
  margin-top: 0.35rem;
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
}

/* ── Instance Cards ───────────────────────────────────────── */
.instances-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 0.75rem;
}

.instance-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
}

.instance-card-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

.instance-id {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--accent);
}

.instance-module {
  font-size: 0.7rem;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.instance-state {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
}

.instance-state.in_progress { background: #0d419d33; color: var(--blue); }
.instance-state.completed { background: #23863533; color: var(--green); }
.instance-state.error { background: #8b131733; color: var(--red); }

.instance-created {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-left: auto;
}

.instance-data {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.6;
  margin-bottom: 0.5rem;
}

.instance-data-row {
  display: flex;
  gap: 0.5rem;
}

.instance-data-key {
  color: var(--text-muted);
  flex-shrink: 0;
}

.instance-data-val {
  color: var(--text-primary);
  word-break: break-word;
}

.instance-actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.instance-action-btn {
  padding: 0.25rem 0.6rem;
  font-size: 0.7rem;
  font-family: var(--font-mono);
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
}

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

.instances-empty {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
}

/* ── Toast Notifications ─────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 1000;
}

.toast {
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-size: 0.8rem;
  animation: slideIn 0.2s ease-out;
  max-width: 360px;
}

.toast.info { background: var(--accent-dim); color: #fff; }
.toast.error { background: #8b1317; color: #fff; }
.toast.success { background: #23863588; color: var(--green); border: 1px solid var(--green); }

@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ── Utility ─────────────────────────────────────────────── */
.muted { color: var(--text-muted); font-size: 0.85rem; }
[hidden] { display: none !important; }

/* ── Mobile (<768px) ─────────────────────────────────────── */
@media (max-width: 768px) {
  .layout { flex-direction: column-reverse; }

  .sidebar {
    width: 100%;
    flex-direction: row;
    border-right: none;
    border-top: 1px solid var(--border);
    overflow-x: auto;
    padding: 0.35rem;
    gap: 2px;
  }

  .nav-btn {
    white-space: nowrap;
    font-size: 0.75rem;
    padding: 0.4rem 0.6rem;
  }

  .main { padding: 0.75rem; }

  .command-layout {
    grid-template-columns: 1fr;
  }

  .status-grid {
    grid-template-columns: 1fr 1fr;
  }

  .header h1 { font-size: 0.85rem; }
}

/* ── Agents / Providers panels ─────────────────────────── */
.nav-divider {
  height: 1px;
  background: var(--border);
  margin: 0.5rem 0.75rem;
}

.btn-small-primary {
  /* A slightly bolder variant for "new" buttons while staying sidebar-sized */
  background: var(--accent-dim);
  color: #fff;
  border-color: var(--accent-dim);
}
.btn-small-primary:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.agent-row {
  padding: 0.75rem 1rem;
  margin-bottom: 0.5rem;
}

.agent-row-main {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.agent-row-meta {
  flex: 1;
  min-width: 0;
}

.agent-row-title {
  font-weight: 500;
  color: var(--text-primary);
}

.agent-row-sub {
  font-size: 0.8rem;
  margin-top: 0.15rem;
}

.agent-row-actions {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  flex-shrink: 0;
}

/* State chips — one color per AgentManager state */
.chip {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  min-width: 70px;
  text-align: center;
}
.chip-stopped  { background: #30363d; color: var(--text-secondary); }
.chip-starting { background: #3b2f00; color: var(--yellow); }
.chip-running  { background: #0f3d1e; color: var(--green); }
.chip-stopping { background: #3b2f00; color: var(--yellow); }
.chip-crashed  { background: #3b0d0d; color: var(--red); }
.chip-disabled { background: #21262d; color: var(--text-muted); }

/* Drawers for editors and logs — slide in from the bottom of the panel */
.drawer {
  margin-top: 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 70vh;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
}

.drawer-header h3 {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
}

.drawer-header-actions {
  display: flex;
  gap: 0.3rem;
}

.drawer-body {
  padding: 0.75rem;
  overflow-y: auto;
}

pre.drawer-body {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text-secondary);
  background: var(--bg-primary);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 60vh;
}

/* POSITION param picker — kind <select> + adaptive input area side-by-side. */
.position-controls {
  display: flex;
  gap: 0.4rem;
  align-items: stretch;
}

.position-controls .position-kind {
  width: auto;
  flex: 0 0 auto;
  min-width: 8rem;
}

.position-input {
  display: flex;
  gap: 0.3rem;
  flex: 1 1 auto;
  align-items: stretch;
  min-width: 0;
}

.position-input input,
.position-input select {
  width: auto;
  flex: 1 1 0;
  min-width: 0;
}

.position-input .position-coord {
  text-align: right;
}

.position-refresh {
  flex: 0 0 auto;
  padding: 0 0.55rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
}
.position-refresh:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.position-static {
  display: flex;
  align-items: center;
  font-style: italic;
  font-size: 0.8rem;
  padding: 0 0.4rem;
}

.param-help {
  color: var(--text-muted);
  font-size: 0.72rem;
  margin-top: 0.3rem;
  font-family: var(--font-mono);
}
