/* ==========================================================================
   LIMS Raw Data Viewer — "lab ledger" system
   Warm paper + ink, a serif/mono pairing, and a deliberate visual split
   between "what we store" (crisp, our own UI) and "the original sheet"
   (framed like a facsimile of someone else's document). No external
   fonts/CDNs — everything below is system-font stacks + native CSS.
   ========================================================================== */

:root {
  color-scheme: light dark;

  --hue: 55;                 /* warm paper hue, shared across tints */
  --bg:        oklch(96.5% 0.014 var(--hue));
  --surface:   oklch(99%   0.008 var(--hue));
  --surface-2: oklch(93.5% 0.016 var(--hue));
  --line:      oklch(85%   0.020 var(--hue));
  --line-soft: oklch(90%   0.016 var(--hue));
  --ink:       oklch(24%   0.020 var(--hue));
  --ink-dim:   oklch(46%   0.022 var(--hue));
  --ink-faint: oklch(62%   0.018 var(--hue));

  --accent:      oklch(47% 0.135 38);   /* rust — brand / primary actions */
  --accent-ink:  oklch(98% 0.010 38);
  --accent-soft: oklch(91% 0.045 38);

  --good:      oklch(52% 0.11 150);
  --good-soft: oklch(91% 0.05 150);
  --warn:      oklch(58% 0.13 75);
  --warn-soft: oklch(92% 0.06 80);
  --bad:       oklch(52% 0.16 25);
  --bad-soft:  oklch(92% 0.06 25);

  --font-display: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, "URW Palladio L", Georgia, serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", "Liberation Mono", monospace;

  --radius: 3px;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        oklch(19% 0.014 var(--hue));
    --surface:   oklch(23% 0.016 var(--hue));
    --surface-2: oklch(27% 0.018 var(--hue));
    --line:      oklch(33% 0.020 var(--hue));
    --line-soft: oklch(29% 0.018 var(--hue));
    --ink:       oklch(92% 0.012 var(--hue));
    --ink-dim:   oklch(72% 0.016 var(--hue));
    --ink-faint: oklch(54% 0.016 var(--hue));

    --accent:      oklch(68% 0.13 40);
    --accent-ink:  oklch(14% 0.02 40);
    --accent-soft: oklch(30% 0.06 40);

    --good:      oklch(72% 0.13 150);
    --good-soft: oklch(28% 0.06 150);
    --warn:      oklch(76% 0.13 80);
    --warn-soft: oklch(30% 0.07 80);
    --bad:       oklch(72% 0.16 25);
    --bad-soft:  oklch(30% 0.08 25);
  }
}

:root[data-theme="light"] {
  --bg:        oklch(96.5% 0.014 var(--hue));
  --surface:   oklch(99%   0.008 var(--hue));
  --ink:       oklch(24%   0.020 var(--hue));
}
:root[data-theme="dark"] {
  --bg:        oklch(19% 0.014 var(--hue));
  --surface:   oklch(23% 0.016 var(--hue));
  --ink:       oklch(92% 0.012 var(--hue));
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--accent-soft); color: var(--ink); }

code { font-family: var(--font-mono); }

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ------------------------------------------------------------- topbar --- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  padding: 0.7rem clamp(1rem, 2vw, 1.75rem);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: baseline; gap: 0.75rem; min-width: 0; }

.brand-tag {
  flex: none;
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.05em;
  color: var(--accent-ink);
  background: var(--accent);
  padding: 0.25em 0.5em 0.2em;
  border-radius: var(--radius);
  white-space: nowrap;
}

#count { font-variant-numeric: tabular-nums; }

.brand h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 1.25rem;
  letter-spacing: 0.005em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand h1 em { font-style: italic; color: var(--ink-dim); font-weight: 400; }

.modeswitch {
  flex: none;
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}
.modeswitch button {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--ink-dim);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.18s var(--ease), color 0.18s var(--ease);
}
.modeswitch button:hover { color: var(--ink); }
.modeswitch button[aria-selected="true"] {
  background: var(--ink);
  color: var(--surface);
}
@media (prefers-color-scheme: dark) {
  .modeswitch button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); }
}

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

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(240px, 300px) 1fr;
}

.sidebar {
  border-right: 1px solid var(--line);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  padding: 0.85rem;
  border-bottom: 1px solid var(--line-soft);
}
.filter-label {
  display: block;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 0.35rem;
}
#filter {
  width: 100%;
  font: inherit;
  font-size: 0.9rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
}
#filter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

#plant-filter {
  width: 100%;
  font: inherit;
  font-size: 0.85rem;
  padding: 0.4rem 0.55rem;
  margin-top: 0.5rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
}
#plant-filter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.record-list {
  flex: 1;
  overflow-y: auto;
}

.list-empty {
  padding: 1.25rem 0.9rem;
  color: var(--ink-faint);
  font-size: 0.85rem;
}
.list-empty-error { color: var(--bad); }

.record-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--line-soft);
  cursor: pointer;
  position: relative;
}
.record-row:hover { background: var(--surface-2); }
.record-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.record-row.is-selected {
  background: var(--accent-soft);
}
.record-row.is-selected::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--accent);
}

.record-ar {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 600;
  flex: none;
  color: var(--ink);
}
.record-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink-dim);
  font-size: 0.82rem;
}

/* status pill, shared by sidebar rows + identity + records table */
.pill {
  flex: none;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0.2em 0.55em;
  border-radius: 999px;
  line-height: 1.5;
}
.pill-done    { background: var(--good-soft); color: var(--good); }
.pill-pending { background: var(--warn-soft); color: var(--warn); }
.pill-error   { background: var(--bad-soft);  color: var(--bad); }
.pill-unknown { background: var(--surface-2); color: var(--ink-faint); }

/* ---------------------------------------------------------------- main --- */

.main {
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  padding: clamp(0.9rem, 2vw, 1.5rem);
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
}

.view[hidden] { display: none; }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 1.1rem; }

.hint {
  color: var(--ink-faint);
  font-size: 0.92rem;
  max-width: 46ch;
  margin: auto;
  text-align: center;
  padding: 2.5rem 1rem;
}
.hint-error { color: var(--bad); }

/* ------------------------------------------------------------ identity --- */

.identity {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  animation: rise 0.22s var(--ease);
}
.identity:has(.identity-head) { padding: 0; }

.identity-head {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.8rem 1.1rem;
  border-bottom: 1px solid var(--line-soft);
  background: var(--surface-2);
  border-radius: var(--radius) var(--radius) 0 0;
}
.identity-ar {
  font-family: var(--font-mono);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.identity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0;
}
.identity-grid-meta {
  border-top: 1px dashed var(--line);
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
}

.field {
  padding: 0.65rem 1.1rem;
  border-right: 1px solid var(--line-soft);
  border-bottom: 1px solid var(--line-soft);
  min-width: 0;
}
.field .k {
  display: block;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 0.2rem;
}
.field .v {
  display: block;
  font-size: 0.88rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.field-meta .v { font-family: var(--font-mono); font-size: 0.8rem; color: var(--ink-dim); }
.field .void { color: var(--ink-faint); }

/* -------------------------------------------------------------- compare --- */

.compare {
  flex: 1;
  min-height: 420px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.1rem;
}

.pane {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-radius: var(--radius);
  animation: rise 0.24s var(--ease);
}

.pane-store {
  background: var(--surface);
  border: 1px solid var(--line);
}

.pane-head {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--line-soft);
  flex-wrap: wrap;
}
.pane-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 0.98rem;
  font-weight: 600;
}
.pane-sub {
  font-size: 0.72rem;
  color: var(--ink-faint);
}
.pane-sub code { font-size: 0.7rem; }

.stored, .records-table-wrap {
  flex: 1;
  overflow: auto;
  padding: 0.75rem;
}
.pane-empty {
  color: var(--ink-faint);
  font-size: 0.85rem;
  padding: 1rem 0.25rem;
}

.table-block { margin-bottom: 1rem; }
.table-block:last-child { margin-bottom: 0; }
.table-block-head {
  display: flex;
  justify-content: space-between;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--ink-dim);
  margin-bottom: 0.3rem;
  padding: 0 0.1rem;
}
.table-count { font-weight: 400; color: var(--ink-faint); }
.table-scroll { overflow-x: auto; border: 1px solid var(--line-soft); border-radius: var(--radius); }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  white-space: nowrap;
}
.data-table th, .data-table td {
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--line-soft);
  text-align: left;
}
.data-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-dim);
  z-index: 1;
}
.data-table tbody tr:hover { background: var(--surface-2); }
.data-table .mono { font-family: var(--font-mono); font-size: 0.78rem; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .void { color: var(--ink-faint); }

/* the original sheet is framed like a facsimile of someone else's document:
   a recessed, slightly darker mat with a crisp inset "page" — a deliberate
   contrast against our own clean panels either side of it. */
.pane-sheet {
  background: var(--surface-2);
  border: 1px solid var(--line);
}
.facsimile {
  flex: 1;
  min-height: 0;
  padding: 0.75rem;
  display: flex;
}
.sheet-frame {
  flex: 1;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: #fff;
  box-shadow: 0 1px 0 var(--line), 0 6px 16px -10px oklch(0% 0 0 / 0.35);
}

.compare-list { min-height: 520px; }

@keyframes rise {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .identity, .pane { animation: none; }
  .modeswitch button { transition: none; }
}

/* ---------------------------------------------------------- responsive --- */

@media (max-width: 900px) {
  .compare { grid-template-columns: 1fr; }
  .facsimile { min-height: 360px; }
}

@media (max-width: 700px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--line); max-height: 40vh; }
  .topbar { flex-wrap: wrap; }
  .brand h1 { font-size: 1.05rem; }
}

/* --------------------------------------------------- header + tests --- */

.store-block { margin-bottom: 1.25rem; }
.store-block h3 { margin: 0 0 .4rem; font-size: .95rem; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .15rem .75rem; margin: 0 0 .5rem; }
.kv dt { font-weight: 600; opacity: .75; }
.kv dd { margin: 0; }
.test-card { border-left: 3px solid var(--accent, #6a7bd6); padding-left: .75rem; }
table.audit { font-size: .85em; opacity: .8; }

.labels { display: flex; flex-wrap: wrap; gap: .35rem; }
.chip { font-size: .8rem; padding: .1rem .5rem; border-radius: 999px; background: rgba(127,127,127,.15); }

/* plant tag: a .chip reused in the record list + records table */
.plant-tag {
  flex: none;
  font-family: var(--font-mono);
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--ink-dim);
}
.table-block-head { font-weight: 600; opacity: .7; font-size: .8rem; text-transform: capitalize; margin: .5rem 0 .25rem; }

/* ------------------------------------------------- toolbar buttons + zoom --- */

.iconbtn {
  flex: none;
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink-dim);
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.18s var(--ease), color 0.18s var(--ease);
}
.iconbtn:hover { color: var(--ink); }
.iconbtn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.iconbtn[aria-pressed="true"] { background: var(--ink); color: var(--surface); }
@media (prefers-color-scheme: dark) {
  .iconbtn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
}

.zoom {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}
.zoom button {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-dim);
  font: inherit;
  font-size: 0.72rem;
  line-height: 1;
  padding: 0.22rem 0.45rem;
  border-radius: var(--radius);
  cursor: pointer;
}
.zoom button:hover { color: var(--ink); background: var(--surface-2); }
.zoom-val {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--ink-dim);
  min-width: 3.5ch;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* zoomed iframes can exceed their frame — let the mat scroll to contain them */
.facsimile { overflow: auto; }

/* -------------------------------------------------- collapse / maximize --- */

#app.nav-collapsed .layout { grid-template-columns: 1fr; }
#app.nav-collapsed .sidebar { display: none; }

#app.compare-max .layout { grid-template-columns: 1fr; }
#app.compare-max .sidebar,
#app.compare-max .identity { display: none; }
#app.compare-max .main { padding: 0.5rem; }

.store-heading { margin: .9rem 0 .3rem; font-size: .82rem; font-weight: 700; letter-spacing: .01em; color: var(--ink); }
.store-heading:first-child { margin-top: 0; }

.final-result { margin: .3rem 0 .5rem; font-size: .85rem; }
.final-result b { color: var(--good); }

.parser-toggle { margin-left: auto; }
