body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; color: #233442; }
.container { max-width: 900px; margin: auto; background: white; padding: 20px; border-radius: 8px; border: 1px solid #dce4ed; box-shadow: 0 2px 8px rgba(35,52,66,0.04); }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { padding: 10px; border: 1px solid #dce4ed; text-align: left; }
th { background-color: #eff3f8; color: #637585; }
.container > h2, .directory-header h3, .chart-header h3, .biweekly-recap h3 { color: #2456a6; }
canvas { max-height: 300px; margin-top: 20px; }
.chart-container { width: 100%; min-width: 0; }
.chart-canvas-container { position: relative; width: 100%; height: 300px; min-width: 0; margin-top: 20px; }
#eloChart { display: block; width: 100% !important; height: 100% !important; max-height: none !important; margin-top: 0; }

/* --- Main Layout --- */
.dashboard-layout { display: flex; gap: 20px; align-items: flex-start; }
.panel-left { flex: 0 0 350px; position: sticky; top: 0; }
.panel-right { flex: 1; display: flex; flex-direction: column; gap: 20px; min-width: 0; }

/* --- Directory --- */
.directory-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.directory-header h3 { margin: 0; }
.directory-player { display: inline-flex; align-items: center; gap: 8px; padding: 0; border: 0; background: transparent; font: inherit; text-align: left; }
.directory-player::before { content: ''; width: 12px; height: 12px; flex: 0 0 12px; border: 1px solid #b8c3d1; border-radius: 4px; }
.directory-row.is-selected { background: #edf3ff; }
.directory-row.is-selected .directory-player { font-weight: 600; }
.directory-row.is-selected .directory-player::before { content: '✓'; border-color: var(--player-color); background: var(--player-color); color: #fff; text-align: center; font-size: 10px; line-height: 12px; }
.directory-row.is-selected + .directory-row:not(.is-selected) td { border-top: 2px solid #b8c3d1; }
.directory-player:focus-visible { outline: 2px solid #3971c9; outline-offset: 3px; border-radius: 2px; }
.search-input { padding: 6px; width: 50%; border: 1px solid #bdcad8; border-radius: 4px; }
.toolbar-container {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    flex-wrap: nowrap;
    gap: 12px;
    width: 100%;
    min-width: 0;
}
.saved-views-row {
    display: flex;
    align-items: center;
    grid-column: 1 / -1;
    gap: 4px;
    min-width: 0;
    padding-top: 8px;
    border-top: 1px solid #edf0f4;
}
.saved-views-scroll {
    flex: 0 0 28px;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 1px solid #dce3ec;
    border-radius: 8px;
    background: #f8fafc;
    color: #40516a;
    font-size: 22px;
    cursor: pointer;
}
.saved-views-scroll[hidden] { display: none; }
.saved-views-scroll:disabled { opacity: .35; cursor: default; }
.saved-views-scroll:hover:not(:disabled) { background: #eaf1ff; }
.saved-views-scroll:focus-visible { outline: 2px solid #3971c9; outline-offset: 2px; }
.toolbar-container > .saved-view-save {
    justify-self: start;
}
.toolbar-right {
    display: flex;
    align-items: center;
    flex: 0 0 auto;
    gap: 6px;
}
.toolbar-right > *, .toolbar-right .filter-select {
    flex-shrink: 0;
}
.saved-views-label {
    flex: 0 0 auto;
    color: #6c757d;
    font-size: 11px;
    font-weight: 600;
}
.saved-views-list {
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
    gap: 8px;
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    scrollbar-width: none;
    padding: 3px;
}
.saved-views-list::-webkit-scrollbar {
    display: none;
}
.saved-views-empty {
    flex: 0 0 auto;
    color: #6c757d;
    font-size: 12px;
}
.saved-view-item {
    display: inline-flex;
    align-items: center;
    flex: 0 0 auto;
    overflow: hidden;
    border: 1px solid #dee2e6;
    border-radius: 14px;
    background: #f1f3f5;
}
.saved-view-load,
.saved-view-delete,
.saved-view-save {
    border: 0;
    cursor: pointer;
}
.saved-view-load {
    max-width: 140px;
    overflow: hidden;
    padding: 4px 8px;
    background: transparent;
    color: #495057;
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.saved-view-load:hover,
.saved-view-load.is-active {
    background: #e7f1ff;
    color: #084298;
}
.saved-view-item.is-active {
    border-color: #b6d4fe;
}
.saved-view-delete {
    align-self: stretch;
    padding: 0 7px;
    border-left: 1px solid #dee2e6;
    background: transparent;
    color: #6c757d;
    font-size: 13px;
}
.saved-view-delete:hover {
    background: #e9ecef;
    color: #343a40;
}
.saved-view-save {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    gap: 6px;
    min-width: 32px;
    height: 32px;
    padding: 0 8px;
    border: 1px solid #ced4da;
    border-radius: 7px;
    background: #fff;
    color: #495057;
    font-size: 12px;
    line-height: 1;
    white-space: nowrap;
}
.saved-view-save:focus-visible, .saved-views-list:focus-visible {
    outline: 2px solid #3971c9;
    outline-offset: 2px;
}
@media (max-width: 600px) {
    .toolbar-container { gap: 8px; }
    .toolbar-container .recap-period { display: none; }
}
.saved-view-save:hover:not(:disabled) {
    background: #f1f3f5;
}
.saved-view-save:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}
.directory-table-container { max-height: 80vh; overflow-y: auto; border: 1px solid #dce4ed; }
.table-header-sticky { position: sticky; top: 0; background: white; z-index: 1; }
.btn-clear {
    flex: 0 0 auto;
    padding: 2px 4px;
    border: 0;
    background: transparent;
    color: #6c757d;
    font-size: 11px;
    cursor: pointer;
    white-space: nowrap;
}
.btn-clear:hover, .btn-clear:focus-visible { color: #495057; text-decoration: underline; }

/* --- Empty State --- */
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 400px; color: #6c757d; border: 2px dashed #dee2e6; border-radius: 8px; background-color: #f8f9fa; padding: 20px; text-align: center; }
.empty-icon { font-size: 48px; margin-bottom: 15px; }
.empty-title { margin: 0 0 10px 0; color: #495057; }
.empty-text { margin: 0; font-size: 15px; }

/* --- Toolbar and Controls --- */
.chart-frame { position: relative; padding-right: 36px; }
.chart-icon-button { position: absolute; top: 0; right: 0; display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; padding: 0; border: 1px solid transparent; border-radius: 8px; background: transparent; color: #68768a; cursor: pointer; }
.chart-icon-button:hover { background: #eef2f7; color: #263449; }
.chart-period { display: flex; align-items: center; color: #68768a; font-size: 12px; }
.chart-period .filter-select { min-height: 32px; padding: 4px 8px; border-color: #dce3ec; border-radius: 7px; background-color: #fff; color: #263449; font: inherit; font-size: 12px; cursor: pointer; }
.recap-toggle { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 32px; padding: 4px 8px; border: 1px solid #dce3ec; border-radius: 7px; background: #fff; color: #40516a; font: inherit; font-size: 12px; font-weight: 500; cursor: pointer; }
.recap-toggle:hover:not(:disabled) { background: #edf3ff; border-color: #b8cdef; color: #2456a6; }
.recap-toggle[aria-expanded="true"] { background: #eaf1ff; border-color: #b8cdef; color: #2456a6; }
.recap-period { padding: 2px 6px; border-radius: 5px; background: #edf1f6; color: #68768a; font-size: 11px; font-weight: 400; white-space: nowrap; }
.recap-toggle[aria-expanded="true"] .recap-chevron { transform: rotate(180deg); }
.recap-control { position: relative; flex-shrink: 0; }
.recap-options { position: absolute; top: calc(100% + 8px); right: 0; z-index: 10; display: flex; flex-direction: column; gap: 10px; width: 190px; padding: 12px; border: 1px solid #dce3ec; border-radius: 10px; background: #fff; box-shadow: 0 8px 24px rgba(38, 52, 73, .14); }
.recap-options label { color: #68768a; font-size: 12px; font-weight: 600; }
.recap-options .filter-select, .recap-options button { min-height: 34px; border-radius: 7px; }
.chart-icon-button:focus-visible, .recap-toggle:focus-visible, .chart-period .filter-select:focus-visible { outline: 2px solid #3971c9; outline-offset: 3px; }
.chips-container { display: flex; align-items: center; gap: 4px 8px; flex-wrap: wrap; flex: 1; }
.controls-group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.filter-select { padding: 4px; border: 1px solid #ccc; border-radius: 4px; font-size: 13px; }
.btn-outline { cursor: pointer; padding: 4px 10px; font-size: 13px; border: 1px solid #b8cdef; border-radius: 4px; background: #fff; color: #2456a6; }
.btn-outline:hover:not(:disabled) { background: #edf3ff; border-color: #2456a6; }
.search-input:focus-visible, .filter-select:focus-visible, .btn-outline:focus-visible { outline: 2px solid #3971c9; outline-offset: 2px; }

/* --- Pills (Chips) --- */
.player-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 1px 2px;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: #495057;
    font-size: 12px;
    font-weight: 400;
    white-space: nowrap;
}
.player-chip::before {
    width: 6px;
    height: 6px;
    flex: 0 0 6px;
    border-radius: 50%;
    background: var(--theme-color);
    content: "";
}
.chip-name { font-weight: 500; }
.chip-elo { color: #6c757d; font-size: 11px; font-weight: normal; opacity: 0.75; }
.chip-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 14px;
    width: 14px;
    height: 14px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #868e96;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    opacity: 0.7;
    transition: background-color 120ms ease, opacity 120ms ease;
}
.chip-close:hover { background: #e9ecef; color: #495057; opacity: 1; }
.chip-close:focus-visible { outline: 2px solid var(--theme-color); outline-offset: 1px; }

/* --- Clickable Links --- */
.clickable-player { cursor: pointer; color: inherit; text-decoration: none; }
.clickable-player.link-style { color: #2456a6; font-weight: 500; text-decoration: underline; }
.clickable-player:hover { text-decoration: underline; text-underline-offset: 2px; }

/* --- Statistics and Tables --- */
.stats-container { background: #f8f9fa; padding: 12px; border-radius: 8px; margin-bottom: 15px; font-size: 14px; border: 1px solid #dee2e6; gap: 15px; flex-wrap: wrap; }
.stat-box { flex: 1; min-width: 140px; }
.h2h-stats { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.h2h-header { text-align: center; border-bottom: 1px solid #dee2e6; padding-bottom: 8px; }
.h2h-scoreboard { display: flex; justify-content: space-around; align-items: center; text-align: center; padding: 10px 0; }
.h2h-player { flex: 1; min-width: 0; }
.h2h-player-name { font-size: 1.1em; margin-bottom: 5px; }
.h2h-score { font-size: 1.8em; font-weight: bold; margin-bottom: 5px; }
.h2h-vs { font-size: 1.2em; font-weight: bold; color: #adb5bd; padding: 0 12px; }
.text-bold { font-weight: bold; }
.text-muted { color: #6c757d; font-size: 13px; }
.history-cell { padding: 8px; border-bottom: 1px solid #ddd; }
.history-title { margin-top: 0; }
.history-rating { color: #637585; font-size: 11px; white-space: nowrap; }

/* --- Dynamic Row and Text Colors --- */
.row-win { background-color: #e6f4ea; }
.row-loss { background-color: #fce8e6; }
.row-draw { background-color: #f8f9fa; }
.text-win { color: #155724; font-weight: bold; }
.text-loss { color: #721c24; font-weight: normal; }

/* --- Modal --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    box-sizing: border-box;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    padding: 16px;
    background-color: rgba(0,0,0,0.65);
    align-items: center;
    justify-content: center;
    overflow: hidden;
    overscroll-behavior: none;
}
.modal-content {
    position: relative;
    box-sizing: border-box;
    width: min(1200px, 100%);
    height: 92%;
    max-height: 100%;
    min-height: 0;
    min-width: 0;
    padding: 20px;
    background: #fff;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,0.25);
}
.modal-header {
    display: flex;
    flex: 0 0 auto;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-right: 48px;
    gap: 15px;
    min-width: 0;
}
.modal-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    overflow: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}
#modalStatsContainer {
    box-sizing: border-box;
    width: 100%;
    flex: 0 0 auto;
    margin-bottom: 0;
}
.modal-chart-container {
    position: relative;
    width: 100%;
    min-width: 0;
    min-height: 280px;
    flex: 1 0 280px;
}
#modalEloChart {
    display: block;
    width: 100% !important;
    height: 100% !important;
    max-height: none !important;
    margin: 0;
    touch-action: pan-y !important;
}
.btn-close {
    position: absolute;
    top: 14px;
    right: 14px;
    z-index: 2;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 1px solid #dee2e6;
    border-radius: 50%;
    background: #f8f9fa;
    color: #495057;
    cursor: pointer;
    font-size: 22px;
    font-weight: 400;
    transition: background-color 120ms ease, color 120ms ease;
}
.btn-close:hover { background: #e9ecef; color: #212529; }
.btn-close:focus-visible { outline: 2px solid #6c757d; outline-offset: 2px; }

/* --- Mobile (Responsive) --- */
@media (max-width: 768px) {
    body { margin: 0; }
    .container { max-width: none; padding: 8px; border-radius: 0; }
    div:has(> .panel-left) { display: flex !important; flex-direction: column !important; padding: 0 !important; gap: 12px; }
    .panel-right { order: 1; width: 100% !important; min-width: 0 !important; box-sizing: border-box !important; margin-bottom: 12px; }
    .chart-container { width: 100%; min-width: 0; padding: 0; }
    .chart-canvas-container { height: clamp(280px, 52vh, 360px); min-height: 280px; margin-top: 12px; }
    #eloChart { width: 100% !important; height: 100% !important; min-height: 280px !important; }
    .panel-left { order: 2; width: 100% !important; min-width: 100% !important; box-sizing: border-box !important; max-height: 50vh; overflow-y: auto; }
    table { display: block; overflow-x: auto; white-space: nowrap; width: 100%; }
    .controls-group { width: 100%; justify-content: flex-start !important; margin-top: 10px; }
}

.streak-indicator {
    display: inline-block;
    padding: 2px 6px;
    margin-right: 4px;
    border-radius: 4px;
    font-size: 0.85em;
    font-weight: bold;
    color: white;
}
.streak-w { background-color: #28a745; }
.streak-l { background-color: #dc3545; }
.streak-d { background-color: #6c757d; }

/* ==========================================
   MOBILE OPTIMIZATIONS
   ========================================= */
@media (max-width: 768px) {
    /* Allow table containers to scroll horizontally when needed. */
    .table-container, .table-responsive, .card-body {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS. */
        width: 100%;
    }

    /* Let tables use the available width on mobile. */
    table {
        width: 100% !important;
        table-layout: auto;
    }

    /* Reduce cell padding and font size on mobile. */
    th, td {
        padding: 8px 4px !important;
        font-size: 0.85em; /* Slightly reduce font size to fit more data. */
        white-space: normal; /* Allow long names to wrap onto another line. */
        word-break: break-word;
    }
}

/* ==========================================
   MOBILE COMPONENTS
   ========================================= */
@media (max-width: 768px) {
    .chips-container {
        display: flex;
        flex-wrap: wrap; /* Allow chips to flow into multiple rows */
        gap: 6px;
        width: 100%;
    }

    .player-chip {
        gap: 4px;
        padding: 1px 2px;
        font-size: 12px;
        margin: 0;
    }

    .controls-group {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        width: 100%;
        margin-top: 0 !important;
    }

    .stats-container {
        flex-direction: column;
        gap: 12px;
        width: 100%;
        box-sizing: border-box;
    }

    .stat-box {
        width: 100%;
        min-width: 0;
        display: flex;
        justify-content: space-between;
    }

    .h2h-scoreboard {
        flex-direction: row;
        align-items: flex-start;
        gap: 8px;
        padding: 4px 0;
    }

    .h2h-stats {
        gap: 6px;
    }

    .stats-container:has(.h2h-stats) {
        padding: 8px 10px;
        margin-bottom: 8px;
    }

    .h2h-header {
        padding-bottom: 5px;
        font-size: 12px;
    }

    .h2h-player-name {
        font-size: 12px;
        line-height: 1.3;
        margin-bottom: 3px;
        overflow-wrap: anywhere;
    }

    .h2h-score {
        font-size: 22px;
        line-height: 1.2;
        margin-bottom: 4px;
    }

    .h2h-stats .streak-indicator {
        padding: 1px 4px;
        margin-right: 2px;
        font-size: 10px;
    }

    .h2h-vs {
        align-self: center;
        flex: 0 0 auto;
        padding: 0;
        font-size: 11px;
    }
}

/* Compact dialogs also cover phones wider than 768px in landscape. */
@media (max-width: 768px), (max-height: 500px) {
    .modal-overlay {
        padding: max(8px, env(safe-area-inset-top, 0px))
                 max(8px, env(safe-area-inset-right, 0px))
                 max(8px, env(safe-area-inset-bottom, 0px))
                 max(8px, env(safe-area-inset-left, 0px));
    }

    .modal-content {
        width: 100%;
        height: 100%;
        padding: 12px;
    }

    .modal-header {
        gap: 8px;
        margin-bottom: 8px;
        padding-right: 44px;
    }

    #modalChipsContainer {
        flex: 1 1 auto;
        width: auto;
        min-width: 0;
        max-height: 64px;
        overflow: auto;
    }

    .modal-header .controls-group {
        width: auto;
        flex: 0 0 auto;
        margin-top: 0;
    }

    .modal-content .btn-close {
        box-sizing: border-box;
        top: 10px;
        right: 10px;
        width: 36px;
        height: 36px;
    }
}

@media (max-width: 768px) and (orientation: portrait) {
    .modal-header {
        flex-direction: column;
        align-items: stretch;
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    .modal-header {
        flex-direction: row;
    }

    #modalChipsContainer {
        max-height: 40px;
    }

    .modal-header .controls-group {
        flex-wrap: nowrap;
        gap: 6px;
    }

    #modalStatsContainer {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 6px;
        padding: 8px;
    }

    #modalStatsContainer .stat-box {
        width: auto;
        min-width: 120px;
        flex: 1 1 120px;
    }

    #modalStatsContainer .h2h-scoreboard {
        flex-direction: row;
        gap: 6px;
        padding: 4px 0;
    }

    #modalStatsContainer .h2h-score {
        font-size: 1.35em;
    }

    .modal-chart-container {
        min-height: 180px;
        flex-basis: 180px;
    }
}

.biweekly-recap { margin-top: 20px; padding: 16px; background: #f4f7fc; border: 1px solid #dce4ef; border-radius: 8px; }
.biweekly-recap[hidden] { display: none; }
.biweekly-recap h3 { margin: 0 0 6px; }
.recap-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.biweekly-recap .recap-header h3 { margin: 0; }
.recap-download { display: inline-flex; align-items: center; justify-content: center; padding: 6px; }
.recap-download:disabled { cursor: wait; opacity: .65; }
.biweekly-recap > p { color: #596579; font-size: 12px; line-height: 1.5; }
.recap-awards { display: flex; flex-wrap: wrap; gap: 10px; }
.recap-award { flex: 1 1 140px; min-width: 0; padding: 12px; background: white; border: 1px solid #dce4ef; border-radius: 6px; overflow-wrap: anywhere; }
.recap-award h4 { margin: 0 0 10px; font-size: 14px; }
.recap-award strong { display: block; font-size: 13px; }
.recap-award p { margin: 8px 0 0; color: #596579; font-size: 12px; }
.biweekly-recap .recap-note { margin-bottom: 0; font-size: 11px; }
#recapStatus { font-size: 13px; color: #596579; }
#recapStatus:empty { display: none; }
#shareRecapBtn:disabled { cursor: wait; opacity: .65; }
