:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --border: #dde1e7;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --ok: #1d8a4e;
  --warn: #b26a00;
  --bad: #c2362f;
  --tile: #eceff3;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --card: #1a1d23;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2c313a;
    --accent: #5b8ef5;
    --ok: #4cc28a;
    --warn: #e0a341;
    --bad: #f06b63;
    --tile: #23272f;
  }
}

* { box-sizing: border-box; }

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

.top, main { max-width: 1200px; margin: 0 auto; padding: 0 16px; }
.top { padding-top: 28px; }
h1 { margin: 0 0 4px; font-size: 1.5rem; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 16px 0;
}

label { display: block; font-weight: 600; margin-bottom: 6px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.fields { display: grid; grid-template-columns: 160px 1fr; gap: 12px; margin-top: 12px; }
.fields input { width: 100%; }
@media (max-width: 560px) { .fields { grid-template-columns: 1fr; } }

input, select, button {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 9px 12px;
  background: var(--card);
  color: var(--text);
}
input { flex: 1 1 320px; min-width: 0; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button { cursor: pointer; background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button:disabled { opacity: .6; cursor: progress; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.error { color: var(--bad); margin: 8px 0 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.job-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.job-head > div:first-child { min-width: 0; }
.status-line { display: flex; gap: 10px; align-items: center; }
.actions { display: flex; gap: 8px; flex-shrink: 0; }

.badge {
  font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em;
  padding: 2px 8px; border-radius: 999px; border: 1px solid currentColor;
}
.badge.queued, .badge.running { color: var(--accent); }
.badge.done { color: var(--ok); }
.badge.failed { color: var(--bad); }
.badge.cancelled { color: var(--warn); }
.badge.uploading { color: var(--accent); }
.badge.uploaded { color: var(--ok); }
.badge.waiting, .badge.skipped { color: var(--muted); }
.status-line + .status-line { margin-top: 4px; }

/* Page links (Extract / Extract & Upload) */
.nav { display: flex; gap: 16px; margin-bottom: 8px; font-size: .9rem; }
.nav a { color: var(--muted); text-decoration: none; padding-bottom: 2px; }
.nav a:hover { color: var(--text); }
.nav a[aria-current="page"] { color: var(--text); font-weight: 600; border-bottom: 2px solid var(--accent); }

/* Upload page file table */
.files { padding: 0; overflow-x: auto; }
.files table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.files th, .files td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.files th { font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; }
.files th:nth-child(1) { width: 40%; }
.files th:nth-child(2) { width: 90px; }
.files th:nth-child(3) { width: 120px; }
.files td:first-child a { display: block; color: var(--text); text-decoration: none; }
.files td:first-child a:hover { text-decoration: underline; }
.files tr:last-child td { border-bottom: 0; }
.files .num { text-align: right; font-variant-numeric: tabular-nums; }
@media (max-width: 600px) {
  .files th:nth-child(4), .files td:nth-child(4) { display: none; }
  .files th:nth-child(1) { width: auto; }
  .job-head { flex-direction: column; }
}

.progress { height: 4px; background: var(--tile); border-radius: 4px; margin: 12px 0; overflow: hidden; }
#bar { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.progress.indeterminate #bar { width: 30%; animation: slide 1.2s infinite ease-in-out; }
@keyframes slide { from { margin-left: -30%; } to { margin-left: 100%; } }

.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: transparent; color: var(--text); border-color: var(--border); padding: 4px 10px; font-size: .85rem; }
.chip.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
  padding-bottom: 40px;
}
.tile {
  margin: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.tile.failed { border-color: var(--bad); }
.preview {
  display: block; aspect-ratio: 4 / 3; background: var(--tile);
  position: relative; color: var(--muted); overflow: hidden;
}
/* Absolute so portrait photos never stretch the tile. */
.preview img, .preview video {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block;
}
.preview .label {
  position: absolute; inset: 0; display: grid; place-items: center; font-size: .85rem; padding: 8px; text-align: center;
}
.preview .kind {
  position: absolute; top: 6px; left: 6px; font-size: .7rem; font-weight: 700;
  background: rgba(0, 0, 0, .6); color: #fff; padding: 1px 6px; border-radius: 4px;
}
figcaption { display: flex; gap: 8px; align-items: center; padding: 6px 8px; font-size: .8rem; }
figcaption .name { flex: 1; min-width: 0; }
.dl { color: var(--accent); text-decoration: none; font-weight: 600; flex-shrink: 0; }
.dl:hover { text-decoration: underline; }
.dl[aria-disabled="true"] { color: var(--muted); pointer-events: none; }

@media (max-width: 560px) {
  .job-head { flex-direction: column; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

/* Server Upload page */
input.wide { width: 100%; }
h2 { font-size: 1.1rem; margin: 0; }
h3 { font-size: .95rem; margin: 0; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 20px 0 8px; }
.small-btn { padding: 4px 10px; font-size: .85rem; }
.hint { margin: 4px 0 0; }
code { font-size: .85em; background: var(--tile); padding: 0 4px; border-radius: 4px; }
.kv-list { display: flex; flex-direction: column; gap: 6px; }
.kv { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 2fr) auto auto; gap: 6px; }
.kv .kv-show[hidden] + .kv-remove { grid-column: 3 / 5; justify-self: end; }
.kv input { width: 100%; }
@media (max-width: 560px) {
  .kv { grid-template-columns: minmax(0, 1fr) auto auto; }
  .kv .kv-name { grid-column: 1 / -1; }
  .kv .kv-show[hidden] + .kv-remove { grid-column: auto; }
}
.opts { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 12px; margin-top: 20px; }
.opts input { width: 100%; }
@media (max-width: 560px) { .opts { grid-template-columns: 1fr 1fr; } .opts > div:first-child { grid-column: 1 / -1; } }
.preview-box { margin-top: 16px; }
.preview-box pre {
  background: var(--tile); border-radius: 8px; padding: 10px; overflow-x: auto;
  font-size: .8rem; margin: 8px 0 0;
}
.submit-row { margin-top: 16px; }
.counts { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin: 0; }
.counts div { background: var(--tile); border-radius: 8px; padding: 8px 10px; }
.counts dt { font-size: .75rem; color: var(--muted); text-transform: uppercase; font-weight: 600; }
.counts dd { margin: 0; font-size: 1.2rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.counts dd.ok { color: var(--ok); }
.counts dd.bad { color: var(--bad); }
@media (max-width: 560px) { .counts { grid-template-columns: repeat(3, 1fr); } }
.token-box { margin-top: 16px; padding: 12px; border: 1px solid var(--warn); border-radius: 8px; }
.token-box p { margin: 0 0 10px; }
.token-box .row { margin-top: 8px; }
.badge.token_expired, .badge.done_with_errors { color: var(--warn); }

/* Face Matching page */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
textarea.wide {
  width: 100%; font: .9rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical;
  border-radius: 8px; border: 1px solid var(--border); padding: 9px 12px; background: var(--card); color: var(--text);
}
textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.thumbs { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.thumbs img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); background: var(--tile); }
.thumbs img.broken { opacity: .35; border-color: var(--bad); }
.tools { display: grid; grid-template-columns: minmax(240px, 1fr) auto; gap: 16px; align-items: start; margin-top: 16px; }
.tools .filters { justify-content: flex-end; }
.threshold input[type="range"] { width: 100%; padding: 0; accent-color: var(--accent); }
.threshold output { font-variant-numeric: tabular-nums; color: var(--accent); margin-left: 6px; }
.inline { display: flex; gap: 6px; align-items: center; justify-content: flex-end; font-weight: 400; margin: 8px 0 0; }
.inline input { flex: none; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; font-size: inherit; }
@media (max-width: 700px) { .tools { grid-template-columns: 1fr; } .tools .filters, .inline { justify-content: flex-start; } }

.face-grid { align-items: start; }
.face-tile.match { border-color: var(--ok); box-shadow: 0 0 0 1px var(--ok); }
.face-tile.noface { opacity: .75; }
.face-preview { display: block; position: relative; background: var(--tile); min-height: 80px; }
.face-preview img { display: block; width: 100%; height: auto; }
.face-preview.no-preview img { visibility: hidden; height: 120px; }
.face-preview.no-preview::after {
  content: attr(data-label); position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--muted); font-size: .8rem; padding: 8px; text-align: center;
}
.face-box { position: absolute; border: 3px solid var(--ok); border-radius: 4px; box-shadow: 0 0 0 1px rgba(0, 0, 0, .5); pointer-events: none; }
.face-box.weak { border-color: rgba(255, 255, 255, .8); border-style: dashed; border-width: 2px; }
.face-preview .kind {
  position: absolute; top: 6px; left: 6px; font-size: .7rem; font-weight: 700;
  background: rgba(0, 0, 0, .6); color: #fff; padding: 1px 6px; border-radius: 4px;
}
.face-tile .score { font-variant-numeric: tabular-nums; font-weight: 600; flex-shrink: 0; }
.face-tile.failed .score { color: var(--bad); font-weight: 400; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.face-tile.match .score { color: var(--ok); }

/* Face Webhook page: delivery state on the job card */
.webhook-panel { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.webhook-panel #wh-detail { overflow-wrap: anywhere; }
