.stats-overlay {
    position: fixed; inset: 0; z-index: 1000;
    background: var(--overlay-bg);
    backdrop-filter: var(--overlay-blur); -webkit-backdrop-filter: var(--overlay-blur);
    display: none; align-items: center; justify-content: center;
    padding: 1rem; opacity: 0; transition: opacity 0.25s ease;
}
.stats-overlay.visible { opacity: 1; }

body.stats-modal-open .card.loading,
body.stats-modal-open .card.skeleton {
    animation: none;
    background: #15151d;
}

.stats-modal {
    --surface: #1f1f29;
    --surface-hover: #2a2a35;
    background: var(--surface); border: 1px solid rgba(255,255,255,0.1);
    border-radius: 18px; padding: 1.75rem;
    width: 100%; max-width: 740px;
    box-shadow: 0 32px 100px rgba(0,0,0,0.7);
    max-height: 88vh;
    display: flex; flex-direction: column;
    cursor: default;
    transform: scale(0.94) translateY(8px);
    transition: transform 0.25s ease;
}
.stats-overlay.visible .stats-modal { transform: scale(1) translateY(0); }

.stats-modal-header {
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: space-between;
    padding-bottom: 1rem; margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}
.stats-modal-title { font-size: 1.1rem; font-weight: 700; color: var(--text); }
.stats-modal-close {
    width: 32px; height: 32px; border-radius: 8px;
    background: rgba(255,255,255,0.06); border: 1px solid var(--border);
    color: var(--text-muted); font-size: 1.3rem; line-height: 1;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    font-family: inherit;
}
.stats-modal-close:hover { color: var(--text); background: rgba(255,255,255,0.1); }

.stats-modal-body-wrap {
    position: relative; flex: 1; min-height: 0;
    display: flex; flex-direction: column;
}
.stats-modal-body {
    flex: 1; overflow-y: auto;
    margin: 0 -0.5rem; padding: 0 0.5rem 0.5rem;
    overscroll-behavior: contain;
    scrollbar-width: none;
    container-type: inline-size;
}
.stats-modal-body::-webkit-scrollbar { display: none; }
.stats-modal-body-wrap::after {
    content: '';
    position: absolute; left: 0; right: 0; bottom: 0;
    height: 60px; pointer-events: none;
    background: linear-gradient(to top, var(--surface) 0%, transparent 100%);
    opacity: 1; transition: opacity 0.2s ease;
}
.stats-modal-body-wrap.at-bottom::after { opacity: 0; }

.stats-block + .stats-block,
.stats-block + .stats-two-col,
.stats-two-col + .stats-block,
.stats-two-col + .stats-two-col {
    padding-top: 1.25rem; margin-top: 1.25rem;
    border-top: 1px solid var(--border);
}

.stats-block-label {
    font-size: 0.7rem; font-weight: 600; letter-spacing: 0.06em;
    text-transform: uppercase; color: var(--text-muted);
    margin-bottom: 0.9rem;
    display: flex; justify-content: space-between; align-items: baseline;
}
.stats-block-label .hint {
    letter-spacing: 0; text-transform: none; font-weight: 500;
}

/* === KPI: 4 cards, uniform gap = two-col gap (alignment guaranteed) === */
.stats-kpi-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1rem; }
.stats-kpi-card {
    padding: 0.95rem 1rem; border-radius: 12px;
    background: var(--surface-hover); border: 1px solid var(--border);
}
.stats-kpi-label {
    font-size: 0.62rem; font-weight: 600; letter-spacing: 0.06em;
    text-transform: uppercase; color: var(--text-muted);
    margin-bottom: 0.45rem;
}
.stats-kpi-value {
    font-size: 1.45rem; font-weight: 700; letter-spacing: -0.03em; line-height: 1;
    color: var(--text);
}
.stats-kpi-value b { color: var(--success); font-weight: 700; }
.stats-kpi-value .unit { color: var(--text-muted); font-size: 0.85rem; font-weight: 500; }
.stats-kpi-delta {
    margin-top: 0.4rem; font-size: 0.68rem; color: var(--text-muted);
}
.stats-kpi-delta .up { color: var(--success); font-weight: 600; }

/* === Activity: 30 days, 3 rows × 10 columns, centered === */
.stats-activity-inner { width: 100%; }
.stats-heat {
    display: grid; grid-template-columns: repeat(10, minmax(0, 24px)); gap: 4px;
    justify-content: center;
    margin-bottom: 1.15rem;
}
.stats-day {
    aspect-ratio: 1 / 1; border-radius: 2px;
    background: rgba(34,197,94,0.08);
}
.stats-day.l1 { background: rgba(34,197,94,0.30); }
.stats-day.l2 { background: rgba(34,197,94,0.55); }
.stats-day.l3 { background: rgba(34,197,94,0.78); }
.stats-day.l4 { background: var(--success); }
.stats-heat-legend {
    display: flex; align-items: center; justify-content: center;
    gap: 0.45rem; font-size: 0.68rem; color: var(--text-muted);
}
.stats-heat-legend .scale { display: flex; gap: 3px; }
.stats-heat-legend .stats-day { width: 10px; height: 10px; aspect-ratio: 1 / 1; }

/* === Columns (two-col) === */
.stats-two-col {
    display: grid; grid-template-columns: 1fr; gap: 1rem; align-items: stretch;
}
.stats-col-card {
    padding: 1rem 1.1rem; border-radius: 12px;
    background: var(--surface-hover); border: 1px solid var(--border);
    display: flex; flex-direction: column;
}
.stats-col-card .stats-block { padding: 0; margin: 0; border: 0; }
.stats-col-card .stats-block-label { margin-bottom: 1rem; }
.stats-col-chart {
    flex: 1;
    display: flex; align-items: center; justify-content: center;
    min-height: 130px;
}

/* === Donut (by level) === */
.stats-donut-block { display: flex; align-items: center; gap: 1rem; width: 100%; }
.stats-donut { position: relative; width: 110px; height: 110px; flex-shrink: 0; }
.stats-donut svg { transform: rotate(-90deg); }
.stats-donut .center {
    position: absolute; inset: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.stats-donut .center b { font-size: 1.4rem; font-weight: 700; color: var(--text); line-height: 1; }
.stats-donut .center span { font-size: 0.62rem; color: var(--text-muted); margin-top: 0.2rem; }

.stats-legend { display: flex; flex-direction: column; gap: 0.4rem; flex: 1; }
.stats-lg { display: flex; align-items: center; gap: 0.45rem; font-size: 0.75rem; }
.stats-lg .dot { width: 9px; height: 9px; border-radius: 2px; flex-shrink: 0; }
.stats-lg .n { color: var(--text-muted); flex: 1; }
.stats-lg .c { color: var(--text); font-weight: 600; }

/* === Sparkline (last 7 days) === */
.stats-spark-block { display: flex; flex-direction: column; width: 100%; }
.stats-spark-top {
    display: flex; align-items: baseline; justify-content: space-between;
    margin-bottom: 0.85rem;
}
.stats-spark-num { font-size: 1.4rem; font-weight: 700; color: var(--text); line-height: 1; }
.stats-spark-num b { color: var(--success); }
.stats-spark-num .lbl {
    color: var(--text-muted); font-size: 0.78rem; font-weight: 500; margin-left: 0.3rem;
}
.stats-spark-meta { font-size: 0.7rem; color: var(--text-muted); }

.stats-bars-7d { display: flex; align-items: flex-end; gap: 6px; height: 60px; }
.stats-bars-7d .bar {
    flex: 1; background: rgba(124,92,252,0.15); border-radius: 3px 3px 0 0;
    min-height: 4px;
}
.stats-bars-7d .bar.has { background: var(--success); }
.stats-bars-7d-labels { display: flex; gap: 6px; margin-top: 5px; }
.stats-bars-7d-labels .lbl {
    flex: 1; font-size: 0.62rem; color: var(--text-muted); text-align: center;
}

/* === Achievements (monochrome SVG icons, green number) === */
.stats-bests { display: flex; flex-direction: column; width: 100%; }
.stats-best-row {
    display: flex; align-items: center; gap: 0.7rem;
    padding: 0.6rem 0;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}
.stats-best-row:first-child { padding-top: 0; }
.stats-best-row:last-child { border-bottom: 0; padding-bottom: 0; }
.stats-best-ico {
    width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
    background: rgba(255,255,255,0.06); color: rgba(255,255,255,0.7);
}
.stats-best-ico svg { display: block; }
.stats-best-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.stats-best-text .label {
    color: var(--text-muted); font-size: 0.74rem; font-weight: 500; line-height: 1;
}
.stats-best-text .value { font-size: 0.92rem; line-height: 1; }
.stats-best-text .value .n { color: var(--success); font-weight: 700; }
.stats-best-text .value .u { color: var(--text-muted); font-weight: 500; font-size: 0.78rem; }
.stats-best-meta {
    margin-left: auto; color: var(--text-muted); font-size: 0.72rem;
    font-weight: 600; text-align: right; font-variant-numeric: tabular-nums;
}

/* === Recently solved === */
.stats-recent-grid {
    display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem;
}
.stats-recent-card {
    padding: 0.7rem;
    background: var(--surface-hover); border: 1px solid var(--border);
    border-radius: 12px;
    display: flex; flex-direction: column; gap: 0.6rem;
    min-width: 0;
}
.stats-recent-diagram {
    width: 100%; aspect-ratio: 1 / 1;
    border-radius: 6px; object-fit: cover;
    background: rgba(255,255,255,0.04); display: block;
}
.stats-recent-info { display: flex; flex-direction: column; gap: 0.3rem; min-width: 0; }
.stats-recent-info .top {
    display: flex; align-items: baseline; justify-content: space-between; gap: 0.35rem;
}
.stats-recent-info .id { font-weight: 700; color: var(--text); font-size: 0.85rem; }
.stats-recent-info .time {
    font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text); font-size: 0.85rem;
}
.stats-recent-info .bottom {
    display: flex; align-items: center; justify-content: space-between; gap: 0.35rem; font-size: 0.68rem;
}
.stats-recent-info .when { color: var(--text-muted); }
.stats-recent-info .errors { display: inline-flex; align-items: center; gap: 0.25rem; font-weight: 600; }
.stats-recent-info .errors.perfect { color: var(--success); }
.stats-recent-info .errors.has { color: #f87171; }
.stats-recent-info .errors b { font-weight: 700; }

.stats-empty {
    padding: 1rem; text-align: center;
    color: var(--text-muted); font-size: 0.85rem;
    background: var(--surface-hover); border: 1px solid var(--border);
    border-radius: 12px;
}

/* === Responsive: multi-column when the modal is wide === */
@container (min-width: 600px) {
    .stats-kpi-grid { grid-template-columns: repeat(4, 1fr); }
    .stats-two-col { grid-template-columns: 1fr 1fr; }
    .stats-recent-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 640px) {
    .stats-modal { padding: 1.25rem; }
}
