/* CAFE Dataverse Dashboard -- design tokens, layout and components.
 *
 * Colour is defined once here as roles, never as raw hex in a chart. The
 * categorical slots are a validated palette: the eight-slot order clears the
 * adjacent-pair CVD and normal-vision gates in both light and dark modes, and
 * the first three slots additionally clear the all-pairs gates, which is why
 * the scatter on the similarity page highlights at most three groups at a time
 * and mutes the rest rather than colouring ten clusters at once.
 *
 * Dark values are declared under both an OS-preference scope and a data-theme
 * scope so the toggle wins in both directions.
 */

/* ======================================================================== */
/* Tokens                                                                   */
/* ======================================================================== */
:root {
  color-scheme: light;

  --page:            #f9f9f7;
  --surface:         #fcfcfb;
  --surface-sunken:  #f2f1ed;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --grid:            #e1e0d9;
  --axis:            #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);
  --border-strong:   rgba(11, 11, 11, 0.18);

  /* Categorical slots, in validated order. */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  /* Sequential blue, light -> dark. Ordinal use starts at step 250. */
  --seq-100: #cde2fb;
  --seq-250: #86b6ef;
  --seq-350: #5598e7;
  --seq-450: #2a78d6;
  --seq-550: #1c5cab;
  --seq-650: #104281;
  --seq-700: #0d366b;

  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;

  --muted-mark: #b9b8b1;

  --radius: 6px;
  --radius-lg: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --page:            #0d0d0d;
    --surface:         #1a1a19;
    --surface-sunken:  #232322;
    --text-primary:    #ffffff;
    --text-secondary:  #c3c2b7;
    --text-muted:      #898781;
    --grid:            #2c2c2a;
    --axis:            #383835;
    --border:          rgba(255, 255, 255, 0.10);
    --border-strong:   rgba(255, 255, 255, 0.20);

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;

    --seq-100: #0d366b;
    --seq-250: #104281;
    --seq-350: #1c5cab;
    --seq-450: #2a78d6;
    --seq-550: #3987e5;
    --seq-650: #6da7ec;
    --seq-700: #9ec5f4;

    --muted-mark: #4a4a47;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --page:            #0d0d0d;
  --surface:         #1a1a19;
  --surface-sunken:  #232322;
  --text-primary:    #ffffff;
  --text-secondary:  #c3c2b7;
  --text-muted:      #898781;
  --grid:            #2c2c2a;
  --axis:            #383835;
  --border:          rgba(255, 255, 255, 0.10);
  --border-strong:   rgba(255, 255, 255, 0.20);

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;

  --seq-100: #0d366b;
  --seq-250: #104281;
  --seq-350: #1c5cab;
  --seq-450: #2a78d6;
  --seq-550: #3987e5;
  --seq-650: #6da7ec;
  --seq-700: #9ec5f4;

  --muted-mark: #4a4a47;
  --shadow: none;
}

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

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

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 15px/1.55 var(--font);
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; line-height: 1.25; }
h1 { font-size: 1.6rem; letter-spacing: -0.015em; }
h2 { font-size: 1.15rem; letter-spacing: -0.01em; }
h3 { font-size: 0.95rem; }
p { margin: 0 0 0.75rem; }
p:last-child { margin-bottom: 0; }

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

code, .mono { font-family: var(--mono); font-size: 0.88em; }

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

/* ======================================================================== */
/* Shell                                                                    */
/* ======================================================================== */
.masthead {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.masthead-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 24px;
  min-height: 56px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-weight: 650;
  color: var(--text-primary);
  text-decoration: none;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand span { font-weight: 400; color: var(--text-muted); font-size: 0.82rem; }

.nav { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
.nav a {
  padding: 6px 11px;
  border-radius: var(--radius);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  white-space: nowrap;
}
.nav a:hover { background: var(--surface-sunken); color: var(--text-primary); }
.nav a[aria-current="page"] {
  background: var(--surface-sunken);
  color: var(--text-primary);
  font-weight: 550;
  box-shadow: inset 0 -2px 0 var(--series-1);
}

.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font: inherit;
  font-size: 0.82rem;
  padding: 5px 10px;
}
.theme-toggle:hover { background: var(--surface-sunken); color: var(--text-primary); }

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

.page-head { margin-bottom: 24px; max-width: 78ch; }
.page-head p { color: var(--text-secondary); }

.sitefoot {
  border-top: 1px solid var(--border);
  margin-top: 48px;
  padding: 20px 24px 40px;
  color: var(--text-muted);
  font-size: 0.82rem;
}
.sitefoot .sitefoot-inner { max-width: 1400px; margin: 0 auto; }

/* ======================================================================== */
/* Cards, grid                                                              */
/* ======================================================================== */
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1080px) {
  .grid.cols-3, .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 720px) {
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: minmax(0, 1fr); }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 18px 18px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card > header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}
.card-note {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin: 2px 0 12px;
  max-width: 76ch;
}

.section { margin-top: 32px; }
.section > h2 { margin-bottom: 4px; }
.section > .section-note {
  color: var(--text-secondary);
  font-size: 0.875rem;
  margin-bottom: 14px;
  max-width: 84ch;
}

/* ======================================================================== */
/* Stat tiles -- the number IS the chart                                    */
/* ======================================================================== */
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px 15px;
  box-shadow: var(--shadow);
}
.stat .stat-label {
  color: var(--text-secondary);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 550;
}
.stat .stat-value {
  font-size: 1.85rem;
  font-weight: 620;
  letter-spacing: -0.02em;
  line-height: 1.15;
  margin-top: 4px;
  /* Proportional figures: tabular-nums makes a standalone number look loose. */
}
.stat .stat-sub { color: var(--text-muted); font-size: 0.8rem; margin-top: 2px; }

/* ======================================================================== */
/* Chart frames                                                             */
/* ======================================================================== */
.chart {
  width: 100%;
  /* Height includes the axis band: a fixed height that clips the axis
   * produces a tiny nested scrollbar. */
  min-height: 220px;
}
.chart.tall { min-height: 400px; }
.chart.xtall { min-height: 560px; }

.chart-actions { display: flex; gap: 4px; align-items: center; flex-shrink: 0; }
.chip {
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-secondary);
  cursor: pointer;
  font: inherit;
  font-size: 0.75rem;
  padding: 3px 10px;
  white-space: nowrap;
}
.chip:hover { background: var(--surface-sunken); color: var(--text-primary); }
.chip[aria-pressed="true"] {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}

.table-view { display: none; max-height: 420px; overflow: auto; margin-top: 8px; }
.table-view.is-open { display: block; }

/* ======================================================================== */
/* Tables                                                                   */
/* ======================================================================== */
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
  font-variant-numeric: tabular-nums;
}
table.data th, table.data td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--grid);
  vertical-align: top;
}
table.data thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  z-index: 1;
  border-bottom: 1px solid var(--axis);
}
table.data td.num, table.data th.num { text-align: right; }
table.data tbody tr:hover { background: var(--surface-sunken); }
table.data td.wrap { white-space: normal; max-width: 460px; }

.table-wrap { overflow-x: auto; max-height: 560px; overflow-y: auto; }

/* ======================================================================== */
/* Filters -- one row above everything it scopes                            */
/* ======================================================================== */
.filterbar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px 18px;
  align-items: flex-end;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label {
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  font-weight: 600;
}
.field select, .field input[type="search"], .field input[type="text"] {
  font: inherit;
  font-size: 0.875rem;
  padding: 6px 9px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-primary);
  min-width: 160px;
}
.field input[type="search"] { min-width: 260px; }

.toggle-group { display: flex; gap: 4px; flex-wrap: wrap; }

.filter-summary {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--grid);
  font-size: 0.85rem;
  color: var(--text-secondary);
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  align-items: center;
}
.filter-summary strong { color: var(--text-primary); font-variant-numeric: tabular-nums; }

.checklist {
  max-height: 190px;
  overflow-y: auto;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 6px 8px;
  min-width: 260px;
  background: var(--surface);
}
.checklist label {
  display: flex;
  gap: 7px;
  align-items: baseline;
  font-size: 0.85rem;
  padding: 2px 0;
  cursor: pointer;
}
.checklist label:hover { color: var(--series-1); }
.checklist .count { margin-left: auto; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ======================================================================== */
/* Legend -- identity is never colour alone                                 */
/* ======================================================================== */
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; }
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  background: none;
  border: 0;
  padding: 2px 4px;
  cursor: pointer;
  font-family: inherit;
  border-radius: var(--radius);
}
.legend-item:hover { color: var(--text-primary); background: var(--surface-sunken); }
.legend-item .swatch {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  flex-shrink: 0;
  background: var(--muted-mark);
}
.legend-item[aria-pressed="true"] { color: var(--text-primary); font-weight: 550; }
.legend-item .count { color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ======================================================================== */
/* Prose blocks (method justifications, notes)                              */
/* ======================================================================== */
.prose { max-width: 74ch; font-size: 0.9rem; color: var(--text-secondary); }
.prose h4 {
  color: var(--text-primary);
  margin: 14px 0 4px;
  font-size: 0.88rem;
}
.prose h4:first-child { margin-top: 0; }
.prose strong { color: var(--text-primary); font-weight: 600; }
.prose em { font-style: italic; }
.prose code {
  background: var(--surface-sunken);
  padding: 1px 5px;
  border-radius: 4px;
}
.prose ol, .prose ul { margin: 0 0 0.75rem; padding-left: 1.3rem; }
.prose li { margin-bottom: 0.3rem; }

.callout {
  border-left: 3px solid var(--series-1);
  background: var(--surface-sunken);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 11px 14px;
  font-size: 0.86rem;
  color: var(--text-secondary);
  margin: 12px 0;
  max-width: 84ch;
}
.callout.caution { border-left-color: var(--status-serious); }
.callout strong { color: var(--text-primary); }

.pill {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-sunken);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  vertical-align: middle;
}
.pill.partial { border-color: var(--status-serious); color: var(--status-serious); }

.coverage-note {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 6px;
}

/* ======================================================================== */
/* Detail panel (similarity page)                                           */
/* ======================================================================== */
.detail {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  padding: 14px 16px;
  font-size: 0.86rem;
}
.detail .detail-empty { color: var(--text-muted); }
.detail h3 { margin-bottom: 2px; font-size: 0.95rem; line-height: 1.3; }
.detail .meta { color: var(--text-muted); font-size: 0.78rem; margin-bottom: 8px; }
.detail ol { margin: 6px 0 0; padding-left: 1.2rem; }
.detail ol li { margin-bottom: 5px; }
.detail .score {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  font-size: 0.76rem;
}
.tag-row { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.tag {
  font-size: 0.72rem;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  color: var(--text-secondary);
}

.loading { color: var(--text-muted); font-size: 0.86rem; padding: 20px 0; }
.error {
  color: var(--status-critical);
  font-size: 0.86rem;
  border: 1px solid var(--status-critical);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: var(--surface);
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

@media print {
  .masthead, .chart-actions, .filterbar { display: none; }
  .table-view { display: block !important; max-height: none; }
}
