/* ── theme tokens ───────────────────────────────────────────── */
:root[data-theme="light"] {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #1b1f24;
  --muted: #687078;
  --border: #e2e6ea;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}
:root[data-theme="dark"] {
  --bg: #0e1116;
  --panel: #161a21;
  --panel-2: #1d222b;
  --text: #e6e9ef;
  --muted: #939db0;
  --border: #272d38;
  --accent: #3b82f6;
  --accent-fg: #ffffff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 6px rgba(0, 0, 0, 0.35);
}
:root {
  --s-queued: #8b95a3;
  --s-rendering: #2563eb;
  --s-ocr: #d39e00;
  --s-done: #16a34a;
  --s-failed: #dc2626;
}

[x-cloak] { display: none !important; }
* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

h2, h3 { margin: 0; font-size: inherit; font-weight: 600; }
p { margin: 0; }

.mono { font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); border: 0;
}

button { font-family: inherit; }

/* ── top bar ────────────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; }
.brand-logo { height: 28px; width: auto; display: block; }
/* Swap the full wordmark for the compact "em" badge on phones. */
.brand-badge { display: none; }
@media (max-width: 480px) {
  .brand-wordmark { display: none; }
  .brand-badge { display: block; }
}
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; row-gap: 8px; }

.health {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted);
  padding: 4px 9px; border: 1px solid var(--border); border-radius: 999px;
}
.health .ver { display: inline-flex; align-items: baseline; gap: 4px; }
.health .ver-k { color: var(--muted); }
.health .ver .mono { color: var(--text); }
.health .ver + .ver::before { content: "·"; color: var(--muted); }

.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer; font-size: 15px; line-height: 1;
}
.icon-btn:hover { border-color: var(--accent); }

.apikey input {
  width: 150px; max-width: 150px; min-width: 0; padding: 7px 10px;
  font: inherit; font-size: 13px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
}
.apikey input:focus { outline: none; border-color: var(--accent); }
.apikey.bad input { border-color: var(--s-failed); }

/* A phone-width header can't fit the key field beside the version pills and
   buttons. Drop the controls to their own full-width row, hand that row's width
   to the key field, and hide the non-essential version pills. */
@media (max-width: 680px) {
  .topbar { flex-wrap: wrap; row-gap: 10px; }
  .topbar-actions { flex: 1 1 100%; }
  .apikey { flex: 1 1 auto; min-width: 0; }
  .apikey input { width: 100%; max-width: none; }
  .health { display: none; }
}

/* ── layout ─────────────────────────────────────────────────── */
.layout {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 360px 1fr; gap: 16px; padding: 16px; overflow: hidden;
}
.sidebar { display: flex; flex-direction: column; gap: 16px; min-height: 0; }
.detail { min-height: 0; overflow: auto; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
.jobs-card { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.card-head h2 { font-weight: 600; }
.card-head .count { font-size: 12px; color: var(--muted); background: var(--panel-2); border-radius: 999px; padding: 1px 9px; }

/* ── upload ─────────────────────────────────────────────────── */
/* Cap the whole upload element (dropzone + list) at half the viewport; only
   the list scrolls inside, so the dropzone stays fixed and the jobs panel
   below is never pushed off-screen. */
.upload-card { padding: 14px; max-height: 50vh; display: flex; flex-direction: column; min-height: 0; }
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  flex: none; padding: 22px 14px; text-align: center; cursor: pointer;
  border: 1.5px dashed var(--border); border-radius: 10px;
  background: var(--panel-2); color: var(--muted);
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); color: var(--text); }
.dropzone.drag { background: color-mix(in srgb, var(--accent) 8%, var(--panel)); }
.dropzone .dz-icon { font-size: 22px; color: var(--accent); }
.dropzone strong { color: var(--accent); }
.dropzone .dz-hint { font-size: 11.5px; }

.uploads-head { display: flex; flex: none; align-items: center; justify-content: space-between; margin-top: 12px; font-size: 11.5px; color: var(--muted); }
/* Fills the space left under the dropzone within the 50vh card, then scrolls. */
.uploads {
  list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 5px;
  flex: 0 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
}
.uploads li {
  display: grid;
  grid-template-columns: 16px 1fr auto;
  grid-template-areas:
    "dot name view"
    ".   msg  msg";
  align-items: center; column-gap: 8px; row-gap: 2px;
  font-size: 12.5px; padding: 6px 9px; border-radius: 7px;
  background: var(--panel-2); border-left: 3px solid var(--s-queued);
}
/* border accent + icon colour track the linked job's live state */
.uploads li.st-rendering { border-left-color: var(--s-rendering); }
.uploads li.st-ocr       { border-left-color: var(--s-ocr); }
.uploads li.st-done      { border-left-color: var(--s-done); }
.uploads li.st-failed,
.uploads li.st-error     { border-left-color: var(--s-failed); }
/* status circle (left), coloured by the linked job's live state */
.uploads .u-dot { grid-area: dot; width: 9px; height: 9px; border-radius: 50%; justify-self: center; background: var(--s-queued); }
.uploads li.st-rendering .u-dot { background: var(--s-rendering); }
.uploads li.st-ocr       .u-dot { background: var(--s-ocr); }
.uploads li.st-done      .u-dot { background: var(--s-done); }
.uploads li.st-failed .u-dot,
.uploads li.st-error  .u-dot { background: var(--s-failed); }
.uploads .u-name { grid-area: name; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.uploads .u-msg { grid-area: msg; min-width: 0; color: var(--muted); overflow-wrap: anywhere; }
.uploads .link { grid-area: view; }

/* ── search + filters ───────────────────────────────────────── */
.search { position: relative; padding: 12px 14px 8px; }
.search input {
  width: 100%; padding: 8px 28px 8px 11px; font: inherit;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
}
.search input:focus { outline: none; border-color: var(--accent); }
.search .clear {
  position: absolute; right: 22px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; color: var(--muted); font-size: 18px; line-height: 1;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; cursor: pointer;
  font-size: 11.5px; padding: 3px 8px; border-radius: 999px; text-transform: capitalize;
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--border);
}
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.chip .chip-n { font-variant-numeric: tabular-nums; opacity: .8; }

/* ── job list ───────────────────────────────────────────────── */
.jobs { list-style: none; margin: 0; padding: 6px; overflow: auto; flex: 1; }
.job {
  display: block; width: 100%; text-align: left; cursor: pointer;
  padding: 9px 10px; border-radius: 9px; border: 1px solid transparent;
  background: none; color: inherit; font: inherit;
}
.job:hover { background: var(--panel-2); }
.job.active { background: color-mix(in srgb, var(--accent) 12%, var(--panel)); border-color: var(--accent); }
.j-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.j-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.j-sub { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.sentinel { height: 1px; }
.showing { font-size: 11.5px; color: var(--muted); text-align: center; padding: 6px; }

/* ── badges ─────────────────────────────────────────────────── */
.badge {
  font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: .03em;
  padding: 2px 8px; border-radius: 999px; white-space: nowrap;
  color: var(--s-queued); background: color-mix(in srgb, var(--s-queued) 16%, transparent);
}
.badge.lg { font-size: 12px; padding: 3px 11px; }
.badge.s-queued    { color: var(--s-queued);    background: color-mix(in srgb, var(--s-queued) 16%, transparent); }
.badge.s-rendering { color: var(--s-rendering); background: color-mix(in srgb, var(--s-rendering) 16%, transparent); }
.badge.s-ocr       { color: var(--s-ocr);       background: color-mix(in srgb, var(--s-ocr) 18%, transparent); }
.badge.s-done      { color: var(--s-done);      background: color-mix(in srgb, var(--s-done) 16%, transparent); }
.badge.s-failed    { color: var(--s-failed);    background: color-mix(in srgb, var(--s-failed) 16%, transparent); }

/* ── detail panel ───────────────────────────────────────────── */
.detail-inner { display: flex; flex-direction: column; gap: 14px; max-width: 760px; }
.d-head { display: flex; align-items: center; gap: 12px; }
.d-head h2 { font-size: 19px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.d-meta { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.d-times { font-size: 12.5px; color: var(--muted); }

.stage { background: var(--panel); border: 1px solid var(--border); border-radius: 11px; padding: 13px 15px; }
.stage-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 9px; }
.stage-h h3 { font-weight: 600; }

/* progress bars (semantic <progress>, restyled cross-browser) */
progress.bar {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 8px; border: none; border-radius: 999px; background: var(--panel-2); display: block;
}
progress.bar::-webkit-progress-bar { background: var(--panel-2); border-radius: 999px; }
progress.bar::-webkit-progress-value { border-radius: 999px; transition: width .4s ease; }
progress.render::-webkit-progress-value { background: var(--s-rendering); }
progress.render::-moz-progress-bar { background: var(--s-rendering); }
progress.ocr::-webkit-progress-value { background: var(--s-ocr); }
progress.ocr::-moz-progress-bar { background: var(--s-ocr); }

.counters { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 9px; font-size: 12.5px; color: var(--muted); }
.counters b { color: var(--text); font-weight: 650; }
.counters .bad b { color: var(--s-failed); }

.grid2 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 0; }
.grid2 > div { background: var(--panel); border: 1px solid var(--border); border-radius: 9px; padding: 9px 11px; }
.grid2 dt { font-size: 11.5px; color: var(--muted); }
.grid2 dd { margin: 0; font-size: 14px; }
.note { font-size: 11.5px; color: var(--muted); margin-top: -8px; }

/* ── per-page timing stats (Render / Parsing distributions) ──── */
.pstats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pstat-card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 11px 13px; }
.pstat-h { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.pstat-h .pstat-n { margin-left: auto; font-weight: 400; font-size: 11.5px; color: var(--muted); }
.pstat-h .pstat-n b { color: var(--text); font-weight: 650; }
.pstat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0 0; }
.pstat-grid dt { font-size: 11px; color: var(--muted); }
.pstat-grid dd { margin: 2px 0 0; font-size: 13.5px; font-variant-numeric: tabular-nums; }
@media (max-width: 480px) { .pstats { grid-template-columns: 1fr; } }

.gauges { font-size: 13px; }
.gauges-toggle {
  display: inline-flex; align-items: center; gap: 8px;
  background: none; border: none; padding: 4px 0;
  color: var(--text); cursor: pointer; font-size: 14px; font-weight: 600;
}
.gauges-toggle:hover .gauges-label { text-decoration: underline; text-underline-offset: 2px; }
.gauges-caret { color: var(--accent); font-size: 20px; line-height: 1; width: 1em; text-align: center; }
.gauge-grid { margin-top: 8px; grid-template-columns: repeat(3, 1fr); }
.gauge-grid dd { font-size: 13px; }

/* ── buttons / result ───────────────────────────────────────── */
.btn {
  display: inline-block; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  font: inherit; font-weight: 600; text-decoration: none;
  background: var(--accent); color: var(--accent-fg); border: 1px solid var(--accent);
}
.btn:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn:disabled { opacity: .6; cursor: default; }
.result-actions { display: flex; gap: 10px; }

.link { background: none; border: none; padding: 0; cursor: pointer; color: var(--accent); font: inherit; text-decoration: underline; }

.preview { border: 1px solid var(--border); border-radius: 11px; overflow: hidden; }
.preview-h {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 12px; background: var(--panel-2); border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.preview pre {
  margin: 0; padding: 13px; max-height: 460px; overflow: auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px; line-height: 1.5; white-space: pre-wrap; word-break: break-word;
}

/* ── misc ───────────────────────────────────────────────────── */
.placeholder { display: grid; place-items: center; height: 100%; color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 18px; font-size: 13px; }
.msg { font-size: 13px; padding: 10px 12px; border-radius: 9px; }
.msg.err {
  color: var(--s-failed); background: color-mix(in srgb, var(--s-failed) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--s-failed) 30%, transparent);
  white-space: pre-wrap; word-break: break-word; margin: 0 14px;
}
.detail .msg.err { margin: 0; }
/* Top-of-page auth banner below the header, inset to align with the content
   grid. The triple class outranks .msg.err's own margin. */
.msg.err.authbar { margin: 16px 16px 0; }

/* ── page timeline (Gantt of per-page render/parsing spans) ─── */
/* legend + live hover readout above the chart */
.tl-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin: 10px 0 8px; min-height: 18px; font-size: 11.5px; color: var(--muted);
}
.tl-legend { display: inline-flex; align-items: center; gap: 12px; flex: none; }
.tl-tag { display: inline-flex; align-items: center; gap: 5px; }
.tl-hover { font-variant-numeric: tabular-nums; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tl-key { display: inline-block; width: 9px; height: 9px; border-radius: 2px; vertical-align: middle; }
.tl-key.render { background: var(--s-rendering); }
.tl-key.ocr { background: var(--s-ocr); }

.tl {
  display: flex; flex-direction: column; gap: 3px;
  max-height: 320px; overflow-y: auto; overscroll-behavior: contain;
  padding: 6px 8px 0; background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px;
}
.tl-row { display: grid; grid-template-columns: 36px 1fr; align-items: center; gap: 8px; border-radius: 4px; }
.tl-row.hot { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.tl-page { font-size: 11px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
/* track background carries the vertical timing guides (0/25/50/75%); bars sit on top */
.tl-track {
  position: relative; height: 14px; border-radius: 4px; overflow: hidden;
  background-color: var(--panel-2);
  background-image: repeating-linear-gradient(90deg, var(--border) 0 1px, transparent 1px 25%);
}
.tl-bar { position: absolute; top: 2px; height: 10px; border-radius: 3px; }
.tl-bar.render { background: var(--s-rendering); }
.tl-bar.ocr { background: var(--s-ocr); }
.tl-row.hot .tl-bar { outline: 1px solid color-mix(in srgb, var(--text) 40%, transparent); }

/* sticky ruler: same 36px+1fr grid as the rows, so ticks line up with the
   per-track gridlines whether or not the list is scrolling */
.tl-axis {
  position: sticky; bottom: 0; display: grid; grid-template-columns: 36px 1fr; gap: 8px;
  margin-top: 3px; padding: 5px 0 7px; background: var(--panel); border-top: 1px solid var(--border);
}
.tl-axis-track { position: relative; height: 12px; }
.tl-axis-tick {
  position: absolute; top: 0; transform: translateX(-50%); white-space: nowrap;
  font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.tl-axis-tick::before {
  content: ""; position: absolute; left: 50%; top: -5px; width: 1px; height: 4px; background: var(--border);
}
.tl-axis-tick.a-start { transform: none; }
.tl-axis-tick.a-start::before { left: 0; }
.tl-axis-tick.a-end { transform: translateX(-100%); }
.tl-axis-tick.a-end::before { left: auto; right: 0; }

/* ── responsive ─────────────────────────────────────────────── */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; overflow: auto; }
  .sidebar { min-height: auto; }
  .jobs { max-height: 320px; }
  .detail { overflow: visible; min-height: auto; }
}
