:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #f2f4f7;
  --border: #e3e6eb;
  --border-strong: #cfd4dc;
  --text: #16191f;
  --text-2: #4b5563;
  --text-3: #5f6b7a;
  --accent: #2557d6;
  --accent-hover: #1e48b5;
  --accent-soft: #e8eefc;
  --ok: #157f3b;
  --ok-soft: #e4f4ea;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --bad: #c4291c;
  --bad-soft: #fdeceb;
  --focus: 0 0 0 3px rgba(37, 87, 214, 0.28);
  --shadow-pop: 0 12px 32px -8px rgba(16, 24, 40, 0.18), 0 2px 6px rgba(16, 24, 40, 0.06);
  --on-accent: #ffffff;
  --radius: 8px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #151a21;
    --surface-2: #1b2129;
    --border: #262d37;
    --border-strong: #353e4a;
    --text: #e8ebf0;
    --text-2: #b3bcc8;
    --text-3: #8c97a5;
    --accent: #5b8cff;
    --accent-hover: #7aa2ff;
    --accent-soft: #1b2a4a;
    --ok: #4cc47a;
    --ok-soft: #13301f;
    --warn: #e0a440;
    --warn-soft: #33270f;
    --bad: #ff6b5e;
    --bad-soft: #3a1714;
    --focus: 0 0 0 3px rgba(91, 140, 255, 0.35);
    --shadow-pop: 0 16px 40px -8px rgba(0, 0, 0, 0.6);
    --on-accent: #0e1116; /* white on the lighter dark-mode blue/red is < 4.5:1 */
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  font-feature-settings: "tnum" 1;
}
[hidden] { display: none !important; }
code, pre { font-family: var(--mono); font-size: 12.5px; }
h1, h2, h3 { margin: 0; line-height: 1.25; letter-spacing: -0.01em; text-wrap: balance; }
h1 { font-size: 20px; font-weight: 600; }
h2 { font-size: 16px; font-weight: 600; }
.muted { color: var(--text-3); margin: 0; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font: 500 13.5px/1 var(--font); cursor: pointer; white-space: nowrap;
  transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible, .row-link:focus-visible, pre:focus-visible, summary:focus-visible {
  outline: none; box-shadow: var(--focus);
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { color: var(--bad); }
.btn-danger:hover { background: var(--bad-soft); border-color: var(--bad); }
.btn-icon { width: 34px; padding: 0; }
.btn-block { width: 100%; }
.btn[aria-busy="true"] { color: transparent; position: relative; }
.btn[aria-busy="true"]::after {
  content: ""; position: absolute; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid currentColor; border-color: var(--on-accent) var(--on-accent) transparent transparent;
  animation: spin .7s linear infinite;
}
.btn:not(.btn-primary)[aria-busy="true"]::after { border-color: var(--text-2) var(--text-2) transparent transparent; }
@keyframes spin { to { transform: rotate(360deg); } }

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-weight: 500; font-size: 13px; color: var(--text-2); }
input {
  height: 36px; width: 100%; padding: 0 11px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font: 14px var(--font);
}
input::placeholder { color: var(--text-3); }
.field input:user-invalid { border-color: var(--bad); }
.hint { color: var(--text-3); font-size: 12px; }
.form-error { margin: -2px 0 14px; color: var(--bad); font-size: 13px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 32px 28px 28px; box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
}
.login-card h1 { font-size: 17px; margin: 22px 0 20px; }

.brand { display: inline-flex; align-items: center; gap: 9px; font-weight: 650; font-size: 15px; }
.brand-mark {
  display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 7px;
  background: var(--text); color: var(--bg); font-size: 11px; font-weight: 700; letter-spacing: .02em;
}

/* ---------- shell ---------- */
.topbar { position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner {
  max-width: 1240px; margin: 0 auto; height: 56px; padding: 0 24px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.topbar-right { display: flex; align-items: center; gap: 12px; min-width: 0; }
.api-base { color: var(--text-3); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.api-base code { color: var(--text-2); }

.page { max-width: 1240px; margin: 0 auto; padding: 32px 24px 64px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; }

.totals {
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
  margin: 0 0 24px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.totals > div { padding: 14px 18px; border-left: 1px solid var(--border); }
.totals > div:first-child { border-left: 0; }
.totals dt { color: var(--text-3); font-size: 12.5px; }
.totals dd { margin: 2px 0 0; font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.panel-toolbar { display: flex; gap: 8px; align-items: center; padding: 12px; border-bottom: 1px solid var(--border); }
.search { max-width: 320px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left; font-weight: 500; font-size: 12.5px; color: var(--text-3);
  padding: 10px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border); white-space: nowrap;
}
.table td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; }
.table tbody tr.clickable { cursor: pointer; transition: background-color .12s ease; }
.table tbody tr.clickable:hover, .table tbody tr.selected { background: var(--surface-2); }
.table-compact td, .table-compact th { padding: 9px 12px; }
.row-link { color: var(--text); font-weight: 550; text-decoration: none; border-radius: 4px; }
.row-sub { display: block; color: var(--text-3); font-size: 12.5px; font-family: var(--mono); }
.cell-key { font-family: var(--mono); font-size: 12.5px; color: var(--text-2); }
.cell-bad { color: var(--bad); font-weight: 550; }
.cell-file { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }

.badge {
  display: inline-flex; align-items: center; gap: 6px; height: 22px; padding: 0 8px;
  border-radius: 999px; font-size: 12px; font-weight: 550; background: var(--surface-2); color: var(--text-2);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-bad { background: var(--bad-soft); color: var(--bad); }
.badge-info { background: var(--accent-soft); color: var(--accent); }

.skeleton td::after {
  content: ""; display: block; height: 12px; border-radius: 4px; background: var(--surface-2);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .45; } }

.empty { padding: 48px 24px; text-align: center; }
.empty h2 { margin-bottom: 6px; }
.empty p { color: var(--text-3); max-width: 46ch; margin: 0 auto 18px; }
.empty-inline { padding: 14px 0; }

/* ---------- drawer ---------- */
.scrim { position: fixed; inset: 0; background: rgba(15, 20, 28, .32); z-index: 20; animation: fade .2s ease-out; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 30;
  width: min(640px, 100vw); display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow-pop);
  animation: slide .22s cubic-bezier(.16, 1, .3, 1);
}
@keyframes slide { from { transform: translateX(24px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 20px 24px 16px; border-bottom: 1px solid var(--border); }
.drawer-head .muted { font-family: var(--mono); font-size: 12.5px; margin-top: 2px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 20px 24px 32px; }
.drawer-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 28px; }
.section-title { font-size: 14px; font-weight: 600; margin: 0 0 10px; }
.drawer .table-wrap { border: 1px solid var(--border); border-radius: var(--radius); }

.kv { display: grid; grid-template-columns: 140px 1fr; gap: 8px 16px; margin: 0 0 16px; }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

.job-detail { margin-top: 24px; }
.job-detail-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.job-detail-head .section-title { margin: 0; }
.job-error { color: var(--bad); background: var(--bad-soft); border-radius: var(--radius); padding: 10px 12px; margin: 0 0 10px; overflow-wrap: anywhere; }

.code {
  margin: 0; padding: 14px 16px; max-height: 420px; overflow: auto;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); line-height: 1.55; white-space: pre;
}

/* ---------- dialog ---------- */
.dialog {
  width: min(520px, calc(100vw - 32px)); padding: 24px; border: 1px solid var(--border);
  border-radius: 12px; background: var(--surface); color: var(--text); box-shadow: var(--shadow-pop);
}
.dialog[open] { animation: pop .2s cubic-bezier(.16, 1, .3, 1); }
@keyframes pop { from { transform: translateY(6px) scale(.985); opacity: 0; } }
.dialog::backdrop { background: rgba(15, 20, 28, .4); }
.dialog h2 { margin-bottom: 4px; }
.dialog .muted { margin-bottom: 20px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

.notice {
  margin: 12px 0 18px; padding: 10px 12px; border-radius: var(--radius);
  background: var(--warn-soft); color: var(--warn); font-size: 13px;
}
.notice strong { font-weight: 650; }
.creds { margin: 0; border: 1px solid var(--border); border-radius: var(--radius); }
.creds > div { display: grid; grid-template-columns: 96px 1fr auto; align-items: center; gap: 12px; padding: 8px 8px 8px 14px; border-top: 1px solid var(--border); }
.creds > div:first-child { border-top: 0; }
.creds dt { color: var(--text-3); font-size: 13px; }
.creds dd { margin: 0; font-family: var(--mono); font-size: 13px; overflow-wrap: anywhere; min-width: 0; }
.creds .btn { height: 28px; padding: 0 10px; font-size: 12.5px; }
.handover-copy { margin-top: 14px; }
.quickstart { margin-top: 16px; }
.quickstart summary { cursor: pointer; color: var(--text-2); font-weight: 500; border-radius: 4px; }
.quickstart .code { margin-top: 10px; max-height: 260px; font-size: 12px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  padding: 10px 16px; border-radius: var(--radius); background: var(--text); color: var(--bg);
  font-weight: 500; box-shadow: var(--shadow-pop); animation: pop .18s ease-out;
}
.toast.toast-bad { background: var(--bad); color: var(--on-accent); }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .totals { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .totals > div:nth-child(4) { border-left: 0; }
  .totals > div:nth-child(n + 4) { border-top: 1px solid var(--border); }
  .totals > div:nth-child(5) { grid-column: span 2; }
  .api-base { display: none; }
}
@media (max-width: 600px) {
  .topbar-inner { padding: 0 16px; }
  /* API key, Pages, Last job: all shown in the drawer; keeps Failed + Cost on screen */
  #vendor-table :is(th, td):is(:nth-child(3), :nth-child(5), :nth-child(8)) { display: none; }
  .table th, .table td { padding-left: 12px; padding-right: 12px; }
  .page { padding: 20px 16px 48px; }
  .totals { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .totals > div { border-left: 0; border-top: 1px solid var(--border); }
  .totals > div:nth-child(2n) { border-left: 1px solid var(--border); }
  .totals > div:nth-child(-n + 2) { border-top: 0; }
  .totals > div:last-child:nth-child(odd) { grid-column: 1 / -1; border-left: 0; }
  .search { max-width: none; }
  .drawer-head, .drawer-body { padding-left: 16px; padding-right: 16px; }
  .kv { grid-template-columns: 110px 1fr; }
  .creds > div { grid-template-columns: 1fr auto; }
  .creds dt { grid-column: 1 / -1; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
