:root {
  --fg: #1a1a1a;
  --muted: #666;
  --accent: #2b6cb0;
  --bg: #fafafa;
  --panel: #ffffff;
  --border: #d8d8d8;
  --danger: #b00020;
  --ok: #1f7a1f;
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  margin: 0;
  background: var(--bg);
  color: var(--fg);
}

header {
  padding: 1rem 2rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  align-items: baseline;
  gap: 1rem;
}

header h1 { margin: 0; font-size: 1.25rem; }

header nav a {
  color: var(--muted);
  text-decoration: none;
  margin-right: 1rem;
}
header nav a:hover { color: var(--accent); }

main {
  padding: 1.5rem 2rem;
  max-width: 1100px;
  margin: 0 auto;
}

section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem 1.25rem;
  margin-bottom: 1.25rem;
}

section h2 { margin-top: 0; font-size: 1.05rem; }
section h3 { font-size: 0.95rem; margin-bottom: 0.4rem; padding-left: 0.5rem; }
section h4 { font-size: 0.9rem; margin-bottom: 0.3rem; padding-left: 1.25rem; }
section > p, section > ul, section > ol, section > pre, section > table, section > .table-scroll { padding-left: 1rem; }
section > h3 ~ h4 ~ p, section > h3 ~ h4 ~ ul, section > h3 ~ h4 ~ ol, section > h3 ~ h4 ~ pre { padding-left: 1.75rem; }
section > h3 ~ h3 ~ p, section > h3 ~ h3 ~ ul, section > h3 ~ h3 ~ ol, section > h3 ~ h3 ~ pre { padding-left: 1rem; }

button {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.45rem 0.9rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.95rem;
}
button:hover { filter: brightness(0.95); }
button.secondary { background: #eee; color: var(--fg); }
button.danger { background: var(--danger); }
button:disabled,
button:disabled:hover {
  background: #d8d8d8;
  color: #777;
  cursor: not-allowed;
  filter: none;
}

input[type="text"], input[type="password"], select, textarea {
  font: inherit;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  min-width: 12rem;
}

label { display: inline-flex; gap: 0.5rem; align-items: center; margin-right: 0.75rem; }

.error { color: var(--danger); }
.ok { color: var(--ok); }
.muted { color: var(--muted); font-size: 0.9rem; }
.warn-inline { color: #8a5a00; font-size: 0.9rem; }

.dropzone {
  border: 2px dashed var(--border);
  border-radius: 6px;
  padding: 1.5rem;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
}
.dropzone.dragover { border-color: var(--accent); color: var(--accent); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--border); }

pre.log {
  background: #111;
  color: #eee;
  padding: 0.75rem;
  border-radius: 4px;
  max-height: 16rem;
  overflow: auto;
  font-size: 0.85rem;
  white-space: pre-wrap;
  border-left: 4px solid transparent;
}
pre.log.ok    { border-left-color: var(--ok); }
pre.log.warn  { border-left-color: #d18a00; }
pre.log.error { border-left-color: var(--danger); }

/* Cumulative LLM-diagnostic stream pane. User-resizable via the bottom-
 * right corner; previous step runs remain in view (no auto-clear). */
.stream-log {
  background: #111;
  color: #eee;
  padding: 0.75rem;
  border-radius: 4px;
  height: 22rem;
  min-height: 8rem;
  resize: both;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  border-left: 4px solid transparent;
}
.stream-log .stream-section {
  border-top: 1px dashed #444;
  padding-top: 0.5rem;
  margin-top: 0.5rem;
}
.stream-log .stream-section:first-child {
  border-top: none;
  padding-top: 0;
  margin-top: 0;
}
.stream-log .stream-header {
  color: #ffd47a;
  font-weight: bold;
  margin-bottom: 0.3rem;
}
.stream-log .stream-role {
  color: #87ceff;
  margin: 0.3rem 0 0.15rem;
}
.stream-log .stream-text {
  white-space: pre-wrap;
  word-break: break-word;
}
.stream-log .stream-image {
  display: block;
  max-width: 320px;
  max-height: 200px;
  border: 1px solid #555;
  margin: 0.3rem 0;
  cursor: zoom-in;
  background: #fff;
}

.stream-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  cursor: zoom-out;
}
.stream-lightbox img {
  max-width: 95vw;
  max-height: 95vh;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

.row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.row > * { flex: 0 0 auto; }
.row .grow { flex: 1 1 auto; }
/* Labels are inline-flex globally so simple "Label <input/>" pairs line
 * up nicely, but a growing label that wraps a paragraph of text plus a
 * textarea ends up splitting each inline node (text, <code>, <a>,
 * textarea) into its own flex column. Force such labels to behave as
 * block containers so the descriptive text flows normally and the
 * textarea sits underneath. */
.row > label.grow { display: block; }
.row > label.grow > textarea { display: block; width: 100%; margin-top: 0.35rem; box-sizing: border-box; }

.status-banner {
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}
.status-banner.ok   { background: #e6f5e6; color: var(--ok); }
.status-banner.warn { background: #fff5e0; color: #8a5a00; }
.status-banner.err  { background: #fde7ea; color: var(--danger); }

.spinner {
  display: inline-block;
  width: 0.9em;
  height: 0.9em;
  margin-right: 0.5em;
  vertical-align: -0.15em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spinner-rotate 0.8s linear infinite;
}
@keyframes spinner-rotate { to { transform: rotate(360deg); } }

.workspace { display: flex; flex-direction: column; gap: 0.5rem; }
.workspace-toolbar { padding: 0.25rem 0; border-bottom: 1px solid var(--border); }
.workspace-toolbar select { min-width: 8rem; }
.zoom-controls .zoom-label { min-width: 3rem; text-align: center; }
.canvas-frame {
  position: relative;
  border: 1px solid var(--border);
  background: #2a2a2a;
  overflow: auto;
  height: 70vh;
}
.canvas-stage {
  position: relative;
  transform-origin: 0 0;
  width: 0;
  height: 0;
}
.canvas-stage .page-canvas {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  background: white;
}
.canvas-overlay {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: auto;
}
.canvas-overlay.drawing { cursor: crosshair; }
.canvas-overlay.ocr-overlay { pointer-events: none; }
.ocr-overlay .ocr-block-mark {
  position: absolute;
  border: 1px solid rgba(34, 139, 34, 0.7);
  box-sizing: border-box;
}
.ocr-overlay .ocr-token-mark {
  position: absolute;
  border: 1px dashed rgba(230, 120, 0, 0.75);
  box-sizing: border-box;
}

.box {
  position: absolute;
  border: 2px solid rgba(43, 108, 176, 0.85);
  background: rgba(43, 108, 176, 0.10);
  box-sizing: border-box;
  cursor: move;
}
.box.selected {
  border-color: rgba(176, 0, 32, 0.95);
  background: rgba(176, 0, 32, 0.12);
  z-index: 1;
}
.resize-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: white;
  border: 1px solid rgba(176, 0, 32, 0.95);
  box-sizing: border-box;
  z-index: 2;
}
.resize-handle.h-nw { left: -6px;  top: -6px;    cursor: nwse-resize; }
.resize-handle.h-n  { left: 50%;   top: -6px;    transform: translateX(-50%); cursor: ns-resize; }
.resize-handle.h-ne { right: -6px; top: -6px;    cursor: nesw-resize; }
.resize-handle.h-e  { right: -6px; top: 50%;     transform: translateY(-50%); cursor: ew-resize; }
.resize-handle.h-se { right: -6px; bottom: -6px; cursor: nwse-resize; }
.resize-handle.h-s  { left: 50%;   bottom: -6px; transform: translateX(-50%); cursor: ns-resize; }
.resize-handle.h-sw { left: -6px;  bottom: -6px; cursor: nesw-resize; }
.resize-handle.h-w  { left: -6px;  top: 50%;     transform: translateY(-50%); cursor: ew-resize; }
.box.ghost {
  border-style: dashed;
  background: rgba(43, 108, 176, 0.05);
  pointer-events: none;
}
.box-label {
  position: absolute;
  left: 0;
  top: -1.1rem;
  background: rgba(43, 108, 176, 0.95);
  color: white;
  font-size: 0.7rem;
  padding: 0 0.25rem;
  border-radius: 2px;
  white-space: nowrap;
}
.box.selected .box-label {
  background: rgba(176, 0, 32, 0.95);
}

.uploads-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.5rem;
}
.upload-row {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  background: var(--bg);
}
.upload-head {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.upload-name { font-size: 0.95rem; }
.upload-pages { font-size: 0.85rem; }
.upload-detail { margin-top: 0.2rem; font-size: 0.85rem; }
.status-pill {
  margin-left: auto;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel);
  font-size: 0.8rem;
}
.status-pill.ok { color: var(--ok); border-color: #b8d8b8; }
.status-pill.warn { color: #8a5a00; border-color: #e6c98a; }
.status-pill.error { color: var(--danger); border-color: #e0a0a8; }

.review-card {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.75rem;
  background: var(--bg);
  margin-bottom: 0.75rem;
}
.review-card-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.review-meta { margin: 0.25rem 0 0.5rem 0; }
.review-body {
  display: grid;
  grid-template-columns: minmax(120px, 240px) 1fr;
  gap: 1rem;
  align-items: start;
}
.review-crop {
  max-width: 100%;
  max-height: 240px;
  border: 1px solid var(--border);
  background: white;
}
.review-editor { display: flex; flex-direction: column; gap: 0.5rem; }
.review-input input[type="text"],
.review-input input[type="number"],
.review-input input[type="date"],
.review-input select { min-width: 12rem; }
.review-multiselect { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.review-multiselect label { display: inline-flex; gap: 0.25rem; align-items: center; }

#editor-layout {
  display: grid;
  grid-template-columns: 1fr 22rem;
  gap: 1rem;
}
.side-panel {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 78vh;
  overflow: auto;
}
.side-panel-field { display: flex; flex-direction: column; gap: 0.25rem; }
.side-panel-field label { display: block; }
.side-panel-field input, .side-panel-field textarea, .side-panel-field select {
  width: 100%;
  min-width: 0;
}

.llm-diag-steps {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.75rem 0;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--panel, transparent);
}
.llm-diag-step {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 3px;
}
.llm-diag-step button { margin: 0; }

.llm-test-table {
  font-size: 0.9rem;
  table-layout: fixed;
}
.llm-test-table th, .llm-test-table td {
  vertical-align: top;
  word-break: break-word;
}
/* Combination currently being measured by the benchmark sweep. */
.llm-test-table tr.bench-row-active > td {
  background: var(--accent-soft, rgba(64, 128, 255, 0.12));
}
.llm-test-table tr.bench-row-active > td:first-child {
  font-weight: 600;
}
.llm-test-crop {
  max-width: 120px;
  max-height: 120px;
  border: 1px solid var(--border);
  background: white;
  display: block;
}
.zoomable { cursor: zoom-in; }
.zoom-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  cursor: zoom-out;
  padding: 1rem;
}
.zoom-overlay.visible { display: flex; }
.zoom-overlay img {
  max-width: 95vw;
  max-height: 95vh;
  background: white;
  border: 1px solid var(--border);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
  image-rendering: pixelated;
}
.llm-test-status-cell .llm-test-retest {
  display: block;
  margin-top: 0.4rem;
  font-size: 0.8rem;
  padding: 0.15rem 0.5rem;
}
.llm-test-raw {
  background: #111;
  color: #eee;
  padding: 0.4rem 0.5rem;
  border-radius: 3px;
  font-size: 0.75rem;
  max-height: 10rem;
  overflow: auto;
  white-space: pre-wrap;
  margin: 0;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 1rem;
}
.modal-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1.25rem 1.5rem;
  max-width: 32rem;
  width: 100%;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.25);
}
.modal-panel h2 { margin-top: 0; font-size: 1.1rem; }
.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

/* Horizontal-scroll wrapper for tables. Without this, narrow viewports
 * collapse fixed-width columns into single-letter-per-line text. The
 * wrapper keeps native table layout (so headers and cells stay aligned)
 * and just lets the whole table scroll sideways. */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
.table-scroll > table { width: 100%; }

@media (max-width: 720px) {
  header {
    padding: 0.6rem 0.9rem;
    flex-wrap: wrap;
    row-gap: 0.25rem;
    column-gap: 0.75rem;
  }
  header h1 { font-size: 1.1rem; flex-basis: 100%; }
  header nav a { margin-right: 0.6rem; font-size: 0.9rem; }

  main { padding: 1rem 0.75rem; }
  section { padding: 0.75rem 0.85rem; margin-bottom: 0.85rem; }
  section h2 { font-size: 1rem; }

  /* Drop the section's nested padding-left ladder; on phones it just
   * eats horizontal space without aiding hierarchy. */
  section > p, section > ul, section > ol, section > pre, section > .table-scroll,
  section > h3 ~ h4 ~ p, section > h3 ~ h4 ~ ul, section > h3 ~ h4 ~ ol, section > h3 ~ h4 ~ pre,
  section > h3 ~ h3 ~ p, section > h3 ~ h3 ~ ul, section > h3 ~ h3 ~ ol, section > h3 ~ h3 ~ pre {
    padding-left: 0;
  }
  section h3 { padding-left: 0; }
  section h4 { padding-left: 0; }

  /* Inputs lose the 12rem floor that overflows a phone viewport. Let
   * them stretch to whatever flex space their row gives them. */
  input[type="text"], input[type="password"], select, textarea {
    min-width: 0;
    max-width: 100%;
  }
  .row { gap: 0.4rem; row-gap: 0.5rem; }
  .row > label { flex: 1 1 100%; }
  .row > label > input[type="text"],
  .row > label > input[type="password"],
  .row > label > input[type="number"],
  .row > label > input[type="date"],
  .row > label > select,
  .row > label > textarea {
    flex: 1 1 auto;
    width: 100%;
  }
  .row > label > input[type="range"] { flex: 1 1 auto; min-width: 0; }

  /* Editor layout: stack the canvas above the side panel. */
  #editor-layout { grid-template-columns: 1fr; }
  .side-panel { max-height: none; }

  /* Review queue cards stack crop on top of editor. */
  .review-body { grid-template-columns: 1fr; }
  .review-crop { max-height: 180px; }

  /* Tables that lived inside a .table-scroll keep their natural width
   * so columns can hold readable text and scroll horizontally instead
   * of collapsing. The fixed inline widths on llm-test-table headers
   * already imply a minimum table width; this just preserves it. */
  .table-scroll > table.llm-test-table { min-width: 36rem; }
  .llm-test-table th, .llm-test-table td { white-space: normal; }

  .canvas-frame { height: 55vh; }
  .workspace-toolbar { font-size: 0.85rem; }

  pre.log, .stream-log { font-size: 0.78rem; }
  .stream-log { height: 16rem; }
}

details.llm-options {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.4rem 0.6rem;
  margin: 0.5rem 0;
  background: var(--bg);
}
details.llm-options > summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
  padding: 0.2rem 0;
}
details.llm-options[open] > summary { color: var(--fg); }
.llm-options-body {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-top: 0.5rem;
}
.llm-options-help {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 0.85rem;
}
.llm-options-textarea {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
}

.preset-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.preset-row {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.6rem 0.8rem;
  background: var(--panel);
}
.preset-row.is-default { border-color: var(--accent); }
.preset-row-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.preset-row-head .preset-name { font-weight: 600; }
.preset-row-head .preset-model {
  color: var(--muted);
  font-family: ui-monospace, monospace;
  font-size: 0.85rem;
}
.preset-row-head .grow { flex: 1; }
.preset-form-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: flex-end;
  margin-bottom: 0.4rem;
}
.preset-form-row label { display: flex; flex-direction: column; gap: 0.2rem; }

