/* ================================================================
   Stats pages (history + current). Uses site palette so it inherits
   all 6 colorway schemes (Ember/Abyss/Frost/Verdant/Parchment/Dusk).
   ================================================================ */

/* The stats pages have a lot of side-by-side charts that get cramped at
   the site's default 960px container. Widen the container and tighten its
   side padding when stats sections are present. */
main.container:has(.stats-section) {
    max-width: 1400px;
    padding-left: 0.75rem;
    padding-right: 0.75rem;
}

.stats-summary {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}
@media (max-width: 900px) {
    .stats-summary { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 480px) {
    .stats-summary { grid-template-columns: 1fr; }
}
.stats-summary .stat-card { padding: 0.7rem 0.9rem; }
.stats-summary .label {
    display: block;
    font-family: var(--font-prose);
    font-size: 0.62rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--text-dim);
    margin-bottom: 0.35rem;
}
.stats-summary .value {
    font-family: var(--font-mono);
    font-size: 1.3rem;
    font-weight: 600;
    color: var(--text-bright);
    line-height: 1.1;
}
.stats-summary .value .sep {
    color: var(--text-dim);
    font-weight: 400;
    margin: 0 0.35em;
    font-family: var(--font-prose);
    font-size: 0.7em;
    text-transform: lowercase;
    letter-spacing: 0.05em;
}
.stats-summary .value .qual {
    font-family: var(--font-prose);
    font-size: 0.68rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 500;
    margin-left: 0.35rem;
    vertical-align: middle;
}

.stats-anchors {
    position: sticky;
    top: 40px;
    z-index: 4;
    background: var(--bg-body);
    border-bottom: 1px solid var(--border);
    margin: 0 -0.75rem 1.25rem;
    padding: 0.5rem 0.75rem;
    overflow-x: auto;
    white-space: nowrap;
    scrollbar-width: thin;
}
@media (max-width: 720px) {
    .stats-anchors { margin: 0 -0.5rem 1.25rem; padding-left: 0.5rem; padding-right: 0.5rem; }
}
.stats-anchors a {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--text-dim);
    text-decoration: none;
    padding: 0.2rem 0.55rem;
    margin-right: 0.15rem;
    border: 1px solid transparent;
    border-radius: var(--radius-sm, 3px);
    transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.stats-anchors a:hover { color: var(--text-bright); background: var(--bg-hover); }
.stats-anchors a.is-active {
    color: var(--highlight);
    border-color: rgba(var(--highlight-rgb), 0.35);
    background: rgba(var(--highlight-rgb), 0.08);
}

.stats-section {
    margin-bottom: 1.5rem;
    scroll-margin-top: 92px;
}
.stats-section-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.75rem;
    border-bottom: 1px solid var(--border);
    padding: 0 0 0.4rem;
    margin-bottom: 0.7rem;
}
.stats-section-head .label {
    font-family: var(--font-prose);
    font-size: 0.62rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--highlight);
}
.stats-section-head .blurb {
    font-family: var(--font-prose);
    font-size: 0.78rem;
    color: var(--text-dim);
    margin: 0;
    text-align: right;
    flex: 1 1 auto;
}

.stats-frame {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 6px);
    padding: 0.85rem 1rem 0.7rem;
    margin-bottom: 0.75rem;
    transition: border-color 0.15s;
}
.stats-frame:hover { border-color: var(--border-hover, var(--border)); }
.stats-frame .ftitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.6rem;
    flex-wrap: wrap;
}
.stats-frame .ftitle h3 {
    margin: 0;
    font-family: var(--font-prose);
    font-size: 0.7rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-dim);
}
.stats-frame .ftitle .meta {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text-dim);
}
.stats-frame svg { display: block; max-width: 100%; height: auto; overflow: visible; }

.stats-grid { display: grid; gap: 0.75rem; }
.stats-grid.feature { grid-template-columns: 5fr 3fr; }
.stats-grid.equal   { grid-template-columns: 1fr 1fr; }
.stats-grid.thirds  { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 900px) {
    .stats-grid.thirds  { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 720px) {
    .stats-grid.feature, .stats-grid.equal, .stats-grid.thirds { grid-template-columns: 1fr; }
}

/* Small multiples */
.sm-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.4rem;
}
.sm-cell {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm, 3px);
    padding: 0.4rem 0.55rem 0.3rem;
    transition: border-color 0.15s, background 0.15s;
}
.sm-cell:hover { border-color: var(--text-dim); background: var(--bg-hover); }
.sm-cell .name {
    font-family: var(--font-prose);
    font-size: 0.78rem;
    color: var(--text-bright);
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}
.sm-cell .chart svg { display: block; width: 100%; height: 36px; margin: 0.1rem 0; }
.sm-cell .stats {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-family: var(--font-mono);
    font-size: 0.68rem;
}
.sm-cell .last { color: var(--text-bright); font-weight: 600; font-size: 0.85rem; }
.sm-cell .delta { color: var(--text-dim); letter-spacing: 0.01em; }
.sm-cell .delta.pos { color: var(--accent); }
.sm-cell .delta.neg { color: var(--error); }
.sm-cell .delta.zero { color: var(--text-dim); }

/* Legend */
.stats-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.75rem;
    margin-top: 0.6rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text);
}
.stats-legend > span {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    cursor: pointer;
    user-select: none;
    transition: opacity 0.15s;
    padding: 0.05rem 0.2rem;
    border-radius: 2px;
}
.stats-legend > span:hover { background: var(--bg-hover); }
.stats-legend > span.dimmed { opacity: 0.3; }
.stats-legend i {
    width: 9px;
    height: 9px;
    display: inline-block;
    border-radius: 1px;
}

/* Table */
table.stats-table {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--font-mono);
    font-size: 0.78rem;
}
table.stats-table thead th {
    background: var(--bg-secondary);
    color: var(--text-dim);
    text-align: left;
    padding: 0.45rem 0.7rem;
    font-family: var(--font-prose);
    font-weight: 500;
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    border-bottom: 1px solid var(--border);
}
table.stats-table tbody td {
    padding: 0.45rem 0.7rem;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    vertical-align: middle;
}
table.stats-table tbody tr:hover td { background: var(--bg-hover); }
table.stats-table .num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    color: var(--text-bright);
}
table.stats-table .name {
    font-family: var(--font-prose);
    font-weight: 500;
    color: var(--link);
    font-size: 0.85rem;
}
table.stats-table .rank { color: var(--text-dim); font-size: 0.72rem; width: 2.5rem; }
table.stats-table .dim { color: var(--text-dim); }
table.stats-table .pos { color: var(--accent); }
table.stats-table .neg { color: var(--error); }

/* Heatmap */
.heat-wrap { overflow-x: auto; padding-bottom: 0.5rem; }
.heat {
    display: grid;
    width: 100%;
    gap: 1px;
    background: var(--border);
    padding: 1px;
    border: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: 0.72rem;
}
.heat > div {
    background: var(--bg-secondary);
    padding: 0.4rem 0.5rem;
    text-align: center;
    color: var(--text-dim);
    min-height: 24px;
    line-height: 1.4;
    display: flex;
    align-items: center;
    justify-content: center;
}
.heat > .corner { background: var(--bg-panel); }
.heat > .h-row {
    color: var(--text);
    font-family: var(--font-prose);
    font-size: 0.78rem;
    background: var(--bg-panel);
    justify-content: flex-end;
    padding-right: 0.7rem;
}
.heat > .h-col {
    color: var(--text-dim);
    font-family: var(--font-prose);
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    background: var(--bg-panel);
    /* Use sideways-lr so the text reads bottom-to-top without needing a
       transform hack that throws off centering. */
    writing-mode: sideways-lr;
    padding: 0.5rem 0.2rem;
    min-height: 90px;
}
@supports not (writing-mode: sideways-lr) {
    .heat > .h-col {
        writing-mode: vertical-rl;
        transform: rotate(180deg);
    }
}
.heat > .cell.zero { color: var(--text-dim); opacity: 0.3; }
.heat > .cell.dense { color: var(--bg-body); font-weight: 600; }

/* Year/month picker */
.year-pick {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 0.15rem;
}
.year-pick button {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    padding: 0.18rem 0.5rem;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm, 3px);
    color: var(--text-dim);
    cursor: pointer;
    transition: all 0.15s;
}
.year-pick button:hover { color: var(--text-bright); border-color: var(--text-dim); background: var(--bg-hover); }
.year-pick button.active {
    color: var(--highlight);
    border-color: rgba(var(--highlight-rgb), 0.5);
    background: rgba(var(--highlight-rgb), 0.1);
}

/* State (loading / error) */
.stats-state {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 6px);
    padding: 2rem 1.5rem;
    margin-bottom: 1.25rem;
    font-family: var(--font-mono);
}
.stats-state .stage {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    color: var(--text-dim);
    font-size: 0.82rem;
    margin-bottom: 0.5rem;
}
.stats-state .stage::before { content: '>'; color: var(--highlight); font-weight: 600; }
.stats-state .stage .blink {
    display: inline-block;
    width: 7px;
    height: 0.95em;
    background: var(--highlight);
    margin-left: 0.2rem;
    animation: blink 1.05s steps(2, jump-none) infinite;
    vertical-align: text-bottom;
}
@keyframes blink { 50% { opacity: 0; } }
.stats-state .substage {
    color: var(--text-dim);
    font-size: 0.74rem;
    line-height: 1.65;
    padding-left: 1rem;
    border-left: 2px solid var(--border);
    margin-left: 0.2rem;
}
.stats-state .substage code {
    color: var(--text-bright);
    font-family: var(--font-mono);
}
.stats-state.error { border-color: rgba(var(--error-rgb, 168, 114, 114), 0.35); }
.stats-state.error .stage { color: var(--error); }
.stats-state.error .stage::before { color: var(--error); content: '!'; }
.stats-state.error .stage .blink { display: none; }
.stats-state.error .substage {
    border-color: rgba(var(--error-rgb, 168, 114, 114), 0.35);
    color: var(--text);
}

/* Tooltip */
.stats-tip {
    position: fixed;
    pointer-events: none;
    background: var(--bg-panel);
    color: var(--text-bright);
    border: 1px solid var(--border-hover, var(--border));
    box-shadow: 0 4px 14px rgba(0,0,0,0.5);
    padding: 0.4rem 0.6rem;
    font-family: var(--font-mono);
    font-size: 0.72rem;
    line-height: 1.5;
    z-index: 100;
    opacity: 0;
    transition: opacity 0.1s;
    max-width: 260px;
    white-space: nowrap;
}
.stats-tip.show { opacity: 1; }
.stats-tip strong { color: var(--highlight); font-weight: 500; }

/* Footnote */
.stats-footnote {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 6px);
    padding: 1rem 1.15rem;
    font-family: var(--font-prose);
    font-size: 0.82rem;
    line-height: 1.65;
    color: var(--text-dim);
}
.stats-footnote code {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--text-bright);
    background: var(--bg-panel);
    padding: 0.05rem 0.3rem;
    border-radius: 2px;
}
.stats-footnote p { margin: 0 0 0.5rem; }
.stats-footnote p:last-child { margin: 0; }

#stats-content[hidden] { display: none !important; }
