/* Stats page: KPI tiles, range bar, SVG charts, heatmap, tooltip.
   Chart colours: categorical slots validated against the panel surface (#121a2f) for
   lightness band, CVD separation and contrast; sequential ramp is one hue, dark → light. */
.stats-page {
  --surface: #121a2f;
  --grid: rgba(142, 162, 255, 0.10);
  --axis: #6c789c;
  --bar: #8ea2ff;
  --bar-dim: rgba(142, 162, 255, 0.22);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s-other: #5b6584;
  --h0: #18213a; --h1: #24386e; --h2: #3557b0; --h3: #5b7fe6; --h4: #a9bbff;
}

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 8px; }
@media (max-width: 1000px) { .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } .kpi--hero { grid-column: span 2; grid-row: auto; } }
.kpi {
  position: relative; padding: 18px 20px; border-radius: 16px; border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(18, 26, 47, 0.88), rgba(10, 15, 30, 0.9));
  min-width: 0; overflow: hidden;
}
.kpi { animation: st-in 0.6s var(--ease-out) backwards; }
.kpi:nth-child(2) { animation-delay: .05s; } .kpi:nth-child(3) { animation-delay: .1s; } .kpi:nth-child(4) { animation-delay: .15s; } .kpi:nth-child(5) { animation-delay: .2s; } .kpi:nth-child(6) { animation-delay: .25s; } .kpi:nth-child(7) { animation-delay: .3s; }
@keyframes st-in { from { opacity: 0; transform: translateY(12px) scale(.98); } }
.kpi__label { font-size: 0.84rem; color: var(--fg-3); display: flex; align-items: center; gap: 8px; }
.kpi__label i { color: var(--signal); font-size: 0.85rem; width: 1em; text-align: center; }
.kpi__value { display: block; font-family: var(--font-body); font-weight: 600; font-size: clamp(1.6rem, 1.2rem + 1vw, 2.2rem); letter-spacing: -0.02em; margin: 6px 0 2px; color: var(--fg); line-height: 1.1; }
.kpi__value small { font-size: 0.5em; font-weight: 500; color: var(--fg-2); margin-left: 4px; letter-spacing: 0; }
.kpi__sub { font-size: 0.82rem; color: var(--fg-2); }
.kpi--hero { grid-row: span 2; display: flex; flex-direction: column; justify-content: space-between; }
.kpi--hero .kpi__value { font-size: clamp(3rem, 2rem + 3vw, 4.6rem); letter-spacing: -0.04em; margin: 10px 0 6px; }
.kpi__spark { display: block; width: 100%; height: 56px; margin-top: 14px; }
.kpi.is-loading .kpi__value, .kpi.is-loading .kpi__sub { color: transparent; border-radius: 6px; background: linear-gradient(90deg, rgba(142,162,255,.06), rgba(142,162,255,.14), rgba(142,162,255,.06)); background-size: 200% 100%; animation: st-shimmer 1.4s linear infinite; }
.kpi.is-loading .kpi__value { width: 60%; }
@keyframes st-shimmer { to { background-position: -200% 0; } }

/* ---------- range bar ---------- */
.stats-bar { margin-top: 18px; }
.stats-bar__label { font-size: 0.85rem; color: var(--fg-3); display: inline-flex; gap: 8px; align-items: center; }
.stats-bar__sum { margin-left: auto; font-size: 0.88rem; color: var(--fg-2); }
.stats-bar__sum b { color: var(--fg); font-weight: 600; }
@media (max-width: 700px) { .stats-bar__sum { margin-left: 0; width: 100%; } }

/* ---------- chart panels ---------- */
.stats-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.stats-grid .span-2 { grid-column: span 2; }
@media (max-width: 900px) { .stats-grid { grid-template-columns: minmax(0, 1fr); } .stats-grid .span-2 { grid-column: auto; } }
.viz .panel__head { align-items: flex-start; }
.viz__sub { font-size: 0.84rem; color: var(--fg-3); margin-top: 4px; }
.viz__body { position: relative; min-height: 120px; }
.viz__body svg { display: block; width: 100%; overflow: visible; }
.viz__body.is-loading::after { content: ''; position: absolute; inset: 0; border-radius: 10px; background: linear-gradient(90deg, rgba(142,162,255,.04), rgba(142,162,255,.10), rgba(142,162,255,.04)); background-size: 200% 100%; animation: st-shimmer 1.4s linear infinite; }
.viz__empty { padding: 30px 0; text-align: center; color: var(--fg-3); }
.viz__toggle[aria-pressed="true"] { border-color: var(--signal); color: var(--fg); }
.viz__legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: -4px 0 12px; font-size: 0.82rem; color: var(--fg-2); }
.viz__legend span { display: inline-flex; align-items: center; gap: 7px; }
.viz__legend i { width: 10px; height: 10px; border-radius: 3px; background: var(--c); }

/* svg text + guides */
.viz text { font-family: var(--font-body); font-size: 12px; fill: var(--axis); }
.viz text.val { fill: var(--fg-2); font-weight: 600; }
.viz text.lbl { fill: var(--fg-2); }
.viz .grid line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.viz .base { stroke: rgba(142, 162, 255, 0.28); stroke-width: 1; shape-rendering: crispEdges; }
.viz .hit { fill: transparent; cursor: default; }
.viz .mark { transition: opacity 0.2s, fill 0.3s; }
.viz .is-dim { opacity: 0.32; }
.viz g.col.hover .mark, .viz g.row.hover .mark { filter: brightness(1.25); }
.viz g.col.hover .hit, .viz g.row.hover .hit { fill: rgba(142, 162, 255, 0.06); }
/* bars grow in once when first drawn */
.viz.grow .mark--v { transform-box: fill-box; transform-origin: 50% 100%; animation: st-grow-v 0.7s var(--ease-out) backwards; animation-delay: calc(var(--i, 0) * 8ms); }
.viz.grow .mark--h { transform-box: fill-box; transform-origin: 0 50%; animation: st-grow-h 0.7s var(--ease-out) backwards; animation-delay: calc(var(--i, 0) * 40ms); }
@keyframes st-grow-v { from { transform: scaleY(0); } }
@keyframes st-grow-h { from { transform: scaleX(0); } }

/* ---------- heatmap ---------- */
.heat rect.cell { rx: 3px; ry: 3px; transition: opacity 0.2s; }
.heat rect.cell[data-l="0"] { fill: var(--h0); } .heat rect.cell[data-l="1"] { fill: var(--h1); } .heat rect.cell[data-l="2"] { fill: var(--h2); }
.heat rect.cell[data-l="3"] { fill: var(--h3); } .heat rect.cell[data-l="4"] { fill: var(--h4); }
.heat rect.cell.future { fill: transparent; stroke: rgba(142, 162, 255, 0.08); }
.heat rect.cell:hover { stroke: var(--fg); stroke-width: 1.5px; }
.heat.grow rect.cell { animation: st-cell 0.5s var(--ease-out) backwards; animation-delay: calc(var(--w, 0) * 9ms); }
@keyframes st-cell { from { opacity: 0; } }
.heat-legend { display: inline-flex; align-items: center; gap: 4px; font-size: 0.78rem; color: var(--fg-3); white-space: nowrap; }
.heat-legend i { width: 11px; height: 11px; border-radius: 3px; }
.heat-legend i[data-l="0"] { background: var(--h0); } .heat-legend i[data-l="1"] { background: var(--h1); } .heat-legend i[data-l="2"] { background: var(--h2); }
.heat-legend i[data-l="3"] { background: var(--h3); } .heat-legend i[data-l="4"] { background: var(--h4); }
.heat-legend i:first-of-type { margin-left: 4px; } .heat-legend i:last-of-type { margin-right: 4px; }

.cal-scroll { overflow-x: auto; padding-bottom: 4px; }
.viz__body .cal-scroll svg { display: block; width: auto; max-width: none; height: auto; }

/* ---------- busiest days ---------- */
.topdays { display: grid; gap: 16px; list-style: none; padding: 0; margin: 0; counter-reset: td; }
.topdays li { display: grid; grid-template-columns: 26px minmax(0, 1fr) auto; column-gap: 12px; row-gap: 6px; align-items: center; animation: run-in-x 0.5s var(--ease-out) backwards; animation-delay: calc(var(--i) * 60ms); }
.topdays li::before { counter-increment: td; content: counter(td); font-family: var(--font-head); font-size: 0.85rem; color: var(--fg-3); text-align: center; }
.topdays .td__name { font-size: 0.92rem; color: var(--fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topdays .td__bar { grid-column: 2 / 4; height: 6px; border-radius: 0 4px 4px 0; background: var(--bar); transform-origin: 0 50%; animation: st-grow-h 0.8s var(--ease-out) backwards; animation-delay: calc(var(--i) * 60ms + 0.1s); }
.topdays .td__val { grid-row: 1; grid-column: 3; font-weight: 600; color: var(--fg); font-variant-numeric: tabular-nums; }
.topdays .td__val small { color: var(--fg-3); font-weight: 400; }
@keyframes run-in-x { from { opacity: 0; transform: translateX(-8px); } }

/* ---------- repo table ---------- */
.repo-table { max-height: 560px; }
.repo-table th { cursor: pointer; user-select: none; }
.repo-table th:hover { color: var(--fg); }
.repo-table th[aria-sort] { color: var(--fg); }
.repo-table th[aria-sort]::after { content: ' ↓'; color: var(--signal); }
.repo-table th[aria-sort="ascending"]::after { content: ' ↑'; }
.repo-table td.num, .repo-table th.num { text-align: right; }
.repo-table td a, .repo-table td .repo-name { color: var(--fg); font-family: var(--font-body); font-weight: 600; }
.repo-table td a:hover { color: var(--signal); }
.repo-table .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--c); margin-right: 8px; vertical-align: 0; }
.repo-table td { font-family: var(--font-body); }
.repo-table td.muted { color: var(--fg-3); }
.fork-table { max-height: 420px; }
.fork-table th { cursor: default; }
.fork-table th:hover { color: var(--fg-3); }
.viz__body .table-wrap { max-height: 380px; }

/* ---------- tooltip ---------- */
.viz-tip {
  position: fixed; z-index: 9000; pointer-events: none; left: 0; top: 0;
  padding: 8px 11px; border-radius: 10px; font-size: 0.82rem; line-height: 1.4; color: var(--fg);
  background: rgba(10, 15, 30, 0.96); border: 1px solid var(--line); box-shadow: 0 12px 30px -10px rgba(0, 0, 0, 0.7);
  transition: opacity 0.12s; white-space: nowrap;
}
.viz-tip[hidden] { display: block; opacity: 0; }
.viz-tip b { font-weight: 600; }
.viz-tip .tip__sw { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 7px; background: var(--c); }
.viz-tip .tip__m { color: var(--fg-3); }

.stats-foot { margin: 22px 0 10px; }
.stats-error { grid-column: 1 / -1; display: flex; gap: 14px; align-items: center; justify-content: space-between; flex-wrap: wrap; padding: 16px 18px; border-radius: 14px; border: 1px solid rgba(255, 184, 107, 0.4); background: rgba(255, 184, 107, 0.07); color: var(--fg-2); }
.stats-error i { color: var(--amber); margin-right: 8px; }

@media (prefers-reduced-motion: reduce) {
  .kpi, .viz.grow .mark--v, .viz.grow .mark--h, .heat.grow rect.cell, .topdays li, .topdays .td__bar { animation: none; }
}

/* ---------- repository timeline ---------- */
.tl a { cursor: pointer; }
.tl a:focus-visible .hit { fill: rgba(142, 162, 255, 0.1); outline: none; }
.tl .tl-today { stroke: var(--amber); stroke-width: 1; shape-rendering: crispEdges; opacity: 0.8; }
.tl text.tl-today-l { fill: var(--amber); }
.tl .tl-end { fill: var(--mint); stroke: var(--surface); stroke-width: 2; }
.viz__legend .tl-dot { background: var(--mint); border-radius: 50%; }
.viz.grow .mark--t { transform-box: fill-box; transform-origin: 0 50%; animation: st-grow-h 0.8s var(--ease-out) backwards; animation-delay: calc(var(--i, 0) * 30ms); }
.viz.grow .tl-end { animation: st-pop 0.4s var(--ease-spring) 0.7s backwards; transform-box: fill-box; transform-origin: center; }
@keyframes st-pop { from { transform: scale(0); } }
@media (prefers-reduced-motion: reduce) { .viz.grow .mark--t, .viz.grow .tl-end { animation: none; } }
