:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1a1d24;
  --text-dim: #5c6673;
  --border: #dfe3ea;
  --accent: #1e5fd6;
  --accent-soft: #dbe6fb;
  --bar: #4f86e6;
  --shadow: 0 1px 3px rgba(20, 30, 50, .08), 0 4px 16px rgba(20, 30, 50, .06);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #171b22;
    --surface-2: #1e242e;
    --text: #e7ebf1;
    --text-dim: #97a1b0;
    --border: #2a323e;
    --accent: #5b9bff;
    --accent-soft: #1c2b45;
    --bar: #3f6fc4;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overflow-wrap: break-word;
}
/* Locked while a modal is open so the page behind doesn't scroll on touch. */
body.modal-open { overflow: hidden; }
a { color: var(--accent); }
.wrap {
  max-width: 1180px; margin: 0 auto;
  padding: 0 max(20px, env(safe-area-inset-right)) 0 max(20px, env(safe-area-inset-left));
}

/* Collapsible-section toggle — mobile only (see the small-screen block below). */
.section-toggle { display: none; }

/* Header */
.site-header {
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg) 100%);
  border-bottom: 1px solid var(--border);
  padding: 28px 0 22px;
}
.site-header h1 { margin: 0; font-size: 1.7rem; letter-spacing: -.02em; }
.accent { color: var(--accent); }
.tagline { margin: 6px 0 0; color: var(--text-dim); max-width: 720px; }
.meta-line { margin: 10px 0 0; font-size: .82rem; color: var(--text-dim); }

/* KPIs */
.dashboard { margin: 26px 0 10px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(160px, 100%), 1fr)); gap: 14px; margin-bottom: 20px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.kpi .num { font-size: 1.7rem; font-weight: 700; letter-spacing: -.02em; }
.kpi .lbl { font-size: .78rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; }

/* Charts */
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 12px; font-size: .95rem; }
.card h3 .hint { font-weight: 400; color: var(--text-dim); font-size: .78rem; }
.card h3 .more-hint { font-weight: 400; color: var(--accent); font-size: .74rem; }

/* Clickable dashboard cards */
.card--clickable { cursor: pointer; transition: border-color .15s, box-shadow .15s, transform .15s; -webkit-tap-highlight-color: transparent; }
@media (hover: hover) {
  .card--clickable:hover { border-color: var(--accent); box-shadow: 0 2px 6px rgba(30, 95, 214, .18), 0 8px 24px rgba(30, 95, 214, .12); }
}
.card--clickable:active { border-color: var(--accent); }
.card--clickable:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.bars { display: flex; flex-direction: column; gap: 7px; }
.bar-row { display: grid; grid-template-columns: 120px 1fr auto; align-items: center; gap: 10px; font-size: .8rem; }
.bar-row .bar-label { color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-row .bar-track { background: var(--surface-2); border-radius: 5px; height: 16px; overflow: hidden; }
.bar-row .bar-fill { background: var(--bar); height: 100%; border-radius: 5px; min-width: 2px; transition: width .3s; }
.bar-row .bar-val { font-variant-numeric: tabular-nums; color: var(--text); font-size: .78rem; }
/* Full list inside the chart modal: wider labels, scrolls if long */
.bars--full { max-height: 64vh; overflow-y: auto; padding-right: 4px; }
.bars--full .bar-row { grid-template-columns: minmax(140px, 260px) 1fr auto; }
.bars--full .bar-row .bar-label { white-space: normal; }

/* Controls */
.controls { margin: 26px 0 12px; }
.controls .row { margin-bottom: 12px; }
.controls #q {
  width: 100%; padding: 12px 14px; font-size: 1rem; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.filters { display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: end; }
.filters label { display: flex; flex-direction: column; font-size: .75rem; color: var(--text-dim); gap: 4px; }
.filters select, .filters input {
  padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: .88rem;
  min-width: 120px; max-width: 240px;   /* the 700+ program names would otherwise stretch the row */
  text-overflow: ellipsis;
}
.filters #fMin { min-width: 90px; }
.btn-reset {
  padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); cursor: pointer; font-size: .85rem;
}
@media (hover: hover) { .btn-reset:hover { border-color: var(--accent); } }

.result-summary { font-size: .85rem; color: var(--text-dim); margin: 8px 2px 12px; }

/* Table */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: .86rem; }
thead th {
  text-align: left; padding: 11px 12px; background: var(--surface-2); color: var(--text-dim);
  font-weight: 600; font-size: .74rem; text-transform: uppercase; letter-spacing: .03em;
  position: sticky; top: 0; white-space: nowrap;
}
tbody td { padding: 11px 12px; border-top: 1px solid var(--border); vertical-align: top; }
tbody tr { cursor: pointer; -webkit-tap-highlight-color: transparent; }
@media (hover: hover) { tbody tr:hover { background: var(--accent-soft); } }
.col-amt, td.col-amt { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.col-date, td.col-date { white-space: nowrap; }
.td-title { font-weight: 600; max-width: 460px; }
.div-tag {
  display: inline-block; font-size: .72rem; padding: 2px 8px; border-radius: 20px;
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
}
/* min-width keeps a 30-char program name on one line inside its chip; the
   narrower breakpoints below give it up and let chips wrap. */
.td-prog { color: var(--text-dim); max-width: 220px; min-width: 190px; }

/* Program chips — one per funding program, so co-funded awards read as a list
   of programs rather than one long comma-separated name. Chips are inline-block
   with margins rather than a flex container: `display: flex` on a <td> pulls the
   cell out of the table layout and the columns stop lining up. */
.prog-chip, .prog-more {
  display: inline-block; font-size: .72rem; line-height: 1.35; padding: 2px 7px;
  margin: 0 4px 3px 0; border-radius: 6px;
  background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border);
}
.prog-chip.is-match { background: var(--accent-soft); color: var(--accent); border-color: transparent; font-weight: 600; }
.prog-more { font-variant-numeric: tabular-nums; opacity: .8; cursor: help; }
.prog-none { color: var(--text-dim); }
.td-org { max-width: 200px; }

/* Pager */
.pager { display: flex; gap: 8px; align-items: center; justify-content: center; margin: 20px 0 40px; flex-wrap: wrap; }
.pager button {
  padding: 7px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; font-size: .85rem;
}
.pager button:disabled { opacity: .4; cursor: default; }
.pager .page-info { color: var(--text-dim); font-size: .85rem; display: inline-flex; align-items: center; gap: 4px; }
.pager .page-jump {
  width: 4.5em; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: .85rem; text-align: center;
  font-variant-numeric: tabular-nums;
}
.pager .page-jump:focus { outline: none; border-color: var(--accent); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 14, 22, .55);
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 50;
  overflow-y: auto;
}
.modal-backdrop[hidden] { display: none; }  /* author display:flex would otherwise override the UA hidden style */
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  max-width: 760px; width: 100%; padding: 28px 30px; box-shadow: var(--shadow); position: relative;
}
.modal-close {
  position: absolute; top: 14px; right: 16px; background: none; border: none;
  font-size: 1.7rem; line-height: 1; color: var(--text-dim); cursor: pointer;
}
.modal h2 { margin: 0 30px 14px 0; font-size: 1.25rem; }
.modal-meta { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: .85rem; color: var(--text-dim); margin-bottom: 16px; }
.modal-meta b { color: var(--text); font-weight: 600; }
.modal-abstract { font-size: .92rem; line-height: 1.65; white-space: pre-wrap; max-height: 46vh; overflow-y: auto; }
.modal-links { margin-top: 18px; }

/* Footer */
.site-footer { border-top: 1px solid var(--border); margin-top: 20px; padding: 20px 0; }
.site-footer p { color: var(--text-dim); font-size: .8rem; margin: 0; }

/* ------------------------------------------------------------------ */
/* Tablet                                                              */
/* ------------------------------------------------------------------ */
@media (max-width: 1000px) {
  .td-title { max-width: 320px; }
  .td-prog, .td-org { max-width: 170px; }
  .td-prog { min-width: 0; }
}

/* ------------------------------------------------------------------ */
/* Phone — cards instead of a table, collapsible charts and filters    */
/* ------------------------------------------------------------------ */
@media (max-width: 900px) {
  .wrap { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }

  /* Header */
  .site-header { padding: 18px 0 16px; }
  .site-header h1 { font-size: 1.35rem; }
  .tagline { font-size: .87rem; margin-top: 4px; }
  .meta-line { font-size: .76rem; margin-top: 8px; }

  /* Section toggles (charts / filters) */
  .section-toggle {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    width: 100%; min-height: 44px; padding: 10px 14px; margin-bottom: 4px;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--text); font: inherit; font-size: .88rem;
    font-weight: 600; cursor: pointer; -webkit-tap-highlight-color: transparent;
  }
  .section-toggle .chev { color: var(--text-dim); transition: transform .18s; }
  .section-toggle[aria-expanded="true"] .chev { transform: rotate(180deg); }
  .section-toggle--inline { width: auto; flex: 0 0 auto; margin-bottom: 0; padding: 10px 14px; }

  /* KPIs */
  .dashboard { margin: 16px 0 6px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
  .kpi { padding: 12px 14px; }
  .kpi .num { font-size: 1.35rem; }
  .kpi .lbl { font-size: .68rem; }

  /* Charts — hidden until the toggle opens them */
  .charts { display: none; grid-template-columns: minmax(0, 1fr); gap: 12px; margin-top: 8px; }
  body.charts-open .charts { display: grid; }
  .card { padding: 14px; }
  .card h3 { font-size: .9rem; }

  /* Bar rows stack: label + value on one line, bar underneath */
  .bar-row {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "label val" "track track";
    row-gap: 3px; column-gap: 10px;
  }
  .bar-row .bar-label { grid-area: label; white-space: normal; color: var(--text); }
  .bar-row .bar-val { grid-area: val; color: var(--text-dim); }
  .bar-row .bar-track { grid-area: track; height: 10px; }
  .bars { gap: 11px; }
  .bars--full { max-height: none; overflow-y: visible; padding-right: 0; }
  .bars--full .bar-row { grid-template-columns: minmax(0, 1fr) auto; }

  /* Controls — 16px inputs so iOS doesn't zoom on focus */
  .controls { margin: 14px 0 10px; }
  .controls .row { margin-bottom: 10px; }
  .search-row { display: flex; gap: 8px; align-items: stretch; }
  .controls #q { font-size: 16px; padding: 11px 12px; min-width: 0; }
  .filters { display: none; }
  body.filters-open .filters {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px;
    padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  }
  .filters label { font-size: .7rem; min-width: 0; }
  .filters select, .filters input, .filters #fMin {
    width: 100%; min-width: 0; max-width: none; font-size: 16px; padding: 10px;
  }
  .filters .f-sort { grid-column: 1 / -1; }   /* option labels are too long for a half-width select */
  .btn-reset { grid-column: 1 / -1; min-height: 44px; font-size: .9rem; }

  .result-summary { font-size: .8rem; margin: 4px 2px 10px; }

  /* Results: each row becomes a self-contained card */
  .table-wrap { border: none; background: none; border-radius: 0; overflow: visible; }
  table { display: block; }
  thead { display: none; }
  tbody { display: block; }
  tbody tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "title title"
      "div   amt"
      "org   date"
      "pi    pi"
      "prog  prog";
    gap: 3px 10px;
    padding: 13px 14px; margin-bottom: 10px;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
  }
  tbody tr:active { border-color: var(--accent); }
  tbody td { display: block; padding: 0; border: none; min-width: 0; max-width: none; }
  .td-title { grid-area: title; font-size: .95rem; line-height: 1.35; margin-bottom: 3px; max-width: none; }
  td.col-div { grid-area: div; }
  td.col-amt { grid-area: amt; font-weight: 600; text-align: right; align-self: center; }
  td.col-org { grid-area: org; font-size: .82rem; color: var(--text-dim); max-width: none; }
  td.col-date { grid-area: date; font-size: .78rem; color: var(--text-dim); text-align: right; }
  td.col-pi { grid-area: pi; font-size: .82rem; color: var(--text-dim); }
  td.col-prog { grid-area: prog; font-size: .76rem; color: var(--text-dim); margin-top: 2px; max-width: none; min-width: 0; }
  td.col-prog .prog-chip, td.col-prog .prog-more { font-size: .7rem; padding: 1px 6px; }
  td.col-org:empty, td.col-pi:empty, td.col-prog:empty, td.is-placeholder { display: none; }
  td.col-pi:not(:empty)::before { content: "PI: "; }

  /* Pager — thumb-sized targets */
  .pager { gap: 10px; margin: 6px 0 32px; }
  .pager button { min-height: 44px; padding: 10px 18px; font-size: .9rem; }
  .pager .page-jump { min-height: 40px; font-size: 16px; width: 4em; }

  /* Modals become bottom sheets */
  .modal-backdrop { padding: 0; align-items: flex-end; overflow: hidden; }
  .modal {
    border-radius: 16px 16px 0 0; border-bottom: none;
    max-height: 92vh; max-height: 92dvh; overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
  }
  .modal h2 { font-size: 1.08rem; margin: 0 40px 12px 0; }
  .modal-close {
    top: 6px; right: 6px; width: 44px; height: 44px; font-size: 1.9rem;
    display: flex; align-items: center; justify-content: center;
  }
  .modal-meta { flex-direction: column; gap: 5px; margin-bottom: 14px; }
  .modal-abstract { max-height: none; overflow-y: visible; font-size: .9rem; }

  .site-footer { padding: 18px 0 calc(18px + env(safe-area-inset-bottom)); }
  .site-footer p { font-size: .76rem; }
}

/* Very narrow phones */
@media (max-width: 380px) {
  .kpis { grid-template-columns: minmax(0, 1fr); }
  body.filters-open .filters { grid-template-columns: minmax(0, 1fr); }
  .site-header h1 { font-size: 1.2rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
