@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
  --bg: #0f1117;
  --surface: #181c25;
  --panel: #1e2330;
  --border: #2a3045;
  --accent: #4f8ef7;
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --muted: #5a6480;
  --text: #e2e8f0;
  --dim: #8896b3;

  --topbar-height: 56px;
  --sidebar-width: 248px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  height: 100%;
}

button, input, select {
  font-family: inherit;
}

.mono {
  font-family: 'JetBrains Mono', monospace;
}

/* ---------------- Buttons ---------------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  min-height: 38px;
  transition: background 0.15s, opacity 0.15s;
}
.btn:hover { background: var(--border); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

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

.btn-secondary {
  background: var(--panel);
  border-color: var(--border);
}

.btn-refresh {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  box-shadow: 0 0 0 1px rgba(79,142,247,0.25), 0 4px 14px rgba(79,142,247,0.25);
}
.btn-refresh:hover {
  box-shadow: 0 0 0 1px rgba(79,142,247,0.35), 0 6px 18px rgba(79,142,247,0.35);
}

.refresh-icon {
  flex-shrink: 0;
  transition: transform 0.5s ease;
}

.btn-refresh.spinning .refresh-icon {
  animation: refresh-spin 0.6s linear;
}

@keyframes refresh-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--dim);
}
.btn-ghost:hover { background: var(--panel); color: var(--text); }

.btn-full { width: 100%; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--dim);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
}
.icon-btn:hover { background: var(--panel); color: var(--text); }

/* ---------------- Login view ---------------- */
.login-view[hidden] {
  display: none;
}

.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 0%, #161b27 0%, var(--bg) 60%);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 36px 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}

.login-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.logo-icon {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.login-wordmark {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

.login-sub {
  color: var(--dim);
  font-size: 13px;
  margin: 0 0 24px 0;
}

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--dim);
  margin-bottom: 6px;
  margin-top: 14px;
}

.field-label:first-of-type { margin-top: 0; }

input.filter-input,
input[type="text"],
input[type="password"],
input[type="date"],
select.filter-input,
select {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13px;
  min-height: 38px;
  outline: none;
}
input.filter-input:focus,
input[type="text"]:focus,
input[type="password"]:focus,
input[type="date"]:focus,
select:focus {
  border-color: var(--accent);
}

.api-key-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.api-key-wrap input { padding-right: 40px; }
.api-key-wrap .icon-btn {
  position: absolute;
  right: 2px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0 20px;
  font-size: 13px;
  color: var(--dim);
  cursor: pointer;
}
.checkbox-row input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

.login-error {
  color: var(--danger);
  font-size: 12.5px;
  margin-top: 12px;
  margin-bottom: 0;
}

/* ---------------- Topbar ---------------- */
.topbar {
  height: var(--topbar-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
  gap: 8px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.topbar-wordmark {
  font-size: 15px;
  font-weight: 600;
}

.sidebar-toggle-btn { display: none; }

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.last-updated {
  font-size: 12.5px;
  color: var(--dim);
  white-space: nowrap;
}

/* ---------------- App body / sidebar ---------------- */
.app-body {
  display: flex;
  align-items: flex-start;
  height: calc(100vh - var(--topbar-height));
}

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  height: 100%;
  overflow-y: auto;
  padding: 16px;
}

.sidebar-section-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 12px;
}

.date-range-group {
  display: flex;
  flex-direction: column;
}

.sidebar-actions {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------------- Main content ---------------- */
.main-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 16px;
  overflow: hidden;
}

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}

.kpi-label {
  font-size: 16px;
  font-weight: 600;
  color: var(--dim);
  margin-bottom: 6px;
}

.kpi-value {
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
}

.kpi-subtitle {
  font-size: 11.5px;
  color: var(--dim);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.kpi-value.success { color: var(--success); }
.kpi-value.warn { color: var(--warn); }
.kpi-value.danger { color: var(--danger); }

.kpi-dual {
  display: flex;
  align-items: center;
  gap: 14px;
}

.kpi-dual-item {
  display: flex;
  flex-direction: column;
}

.kpi-dual-caption {
  font-size: 11.5px;
  color: var(--dim);
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.kpi-dual-divider {
  width: 1px;
  height: 28px;
  background: var(--border);
  flex-shrink: 0;
}

/* ---------------- Table ---------------- */
.table-section {
  flex: 1;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.table-scroll {
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.runs-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  min-width: 1100px;
}

.runs-table.summary-mode th:nth-child(1),
.runs-table.summary-mode td:nth-child(1),
.runs-table.summary-mode th:nth-child(3),
.runs-table.summary-mode td:nth-child(3),
.runs-table.summary-mode th:nth-child(11),
.runs-table.summary-mode td:nth-child(11) {
  display: none;
}

.runs-table thead th {
  position: sticky;
  top: 0;
  background: var(--panel);
  text-align: left;
  padding: 10px 12px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  z-index: 5;
}

.runs-table thead th[data-sort-key] {
  cursor: pointer;
  user-select: none;
}

.runs-table thead th[data-sort-key]:hover {
  color: var(--text);
}

.runs-table thead th.sorted {
  color: var(--accent);
}

.sort-indicator {
  display: inline-block;
  margin-left: 4px;
  font-size: 12px;
  opacity: 0.8;
  color: var(--dim);
}

.runs-table thead th[data-sort-key]:hover .sort-indicator {
  opacity: 1;
  color: var(--text);
}

.runs-table thead th.sorted .sort-indicator {
  opacity: 1;
  color: var(--accent);
}

.runs-table tbody td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
}

.runs-table tbody tr:hover {
  background: rgba(255,255,255,0.02);
}

.cell-sub {
  font-size: 11.5px;
  color: var(--dim);
  margin-top: 2px;
}

.cell-mono {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12.5px;
}

.cell-run-id {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12.5px;
  color: var(--accent);
  max-width: 200px;
}

.run-id-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 200px;
}

.run-id-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.run-id-copy-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 5px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s, color 0.12s, background 0.12s;
}

.runs-table tbody tr:hover .run-id-copy-btn {
  opacity: 1;
}

.run-id-copy-btn:hover {
  color: var(--text);
  background: var(--panel);
}

.run-id-copy-btn.copied {
  opacity: 1;
  color: var(--success);
}

/* Status pill */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 100px;
  font-size: 12px;
  font-weight: 500;
  text-transform: capitalize;
}
.status-pill .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}
.status-success { background: rgba(34,197,94,0.12); color: var(--success); }
.status-success .dot { background: var(--success); }
.status-failed { background: rgba(239,68,68,0.12); color: var(--danger); }
.status-failed .dot { background: var(--danger); }
.status-running { background: rgba(79,142,247,0.12); color: var(--accent); }
.status-running .dot { background: var(--accent); }
.status-pending { background: rgba(245,158,11,0.12); color: var(--warn); }
.status-pending .dot { background: var(--warn); }

/* Job type pill */
.job-pill {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 6px;
  font-size: 11.5px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--dim);
  text-transform: capitalize;
}

/* Items progress bar */
.items-cell {
  min-width: 130px;
}
.items-bar-track {
  width: 100%;
  height: 6px;
  background: var(--panel);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 4px;
}
.items-bar-fill {
  height: 100%;
  background: var(--success);
  border-radius: 4px;
}
.items-bar-fill.low { background: var(--danger); }
.items-text {
  font-size: 11.5px;
  color: var(--dim);
}

/* ---------------- Skeleton loading ---------------- */
.skeleton-cell {
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--panel) 0%, var(--border) 50%, var(--panel) 100%);
  background-size: 200% 100%;
  animation: skeleton-pulse 1.4s ease-in-out infinite;
}
@keyframes skeleton-pulse {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---------------- Empty state ---------------- */
.empty-state[hidden] {
  display: none;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--dim);
  text-align: center;
}
.empty-state svg { margin-bottom: 12px; opacity: 0.6; }
.empty-title { font-size: 14px; font-weight: 500; color: var(--text); margin-bottom: 4px; }
.empty-sub { font-size: 12.5px; color: var(--dim); }

/* ---------------- Pagination ---------------- */
.pagination-footer {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 10px 10px;
  flex-wrap: wrap;
  font-size: 12.5px;
  color: var(--dim);
}

.pagination-left {
  display: flex;
  align-items: center;
  gap: 8px;
}
.pagination-left select {
  width: auto;
  min-height: 32px;
  padding: 4px 8px;
}

.pagination-info {
  white-space: nowrap;
}

.pagination-pages {
  display: flex;
  align-items: center;
  gap: 4px;
}

.page-btn {
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 12.5px;
  cursor: pointer;
}
.page-btn:hover { background: var(--border); }
.page-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.page-ellipsis {
  color: var(--muted);
  padding: 0 4px;
}

/* ---------------- Toast ---------------- */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--panel);
  border: 1px solid var(--danger);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  max-width: 320px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  z-index: 100;
  animation: toast-in 0.2s ease-out;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------------- Sidebar overlay (mobile) ---------------- */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 15;
}

/* ---------------- Responsive ---------------- */
@media (max-width: 900px) {
  .sidebar-toggle-btn { display: inline-flex; }

  .app-body {
    position: relative;
  }

  .sidebar {
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    bottom: 0;
    z-index: 25;
    transform: translateX(-100%);
    transition: transform 0.2s ease-out;
    box-shadow: 4px 0 20px rgba(0,0,0,0.3);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .kpi-row {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .kpi-row {
    grid-template-columns: 1fr;
  }

  .topbar-wordmark { display: none; }

  .pagination-footer {
    flex-direction: column;
    align-items: stretch;
  }

  .pagination-pages {
    justify-content: center;
  }

  .login-card {
    padding: 28px 20px;
  }
}
