:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-wash: #e6f0fc;
  --meter-track: #cde2fb;
  --meter-fill: #2a78d6;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-wash: #16263b;
  --meter-track: #184f95;
  --meter-fill: #6da7ec;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.45;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--ink-2); }
code { font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 500;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: var(--surface); color: var(--ink); border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); filter: none; }
.btn.icon { width: 34px; height: 34px; justify-content: center; padding: 0; font-size: 20px; line-height: 1; }
.btn:disabled { opacity: .45; cursor: default; }
.link { font: inherit; background: none; border: 0; color: var(--accent-ink); cursor: pointer; padding: 0; }
.link:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 18px; }
.brand p { margin: 0; font-size: 12px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 10px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 13px; letter-spacing: .5px;
}
.topbar-actions { display: flex; gap: 8px; }

/* Tabs */
.tabs {
  display: flex; gap: 4px; padding: 0 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); overflow-x: auto;
}
.tab {
  font: inherit; background: none; border: 0; border-bottom: 2px solid transparent;
  padding: 12px 14px 10px; color: var(--ink-2); cursor: pointer; white-space: nowrap; font-weight: 500;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.tab .count {
  display: inline-block; margin-left: 6px; font-size: 11px; padding: 1px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); font-variant-numeric: tabular-nums;
}
.tab[aria-selected="true"] .count { background: var(--accent-wash); color: var(--accent-ink); }

/* Layout */
.layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 20px; padding: 20px 24px; align-items: start; }
.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow-y: auto;
}
.sidebar-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.sidebar-head h2 { font-size: 15px; }
.content { min-width: 0; }
.dataset-intro h2 { font-size: 20px; }
.dataset-intro p { margin: 4px 0 14px; max-width: 900px; }

/* Facets */
.facet { border-top: 1px solid var(--grid); padding: 12px 0; }
.facet:first-child { border-top: 0; }
.facet-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.facet-head h3 { font-size: 13px; }
.facet-head .link { font-size: 12px; }
.facet-hint { font-size: 11px; color: var(--muted); margin: -4px 0 8px; }
.facet-search {
  width: 100%; font: inherit; font-size: 13px; padding: 5px 8px; margin-bottom: 6px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--page); color: var(--ink);
}
.opt {
  display: flex; align-items: center; gap: 8px; padding: 3px 4px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.opt:hover { background: var(--surface-2); }
.opt input { margin: 0; accent-color: var(--accent); flex: none; }
.opt .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opt .n { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.opt.zero { opacity: .45; }
.facet .more { font-size: 12px; margin-top: 4px; }
.range { display: grid; grid-template-columns: 1fr auto 1fr; gap: 6px; align-items: center; }
.range input {
  width: 100%; font: inherit; font-size: 13px; padding: 5px 8px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--page); color: var(--ink); font-variant-numeric: tabular-nums;
}
.range span { color: var(--muted); }
.presets { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.preset {
  font: inherit; font-size: 11px; padding: 2px 8px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--page); color: var(--ink-2);
}
.preset:hover { border-color: var(--accent); color: var(--accent-ink); }

/* Stat tiles */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.tile .label { font-size: 12px; color: var(--ink-2); }
.tile .value { font-size: 22px; font-weight: 600; margin-top: 2px; }
.tile .sub { font-size: 11px; color: var(--muted); }

/* Toolbar */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.search { position: relative; flex: 1 1 320px; max-width: 520px; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; fill: var(--muted); }
.search input {
  width: 100%; font: inherit; padding: 8px 10px 8px 34px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--ink);
}
.toolbar-right { display: flex; gap: 8px; align-items: center; margin-left: auto; }
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 230px; max-height: 360px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px;
}
.mobile-only { display: none; }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chips:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 3px 4px 3px 10px; border-radius: 999px;
  background: var(--accent-wash); color: var(--accent-ink); border: 1px solid transparent;
}
.chip b { font-weight: 600; }
.chip button {
  font: inherit; border: 0; background: none; color: inherit; cursor: pointer; width: 20px; height: 20px;
  border-radius: 999px; line-height: 1; font-size: 15px;
}
.chip button:hover { background: rgba(0, 0, 0, .08); }

/* Table */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; font-weight: 600; color: var(--ink-2); font-size: 12px; padding: 10px 10px;
  border-bottom: 1px solid var(--baseline); white-space: nowrap; background: var(--surface); position: sticky; top: 0; z-index: 1;
  user-select: none;
}
thead th.sortable { cursor: pointer; }
thead th.sortable:hover { color: var(--ink); }
thead th .arrow { display: inline-block; width: 12px; color: var(--accent); }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody td { padding: 9px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
tbody tr { cursor: pointer; }
tbody tr:hover td { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.wrap { min-width: 220px; max-width: 380px; }
td.mid { min-width: 150px; max-width: 220px; }
td.num { white-space: nowrap; }
td.measure { white-space: normal; min-width: 90px; max-width: 130px; }
td.nowrap { white-space: nowrap; }
td .primary { font-weight: 500; }
td .secondary { color: var(--muted); font-size: 12px; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
mark { background: #fde68a; color: inherit; border-radius: 2px; padding: 0 1px; }
:root[data-theme="dark"] mark { background: #7a5a00; }
.empty { text-align: center; padding: 40px 20px; color: var(--ink-2); }

/* Badges */
.sev { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.sev .dot { width: 8px; height: 8px; border-radius: 999px; flex: none; }
.sev-0 .dot { background: var(--good); }
.sev-1 .dot { background: var(--warning); }
.sev-2 .dot { background: var(--serious); }
.sev-3 .dot { background: var(--critical); }
.sev-4 .dot { background: var(--critical); box-shadow: 0 0 0 2px var(--surface), 0 0 0 3.5px var(--critical); }
.tag {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2);
  color: var(--ink); white-space: nowrap; border: 1px solid var(--border);
}
.meter { display: flex; align-items: center; gap: 8px; justify-content: flex-end; }
.meter .bar { width: 40px; height: 6px; background: var(--meter-track); border-radius: 999px; overflow: hidden; }
.meter .fill { display: block; height: 100%; background: var(--meter-fill); border-radius: 999px; }
.meter .v { width: 24px; text-align: right; }

/* Pager */
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 2px; flex-wrap: wrap; }
.pager .pages { display: flex; gap: 4px; align-items: center; }
.pager .page-btn { min-width: 34px; justify-content: center; }
.pager .page-btn[aria-current="page"] { background: var(--accent); color: #fff; border-color: var(--accent); }
.pager select {
  font: inherit; padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--ink);
}

/* Drawer */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(620px, 100vw); background: var(--surface); z-index: 50;
  box-shadow: var(--shadow); transform: translateX(100%); transition: transform .2s ease; display: flex; flex-direction: column;
}
.drawer.open { transform: none; }
.drawer-head { display: flex; justify-content: space-between; gap: 12px; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { font-size: 18px; }
.drawer-head p { margin: 0 0 2px; font-size: 12px; }
.drawer-nav { display: flex; gap: 4px; flex: none; }
.drawer-body { padding: 8px 20px 24px; overflow-y: auto; }
.drawer-summary { margin: 14px 0 6px; font-size: 14px; line-height: 1.55; }
.kv { display: grid; grid-template-columns: 170px 1fr; gap: 0 16px; }
.kv dt { color: var(--ink-2); font-size: 12px; padding: 9px 0; border-bottom: 1px solid var(--grid); }
.kv dd { margin: 0; padding: 8px 0; border-bottom: 1px solid var(--grid); word-break: break-word; }
.kv dd ul { margin: 0; padding-left: 18px; }
.kv dd .sub-kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 13px; }
.kv dd .sub-kv span:nth-child(odd) { color: var(--ink-2); }

.footer { padding: 10px 24px 28px; font-size: 12px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 14px; }
  .sidebar { position: static; max-height: none; display: none; }
  .sidebar.show { display: block; }
  .mobile-only { display: inline-flex; }
  .brand p, .classic-link { display: none; }
  .brand h1 { font-size: 16px; }
  .topbar { padding: 12px 14px; }
  .tabs { padding: 0 8px; }
  .kv { grid-template-columns: 1fr; }
  .kv dt { border-bottom: 0; padding-bottom: 0; }
}

/* ---- Mobile / touch ---- */
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 6px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}
.badge[hidden] { display: none; }
.sort-mobile { display: none; gap: 6px; align-items: center; }
.sort-mobile select {
  font: inherit; padding: 7px 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--ink); min-width: 0; flex: 1;
}
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 55; }
.sheet-backdrop[hidden] { display: none; }
.theme-icon { display: none; }
.page-of { display: none; }
body.sheet-open { overflow: hidden; }
.tab, .page-btn { touch-action: manipulation; }

@media (hover: none) {
  tbody tr:hover td, .opt:hover, .btn.ghost:hover { background: none; }
  .btn.ghost:hover { background: var(--surface); }
}

/* Filters become a full-height bottom sheet on tablets and phones */
@media (max-width: 900px) {
  .sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; top: 8vh; z-index: 60; border-radius: 16px 16px 0 0;
    padding: 0; max-height: none; display: none; flex-direction: column; box-shadow: var(--shadow); overflow: hidden;
  }
  .sidebar.show { display: flex; }
  .sidebar-head { padding: 12px 16px; margin: 0; border-bottom: 1px solid var(--border); gap: 12px; }
  .sidebar-head h2 { flex: 1; font-size: 17px; }
  #filters { flex: 1; overflow-y: auto; padding: 4px 16px; overscroll-behavior: contain; }
  .sidebar-foot {
    display: block; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border);
    background: var(--surface);
  }
  .sidebar-foot .btn { width: 100%; justify-content: center; min-height: 46px; font-size: 16px; }
}

@media (max-width: 700px) {
  body { font-size: 15px; -webkit-text-size-adjust: 100%; }
  input, select, textarea { font-size: 16px !important; }

  /* Top bar + tabs */
  .topbar { padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top)); }
  .brand-mark { width: 34px; height: 34px; }
  .brand h1 { font-size: 15px; }
  /* Site build: the extra header links wrap onto their own scrollable row instead of widening the page. */
  .topbar { flex-wrap: wrap; gap: 8px; }
  .topbar-actions { width: 100%; overflow-x: auto; scrollbar-width: none; }
  .topbar-actions::-webkit-scrollbar { display: none; }
  .topbar-actions > * { flex-shrink: 0; white-space: nowrap; }
  .dl-link { display: none; }
  .theme-icon { display: inline; font-size: 18px; }
  .theme-label { display: none; }
  #themeToggle { min-width: 44px; min-height: 44px; justify-content: center; padding: 0; }
  .tabs { position: sticky; top: 0; z-index: 30; padding: 0 6px; scrollbar-width: none; scroll-snap-type: x proximity; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { min-height: 46px; padding: 12px 12px 10px; scroll-snap-align: start; }

  .layout { padding: 12px 12px calc(12px + env(safe-area-inset-bottom)); gap: 12px; }
  .dataset-intro h2 { font-size: 18px; }
  .dataset-intro p { font-size: 13px; margin-bottom: 10px; }

  /* Stat tiles: two across */
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tile { padding: 10px 12px; }
  .tile .value { font-size: 19px; }

  /* Toolbar: search on its own row, then filters/sort, then actions */
  .toolbar { gap: 8px; }
  .search { flex: 1 1 100%; max-width: none; }
  .search input { padding: 11px 12px 11px 36px; min-height: 44px; }
  .btn { min-height: 44px; padding: 8px 14px; }
  .btn.icon { width: 44px; height: 44px; }
  .sort-mobile { display: flex; flex: 1 1 0; min-width: 0; }
  .sort-mobile select { min-height: 44px; }
  .toolbar-right { margin-left: 0; flex: 1 1 100%; justify-content: space-between; }
  #resultCount { font-size: 12px; }

  /* Columns menu as a bottom sheet */
  .dropdown-menu {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; min-width: 0; max-height: 65vh; z-index: 70;
    border-radius: 16px 16px 0 0; padding: 10px 14px calc(14px + env(safe-area-inset-bottom));
  }
  .opt { padding: 10px 6px; min-height: 44px; font-size: 15px; }
  .opt input { width: 20px; height: 20px; }
  .opt .n { font-size: 13px; }
  .facet { padding: 14px 0; }
  .facet-head h3 { font-size: 14px; }
  .facet-head .link, .facet .more, .link { padding: 8px 4px; font-size: 14px; }
  .facet-search { padding: 10px; }
  .range input { padding: 10px; }
  .preset { font-size: 13px; padding: 8px 12px; min-height: 36px; }

  /* Chips */
  .chip { font-size: 13px; padding: 4px 4px 4px 12px; }
  .chip button { width: 32px; height: 32px; font-size: 18px; }

  /* Table -> cards */
  .table-wrap { background: none; border: 0; overflow: visible; }
  table, thead, tbody, tr, td { display: block; }
  thead { display: none; }
  tbody tr {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 12px; margin-bottom: 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px;
  }
  tbody tr:active { background: var(--surface-2); }
  tbody td, tbody tr:last-child td { border: 0; padding: 0; min-width: 0; max-width: none; white-space: normal; text-align: left; font-size: 14px; }
  tbody td::before {
    content: attr(data-label); display: block; font-size: 11px; color: var(--muted); text-transform: uppercase;
    letter-spacing: .3px; margin-bottom: 1px;
  }
  tbody td.card-title { grid-column: 1 / -1; order: -1; font-size: 16px; }
  tbody td.card-title::before { display: none; }
  tbody td.card-title .primary { font-weight: 600; }
  tbody td.wrap, tbody td.mid { grid-column: span 2; }
  tbody td.is-empty { display: none; }
  tbody td.num { text-align: left; }
  .meter { justify-content: flex-start; }
  .meter .bar { width: 64px; }
  .clamp { -webkit-line-clamp: 3; }
  tbody td.nowrap { white-space: normal; }
  td .secondary { font-size: 13px; }

  /* Pager: prev / page x of y / next */
  .pager { flex-direction: column; align-items: stretch; gap: 10px; }
  .pager > .muted:first-child { text-align: center; font-size: 12px; }
  .pager .pages { justify-content: space-between; }
  .pager .page-num, .pager .pages > .muted:not(.page-of) { display: none; }
  .page-of { display: inline; }
  .pager .page-btn { flex: 1; max-width: 120px; min-height: 44px; }
  .pager > label { text-align: center; }
  .pager select { padding: 8px 10px; min-height: 40px; }

  /* Detail drawer */
  .drawer { width: 100vw; height: 100dvh; }
  .drawer-head { padding: 14px 14px 12px; padding-top: calc(14px + env(safe-area-inset-top)); flex-wrap: wrap; }
  .drawer-head > div:first-child { flex: 1 1 100%; order: 2; }
  .drawer-nav { order: 1; width: 100%; justify-content: space-between; }
  .drawer-nav #drawerClose { order: 3; }
  .drawer-body { padding: 4px 14px calc(24px + env(safe-area-inset-bottom)); overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
  .kv dd { padding: 4px 0 10px; border-bottom: 1px solid var(--grid); }
  .kv dt { padding-top: 10px; }

  .footer { padding: 10px 14px calc(28px + env(safe-area-inset-bottom)); }
}
