/*
 * Stats Overlay — shared stylesheet
 *
 * Extracted from the demo dashboard (Demos.WebDashboard.Blazor) so that any
 * OFF host (including game clients) can reuse the stats panel without pulling
 * in the full dashboard CSS.
 *
 * Provides: expandable stats panel, chip filters, row/item layout,
 *           semantic colour classes, graph canvas, and mini FPS badge.
 */

/* --- Panel chrome --- */

.stats-panel {
    position: absolute;
    /* Top-left: pops out from where the FPS chip lives. */
    top: calc(56px + env(safe-area-inset-top));
    left: calc(4px + env(safe-area-inset-left));
    right: auto;
    z-index: 50;
    background: rgba(0, 0, 0, 0.85);
    border-radius: 6px;
    font: 11px/1.3 monospace;
    color: #e0e0e0;
    pointer-events: auto;
    width: 620px;
    user-select: text;
    overflow: hidden;
}

.stats-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 3px 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(255, 255, 255, 0.03);
}

.stats-header-label {
    font-size: 10px;
    color: #7b8fb8;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stats-hint {
    font-size: 9px;
    color: rgba(255, 255, 255, 0.25);
    margin-left: auto;
}

/* --- Buttons --- */

.stats-btn {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: #e0e0e0;
    font: 10px monospace;
    padding: 0 5px;
    border-radius: 3px;
    cursor: pointer;
    line-height: 16px;
}
.stats-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* --- Chip filters --- */

.stats-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    padding: 3px 6px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.stats-chip {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.3);
    font: 9px monospace;
    padding: 1px 5px;
    border-radius: 3px;
    cursor: pointer;
    line-height: 14px;
}
.stats-chip:hover {
    color: rgba(255, 255, 255, 0.6);
    border-color: rgba(255, 255, 255, 0.2);
}
.stats-chip-on {
    color: #e0e0e0;
    border-color: rgba(76, 201, 240, 0.4);
    background: rgba(76, 201, 240, 0.1);
}

/* --- Body / cards ---
 *
 * Dashboard model. Each registered section renders into a fixed-size
 * card; card grid is auto-fill so the panel adapts to its width. All
 * numeric values use tabular-nums so changing digits don't reflow the
 * layout. Cards never collapse — values stay anchored even when their
 * digits widen.
 */

.stats-body {
    padding: 6px;
    overflow: hidden;
}

.stats-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 6px;
    font-variant-numeric: tabular-nums;
}

.stats-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 4px;
    padding: 5px 7px 6px;
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
    /* Anchors card height — `update(body)` callbacks that conditionally
       skip helpers won't yank neighbouring cards around. The grid still
       grows past this when content needs more (e.g. RENDER list with
       many passes); it just stops shrinking. */
    min-height: 76px;
    /* Card body is pushed to the top so taller cards don't centre
       their content. */
    justify-content: flex-start;
}

.stats-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.stats-card-title {
    font-size: 9px;
    color: #7b8fb8;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    cursor: help;
}

.stats-card-tag {
    font-size: 9px;
    color: rgba(255, 255, 255, 0.4);
    font-variant-numeric: tabular-nums;
}

/* Big metric value — anchors the card. Always tabular-nums so digits
   don't shift the surrounding layout. */
.stats-card-big {
    font-size: 18px;
    line-height: 1.1;
    font-weight: 500;
    color: #e0e0e0;
    display: flex;
    align-items: baseline;
    gap: 4px;
}

.stats-card-unit {
    font-size: 10px;
    color: rgba(255, 255, 255, 0.4);
    font-weight: normal;
}

/* SVG sparkline — fixed height, scales horizontally. */
.stats-card-spark {
    display: block;
    width: 100%;
    height: 16px;
    opacity: 0.85;
}
.stats-card-spark .spark-line {
    fill: none;
    stroke: #4cc9f0;
    stroke-width: 1;
}
.stats-card-spark .spark-budget {
    stroke: rgba(255, 255, 255, 0.15);
    stroke-width: 1;
    stroke-dasharray: 2 2;
}

/* Budget bar — value/budget visualised as a fill + numeric label. */
.stats-card-budget {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 6px;
    font-size: 10px;
}
.stats-card-budget-track {
    height: 4px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 2px;
    overflow: hidden;
    position: relative;
}
.stats-card-budget-fill {
    position: absolute;
    inset: 0 auto 0 0;
    border-radius: 2px;
    background: #06d6a0;
    transition: width 200ms ease-out;
}
.stats-card-budget-fill.stats-warn { background: #ffd166; }
.stats-card-budget-fill.stats-bad  { background: #f44; }
.stats-card-budget-label {
    color: rgba(255, 255, 255, 0.55);
    white-space: nowrap;
}

/* Detail rows — grid-aligned key/value pairs. fixed-width values,
   ellipsis-truncated label. Card width owns the alignment, not the
   number of digits. */
.stats-card-details {
    display: grid;
    grid-template-columns: auto 1fr;
    column-gap: 6px;
    row-gap: 1px;
    font-size: 10px;
    color: rgba(255, 255, 255, 0.7);
}
.stats-card-detail-label {
    color: #7b8fb8;
    text-transform: uppercase;
    font-size: 9px;
    letter-spacing: 0.4px;
    cursor: help;
}
.stats-card-detail-val {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Compact resource KPI strip — keeps GPU/CPU/runtime/lifecycle counters
   scannable without consuming one vertical row per value. */
.stats-resource-kpis {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(68px, 1fr));
    gap: 2px 8px;
    font-size: 10px;
    font-variant-numeric: tabular-nums;
}
.stats-resource-kpi {
    min-width: 0;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 4px;
    white-space: nowrap;
}
.stats-resource-kpi-label {
    flex: 0 0 auto;
    color: #7b8fb8;
    font-size: 9px;
    letter-spacing: 0.4px;
    cursor: help;
}
.stats-resource-kpi-value {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    color: rgba(255, 255, 255, 0.8);
    text-align: right;
}
.stats-resource-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 1px;
}
.stats-resource-open,
.stats-resource-popup-close {
    border: 1px solid rgba(76, 201, 240, 0.35);
    background: rgba(76, 201, 240, 0.1);
    color: #e0e0e0;
    border-radius: 3px;
    font: 10px/1 monospace;
    cursor: pointer;
}
.stats-resource-open {
    padding: 3px 7px;
}
.stats-resource-open:hover,
.stats-resource-popup-close:hover {
    background: rgba(76, 201, 240, 0.2);
    border-color: rgba(76, 201, 240, 0.55);
}

.stats-resource-popup-backdrop {
    position: fixed;
    inset: 0;
    z-index: 90;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    box-sizing: border-box;
    padding: calc(64px + env(safe-area-inset-top)) 16px 16px;
    background: rgba(0, 0, 0, 0.32);
    pointer-events: auto;
}
.stats-resource-popup {
    width: min(920px, calc(100vw - 32px));
    max-height: calc(100vh - 96px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    background: rgba(7, 12, 18, 0.96);
    color: #e0e0e0;
    font: 11px/1.3 monospace;
    box-shadow: 0 18px 48px rgba(0, 0, 0, 0.5);
}
.stats-resource-popup-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(255, 255, 255, 0.04);
}
.stats-resource-popup-title {
    color: #7b8fb8;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
}
.stats-resource-popup-close {
    margin-left: auto;
    width: 22px;
    height: 22px;
    padding: 0;
}
.stats-resource-popup-body {
    overflow: auto;
    padding: 10px;
}
.stats-resource-filters {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) auto auto auto;
    gap: 6px;
    align-items: center;
    margin: 0 0 10px;
}
.stats-resource-search {
    min-width: 0;
    height: 24px;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.28);
    color: #e0e0e0;
    padding: 0 8px;
    font: 10px/1 monospace;
    outline: none;
}
.stats-resource-search:focus {
    border-color: rgba(76, 201, 240, 0.55);
    background: rgba(0, 0, 0, 0.4);
}
.stats-resource-filter-group {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
    justify-content: flex-end;
}
.stats-resource-filter {
    min-height: 22px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    background: transparent;
    color: rgba(255, 255, 255, 0.48);
    padding: 2px 6px;
    font: 9px/1 monospace;
    cursor: pointer;
}
.stats-resource-filter:hover {
    color: rgba(255, 255, 255, 0.75);
    border-color: rgba(255, 255, 255, 0.22);
}
.stats-resource-filter.is-active {
    color: #e0e0e0;
    border-color: rgba(76, 201, 240, 0.42);
    background: rgba(76, 201, 240, 0.12);
}
.stats-resource-filter-count {
    color: rgba(255, 255, 255, 0.42);
    font-size: 9px;
    text-align: right;
    white-space: nowrap;
}
.stats-resource-popup-overview {
    display: grid;
    grid-template-columns: minmax(110px, auto) 1fr;
    gap: 8px 14px;
    align-items: center;
    margin-bottom: 10px;
}
.stats-resource-popup-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}
.stats-resource-popup-section {
    min-width: 0;
    padding: 8px;
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.03);
}
.stats-resource-popup-wide {
    margin-top: 10px;
}
.stats-resource-popup-section-title {
    margin-bottom: 6px;
    color: #7b8fb8;
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.stats-resource-section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 6px;
}
.stats-resource-section-head .stats-resource-popup-section-title {
    margin-bottom: 0;
}
.stats-resource-mini-button {
    min-height: 20px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.04);
    color: rgba(255, 255, 255, 0.6);
    padding: 2px 6px;
    font: 9px/1 monospace;
    cursor: pointer;
}
.stats-resource-mini-button:hover {
    color: #e0e0e0;
    border-color: rgba(76, 201, 240, 0.42);
    background: rgba(76, 201, 240, 0.12);
}
.stats-resource-accounting {
    margin-bottom: 10px;
}
.stats-resource-server {
    margin-bottom: 10px;
}
.stats-resource-server-status {
    color: rgba(255, 255, 255, 0.48);
    font-size: 10px;
    line-height: 1.35;
}
.stats-resource-accounting-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 2px 12px;
}
.stats-resource-accounting-note {
    margin-top: 6px;
    color: rgba(255, 255, 255, 0.48);
    font-size: 9px;
    line-height: 1.35;
}
.stats-resource-empty {
    color: rgba(255, 255, 255, 0.42);
    font-size: 10px;
    padding: 4px 0;
}

@media (max-width: 760px) {
    .stats-resource-popup {
        width: calc(100vw - 20px);
        max-height: calc(100vh - 72px);
    }
    .stats-resource-popup-backdrop {
        padding: calc(48px + env(safe-area-inset-top)) 10px 10px;
    }
    .stats-resource-popup-overview,
    .stats-resource-popup-grid,
    .stats-resource-accounting-grid,
    .stats-resource-filters {
        grid-template-columns: 1fr;
    }
    .stats-resource-filter-group {
        justify-content: flex-start;
    }
    .stats-resource-filter-count {
        text-align: left;
    }
}

/* List card body (e.g. RENDER passes) — each row: label | bar | value.
   Bar is proportional to budget (or row max); value is right-aligned. */
.stats-card-list {
    display: grid;
    grid-template-columns: minmax(50px, auto) 1fr 4em;
    column-gap: 6px;
    row-gap: 2px;
    font-size: 10px;
    align-items: center;
    min-width: 0;
}

/* Pair container — splits a long list into two side-by-side columns
   so wide cards (e.g. RENDER spanning 2 grid cols) don't grow tall.
   Insertion order fills left column first, then right. */
.stats-card-list-pair {
    display: flex;
    flex-direction: row;
    gap: 12px;
    align-items: start;
}
.stats-card-list-pair > .stats-card-list {
    flex: 1 1 0;
    min-width: 0;
}
.stats-card-listlabel {
    color: rgba(255, 255, 255, 0.75);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.stats-card-listbar {
    height: 4px;
    background: rgba(255, 255, 255, 0.06);
    border-radius: 2px;
    overflow: hidden;
    position: relative;
}
.stats-card-listfill {
    position: absolute;
    inset: 0 auto 0 0;
    background: #4cc9f0;
    border-radius: 2px;
}
.stats-card-listfill.stats-warn { background: #ffd166; }
.stats-card-listfill.stats-bad  { background: #f44; }
.stats-card-listval {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Card-internal divider used for footer summaries (e.g. RENDER list's
   GPU/CPU totals after the per-pass rows). */
.stats-card-divider {
    height: 1px;
    background: rgba(255, 255, 255, 0.06);
    margin: 2px 0 1px;
}

/* Legacy inline rows — kept so external sections can still register
   the old way. New built-ins use the card components above. */
.stats-row {
    line-height: 1.5;
}
.stats-row-label {
    color: #7b8fb8;
    margin-right: 6px;
    font-size: 10px;
    text-transform: uppercase;
    cursor: help;
}
.stats-row-content {
    display: inline;
}
.stats-item {
    margin-right: 8px;
    white-space: nowrap;
}
.stats-subline {
    display: block;
    margin-top: 2px;
}
.stats-line {
    display: block;
    line-height: 1.5;
}
.stats-val {
    display: inline-block;
    min-width: 2.5em;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* --- Semantic colours --- */

.stats-good { color: #06d6a0; }
.stats-warn { color: #ffd166; }
.stats-bad  { color: #f44; }
.stats-info { color: #4cc9f0; }
/* Dim filler — used for zero-state values that always occupy a slot
   to keep card layout stable. Reads as "not interesting" without
   removing the row. */
.stats-zero { color: rgba(255, 255, 255, 0.25); }

/* --- Graph --- */

.stats-graph-canvas {
    display: block;
    width: 100%;
}

/* --- Mini FPS badge --- */

.stats-mini {
    position: absolute;
    /* Top-left chip — companion to host's top-row HUD widgets (strip,
       trigger). Pill geometry + dark-blue surface so all three read as
       one visual family. */
    top: calc(12px + env(safe-area-inset-top));
    left: calc(12px + env(safe-area-inset-left));
    right: auto;
    z-index: 50;
    height: 32px;
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    font: 11px/1 monospace;
    color: rgba(232, 240, 255, 0.7);
    pointer-events: auto;
    cursor: pointer;
    padding: 0 12px;
    background: rgba(13, 21, 37, 0.78);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 999px;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    transition: color 120ms, background 120ms;
}
.stats-mini:hover {
    color: #fff;
    background: rgba(13, 21, 37, 0.88);
}
