/* ============================================
   STATS PANEL
   Charts, mana curve, synergy stats, widgets
   ============================================ */

/* Stats Charts Layout */
.stats-charts {
    display: flex;
    gap: 16px;
    margin-bottom: 20px;
}

.stats-charts-row {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: stretch;
}

.stats-charts-row .chart-container {
    flex: 1;
    min-width: 200px;
}

.stats-widgets-row {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: stretch;
}

.stats-widgets-row .stat-item {
    flex: 1 1 120px;
    min-width: 120px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    min-height: 140px;
    padding: 12px;
    background: var(--chart-bg);
    border: 1px solid var(--border-color);
}

.stats-widgets-row .stat-item .stat-label {
    font-size: 0.75em;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    text-align: center;
    flex-shrink: 0;
}

.stats-widgets-row .stat-item .stat-value {
    font-size: 2.2em;
    font-weight: 700;
    text-align: center;
}

.stats-widgets-row .stat-item .widget-content,
.stats-widgets-row .stat-item .salt-score-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stats-widgets-row .stat-item .widget-extras,
.stats-widgets-row .stat-item .salt-extras {
    flex-shrink: 0;
    text-align: center;
}

/* Chart Container */
.chart-container {
    flex: 1;
    background: var(--chart-bg);
    padding: 12px;
    border-radius: 8px;
}

/* Enhanced Chart Container */
.chart-container {
    background: var(--chart-bg);
    border-radius: 12px;
    padding: 12px;
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 250px;
}

.chart-title {
    font-size: 0.85em;
    color: var(--chart-title-color);
    margin: 0 0 10px 0;
    text-align: center;
}

/* Mana Curve */
.mana-curve {
    height: 100px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
}

.curve-bars {
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    height: 80px;
    gap: 4px;
}

.curve-bar-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    flex: 1;
    height: 80px;
}

.curve-bar {
    width: 100%;
    min-width: 20px;
    max-width: 30px;
    min-height: 8px;
    background: linear-gradient(to top, var(--accent-color), rgba(158, 158, 255, 0.6));
    border-radius: 4px 4px 0 0;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    position: relative;
    transition: height 0.3s ease;
}

.bar-count {
    font-size: 0.65em;
    color: #fff;
    position: absolute;
    top: -16px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.curve-label {
    font-size: 0.7em;
    color: #888;
    margin-top: 4px;
}

/* Stacked bar for 0 CMC column */
.stacked-bar {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    width: 100%;
}

.spell-bar {
    background: linear-gradient(to top, var(--accent-color), rgba(158, 158, 255, 0.6)) !important;
    border-radius: 4px 4px 0 0 !important;
}

.land-bar {
    background: linear-gradient(to top, #8B4513, #A0522D) !important;
    border-radius: 0 !important;
}

.stacked-bar .curve-bar:first-child {
    border-radius: 4px 4px 0 0;
}

.mana-bars {
    flex: 1;
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    padding-bottom: 8px;
    width: 100%;
}

.bar-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
}

/* Stats Row 2 */
.stats-row-2 {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: stretch;
}

.stats-row-2 .stat-item {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--chart-bg);
    border-radius: 12px;
    min-width: 100px;
    min-height: 130px;
}

.stats-row-2 .stat-label {
    font-size: 0.85em;
    color: var(--text-muted);
    font-weight: 500;
    text-align: center;
    flex-shrink: 0;
}

.stats-row-2 .stat-value {
    font-size: 1.6em;
    font-weight: 700;
    color: var(--accent-color);
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stats-row-2 .type-breakdown {
    flex: 1;
    min-width: 200px;
}

.stats-color-row {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
}

/* Stat Item */
.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 12px 20px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    min-width: 80px;
}

.stat-label {
    font-size: 0.7em;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.stat-value {
    font-size: 1.5em;
    font-weight: bold;
    color: var(--accent-color);
}

/* Type Breakdown */
.type-breakdown {
    flex: 1;
    min-width: 200px;
}

.type-bars {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
    justify-content: center;
    width: 100%;
}

.type-bar-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.type-label {
    font-size: 0.7em;
    color: #888;
    width: 80px;
    text-align: right;
}

.type-bar-container {
    flex: 1;
    height: 12px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
    overflow: hidden;
}

.type-bar {
    height: 100%;
    border-radius: 4px;
    transition: width 0.3s ease;
}

.type-count {
    font-size: 0.75em;
    color: #aaa;
    width: 24px;
    text-align: right;
}

/* Color/Rarity Pie Charts */
.color-pie,
.rarity-pie {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
}

.pie-chart-container {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    flex: 1;
    padding: 8px;
}

.pie-chart {
    width: 110px;
    height: 110px;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.pie-legend {
    display: flex;
    flex-direction: column;
    gap: 8px;
    justify-content: center;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85em;
}

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    flex-shrink: 0;
}

.legend-label {
    color: var(--text-secondary);
    white-space: nowrap;
}

/* Synergy Stats */
.synergy-stats {
    padding: 10px;
}

.stats-title {
    margin: 0 0 16px 0;
    color: var(--accent-color);
    font-size: 1.1em;
}

.synergy-title {
    font-size: 1em;
    color: #aaa;
    margin: 16px 0 10px 0;
}

.synergy-count {
    margin: 0 0 10px 0;
    color: #888;
    font-size: 0.9em;
}

.synergy-count strong {
    color: var(--accent-color);
    font-size: 1.2em;
}

.synergy-filter-row {
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.synergy-dropdown {
    flex: 1;
    padding: 10px;
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: 6px;
    color: var(--text-color);
    font-size: 1em;
    cursor: pointer;
}

.synergy-dropdown:focus {
    outline: none;
    border-color: var(--accent-color);
}

.synergy-badge-count {
    background: var(--accent-color);
    color: #000;
    font-weight: bold;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 0.9em;
    min-width: 30px;
    text-align: center;
}

.synergy-badge-count.empty {
    background: #444;
    color: #888;
}

.no-synergies {
    color: #666;
    font-style: italic;
    text-align: center;
    padding: 20px;
}

.synergy-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* Stats Synergy Container */
.stats-synergy-container {
    background: rgba(0, 0, 0, 0.4);
    border-radius: 8px;
    padding: 12px;
    margin-top: 8px;
}

.stats-synergy-container .info-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.stats-synergy-container .info-header .info-name {
    font-weight: bold;
    color: #fff;
    font-size: 1.1em;
}

.stats-synergy-container .info-header .info-synergies {
    color: #aaa;
    font-size: 0.9em;
}

/* Grouped Interactions Container */
.grouped-interactions-container {
    display: flex;
    gap: 20px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    padding: 15px;
}

.focus-card-column {
    width: 200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.focus-card-image img {
    width: 100%;
    border-radius: 4.75% / 3.5%;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
}

.focus-card-name {
    font-weight: bold;
    color: var(--accent-color);
    text-align: center;
    font-size: 0.9em;
}

.interactions-list-column {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.synergy-group {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 8px;
    align-items: flex-start;
}

.synergy-group-header {
    width: 100%;
    font-size: 0.85em;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 4px 10px;
    border-radius: 4px;
    align-self: flex-start;
    background: rgba(255, 255, 255, 0.05);
}

/* Partner Links */
.partner-link {
    display: inline-block;
    cursor: pointer;
    transition: color 0.2s ease;
    position: relative;
    padding: 2px 0;
}

.partner-link:hover {
    color: var(--accent-color);
    text-decoration: underline;
}

.interaction-row {
    display: flex;
    align-items: center;
    padding: 4px 0 4px 15px;
}

.interaction-row:hover {
    background: rgba(255, 255, 255, 0.03);
    border-radius: 4px;
}

.interaction-partner-name {
    flex: 1;
    text-align: left;
    font-weight: normal;
    color: #ddd;
}

.synergy-pair {
    display: flex;
    align-items: center;
    padding: 4px 0 4px 12px;
    font-size: 0.85em;
    color: #aaa;
}

.pair-card {
    color: var(--text-color);
}

.pair-connector {
    margin: 0 8px;
    color: var(--accent-color);
}

.pair-card.hoverable {
    position: relative;
    cursor: pointer;
    text-decoration: underline dotted;
    text-underline-offset: 3px;
}

.pair-card.hoverable:hover {
    color: var(--accent-color);
}

/* Salt Score Widget */
.salt-score-widget {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--chart-bg);
    border-radius: 12px;
    min-width: 140px;
    min-height: 130px;
}

.salt-score-widget .stat-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85em;
    color: var(--text-muted);
    font-weight: 500;
    flex-shrink: 0;
}

.salt-score-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    gap: 0;
}

.salt-value {
    font-size: 2em;
    font-weight: 700;
    line-height: 1;
}

.salt-extras {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.salt-label {
    font-size: 0.9em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.salt-flavor {
    font-size: 0.7em;
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
    max-width: 140px;
}

.salt-total {
    font-size: 0.65em;
    color: var(--text-secondary);
    margin-top: 2px;
    padding: 2px 8px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 10px;
}

.salt-badge {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid;
    border-radius: 8px;
    width: 100%;
}

.salt-badge-value {
    font-size: 1.3em;
    font-weight: 700;
    line-height: 1;
}

.salt-badge-label {
    font-size: 0.75em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 2px;
}

.no-salt-data {
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.85em;
}

/* CMC Widget */
.cmc-widget {
    min-width: 120px;
}

.cmc-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
}

.cmc-extras {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.cmc-label {
    font-size: 0.9em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.cmc-flavor {
    font-size: 0.7em;
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
}

/* Generic Widget Layout */
.widget-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
}

.widget-extras {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.widget-label {
    font-size: 0.9em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.widget-flavor {
    font-size: 0.7em;
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
}

.widget-pct {
    font-size: 0.7em;
    color: var(--text-secondary);
    padding: 2px 8px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 10px;
}

/* Archetype Radar Chart */
.archetype-display {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    position: relative;
}

.radar-svg {
    width: 100%;
    max-width: 250px;
    max-height: 250px;
    overflow: visible;
    flex-shrink: 1;
}

.radar-grid {
    fill: none;
    stroke: var(--input-border);
    stroke-width: 1px;
}

.radar-axis {
    stroke: var(--input-border);
    stroke-width: 1px;
}

.radar-data {
    fill: var(--radar-color);
    fill-opacity: 0.2;
    stroke: var(--radar-color);
    stroke-width: 2px;
    transition: all 0.5s ease-out;
}

.radar-point {
    fill: var(--radar-color);
    transition: all 0.5s ease-out;
}

.radar-label {
    fill: var(--text-muted);
    font-size: 10px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.archetype-summary {
    margin-top: 4px;
    text-align: center;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    width: 100%;
}

/* Badge Styling */
.archetype-badge {
    position: absolute;
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 14px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.03));
    border: 1.5px solid var(--radar-color);
    border-radius: 20px;
    font-size: 0.75em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--radar-color);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    max-width: 90%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-shrink: 0;
}

/* Multi-color gradient border for combined archetypes */
.archetype-badge.multi {
    border: 2px solid transparent;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.03)) padding-box,
        linear-gradient(90deg, var(--color-1), var(--color-2)) border-box;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    color: #ffffff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

/* Mobile Overrides */
@media (max-width: 768px) {
    .stats-widgets-row {
        flex-direction: column;
        gap: 12px;
    }

    .stats-widgets-row .stat-item {
        flex: none;
        width: 100%;
        min-height: 100px;
        padding: 10px;
    }

    .stats-widgets-row .stat-item .stat-value {
        font-size: 1.8em;
    }

    /* Stack chart rows vertically on mobile */
    .stats-charts-row {
        flex-direction: column;
        gap: 12px;
        height: auto !important;
        /* CRITICAL: Prevent wrapping */
        flex-wrap: nowrap !important;
    }

    /* Force single chart per row on mobile */
    .stats-charts-row .chart-container {
        flex: 0 0 auto !important;
        /* Allow natural height */
        width: 100% !important;
        min-width: unset;
        max-width: 100%;
    }

    /* Also handle any direct .stats-charts containers */
    .stats-charts {
        flex-direction: column;
    }

    .stats-charts .chart-container {
        flex: 0 0 auto !important;
        /* Allow natural height */
        width: 100% !important;
    }

    /* Limit mana curve chart height - but NOT type breakdown */
    .chart-container:not(.type-breakdown) {
        min-height: 180px;
        max-height: 200px;
    }

    /* Type breakdown needs more height for all the bars */
    .chart-container.type-breakdown {
        min-height: 200px;
        height: auto !important;
        /* Force content height */
        max-height: none !important;
        overflow: visible !important;
        margin-bottom: 24px !important;
        /* Ensure spacing */
    }

    .mana-curve {
        height: auto;
        max-height: 120px;
    }

    .curve-bars {
        height: 60px;
    }

    .curve-bar-wrapper {
        height: 60px;
    }

    /* Pie charts - ensure they don't overlap */
    .pie-chart-content {
        max-width: 150px;
        margin: 0 auto;
    }

    .pie-container svg {
        max-width: 100%;
        height: auto;
    }

    /* Type distribution bars */
    .type-bar {
        height: 16px;
    }

    /* Stack color/rarity/archetype charts vertically */
    .stats-color-row {
        flex-direction: column;
        gap: 12px;
    }

    .stats-color-row>* {
        width: 100% !important;
        flex: 0 0 auto !important;
        /* Allow natural height */
    }

    /* Radar chart - contain within its box using more specific selectors */
    .archetype-radar {
        max-height: 260px !important;
        overflow: hidden !important;
    }

    .archetype-radar .radar-chart {
        max-height: 220px !important;
        overflow: hidden !important;
    }

    .archetype-radar .archetype-display {
        max-height: 200px !important;
        overflow: hidden !important;
    }

    .archetype-radar .radar-svg {
        max-width: 160px !important;
        max-height: 160px !important;
    }

    /* Ensure all chart containers have max-height to prevent overflow */
    .stats-color-row .chart-container {
        max-height: 280px !important;
        overflow: hidden !important;
    }

    .stats-color-row>.chart-container {
        box-sizing: border-box;
    }

    /* Add spacing between color row and synergy section */
    .stats-color-row {
        margin-bottom: 24px !important;
        height: auto !important;
        /* CRITICAL: Allow container to grow */
    }

    .synergy-section-header,
    .synergy-title {
        margin-top: 24px !important;
    }
}

/* ============================================
   ULTRA-WIDE LAYOUT (> 1600px)
   ============================================ */
@media (min-width: 1600px) {
    .stats-panel {
        display: grid;
        grid-template-columns: repeat(6, 1fr);
        gap: 24px;
        align-items: start;
    }

    /* Full width elements */
    .stats-header,
    .stats-widgets-row {
        grid-column: 1 / -1;
    }

    /* Split Synergies and Combos (3 cols each = 50%) */
    .synergy-section-wrapper,
    .combos-section-wrapper {
        grid-column: span 3;
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    /* Unwrap flex rows to use the grid */
    .stats-charts-row,
    .stats-color-row {
        display: contents;
    }

    /* --- EXPLICIT GRID PLACEMENT FOR 3 COLUMNS (REORDERED) --- */

    /* Left Column: Color Distribution (Top - Moved from Bottom) */
    .stats-color-row>.chart-container:nth-child(1) {
        grid-column: 1 / 3;
        grid-row: 2;
        height: 100%;
    }

    /* Left Column: Mana Curve (Bottom - Moved from Top) */
    .stats-charts-row>.chart-container:nth-child(1) {
        grid-column: 1 / 3;
        grid-row: 3;
        height: 100%;
    }

    /* Middle Column: Archetype Radar (Center - Swapped with Rarity) */
    .stats-color-row>.chart-container:nth-child(3) {
        grid-column: 3 / 5;
        grid-row: 2 / 4;
        height: 100%;
        display: flex;
        flex-direction: column;
        /* justify-content: center; removed to lock title to top */
        /* align-items: center; removed to allow stretching */
    }

    /* Right Column: Rarity Distribution (Top - Swapped & Moved) */
    .stats-color-row>.chart-container:nth-child(2) {
        grid-column: 5 / 7;
        grid-row: 2;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    /* Right Column: Card Types (Bottom - Moved from Top) */
    .stats-charts-row>.chart-container:nth-child(2) {
        grid-column: 5 / 7;
        grid-row: 3;
        height: 100%;
    }

    /* Constrain Archetype (now in middle) */
    .archetype-radar {
        width: 100%;
        max-width: 600px;
        margin: 0 auto;
    }

    .archetype-radar .radar-chart {
        width: 100%;
        flex-grow: 1;
        /* Check this */
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        /* Vertically center the chart content */
    }

    .archetype-radar .archetype-display {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    /* Ensure Radar chart uses available space */
    .archetype-radar .radar-svg {
        max-width: 100%;
        max-height: 450px;
        width: auto;
    }
}