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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #0f1117;
  color: #e0e0e0;
  height: 100vh;
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

header {
  background: #1a1d27;
  padding: 12px 24px;
  border-bottom: 1px solid #2a2d3a;
  display: flex;
  align-items: center;
  gap: 24px;
}

header h1 {
  font-size: 18px;
  font-weight: 600;
  color: #f0f0f0;
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tab {
  background: transparent;
  color: #888;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}

.tab:hover { color: #ccc; background: #1e2130; }
.tab.active { color: #fff; background: #3b82f6; }

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

/* Sidebar */
.sidebar {
  width: 220px;
  min-width: 220px;
  background: #141620;
  border-right: 1px solid #2a2d3a;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #2a2d3a;
}

.sidebar-header h2 { font-size: 14px; font-weight: 600; }

.sidebar-header button {
  background: #2a2d3a;
  color: #ccc;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
}
.sidebar-header button:hover { background: #3a3d4a; }

.agent-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.agent-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.15s;
}

.agent-item:hover { background: #1e2130; }
.agent-item.active { background: #1e3a5f; }

.agent-item .agent-name {
  font-size: 13px;
  font-weight: 600;
  color: #e0e0e0;
}

.agent-item .agent-role {
  font-size: 11px;
  color: #888;
  margin-top: 2px;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.status-dot.idle { background: #4ade80; }
.status-dot.running { background: #facc15; }
.status-dot.unknown { background: #666; }

/* Content */
.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tab-panel { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
.tab-panel.hidden { display: none; }

/* Chat */
.chat-header {
  padding: 12px 20px;
  border-bottom: 1px solid #2a2d3a;
  display: flex;
  align-items: center;
  gap: 12px;
  background: #1a1d27;
}

.chat-header span:first-child {
  font-size: 15px;
  font-weight: 600;
}

.status-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
  font-family: monospace;
}

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

.msg {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: #1e3a5f;
  color: #e0e0e0;
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  align-self: flex-start;
  background: #1e2130;
  color: #e0e0e0;
  border-bottom-left-radius: 4px;
}

.msg.system {
  align-self: center;
  background: transparent;
  color: #666;
  font-size: 12px;
  font-style: italic;
}

.msg .msg-meta {
  font-size: 11px;
  color: #555;
  margin-top: 4px;
}

.loading {
  align-self: flex-start;
  padding: 10px 14px;
  background: #1e2130;
  border-radius: 12px;
  color: #888;
  font-size: 14px;
}

.loading::after {
  content: "";
  animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
  0%   { content: ""; }
  25%  { content: "."; }
  50%  { content: ".."; }
  75%  { content: "..."; }
}

.chat-form {
  padding: 12px 20px;
  border-top: 1px solid #2a2d3a;
  display: flex;
  gap: 8px;
  background: #1a1d27;
}

.chat-form textarea {
  flex: 1;
  background: #0f1117;
  color: #e0e0e0;
  border: 1px solid #3a3d4a;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  line-height: 1.4;
  min-height: 42px;
  max-height: 200px;
  overflow-y: auto;
}

.chat-form textarea:focus { outline: none; border-color: #3b82f6; }

.chat-form button {
  background: #3b82f6;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  align-self: flex-end;
}
.chat-form button:hover { background: #2563eb; }
.chat-form button:disabled { background: #2a2d3a; color: #666; cursor: not-allowed; }

/* Panel Headers */
.panel-header {
  padding: 12px 20px;
  border-bottom: 1px solid #2a2d3a;
  background: #1a1d27;
  display: flex;
  align-items: center;
  gap: 16px;
}

.panel-header h2 { font-size: 15px; font-weight: 600; }

.panel-header select {
  background: #0f1117;
  color: #e0e0e0;
  border: 1px solid #3a3d4a;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
}

.panel-header button {
  background: #2a2d3a;
  color: #ccc;
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
.panel-header button:hover { background: #3a3d4a; }

.filter-row { display: flex; gap: 8px; }

/* Item Lists */
.item-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px;
}

.item-card {
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 8px;
}

.item-card .item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.item-card .item-title {
  font-size: 14px;
  font-weight: 600;
}

.item-card .item-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
  background: #2a2d3a;
}

.item-card .item-badge.open { background: #1e3a5f; color: #60a5fa; }
.item-card .item-badge.in_progress { background: #3b2f0a; color: #facc15; }
.item-card .item-badge.done { background: #0a3b1a; color: #4ade80; }
.item-card .item-badge.cancelled { background: #3b0a0a; color: #f87171; }
.item-card .item-badge.incoming { background: #1e3a5f; color: #60a5fa; }
.item-card .item-badge.outgoing { background: #1a3a2a; color: #4ade80; }
.item-card.outgoing { border-left: 3px solid #4ade80; }
.item-card.incoming { border-left: 3px solid #60a5fa; }
.item-card .item-badge.unread { background: #1e3a5f; color: #60a5fa; }
.item-card .item-badge.read { background: #2a2d3a; color: #888; }
.item-card .item-badge.urgent { background: #7f1d1d; color: #fca5a5; }
.item-card .item-badge.high { background: #78350f; color: #fbbf24; }

.item-card .item-body {
  font-size: 13px;
  color: #aaa;
  line-height: 1.4;
  margin-top: 4px;
}

.item-card .item-meta {
  font-size: 11px;
  color: #666;
  margin-top: 6px;
  display: flex;
  gap: 12px;
}

/* Queue */
.queue-section {
  padding: 0 20px;
}

.queue-section h3 {
  font-size: 13px;
  font-weight: 600;
  color: #888;
  margin: 12px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.queue-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  border-radius: 6px;
  margin-bottom: 4px;
  font-size: 13px;
}

.queue-item .q-agent {
  font-weight: 600;
  min-width: 100px;
}

.queue-item .q-source {
  color: #888;
}

.queue-item .q-time {
  color: #666;
  margin-left: auto;
  font-size: 11px;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #666;
  font-size: 14px;
}

/* Live Feed */
.live-feed .live-entry {
  display: flex;
  gap: 10px;
  padding: 8px 12px;
  border-left: 3px solid #3a3d4a;
  margin-bottom: 4px;
  font-size: 13px;
  line-height: 1.4;
  background: #141620;
  border-radius: 0 6px 6px 0;
}
.live-entry.job_start { border-left-color: #3b82f6; }
.live-entry.job_done { border-left-color: #4ade80; }
.live-entry.job_failed, .live-entry.job_error, .live-entry.job_timeout { border-left-color: #f87171; }
.live-entry.message { border-left-color: #a78bfa; }
.live-entry.task_created { border-left-color: #facc15; }
.live-entry.task_updated { border-left-color: #fb923c; }
.live-entry.tool_call { border-left-color: #38bdf8; }

.live-entry .live-time {
  color: #666;
  font-family: monospace;
  font-size: 11px;
  min-width: 55px;
  flex-shrink: 0;
}
.live-entry .live-agent {
  font-weight: 600;
  min-width: 80px;
  flex-shrink: 0;
}
.live-entry .live-type {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 3px;
  background: #2a2d3a;
  color: #aaa;
  min-width: 70px;
  text-align: center;
  flex-shrink: 0;
}
.live-entry .live-detail {
  color: #bbb;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}
.live-entry .live-arrow {
  color: #666;
  flex-shrink: 0;
}

.auto-refresh-label {
  font-size: 12px;
  color: #888;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.auto-refresh-label input { accent-color: #3b82f6; }

/* Login */
.login-overlay {
  position: fixed;
  inset: 0;
  background: #0f1117;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.login-overlay.hidden { display: none; }

.login-box {
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  border-radius: 12px;
  padding: 40px;
  width: 360px;
  text-align: center;
}

.login-box h2 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-box p {
  color: #888;
  font-size: 13px;
  margin-bottom: 24px;
}

.login-box input {
  width: 100%;
  background: #0f1117;
  color: #e0e0e0;
  border: 1px solid #3a3d4a;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 12px;
}

.login-box input:focus { outline: none; border-color: #3b82f6; }

.login-box button {
  width: 100%;
  background: #3b82f6;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  margin-top: 4px;
}

.login-box button:hover { background: #2563eb; }

.login-error {
  color: #f87171;
  font-size: 13px;
  margin-top: 12px;
  display: none;
}

/* Agent Row + Toggle */
.agent-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.agent-info {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  flex-shrink: 0;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch .slider {
  position: absolute;
  inset: 0;
  background: #3a3d4a;
  border-radius: 20px;
  transition: background 0.2s;
}

.toggle-switch .slider::before {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  left: 3px;
  bottom: 3px;
  background: #888;
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}

.toggle-switch input:checked + .slider {
  background: #166534;
}

.toggle-switch input:checked + .slider::before {
  transform: translateX(16px);
  background: #4ade80;
}

.agent-item.disabled {
  opacity: 0.45;
}

.agent-item.disabled .agent-name,
.agent-item.disabled .agent-role,
.agent-item.disabled .status-dot {
  color: #666;
}

.agent-item.disabled .toggle-switch {
  opacity: 1;
}

/* Live Feed: Toggle + Blocked */
.live-entry.job_blocked { border-left-color: #f87171; }
.live-entry.agent_toggle { border-left-color: #a78bfa; }

/* Queue Item clickable */
.queue-item { cursor: pointer; transition: background 0.15s; }
.queue-item:hover { background: #1e2130; }

/* Job Detail Modal */
.job-detail-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.job-detail-overlay.hidden { display: none; }

.job-detail-modal {
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  border-radius: 12px;
  width: 90%;
  max-width: 800px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

.job-detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #2a2d3a;
  flex-shrink: 0;
}

.job-detail-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.job-detail-header button {
  background: none;
  border: none;
  color: #888;
  font-size: 24px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}
.job-detail-header button:hover { color: #e0e0e0; }

.job-detail-content {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.job-detail-section {
  margin-bottom: 20px;
}

.job-detail-section h3 {
  font-size: 12px;
  font-weight: 600;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.job-detail-meta {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
}

.job-detail-meta .meta-item {
  background: #141620;
  padding: 8px 12px;
  border-radius: 6px;
}

.job-detail-meta .meta-label {
  font-size: 11px;
  color: #666;
  margin-bottom: 2px;
}

.job-detail-meta .meta-value {
  font-size: 13px;
  color: #e0e0e0;
}

.job-detail-code {
  background: #0f1117;
  border: 1px solid #2a2d3a;
  border-radius: 6px;
  padding: 12px;
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 12px;
  line-height: 1.5;
  color: #ccc;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 300px;
  overflow-y: auto;
}

.job-detail-activity {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.job-detail-activity .live-entry {
  display: flex;
  gap: 10px;
  padding: 6px 10px;
  border-left: 3px solid #3a3d4a;
  font-size: 12px;
  line-height: 1.4;
  background: #141620;
  border-radius: 0 6px 6px 0;
}

.job-detail-badge {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
}
.job-detail-badge.completed { background: #0a3b1a; color: #4ade80; }
.job-detail-badge.running { background: #3b2f0a; color: #facc15; }
.job-detail-badge.queued { background: #1e3a5f; color: #60a5fa; }
.job-detail-badge.failed { background: #3b0a0a; color: #f87171; }

/* ── Metrics Tab ── */
.metrics-overview {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.metric-card {
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  border-radius: 8px;
  padding: 16px;
  text-align: center;
}
.metric-value {
  font-size: 28px;
  font-weight: 700;
  color: #60a5fa;
}
.metric-label {
  font-size: 12px;
  color: #888;
  margin-top: 4px;
}

.metrics-anomalies {
  margin-bottom: 16px;
}
.anomaly {
  padding: 8px 12px;
  border-radius: 6px;
  margin-bottom: 6px;
  font-size: 13px;
}
.anomaly-ok {
  background: #0a3b1a;
  color: #4ade80;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
}
.anomaly-warning {
  background: #3b2f0a;
  color: #facc15;
  border-left: 3px solid #facc15;
}
.anomaly-critical {
  background: #3b0a0a;
  color: #f87171;
  border-left: 3px solid #f87171;
}

.metrics-table-wrap {
  overflow-x: auto;
  margin-bottom: 20px;
}
.metrics-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.metrics-table th {
  background: #1a1d27;
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  color: #aaa;
  border-bottom: 2px solid #2a2d3a;
}
.metrics-table td {
  padding: 6px 10px;
  border-bottom: 1px solid #1e2130;
}
.metrics-table tr:hover { background: #1a1d27; }
.rate-good { color: #4ade80; }
.rate-warn { color: #facc15; }
.rate-bad { color: #f87171; }

.badge-enabled {
  background: #0a3b1a;
  color: #4ade80;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
}
.badge-disabled {
  background: #3b0a0a;
  color: #f87171;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
}

.metrics-section {
  margin-top: 16px;
}
.metrics-section h3 {
  color: #ccc;
  margin-bottom: 8px;
  font-size: 14px;
}

.feedback-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.feedback-item {
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
}

.muted { color: #666; font-size: 13px; }

@media (max-width: 768px) {
  .metrics-overview { grid-template-columns: repeat(2, 1fr); }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3a3d4a; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #4a4d5a; }
