/* ============================================================================
   Stats app — MASTER THEME (single source of truth; CLAUDE #6)
   Looker-Studio-inspired: white cards on cool grey, blue accent, quiet borders.
   EVERY color/size the UI or a chart uses is a token below — retheme here only.
   ============================================================================ */
:root {
  /* brand + interaction */
  --accent: #1a73e8;            /* links, active nav, focus, primary buttons */
  --accent-strong: #1557b0;     /* hover/pressed */
  --accent-soft: #e8f0fe;       /* active-tab wash, hover wash */

  /* neutrals (Google greys) */
  --bg: #f1f3f4;
  --surface: #ffffff;
  --surface-2: #f8f9fa;
  --border: #dadce0;
  --border-soft: #e8e8e8;
  --text: #202124;
  --text-2: #3c4043;
  --muted: #5f6368;
  --faint: #80868b;              /* AA-contrast on white (audit a11y) */
  --danger: #c5221f;

  /* chart series */
  --chart-1: #4472c4;           /* primary series (bars/lines)            */
  --chart-1-soft: #a8ccf0;      /* secondary/net series                   */
  --chart-compare: #b0b8c4;     /* prior-year comparison                  */
  --chart-grid: #e8e8e8;
  --chart-axis: #9aa0a6;
  --chart-ref: #9aa0a6;         /* reference/average lines                */

  /* semantic (goals, deltas) */
  --good: #137333;   --good-bg: #e6f4ea;
  --bad:  #c5221f;   --bad-bg:  #fce8e6;
  --warn: #b06000;   --warn-bg: #fff8e1;

  /* TC outcome trio (funnel/trend/ladder) */
  --c-sds: #137333;             /* Same Day Start    */
  --c-sch: #1a73e8;             /* Scheduled Start   */
  --c-pnd: #e07b39;             /* Pending           */
  --c-notx: #b0bec5;            /* not recommended   */

  /* referral types */
  --ref-professional: #4472c4;
  --ref-patient: #f4a500;
  --ref-family: #e84393;
  --ref-other: #9aa0a6;

  /* discount types */
  --disc-insurance: #70ad47;
  --disc-professional: #4472c4;
  --disc-family: #ed7d31;
  --disc-pif: #9b59b6;
  --disc-ad: #f0a500;
  --disc-other: #9aa0a6;

  /* conversion-speed buckets — green > yellow > orange > red */
  --speed-fast: #137333;   /* ≤30d   */
  --speed-mid: #f4a500;    /* 31–60d */
  --speed-slow: #e8710a;   /* 61–120d */
  --speed-crit: #c5221f;   /* >120d  */

  /* observation program statuses (census stack, OBS 1 → 4) */
  --obs-1: #8e7cc3;
  --obs-2: #4472c4;
  --obs-3: #45b6b0;
  --obs-4: #6aa84f;

  /* emergency appointment types (Assistant dashboard) */
  --emerg-c1: #8e7cc3;     /* C1 comfort */
  --emerg-r2: #e06666;     /* R2 repair  */

  /* heatmap scale (0 → 6+) */
  --heat-0: #f8f9fa; --heat-1: #d0e4f7; --heat-2: #a8ccf0; --heat-3: #6fa8e0;
  --heat-4: #4472c4; --heat-5: #2a5aab; --heat-6: #1a3e80;

  /* type scale — ONE standardized size per text role; use these, never raw px */
  --fs-title: 15px;         /* card/section titles */
  --fs-body: .875rem;       /* buttons, inputs, tabs, controls */
  --fs-table: .84rem;       /* data-grid + summary-table cells */
  --fs-label: .8rem;        /* popovers, toolbar labels */
  --fs-caption: 11.5px;     /* fine print under numbers, legends */
  --fs-eyebrow: 10.5px;     /* uppercase micro-labels */
  --fs-kpi: 25px;           /* KPI hero numbers */
  --fs-chart-axis: 11px;    /* SVG: axes + column headers */
  --fs-chart-value: 11px;   /* SVG: in-chart numbers */
  --fs-chart-name: 12.5px;  /* SVG: row/series names, trend cells */
  --fs-chart-big: 13.5px;   /* SVG: emphasized totals */

  /* misc */
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --shadow-card: 0 1px 4px rgba(0,0,0,.07);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --chip: var(--surface-2);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--text); display: flex; flex-direction: column; min-height: 100vh; font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }

/* ── top bar ─────────────────────────────────────────────────────────────── */
.topbar { display: flex; align-items: center; gap: 1rem; padding: 0 1rem; height: 52px; background: var(--surface);
  border-bottom: 1px solid var(--border); box-shadow: var(--shadow); position: sticky; top: 0; z-index: 5; }
.brand { font-weight: 700; color: var(--text); white-space: nowrap; font-size: .95rem; margin-right: 2.25rem; }
.brand::before { content: ""; display: inline-block; width: .62rem; height: .62rem; border-radius: 3px;
  background: var(--accent); margin-right: .45rem; }
.brand span { color: var(--muted); font-weight: 500; }
.spacer { flex: 1; }
.tabs { display: flex; gap: .1rem; flex-wrap: wrap; align-self: stretch; align-items: stretch; }
.tab { border: none; background: transparent; color: var(--muted); padding: 0 .8rem; cursor: pointer;
  font-size: .88rem; font-weight: 500; border-bottom: 3px solid transparent; border-top: 3px solid transparent; }
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.icon-btn { border: 1px solid var(--border); background: var(--surface); color: var(--text);
  width: 2rem; height: 2rem; border-radius: 50%; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.btn { border: 1px solid var(--border); background: var(--surface); color: var(--text-2);
  padding: .4rem .9rem; border-radius: 8px; cursor: pointer; font-size: var(--fs-body); }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-strong); }
.btn.small { padding: .3rem .6rem; font-size: .82rem; }
.btn.danger { color: var(--danger); border-color: var(--danger); }

/* ── data workspace (grid) ───────────────────────────────────────────────── */
.workspace { flex: 1; display: flex; flex-direction: column; padding: .75rem 1rem; gap: .5rem; min-height: 0; }
.gridhead { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.gridhead h2 { margin: 0; font-size: 1.05rem; }
.gridfoot { text-align: center; padding: .4rem 0; }
.input { border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .35rem .55rem; border-radius: 8px; font-size: var(--fs-body); }
.input:focus, .btn:focus-visible, .tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }   /* visible focus everywhere (audit a11y) */
#search { width: 14rem; max-width: 40vw; }
.monthnav { display: inline-flex; align-items: center; gap: .25rem; }
.monthnav .input { width: 9.5rem; }
.monthnav .input.yearinput { width: 5.5rem; text-align: center; }

/* login page — Cognito button primary; local form is break-glass, revealed by a small link */
.login-card .sso-btn { display: block; text-align: center; text-decoration: none; margin: .2rem 0 .1rem; }
.login-card .link-btn { align-self: center; background: none; border: 0; color: var(--muted);
  font-size: .76rem; text-decoration: underline; cursor: pointer; padding: .35rem; }
.login-card .link-btn:hover { color: var(--accent); }
.sso-or { display: flex; align-items: center; gap: .6rem; color: var(--muted); font-size: .76rem; margin: .1rem 0 .3rem; }
.sso-or::before, .sso-or::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* filtered views — a single toolbar dropdown; accent-styled while a view is active
   so a filtered table reads as filtered at a glance */
.view-picker { display: inline-flex; align-items: center; gap: .35rem; }
.views-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
#view-select { padding-right: 1.6rem; }
#view-select.active { background: var(--accent-soft); color: var(--accent-strong);
  border-color: var(--accent); font-weight: 600; }

/* data-integrity flag on a cell (e.g. Same-Day Start mismatch) */
.grid td.flag-error { background: color-mix(in srgb, var(--bad) 13%, transparent);
  box-shadow: inset 3px 0 0 var(--bad); cursor: help; }
.flag-warn { color: var(--bad); font-weight: 700; margin-right: .3rem; cursor: help; }

/* row-number rail (event tables): a real first column so it can never drift.
   Numbers come from a CSS counter — add/delete a row and they renumber themselves. */
#grid tbody { counter-reset: rn; }
#grid tbody tr:not(.entry) td.rownum { counter-increment: rn; }
#grid tbody tr:not(.entry) td.rownum::before { content: counter(rn); }
.grid th.rownum, .grid td.rownum {
  position: sticky; left: 0; z-index: 2; background: var(--surface);
  width: 2.4rem; min-width: 2.4rem; max-width: 2.4rem;
  padding: .32rem .45rem .32rem .2rem; text-align: right;
  color: var(--faint); font-size: .7rem; font-variant-numeric: tabular-nums; user-select: none; }
.grid thead th.rownum { z-index: 4; background: var(--surface-2); }
.grid tbody tr:hover td.rownum { background: color-mix(in srgb, var(--accent) 5%, var(--surface)); }
.grid tr.row-locked td.rownum { background: color-mix(in srgb, #000 3%, var(--surface)); }

.grid-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); flex: 1; min-height: 0; }
.grid { border-collapse: separate; border-spacing: 0; font-size: var(--fs-table); }
.grid th, .grid td { padding: .32rem .5rem; border-bottom: 1px solid var(--border-soft);
  border-right: 1px solid var(--border-soft); text-align: left; }
.grid td { white-space: nowrap; }
.grid thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 3; font-weight: 600;
  border-bottom: 2px solid var(--border); vertical-align: bottom; white-space: normal;
  overflow-wrap: normal; line-height: 1.2; }
.grid thead th .lbl { display: inline; }
.grid tbody tr:hover td { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.ellip { max-width: 12rem; overflow: hidden; text-overflow: ellipsis; }
.grid td.empty { color: var(--muted); }
.grid td.calc { background: color-mix(in srgb, var(--accent) 4%, transparent); font-style: italic; }
.grid .flag { color: var(--muted); font-size: .72rem; }

.grid th.freeze, .grid td.freeze { position: sticky; z-index: 2; background: var(--surface); }
.grid thead th.freeze { z-index: 4; background: var(--surface-2); }
.grid tbody tr:hover td.freeze { background: color-mix(in srgb, var(--accent) 5%, var(--surface)); }
.grid td.freeze, .grid th.freeze { border-right: 1px solid var(--border-soft); }
.grid .freeze.freeze-last { box-shadow: 2px 0 4px -2px rgba(0,0,0,.15); }

/* subtle column-section boundary (fields.section_start in the registry) */
.grid th.sec-start, .grid td.sec-start { border-left: 2px solid var(--border); }

.grid td.editable { cursor: cell; }
.grid td.editable:hover { outline: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: -1px; }
.grid td.editing { padding: 0; }
.cell-input { width: 100%; min-width: 0; border: 2px solid var(--accent); background: var(--surface);
  color: var(--text); padding: .28rem .4rem; font: inherit; font-size: .84rem; border-radius: 4px; }

.grid tr.entry td { padding: 0; background: color-mix(in srgb, var(--accent) 3%, var(--surface)); overflow: hidden; }
.grid tr.entry td.disabled { background: var(--surface-2); }
.entry-input { width: 100%; min-width: 0; border: 1px solid transparent; background: transparent;
  color: var(--text); padding: .32rem .4rem; font: inherit; font-size: .84rem; }
.entry-input:focus { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--surface); border-radius: 3px; }
.entry-input[type=number]::-webkit-inner-spin-button, .entry-input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.entry-input[type=number] { -moz-appearance: textfield; }
.entry-input[type=date] { min-width: 0; width: 100%; }
.entry-input[type=date]::-webkit-calendar-picker-indicator { display: none; }

/* conditional formatting + locks */
.grid td.ruled { background: var(--rule-bg, transparent) !important; color: var(--rule-text, inherit); }
.grid td.rule-bold { font-weight: 700; }
.grid td.hidden-cell { background: repeating-linear-gradient(45deg, transparent, transparent 4px,
  color-mix(in srgb, var(--border) 50%, transparent) 4px, color-mix(in srgb, var(--border) 50%, transparent) 5px); }
.grid tr.row-locked td { background: color-mix(in srgb, #000 3%, transparent); }
.grid tr.row-locked td.freeze { background: color-mix(in srgb, #000 3%, var(--surface)); }
.grid tr.row-locked td.editable { cursor: not-allowed; }
.grid thead th.th-lockable { cursor: context-menu; }
.grid thead th.th-locked { background: color-mix(in srgb, var(--accent) 10%, var(--surface-2)); }
.grid thead th.th-locked::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0;
  height: 2px; background: var(--accent); }

/* ── modal ───────────────────────────────────────────────────────────────── */
.modal { position: fixed; inset: 0; background: rgba(32,33,36,.5); display: grid; place-items: center; z-index: 20; padding: 1rem; }
.modal[hidden] { display: none; }
.modal-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 40px rgba(0,0,0,.25); width: min(560px, 95vw); max-height: 90vh; display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border-soft); }
.modal-head h3 { margin: 0; font-size: 1.02rem; }
.modal-body { padding: 1.25rem; overflow: auto; }
.rec-form { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.rec-form .fld { display: flex; flex-direction: column; gap: .2rem; font-size: .82rem; }
.modal-actions { grid-column: 1 / -1; display: flex; gap: .5rem; justify-content: flex-end; margin-top: .5rem; }
/* free-form textareas in a dialog should fill the width, not sit at the default ~20 cols */
#modal-body textarea { width: 100%; box-sizing: border-box; resize: vertical; min-height: 4.5rem; }
#modal-body > p { margin: 0 0 .8rem; }
.history { display: flex; flex-direction: column; gap: .4rem; }
.hist-row { display: grid; grid-template-columns: 12rem 1fr auto; gap: .75rem; align-items: baseline;
  padding: .35rem 0; border-bottom: 1px solid var(--border-soft); }

/* ── shared view scaffolding ─────────────────────────────────────────────── */
.workspace[hidden], .scorecard-view[hidden], .dash-view[hidden] { display: none; }

/* ── dashboards shell ────────────────────────────────────────────────────── */
.dash-view { flex: 1; display: flex; flex-direction: column; padding: 0; min-height: 0; overflow: auto; background: var(--bg); }
.dash-bar { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; padding: .55rem 1.25rem;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 4; }
.dash-pick { display: inline-flex; align-items: center; gap: .45rem; }
.dash-pick label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--faint); }
.dash-pick select { font-weight: 600; }
.page-tabs { display: inline-flex; gap: .25rem; flex-wrap: wrap; }
.page-tab { border: 1px solid transparent; background: transparent; color: var(--muted); padding: .34rem .8rem;
  border-radius: 999px; cursor: pointer; font-size: .84rem; font-weight: 500; }
.page-tab:hover { background: var(--surface-2); color: var(--text); }
.page-tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.dash-filter { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: transparent; color: var(--text-2); padding: .3rem .7rem; font: inherit; font-size: .82rem; cursor: pointer; }
.seg button.on { background: var(--accent); color: #fff; }

.dash-pages { padding: 1.1rem 1.25rem 2.5rem; display: flex; flex-direction: column; gap: 1rem; max-width: 1340px; margin: 0 auto; width: 100%; }
.dash-loading { color: var(--muted); text-align: center; padding: 3rem 0; }

/* card grid: charts arrange 2-up where space allows */
.card { background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--radius);
  box-shadow: var(--shadow-card); padding: 18px 20px 16px; grid-column: span 12; min-width: 0; }
.card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: .75rem; margin-bottom: 2px; }
.card-title { font-size: var(--fs-title); font-weight: 700; color: var(--text); }

/* legends (shared look for every chart) */
.legend { display: flex; gap: 18px; flex-wrap: wrap; margin: 10px 0 14px; }
.leg { display: flex; align-items: center; gap: 7px; font-size: var(--fs-caption); color: var(--muted); }
.leg-box { width: 13px; height: 13px; border-radius: 3px; flex-shrink: 0; }
.leg-line { width: 18px; height: 0; border-top: 2px dashed var(--chart-ref); flex-shrink: 0; }
.leg-line.solid { border-top-style: solid; }

/* KPI scorecards (mockup style) */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; margin-top: 16px; }
.kpi { background: var(--surface-2); border: 1px solid var(--border-soft); border-radius: 8px; padding: 12px 14px; text-align: center;
  min-height: 6rem; display: flex; flex-direction: column; justify-content: center; }   /* uniform card height */
.kpi-label { font-size: var(--fs-eyebrow); font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--faint); margin-bottom: 6px; }
.kpi-val { font-size: var(--fs-kpi); font-weight: 700; color: var(--text); line-height: 1; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: var(--fs-caption); color: var(--faint); margin-top: 4px; }
.kpi-delta { font-size: 11px; font-weight: 700; margin-top: 4px; }
.kpi-delta.up { color: var(--good); } .kpi-delta.down { color: var(--bad); } .kpi-delta.flat { color: var(--faint); }

/* goal badges (funnel cards) */
.goal-badge { font-size: 10px; font-weight: 700; padding: 2px 9px; border-radius: 10px; }
.badge-good { background: var(--good-bg); color: var(--good); }
.badge-warn { background: var(--bad-bg); color: var(--bad); }
.badge-ok { background: var(--warn-bg); color: var(--warn); }

/* outcome funnel (P1_04): rows of UNIFORM, softly tinted cards + svg connectors.
   Every card is the same width; the stage color is a subtle full-card wash
   (not a colored top strip) so the trio reads on the white page. */
.funnel-cards { --fnl-w: 230px; }
.fnl-row1 { display: flex; justify-content: center; }
/* the funnel spine (Total → Tx-Rec → outcomes) runs straight down the CENTER;
   Not-Recommended is the side branch, offset to the right (owner 2026-07-10) */
.fnl-row2 { display: grid; grid-template-columns: 1fr var(--fnl-w) 1fr; align-items: stretch; }
.fnl-row2 .tx { grid-column: 2; }
.fnl-row2 .side { grid-column: 3; display: flex; justify-content: flex-start; padding-left: 64px; }
.fnl-row3 { display: flex; justify-content: center; align-items: stretch; gap: 48px; }
.fnl-card { width: var(--fnl-w); box-sizing: border-box; background: var(--surface);
  border: 1px solid var(--border-soft); border-radius: 10px;
  box-shadow: var(--shadow-card); padding: 14px 16px; text-align: center; }
.fnl-eyebrow { font-size: var(--fs-eyebrow); font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--faint); margin-bottom: 8px; }
.fnl-big { font-size: 32px; font-weight: 700; color: var(--text); line-height: 1; }
.fnl-sub { font-size: 11.5px; color: var(--muted); margin-top: 6px; }
.fnl-card.total { background: var(--surface-2); }
.fnl-card.tx { background: color-mix(in srgb, var(--chart-1) 8%, var(--surface));
  border-color: color-mix(in srgb, var(--chart-1) 30%, var(--border-soft)); }
.fnl-card.notrec { background: color-mix(in srgb, var(--c-notx) 12%, var(--surface));
  border-color: color-mix(in srgb, var(--c-notx) 35%, var(--border-soft)); }
.fnl-cats { font-size: 11px; color: var(--muted); margin-top: 8px; line-height: 1.7; }
.fnl-cats b { color: var(--text-2); }
.fnl-card .pct { font-size: 34px; font-weight: 700; line-height: 1; }
.fnl-card .count { font-size: var(--fs-chart-name); font-weight: 600; margin-top: 7px; }
.fnl-card.tx .pct, .fnl-card.tx .count { color: var(--chart-1); }
.fnl-card.notrec .pct, .fnl-card.notrec .count { color: var(--muted); }
.fnl-out.sds { background: color-mix(in srgb, var(--c-sds) 8%, var(--surface));
  border-color: color-mix(in srgb, var(--c-sds) 30%, var(--border-soft)); }
.fnl-out.sds .pct, .fnl-out.sds .count { color: var(--c-sds); }
.fnl-out.sch { background: color-mix(in srgb, var(--c-sch) 8%, var(--surface));
  border-color: color-mix(in srgb, var(--c-sch) 30%, var(--border-soft)); }
.fnl-out.sch .pct, .fnl-out.sch .count { color: var(--c-sch); }
.fnl-out.pnd { background: color-mix(in srgb, var(--c-pnd) 9%, var(--surface));
  border-color: color-mix(in srgb, var(--c-pnd) 32%, var(--border-soft)); }
.fnl-out.pnd .pct, .fnl-out.pnd .count { color: var(--c-pnd); }
.fnl-goal { display: flex; align-items: center; justify-content: center; gap: 7px; margin-top: 12px; padding-top: 10px;
  border-top: 1px solid color-mix(in srgb, #000 10%, transparent); font-size: 10px; color: var(--faint); }
.fnl-connector { display: block; width: 100%; overflow: visible; }

/* chart containers + tooltip (one style for every chart) */
.chart-wrap { position: relative; }
.chart-wrap svg { display: block; width: 100%; }
.chart-tip { position: absolute; background: var(--text); color: #fff; border-radius: 7px; padding: 9px 12px;
  font-size: 12px; line-height: 1.75; pointer-events: none; opacity: 0; transition: opacity .12s; z-index: 40;
  white-space: nowrap; box-shadow: 0 4px 16px rgba(0,0,0,.25); }
.chart-tip strong { font-size: 10.5px; display: block; margin-bottom: 3px; color: #bdc1c6; text-transform: uppercase; letter-spacing: .06em; }
.chart-tip .tt-row { display: flex; align-items: center; gap: 7px; }
.chart-tip .tt-dot { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }
.chart-tip .tt-total { margin-top: 4px; padding-top: 4px; border-top: 1px solid rgba(255,255,255,.18); font-weight: 700; }

/* heatmap */
.heat-note { font-size: 11px; color: var(--faint); margin-top: 8px; }

.tab[hidden] { display: none; }   /* permission-gated tabs (e.g. Summary) truly disappear */

/* app-wide error banner (jfetch failures — dependability, audit #6) */
.app-error { position: fixed; top: .8rem; left: 50%; transform: translateX(-50%); z-index: 100;
  background: var(--bad-bg); color: var(--bad); border: 1px solid color-mix(in srgb, var(--bad) 35%, transparent);
  border-radius: 8px; padding: .5rem .9rem; font-size: .85rem; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,.18); max-width: min(34rem, 92vw); }

/* ── Summary (KPI scorecard table) ───────────────────────────────────────── */
.scorecard-view { flex: 1; display: flex; flex-direction: column; padding: .75rem 1rem; gap: .6rem; min-height: 0; }
.sc-head { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.sc-head h2 { margin: 0; font-size: 1.05rem; }
.sc-tabs { display: flex; gap: .25rem; }
.sc-tab { border: 1px solid transparent; background: transparent; color: var(--muted); padding: .3rem .8rem;
  border-radius: 999px; cursor: pointer; font-size: .85rem; }
.sc-tab:hover { background: var(--surface-2); color: var(--text); }
.sc-tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sc-controls { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.sc-cmp { display: inline-flex; align-items: center; gap: .3rem; font-size: .82rem; color: var(--muted); }
.sc-cmp .input { padding: .25rem .4rem; }
.sc-wrap { overflow: auto; border: 1px solid var(--border-soft); border-radius: var(--radius); background: var(--surface); flex: 1; min-height: 0; }
#sc-table { border-collapse: separate; border-spacing: 0; font-size: var(--fs-table); width: 100%; }
#sc-table th, #sc-table td { padding: .4rem .7rem; border-bottom: 1px solid var(--border-soft); white-space: nowrap; text-align: right; }
#sc-table thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 2; font-weight: 600; }
#sc-table th.sc-metric, #sc-table td.sc-metric { position: sticky; left: 0; background: var(--surface); text-align: left; z-index: 1; white-space: nowrap; padding-right: 1.4rem; }
#sc-table thead th.sc-metric { z-index: 3; background: var(--surface-2); }
#sc-table td.num { font-variant-numeric: tabular-nums; }
#sc-table th.cur, #sc-table td.cur { background: color-mix(in srgb, var(--accent) 7%, var(--surface)); }
#sc-table th.base, #sc-table td.base { background: color-mix(in srgb, var(--muted) 8%, var(--surface)); }
#sc-table tr.sc-group td { background: color-mix(in srgb, var(--accent) 7%, var(--surface-2)); color: var(--accent-strong);
  font-weight: 700; text-transform: uppercase; letter-spacing: .05em; font-size: .72rem; text-align: left; position: sticky; left: 0; }
#sc-table tbody tr:hover td:not(.sc-group) { background: color-mix(in srgb, var(--accent) 4%, transparent); }
.sc-delta-h { text-align: right; }
.sc-delta { font-weight: 600; }
.sc-delta.up { color: var(--good); } .sc-delta.down { color: var(--bad); } .sc-delta.flat { color: var(--muted); }
/* Δ trio: arrow | absolute change | relative %. width:1% shrink-wraps each column so
   the table's surplus width goes to the period columns — the trio stays compact, pinned
   to the right edge under its single Δ header. */
.sc-delta { width: 1%; }
.sc-delta.arw-c { font-size: .72rem; text-align: right; padding: .4rem .15rem .4rem .9rem;
  border-left: 1px solid var(--border-soft); }
.sc-delta.num { text-align: right; padding-left: .3rem; padding-right: .3rem; }
.sc-delta.rel-c { font-weight: 500; font-size: .78rem; opacity: .8; padding-left: .25rem; }
/* running-period badge on its column header ("thru Jun" / "partial") */
.sc-partial { display: block; font-size: .62rem; font-weight: 500; color: var(--muted);
  text-transform: uppercase; letter-spacing: .03em; }
.spark { vertical-align: middle; }

/* ── admin console ───────────────────────────────────────────────────────── */
.admin-tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--border-soft); margin-bottom: 1rem; }
.atab { border: none; background: transparent; color: var(--muted); padding: .5rem .9rem; cursor: pointer; border-bottom: 2px solid transparent; }
.atab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.admin-panel { min-height: 12rem; }
.input.small { padding: .25rem .4rem; font-size: .82rem; }
.list-area { margin-top: .75rem; display: flex; flex-direction: column; gap: .4rem; }
.item-row { display: flex; align-items: center; gap: .5rem; }
.item-row .input { flex: 1; }
.item-row input[type=color] { width: 2.2rem; height: 2rem; border: 1px solid var(--border); border-radius: 6px; padding: 0; }
.item-row.add { margin-top: .5rem; padding-top: .5rem; border-top: 1px dashed var(--border); }
.user-row { border: 1px solid var(--border-soft); border-radius: 8px; padding: .75rem; margin-bottom: .6rem; }
.user-head { display: flex; align-items: center; gap: .6rem; }
.user-head .toggle { margin-left: auto; display: flex; align-items: center; gap: .3rem; }
.rolebox { display: flex; flex-wrap: wrap; gap: .5rem; margin: .5rem 0; }
.rolechk { display: flex; align-items: center; gap: .25rem; background: var(--surface-2); padding: .2rem .5rem; border-radius: 999px; }
.pwrow, .user-add { display: flex; gap: .5rem; align-items: center; }
.user-add { margin-top: .4rem; }
.audit-bar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-bottom: .6rem; }
.audit-count { margin-left: auto; }
.audit-wrap { overflow: auto; max-height: 55vh; border: 1px solid var(--border-soft); border-radius: 8px; }
.audit-table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: .82rem; }
.audit-table th, .audit-table td { padding: .35rem .5rem; border-bottom: 1px solid var(--border-soft); text-align: left; white-space: nowrap; vertical-align: top; }
.audit-table thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 1; font-weight: 600; }
.audit-when { font-variant-numeric: tabular-nums; }
.audit-change { white-space: normal; max-width: 22rem; word-break: break-word; color: var(--muted); }
.act-pill { display: inline-block; padding: .06rem .45rem; border-radius: 999px; font-size: .74rem; font-weight: 600; background: var(--surface-2); }
.act-create { background: var(--good-bg); color: var(--good); }
.act-update { background: var(--accent-soft); color: var(--accent-strong); }
.act-delete { background: var(--bad-bg); color: var(--bad); }
.act-lock, .act-unlock { background: var(--warn-bg); color: var(--warn); }
.act-permission_change, .act-list_edit, .act-import { background: #ece6ff; color: #5a3ea0; }
.perm-table { border-collapse: collapse; width: 100%; }
.perm-table th, .perm-table td { border: 1px solid var(--border-soft); padding: .4rem .5rem; text-align: left; }
.perm-table select { width: 100%; }
.perm-sec { margin: 1.1rem 0 .4rem; font-size: .72rem; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--faint); }

/* ── context menu ────────────────────────────────────────────────────────── */
.ctxmenu { position: absolute; z-index: 30; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.2); padding: .3rem; min-width: 12rem; }
.ctx-item { display: block; width: 100%; text-align: left; border: none; background: transparent;
  color: var(--text); padding: .45rem .6rem; border-radius: 6px; cursor: pointer; font-size: .9rem; }
.ctx-item:hover { background: var(--surface-2); }
.ctx-item.ctx-danger { color: var(--danger); }
.ctx-item.ctx-danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.ctx-sep { height: 1px; background: var(--border-soft); margin: .25rem .3rem; }

/* notes column: plain text, truncated at 2x the old width; full note on hover; dbl-click edits */
.grid td.note-cell { max-width: 22rem; overflow: hidden; text-overflow: ellipsis; }

/* notes popups: styled hover viewer + editor with click-out save */
.note-pop { position: absolute; z-index: 60; max-width: 26rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 28px rgba(0,0,0,.16);
  padding: .6rem .8rem; font-size: .84rem; line-height: 1.55; color: var(--text-2);
  white-space: pre-wrap; word-break: break-word; pointer-events: none; }
.note-editor { position: absolute; z-index: 61; width: min(24rem, 92vw); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 10px 32px rgba(0,0,0,.2);
  padding: .6rem; }
.note-editor textarea { width: 100%; min-height: 6.5rem; resize: vertical; font: inherit;
  font-size: .86rem; line-height: 1.5; color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: .45rem .55rem; }
.note-editor textarea:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.note-editor .ne-actions { display: flex; justify-content: flex-end; gap: .4rem; margin-top: .45rem; }

/* ── chips (list-option colors are data, applied via --chip) ─────────────── */
.chip { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem;
  background: var(--chip); color: var(--text-2); border: 1px solid rgba(0,0,0,.06); }

/* ── login ───────────────────────────────────────────────────────────────── */
.login-wrap { flex: 1; display: grid; place-items: center; padding: 2rem; }
.login-card { background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--radius);
  box-shadow: var(--shadow-card); padding: 2rem; width: min(360px, 92vw); display: flex; flex-direction: column; gap: .8rem; }
.brand.big { font-weight: 700; color: var(--text); font-size: 1.25rem; }
.brand.big span { color: var(--muted); font-weight: 500; }
.fld { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; }
.fld .input { width: 100%; }
.err { color: var(--danger); font-size: .85rem; margin: 0; }
.boot { max-width: 640px; margin: 12vh auto; padding: 2rem; text-align: center; }
.boot h1 { color: var(--accent); }

/* ============================================================================
   MOBILE SHELL — activated by html.mobile (set in app.js via UA/viewport).
   A distinct phone experience: bottom navigation, single-column stacked cards,
   all dashboard pages in one scroll, horizontally-scrollable wide charts.
   ============================================================================ */
html:not(.mobile) .m-only { display: none !important; }
html:not(.mobile) .bottom-nav { display: none !important; }

html.mobile body { font-size: 15px; }
html.mobile .topbar { height: 48px; padding: 0 .8rem; gap: .5rem; }
html.mobile .topbar .tabs { display: none; }              /* nav moves to the bottom bar */
html.mobile .topbar #whoami { display: none; }
html.mobile .brand span { display: none; }

/* bottom navigation */
.bottom-nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; display: flex;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0); box-shadow: 0 -1px 6px rgba(0,0,0,.06); }
.bn-item { flex: 1; border: none; background: transparent; color: var(--muted); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: .5rem 0 .55rem;
  font-size: .68rem; font-weight: 600; }
.bn-item svg { width: 22px; height: 22px; }
.bn-item.active { color: var(--accent); }
html.mobile main.workspace, html.mobile .scorecard-view, html.mobile .dash-view {
  padding-bottom: calc(64px + env(safe-area-inset-bottom, 0)); }

/* data view on phone */
html.mobile .gridhead { gap: .4rem; }
html.mobile .gridhead h2 { display: none; }               /* the dataset select is the title */
html.mobile #m-dataset { font-weight: 700; font-size: 1rem; max-width: 60vw; }
html.mobile #search { width: 100%; max-width: none; order: 9; }
html.mobile .grid { font-size: .8rem; }
html.mobile .grid td.ellip { max-width: 9rem; }

/* summary view on phone */
html.mobile .sc-head { gap: .5rem; }
html.mobile .sc-controls { width: 100%; }
html.mobile .sc-cmp { flex-wrap: wrap; }

/* dashboards on phone — every page stacked in one scroll */
html.mobile .dash-bar { padding: .5rem .8rem; gap: .5rem; position: static; }
html.mobile .page-tabs { width: 100%; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
html.mobile .page-tabs::-webkit-scrollbar { display: none; }
html.mobile .page-tab { white-space: nowrap; flex-shrink: 0; }
html.mobile .dash-filter { width: 100%; }
html.mobile .dash-pages { padding: .8rem .8rem 2rem; gap: .8rem; }
html.mobile .card { padding: 14px 14px 12px; }
html.mobile .kpi-row { grid-template-columns: repeat(2, 1fr); gap: 8px; }
html.mobile .kpi-val { font-size: 21px; }
html.mobile .legend { gap: 10px 14px; margin: 8px 0 10px; }

/* mobile section headers between stacked dashboard pages */
.m-section-head { display: flex; align-items: baseline; gap: .5rem; margin: 1.2rem 0 .2rem;
  padding-top: .8rem; border-top: 2px solid var(--border-soft); }
.m-section-head:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.m-section-head h2 { margin: 0; font-size: 1.02rem; color: var(--text); }
.m-section-head .n { font-size: .7rem; font-weight: 700; color: var(--accent);
  background: var(--accent-soft); border-radius: 999px; padding: .1rem .5rem; }

/* funnel cards stack vertically on phone; connectors are a desktop affordance */
html.mobile .fnl-connector { display: none; }
html.mobile .fnl-row2, html.mobile .fnl-row3 { display: flex; flex-direction: column; align-items: stretch; gap: 10px; }
html.mobile .fnl-row2 .side { padding-left: 0; }
html.mobile .fnl-row3 { margin-top: 10px; }
html.mobile .fnl-row1 + * { margin-top: 10px; }
html.mobile .fnl-card { width: 100%; }
html.mobile .fnl-big { font-size: 30px; }
html.mobile .fnl-out .pct { font-size: 38px; }

/* wide charts scroll sideways instead of shrinking into mush */
html.mobile .chart-wrap, html.mobile .sankey-wrap, html.mobile .funnel-wrap, html.mobile .quality-wrap {
  overflow-x: auto; -webkit-overflow-scrolling: touch; }
html.mobile .dash-stats { gap: .8rem 1.2rem; }
html.mobile .dash-stats .stat-v { font-size: 1.15rem; }
html.mobile .dq-legend .lg-agg { margin-left: 0; width: 100%; }

/* pipeline bars rows tighten */
html.mobile .bar-row { grid-template-columns: 7.5rem 1fr 4.6rem; gap: .45rem; }
html.mobile .bar-label { font-size: .78rem; }

/* modal near-fullscreen on phone */
html.mobile .modal { padding: .5rem; }
html.mobile .modal-card { width: 100%; max-height: 96vh; }
html.mobile .rec-form { grid-template-columns: 1fr; }

/* ── UX sprint additions (audit #10) ─────────────────────────────────────── */
/* selected cell — the keyboard home base: arrows move, Enter edits, Esc clears */
.grid td.cell-selected, .grid td:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

/* the row-number cell doubles as the row-actions (⋯) button on hover */
#grid tbody tr:not(.entry):hover td.rownum { cursor: pointer; }
#grid tbody tr:not(.entry):hover td.rownum::before { content: "⋯"; color: var(--accent); font-weight: 700; font-size: .9rem; }

/* admin gear on dashboard cards (same action as right-click → permissions) */
.card-top { position: relative; }
.card-gear { position: absolute; top: 0; right: 0; border: none; background: transparent;
  color: var(--faint); font-size: .95rem; cursor: pointer; opacity: 0; padding: .1rem .3rem; border-radius: 6px; }
.card:hover .card-gear, .card-gear:focus-visible { opacity: 1; }
.card-gear:hover { background: var(--surface-2); color: var(--accent); }

/* toast variants + action button (Undo) */
.app-error.ok { background: var(--good-bg); color: var(--good); border-color: color-mix(in srgb, var(--good) 35%, transparent); }
.toast-act { margin-left: .7rem; border: 1px solid currentColor; background: transparent; color: inherit;
  border-radius: 6px; padding: .15rem .55rem; font: inherit; font-size: .8rem; cursor: pointer; }

/* grid empty state */
.grid tr.grid-msg td { color: var(--muted); font-style: italic; padding: 1rem .8rem; border-right: none; }

/* tablet (700–980px): toolbars wrap instead of cramming (audit mobile #2) */
@media (max-width: 980px) {
  .gridhead, .dash-bar, .sc-head { flex-wrap: wrap; row-gap: .4rem; }
  #search { max-width: 12rem; }
}

/* ── print: clean paper output for meeting handouts (audit CSS #1) ────────── */
@media print {
  .topbar, .monthnav, #search, #sort, #add, #more, .gridfoot, .bottom-nav,
  .dash-filter, .sc-controls, .app-error, .card-gear, .ctxmenu, .note-pop, .note-editor,
  #m-dataset { display: none !important; }
  body { background: #fff; }
  .workspace, .scorecard-view, .dash-pages { padding: 0; }
  .grid-wrap, .sc-wrap { overflow: visible; border: none; box-shadow: none; }
  .grid th, .grid td, #sc-table thead th, #sc-table .sc-metric { position: static !important; }
  #grid tbody tr:not(.entry):hover td.rownum::before { content: counter(rn); }
  .grid tr.entry { display: none; }
  .card { break-inside: avoid; box-shadow: none; }
}

/* ── card info popovers + tinted type KPIs (owner 2026-07-10) ─────────────── */
.card-titlerow { display: flex; align-items: center; gap: .45rem; }
.info-dot { position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 15px; height: 15px; border: 1.4px solid var(--faint); border-radius: 50%;
  color: var(--faint); font-size: 9.5px; font-weight: 700; font-style: italic; font-family: Georgia, serif;
  cursor: help; user-select: none; flex-shrink: 0; }
.info-dot:hover, .info-dot:focus-visible { border-color: var(--accent); color: var(--accent); }
.info-pop { display: none; position: absolute; top: calc(100% + 8px); left: -10px; z-index: 60;
  width: max-content; max-width: min(24rem, 85vw); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.16);
  padding: .6rem .8rem; font-size: .8rem; font-weight: 400; font-style: normal;
  font-family: var(--font); color: var(--text-2); line-height: 1.55; white-space: normal; text-align: left; }
.info-pop::before { content: ""; position: absolute; top: -5px; left: 13px; width: 8px; height: 8px;
  background: var(--surface); border-left: 1px solid var(--border); border-top: 1px solid var(--border);
  transform: rotate(45deg); }
.info-dot:hover .info-pop, .info-dot:focus .info-pop, .info-dot:focus-visible .info-pop { display: block; }

/* per-discount-type wash on the Avg $ cards — matches the stacked chart's series colors */
.kpi.kpi-tint { background: color-mix(in srgb, var(--kpi-c, var(--chart-1)) 8%, var(--surface));
  border-color: color-mix(in srgb, var(--kpi-c, var(--chart-1)) 30%, var(--border-soft)); }
.kpi.kpi-tint .kpi-val { color: color-mix(in srgb, var(--kpi-c, var(--chart-1)) 82%, #000); }
.kpi.disc-insurance   { --kpi-c: var(--disc-insurance); }
.kpi.disc-professional{ --kpi-c: var(--disc-professional); }
.kpi.disc-family      { --kpi-c: var(--disc-family); }
.kpi.disc-pay-in-full { --kpi-c: var(--disc-pif); }
.kpi.disc-ad          { --kpi-c: var(--disc-ad); }
.kpi.disc-other       { --kpi-c: var(--disc-other); }

/* heatmap expander (top 10 ⇄ all referrers) */
.heat-expand { display: flex; justify-content: flex-end; margin-top: .45rem; }

/* ── admin as a full page (owner 2026-07-10 — the modal clipped its tables) ── */
.admin-view { flex: 1; overflow: auto; padding: 1rem 1.25rem 2.5rem; background: var(--bg); }
.admin-inner { max-width: 1100px; margin: 0 auto; width: 100%; background: var(--surface);
  border: 1px solid var(--border-soft); border-radius: var(--radius); box-shadow: var(--shadow-card);
  padding: 1.25rem 1.5rem 2rem; }
.admin-inner h2 { margin: 0 0 .9rem; font-size: var(--fs-title); }
.perm-table th, .perm-table td { padding: .5rem .65rem; border-bottom: 1px solid var(--border-soft);
  text-align: left; vertical-align: middle; font-size: var(--fs-table); }
.perm-table th { color: var(--muted); font-weight: 600; font-size: var(--fs-label); }
.perm-table select { width: auto; min-width: 6.5rem; }

/* funnel expand toggle (⤢ reveals the New Patient Entries level) */
.card-expand { position: absolute; top: 0; right: 1.9rem; border: none; background: transparent;
  color: var(--muted); font-size: 1rem; cursor: pointer; padding: .1rem .3rem; border-radius: 6px; }
.card-expand:hover { background: var(--surface-2); color: var(--accent); }
