/* ==========================================================================
   Kalun Ho — forecast ledger
   Palette: cool chart stock, one cool data ink, one warm data ink, one alarm.
   hit / miss is a teal <-> orange split so it survives red-green colourblindness.
   pending / partial / void get NO extra hue — they are distinguished by form
   (outline, hatch, strikethrough). overdue is the only solid-filled chip.
   ========================================================================== */

:root {
  --paper:     #E8EBEE;
  --ink:       #141A21;
  --graphite:  #5C6773;
  --signal:    #0E6F86;
  --ember:     #A9490F;
  --alarm:     #97246B;

  /* derived */
  --panel:      #F3F5F6;
  --rule:       rgba(20, 26, 33, 0.14);
  --rule-soft:  rgba(20, 26, 33, 0.07);
  --signal-wash: rgba(14, 111, 134, 0.10);
  --ember-wash:  rgba(169, 73, 15, 0.10);
  --alarm-wash:  rgba(151, 36, 107, 0.10);
  --focus:      #0E6F86;

  --mono: ui-monospace, "SF Mono", SFMono-Regular, "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --r-chip: 3px;
  --r-panel: 2px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper:     #10151A;
    --ink:       #DFE4E8;
    --graphite:  #8B98A5;
    --signal:    #4FB8D0;
    --ember:     #E58A4A;
    --alarm:     #DE72B0;

    --panel:      #171E25;
    --rule:       rgba(223, 228, 232, 0.16);
    --rule-soft:  rgba(223, 228, 232, 0.08);
    --signal-wash: rgba(79, 184, 208, 0.14);
    --ember-wash:  rgba(229, 138, 74, 0.14);
    --alarm-wash:  rgba(222, 114, 176, 0.14);
    --focus:      #4FB8D0;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 16px/1.5 var(--sans);
  /* faint plotter grid — the page is chart stock, not a document */
  background-image:
    linear-gradient(var(--rule-soft) 1px, transparent 1px),
    linear-gradient(90deg, var(--rule-soft) 1px, transparent 1px);
  background-size: 64px 64px, 64px 64px;
}

.wrap { max-width: 1140px; margin: 0 auto; padding: 0 20px; }

a { color: var(--signal); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 1px;
}

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 20;
  background: var(--ink); color: var(--paper);
  padding: 10px 16px; font: 600 14px/1 var(--sans);
}
.skip-link:focus { left: 8px; top: 8px; }

/* ---------- type roles ------------------------------------------------ */

.readout {
  font-family: var(--mono);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  letter-spacing: -0.02em;
}
.datum {
  font-family: var(--mono);
  font-weight: 400;
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  color: var(--graphite);
}
.label {
  font: 600 0.6875rem/1.3 var(--sans);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.claim { max-width: 68ch; }

h1, h2, h3 { font-weight: 600; line-height: 1.15; letter-spacing: -0.015em; margin: 0; }
h1 { font-size: clamp(1.5rem, 4vw, 2.25rem); }
h2 { font-size: clamp(1.15rem, 2.4vw, 1.4rem); }
h3 { font-size: 1rem; }

.eyebrow {
  font: 600 0.6875rem/1.3 var(--sans);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--graphite);
  margin: 0 0 8px;
}

/* ---------- masthead / footer ----------------------------------------- */

.masthead {
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 10;
}
.masthead-inner {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; padding-block: 12px;
}
.brand { text-decoration: none; color: inherit; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.brand-name { font: 600 1.0625rem/1 var(--sans); letter-spacing: -0.01em; }
.brand-sub {
  font: 600 0.625rem/1 var(--sans); letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--graphite);
}
.nav { list-style: none; display: flex; gap: 4px; margin: 0; padding: 0; flex-wrap: wrap; }
.nav a {
  display: block; padding: 6px 10px; border-radius: var(--r-chip);
  font: 600 0.8125rem/1.3 var(--sans); text-decoration: none; color: var(--graphite);
}
.nav a:hover { color: var(--ink); background: var(--rule-soft); }
.nav a[aria-current="page"] { color: var(--ink); background: var(--signal-wash); }

.footer {
  border-top: 1px solid var(--rule);
  margin-top: 72px; padding-block: 28px 40px;
  color: var(--graphite); font-size: 0.875rem;
}
.footer p { margin: 0 0 8px; max-width: 68ch; }
.footer-meta { font-family: var(--mono); font-size: 0.75rem; }

main { padding-block: 32px 0; }
.view > * + * { margin-top: 32px; }

/* ---------- status vocabulary ----------------------------------------- */
/* Colour is never the only signal: every chip carries its word. */

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 7px; border-radius: var(--r-chip);
  font: 600 0.6875rem/1.5 var(--sans);
  letter-spacing: 0.07em; text-transform: uppercase;
  border: 1.5px solid currentColor; white-space: nowrap;
}
.chip--hit     { color: var(--signal); background: var(--signal-wash); }
.chip--miss    { color: var(--ember);  background: var(--ember-wash); }
.chip--pending { color: var(--graphite); background: transparent; }
.chip--partial {
  color: var(--signal);
  background: repeating-linear-gradient(
    -45deg, var(--signal-wash) 0 4px, transparent 4px 8px);
}
.chip--void { color: var(--graphite); background: transparent; text-decoration: line-through; }
/* the only solid fill in the system — identifiable by weight, not hue alone */
.chip--overdue {
  color: var(--paper); background: var(--alarm); border-color: var(--alarm);
}

.rule-hit     { --row-rule: var(--signal); }
.rule-miss    { --row-rule: var(--ember); }
.rule-pending { --row-rule: var(--graphite); }
.rule-partial { --row-rule: var(--signal); }
.rule-void    { --row-rule: var(--rule); }
.rule-overdue { --row-rule: var(--alarm); }

/* ---------- panels ----------------------------------------------------- */

.panel {
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: var(--r-panel);
  padding: 20px;
}

/* ---------- scorecard -------------------------------------------------- */

.scorecard-head { margin-bottom: 20px; }
.scorecard-head p { color: var(--graphite); margin: 10px 0 0; }

.stats {
  display: grid; gap: 1px;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  background: var(--rule);
  border: 1px solid var(--rule);
  border-radius: var(--r-panel);
  overflow: hidden;
}
.stat { background: var(--panel); padding: 18px 18px 16px; }
.stat dt {
  font: 600 0.6875rem/1.3 var(--sans);
  letter-spacing: 0.08em; text-transform: uppercase; color: var(--graphite);
  margin-bottom: 8px;
}
.stat dd { margin: 0; }
.stat-value {
  font-family: var(--mono); font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: clamp(2rem, 6vw, 3.25rem); line-height: 1;
  letter-spacing: -0.035em;
  display: block;
}
.stat-unit { font-size: 0.5em; letter-spacing: 0; color: var(--graphite); }
.stat-note {
  display: block; margin-top: 8px;
  font-family: var(--mono); font-size: 0.75rem; color: var(--graphite);
  font-variant-numeric: tabular-nums;
}
/* shared by the home scorecard tile and the track-record metric tile */
.is-alarm { background: var(--alarm-wash); }
.is-alarm dt,
.is-alarm a,
.is-alarm .stat-value,
.is-alarm .metric-value,
.is-alarm .stat-note,
.is-alarm .metric-note { color: var(--alarm); }

/* ---------- episode rows ----------------------------------------------- */

.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; margin-bottom: 12px;
}

.ep-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
.ep-row { border-bottom: 1px solid var(--rule); }
.ep-row a {
  display: grid; gap: 4px 16px; padding: 14px 4px;
  grid-template-columns: 1fr auto;
  text-decoration: none; color: inherit;
}
.ep-row a:hover { background: var(--rule-soft); }
.ep-row a:hover .ep-title { text-decoration: underline; text-underline-offset: 2px; }
.ep-title { font-weight: 600; }
.ep-meta { grid-column: 1; }
.ep-count { grid-column: 2; grid-row: 1 / span 2; align-self: center; text-align: right; }

/* ---------- instrument panel (calibration + summary) ------------------- */

.instrument {
  display: grid; gap: 24px;
  grid-template-columns: minmax(0, 520px) minmax(240px, 1fr);
  align-items: start;
}
@media (max-width: 860px) { .instrument { grid-template-columns: 1fr; } }

.plot-frame { background: var(--panel); border: 1px solid var(--rule); border-radius: var(--r-panel); padding: 16px; }
.plot-frame svg { display: block; width: 100%; height: auto; }
.plot-caption { margin: 12px 2px 0; font-size: 0.8125rem; color: var(--graphite); }

.metrics { display: grid; gap: 1px; background: var(--rule); border: 1px solid var(--rule); border-radius: var(--r-panel); overflow: hidden; }
.metric { background: var(--panel); padding: 16px 18px; }
.metric dt {
  font: 600 0.6875rem/1.3 var(--sans); letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--graphite); margin-bottom: 6px;
}
.metric dd { margin: 0; }
.metric-value {
  font-family: var(--mono); font-weight: 700; font-variant-numeric: tabular-nums;
  font-size: 2rem; line-height: 1; letter-spacing: -0.03em;
}
.metric-note { display: block; margin-top: 6px; font-family: var(--mono); font-size: 0.75rem; color: var(--graphite); }

/* ---------- filters ---------------------------------------------------- */

.filters { display: grid; gap: 16px; }
.filter-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.filter-group > .label { color: var(--graphite); margin-right: 4px; }

.toggle {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; border-radius: var(--r-chip);
  border: 1.5px solid var(--rule); background: transparent; color: var(--graphite);
  font: 600 0.6875rem/1.4 var(--sans); letter-spacing: 0.07em; text-transform: uppercase;
  cursor: pointer;
}
.toggle:hover { border-color: var(--graphite); color: var(--ink); }
.toggle[data-on="true"] { border-color: currentColor; color: var(--ink); background: var(--rule-soft); }
.toggle[data-on="true"].toggle--hit  { color: var(--signal); background: var(--signal-wash); }
.toggle[data-on="true"].toggle--miss { color: var(--ember);  background: var(--ember-wash); }
.toggle[data-on="true"].toggle--overdue { color: var(--paper); background: var(--alarm); border-color: var(--alarm); }
.toggle-count { font-family: var(--mono); font-variant-numeric: tabular-nums; opacity: 0.75; letter-spacing: 0; }

.toggle--overdue { border-color: var(--alarm); color: var(--alarm); }
.filter-sep { width: 1px; align-self: stretch; background: var(--rule); margin: 0 4px; }

.select-field { display: inline-flex; flex-direction: column; gap: 4px; }
.select-field label { font: 600 0.6875rem/1.3 var(--sans); letter-spacing: 0.08em; text-transform: uppercase; color: var(--graphite); }
select {
  font: 400 0.875rem/1.4 var(--sans);
  padding: 6px 8px; border: 1px solid var(--rule); border-radius: var(--r-chip);
  background: var(--panel); color: var(--ink); min-width: 130px;
}

.filter-status { display: flex; align-items: center; gap: 20px; justify-content: space-between; flex-wrap: wrap; }
.result-count { font-family: var(--mono); font-size: 0.8125rem; color: var(--graphite); font-variant-numeric: tabular-nums; }

/* ---------- ledger table ----------------------------------------------- */

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

.ledger { width: 100%; border-collapse: collapse; font-size: 0.9375rem; }
.ledger caption { text-align: left; color: var(--graphite); font-size: 0.8125rem; padding-bottom: 10px; }
.ledger th, .ledger td { text-align: left; vertical-align: top; padding: 12px 12px; }
.ledger thead th {
  font: 600 0.6875rem/1.3 var(--sans); letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--graphite); border-bottom: 1px solid var(--rule); white-space: nowrap;
  padding-block: 8px;
}
.ledger tbody tr { border-bottom: 1px solid var(--rule-soft); }
.ledger tbody tr:hover { background: var(--rule-soft); }
.ledger tbody td:first-child {
  border-left: 3px solid var(--row-rule, var(--rule));
  padding-left: 12px;
}
.col-num { text-align: right; font-variant-numeric: tabular-nums; }
.ledger th.col-num { text-align: right; }

.conf {
  font-family: var(--mono); font-weight: 700; font-variant-numeric: tabular-nums;
  font-size: 1.375rem; line-height: 1.1; letter-spacing: -0.03em; white-space: nowrap;
}
.conf-pct { font-size: 0.625em; color: var(--graphite); font-weight: 400; }
.conf-bar {
  display: block; height: 3px; margin-top: 5px; margin-left: auto;
  background: var(--rule); width: 56px; border-radius: 2px; overflow: hidden;
}
.conf-bar > i { display: block; height: 100%; background: var(--graphite); }

.statement { display: block; }
.stmt-meta { margin-top: 6px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.stmt-meta a { text-decoration: none; }
.stmt-meta a:hover { text-decoration: underline; }
.pid { font-family: var(--mono); font-size: 0.75rem; color: var(--graphite); }
.sha {
  font-family: var(--mono); font-size: 0.75rem;
  padding: 1px 5px; border: 1px solid var(--rule); border-radius: var(--r-chip);
}
.res-note { margin: 8px 0 0; font-size: 0.875rem; color: var(--graphite); max-width: 60ch; }
.date-cell { white-space: nowrap; }
.date-overdue { color: var(--alarm); font-weight: 700; }

.sort-btn {
  background: none; border: 0; padding: 0; cursor: pointer; color: inherit;
  font: inherit; letter-spacing: inherit; text-transform: inherit;
  display: inline-flex; align-items: center; gap: 4px;
}
.sort-btn:hover { color: var(--ink); }
.sort-btn[data-active="true"] { color: var(--ink); }
.sort-arrow { font-size: 0.75em; }

/* The header sort controls are the desktop affordance; the select duplicates
   them for narrow screens where the table head collapses away. */
.sort-mobile { display: none; }

/* narrow: table becomes stacked cards, confidence stays large and top-right */
@media (max-width: 760px) {
  .sort-mobile { display: inline-flex; }
  .ledger, .ledger tbody, .ledger tr, .ledger td { display: block; width: 100%; }
  .ledger thead { display: none; }
  .ledger tbody tr {
    position: relative; border: 1px solid var(--rule); border-left: 3px solid var(--row-rule, var(--rule));
    border-radius: var(--r-panel); background: var(--panel);
    margin-bottom: 10px; padding: 14px 14px 10px;
  }
  .ledger tbody td { padding: 0; border: 0 !important; }
  .ledger tbody td + td { margin-top: 8px; }
  .ledger td[data-cell="confidence"] {
    position: absolute; top: 12px; right: 14px; margin: 0; text-align: right;
  }
  .ledger td[data-cell="status"] { padding-right: 92px !important; }
  .ledger td[data-cell="statement"] { padding-right: 0 !important; }
  .ledger td[data-cell="thesis"],
  .ledger td[data-cell="made"],
  .ledger td[data-cell="resolve"],
  .ledger td[data-cell="episode"] {
    display: inline-block; width: auto; margin-top: 8px; margin-right: 16px;
  }
  .ledger td[data-cell="made"]::before,
  .ledger td[data-cell="resolve"]::before {
    content: attr(data-head) " ";
    font: 600 0.625rem/1 var(--sans); letter-spacing: 0.08em;
    text-transform: uppercase; color: var(--graphite);
  }
  .conf-bar { margin-left: auto; }
}

/* ---------- episode view ------------------------------------------------ */

.video-frame {
  position: relative; aspect-ratio: 16 / 9; width: 100%;
  background: var(--ink); border: 1px solid var(--rule); border-radius: var(--r-panel);
  overflow: hidden;
}
.video-frame iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }

.summary p { max-width: 68ch; }

.sources { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
.sources li { border-bottom: 1px solid var(--rule); padding: 12px 4px; }
.source-title { font-weight: 600; }
.source-links { margin-top: 4px; display: flex; gap: 12px; flex-wrap: wrap; align-items: baseline; }
.as-of {
  font-family: var(--mono); font-size: 0.75rem; color: var(--graphite);
  border: 1px solid var(--rule); border-radius: var(--r-chip); padding: 1px 5px;
}
.no-archive { font-family: var(--mono); font-size: 0.75rem; color: var(--ember); }

.meta-bar { display: flex; gap: 8px 20px; flex-wrap: wrap; align-items: center; margin-top: 10px; }

/* ---------- misc -------------------------------------------------------- */

.empty {
  border: 1px dashed var(--rule); border-radius: var(--r-panel);
  padding: 24px; color: var(--graphite); background: var(--panel);
}
.empty strong { color: var(--ink); display: block; margin-bottom: 6px; font-size: 1.0625rem; }
.empty p { margin: 0; max-width: 60ch; }

.loading { color: var(--graphite); font-family: var(--mono); font-size: 0.875rem; }

.error-box { border-left: 3px solid var(--alarm); }
.error-box pre {
  background: var(--paper); border: 1px solid var(--rule); border-radius: var(--r-panel);
  padding: 12px; overflow-x: auto; font-family: var(--mono); font-size: 0.8125rem; margin: 12px 0;
}
.error-box code { font-family: var(--mono); font-size: 0.875rem; }

.backlink { display: inline-block; font: 600 0.8125rem/1.4 var(--sans); text-decoration: none; }
.backlink:hover { text-decoration: underline; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
