:root {
  --paper: #f4f2ec;
  --surface: #fffefb;
  --surface-2: #f9f7f2;
  --ink: #14171a;
  --ink-2: #414850;
  --muted: #767d84;
  --line: #e2ded3;
  --line-strong: #cfc9ba;
  --accent: #0a5c47;
  --accent-soft: #e6f0eb;
  --danger: #a33a2b;
  --danger-soft: #fbebe7;
  --warn: #8a6114;
  --warn-soft: #fbf3e2;
  --radius: 10px;
  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

/* Author display rules must not defeat the hidden attribute. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }

.wrap { width: min(1180px, calc(100% - 40px)); margin: 0 auto; }

/* ---------- top bar ---------- */

.topbar { border-bottom: 1px solid var(--line); background: var(--paper); }
.topbar-inner {
  display: flex; align-items: center; justify-content: space-between;
  height: 64px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--ink); color: var(--surface);
  display: grid; place-items: center;
  font-family: var(--mono); font-size: 17px; font-weight: 600;
}
.brand h1 { font-size: 16px; line-height: 1.1; }
.brand p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }

.local-badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid #cfe0d8;
  padding: 5px 10px; border-radius: 999px;
}
.local-badge svg { width: 14px; height: 14px; }

/* ---------- layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
  gap: 20px;
  padding: 24px 0 8px;
  align-items: start;
}

/* A loaded PDF gets its own column between input and result. */
body.has-doc .wrap { width: min(1400px, calc(100% - 40px)); }
body.has-doc .layout {
  grid-template-columns: minmax(280px, 0.78fr) minmax(320px, 0.92fr) minmax(0, 1.15fr);
  grid-template-areas: "input doc output";
}
body.has-doc .input-panel { grid-area: input; }
body.has-doc .doc-panel { grid-area: doc; }
body.has-doc .output-panel { grid-area: output; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 1px 0 rgba(20, 23, 26, 0.03);
}

.panel-head {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.panel-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-2); }

.output-head { flex-wrap: wrap; }
.output-actions { margin-left: auto; display: flex; gap: 8px; }

/* ---------- tabs ---------- */

.tabs, .tabs-mini { display: flex; gap: 2px; }
.tabs-mini { margin-left: auto; }

.tab, .mini-tab {
  font: inherit; font-size: 13px;
  border: 0; background: transparent; color: var(--muted);
  padding: 6px 10px; border-radius: 6px; cursor: pointer;
}
.tab:hover, .mini-tab:hover { color: var(--ink); background: var(--surface-2); }
.tab.is-active, .mini-tab.is-active {
  color: var(--ink); background: var(--surface-2);
  box-shadow: inset 0 0 0 1px var(--line);
}

/* ---------- input ---------- */

.input-panel > #mode-file, .input-panel > #mode-paste { padding: 18px; }

.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; text-align: center;
  min-height: 190px; padding: 24px;
  border: 1.5px dashed var(--line-strong);
  border-radius: 8px;
  background: var(--surface-2);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.is-over {
  border-color: var(--accent); background: var(--accent-soft); outline: none;
}
.dz-icon { width: 40px; height: 40px; color: var(--accent); margin-bottom: 4px; }
.dropzone strong { font-size: 15px; }
.dropzone span { font-size: 13px; color: var(--muted); }

.fineprint { margin: 12px 0 0; font-size: 12px; line-height: 1.55; color: var(--muted); }

textarea {
  width: 100%; min-height: 190px; resize: vertical;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.6;
  color: var(--ink); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 12px;
}
textarea:focus { outline: none; border-color: var(--accent); }

.row { display: flex; gap: 8px; margin-top: 10px; }

.btn {
  font: inherit; font-size: 13px; font-weight: 500;
  padding: 7px 11px; border-radius: 6px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--ink-2); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--ink); border-color: var(--ink); color: var(--surface); }
.btn-primary:hover { background: #000; }
.btn-ghost { background: transparent; }

/* ---------- status / alerts ---------- */

.status {
  display: flex; align-items: center; gap: 10px;
  margin: 0 18px 18px; padding: 10px 12px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px;
  font-size: 13px; color: var(--ink-2);
}
.spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--line-strong); border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.alert {
  margin: 0 18px 18px; padding: 10px 12px; border-radius: 8px; font-size: 13px;
}
.alert-error { background: var(--danger-soft); border: 1px solid #eed3cb; color: var(--danger); }

/* ---------- result meta ---------- */

.result-meta {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  margin: 0 18px 14px;
}
.bank-pill {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 13px; font-weight: 600;
  padding: 5px 10px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line);
}
.bank-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.chip {
  font-size: 12px; color: var(--ink-2);
  padding: 4px 8px; border-radius: 5px;
  background: var(--surface-2); border: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.chip.is-ok { color: var(--accent); border-color: #cfe0d8; background: var(--accent-soft); }
.chip.is-warn { color: var(--warn); border-color: #ebdcbb; background: var(--warn-soft); }

/* ---------- stats ---------- */

.stats {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px; margin: 0 18px 18px;
  background: var(--line); border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.stat { background: var(--surface); padding: 12px 14px; }
.stat .k { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.stat .v {
  font-size: 19px; font-weight: 600; letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums; margin-top: 2px;
}
.stat .v small { font-size: 12px; font-weight: 500; color: var(--muted); margin-left: 3px; }
.stat.accent .v { color: var(--accent); }

/* ---------- checks ---------- */

.checks { margin: 0 18px 18px; }
.checks h3 {
  font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); margin-bottom: 8px;
}
.check {
  display: flex; gap: 9px; align-items: flex-start;
  font-size: 12.5px; padding: 6px 0;
  border-bottom: 1px dotted var(--line);
}
.check:last-child { border-bottom: 0; }
.check .mark {
  width: 16px; height: 16px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-size: 10px; font-weight: 700;
  margin-top: 1px;
}
.check.ok .mark { background: var(--accent-soft); color: var(--accent); }
.check.bad .mark { background: var(--warn-soft); color: var(--warn); }
.check .txt { color: var(--ink-2); }
.check .delta { color: var(--muted); font-family: var(--mono); font-size: 11.5px; }

/* ---------- output ---------- */

.view { min-height: 420px; }
pre.raw {
  margin: 0; padding: 16px 18px;
  font-family: var(--mono); font-size: 12px; line-height: 1.62;
  overflow: auto; max-height: 68vh;
  tab-size: 2;
  color: var(--ink-2); white-space: pre-wrap;
}

/* collapsible JSON tree */
.json-toolbar {
  display: flex; align-items: center; gap: 14px;
  padding: 9px 18px 0;
}
.linkbtn {
  font: inherit; font-size: 11.5px; color: var(--muted);
  background: none; border: 0; padding: 0; cursor: pointer;
  text-decoration: underline; text-underline-offset: 2px;
}
.linkbtn:hover { color: var(--ink); }
.json-note { margin-left: auto; font-size: 11px; color: var(--muted); }

.json {
  padding: 10px 18px 18px;
  font-family: var(--mono); font-size: 12px; line-height: 1.65;
  overflow: auto; max-height: 66vh;
  font-variant-numeric: tabular-nums;
}
.jrow { white-space: pre-wrap; overflow-wrap: anywhere; }
.jbody {
  margin-left: 14px;
  padding-left: 9px;
  border-left: 1px solid #ece7da;
}
summary.jrow { cursor: pointer; list-style: none; }
summary.jrow::-webkit-details-marker { display: none; }
summary.jrow::before {
  content: "▸"; display: inline-block; width: 14px;
  color: var(--ink-2); font-size: 11px; line-height: 1;
  transform: translateY(-1px);
}
details[open] > summary.jrow::before { content: "▾"; }
summary.jrow:hover { background: var(--surface-2); }

.jkey { color: #0a5c47; }
.jstr { color: #8a4b1f; }
.jnum { color: #1f4e8c; }
.jbool { color: #7a2f6b; }
.jnull { color: var(--muted); }
.jp, .jbrace, .jclose { color: var(--ink-2); }
.jhint { color: var(--muted); }
details[open] > summary .jhint { display: none; }
.placeholder { color: var(--muted); font-family: var(--sans); font-size: 13px; }

.tok-key { color: #0a5c47; }
.tok-str { color: #8a4b1f; }
.tok-num { color: #1f4e8c; }
.tok-bool { color: #7a2f6b; }
.tok-null { color: var(--muted); }

.table-wrap { overflow: auto; max-height: 68vh; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
thead th {
  position: sticky; top: 0; z-index: 1;
  text-align: left; font-weight: 600; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
  background: var(--surface); border-bottom: 1px solid var(--line);
  padding: 9px 12px; white-space: nowrap;
}
tbody td { padding: 8px 12px; border-bottom: 1px solid #efece4; vertical-align: top; }
tbody tr:hover { background: var(--surface-2); }
td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.date { white-space: nowrap; color: var(--ink-2); font-variant-numeric: tabular-nums; }
td.desc { min-width: 190px; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
td.credit { color: var(--accent); }
.tag {
  display: inline-block; font-size: 10.5px; letter-spacing: 0.02em;
  padding: 2px 6px; border-radius: 4px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2);
  white-space: nowrap;
}

.panel-foot {
  border-top: 1px solid var(--line);
  padding: 9px 18px; font-size: 11.5px; color: var(--muted);
}

.page-foot { padding: 14px 0 40px; }
.page-foot p { margin: 0; font-size: 12px; color: var(--muted); max-width: 70ch; }
code { font-family: var(--mono); font-size: 0.92em; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }

/* ---------- document preview ---------- */

.doc-panel { display: flex; flex-direction: column; min-width: 0; }

.doc-pages {
  padding: 12px;
  background: var(--surface-2);
  display: flex; flex-direction: column; gap: 10px;
  overflow: auto;
  max-height: calc(100vh - 150px);
  border-radius: 0 0 var(--radius) var(--radius);
}
.doc-page {
  position: relative;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 3px;
  overflow: hidden;
  cursor: zoom-in;
  box-shadow: 0 1px 2px rgba(20, 23, 26, 0.05);
}
.doc-page canvas { display: block; width: 100%; height: auto; }
.doc-page:hover { border-color: var(--line-strong); }
.doc-pageno {
  position: absolute; right: 6px; bottom: 6px;
  font-size: 10px; color: var(--muted);
  background: rgba(255, 255, 255, 0.9);
  border-radius: 3px; padding: 1px 4px;
}
.doc-hint { margin-left: auto; font-size: 11px; color: var(--muted); }
.doc-img { display: block; width: 100%; border-radius: 3px; }

.doc-overlay {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(20, 23, 26, 0.75);
  display: grid; place-items: center;
  padding: 24px; cursor: zoom-out;
}
.doc-overlay canvas {
  max-width: min(92vw, 920px); max-height: 92vh;
  width: auto; height: auto; background: #fff;
  box-shadow: 0 14px 48px rgba(0, 0, 0, 0.45);
}

@media (max-width: 1150px) {
  body.has-doc .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "input" "output" "doc";
  }
  /* Film strip instead of a tall column. */
  .doc-pages {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    max-height: none;
  }
  .doc-page { flex: 0 0 auto; width: min(76vw, 420px); }
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .view { min-height: 0; }
  pre.json { max-height: 52vh; }
}

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
}
