/* Stats page — layout and charts. Tokens come from styles.css; this file
   only adds what the report needs. The ordinal green ramps are validated
   (light: on white, dark: on #14171b) — don't re-tune them by eye. */

:root {
  --chart-1: #0D3627;
  --chart-2: #1c553e;
  --chart-3: #2c7455;
  --chart-4: #3f926b;
  --chart-5: #55ad82;
  --chart-6: #6fc39a;
  --chart-other: #c2c8d0;
  --chart-track: #eef1f4;
  --status-available: #0D3627;
  --status-on-hold: #9a5b06;
  --status-sold: #f2c40f;
}
:root[data-theme="dark"] {
  --chart-1: #aee8ca;
  --chart-2: #8dd7ae;
  --chart-3: #6dc494;
  --chart-4: #4fb07b;
  --chart-5: #349b64;
  --chart-6: #1f8450;
  --chart-other: #4a5261;
  --chart-track: #1f242b;
  --status-available: #6fbe98;
  --status-on-hold: #e0a944;
  --status-sold: #f5d051;
}

/* The topbar/filterbar stay pinned; the report itself scrolls. */
.stats-main {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 20px 32px;
}

/* ---------- KPI tiles ---------- */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
}
.stat-label {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  font-weight: 600;
}
.stat-value {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
  margin-top: 2px;
}

/* ---------- chart cards ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 12px;
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px 16px;
  min-width: 0;
}
.chart-card-wide { grid-column: 1 / -1; }
.chart-card-head { margin-bottom: 12px; }
.chart-card-head h2 {
  margin: 0;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  font-weight: 700;
}
.chart-sub {
  margin: 4px 0 0;
  font-size: 12.5px;
  color: var(--muted);
}
.chart-note {
  margin: 10px 0 0;
  font-size: 11.5px;
  color: var(--muted);
}

/* ---------- donut + ranked aggregator report ---------- */
.agg-share-body {
  display: flex;
  gap: 28px;
  align-items: flex-start;
  flex-wrap: wrap;
}
.donut-wrap {
  position: relative;
  width: min(260px, 100%);
  flex: 0 0 auto;
  margin: 4px auto 0;
}
.donut-wrap svg { display: block; width: 100%; height: auto; }
.donut-slice { cursor: pointer; }
.donut-slice:hover { opacity: 0.85; }
.donut-slice.dimmed { opacity: 0.35; }
.donut-label {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 700;
  pointer-events: none;
}
.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  pointer-events: none;
  padding: 0 46px;
}
.donut-center-value {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
  line-height: 1.1;
}
.donut-center-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.agg-rank {
  flex: 1 1 300px;
  min-width: 0;
  max-height: 340px;
  overflow-y: auto;
  padding-right: 4px;
}
.rank-row {
  display: grid;
  grid-template-columns: 12px minmax(90px, 150px) 1fr 76px;
  align-items: center;
  gap: 10px;
  padding: 5px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font: inherit;
  color: var(--text);
}
.rank-row:hover { background: var(--row-hover); }
.rank-row.focused { background: var(--row-selected); }
.rank-row.dimmed { opacity: 0.45; }
.rank-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex: none;
}
.rank-name {
  font-size: 13px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rank-track {
  display: block;
  height: 14px;
  background: var(--chart-track);
  border-radius: 0 4px 4px 0;
  overflow: hidden;
  min-width: 40px;
}
.rank-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 0 4px 4px 0;
  min-width: 2px;
}
.rank-value {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

/* ---------- horizontal bar charts (state / region) ---------- */
.bar-chart { display: flex; flex-direction: column; gap: 7px; }
.bar-row {
  display: grid;
  grid-template-columns: 88px 1fr 76px;
  align-items: center;
  gap: 10px;
}
.bar-label { font-size: 12.5px; font-weight: 600; color: var(--text); }
.bar-track {
  display: block;
  height: 16px;
  background: var(--chart-track);
  border-radius: 0 4px 4px 0;
  overflow: hidden;
}
.bar-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 0 4px 4px 0;
  min-width: 2px;
}
.bar-value {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.bar-empty { color: var(--muted); font-size: 12.5px; padding: 12px 0; }

/* ---------- price histogram ---------- */
.col-chart {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  height: 170px;
  padding-top: 18px;
}
.col-item {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  gap: 4px;
}
.col-value {
  font-size: 11.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.col-bar {
  width: 100%;
  max-width: 44px;
  background: var(--accent);
  border-radius: 4px 4px 0 0;
}
.col-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}

/* ---------- status stacked bar ---------- */
.status-bar {
  display: flex;
  height: 22px;
  border-radius: 4px;
  overflow: hidden;
  gap: 2px;
  background: var(--surface);
}
.status-seg { min-width: 3px; }
.status-seg[data-status="available"] { background: var(--status-available); }
.status-seg[data-status="on_hold"] { background: var(--status-on-hold); }
.status-seg[data-status="sold"] { background: var(--status-sold); }
.status-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 12px;
}
.status-key {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--text);
}
.status-key .rank-swatch[data-status="available"] { background: var(--status-available); }
.status-key .rank-swatch[data-status="on_hold"] { background: var(--status-on-hold); }
.status-key .rank-swatch[data-status="sold"] { background: var(--status-sold); }
.status-key b { font-variant-numeric: tabular-nums; }
.status-key .pct { color: var(--muted); }

/* ---------- tooltip ---------- */
.chart-tip {
  position: fixed;
  z-index: 90;
  background: #2a2f36;
  color: #fff;
  font-size: 12.5px;
  padding: 6px 10px;
  border-radius: var(--radius);
  pointer-events: none;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  white-space: nowrap;
}
.chart-tip b { font-variant-numeric: tabular-nums; }

/* ---------- empty state ---------- */
.stats-empty {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 48px 20px;
  text-align: center;
  color: var(--muted);
}

@media (max-width: 760px) {
  /* On a phone the pinned topbar + wrapped filterbar would eat half the
     screen — let the whole page scroll instead so the report gets room. */
  body.stats-page { display: block; overflow-y: auto; }
  .stats-main { overflow: visible; padding: 12px 14px 28px; }
  .agg-share-body { gap: 12px; }
  .agg-rank { max-height: none; }
  .rank-row { grid-template-columns: 12px minmax(80px, 120px) 1fr 70px; gap: 8px; }
  .col-chart { height: 140px; gap: 6px; }
}
