/* Live statistics page.
 *
 * Chart colour is a single-hue ordinal gold ramp drawn from the brand accent, one
 * hue for magnitude, plus the reserved status colours for Core Web Vitals. Both
 * ramps were validated against this site's own chart surfaces (#FDFCFA light,
 * #1C1C1E dark) for monotone lightness, step separation and contrast at the end
 * nearest the surface — not chosen by eye.
 *
 * Status colours never carry meaning alone: every vital ships a dot, the measured
 * value and the rating in words, so the reading survives colour blindness,
 * greyscale printing and forced-colours mode. */

.stats-page {
  --stats-surface: #FDFCFA;
  --stats-grid: #E8DDD4;
  --stats-axis: #C7C7CC;
  --stats-muted: #655D56;

  /* Ordinal gold ramp, light → dark. Validated: monotone L, ΔL ≥ 0.06, light end 2.23:1. */
  --stats-ramp-1: #D4A520;
  --stats-ramp-2: #B78A0C;
  --stats-ramp-3: #96700A;
  --stats-ramp-4: #785900;
  --stats-ramp-5: #5C4300;
  --stats-series: #96700A;
  --stats-series-soft: rgba(150, 112, 10, 0.14);

  /* Reserved status palette — never reused for a series. */
  --stats-good: #0CA30C;
  --stats-warn: #FAB219;
  --stats-poor: #D03B3B;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .stats-page {
    --stats-surface: #1C1C1E;
    --stats-grid: #38383A;
    --stats-axis: #48484A;
    --stats-muted: rgba(235, 235, 245, 0.6);
    --stats-ramp-1: #F2D98A;
    --stats-ramp-2: #E3C46B;
    --stats-ramp-3: #D4A520;
    --stats-ramp-4: #B78A0C;
    --stats-ramp-5: #96700A;
    --stats-series: #D4A520;
    --stats-series-soft: rgba(212, 165, 32, 0.18);
  }
}

:root[data-theme="dark"] .stats-page {
  --stats-surface: #1C1C1E;
  --stats-grid: #38383A;
  --stats-axis: #48484A;
  --stats-muted: rgba(235, 235, 245, 0.6);
  --stats-ramp-1: #F2D98A;
  --stats-ramp-2: #E3C46B;
  --stats-ramp-3: #D4A520;
  --stats-ramp-4: #B78A0C;
  --stats-ramp-5: #96700A;
  --stats-series: #D4A520;
  --stats-series-soft: rgba(212, 165, 32, 0.18);
}

/* ------------------------------------------------------------------ status */

.stats-live {
  display: inline-flex; align-items: center; gap: 9px;
  padding: 7px 15px 7px 13px;
  border: 1px solid var(--separator-nonopaque); border-radius: 999px;
  background: var(--bg-primary);
  color: var(--label-secondary); font-size: 13px; font-weight: 650; letter-spacing: .01em;
}
.stats-live__dot {
  position: relative; width: 8px; height: 8px; border-radius: 50%;
  background: var(--stats-good); flex: none;
}
.stats-live__dot::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%;
  border: 1px solid var(--stats-good); opacity: .55;
  animation: stats-pulse 2.4s var(--ease-standard) infinite;
}
.stats-live[data-state="stale"] .stats-live__dot,
.stats-live[data-state="stale"] .stats-live__dot::after { background: var(--stats-warn); border-color: var(--stats-warn); }
.stats-live[data-state="error"] .stats-live__dot,
.stats-live[data-state="error"] .stats-live__dot::after { background: var(--stats-poor); border-color: var(--stats-poor); }

@keyframes stats-pulse {
  0% { transform: scale(.7); opacity: .7; }
  70% { transform: scale(1.9); opacity: 0; }
  100% { transform: scale(1.9); opacity: 0; }
}

/* ------------------------------------------------------------- stat tiles */

.stats-tiles {
  display: grid; gap: 16px; margin: 36px 0 0;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  list-style: none; padding: 0;
}
.stats-tile {
  padding: 26px 24px 22px;
  border: 1px solid var(--separator-nonopaque); border-radius: 22px;
  background: var(--stats-surface);
}
.stats-tile__value {
  display: block; font-size: clamp(34px, 4.6vw, 50px); font-weight: 700;
  line-height: 1.02; letter-spacing: -.03em; color: var(--label-primary);
}
.stats-tile__label {
  display: block; margin-top: 10px;
  color: var(--stats-muted); font-size: 14px; font-weight: 600; line-height: 1.4;
}
.stats-tile__note { display: block; margin-top: 4px; color: var(--stats-muted); font-size: 13px; opacity: .85; }

/* --------------------------------------------------------------- sections */

.stats-panel {
  margin-top: 44px; padding: 30px 28px 26px;
  border: 1px solid var(--separator-nonopaque); border-radius: 26px;
  background: var(--stats-surface);
}
.stats-panel__head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 10px 20px; margin-bottom: 22px; }
.stats-panel__head h2 { margin: 0; font-size: 22px; line-height: 1.25; letter-spacing: -.018em; }
.stats-panel__head p { margin: 0; color: var(--stats-muted); font-size: 14px; }

.stats-columns { display: grid; gap: 20px; grid-template-columns: repeat(2, minmax(0, 1fr)); }

/* ------------------------------------------------------------------ chart */

.stats-chart { position: relative; }
.stats-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.stats-chart__grid line { stroke: var(--stats-grid); stroke-width: 1; }
.stats-chart__axis { stroke: var(--stats-axis); stroke-width: 1; }
.stats-chart__area { fill: var(--stats-series-soft); }
.stats-chart__line { fill: none; stroke: var(--stats-series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.stats-chart__tick { fill: var(--stats-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.stats-chart__marker { fill: var(--stats-series); stroke: var(--stats-surface); stroke-width: 2; }
.stats-chart__crosshair { stroke: var(--stats-axis); stroke-width: 1; stroke-dasharray: 3 3; }
.stats-chart__hit { fill: transparent; }

.stats-tip {
  position: absolute; z-index: 2; min-width: 120px; padding: 9px 12px;
  border: 1px solid var(--separator-nonopaque); border-radius: 12px;
  background: var(--bg-grouped-secondary); box-shadow: var(--shadow-2);
  color: var(--label-primary); font-size: 13px; line-height: 1.4;
  pointer-events: none; opacity: 0; transform: translate(-50%, -112%);
  transition: opacity var(--dur-fast) var(--ease-standard);
}
.stats-tip[data-visible="true"] { opacity: 1; }
.stats-tip__value { font-weight: 700; font-variant-numeric: tabular-nums; }
.stats-tip__label { display: block; color: var(--stats-muted); font-size: 12px; }

/* ------------------------------------------------------------------- bars */

.stats-bars { margin: 0; padding: 0; list-style: none; display: grid; gap: 10px; }
.stats-bar { display: grid; grid-template-columns: minmax(96px, 1fr) minmax(0, 2fr) auto; align-items: center; gap: 14px; }
.stats-bar__label { color: var(--label-primary); font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stats-bar__label .stats-flag { margin-right: 8px; font-size: 15px; }
.stats-bar__track { position: relative; height: 10px; border-radius: 999px; background: var(--fill-quaternary); overflow: hidden; }
/* The ramp steps with rank and must never reverse: rows past the fifth all take
 * the lightest validated step rather than cycling back toward the dark end. */
.stats-bar__fill {
  position: absolute; inset-block: 0; left: 0; width: 0;
  border-radius: 0 4px 4px 0; background: var(--stats-ramp-1);
  transition: width var(--dur-slow) var(--ease-standard);
}
.stats-bar:nth-child(1) .stats-bar__fill { background: var(--stats-ramp-5); }
.stats-bar:nth-child(2) .stats-bar__fill { background: var(--stats-ramp-4); }
.stats-bar:nth-child(3) .stats-bar__fill { background: var(--stats-ramp-3); }
.stats-bar:nth-child(4) .stats-bar__fill { background: var(--stats-ramp-2); }
.stats-bar:nth-child(n+5) .stats-bar__fill { background: var(--stats-ramp-1); }
.stats-bar__value { color: var(--label-primary); font-size: 14px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stats-bar__share { color: var(--stats-muted); font-size: 13px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------- core web vitals */

.stats-vitals { display: grid; gap: 14px; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 0; padding: 0; list-style: none; }
.stats-vital {
  padding: 20px 18px; border: 1px solid var(--separator-nonopaque);
  border-radius: 18px; background: var(--bg-primary);
}
.stats-vital__name { display: flex; align-items: center; gap: 8px; color: var(--stats-muted); font-size: 12px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; }
.stats-vital__value { display: block; margin-top: 10px; font-size: 30px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stats-vital__rating { display: inline-flex; align-items: center; gap: 7px; margin-top: 8px; font-size: 13px; font-weight: 650; }
.stats-vital__dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.stats-vital__meta { display: block; margin-top: 8px; color: var(--stats-muted); font-size: 12px; }

.stats-vital[data-rating="good"] .stats-vital__dot { background: var(--stats-good); }
.stats-vital[data-rating="needs-improvement"] .stats-vital__dot { background: var(--stats-warn); }
.stats-vital[data-rating="poor"] .stats-vital__dot { background: var(--stats-poor); }
.stats-vital[data-rating="none"] .stats-vital__dot { background: var(--stats-axis); }

/* The meter is secondary to the words above it, so it needs no legend of its own. */
.stats-meter { display: flex; height: 6px; margin-top: 14px; border-radius: 999px; overflow: hidden; gap: 2px; background: var(--fill-quaternary); }
.stats-meter span { display: block; min-width: 0; }
.stats-meter span[data-band="good"] { background: var(--stats-good); }
.stats-meter span[data-band="needs-improvement"] { background: var(--stats-warn); }
.stats-meter span[data-band="poor"] { background: var(--stats-poor); }

/* -------------------------------------------------------- table & states */

.stats-table-toggle {
  display: inline-flex; align-items: center; min-height: 36px; padding: 6px 14px;
  border: 1px solid var(--separator-opaque); border-radius: 999px;
  background: transparent; color: var(--label-primary);
  font: inherit; font-size: 13px; font-weight: 650; cursor: pointer;
}
.stats-table-toggle:hover { border-color: var(--equity); color: var(--equity); }
.stats-table-wrap { margin-top: 18px; overflow-x: auto; }
.stats-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.stats-table caption { text-align: left; padding-bottom: 10px; color: var(--stats-muted); font-size: 13px; }
.stats-table th, .stats-table td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--separator-nonopaque); }
.stats-table td:not(:first-child), .stats-table th:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }

.stats-empty { margin: 0; padding: 26px 0; color: var(--stats-muted); font-size: 15px; }
.stats-skeleton { color: var(--stats-muted); }

/* ---------------------------------------------------------------- motion */

.stats-reveal { opacity: 0; transform: translateY(14px); }
.stats-reveal[data-shown="true"] { opacity: 1; transform: none; transition: opacity var(--dur-slow) var(--ease-standard), transform var(--dur-slow) var(--ease-standard); }

@media (prefers-reduced-motion: reduce) {
  .stats-live__dot::after { animation: none; }
  .stats-bar__fill { transition: none; }
  .stats-reveal, .stats-reveal[data-shown="true"] { opacity: 1; transform: none; transition: none; }
  .stats-tip { transition: none; }
}

/* ------------------------------------------------------------ responsive */

@media (max-width: 900px) {
  .stats-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats-vitals { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats-columns { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 560px) {
  .stats-panel { padding: 24px 18px 20px; border-radius: 20px; }
  .stats-tiles { gap: 12px; }
  .stats-tile { padding: 20px 18px 18px; }
  .stats-bar { grid-template-columns: minmax(78px, 1fr) minmax(0, 1.4fr) auto; gap: 10px; }
}

/* Forced colours: lean on the text, not the fills. */
@media (forced-colors: active) {
  .stats-bar__fill, .stats-meter span, .stats-vital__dot, .stats-live__dot { forced-color-adjust: none; }
  .stats-tile, .stats-panel, .stats-vital { border-color: CanvasText; }
}
