/* ==================== CSS Variables (ygbuff dark theme) ==================== */
:root {
    --bg-dark: #111827;
    --bg-light: #1f2937;
    --card-bg: #1e293b;
    --text-primary: #f9fafb;
    --text-secondary: #d1d5db;
    --text-muted: #9ca3af;
    --accent-blue: #3b82f6;
    --accent-green: #10b981;
    --accent-purple: #9b59b6;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --border-color: rgba(255, 255, 255, 0.1);
    --hover-bg: rgba(59, 130, 246, 0.1);
    --text-highlight: #60a5fa;
}

/* ==================== Global Styles ==================== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    line-height: 1.6;
}

.app-container {
    max-width: 1600px;
    margin: 0 auto;
    padding: 2rem;
}

/* ==================== Header ==================== */
.header {
    text-align: center;
    margin-bottom: 2rem;
}

.header h1 {
    font-size: 2rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
}

.header h1 i {
    color: var(--accent-blue);
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1rem;
}

/* ==================== Filters Section ==================== */
.filters-section {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: 12px;
    margin-bottom: 2rem;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.filter-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1rem;
}

.filter-row:last-child {
    margin-bottom: 0;
}

.filter-group {
    display: flex;
    flex-direction: column;
}

.filter-action-group {
    justify-content: flex-end;
}

.filter-group label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.filter-group label i {
    margin-right: 0.25rem;
}

.filter-select,
.filter-input {
    background: var(--bg-dark);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.75rem;
    font-size: 1rem;
    transition: all 0.2s ease;
}

.filter-select:focus,
.filter-input:focus {
    outline: none;
    border-color: var(--accent-blue);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.filter-select[multiple] {
    min-height: 120px;
}

.filter-select option {
    padding: 0.5rem;
}

.refresh-btn {
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-purple));
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all 0.2s ease;
    width: 100%;
}

.refresh-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.refresh-btn:active {
    transform: translateY(0);
}

.refresh-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==================== Loading & Error States ==================== */
.loading {
    text-align: center;
    padding: 4rem;
    color: var(--text-secondary);
    font-size: 1.2rem;
}

.loading .spin {
    animation: spin 1s linear infinite;
    font-size: 2rem;
    display: block;
    margin-bottom: 1rem;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.error {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger-color);
    padding: 1rem;
    border-radius: 8px;
    border-left: 4px solid var(--danger-color);
    margin-bottom: 2rem;
}

.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

.empty-state i {
    font-size: 4rem;
    color: var(--text-muted);
    opacity: 0.5;
    margin-bottom: 1rem;
    display: block;
}

.empty-state h3 {
    font-size: 1.5rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.empty-state p {
    font-size: 1rem;
    color: var(--text-muted);
}

/* ==================== KPI Cards ==================== */
.kpi-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.kpi-card {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    text-align: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    border: 1px solid var(--border-color);
}

.kpi-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
    border-color: rgba(59, 130, 246, 0.3);
}

.kpi-icon {
    font-size: 2rem;
    color: var(--accent-blue);
    margin-bottom: 0.5rem;
}

.kpi-value {
    font-size: 2.5rem;
    font-weight: bold;
    color: var(--accent-blue);
    margin-bottom: 0.5rem;
}

.kpi-label {
    color: var(--text-secondary);
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ==================== Main Content ==================== */
.main-content {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.analysis-section {
    background: var(--card-bg);
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    border: 1px solid var(--border-color);
}

.section-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 1.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--border-color);
}

.section-title i {
    color: var(--accent-blue);
    margin-right: 0.5rem;
}

.subsection {
    margin-bottom: 2rem;
}

.subsection:last-child {
    margin-bottom: 0;
}

.subsection-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.subsection-title i {
    color: var(--accent-purple);
}

.mini-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.side-by-side-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
}

.four-column-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
}

.five-column-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 1rem;
}

/* ==================== Tournament Performance ==================== */
.tournament-performance {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.tournament-item {
    background: rgba(255, 255, 255, 0.03);
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: all 0.2s ease;
}

.tournament-item:hover {
    background: var(--hover-bg);
    border-color: rgba(59, 130, 246, 0.3);
}

.tournament-name {
    font-weight: 600;
    color: var(--text-primary);
    flex: 1;
}

.tournament-stats {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.badge {
    padding: 0.25rem 0.75rem;
    border-radius: 6px;
    font-size: 0.875rem;
    font-weight: 600;
}

.badge-winrate {
    background: rgba(16, 185, 129, 0.15);
    color: var(--success-color);
}

.badge-wins {
    background: rgba(59, 130, 246, 0.15);
    color: var(--accent-blue);
}

.badge-losses {
    background: rgba(239, 68, 68, 0.15);
    color: var(--danger-color);
}

/* ==================== Coach Insights Grid ==================== */
.coach-insights-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1rem;
}

.insight-card {
    background: rgba(255, 255, 255, 0.03);
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    transition: all 0.2s ease;
}

.insight-card:hover {
    background: var(--hover-bg);
    border-color: rgba(59, 130, 246, 0.3);
}

.insight-label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.insight-value {
    font-size: 1.5rem;
    font-weight: 700;
}

.insight-value.high {
    color: var(--success-color);
}

.insight-value.mid {
    color: var(--warning-color);
}

.insight-value.low {
    color: var(--danger-color);
}

.insight-detail {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* ==================== Hero Chips ==================== */
.hero-chips-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.hero-chip {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    transition: all 0.2s ease;
}

.hero-chip:hover {
    background: var(--hover-bg);
    border-color: rgba(59, 130, 246, 0.3);
    transform: translateY(-2px);
}

.hero-chip img {
    width: 48px;
    height: 27px;
    border-radius: 4px;
    object-fit: cover;
}

.hero-chip-name {
    font-weight: 600;
    color: var(--text-primary);
}

.hero-chip-stats {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

/* ==================== Hero List ==================== */
.hero-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.hero-item {
    background: rgba(255, 255, 255, 0.03);
    padding: 0.75rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    transition: all 0.2s ease;
}

.hero-item:hover {
    background: var(--hover-bg);
    border-color: rgba(59, 130, 246, 0.3);
}

.hero-item img {
    width: 64px;
    height: 36px;
    border-radius: 4px;
    object-fit: cover;
}

.hero-item-wr {
    font-weight: 700;
    font-size: 1rem;
}

.hero-item-wr.high {
    color: var(--success-color);
}

.hero-item-wr.mid {
    color: var(--warning-color);
}

.hero-item-wr.low {
    color: var(--danger-color);
}

.hero-item-games {
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* ==================== Pairs List ==================== */
.pairs-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.pair-item {
    background: rgba(255, 255, 255, 0.03);
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    transition: all 0.2s ease;
}

.pair-item:hover {
    background: var(--hover-bg);
    border-color: rgba(59, 130, 246, 0.3);
}

.pair-heroes {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.pair-heroes img {
    width: 64px;
    height: 36px;
    border-radius: 4px;
    object-fit: cover;
}

.pair-plus {
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.875rem;
}

.pair-stats {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.pair-wr {
    font-weight: 700;
    font-size: 1rem;
}

.pair-wr.high {
    color: var(--success-color);
}

.pair-wr.mid {
    color: var(--warning-color);
}

.pair-wr.low {
    color: var(--danger-color);
}

.pair-games {
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* ==================== Responsive Design ==================== */
@media (max-width: 1200px) {
    .side-by-side-grid {
        grid-template-columns: 1fr;
    }

    .four-column-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .five-column-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 768px) {
    .app-container {
        padding: 0.5rem;
        max-width: 100%;
        overflow-x: hidden;
    }

    body, html { overflow-x: hidden; }

    .header h1 { font-size: 1.1rem; }
    .header { margin-bottom: 0.5rem; }

    /* Filters: compact, 2 cols, no wasted vertical space */
    .filters-section {
        padding: 0.6rem;
        margin-bottom: 0.75rem;
        border-radius: 8px;
    }
    .filter-row {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 0.35rem;
        margin-bottom: 0.35rem;
    }
    .filter-group { min-width: 0; }
    .filter-group label {
        font-size: 0.6rem;
        letter-spacing: 0.2px;
        margin-bottom: 0.15rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .filter-group label i { display: none; }
    .filter-select,
    .filter-input {
        padding: 0.35rem 0.4rem;
        font-size: 0.75rem;
        border-radius: 6px;
        -webkit-appearance: none;
        appearance: none;
        width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }
    input.filter-input[type="date"] {
        font-size: 0.7rem;
        padding: 0.35rem 0.3rem;
    }
    .filter-select[multiple] { min-height: 60px; }
    .filter-action-group { grid-column: 1 / -1; }
    .refresh-btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
    }

    /* KPI cards: 2x2, smaller value */
    .kpi-section {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.4rem;
        margin-bottom: 0.75rem;
    }
    .kpi-card {
        padding: 0.6rem;
        border-radius: 8px;
    }
    .kpi-icon { font-size: 1.1rem; margin-bottom: 0.2rem; }
    .kpi-value {
        font-size: 1.4rem;
        margin-bottom: 0.15rem;
    }
    .kpi-label {
        font-size: 0.65rem;
        letter-spacing: 0.3px;
    }

    /* Sections and titles */
    .main-content { gap: 0.75rem; }
    .analysis-section {
        padding: 0.75rem;
        border-radius: 8px;
        max-width: 100%;
        overflow-x: hidden;
    }
    .section-title {
        font-size: 1rem;
        margin-bottom: 0.75rem;
        padding-bottom: 0.4rem;
    }
    .subsection { margin-bottom: 1rem; }
    .subsection-title { font-size: 0.9rem; margin-bottom: 0.5rem; }
    .mini-title { font-size: 0.75rem; margin-bottom: 0.4rem; }

    /* All multi-col grids collapse */
    .four-column-grid,
    .five-column-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.5rem;
    }
    .side-by-side-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }
    .coach-insights-grid {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    /* Tournament rows: allow wrap so stats don't push off-screen */
    .tournament-item {
        padding: 0.5rem 0.6rem;
        flex-wrap: wrap;
        gap: 0.35rem;
    }
    .tournament-name { font-size: 0.85rem; flex: 1 1 100%; min-width: 0; }
    .tournament-stats { gap: 0.4rem; flex-wrap: wrap; }
    .badge { padding: 0.15rem 0.5rem; font-size: 0.75rem; }

    .insight-card { padding: 0.6rem; }

    /* Generic chart/canvas: never exceed container */
    canvas { max-width: 100% !important; height: auto !important; }

    /* Any inner grids of unknown classes — defensive shrink */
    .analysis-section [class*="grid"] {
        min-width: 0;
    }
    .analysis-section [class*="grid"] > * {
        min-width: 0;
    }
}

@media (max-width: 480px) {
    .kpi-section {
        grid-template-columns: 1fr;
    }
    .four-column-grid,
    .five-column-grid {
        grid-template-columns: 1fr;
    }
}

/* ==================== Opponent Analysis (Collapsible) ==================== */
.opponent-analysis {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.opponent-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    transition: all 0.2s ease;
}

.opponent-card:hover {
    border-color: rgba(59, 130, 246, 0.3);
}

.opponent-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem;
    cursor: pointer;
    transition: background 0.2s ease;
}

.opponent-header:hover {
    background: var(--hover-bg);
}

.opponent-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.opponent-name {
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--text-primary);
}

.opponent-record {
    display: flex;
    gap: 0.5rem;
}

.opponent-toggle {
    color: var(--text-muted);
    transition: transform 0.2s ease;
}

.opponent-toggle.expanded {
    transform: rotate(180deg);
}

.opponent-content {
    display: none;
    padding: 0 1rem 0.5rem;
    border-top: 1px solid var(--border-color);
}

.opponent-content.expanded {
    display: block;
}

.opponent-section {
    margin-top: 0.5rem;
}

.opponent-section-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.35rem;
}

/* ==================== Win Rate Conditional Formatting ==================== */
.wr-high {
    color: var(--success-color);
}

.wr-mid {
    color: var(--warning-color);
}

.wr-low {
    color: var(--danger-color);
}

/* ==================== Full Analysis Text ==================== */
.full-analysis-text {
    background: rgba(0, 0, 0, 0.2);
    padding: 1.5rem;
    border-radius: 8px;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    white-space: pre-wrap;
    color: var(--text-secondary);
    max-height: 500px;
    overflow-y: auto;
}