:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #f0efea;
  --border: #e2e0d8;
  --text: #1c2228;
  --muted: #69707a;
  --accent: #0f6e6a;
  --accent-soft: #e2f0ee;
  --open: #1d7a4a;      --open-bg: #e4f3ea;
  --closed: #a1382e;    --closed-bg: #f8e7e4;
  --created: #2d5c9c;   --created-bg: #e5edf8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 24, 28, .05), 0 2px 8px rgba(20, 24, 28, .04);
  --font: 'General Sans', 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #121417;
  --surface: #1a1d21;
  --surface-2: #22262b;
  --border: #2e3339;
  --text: #e6e8ea;
  --muted: #9aa1a9;
  --accent: #2aa39c;
  --accent-soft: #173331;
  --open: #5ccb8e;      --open-bg: #16301f;
  --closed: #f0877b;    --closed-bg: #3a1d1a;
  --created: #82aef0;   --created-bg: #1b2a40;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-name { font-weight: 600; font-size: 16px; letter-spacing: -.01em; }
.brand-sub { font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.live { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--open); box-shadow: 0 0 0 0 color-mix(in srgb, var(--open) 60%, transparent); animation: pulse 2s infinite; }
.live.is-error .live-dot { background: var(--closed); animation: none; }
.live.is-busy .live-dot { background: var(--created); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--open) 55%, transparent); } 70% { box-shadow: 0 0 0 7px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }

.btn {
  white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); cursor: pointer; font-weight: 500; font-size: 13px;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn[disabled] { opacity: .6; cursor: progress; }
.btn-icon { width: 34px; padding: 0; justify-content: center; }
.btn-ghost { background: transparent; }
.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.wrap { max-width: 1320px; margin: 0 auto; padding: 20px 24px 40px; }

/* KPI cards */
.kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 16px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; min-height: 128px;
  cursor: pointer; transition: border-color .15s, transform .15s; text-align: left;
}
.kpi:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.kpi.is-selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.kpi-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.kpi-title { font-weight: 600; font-size: 14px; }
.kpi-meta { font-size: 12px; color: var(--muted); white-space: nowrap; }
.kpi-nums { display: flex; gap: 22px; margin: 10px 0 10px; }
.kpi-num { display: flex; flex-direction: column; }
.kpi-num b { font-size: 26px; font-weight: 600; letter-spacing: -.02em; font-variant-numeric: tabular-nums lining-nums; line-height: 1.1; }
.kpi-num span { font-size: 12px; color: var(--muted); }
.kpi-num.open b { color: var(--open); }
.kpi-num.closed b { color: var(--closed); }
.kpi-num.created b { color: var(--created); }
.bar { display: flex; height: 6px; border-radius: 3px; overflow: hidden; background: var(--surface-2); }
.bar i { display: block; height: 100%; }
.bar .b-open { background: var(--open); }
.bar .b-closed { background: var(--closed); opacity: .8; }
.bar .b-created { background: var(--created); opacity: .8; }
.kpi-error { margin-top: 8px; font-size: 12px; color: var(--closed); }
.skeleton { background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: shimmer 1.3s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* Filters */
.filters {
  display: grid; grid-template-columns: repeat(4, minmax(110px, 1fr)) minmax(190px, 1.6fr) minmax(140px, 1.1fr) auto; gap: 12px; align-items: end;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.field-actions { min-width: 0; }
.actions { display: flex; gap: 6px; }
select, input[type="search"] {
  height: 36px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); min-width: 0; width: 100%;
}
select { padding-right: 28px; }

/* Panels */
.grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); gap: 10px; }
.panel-head h2 { margin: 0; font-size: 14px; font-weight: 600; }
.legend { display: flex; gap: 6px; }
aside.panel { position: sticky; top: 76px; }

/* Table */
.table-wrap { max-height: calc(100dvh - 330px); min-height: 320px; overflow: auto; overscroll-behavior: contain; }
table.funds { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.funds th {
  position: sticky; top: 0; z-index: 1; background: var(--surface-2);
  text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  padding: 9px 12px; border-bottom: 1px solid var(--border); cursor: pointer; user-select: none; white-space: nowrap;
}
.funds th:hover { color: var(--text); }
.funds th.sorted::after { content: ' ↑'; }
.funds th.sorted.desc::after { content: ' ↓'; }
.funds td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.funds tbody tr:hover td { background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
.funds td.name a { color: var(--text); font-weight: 500; }
.funds td.name a:hover { color: var(--accent); }
.sub { display: block; font-size: 12px; color: var(--muted); margin-top: 1px; }
.empty { text-align: center; color: var(--muted); padding: 40px 12px !important; }
tr.flash td { animation: flash 2.4s ease-out; }
@keyframes flash { 0%, 30% { background: color-mix(in srgb, var(--accent) 22%, transparent); } 100% { background: transparent; } }

.pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill-open { color: var(--open); background: var(--open-bg); }
.pill-closed { color: var(--closed); background: var(--closed-bg); }
.pill-created { color: var(--created); background: var(--created-bg); }
.pill-unknown { color: var(--muted); background: var(--surface-2); }
.tag { display: inline-block; padding: 1px 7px; border-radius: 5px; font-size: 12px; font-weight: 500; background: var(--surface-2); border: 1px solid var(--border); white-space: nowrap; }
.tag-medicare { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.site { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* Changes feed */
.changes { list-style: none; margin: 0; padding: 6px 0; max-height: calc(100dvh - 330px); overflow: auto; }
.changes li { padding: 9px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.changes li:last-child { border-bottom: 0; }
.changes .c-name { font-weight: 500; }
.changes .c-meta { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; font-size: 12px; color: var(--muted); align-items: center; }
.changes .arrow { color: var(--muted); margin: 0 3px; }
.note { margin: 0; padding: 10px 16px 14px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 50; max-width: 360px;
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
}

@media (max-width: 1080px) {
  .filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .field-disease { grid-column: span 2; }
  .field-grow { grid-column: span 1; }
  .field-actions { grid-column: span 3; }
  .grid { grid-template-columns: 1fr; }
  aside.panel { position: static; }
  .table-wrap, .changes { max-height: none; }
}
@media (max-width: 760px) {
  .kpis { grid-template-columns: 1fr; }
  .topbar { padding: 10px 14px; }
  .brand-sub, .btn span { display: none; }
  .wrap { padding: 14px; }
  .filters { grid-template-columns: 1fr 1fr; }
  .field-disease, .field-grow, .field-actions { grid-column: 1 / -1; }
  .field-actions > span { display: none; }
  .actions .btn { flex: 1; justify-content: center; }
  .actions .btn span { display: inline; }
  .hide-sm, .legend { display: none; }
  .live #live-text { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

.funds td.since { white-space: nowrap; }
