/* ============================================================
   Church Tech Compensation Explorer
   Palette + tokens from the validated data-viz reference palette
   (light + dark are both selected, not an auto-invert).
   ============================================================ */
:root {
  color-scheme: light;
  --page:        #f9f9f7;
  --surface-1:   #fcfcfb;
  --surface-2:   #f2f1ec;
  --text-1:      #0b0b0b;
  --text-2:      #52514e;
  --muted:       #898781;
  --grid:        #e1e0d9;
  --baseline:    #c3c2b7;
  --border:      rgba(11,11,11,0.10);
  --s1:          #2a78d6;
  --good-text:   #006300;
  --critical:    #d03b3b;
  --warning:     #fab219;
  --shadow:      0 1px 2px rgba(0,0,0,0.04), 0 10px 30px rgba(0,0,0,0.06);
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page:        #0d0d0d;
  --surface-1:   #1a1a19;
  --surface-2:   #232321;
  --text-1:      #ffffff;
  --text-2:      #c3c2b7;
  --muted:       #898781;
  --grid:        #2c2c2a;
  --baseline:    #383835;
  --border:      rgba(255,255,255,0.10);
  --s1:          #3987e5;
  --good-text:   #0ca30c;
  --critical:    #d03b3b;
  --warning:     #fab219;
  --shadow:      0 1px 2px rgba(0,0,0,0.3), 0 12px 34px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text-1);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--s1); }

/* ---------- page shell ---------- */
.page { max-width: 1180px; margin: 0 auto; padding: 26px 22px 80px; }
.masthead { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 22px; }
.masthead h1 { font-size: 23px; margin: 0 0 4px; letter-spacing: -0.01em; }
.masthead p { margin: 0; color: var(--text-2); font-size: 14px; max-width: 680px; }
.theme-toggle {
  flex: 0 0 auto; border: 1px solid var(--border); background: var(--surface-1);
  color: var(--text-2); border-radius: 9px; padding: 8px 12px; font: inherit;
  font-size: 13px; cursor: pointer;
}
.theme-toggle:hover { color: var(--text-1); }

/* ---------- explorer layout ---------- */
.explorer { display: grid; grid-template-columns: 264px 1fr; gap: 22px; align-items: start; }
.results { min-width: 0; display: flex; flex-direction: column; gap: 18px; }

/* ---------- filter rail ---------- */
.rail {
  position: sticky; top: 16px;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; box-shadow: var(--shadow); max-height: calc(100vh - 32px); overflow: auto;
}
.rail-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.rail-head h2 { font-size: 14px; margin: 0; }
.rail-clear { border: 0; background: none; color: var(--s1); font: inherit; font-size: 12px; cursor: pointer; padding: 4px; }
.rail-clear:hover { text-decoration: underline; }
.rail section { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 10px; }
.rail section:first-of-type { border-top: 0; margin-top: 0; }
.rail h3 { font-size: 11px; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); margin: 0 0 6px; }
.facet-dim { font-size: 12px; font-weight: 600; color: var(--text-2); margin: 10px 0 3px; }
.facet-group:first-child .facet-dim { margin-top: 0; }

.facet {
  display: flex; align-items: center; justify-content: space-between; width: 100%;
  padding: 5px 8px; border: 0; border-radius: 8px; background: none; cursor: pointer;
  font: inherit; font-size: 13px; color: var(--text-2); text-align: left;
}
.facet:hover { background: var(--surface-2); }
.facet.on { background: color-mix(in srgb, var(--s1) 14%, transparent); color: var(--text-1); font-weight: 500; }
.facet .lab { display: flex; align-items: center; min-width: 0; }
.facet .lab span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.facet .n { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; padding-left: 8px; }
.facet .box {
  width: 15px; height: 15px; flex: 0 0 auto; margin-right: 8px; border-radius: 4px;
  border: 1.5px solid var(--baseline); position: relative;
}
.facet.on .box { background: var(--s1); border-color: var(--s1); }
.facet.on .box::after {
  content: ""; position: absolute; left: 4px; top: 1px; width: 4px; height: 8px;
  border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg);
}
:root[data-theme="dark"] .facet.on .box::after { border-color: #0d0d0d; }

/* ---------- chips ---------- */
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chip-lead { font-size: 13px; color: var(--text-2); font-weight: 600; }
.chip {
  display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--border);
  background: var(--surface-1); border-radius: 999px; padding: 4px 10px 4px 11px;
  font: inherit; font-size: 12.5px; color: var(--text-1); cursor: pointer;
}
.chip:hover { border-color: var(--critical); }
.chip .x { color: var(--muted); font-size: 11px; }
.chip:hover .x { color: var(--critical); }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.tile {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; box-shadow: var(--shadow);
}
.tile .k-lab { font-size: 12px; color: var(--text-2); }
.tile .k-val { font-size: 27px; font-weight: 600; letter-spacing: -0.01em; margin-top: 2px; }
.tile .k-val.small { font-size: 18px; padding-top: 5px; }
.tile .k-sub { font-size: 12px; color: var(--muted); margin-top: 3px; }
.delta-up { color: var(--good-text); font-weight: 600; }
.delta-dn { color: var(--critical); font-weight: 600; }

/* ---------- cards / figures ---------- */
.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px;
  padding: 18px; margin: 0; box-shadow: var(--shadow);
}
.card figcaption, .card .cap { font-size: 15px; font-weight: 600; }
.card .cap-sub { font-size: 12.5px; color: var(--text-2); margin: 2px 0 12px; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; align-items: center; margin: 6px 0 10px; font-size: 12.5px; color: var(--text-2); }
.legend .key { display: inline-flex; align-items: center; gap: 7px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; flex: 0 0 auto; }
.legend .lk { width: 18px; height: 0; border-top: 2px solid var(--muted); }

.chart-box { position: relative; width: 100%; }
.chart-box canvas { max-width: 100%; }
.chart-box.h-dist { height: 300px; }
.chart-box.h-break { height: 260px; }
.chart-box.h-sent { height: 260px; }
.chart-box.h-drivers { height: 210px; }

/* ---------- breakdown selector ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button {
  border: 0; background: none; font: inherit; font-size: 12px; color: var(--text-2);
  padding: 5px 10px; border-radius: 7px; cursor: pointer;
}
.seg button.on { background: var(--surface-1); color: var(--text-1); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,0.08); }

/* ---------- suppression ---------- */
.suppress {
  background: var(--surface-1); border: 1px dashed var(--baseline); border-radius: 14px;
  padding: 34px; text-align: center;
}
.suppress svg { width: 38px; height: 38px; color: var(--warning); margin-bottom: 8px; }
.suppress h3 { margin: 0 0 6px; font-size: 16px; }
.suppress p { margin: 0 auto; max-width: 440px; color: var(--text-2); font-size: 13.5px; }
.suppress .badge { display: inline-block; margin-top: 14px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- table view ---------- */
.table-toggle { margin-top: 12px; }
.table-toggle summary { cursor: pointer; font-size: 12.5px; color: var(--s1); }
table.dist { border-collapse: collapse; width: 100%; font-size: 12.5px; margin-top: 10px; }
table.dist th, table.dist td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); }
table.dist th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .03em; }
table.dist td.num, table.dist th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.dist tr.hi { background: color-mix(in srgb, var(--s1) 9%, transparent); }
.note { font-size: 12px; color: var(--muted); margin: 10px 0 0; }

/* ---------- two-up ---------- */
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }

@media (max-width: 900px) {
  .explorer { grid-template-columns: 1fr; }
  .two { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}

/* Hold the previous render during a live refetch (no skeleton flash). */
[data-live-loading] { opacity: 0.55; transition: opacity .12s ease; }

/* ---------- section headers ---------- */
.section-head { margin: 10px 2px 0; }
.section-head h2 { font-size: 17px; margin: 0; }
.section-head p { margin: 2px 0 0; color: var(--text-2); font-size: 13px; }

/* ---------- demographics grid ---------- */
.demo-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px; }
.chart-box.h-pie { height: 230px; }
.mock-tag {
  display: inline-block; margin-left: 6px; font-size: 10px; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--warning); border: 1px solid var(--warning);
  border-radius: 4px; padding: 0 4px; vertical-align: middle;
}

/* ---------- tile-grid map ---------- */
.tilemap { display: grid; gap: 4px; margin-top: 12px; aspect-ratio: 11 / 8; max-width: 720px; }
.tile-state {
  display: flex; align-items: center; justify-content: center; border-radius: 5px;
  font-size: 10px; font-weight: 600; font-variant-numeric: tabular-nums;
  color: var(--text-2); background: var(--surface-2); cursor: default; user-select: none;
}
.tile-state.lvl0 { color: var(--muted); opacity: .55; }

/* ---------- takeaways ---------- */
.takeaways { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.takeaway {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px 18px; box-shadow: var(--shadow); border-left: 3px solid var(--s1);
}
.takeaway .tk-stat { font-size: 30px; font-weight: 600; letter-spacing: -0.01em; color: var(--s1); }
.takeaway .tk-title { font-size: 14px; font-weight: 600; margin-top: 2px; }
.takeaway .tk-body { font-size: 13px; color: var(--text-2); margin-top: 6px; }

/* ---------- word cloud ---------- */
.wordcloud { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: baseline; padding: 12px 4px 4px; }
.cloud-word { color: var(--s1); font-weight: 600; line-height: 1.1; }
.cloud-word:nth-child(3n) { color: var(--text-1); }
.cloud-word:nth-child(3n+2) { color: var(--text-2); }

/* ---------- quotes ---------- */
.quotes blockquote {
  margin: 10px 0 0; padding: 10px 12px; font-size: 13px; color: var(--text-1);
  background: var(--surface-2); border-radius: 8px; border-left: 3px solid var(--baseline);
}
.quotes.tough blockquote { border-left-color: var(--critical); }
.quotes.great blockquote { border-left-color: var(--s1); }

/* ---------- chart colour index (legend/key for pies & doughnuts) ---------- */
.chart-index { list-style: none; margin: 10px 0 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 2px 14px; }
.chart-index li { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text-2); min-width: 0; }
.idx-sw { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.idx-lab { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.idx-val { color: var(--text-1); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.idx-n { color: var(--muted); font-weight: 400; }

/* ---------- map colour scale ---------- */
.map-scale { display: flex; align-items: center; gap: 5px; margin-top: 12px; font-size: 11px; color: var(--muted); }
.scale-swatch { width: 18px; height: 12px; border-radius: 3px; background: var(--surface-2); flex: 0 0 auto; }
.scale-swatch.lvl0 { background: var(--surface-2); border: 1px solid var(--border); }

/* ---------- mobile filter drawer (hidden on desktop) ---------- */
.mobile-filterbar, .rail-close, .rail-backdrop { display: none; }
.filters-btn {
  width: 100%; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-1);
  border-radius: 10px; padding: 11px 14px; font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer; box-shadow: var(--shadow);
}
.rail-close {
  margin-left: auto; border: 0; background: var(--s1); color: #fff; border-radius: 7px;
  padding: 5px 12px; font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
}
:root[data-theme="dark"] .rail-close { color: #0d0d0d; }

@media (max-width: 900px) {
  .page { padding: 18px 14px 72px; }
  .masthead { flex-direction: column; align-items: flex-start; gap: 10px; }
  .demo-grid { grid-template-columns: 1fr; }
  .takeaways { grid-template-columns: 1fr; }
  .chart-index { grid-template-columns: 1fr; }

  .mobile-filterbar { display: block; margin-bottom: 6px; }
  .rail-close { display: inline-block; }
  .rail {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 60;
    width: min(87vw, 340px); max-height: 100vh; border-radius: 0 16px 16px 0;
    transform: translateX(-100%); transition: transform .22s ease; overflow-y: auto;
  }
  body.filters-open .rail { transform: none; box-shadow: 0 0 44px rgba(0,0,0,0.45); }
  .rail-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 55; }
  body.filters-open { overflow: hidden; }
  body.filters-open .rail-backdrop { display: block; }
}
