.highscore-page .country-flag {
    width: 16px;
    height: 12px;
}

.highscore-page {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* The view, scope, period and category choices look like the shared
   segmented control (components/segmented.css): separate tiles, the chosen
   one lifted by its fill and border. */
.view-toggle {
    display: flex;
    gap: .35rem;
}

.view-toggle-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 0 .8rem;
    font-size: .8rem;
    font-weight: 600;
    text-align: center;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    cursor: pointer;
    transition: background .15s, border-color .15s, color .15s;
    text-decoration: none;
}

.view-toggle-btn:hover {
    color: var(--text);
}

.view-toggle-btn.is-active {
    background: var(--surface-2);
    border-color: var(--border-chosen);
    color: var(--text);
}

.category-filter__scroller {
    display: flex;
    gap: .25rem;
    flex-wrap: wrap;
}

.leaderboard-filter-stack {
    display: flex;
    flex-direction: column;
    gap: .5rem;
}

.leaderboard-trainer-search {
    position: relative;
    width: min(100%, 30rem);
    margin-bottom: 1.5rem;
}

.leaderboard-trainer-search > .form-select-shell {
    width: 100%;
}

.leaderboard-trainer-search__clear {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 2;
    width: 2.5rem;
    height: 2.5rem;
}

.leaderboard-trainer-search__clear::before {
    --leaderboard-clear-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m6 6 12 12M18 6 6 18' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");

    content: '';
    position: absolute;
    inset: .75rem;
    background: var(--text-muted);
    -webkit-mask: var(--leaderboard-clear-icon) center / contain no-repeat;
    mask: var(--leaderboard-clear-icon) center / contain no-repeat;
}

.leaderboard-trainer-search__clear:hover::before,
.leaderboard-trainer-search__clear:focus-visible::before {
    background: var(--text);
}

.leaderboard-trainer-search__option {
    display: flex;
    align-items: center;
    gap: .5rem;
}

.leaderboard-scope-filter {
    display: flex;
    gap: .35rem;
    flex-wrap: wrap;
}

.leaderboard-scope-filter__link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: .35rem .75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: none;
    color: var(--text-muted);
    font-size: .8rem;
    font-weight: 600;
    line-height: 1.2;
    transition: background .15s, border-color .15s, color .15s;
}

.leaderboard-scope-filter__link:hover {
    color: var(--text);
}

.leaderboard-scope-filter__link.is-active {
    background: var(--surface-2);
    border-color: var(--border-chosen);
    color: var(--text);
}

.leaderboard-scope-filter__link--disabled,
.leaderboard-scope-filter__link--disabled:hover {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text-muted);
    cursor: not-allowed;
    opacity: .58;
}

.category-filter-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: .25rem .65rem;
    font-size: .75rem;
    font-weight: 600;
    line-height: 1.2;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    cursor: pointer;
    transition: background .15s, color .15s, border-color .15s;
}

.category-filter-btn:hover {
    color: var(--text);
}

.category-filter-btn.is-active {
    background: var(--surface-2);
    border-color: var(--border-chosen);
    color: var(--text);
}

/* The dex strip under the Pokédex category reads as tabs inside the chosen
   tile: plain text, the chosen one underlined, so it does not compete with
   the category tiles above. */
.category-filter--dex .category-filter-btn {
    position: relative;
    padding: .2rem .55rem;
    font-size: .7rem;
    font-weight: 500;
    border-color: transparent;
}

.category-filter--dex .category-filter-btn.is-active {
    background: none;
    border-color: transparent;
    color: var(--text);
}

.category-filter--dex .category-filter-btn.is-active::after {
    content: '';
    position: absolute;
    right: .55rem;
    bottom: 0;
    left: .55rem;
    height: 2px;
    border-radius: 1px;
    background: var(--text-muted);
}

.leaderboard-notices {
    display: flex;
    flex-direction: column;
    gap: .5rem;
}

/* Dismissed notices stay in the markup, hidden; drop the empty stack from the page gap. */
.leaderboard-notices:not(:has(> :not([hidden]))) {
    display: none;
}

.leaderboard-onboarding {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(340px, 1.25fr) auto;
    align-items: center;
    gap: 1rem;
    padding: 1.25rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.leaderboard-onboarding__copy {
    display: flex;
    flex-direction: column;
    gap: .35rem;
}

.leaderboard-onboarding__eyebrow {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    color: var(--accent);
    font-size: .72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
}

.leaderboard-onboarding h2 {
    margin: 0;
    color: var(--text);
    font-size: 1rem;
    font-weight: 700;
}

.leaderboard-onboarding p {
    margin: 0;
    color: var(--text-muted);
    font-size: .875rem;
    line-height: 1.5;
}

.leaderboard-onboarding__steps {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .75rem;
    margin: 0;
    padding: 0;
    list-style: none;
}

.leaderboard-onboarding__step {
    display: flex;
    gap: .7rem;
    min-width: 0;
    padding: .85rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.leaderboard-onboarding__step--active {
    border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}

.leaderboard-onboarding__step--done {
    border-color: color-mix(in srgb, #4caf7d 40%, var(--border));
}

.leaderboard-onboarding__step-icon {
    width: 1.75rem;
    height: 1.75rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    color: var(--accent);
    background: color-mix(in srgb, var(--accent) 12%, transparent);
    border-radius: 999px;
}

.leaderboard-onboarding__step--done .leaderboard-onboarding__step-icon {
    color: #4caf7d;
    background: color-mix(in srgb, #4caf7d 14%, transparent);
}

.leaderboard-onboarding__step strong {
    display: block;
    color: var(--text);
    font-size: .875rem;
}

.leaderboard-onboarding__step small {
    display: block;
    margin-top: .15rem;
    color: var(--text-muted);
    font-size: .78rem;
    line-height: 1.45;
}

.leaderboard-onboarding__actions {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: .5rem;
}

.leaderboard-onboarding__actions .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .45rem;
    white-space: nowrap;
}

.leaderboard-ranking {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    margin-top: 0;
}

.stats-period-alert + .leaderboard-ranking {
    margin-top: 1.5rem;
}

.leaderboard-all-metrics {
    display: flex;
    flex-direction: column;
    gap: 2.5rem;
}

.leaderboard-top-three {
    display: flex;
    flex-direction: column;
    gap: 0;
}

/* Keep section spacing separate from the country and scope controls. */
.leaderboard-top-three > .stats-period-alert,
.leaderboard-top-three > .empty-state,
.leaderboard-top-three > .leaderboard-all-metrics {
    margin-top: 1.5rem;
}

.metric-category-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.metric-category-title {
    font-size: .7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-muted);
    padding-bottom: .5rem;
    border-bottom: 1px solid var(--border);
}

.metric-cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1rem;
}

.metric-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.metric-card-header {
    padding: .55rem .85rem;
    font-size: .75rem;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    text-transform: uppercase;
    letter-spacing: .05em;
}

.metric-card-row {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .55rem .85rem;
}

.metric-card-row + .metric-card-row {
    border-top: 1px solid var(--border);
}

.metric-card-rank {
    font-size: .75rem;
    font-weight: 700;
    width: 1.5rem;
    text-align: center;
    flex-shrink: 0;
    white-space: nowrap;
    overflow: hidden;
}

.metric-card-name {
    display: flex;
    align-items: center;
    gap: .4rem;
    flex: 1;
    font-size: .9rem;
    min-width: 0;
}

.metric-card-name a {
    overflow-wrap: anywhere;
    min-width: 0;
}

.metric-card-value {
    font-size: .85rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-muted);
    flex-shrink: 0;
}

.metric-card-row--gold   { background: rgba(255, 215, 0, .03); }
.metric-card-row--silver { background: rgba(192, 192, 192, .03); }
.metric-card-row--bronze { background: rgba(205, 127, 50, .03); }

.metric-card-row--current-player {
    background: color-mix(in srgb, var(--accent) 10%, var(--surface)) !important;
    box-shadow: inset 3px 0 0 var(--accent);
}

.metric-card-row--gold   .metric-card-rank { color: var(--tier-gold-bright); }
.metric-card-row--silver .metric-card-rank { color: var(--tier-silver-text); }
.metric-card-row--bronze .metric-card-rank { color: var(--tier-bronze); }

.metric-card-row--gold   .metric-card-rank::after { content: ' ★'; }
.metric-card-row--silver .metric-card-rank::after { content: ' ★'; }
.metric-card-row--bronze .metric-card-rank::after { content: ' ★'; }

.pagination {
    padding-top: 1rem;
}

.pagination__status {
    color: var(--text-muted);
    font-size: .85rem;
    text-align: center;
}

.leaderboard-empty {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* On a phone the ranking has no gap between its filters and the table. */
.leaderboard-ranking > .leaderboard-period-bar {
    padding-block: .75rem;
}

.pagination__status--focused {
    margin: 1rem 0 0;
}


/* The country picker stays above the scope tabs. */
.leaderboard-location-filter {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .5rem;
    min-width: 0;
}

.leaderboard-country-filter {
    width: 100%;
    min-width: 0;
    margin: 0 0 .5rem;
}

.leaderboard-country-filter__row {
    display: flex;
    gap: .35rem;
}

.leaderboard-country-filter__row > .form-select-shell {
    flex: 1 1 auto;
    min-width: 0;
}

/* Beside the field rather than in it, where Tom Select takes every tap. */
.leaderboard-country-filter__clear {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 40px;
    min-height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    transition: color .15s;
}

.leaderboard-country-filter__clear:hover,
.leaderboard-country-filter__clear:focus-visible {
    color: var(--text);
}

/* The country field looks like an unchosen tile beside it: outlined, the
   same corners and type, a little taller as a field. With a country chosen,
   or while its list is open, it takes the chosen fill; the focus ring stays
   that of every select. */
.leaderboard-country-filter :is(select.form-select, .ts-wrapper.form-select .ts-control) {
    min-height: 40px;
    padding-left: 2.35rem !important;
    background-color: transparent !important;
    border-radius: var(--radius) !important;
    font-size: .8rem;
    font-weight: 600;
}

.leaderboard-country-filter .ts-wrapper.form-select.dropdown-active .ts-control,
.leaderboard-country-filter--chosen :is(select.form-select, .ts-wrapper.form-select .ts-control) {
    background-color: var(--surface-2) !important;
}

.leaderboard-country-filter--chosen :is(select.form-select:not(:focus), .ts-wrapper.form-select:not(.focus, .dropdown-active) .ts-control) {
    border-color: var(--border-chosen) !important;
}

/* The label in the field is no typing field (the search sits in the list),
   so it can be as small as the tiles without iOS zooming in on a tap. */
.leaderboard-country-filter .ts-wrapper .ts-control > input.items-placeholder {
    font-size: .8rem !important;
}

/* "Country" is a label here, not a hint, so it is not dimmed like one, and it
   stays while the list is open (Tom Select hides the placeholder then). */
.leaderboard-country-filter .items-placeholder::placeholder {
    color: var(--text-muted);
    opacity: 1;
    font-weight: 600;
}

.leaderboard-country-filter .plugin-dropdown_input.dropdown-active .items-placeholder {
    display: block !important;
}

.leaderboard-country-filter .plugin-dropdown_input.dropdown-active .items-placeholder::placeholder {
    color: var(--text);
}

.leaderboard-country-filter .form-select-shell:has(.dropdown-active)::before {
    background: var(--text);
}

/* A globe before the label, like the bars' plain text options. */
.leaderboard-country-filter .form-select-shell::before {
    --leaderboard-globe-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cg fill='none' stroke='black' stroke-width='2'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18'/%3E%3C/g%3E%3C/svg%3E");

    content: '';
    position: absolute;
    top: 50%;
    left: .8rem;
    z-index: 3;
    width: 1rem;
    height: 1rem;
    transform: translateY(-50%);
    background: var(--text-muted);
    -webkit-mask: var(--leaderboard-globe-icon) center / contain no-repeat;
    mask: var(--leaderboard-globe-icon) center / contain no-repeat;
    pointer-events: none;
}

.trainer-search-btn[aria-pressed="true"] {
    background: var(--surface-2);
    border-color: var(--border-chosen);
    color: var(--text);
}

@media (max-width: 600px) {
    .highscore-page .section-header {
        flex-direction: column;
        align-items: stretch;
        gap: .75rem;
    }

    .highscore-page .header-actions {
        margin-left: 0;
    }

    .view-toggle {
        width: 100%;
    }

    .view-toggle-btn {
        flex: 1;
        padding: .55rem .5rem;
    }

    .leaderboard-trainer-search {
        width: 100%;
        margin-bottom: 1rem;
    }

    .leaderboard-onboarding {
        grid-template-columns: 1fr;
        align-items: stretch;
    }

    .leaderboard-onboarding__steps {
        grid-template-columns: 1fr;
    }

    .leaderboard-onboarding__actions {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .leaderboard-onboarding__actions .btn {
        flex: 1 1 180px;
    }

    .metric-cards-grid {
        grid-template-columns: 1fr;
    }

    .leaderboard-ranking {
        gap: 0;
        margin-top: -0.5;
    }

    .stats-period-alert + .leaderboard-ranking {
        margin-top: .5rem;
    }

    /* Swipeable filter rows that pin above the table, so controls stay
       reachable while the cloned table header is showing. */
    .highscore-page .leaderboard-filter-stack {
        position: sticky;
        top: 0;
        z-index: 5; /* above the sticky rank/name cells, below the cloned header */
        gap: 0;
        /* Room above the table at rest; while pinned the cloned header meets
           the border box, so the margin does not show. The line under the
           pinned stack is the clone's (components/data-table.css), so at rest
           only the table's own border shows. */
        margin: 0 -1rem .5rem;
        background: var(--bg);
    }

    .highscore-page .leaderboard-scope-filter,
    .highscore-page .category-filter__scroller {
        flex-wrap: nowrap;
        gap: .4rem;
        overflow-x: auto;
        padding: .5rem 1rem;
        scrollbar-width: none;
        -ms-overflow-style: none;
        -webkit-mask-image: linear-gradient(to right, transparent 0, #000 .9rem, #000 calc(100% - .9rem), transparent 100%);
        mask-image: linear-gradient(to right, transparent 0, #000 .9rem, #000 calc(100% - .9rem), transparent 100%);
    }

    .highscore-page .leaderboard-scope-filter + .leaderboard-scope-filter--period {
        border-top: 1px solid var(--border);
    }

    .highscore-page .leaderboard-scope-filter::-webkit-scrollbar,
    .highscore-page .category-filter__scroller::-webkit-scrollbar {
        display: none;
    }

    .highscore-page .leaderboard-scope-filter__link,
    .highscore-page .category-filter-btn {
        flex: 0 0 auto;
        min-height: 2.25rem;
        padding: .45rem .85rem;
        font-size: .8rem;
        white-space: nowrap;
    }

    .highscore-page .category-filter--dex .category-filter__scroller {
        padding-top: 0;
    }

    .highscore-page .category-filter--dex .category-filter-btn {
        min-height: 2rem;
        padding: .35rem .7rem;
        font-size: .75rem;
    }

    .highscore-page .table-scroll {
        margin-left: -1rem;
        margin-right: -1rem;
        border-radius: 0;
        border-left: none;
        border-right: none;
    }

    .highscore-page .current-player-badge {
        display: none;
    }

}

@media (min-width: 601px) {
    .leaderboard-empty {
        gap: 1.5rem;
    }

    /* The ranking spaces its children with a gap from here on. */
    .leaderboard-ranking > .leaderboard-period-bar {
        padding-block: 0;
    }
}

@media (min-width: 601px) {
    .leaderboard-country-filter {
        width: min(100%, 16rem);
    }

    /* The view, scope, period and categories in one row each, as tiles of
       the same size as the shared segmented control. */
    .view-toggle,
    .leaderboard-scope-filter,
    .category-filter:not(.category-filter--dex) .category-filter__scroller {
        flex-wrap: nowrap;
        gap: .35rem;
    }

    .view-toggle-btn,
    .leaderboard-scope-filter__link,
    .category-filter:not(.category-filter--dex) .category-filter-btn {
        min-height: 36px;
        padding: 0 .85rem;
        font-size: .8rem;
        font-weight: 600;
        white-space: nowrap;
    }

    /* The categories fill the width; a narrow window scrolls them. */
    .category-filter:not(.category-filter--dex) .category-filter__scroller {
        overflow-x: auto;
        scrollbar-width: none;
    }

    .category-filter:not(.category-filter--dex) .category-filter-btn {
        flex: 1 0 auto;
    }

    /* The ranking's controls in rows: scope, country and period; the
       categories; then the trainer search and the Period row right above
       the table. `order` places them; the DOM keeps the phone's order. */
    .leaderboard-ranking {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        column-gap: .75rem;
        row-gap: 1rem;
    }

    .leaderboard-ranking > *,
    .leaderboard-ranking .leaderboard-filter-stack > * {
        order: 9;
        flex: 0 0 100%;
        min-width: 0;
    }

    .leaderboard-ranking > .leaderboard-filter-stack {
        display: contents;
    }

    .leaderboard-ranking > .leaderboard-country-filter {
        order: 1;
        flex: 0 1 14rem;
        margin: 0;
    }

    .leaderboard-ranking .leaderboard-location-filter {
        order: 1;
        flex: 0 1 auto;
    }

    .leaderboard-ranking .leaderboard-scope-filter--period {
        order: 3;
        flex: 0 0 auto;
        margin-left: auto;
    }

    .leaderboard-ranking .stats-period-alert {
        order: 4;
    }

    .leaderboard-ranking .category-filter {
        order: 5;
    }

    .leaderboard-ranking .category-filter--dex {
        order: 6;
    }

    .leaderboard-ranking > .leaderboard-trainer-search {
        order: 7;
        flex: 0 1 18rem;
        margin: 0;
    }

    .leaderboard-ranking > .leaderboard-period-bar {
        order: 8;
        flex: 1 0 100%;
    }
}
