/* Invisible Ink admin. Light and dark follow the OS. No inline styles (CSP). */
:root {
  --bg: #f6f3ec;
  --surface: #ffffff;
  --surface-2: #f1ede4;
  --text: #111827;
  --muted: #4b5563;
  --line: #d9d3c5;
  --line-strong: #857c69;      /* input and button borders: >= 3:1 */
  --primary: #cc8800;          /* fills; text on it is --on-primary */
  --primary-hover: #b87a00;
  --on-primary: #111827;
  --primary-text: #8a5a00;     /* amber as text or rings on light surfaces */
  --focus: #8a5a00;
  --indicator: #8a5a00;        /* active nav bar: >= 3:1 on surfaces */
  --danger: #b91c1c;
  --danger-hover: #991b1b;
  --on-danger: #ffffff;
  --danger-bg: #fdecec;
  --warn-text: #92400e;
  --warn-bg: #fdf3dc;
  --ok-text: #166534;
  --ok-bg: #e5f5ea;
  --info-text: #1e40af;
  --info-bg: #e6edfb;
  --off-text: #4b5563;
  --off-bg: #eceae4;
  --thumb-bg: #ffffff;
  --shadow: 0 1px 2px rgba(17, 24, 39, 0.06), 0 4px 16px rgba(17, 24, 39, 0.06);
  --radius: 8px;
  --radius-sm: 6px;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14120e;
    --surface: #1d1a15;
    --surface-2: #26221b;
    --text: #f3efe6;
    --muted: #b8b0a0;
    --line: #3a342a;
    --line-strong: #80765f;
    --primary: #e0a020;
    --primary-hover: #f0b43a;
    --on-primary: #14120e;
    --primary-text: #f0b43a;
    --focus: #f0b43a;
    --indicator: #e0a020;
    --danger: #f87171;
    --danger-hover: #fca5a5;
    --on-danger: #14120e;
    --danger-bg: #3b1717;
    --warn-text: #fcd34d;
    --warn-bg: #3a2c0c;
    --ok-text: #86efac;
    --ok-bg: #11301d;
    --info-text: #a5c0fb;
    --info-bg: #17223d;
    --off-text: #c7c0b2;
    --off-bg: #2d2921;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
}

h1, h2 { line-height: 1.25; margin: 0 0 var(--s3); }
h1 { font-size: 24px; font-weight: 700; }
h2 { font-size: 20px; font-weight: 600; }
p { margin: 0 0 var(--s3); }
code, .secret { font-family: var(--mono); font-size: 14px; }
a { color: var(--primary-text); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip {
  position: absolute;
  left: var(--s2);
  top: -100px;
  z-index: 20;
  padding: var(--s2) var(--s3);
  background: var(--primary);
  color: var(--on-primary);
  border-radius: var(--radius-sm);
  font-weight: 600;
}
.skip:focus { top: var(--s2); }

/* ---- text helpers ---- */
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 14px; margin: var(--s1) 0 0; }
.small { font-size: 14px; }
.error { color: var(--danger); font-weight: 500; }
.error:empty { display: none; }
.warn { color: var(--warn-text); background: var(--warn-bg); padding: var(--s2) var(--s3); border-radius: var(--radius-sm); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.center { text-align: center; margin: var(--s4) 0; }
.stack { display: flex; flex-direction: column; gap: 2px; align-items: flex-start; }
.empty { padding: var(--s5) 0; text-align: center; }
.entry-text { font-weight: 600; overflow-wrap: anywhere; }
.label { font-weight: 600; margin: 0 0 var(--s1); }

/* ---- shell ---- */
.top {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: var(--s4);
  padding: var(--s2) var(--s5);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; white-space: nowrap; }
.brand::before {
  content: "";
  display: inline-block;
  width: 10px; height: 10px;
  margin-right: var(--s2);
  border-radius: 50%;
  background: var(--primary);
  vertical-align: 1px;
}
.nav { display: flex; gap: var(--s1); flex: 1; overflow-x: auto; scrollbar-width: thin; }
.nav a {
  padding: var(--s2) var(--s3);
  border-radius: var(--radius-sm);
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  white-space: nowrap;
}
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a[aria-current="page"] {
  color: var(--text);
  background: var(--surface-2);
  box-shadow: inset 0 -3px 0 var(--indicator);
}

.main {
  max-width: 1280px;
  margin: 0 auto;
  padding: var(--s5);
}
.main:focus { outline: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s5);
  margin: 0 0 var(--s5);
  box-shadow: var(--shadow);
}

/* ---- login ---- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--s4); }
.login { width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: var(--s2); }
.login h1 { margin-bottom: 0; }
.login label { font-weight: 600; margin-top: var(--s2); }
.login .btn { margin-top: var(--s2); }

/* ---- buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: 40px;
  padding: var(--s2) var(--s4);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  text-align: center;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: var(--on-danger); }
.btn.danger:hover:not(:disabled) { background: var(--danger-hover); border-color: var(--danger-hover); }
/* per-row destructive actions stay quiet; the confirm dialog carries the solid red */
.row-actions .btn.danger { background: var(--surface); color: var(--danger); border-color: var(--danger); }
.row-actions .btn.danger:hover:not(:disabled) { background: var(--danger-bg); color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--surface-2); }
.btn.small { min-height: 32px; padding: var(--s1) var(--s3); font-size: 14px; }

/* ---- forms ---- */
input[type="text"], input[type="password"], input[type="search"], input[type="number"], input[type="date"], select {
  min-height: 40px;
  width: 100%;
  padding: var(--s2) var(--s3);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
input[type="color"] {
  width: 64px; height: 40px;
  padding: 2px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--primary); margin: 0; flex: none; }
input:user-invalid { border-color: var(--danger); }
input::placeholder { color: var(--muted); }

.field { display: flex; flex-direction: column; gap: var(--s1); min-width: 0; }
.field > label, .field > legend { font-weight: 600; }
fieldset.field { border: 0; padding: 0; margin: 0; gap: var(--s2); }
fieldset.field > legend { padding: 0; margin-bottom: var(--s2); }
.check { display: inline-flex; align-items: center; gap: var(--s2); font-weight: 500; cursor: pointer; min-height: 32px; }
.field > label.check { font-weight: 600; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: var(--s5) var(--s4); }
.form-grid > h2, .form-grid > .form-actions { grid-column: 1 / -1; }
.form-grid > h2 { margin: 0; }
.form-inline { display: grid; grid-template-columns: 1fr 1fr auto; gap: var(--s4); align-items: start; }
.form-inline > h2 { grid-column: 1 / -1; margin: 0; }
.form-inline > .form-actions { align-self: start; padding-top: 28px; }
.form-actions { display: flex; flex-wrap: wrap; gap: var(--s2); }

.filters {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr)) minmax(0, 2fr) auto;
  gap: var(--s3);
  align-items: end;
  margin: 0 0 var(--s4);
}
.filters .actions { flex-direction: row; gap: var(--s2); }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s2); margin: 0 0 var(--s4); }
.inline-edit { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.inline-edit input { flex: 1 1 160px; width: auto; }
.row-actions { display: flex; flex-wrap: wrap; gap: var(--s2); }

/* ---- tables ---- */
.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin: 0 0 var(--s4);
}
.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
.grid th, .grid td { padding: var(--s2) var(--s3); text-align: left; vertical-align: middle; border-bottom: 1px solid var(--line); }
.grid thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}
.grid tbody tr:last-child > * { border-bottom: 0; }
.grid tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
.grid tbody th { font-weight: 600; }
.grid .num { text-align: right; }
.grid .sel { width: 40px; }
.grid td.row-actions { min-width: 220px; }
.text-cell { min-width: 220px; }
.text-cell .stack { margin-bottom: var(--s1); }
.health-cell { min-width: 180px; }
.health-cell .stack span { white-space: nowrap; }

.thumb {
  display: block;
  width: auto;
  height: auto;
  max-width: 160px;
  max-height: 56px;
  background: var(--thumb-bg);
  border: 1px solid var(--line);
  border-radius: 4px;
}

.words { max-width: 760px; }
.bar-col { width: 40%; }
meter { width: 100%; height: 12px; }
.words meter { display: block; }

/* ---- badges and banners ---- */
.badge {
  display: inline-block;
  padding: 2px var(--s2);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--off-bg);
  color: var(--off-text);
}
.state-ok, .display-approved, .status-ocr_done { background: var(--ok-bg); color: var(--ok-text); }
.state-warn, .status-needs_review { background: var(--warn-bg); color: var(--warn-text); }
.status-failed { background: var(--danger-bg); color: var(--danger); }
.status-edited { background: var(--info-bg); color: var(--info-text); }
.state-off, .display-hidden, .display-pending, .status-ocr_pending { background: var(--off-bg); color: var(--off-text); }

.banner { padding: var(--s3) var(--s4); border-radius: var(--radius); border-left: 4px solid; font-weight: 500; }
.banner.warn { background: var(--warn-bg); color: var(--warn-text); border-color: var(--primary); }
.banner.danger { background: var(--danger-bg); color: var(--danger); border-color: var(--danger); }

/* ---- stats ---- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: var(--s3);
  margin: 0 0 var(--s5);
}
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--s3) var(--s4); }
.card .stat { background: var(--surface-2); border-color: transparent; }
.stat dt { font-size: 12px; color: var(--muted); font-weight: 600; }
.stat dd { margin: var(--s1) 0 0; font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---- OCR engine order ---- */
.order-list { list-style: none; counter-reset: engine; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--s1); max-width: 420px; }
.order-list li {
  display: flex;
  align-items: center;
  gap: var(--s2);
  padding: var(--s1) var(--s1) var(--s1) var(--s3);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  counter-increment: engine;
}
.order-list li span { flex: 1; font-weight: 500; }
.order-list li span::before { content: counter(engine) ". "; color: var(--muted); }

/* ---- review ---- */
.review-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--s4); }
.review-card { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); gap: var(--s5); margin: 0; }
.review-image { display: flex; align-items: center; justify-content: center; background: var(--thumb-bg); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: var(--s3); min-height: 120px; }
.review-img { display: block; max-width: 100%; max-height: 260px; width: auto; height: auto; }
.review-body { display: flex; flex-direction: column; gap: var(--s3); min-width: 0; }
.review-body > p { margin: 0; }
.candidates { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--s2); }
.candidate {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr) auto;
  grid-template-areas: "engine text use" "engine meta use";
  column-gap: var(--s3);
  align-items: center;
  padding: var(--s2) var(--s3);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}
.candidate .engine { grid-area: engine; font-size: 12px; font-weight: 600; color: var(--muted); }
.candidate .entry-text { grid-area: text; font-size: 18px; }
.candidate .muted.small { grid-area: meta; }
.candidate .muted.small:empty { display: none; }
.candidate .btn { grid-area: use; }
.candidate.muted { grid-template-areas: "engine text use"; }

/* ---- data page ---- */
.danger-zone { border-color: var(--danger); }
.danger-zone h2 { color: var(--danger); }

/* ---- toast ---- */
.toast {
  position: fixed;
  right: var(--s5);
  bottom: var(--s5);
  z-index: 30;
  max-width: min(440px, calc(100vw - 2 * var(--s4)));
  padding: var(--s3) var(--s4);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.toast:empty { display: none; }
.toast[data-kind="error"] { background: var(--danger); color: var(--on-danger); }

/* ---- dialogs ---- */
.dialog {
  width: min(520px, calc(100vw - 2 * var(--s4)));
  padding: var(--s5);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.3);
}
.dialog::backdrop { background: rgba(17, 24, 39, 0.55); }
.dialog input { margin: 0 0 var(--s3); }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: center; gap: var(--s2); margin-top: var(--s4); }
.dialog-actions > .muted { margin-right: auto; }
.secret {
  display: block;
  padding: var(--s3);
  background: var(--surface-2);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm);
  overflow-wrap: anywhere;
  user-select: all;
}

/* ---- responsive ---- */
@media (max-width: 1100px) {
  .filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .filters .grow { grid-column: span 2; }
}

@media (max-width: 760px) {
  .top { flex-wrap: wrap; gap: var(--s2); padding: var(--s2) var(--s4); }
  .brand { flex: 1; }
  .nav { order: 3; flex-basis: 100%; }
  .main { padding: var(--s4); }
  .card { padding: var(--s4); }
  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters .grow, .filters .actions { grid-column: 1 / -1; }
  .form-inline { grid-template-columns: 1fr; }
  .form-inline > .form-actions { padding-top: 0; }
  .review-card { grid-template-columns: 1fr; gap: var(--s4); }
  .candidate { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "engine use" "text use" "meta use"; }
  .toast { left: var(--s4); right: var(--s4); bottom: var(--s4); }
  .form-grid { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
  .btn:active { transform: none; }
}
