/* Stats pages (/stats/ and /stats2/). Hub-neutral shell; the desk cards are scoped with
   data-persona, so each one wears its writer's theme. Charts are one-series ink marks,
   except where the series ARE the writers: those use the --c-<key> chart steps below.

   Chart steps: the writers' brand accents fail as chart colors (navy and oxblood fall
   below the lightness band, amber and rose sit too close together), so each hue is stepped
   for charts and checked with the dataviz validator, adjacent pairs in desk order:
     light, on #FFFFFF: #dea219 #d07973 #295591 #8b333b  (amber is under 3:1, so every
       colored mark has a text label or a table beside it)
     dark, on #1A1A1A:  #ab8c0e #ce6f75 #3368a3 #9f4d47 */
.stats {
  --c-nicholas: #dea219;
  --c-ava: #d07973;
  --c-ryan: #295591;
  --c-jason: #8b333b;
  --viz-ink: #2b2b2b;
  --viz-grid: #ebebe9;
  --viz-base: #c4c4c0;
  --live: #23874b;
  position: relative;
  padding-bottom: var(--space-2xl);
}
@media (prefers-color-scheme: dark) {
  .stats {
    --c-nicholas: #ab8c0e;
    --c-ava: #ce6f75;
    --c-ryan: #3368a3;
    --c-jason: #9f4d47;
    --viz-ink: #d6d6d6;
    --viz-grid: #262626;
    --viz-base: #4a4a4a;
    --live: #4cc27a;
  }
}

/* -- head ------------------------------------------------------------------- */
.stats-head { padding-block: clamp(2.75rem, 6vw, 5rem) var(--space-m); }
.stats-head__title {
  margin-top: .6rem;
  font-family: var(--font-shell);
  font-size: clamp(2.5rem, 1.4rem + 3.6vw, 4.5rem);
  font-weight: 650;
  line-height: .96;
  letter-spacing: -.045em;
}
.stats-head__deck {
  max-width: 58ch;
  margin-top: 1rem;
  color: var(--fg-muted);
  font-size: var(--step-1);
  line-height: 1.5;
}
.stats-head__deck a { color: var(--fg); text-underline-offset: .2em; }
.stats-live {
  display: inline-flex;
  align-items: center;
  gap: .55rem;
  margin-top: 1.25rem;
  padding: .4rem .85rem .4rem .7rem;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg-2);
  font-size: .875rem;
  font-weight: 600;
}
.stats-live[hidden] { display: none; }
.stats-live__dot {
  position: relative;
  width: .55rem;
  height: .55rem;
  border-radius: 50%;
  background: var(--live);
}
.stats-live__dot::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--live);
  opacity: 0;
  animation: stats-pulse 2.4s ease-out infinite;
}
.stats-live.is-quiet { color: var(--fg-muted); font-weight: 500; }
.stats-live.is-quiet .stats-live__dot { background: var(--viz-base); }
.stats-live.is-quiet .stats-live__dot::after { display: none; }
@keyframes stats-pulse {
  0% { transform: scale(.6); opacity: .7; }
  80%, 100% { transform: scale(1.6); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) { .stats-live__dot::after { animation: none; } }

/* -- range row (one row, above everything it scopes) --------------------------- */
.stats-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: .75rem 1.5rem;
  margin-bottom: var(--space-m);
}
.stats-range {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg-2);
}
.stats-range__btn {
  padding: .45rem 1rem;
  border-radius: 999px;
  color: var(--fg-muted);
  font-family: var(--font-shell);
  font-size: .875rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background-color .15s, color .15s;
}
.stats-range__btn:hover { color: var(--fg); }
.stats-range__btn[aria-pressed="true"] { background: var(--fg); color: var(--bg); }
.stats-range__btn:focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }
.stats-updated { color: var(--fg-muted); font-size: .8125rem; }

/* -- loading and refresh keep the frame ----------------------------------------- */
.stats[data-state="loading"] .stats-kpis,
.stats[data-state="loading"] .stats-card,
.stats[data-state="loading"] .stats-desks,
.stats[data-state="loading"] .stats-facts { opacity: .35; }
.stats.is-refreshing .chart, .stats.is-refreshing .kpi__value { opacity: .6; transition: opacity .2s; }
.stats[data-state="error"] .stats-kpis,
.stats[data-state="error"] .stats-card,
.stats[data-state="error"] .stats-desks,
.stats[data-state="error"] .stats-facts { display: none; }

/* -- KPI row -------------------------------------------------------------------- */
.stats-kpis {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: .75rem;
}
@media (min-width: 900px) {
  .stats-kpis { grid-template-columns: minmax(0, 1.5fr) repeat(3, minmax(0, 1fr)); gap: 1rem; }
}
.kpi {
  display: flex;
  flex-direction: column;
  padding: 1.1rem 1.25rem 1rem;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-2);
}
.kpi--hero, .kpi:last-child { grid-column: 1 / -1; }
@media (min-width: 900px) { .kpi--hero, .kpi:last-child { grid-column: auto; } }
.kpi__label { color: var(--fg-muted); font-size: .8125rem; font-weight: 600; }
.kpi__value {
  margin-top: .45rem;
  font-family: var(--font-shell);
  font-size: clamp(1.875rem, 1.5rem + 1vw, 2.375rem);
  font-weight: 650;
  line-height: 1;
  letter-spacing: -.035em;
}
.kpi--hero .kpi__value { font-size: clamp(3rem, 2.2rem + 2.6vw, 4.25rem); letter-spacing: -.05em; }
.kpi__delta { min-height: 1.3em; margin-top: auto; padding-top: .55rem; color: var(--fg-muted); font-size: .8125rem; }

/* -- cards ------------------------------------------------------------------------ */
.stats-card {
  margin-top: var(--space-m);
  padding: clamp(1.1rem, .8rem + 1.2vw, 1.75rem);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-2);
}
.stats-card[hidden] { display: none; }
.stats-card__head, .stats-section__head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: .35rem 1.5rem;
}
.stats-card__title {
  display: inline-flex;
  align-items: center;
  gap: .5em;
  font-family: var(--font-shell);
  font-size: 1.125rem;
  font-weight: 650;
  letter-spacing: -.02em;
}
.stats-section__title {
  display: inline-flex;
  align-items: center;
  gap: .45em;
  font-family: var(--font-shell);
  font-size: clamp(1.5rem, 1.1rem + 1.2vw, 2rem);
  font-weight: 650;
  letter-spacing: -.035em;
}
.stats-card__mark { width: 1.15em; height: 1.15em; flex: none; }
.stats-card__deck { color: var(--fg-muted); font-size: .875rem; }

/* -- column charts ---------------------------------------------------------------- */
.chart { margin-top: 1rem; }
.chart__svg { display: block; width: 100%; height: auto; overflow: visible; border-radius: 4px; }
.chart__svg:focus-visible { outline: 2px solid var(--fg); outline-offset: 6px; }
.chart__grid { stroke: var(--viz-grid); stroke-width: 1; }
.chart__base { stroke: var(--viz-base); stroke-width: 1; }
.chart__tick { fill: var(--fg-muted); font-family: var(--font-shell); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart__bar { fill: var(--viz-ink); transition: opacity .15s; }
.chart__col.is-dim .chart__bar { opacity: .3; }
.chart__hit { fill: transparent; cursor: crosshair; }
.chart__empty { fill: var(--fg-muted); font-family: var(--font-shell); font-size: 14px; }

.stats-table { margin-top: .9rem; }
.stats-table summary { width: fit-content; color: var(--fg-muted); font-size: .8125rem; cursor: pointer; }
.stats-table summary:hover { color: var(--fg); }
.stats-table__wrap { max-height: 18rem; margin-top: .6rem; overflow: auto; }
.stats-table table { width: 100%; border-collapse: collapse; font-size: .8125rem; font-variant-numeric: tabular-nums; }
.stats-table th, .stats-table td { padding: .32rem .5rem; border-bottom: 1px solid var(--rule); text-align: right; }
.stats-table th:first-child, .stats-table thead th:first-child { text-align: left; font-weight: 500; }
.stats-table thead th { position: sticky; top: 0; background: var(--bg-2); color: var(--fg-muted); font-weight: 600; }

.chart-tip {
  position: fixed;
  z-index: 60;
  display: grid;
  gap: .1rem;
  padding: .5rem .7rem;
  border-radius: 8px;
  background: var(--fg);
  color: var(--bg);
  box-shadow: 0 8px 24px -10px rgba(0, 0, 0, .45);
  font-family: var(--font-shell);
  font-size: .8125rem;
  pointer-events: none;
  white-space: nowrap;
}
.chart-tip[hidden] { display: none; }
.chart-tip__value { font-size: .9375rem; font-weight: 650; }
.chart-tip__label { opacity: .78; }

/* -- the four desks ----------------------------------------------------------------- */
.stats-desks { margin-top: var(--space-xl); }
.share { margin-top: 1.1rem; }
.share__bar { display: flex; gap: 2px; height: 14px; border-radius: 4px; overflow: hidden; }
.share__bar.is-empty { background: var(--rule); }
.share__seg { flex: 1 1 0; min-width: 3px; cursor: default; }
.share__seg--nicholas { background: var(--c-nicholas); }
.share__seg--ava { background: var(--c-ava); }
.share__seg--ryan { background: var(--c-ryan); }
.share__seg--jason { background: var(--c-jason); }
.share__legend { display: flex; flex-wrap: wrap; gap: .5rem 1.5rem; margin-top: .75rem; padding: 0; list-style: none; }
.share__key { display: inline-flex; align-items: center; gap: .45rem; font-size: .875rem; }
.share__swatch { width: 10px; height: 10px; border-radius: 2px; }
.share__name { font-weight: 600; }
.share__pct { color: var(--fg-muted); font-variant-numeric: tabular-nums; }

.desk-stats {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
  margin-top: 1.4rem;
}
@media (min-width: 640px) { .desk-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 1100px) { .desk-stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.desk-stat {
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1.25rem 1.15rem;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
}
.desk-stat__head { display: flex; align-items: center; gap: .6rem; }
.desk-stat__mark { width: 30px; height: 30px; flex: none; color: var(--fg); }
.desk-stat__name { font-family: var(--font-display); font-size: 1.25rem; font-weight: 600; line-height: 1.15; }
.desk-stat__name a { color: inherit; text-decoration: none; }
.desk-stat__name a:hover { text-decoration: underline; text-underline-offset: .18em; }
.desk-stat__value { margin-top: 1.1rem; }
.desk-stat__value [data-desk-views] {
  font-family: var(--font-display);
  font-size: 2.75rem;
  font-weight: 600;
  line-height: 1;
  letter-spacing: -.02em;
}
.desk-stat__unit { color: var(--fg-muted); font-size: .875rem; }
.desk-stat__share { margin-top: .35rem; color: var(--fg-muted); font-size: .8125rem; }
.desk-stat__label {
  margin-top: 1.1rem;
  padding-top: .9rem;
  border-top: 1px solid var(--rule);
  color: var(--fg-muted);
  font-family: var(--font-ui, var(--font-body));
  font-size: .6875rem;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
}
.desk-stat__top { display: grid; gap: .55rem; margin-top: .6rem; padding: 0; list-style: none; }
.desk-stat__item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .75rem; align-items: baseline; }
.desk-stat__link {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  color: var(--fg);
  font-size: .875rem;
  line-height: 1.35;
  text-decoration: none;
}
.desk-stat__link:hover .desk-stat__title { text-decoration: underline; text-underline-offset: .16em; }
.desk-stat__num {
  margin-right: .45rem;
  color: var(--accent-ink, var(--fg-muted));
  font-family: var(--font-mono);
  font-size: .8125rem;
}
.desk-stat__views { color: var(--fg-muted); font-size: .8125rem; font-variant-numeric: tabular-nums; }
.desk-stat__none { color: var(--fg-muted); font-size: .875rem; font-style: italic; }

/* -- most-read items ---------------------------------------------------------------- */
.top-items { margin-top: .75rem; padding: 0; list-style: none; }
.top-item {
  display: grid;
  grid-template-columns: 1.75rem minmax(0, 1fr) auto;
  gap: .25rem 1rem;
  align-items: center;
  padding: .75rem 0;
  border-top: 1px solid var(--rule);
}
.top-item:first-child { border-top: 0; }
.top-item__rank { color: var(--fg-muted); font-size: .875rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.top-item__title { color: var(--fg); font-size: .9375rem; font-weight: 600; line-height: 1.3; text-decoration: none; }
.top-item__title:hover { text-decoration: underline; text-underline-offset: .16em; }
.top-item__meta { display: flex; align-items: center; gap: .4rem; margin-top: .2rem; color: var(--fg-muted); font-size: .75rem; }
.top-item__swatch { width: 8px; height: 8px; flex: none; border-radius: 2px; }
.top-item__track { display: block; height: 8px; margin-top: .5rem; }
.top-item__fill { display: block; height: 8px; border-radius: 0 4px 4px 0; }
.top-item__value { align-self: end; font-size: .9375rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.top-items__empty, .hbars__empty { padding: 1rem 0 .25rem; color: var(--fg-muted); font-size: .875rem; }

/* -- trio: hours, devices, sources --------------------------------------------------- */
.stats-trio { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1rem; margin-top: var(--space-m); }
.stats-trio .stats-card { margin-top: 0; }
@media (min-width: 1000px) { .stats-trio { grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr) minmax(0, 1fr); } }
.hbars { display: grid; gap: .8rem; margin-top: 1rem; padding: 0; list-style: none; }
.hbar__head { display: flex; justify-content: space-between; gap: .75rem; font-size: .875rem; }
.hbar__label { overflow-wrap: anywhere; }
.hbar__value { flex: none; color: var(--fg-muted); font-variant-numeric: tabular-nums; white-space: pre; }
.hbar__track { display: block; height: 8px; margin-top: .35rem; }
.hbar__fill { display: block; height: 8px; border-radius: 0 4px 4px 0; background: var(--viz-ink); }

/* -- odds and ends -------------------------------------------------------------------- */
.stats-facts { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1rem; margin-top: var(--space-m); }
@media (min-width: 900px) { .stats-facts { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.fact {
  display: flex;
  flex-direction: column;
  gap: .35rem;
  padding: 1.1rem 1.25rem;
  border: 1px dashed var(--viz-base);
  border-radius: var(--radius);
}
.fact__value { font-family: var(--font-shell); font-size: 1.75rem; font-weight: 650; line-height: 1; letter-spacing: -.03em; }
.fact__text { color: var(--fg-muted); font-size: .875rem; line-height: 1.45; }
.fact code {
  padding: .05rem .35rem;
  border-radius: 4px;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-mono);
  font-size: .8125rem;
  overflow-wrap: anywhere;
}

.stats-note { margin-top: var(--space-xl); }
.stats-note > * { max-width: 70ch; }
.stats-note__title {
  color: var(--fg-muted);
  font-family: var(--font-shell);
  font-size: .75rem;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
}
.stats-note p { margin-top: .6rem; color: var(--fg-muted); font-size: .875rem; line-height: 1.65; }
.stats-note a { color: var(--fg); }
