/* ===========================================================================
   Typography
   ---------------------------------------------------------------------------
   Three families, each with a job. All are system fonts: this app holds
   privileged material, and a webfont CDN would leak a request revealing which
   tool the firm is using, on every page load.

   --font-ui    Interface: labels, tables, buttons. A humanist sans.
   --font-read  Sustained prose: assessments, summaries, the chronology. A
                serif is easier on the eye over paragraphs of narrative.
   --font-mono  Verbatim document text, Bates numbers, dates. Monospace does
                two jobs here: it preserves the column alignment that
                `pdftotext -layout` produces (tables, signature blocks), and
                its glyphs keep 1/l/I and 0/O apart — which is the difference
                between reading a Bates number or an amount correctly off an
                OCR'd page and misreading it.
   =========================================================================== */
:root {
  --font-ui: system-ui, -apple-system, "Segoe UI Variable Text", "Segoe UI",
             Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif;
  --font-read: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua",
               Charter, "Bitstream Charter", Georgia, "Noto Serif", serif;
  --font-mono: ui-monospace, "Cascadia Mono", "SF Mono", SFMono-Regular, Menlo,
               Consolas, "DejaVu Sans Mono", "Liberation Mono", "Courier New", monospace;

  --radius: 8px;
  --reading-measure: 68ch;

  /* Reader preferences, overridden per-user from localStorage. */
  --doc-text-size: 0.86rem;
}

/* ---------------------------------------------------------------------------
   Light theme — the default, tuned for long reading sessions.
   A warm off-white rather than #fff: paper white at full brightness causes
   glare over the hours a document review actually takes. Ink is near-black
   rather than pure black for the same reason. Body text clears WCAG AAA.
   --------------------------------------------------------------------------- */
:root {
  --bg: #efece6;
  --panel: #fbf9f5;
  --reading-bg: #fffdf8;
  --ink: #1b1e23;
  --ink-soft: #565d67;
  --line: #dcd6cb;
  --line-strong: #c3bbac;
  --accent: #14567f;
  --accent-ink: #ffffff;
  --ok: #1a6b43;
  --warn: #8a5600;
  --bad: #9c211f;
  --ai: #533f96;
  --helps: #1a6b43;
  --hurts: #9c211f;
  --mixed: #8a5600;
  --neutral: #666f7a;
  --unassessed: #b3aca0;
  --mark-bg: #ffe9a8;
  --shadow: 0 1px 2px rgba(30, 25, 15, .06);
}

/* Dark theme. Ink is a soft grey, never #fff: pure white on a dark ground
   blooms and makes small text harder to resolve, which is the opposite of what
   OCR review needs. Backgrounds stay slightly blue-grey to avoid a muddy cast. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181c;
    --panel: #1c2026;
    --reading-bg: #191d22;
    --ink: #dfe3e8;
    --ink-soft: #98a1ac;
    --line: #2d333b;
    --line-strong: #3d444e;
    --accent: #74b4e2;
    --accent-ink: #0c1015;
    --ok: #54c68c;
    --warn: #dda944;
    --bad: #ea7877;
    --ai: #ab9ee8;
    --helps: #54c68c;
    --hurts: #ea7877;
    --mixed: #dda944;
    --neutral: #8b95a1;
    --unassessed: #414952;
    --mark-bg: #6a5518;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

/* Explicit choice wins over the OS setting, in both directions. */
:root[data-theme="dark"] {
  --bg: #15181c;
  --panel: #1c2026;
  --reading-bg: #191d22;
  --ink: #dfe3e8;
  --ink-soft: #98a1ac;
  --line: #2d333b;
  --line-strong: #3d444e;
  --accent: #74b4e2;
  --accent-ink: #0c1015;
  --ok: #54c68c;
  --warn: #dda944;
  --bad: #ea7877;
  --ai: #ab9ee8;
  --helps: #54c68c;
  --hurts: #ea7877;
  --mixed: #dda944;
  --neutral: #8b95a1;
  --unassessed: #414952;
  --mark-bg: #6a5518;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  background: var(--bg);
  color: var(--ink);
  /* Dates and figures line up in columns instead of jittering. */
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
}

/* Dark themes render type heavier; thinning it back keeps small text crisp. */
:root[data-theme="dark"] body { -webkit-font-smoothing: antialiased; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) body { -webkit-font-smoothing: antialiased; }
}

body.centered {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

main { padding: 24px; max-width: 1400px; margin: 0 auto; }

::selection { background: var(--mark-bg); color: var(--ink); }

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

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

a { color: var(--accent); }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { font-family: var(--font-ui); line-height: 1.25; text-wrap: balance; }
h1 { font-size: 1.45rem; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 10px; }
h3 { font-size: 1rem; margin: 18px 0 6px; }
h4 { font-size: .9rem; margin: 12px 0 4px; }

.muted { color: var(--ink-soft); }
.small { font-size: .875rem; }
.strong { font-weight: 600; }
.num { text-align: right; }

/* Verbatim values — Bates numbers, dates, filenames. Monospace with slashed
   zero so 0 and O cannot be confused when transcribing from a scan. */
.mono, code, kbd, pre {
  font-family: var(--font-mono);
  font-variant-numeric: slashed-zero tabular-nums;
}

/* Prose the reviewer actually reads in paragraphs. */
.prose, .ai-draft p, .entry-summary, .finding-line, .analysis-form textarea,
.chronology .entry-body p {
  font-family: var(--font-read);
  font-size: 1rem;
  line-height: 1.65;
}
.prose { max-width: var(--reading-measure); }

/* ---------- topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar .brand { font-weight: 700; text-decoration: none; color: var(--ink); }
.topbar nav { display: flex; align-items: center; gap: 10px; flex: 1; }
.topbar .sep { color: var(--ink-soft); }
.topbar .pill {
  font-size: .8rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 10px;
  text-decoration: none;
}
.linkish {
  background: none;
  border: 0;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  padding: 0;
}
.theme-toggle {
  padding: 4px 10px;
  font-size: .8rem;
  border-radius: 999px;
  min-width: 5.5rem;
}

/* ---------- layout ---------- */
.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}
.section-title { margin: 26px 0 10px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.panel.narrow { width: min(520px, 92vw); }

.cols { display: grid; grid-template-columns: 1.2fr 1fr; gap: 16px; }
@media (max-width: 900px) { .cols { grid-template-columns: 1fr; } }

/* ---------- forms ---------- */
label { display: block; font-weight: 600; font-size: .875rem; margin: 12px 0 4px; }
label.radio, label.checkbox { font-weight: 400; display: flex; align-items: center; gap: 8px; }

input[type="text"], input[type="password"], input[type="date"], input:not([type]),
select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}
textarea { resize: vertical; }
fieldset { border: 1px solid var(--line); border-radius: 6px; margin: 14px 0; }
legend { font-weight: 600; font-size: .875rem; padding: 0 6px; }

button, .button {
  display: inline-block;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .button:hover { border-color: var(--accent); text-decoration: none; }
button.primary, .button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  margin-top: 14px;
}
button:disabled { opacity: .5; cursor: not-allowed; }
.small-button { padding: 4px 10px; font-size: .8rem; margin-top: 8px; }
form.inline { display: inline-flex; align-items: center; gap: 8px; margin: 8px 8px 8px 0; }
.actions { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }

/* ---------- alerts and tags ---------- */
.alert { padding: 10px 12px; border-radius: 6px; border: 1px solid; margin: 0 0 14px; }
.alert.ok { border-color: var(--ok); color: var(--ok); }
.alert.warn { border-color: var(--warn); color: var(--warn); }
.alert.error { border-color: var(--bad); color: var(--bad); }

.tag {
  display: inline-block;
  font-size: .72rem;
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  white-space: nowrap;
}
.tag.ok { color: var(--ok); border-color: var(--ok); }
.tag.warn { color: var(--warn); border-color: var(--warn); }
.tag.bad { color: var(--bad); border-color: var(--bad); }
.tag.ai { color: var(--ai); border-color: var(--ai); }
.tag.working { color: var(--accent); border-color: var(--accent); }

.sig {
  display: inline-grid;
  place-items: center;
  width: 20px; height: 20px;
  border-radius: 4px;
  font-size: .72rem;
  font-weight: 700;
  background: var(--line);
  color: var(--ink);
}
.sig-4, .sig-5 { background: var(--warn); color: #fff; }
.sig-5 { background: var(--bad); }

/* ---------- tables ---------- */
.grid { width: 100%; border-collapse: collapse; background: var(--panel);
        border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.grid th, .grid td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
.grid th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--ink-soft); }
.grid tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover { background: color-mix(in srgb, var(--accent) 6%, transparent); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 0; }
.stats dt { font-size: .75rem; color: var(--ink-soft); text-transform: uppercase; }
.stats dd { margin: 2px 0 0; font-size: 1.35rem; font-weight: 600; }
.stats dd.bad { color: var(--bad); }

pre.path {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  overflow-x: auto;
  font-size: .82rem;
  user-select: all;
}

/* ---------- document viewer ---------- */
.filename { word-break: break-word; }
.viewer { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 1000px) { .viewer { grid-template-columns: 1fr; } }

.pages { position: sticky; top: 16px; }
.pager { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.page-image-wrap { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px; }
#page-image { width: 100%; height: auto; display: block; background: #fff; }
#page-image.missing { display: none; }
.missing-note { display: none; }
#page-image.missing + .missing-note { display: block; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tab { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 8px 12px; }
.tab.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.tab-body { display: none; }
.tab-body.active { display: block; }

.ai-draft {
  border: 1px solid var(--ai);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 20px;
  background: color-mix(in srgb, var(--ai) 6%, transparent);
}
.finding h4 { margin-bottom: 2px; }

.page-text { margin-bottom: 20px; }
.page-text pre {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--reading-bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px 14px;
  /* Reader-controlled; see the A-/A+ buttons on the text tab. */
  font-size: var(--doc-text-size);
  line-height: 1.55;
  max-height: 62vh;
  overflow: auto;
  tab-size: 4;
}

/* "Preserve layout" mode: stop wrapping so the column alignment that
   pdftotext -layout produces survives — tables and signature blocks are
   unreadable once the lines reflow. */
.page-text.keep-layout pre { white-space: pre; word-break: normal; }

.reader-controls {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-bottom: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--line);
}
.reader-controls .spacer { flex: 1; }
.reader-controls button { padding: 3px 9px; font-size: .8rem; }
.reader-controls button[aria-pressed="true"] {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
}
.page-text mark { background: var(--mark-bg); color: var(--ink); }
.snippet { font-style: italic; font-size: .8rem; margin-top: 2px; }

/* ---------- timeline ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.filters label { margin-top: 0; }
.filters > div { flex: 1 1 150px; }
.filter-actions { display: flex; gap: 8px; align-items: center; flex: 0 0 auto; }
.filter-actions .primary { margin-top: 0; }

.legend { display: flex; flex-wrap: wrap; gap: 16px; align-items: center; margin: 0 0 12px; font-size: .82rem; }
.legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; }
.legend .dot.helps { background: var(--helps); }
.legend .dot.hurts { background: var(--hurts); }
.legend .dot.mixed { background: var(--mixed); }
.legend .dot.neutral { background: var(--neutral); }
.legend .dot.unassessed { background: var(--unassessed); }

.timeline-panel { overflow: hidden; }
#timeline { width: 100%; display: block; cursor: grab; touch-action: none; }
#timeline.dragging { cursor: grabbing; }
#timeline .grid { stroke: var(--line); stroke-width: 1; }
#timeline .axis { stroke: var(--ink-soft); stroke-width: 1; }
#timeline .axis-label { fill: var(--ink-soft); font-size: 11px; }
#timeline .marker { cursor: pointer; }
#timeline .marker circle { stroke: var(--panel); stroke-width: 1.5; }
#timeline .marker:hover circle, #timeline .marker:focus circle { stroke: var(--ink); }
#timeline .stance-helps circle { fill: var(--helps); }
#timeline .stance-hurts circle { fill: var(--hurts); }
#timeline .stance-mixed circle { fill: var(--mixed); }
#timeline .stance-neutral circle { fill: var(--neutral); }
#timeline .stance-unassessed circle { fill: var(--unassessed); }
#timeline .reviewed-ring { fill: none; stroke: var(--ok); stroke-width: 1.5; }
#timeline .privileged-ring { fill: none; stroke: var(--bad); stroke-width: 2; stroke-dasharray: 3 2; }
.legend .ring-legend {
  display: inline-block; width: 12px; height: 12px; border-radius: 50%;
  border: 2px dashed var(--bad); margin-right: 5px; vertical-align: middle;
}

.undated-list { list-style: none; padding: 0; margin: 0; columns: 2; }
.undated-list li { margin-bottom: 6px; break-inside: avoid; }
@media (max-width: 700px) { .undated-list { columns: 1; } }

/* ---------- timeline document table ---------- */
.table-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 2px; }
.table-head h2 { margin: 0; }
#table-hint { margin: 0 0 12px; }

.date-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid var(--accent); border-radius: 999px;
  padding: 3px 6px 3px 12px; font-size: .82rem;
}
.date-chip span { font-weight: 600; }
.date-chip button {
  border: 0; background: var(--accent); color: var(--accent-ink);
  border-radius: 999px; padding: 2px 10px; font-size: .78rem; cursor: pointer;
}

.table-scroll { overflow-x: auto; }

#timeline .axis-label.clickable { cursor: pointer; }
#timeline .axis-label.clickable:hover,
#timeline .axis-label.clickable:focus { fill: var(--accent); font-weight: 600; }

/* ---------- review workflow ---------- */
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .82rem; }

.head-actions { display: flex; gap: 8px; align-items: flex-start; }

.menu { position: relative; }
.menu > summary { list-style: none; cursor: pointer; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-body {
  position: absolute; right: 0; z-index: 20; min-width: 260px; margin-top: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.14);
}
.menu-body a { display: block; padding: 6px 4px; text-decoration: none; }
.menu-body a:hover { text-decoration: underline; }

.review-nav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.review-nav .button.disabled { opacity: .35; pointer-events: none; }
.review-nav .primary { margin-top: 0; }

.save-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 14px; }
.save-row .primary { margin-top: 0; }

.shortcuts { margin-top: 20px; }
kbd {
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px;
  padding: 0 5px; font-family: inherit; font-size: .78rem; background: var(--panel);
}

.progress {
  height: 10px; border-radius: 999px; background: var(--line);
  overflow: hidden; margin-bottom: 6px;
}
.progress-fill { height: 100%; background: var(--ok); transition: width .4s ease; }

.stance-dot {
  display: inline-block; width: 9px; height: 9px; border-radius: 50%;
  vertical-align: middle; margin: 0 3px;
}
.stance-dot.stance-helps { background: var(--helps); }
.stance-dot.stance-hurts { background: var(--hurts); }
.stance-dot.stance-mixed { background: var(--mixed); }
.stance-dot.stance-neutral { background: var(--neutral); }
.stance-dot.stance-unassessed { background: var(--unassessed); }

.email-headers dl { margin: 0; display: grid; gap: 4px 14px; }
.email-headers dl > div { display: grid; grid-template-columns: 70px 1fr; gap: 10px; }
.email-headers dt { font-weight: 600; font-size: .8rem; color: var(--ink-soft); }
.email-headers dd { margin: 0; font-size: .88rem; word-break: break-word; }

.text-search { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.text-search input { flex: 1; }

button.danger, .button.danger {
  border-color: var(--bad); color: var(--bad); background: var(--panel);
}
button.danger:hover { background: var(--bad); color: #fff; }
.danger-zone { border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }
.danger-zone > summary { cursor: pointer; font-weight: 600; color: var(--bad); }
.danger-zone .inline input { min-width: 240px; }

/* ---------- chronology ---------- */
.chronology { max-width: 900px; margin: 0 auto; padding: 24px; }
.chron-head { margin-bottom: 24px; }
.chron-head .case-line { font-size: 1.05rem; margin: 4px 0; }
.year-break {
  margin: 30px 0 12px; padding-bottom: 4px;
  border-bottom: 1px solid var(--line); font-size: 1.2rem;
}
.entry { display: grid; grid-template-columns: 120px 1fr; gap: 16px; margin-bottom: 20px; }
@media (max-width: 700px) { .entry { grid-template-columns: 1fr; gap: 4px; } }
.entry-date .day { font-weight: 600; }
.entry-body h3 { margin: 0 0 3px; font-size: 1rem; }
.entry-meta { margin: 0 0 6px; }
.entry-summary { margin: 0 0 6px; font-size: .9rem; }
.finding-line { margin: 0 0 4px; font-size: .9rem; }
.finding-line .label {
  display: inline-block; font-weight: 600; font-size: .75rem;
  text-transform: uppercase; letter-spacing: .03em; margin-right: 6px;
}
.finding-line .label.helps { color: var(--helps); }
.finding-line .label.hurts { color: var(--hurts); }
.undated-chron { font-size: .9rem; }
