/**
 * Stats page — mobile / tablet responsive layer.
 * Scoped to body.page-stats (set in templates/stats.html).
 */

/* Align main + sidebar row with .global-header-inner (max 1350px) and the same 12px
   horizontal gutters as .global-header. Also covers Player Intelligence (page-players page-stats). */
body.page-stats .container,
body.page-players .container {
    width: min(1350px, calc(100vw - 24px)) !important;
    max-width: 1350px !important;
    margin-left: auto !important;
    margin-right: auto !important;
    box-sizing: border-box !important;
}

/* Below 1134px the desktop sidebar is replaced by SectionPicker (selection bar + sheet).
   Keep container stacking for the main column. */
@media (max-width: 1133px) {
    body.page-stats .container {
        flex-direction: column !important;
        align-items: stretch !important;
    }
}

/* Scroll target: clear fixed/sticky header when jumping to stats content */
#stats-main-content {
    scroll-margin-top: clamp(3.5rem, 12vw, 5.5rem);
}

/* ── Filters: stack on narrow viewports, touch-friendly selects ───── */
@media (max-width: 767px) {
    body.page-stats .stats-filter-bar {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 10px 12px;
    }

    body.page-stats .stats-filter-controls {
        flex-direction: column;
        width: 100%;
        align-items: stretch;
        gap: 10px;
    }

    body.page-stats .stats-filter-select-wrapper {
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        width: 100%;
        min-width: 0;
    }

    body.page-stats .stats-filter-select-label {
        font-size: 11px;
    }

    /* 16px+ on inputs reduces iOS zoom-on-focus */
    body.page-stats .stats-filter-select {
        width: 100%;
        min-height: 44px;
        padding: 10px 12px;
        font-size: 16px;
        box-sizing: border-box;
    }
}

@media (min-width: 768px) and (max-width: 1023px) {
    body.page-stats .stats-filter-controls {
        display: flex;
        flex-wrap: wrap;
        gap: 12px;
    }

    body.page-stats .stats-filter-select-wrapper {
        flex: 1 1 calc(50% - 8px);
        min-width: min(100%, 220px);
    }
}

/* ── Hub: single column through tablet (not a shrunk 2-col desktop grid) ─ */
@media (max-width: 1023px) {
    body.page-stats .stats-hub-body {
        grid-template-columns: minmax(0, 1fr);
        gap: 10px;
    }

    body.page-stats .stats-hub-column {
        padding: 10px 12px;
    }

    body.page-stats .stats-hub-tabs {
        display: flex;
        flex-wrap: wrap;
        max-width: 100%;
        gap: 6px;
    }

    body.page-stats .stats-hub-tab {
        min-height: 36px;
        padding: 6px 12px;
        font-size: 11px;
    }

    body.page-stats .stats-hub-column-tab {
        min-height: 36px;
        padding: 6px 10px;
    }

    body.page-stats .stats-hub-row-main {
        grid-template-columns: 22px minmax(0, 1fr) auto minmax(54px, auto);
        gap: 7px;
    }

    body.page-stats .stats-hub-row {
        padding: 8px;
    }

    body.page-stats .stats-hub-inline-meta {
        gap: 5px;
        font-size: 10px;
    }

    body.page-stats .stats-hub-pct-pill {
        font-size: 16px;
    }
}

/* ── Main copy ───────────────────────────────────────────────────── */
@media (max-width: 767px) {
    body.page-stats .stats-main-title {
        font-size: clamp(0.95rem, 4.2vw, 1.05rem);
        line-height: 1.25;
    }

    body.page-stats .stats-main-subtitle {
        font-size: 12px;
        line-height: 1.45;
    }

    body.page-stats .stats-hub-row {
        padding: 7px 6px;
    }

    body.page-stats .stats-hub-row-main {
        grid-template-columns: 18px minmax(0, 1fr) auto minmax(48px, auto);
        gap: 5px;
    }

    body.page-stats .stats-hub-rank {
        font-size: 10px;
    }

    body.page-stats .stats-hub-club {
        gap: 5px;
    }

    body.page-stats .stats-hub-club .club-logo {
        width: 18px;
        height: 18px;
    }

    body.page-stats .stats-hub-club-text {
        font-size: 11px;
    }

    body.page-stats .stats-hub-inline-meta {
        gap: 3px;
        font-size: 9px;
    }

    body.page-stats .stats-hub-gp {
        display: none;
    }

    body.page-stats .stats-hub-pct-pill {
        min-width: 48px;
        font-size: 15px;
    }

    body.page-stats .stats-selected-title {
        font-size: 15px;
        margin-top: 4px;
    }

    body.page-stats .stats-selected-description {
        font-size: 12px;
        line-height: 1.45;
    }
}

/* ── Sidebar tree: touch-friendly rows on narrow viewports ──────── */
@media (max-width: 1023px) {
    body.page-stats .stats-sidebar-category-toggle {
        min-height: 44px;
        padding-top: 10px;
        padding-bottom: 10px;
    }

    body.page-stats .stats-sidebar-group-toggle,
    body.page-stats .stats-sidebar-subgroup-toggle {
        min-height: 40px;
        padding-top: 8px;
        padding-bottom: 8px;
    }

    body.page-stats .stats-sidebar-item {
        min-height: 40px;
        padding-top: 9px;
        padding-bottom: 9px;
        font-size: 12px;
    }

    body.page-stats .stats-sidebar-category-body,
    body.page-stats .stats-sidebar-group-body,
    body.page-stats .stats-sidebar-subgroup-body,
    body.page-stats .stats-sidebar-items {
        margin-left: 10px;
    }
}

/* ── Result stats table: condensed single-row layout (mobile + tablet) ─ */
@media (max-width: 1023px) {
    body.page-stats .enhanced-table.mobile-stack {
        display: block;
        overflow-x: visible;
    }

    body.page-stats .enhanced-table.mobile-stack .betting-stats-table {
        display: table;
        width: 100%;
        table-layout: fixed;
        border-collapse: collapse;
        border: 1px solid var(--match-border);
        border-radius: 8px;
        overflow: hidden;
        background: var(--match-card-bg);
        font-size: 12px;
    }

    body.page-stats .enhanced-table.mobile-stack thead {
        display: table-header-group;
    }

    body.page-stats .enhanced-table.mobile-stack tbody {
        display: table-row-group;
    }

    body.page-stats .enhanced-table.mobile-stack tbody tr {
        display: table-row;
        margin-bottom: 0;
        padding: 0;
        border: none;
        border-radius: 0;
        overflow: visible;
    }

    body.page-stats .enhanced-table.mobile-stack tbody td {
        display: table-cell;
        vertical-align: middle;
        box-sizing: border-box;
        padding: 7px 8px;
        border-bottom: 1px solid var(--match-border);
        text-align: right;
        white-space: nowrap;
        font-size: 12px;
    }

    body.page-stats .enhanced-table.mobile-stack tbody td::before {
        content: none;
        display: none;
    }

    /* Keep the scan-critical rank, club, ratio and percentage on one row.
       Value and GP remain available on desktop. */
    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(3),
    body.page-stats .enhanced-table.mobile-stack tbody td:nth-child(3),
    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(6),
    body.page-stats .enhanced-table.mobile-stack tbody td:nth-child(6) {
        display: none;
    }

    body.page-stats .enhanced-table.mobile-stack thead th {
        padding: 4px 6px;
        font-size: 7px;
        font-weight: 500;
        letter-spacing: 0.05em;
        color: var(--text-muted);
        text-align: right;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        vertical-align: middle;
        line-height: 1.2;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:first-child {
        width: 30px;
        text-align: center;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(2) {
        width: auto;
        text-align: left;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(4) {
        width: 48px;
        text-align: right;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(5) {
        width: 60px;
        text-align: right;
    }

    body.page-stats .enhanced-table.mobile-stack td.stats-rank-cell {
        width: 30px;
        padding-left: 5px;
        padding-right: 3px;
        text-align: center;
        font-size: 10px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 8px;
        text-align: left;
        min-width: 0;
        width: auto;
        padding-right: 6px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell .club-logo {
        width: 20px;
        height: 20px;
        flex-shrink: 0;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell .club-text-main {
        flex: 1 1 auto;
        min-width: 0;
        font-size: 12px;
        font-weight: 500;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: normal;
    }

    body.page-stats .enhanced-table.mobile-stack td.value-cell,
    body.page-stats .enhanced-table.mobile-stack td.pct-cell,
    body.page-stats .enhanced-table.mobile-stack td.gp-cell {
        text-align: right;
    }

    body.page-stats .enhanced-table.mobile-stack .value-pill {
        display: inline;
        padding: 0;
        border-radius: 0;
        background: transparent;
        color: var(--text-primary);
        font-size: 12px;
        font-weight: 500;
        font-variant-numeric: tabular-nums;
    }

    body.page-stats .enhanced-table.mobile-stack .value-pill-icon {
        display: none;
    }

    body.page-stats .enhanced-table.mobile-stack .stats-detail-pct {
        display: inline-flex;
        align-items: flex-end;
        justify-content: flex-end;
        gap: 0;
        min-width: 0;
    }

    body.page-stats .enhanced-table.mobile-stack .stats-detail-pct-label {
        font-size: 16px;
        font-weight: 800;
        line-height: 1;
    }

    body.page-stats .enhanced-table.mobile-stack td.gp-cell {
        font-size: 11px;
        color: var(--text-secondary);
        font-weight: 500;
    }

    body.page-stats .enhanced-table.mobile-stack tbody td.stats-table-full-row,
    body.page-stats .enhanced-table.mobile-stack tbody td[colspan] {
        display: table-cell;
        text-align: center;
        white-space: normal;
    }
}

/* Mobile: tighter gutters and column widths */
@media (max-width: 767px) {
    body.page-stats .enhanced-table.mobile-stack .betting-stats-table {
        font-size: 11px;
    }

    body.page-stats .enhanced-table.mobile-stack thead th {
        padding: 3px 5px;
        font-size: 6px;
        font-weight: 500;
        letter-spacing: 0.04em;
        line-height: 1.2;
        color: var(--text-muted);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        vertical-align: middle;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:first-child {
        width: 26px;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(4) {
        width: 42px;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(5) {
        width: 54px;
    }

    body.page-stats .enhanced-table.mobile-stack tbody td {
        padding: 6px 6px;
        font-size: 11px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell {
        gap: 6px;
        padding-left: 4px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell .club-logo {
        width: 18px;
        height: 18px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell .club-text-main {
        font-size: 11px;
    }

    body.page-stats .enhanced-table.mobile-stack .value-pill {
        font-size: 11px;
    }

    body.page-stats .enhanced-table.mobile-stack .stats-detail-pct-label {
        font-size: 15px;
        font-weight: 800;
    }

    body.page-stats .enhanced-table.mobile-stack td.gp-cell {
        font-size: 10px;
        padding-right: 8px;
    }
}

/* Tablet: same structure, slightly more room */
@media (min-width: 768px) and (max-width: 1023px) {
    body.page-stats .enhanced-table.mobile-stack .betting-stats-table {
        font-size: 12px;
    }

    body.page-stats .enhanced-table.mobile-stack thead th {
        padding: 4px 10px;
        font-size: 7px;
        font-weight: 500;
        letter-spacing: 0.05em;
        color: var(--text-muted);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        vertical-align: middle;
        line-height: 1.2;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:first-child {
        width: 34px;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(4) {
        width: 56px;
    }

    body.page-stats .enhanced-table.mobile-stack thead th:nth-child(5) {
        width: 68px;
    }

    body.page-stats .enhanced-table.mobile-stack tbody td {
        padding: 9px 12px;
        font-size: 12px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell {
        gap: 10px;
        padding-left: 12px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell .club-logo {
        width: 22px;
        height: 22px;
    }

    body.page-stats .enhanced-table.mobile-stack td.club-cell .club-text-main {
        font-size: 13px;
    }

    body.page-stats .enhanced-table.mobile-stack .value-pill {
        font-size: 12px;
    }

    body.page-stats .enhanced-table.mobile-stack .stats-detail-pct-label {
        font-size: 16px;
        font-weight: 800;
    }

    body.page-stats .enhanced-table.mobile-stack td.gp-cell {
        font-size: 11px;
        padding-right: 12px;
    }
}

/* Desktop / large tablet: keep table, optional horizontal safety scroll */
@media (min-width: 1024px) {
    body.page-stats .enhanced-table {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    body.page-stats .betting-stats-table {
        min-width: 520px;
    }
}

/* Sticky thead on condensed mobile/tablet table */
@media (max-width: 1023px) {
    body.page-stats .enhanced-table.sticky-header thead {
        position: sticky;
        top: 0;
        z-index: 6;
    }

    body.page-stats .enhanced-table.sticky-header thead th {
        background: var(--match-card-bg);
        box-shadow: 0 1px 0 var(--match-border);
    }
}

@media (min-width: 1024px) {
    body.page-stats .enhanced-table.sticky-header thead {
        position: sticky;
        top: 0;
        z-index: 6;
    }

    body.page-stats .enhanced-table.sticky-header thead th {
        background: var(--match-card-bg);
        box-shadow: 0 1px 0 var(--match-border);
    }
}

/* ── Stats detail table pager: touch targets + light theme (prev/next only) ─ */
@media (max-width: 1023px) {
    body.page-stats #stats-pagination .comp-pager-btn {
        width: 40px;
        height: 40px;
        font-size: 20px;
    }
}

[data-theme="light"] body.page-stats #stats-pagination .comp-pager-btn {
    border-color: rgba(0, 0, 0, 0.14);
    color: var(--text-primary, #1a202c);
    background: rgba(0, 0, 0, 0.04);
}

[data-theme="light"] body.page-stats #stats-pagination .comp-pager-btn:hover:not(:disabled) {
    background: rgba(0, 0, 0, 0.08);
    color: var(--text-primary, #1a202c);
}

/* ── Main stats card: tighter gutters on phones ─────────────────── */
@media (max-width: 767px) {
    body.page-stats .content > .card {
        padding: 12px 12px 16px;
        margin-bottom: 12px;
    }
}
