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

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background-color: #0f172a;
  color: #e5e7eb;
}

/* ── Top bar ─────────────────────────────────────── */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid rgba(148, 163, 184, 0.5);
  background: radial-gradient(circle at top left, #1f2937, #020617);
}

.top-bar h1 {
  margin: 0;
  font-size: 1.25rem;
}

.subtitle {
  margin: 0.25rem 0 0;
  font-size: 0.875rem;
  color: #9ca3af;
}

.top-bar-right {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

.top-bar-right a {
  margin-left: 1rem;
  color: #fff;
  text-decoration: none;
  background: #2563eb;
  padding: 0.4rem 0.8rem;
  border-radius: 5px;
  white-space: nowrap;
}

.create-code-btn {
  background: linear-gradient(135deg, #7c3aed, #4f46e5) !important;
  font-weight: 600;
  letter-spacing: 0.01em;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.create-code-btn:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.toggle {
  display: inline-flex;
  gap: 0.35rem;
  align-items: center;
  font-size: 0.875rem;
  color: #d1d5db;
}

select {
  background: #020617;
  border-radius: 999px;
  border: 1px solid #4b5563;
  color: #e5e7eb;
  padding: 0.35rem 0.75rem;
  font-size: 0.875rem;
}

/* ── Layout ──────────────────────────────────────── */
.layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  gap: 1rem;
  padding: 1rem;
}

.panel {
  background: radial-gradient(circle at top left, rgba(30, 64, 175, 0.3), #020617);
  border-radius: 0.75rem;
  border: 1px solid rgba(148, 163, 184, 0.4);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  min-height: 300px;
}

.panel h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

/* ── Agents list ─────────────────────────────────── */
.agents-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
  max-height: calc(100vh - 200px);
}

.agent-card {
  border-radius: 0.6rem;
  padding: 0.6rem 0.7rem;
  cursor: pointer;
  border: 1px solid transparent;
  background: rgba(15, 23, 42, 0.85);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  transition: border-color 0.12s ease, background-color 0.12s ease, transform 0.08s ease;
}

.agent-card:hover {
  border-color: #6366f1;
  background: rgba(30, 64, 175, 0.3);
  transform: translateY(-1px);
}

.agent-card.active {
  border-color: #818cf8;
  box-shadow: 0 0 0 1px rgba(129, 140, 248, 0.7);
}

.agent-title-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.agent-key {
  font-size: 0.9rem;
  font-weight: 600;
}

.agent-task {
  font-size: 0.75rem;
  color: #9ca3af;
}

.agent-stats-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: #d1d5db;
}

/* ── Badges ──────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-size: 0.7rem;
  border: 1px solid rgba(148, 163, 184, 0.7);
  background: rgba(15, 23, 42, 0.9);
}

.badge-success {
  border-color: rgba(34, 197, 94, 0.8);
  color: #bbf7d0;
}

.badge-muted {
  color: #9ca3af;
}

/* ── Runs table (desktop) ────────────────────────── */
.runs-container {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.runs-table {
  width: 100%;
  min-width: 960px;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.runs-table thead {
  position: sticky;
  top: 0;
  background: rgba(15, 23, 42, 0.98);
  z-index: 1;
}

.runs-table th,
.runs-table td {
  padding: 0.34rem 0.42rem;
  text-align: left;
  border-bottom: 1px solid rgba(55, 65, 81, 0.7);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.runs-table tbody {
  display: block;
  max-height: calc(100vh - 210px);
  overflow-y: auto;
}

.runs-table thead,
.runs-table tbody tr {
  display: table;
  width: 100%;
  table-layout: auto;
}

.runs-table th:nth-child(1),
.runs-table td:nth-child(1) { width: 56px; }

.runs-table th:nth-child(2),
.runs-table td:nth-child(2) { width: 120px; }

.runs-table th:nth-child(3),
.runs-table td:nth-child(3) { width: 110px; }

.runs-table th:nth-child(4),
.runs-table td:nth-child(4) { width: 120px; }

.runs-table th:nth-child(5),
.runs-table td:nth-child(5) { width: 120px; }

.runs-table tr {
  cursor: pointer;
}

.runs-table tr:hover {
  background: rgba(30, 64, 175, 0.25);
}

/* ── Status pills ────────────────────────────────── */
.status-pill {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.03rem 0.28rem;
  font-size: 0.66rem;
}

.status-success {
  background: rgba(22, 163, 74, 0.2);
  color: #bbf7d0;
}

.status-error {
  background: rgba(220, 38, 38, 0.25);
  color: #fecaca;
}

/* ── Run cards (mobile) ──────────────────────────── */
.runs-cards {
  display: none; /* shown only on mobile */
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
  flex: 1;
}

.run-card {
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(55, 65, 81, 0.7);
  border-radius: 0.6rem;
  padding: 0.65rem 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  cursor: pointer;
  transition: border-color 0.12s;
}

.run-card:hover,
.run-card:active {
  border-color: #6366f1;
  background: rgba(30, 64, 175, 0.2);
}

.run-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.run-card-task {
  font-size: 0.85rem;
  font-weight: 600;
  color: #e5e7eb;
}

.run-card-stats {
  display: flex;
  gap: 1rem;
  font-size: 0.75rem;
  color: #9ca3af;
}

.run-card-time {
  font-size: 0.7rem;
  color: #6b7280;
}

/* ── Mobile back button ──────────────────────────── */
.mobile-back-btn {
  display: none; /* shown only on mobile */
  align-items: center;
  gap: 0.4rem;
  background: transparent;
  border: 1px solid rgba(148, 163, 184, 0.4);
  color: #e5e7eb;
  border-radius: 0.4rem;
  padding: 0.45rem 0.75rem;
  font-size: 0.875rem;
  cursor: pointer;
  margin-bottom: 0.75rem;
  width: fit-content;
}

.mobile-back-btn:hover {
  background: rgba(30, 64, 175, 0.25);
}

/* ── Misc ────────────────────────────────────────── */
.empty-state {
  margin-top: 1rem;
  font-size: 0.85rem;
  color: #9ca3af;
}

.hidden {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  z-index: 41;
  background: #020617;
  border-radius: 0.75rem;
  border: 1px solid rgba(148, 163, 184, 0.6);
  width: 420px;
  max-width: calc(100% - 2rem);
  max-height: calc(100% - 2rem);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid rgba(55, 65, 81, 0.8);
}

.modal-header h3 {
  margin: 0;
  font-size: 0.95rem;
}

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

.icon-button {
  border: none;
  background: transparent;
  color: #9ca3af;
  font-size: 1.2rem;
  cursor: pointer;
}

.icon-button:hover {
  color: #e5e7eb;
}

.detail-list {
  margin: 0;
}

.detail-list dt {
  font-size: 0.78rem;
  color: #9ca3af;
  margin-top: 0.45rem;
}

.detail-list dd {
  margin: 0.05rem 0 0;
  font-size: 0.82rem;
  word-break: break-word;
}

/* ── Responsive: tablet ──────────────────────────── */
@media (max-width: 900px) {
  .top-bar-right {
    width: 100%;
    justify-content: flex-start;
  }

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

  .panel {
    min-height: auto;
  }

  .agents-list {
    max-height: none;
  }
}

/* ── Responsive: mobile ──────────────────────────── */
@media (max-width: 640px) {
  .top-bar {
    flex-direction: column;
    align-items: flex-start;
    padding: 0.875rem 1rem;
    gap: 0.6rem;
  }

  .top-bar h1 {
    font-size: 1.1rem;
  }

  .top-bar-left,
  .top-bar-right {
    width: 100%;
  }

  .top-bar-right {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 0.5rem;
    align-items: center;
  }

  .top-bar-right a {
    margin-left: 0;
    text-align: center;
  }

  .toggle {
    grid-column: 1 / -1;
    font-size: 0.8rem;
  }

  select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
  }

  .layout {
    padding: 0.75rem;
    gap: 0.75rem;
  }

  /* Panel switching: show only one panel at a time */
  .panel-right {
    display: none;
  }

  .layout.agent-selected .panel-left {
    display: none;
  }

  .layout.agent-selected .panel-right {
    display: flex;
  }

  /* Show back button on mobile */
  .mobile-back-btn {
    display: flex;
  }

  /* Use cards, hide table */
  .runs-table-wrapper {
    display: none;
  }

  .runs-cards {
    display: flex;
  }

  .panel-header select {
    font-size: 0.8rem;
    padding: 0.4rem 0.6rem;
  }

  /* Bigger tap targets for agent cards */
  .agent-card {
    padding: 0.75rem 0.85rem;
  }

  .agent-key {
    font-size: 0.95rem;
  }
}
