:root {
  color-scheme: light;
  --canvas: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f5f7fb;
  --ink: #0f1f3d;
  --ink-2: #33415a;
  --muted: #64728a;
  --line: #dde3ec;
  --line-strong: #c3ccd9;
  --navy: #0f2747;
  --accent: #2f5bd3;
  --accent-hover: #2449b3;
  --accent-soft: #e9effd;
  --warn: #855000;
  --warn-soft: #fdf4e3;
  --danger: #a12828;
  --danger-soft: #fdecec;
  --info: #2449b3;
  --info-soft: #eaf0fd;
  --del-bg: #fde8e8;
  --del-ink: #9b1c1c;
  --ins-bg: #def4e6;
  --ins-ink: #0f5132;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow-sm: 0 1px 2px rgba(15, 39, 71, 0.06), 0 1px 3px rgba(15, 39, 71, 0.06);
  --shadow-md: 0 6px 18px rgba(15, 39, 71, 0.12);
  --sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --doc-font: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
}

/* ---------- Base ---------- */

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font: 14px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

button, input { font: inherit; color: inherit; }

button { cursor: pointer; }

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

.muted { color: var(--muted); }

.spacer { flex: 1; }

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

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  font-size: 13.5px;
  font-weight: 500;
  white-space: nowrap;
}

.btn:hover:not(:disabled) { border-color: var(--muted); }

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { border-color: var(--accent); background: var(--accent); color: #fff; }

.btn-primary:hover:not(:disabled) { border-color: var(--accent-hover); background: var(--accent-hover); }

.btn-quiet { border-color: transparent; background: transparent; color: var(--accent); }

.btn-quiet:hover:not(:disabled) { border-color: transparent; background: var(--accent-soft); }

.btn-danger { border-color: var(--danger); background: var(--danger); color: #fff; }

.btn.icon { width: 34px; padding: 0; font-size: 20px; line-height: 1; }

.btn.small { height: 28px; padding: 0 10px; font-size: 12.5px; }

.btn-large { height: 44px; padding: 0 28px; border-radius: 9px; font-size: 15px; font-weight: 600; }

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-2);
}

.segmented button {
  height: 28px;
  padding: 0 14px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink-2);
  font-weight: 500;
}

.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}


/* ---------- App bar, banner, toast ---------- */

.appbar {
  display: flex;
  align-items: center;
  flex: none;
  height: 52px;
  padding: 0 20px;
  background: var(--navy);
  color: #fff;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #fff;
  font-family: var(--doc-font);
  font-size: 18px;
  text-decoration: none;
}

.brand svg { width: 22px; height: 22px; }

.nav { display: flex; gap: 4px; margin-left: 32px; }

.nav button {
  height: 34px;
  padding: 0 16px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: rgba(255, 255, 255, 0.75);
  font-weight: 500;
}

.nav button:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }

.nav button[aria-current="page"] { background: #fff; color: var(--navy); font-weight: 600; }

.user-button { margin-left: auto; }

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: none;
  padding: 10px 20px;
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 500;
}

.banner .btn { margin-left: auto; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 50;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--navy);
  color: #fff;
  font-size: 13px;
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 0.15s, transform 0.15s;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Sign-in ---------- */

.auth {
  display: grid;
  place-items: center;
  flex: 1;
  overflow: auto;
  padding: 24px;
}

.auth-card { width: min(100%, 420px); text-align: center; }

.auth-card h1 { font-family: var(--doc-font); font-size: 28px; color: var(--navy); }

.auth-card > p { margin: 6px 0 20px; color: var(--muted); }

#clerkMount { display: flex; justify-content: center; }

/* ---------- Pages: upload and history ---------- */

.page { flex: 1; overflow: auto; }

.page-inner { width: min(100%, 900px); margin: 0 auto; padding: 48px 20px 64px; }

.hero h1 { font-family: var(--doc-font); font-size: 34px; color: var(--navy); }

.hero p { margin-top: 6px; color: var(--muted); font-size: 16px; }

.hero-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }

.upload-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
  margin-top: 32px;
}

.upload-grid.single { grid-template-columns: minmax(0, 560px); justify-content: center; }

.upload-card {
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.upload-card h2 { margin-bottom: 12px; font-size: 15px; font-weight: 600; }

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 36px 16px;
  border: 2px dashed var(--line-strong);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--ink-2);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

.drop-icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  margin-bottom: 4px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
}

.drop-icon svg { width: 24px; height: 24px; }

.dropzone small { color: var(--muted); }

.dropzone:hover,
.dropzone.is-dragging { border-color: var(--accent); background: var(--accent-soft); }

.dropzone:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.dropzone.has-file { padding: 14px 16px; }

.dropzone.has-file .drop-icon,
.dropzone.has-file small { display: none; }

.dropzone.has-file strong { font-weight: 500; }

.dropzone.has-error { border-color: var(--danger); }

.file-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  padding: 8px 8px 8px 12px;
  border: 1px solid #b9cdf6;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
}

.file-chip svg { flex: none; width: 18px; height: 18px; color: var(--accent); }

.file-chip-name { overflow: hidden; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }

.file-chip-size { flex: none; margin-left: auto; color: var(--muted); font-size: 12.5px; }

.field-error { margin-top: 8px; color: var(--danger); font-size: 13px; }

.setup-actions { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 28px; }

#historySearch {
  width: 260px;
  height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
}

.history-list {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.history-row { display: flex; align-items: center; gap: 8px; padding-right: 12px; }

.history-row + .history-row { border-top: 1px solid var(--line); }

.history-open {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: 1;
  min-width: 0;
  padding: 14px 18px;
  border: 0;
  background: transparent;
  text-align: left;
}

.history-open:hover strong { color: var(--accent); }

.history-open.is-loading { opacity: 0.5; }

.history-open svg { flex: none; width: 20px; height: 20px; color: var(--muted); }

.history-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }

.history-main strong { overflow: hidden; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }

.history-main span { color: var(--muted); font-size: 13px; }

.history-actions { display: flex; align-items: center; gap: 6px; flex: none; color: var(--danger); font-size: 13px; }

.history-delete {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
}

.history-delete:hover { background: var(--danger-soft); color: var(--danger); }

.empty { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 48px 16px; color: var(--muted); font-size: 15px; }

#historyMore { margin-top: 10px; }

/* ---------- Workspace (comparison and extracted text) ---------- */

.workspace {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 10px;
  min-height: 0;
  padding: 12px 20px 16px;
}

.bar { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; flex: none; }

.bar-title { min-width: 0; margin-left: 4px; }

.bar-title h1 {
  overflow: hidden;
  font-family: var(--doc-font);
  font-size: 19px;
  color: var(--navy);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stepper { display: flex; align-items: center; gap: 8px; }

#changePos { min-width: 104px; color: var(--ink-2); font-weight: 500; text-align: center; }

.notices { display: flex; flex-direction: column; gap: 6px; flex: none; }

.notices:empty { display: none; }

.notice {
  padding: 8px 12px;
  border-left: 3px solid var(--info);
  border-radius: var(--radius-sm);
  background: var(--info-soft);
  color: var(--ink-2);
  font-size: 13px;
}

.notice-warn { border-left-color: var(--warn); background: var(--warn-soft); }

.notice-danger { border-left-color: var(--danger); background: var(--danger-soft); }

.tabs { display: none; }

.ws-grid {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  flex: 1;
  min-height: 0;
}

.card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.pane-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex: none;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
}

.pane-head h2 { flex: none; font-family: var(--doc-font); font-size: 17px; color: var(--navy); }

.pane-file { overflow: hidden; color: var(--muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }

.pane-loc { flex: none; margin-left: auto; color: var(--muted); font-size: 12.5px; }

.doc-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 20px 28px 40px;
  font-family: var(--doc-font);
  font-size: 16px;
  line-height: 1.7;
}

.doc-empty { color: var(--muted); font-family: var(--sans); font-style: italic; }

/* ---------- List of changes ---------- */

.summary {
  flex: none;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  box-shadow: inset 4px 0 0 var(--info);
}

.summary.tone-warn { box-shadow: inset 4px 0 0 #d08a00; }

.summary.tone-danger { box-shadow: inset 4px 0 0 var(--danger); }

.summary h2 { font-family: var(--doc-font); font-size: 20px; line-height: 1.25; color: var(--navy); }

.summary p { margin-top: 4px; color: var(--muted); font-size: 13px; }

.changes-scroll { flex: 1; min-height: 0; overflow: auto; padding: 8px; }

.changes-scroll h3 {
  margin: 14px 10px 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.change-link {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  text-align: left;
}

.change-link:hover { background: var(--surface-2); }

.change-link.is-current { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }

.change-where { overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }

.change-link strong { font-weight: 600; }

.change-note { color: var(--ink-2); font-size: 12.5px; }

.graphic-change { margin: 0 10px 14px; }

.graphic-change > strong { display: block; margin-bottom: 6px; font-size: 13px; }

.graphic-compare { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.graphic-figure {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.graphic-figure figcaption { color: var(--muted); font-size: 11px; font-weight: 600; text-transform: uppercase; }

.graphic-figure img { align-self: center; max-width: 100%; max-height: 100px; background: #fff; }

.graphic-missing { padding: 20px 0; color: var(--muted); font-style: italic; text-align: center; }

/* ---------- Document text with changes ---------- */

.page-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 18px 0 10px;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.page-divider:first-child { margin-top: 0; }

.page-divider::after { content: ""; flex: 1; border-top: 1px dashed var(--line-strong); }

.clause,
.absent {
  max-width: 78ch;
  margin: 0 -12px;
  padding: 6px 12px;
  border-left: 2px solid transparent;
}

.clause + .clause,
.clause + .absent,
.absent + .clause { margin-top: 8px; }

.clause-changed { border-left-color: #d3dcea; }

.clause-heading { font-size: 1.04em; font-weight: 700; color: var(--navy); }

.clause-text { color: #23314a; }

.clause-footer { color: var(--muted); font-size: 0.85em; }

.absent {
  border: 1px dashed var(--line-strong);
  border-left: 3px solid var(--muted);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.88em;
  font-style: italic;
}

.chg {
  padding: 0 1px;
  border-radius: 3px;
  cursor: pointer;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.chg-del {
  background: var(--del-bg);
  color: var(--del-ink);
  text-decoration: line-through rgba(155, 28, 28, 0.7) 1.5px;
}

.chg-ins {
  background: var(--ins-bg);
  color: var(--ins-ink);
  text-decoration: underline rgba(15, 81, 50, 0.6) 1.5px;
  text-underline-offset: 3px;
}

.gap {
  display: inline-grid;
  place-items: center;
  width: 1.05em;
  height: 1.05em;
  margin: 0 2px;
  border-radius: 3px;
  font-family: var(--sans);
  font-size: 0.85em;
  font-weight: 700;
  line-height: 1;
  vertical-align: 0.05em;
  cursor: pointer;
}

.gap-ins { border: 1px solid rgba(15, 81, 50, 0.45); background: var(--ins-bg); color: var(--ins-ink); }

.gap-del { border: 1px solid rgba(155, 28, 28, 0.45); background: var(--del-bg); color: var(--del-ink); }

.chg.is-current,
.gap.is-current { outline: 2px solid var(--accent); outline-offset: 2px; }

/* OCR text that may be misread */
.ocr-low {
  text-decoration: underline dotted #c27c00 2px;
  text-underline-offset: 4px;
}

/* ---------- Source page images ---------- */

.page-image { margin: 0 0 20px; }

.page-frame {
  position: relative;
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  box-shadow: var(--shadow-sm);
}

.page-frame img { display: block; width: 100%; height: 100%; user-select: none; }

.page-image figcaption { margin-top: 4px; color: var(--muted); font-family: var(--sans); font-size: 11px; }

.ocr-box,
.graphic-box,
.ocr-line-box { position: absolute; border-radius: 2px; cursor: pointer; }

.ocr-del { background: rgba(220, 38, 38, 0.14); box-shadow: inset 0 -2px 0 rgba(161, 40, 40, 0.85); }

.ocr-ins { background: rgba(22, 163, 74, 0.14); box-shadow: inset 0 -2px 0 rgba(16, 85, 47, 0.85); }

.ocr-box.is-current { outline: 2px solid var(--accent); outline-offset: 2px; }

.graphic-box { outline: 2px dashed rgba(47, 91, 211, 0.75); outline-offset: 2px; cursor: default; }

/* ---------- Extracted text ---------- */

.extract-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; flex: 1; min-height: 0; }

.extract-grid[data-layout="split"] { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }

.extract-text { white-space: pre-wrap; }

.extract-page + .page-divider { margin-top: 24px; }

.ocr-line { cursor: pointer; }

.ocr-line:hover { background: var(--accent-soft); }

.ocr-line.is-focus { background: #fff1a8; }

.ocr-line-box:hover { background: rgba(47, 91, 211, 0.1); outline: 1px solid rgba(47, 91, 211, 0.5); }

.ocr-line-box.is-focus { background: rgba(255, 216, 77, 0.35); outline: 2px solid #b08900; }

/* ---------- Narrow screens ---------- */

@media (max-width: 1099px) {
  .workspace { padding: 10px 12px 12px; }

  .ws-grid { grid-template-columns: minmax(0, 1fr); }

  .tabs {
    display: flex;
    flex: none;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--surface-2);
  }

  .tabs button {
    flex: 1;
    height: 32px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--ink-2);
    font-weight: 500;
  }

  .tabs [aria-selected="true"] { background: var(--surface); color: var(--accent); box-shadow: var(--shadow-sm); }

  .workspace[data-mobile-view="panel"] .ws-grid > :not([data-side="panel"]),
  .workspace[data-mobile-view="old"] .ws-grid > :not([data-side="old"]),
  .workspace[data-mobile-view="new"] .ws-grid > :not([data-side="new"]) { display: none; }

  .extract-grid[data-layout="split"] { grid-template-columns: minmax(0, 1fr); grid-template-rows: 1fr 1fr; }

  .doc-scroll { padding: 16px 18px 32px; }
}

@media (max-width: 720px) {
  .appbar { padding: 0 12px; }

  .brand span { display: none; }

  .nav { margin-left: 12px; }

  .nav button { padding: 0 12px; font-size: 13px; }

  .page-inner { padding: 28px 16px 40px; }

  .hero h1 { font-size: 26px; }

  .hero-row { flex-direction: column; align-items: stretch; }

  .upload-grid { grid-template-columns: minmax(0, 1fr); }

  .setup-actions { flex-direction: column-reverse; }

  .setup-actions .btn { width: 100%; }

  #historySearch { width: 100%; }

  .bar-title { flex-basis: 100%; order: 3; }
}

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