/* ============================================
   STATS PANEL
   Charts, mana curve, synergy stats, widgets
   ============================================ */

/* Main Grid Layout */
.stats-grid-main {
    display: grid;
    gap: var(--space-4);
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
        "brk"
        "arch" "cons" "mana" "type"
        "color" "prod" "rare" "tags"
        "actor" "synd" "cog" "pop" "weak" "func" "syng" "comb";
}

/* The fade-in follows the reading order of the areas above, top down
   and left to right (stats.css, "Fade-in order"). */
.grid-item-brk { --reveal-order: 0; }
.grid-item-arch { --reveal-order: 1; }
.grid-item-cons { --reveal-order: 2; }
.grid-item-mana { --reveal-order: 3; }
.grid-item-type { --reveal-order: 4; }
.grid-item-color { --reveal-order: 5; }
.grid-item-prod { --reveal-order: 6; }
.grid-item-rare { --reveal-order: 7; }
.grid-item-tags { --reveal-order: 8; }
.grid-item-actor { --reveal-order: 9; }
.grid-item-synd { --reveal-order: 10; }
.grid-item-cog { --reveal-order: 11; }
.grid-item-pop { --reveal-order: 12; }
.grid-item-weak { --reveal-order: 13; }
.grid-item-func { --reveal-order: 14; }

/* Fade-in order: each layout numbers its widgets in reading order
   (`--reveal-order`, set beside its `grid-template-areas`), and the delay
   follows that number rather than the order the widgets sit in the page,
   which the areas rearrange at every width. */
.stats-panel .stats-grid-main.stagger-reveal > * {
    animation-delay: calc(var(--reveal-order, 0) * 36ms);
}

/* The Bracket row: ours and Commander Bracket's on one scale, first and full width. */
.grid-item-brk {
    grid-area: brk;
}

/* Base Area Assignments */
.grid-item-arch {
    grid-area: arch;
}

.grid-item-cons {
    grid-area: cons;
}

.grid-item-mana {
    grid-area: mana;
}

.grid-item-type {
    grid-area: type;
}

.grid-item-color {
    grid-area: color;
}

.grid-item-prod {
    grid-area: prod;
}

.grid-item-rare {
    grid-area: rare;
}

.grid-item-tags {
    grid-area: tags;
}

.grid-item-actor {
    grid-area: actor;
}

/* Effect Focus (actor-volume) widget */
.actor-volume-body {
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
}

.actor-volume-empty {
    padding: var(--space-4) var(--space-1);
    color: var(--text-muted);
    font-size: 0.85rem;
}

.grid-item-synd {
    grid-area: synd;
}

.grid-item-cog {
    grid-area: cog;
}

.grid-item-weak {
    grid-area: weak;
}

.grid-item-pop {
    grid-area: pop;
}

.grid-item-func {
    grid-area: func;
}

/* Size-query container so the score bar responds to the panel's actual width
   (the builder's panels resize independently of the viewport). Overlays
   rendered by stats widgets are portaled to <body>, so the layout containment
   this creates cannot capture their fixed positioning. */
.stats-panel {
    container-type: inline-size;
}

/* The score tiles wrap rather than sit in fixed columns: a tile that renders nothing
   (Popularity without rank data) leaves no empty column, and a short last row
   stretches to fill. One row on a wide panel, three then two on a narrower one, two
   by two on a phone. */
.stats-score-bar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-4);
    margin-bottom: var(--space-5);
    align-items: stretch;
}

.stats-score-bar > .stat-item {
    flex: 1 1 0;
}

@container (max-width: 1000px) {
    .stats-score-bar > .stat-item {
        flex-basis: 30%;
    }
}

@container (max-width: 600px) {
    .stats-score-bar > .stat-item {
        flex-basis: 40%;
    }
}

.stats-score-bar .stat-item {
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    min-height: 140px;
    padding: var(--space-3);
    background: var(--widget-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
}

.stats-score-bar .stat-item .stat-label {
    font-size: 0.85em;
    text-transform: none;
    letter-spacing: normal;
    color: var(--chart-title-color);
    text-align: center;
    flex-shrink: 0;
    margin-bottom: var(--space-2);
}

.stats-score-bar .stat-item .stat-value {
    font-size: 2.2em;
    font-weight: 700;
    text-align: center;
}

.stats-score-bar .stat-item .widget-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stats-score-bar .stat-item .widget-extras {
    flex-shrink: 0;
    text-align: center;
}

/* Chart Container */
.chart-container {
    background: var(--widget-bg);
    border-radius: var(--radius-2xl);
    padding: var(--space-3);
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 250px;
    position: relative;
}

.chart-title {
    font-size: 0.85em;
    color: var(--chart-title-color);
    margin: 0 0 var(--space-2-5) 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: var(--space-1);
}

.curve-bar-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    flex: 1;
    height: 80px;
    position: relative;
}

.curve-bar {
    width: 100%;
    min-width: 20px;
    max-width: 30px;
    min-height: 8px;
    background: linear-gradient(to top, var(--accent-color), var(--accent-hover));
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    position: relative;
    transition: height 0.3s ease;
}

.bar-count,
.bar-total {
    font-size: 0.65em;
    color: var(--text-color);
    margin-bottom: var(--space-0-5);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    white-space: nowrap;
}

.curve-label {
    font-size: 0.7em;
    color: var(--text-muted);
    margin-top: var(--space-1);
    display: inline-flex;
    align-items: center;
    gap: var(--space-px);
}

/* Scryfall symbol glyphs used as chart keys (curve x-axis, color legend) */
.stats-mana-symbol {
    width: 14px;
    height: 14px;
    display: inline-block;
    vertical-align: middle;
}

.curve-label .stats-mana-symbol {
    width: 13px;
    height: 13px;
}

/* Stacked bar for 0 CMC column */
.stacked-bar {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    width: 100%;
}

/* Alternative casts (overload, kicked, flashback, foretold) at this mana
   value: an outline above the solid bars, never part of the count. */
.curve-bar.curve-bar-alt {
    background: transparent;
    border: 1.5px dashed rgba(var(--accent-rgb), 0.55);
    border-bottom: none;
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    box-sizing: border-box;
}

.stacked-bar .curve-bar:first-child {
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}

.bar-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
}

/* Stat Item. The values are the ones that render: goldfish.css used to define
   these classes too, loaded later, and won wherever the two disagreed. */
.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-2-5);
    padding: var(--space-2) var(--space-4);
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    min-width: 80px;
    position: relative;
    transition: border-color 0.2s;
}

.stat-item:hover {
    border-color: var(--accent-glow);
}

/* Circular "?" help button pinned to a stat widget's corner */
.stat-help-btn {
    position: absolute;
    bottom: 5px;
    right: 5px;
    width: 20px;
    height: 20px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-round);
    background: var(--accent-bg);
    color: var(--text-muted);
    cursor: pointer;
    opacity: 0.65;
    transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}

.stat-help-btn:hover,
.stat-help-btn:focus-visible {
    opacity: 1;
    color: var(--accent-color);
    border-color: var(--accent-color);
    transform: scale(1.08);
}

.stat-help-btn-icon {
    width: 13px;
    height: 13px;
    flex: 0 0 13px;
}

.stat-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: none;
    letter-spacing: normal;
    margin-bottom: var(--space-1);
}

.stat-value {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-color);
}

/* Type Breakdown */
.type-breakdown {
    flex: 1;
    min-width: 200px;
}

.type-bars {
    display: flex;
    flex-direction: column;
    gap: var(--space-1-5);
    flex: 1;
    justify-content: center;
    width: 100%;
}

.type-bar-row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.type-label {
    font-size: 0.7em;
    color: var(--text-muted);
    width: 34px;
    text-align: center;
    flex-shrink: 0;
}

/* Mana-font type glyphs (same set as the Advanced Search type buttons) */
.type-label i {
    font-size: 1.5em;
    color: var(--text-secondary);
}

.type-bar-container {
    flex: 1;
    height: 12px;
    background: var(--chart-bar-track);
    border: 1px solid var(--chart-bar-track-border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.type-bar {
    height: 100%;
    border-radius: var(--radius-sm);
    box-shadow: inset 0 0 0 1px var(--chart-bar-fill-outline);
    transition: width 0.3s ease;
}

.type-count {
    font-size: 0.75em;
    color: var(--text-secondary);
    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 {
    width: 110px;
    height: 110px;
    border-radius: var(--radius-round);
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: 0.85em;
}

/* Synergy Stats */
.synergy-stats {
    padding: var(--space-2-5);
}

.synergy-count {
    margin: 0 0 var(--space-2-5) 0;
    color: var(--text-muted);
    font-size: 0.9em;
}

.synergy-count strong {
    color: var(--accent-color);
    font-size: 1.2em;
}

/* Shared "a filter is active" banner (Combos + Synergies tabs) — a search query, a disabled
   category/effect filter, or (Combos only) a precise cross-tab jump from the Genome drawer's
   "See combo details". */
.filter-status-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2-5);
    margin: var(--space-2-5) 0 var(--space-3) 0;
    padding: var(--space-1-5) var(--space-2-5);
    background: var(--entry-hover-bg);
    border-radius: var(--radius-md);
    font-size: 0.85em;
    color: var(--text-secondary);
}

.filter-status-clear-btn {
    flex: 0 0 auto;
    max-width: fit-content;
    margin-left: auto;
}

/* Footer section of a combo widget — divider matches the header's `.info-header`. */
.combo-genome-link-row {
    margin-top: var(--space-3);
    padding-top: var(--space-2);
    border-top: 1px solid var(--border-color);
}

/* Jumps back to the Genome tab with this combo's primary card focused. */
.combo-view-genome-btn {
    font-size: 0.78em;
    padding: var(--space-1) var(--space-2-5);
}

.no-synergies {
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
    padding: var(--space-5);
}

/* Flat synergy-pair list (Synergy tab) */
.synergy-pair-list,
.addition-discovery-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-2-5);
    --synergy-card-preview-height: clamp(150px, 34vh, 380px);
    /* What a preview is *allowed* to take across, from the height it would
       reach at the standard 63:88 card ratio. The previews are laid out from
       this width rather than from their height, so a pair shrinks to whatever
       column it is in — a narrow docked pane included — instead of being cut
       off by it. Reading the height variable here means the clamps below
       still decide the size. */
    --synergy-card-preview-width: calc(var(--synergy-card-preview-height) * 63 / 88);
    --synergy-group-card-width: clamp(107px, 24.3vh, 272px);
}

@supports (height: 1svh) {
    .synergy-pair-list,
    .addition-discovery-list {
        --synergy-card-preview-height: clamp(150px, 34svh, 380px);
        --synergy-group-card-width: clamp(107px, 24.3svh, 272px);
    }
}

.synergy-pair-row {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    /* The footer reads this to run its divider edge to edge. */
    --synergy-row-pad-x: var(--space-3-5);
    padding: var(--space-3) var(--synergy-row-pad-x);
    background: var(--widget-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-sizing: border-box;
}

/* Centered header: title on line 1, category + strength on line 2. */
.synergy-pair-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1-5);
    width: 100%;
    text-align: center;
}

/* Cards row: image | direction | image — only the SVG between them. */
.synergy-pair-cards {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: var(--space-2-5);
    width: 100%;
}

.synergy-pair-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1-5);
    /* One preview wide at most, and free to shrink below that: two of these
       plus the arrow always fit their row. */
    flex: 0 1 var(--synergy-card-preview-width);
    min-width: 0;
    text-align: center;
}

.synergy-pair-img {
    display: flex;
    justify-content: center;
    width: 100%;
    padding: 0;
    background: none;
    border: none;
    appearance: none;
    cursor: pointer;
    border-radius: var(--radius-md);
    overflow: hidden;
    line-height: 0;
    transition: transform 0.12s, box-shadow 0.12s;
}

@media (hover: hover) and (pointer: fine) {
    .synergy-pair-img:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
    }
}

/* The image fills the width its card was given, so it is as large as the
   clamps allow when there is room and no larger than its column when there
   is not. */
.synergy-pair-thumb {
    width: 100%;
    height: auto;
    display: block;
    border-radius: var(--radius-md);
}

/* A split card's half prints sideways and a flip card's other half upside
   down: turn the preview so the face the synergy reads is upright, scaled to
   the width its portrait box has (63:88). */
.synergy-pair-img.face-rot-cw .synergy-pair-thumb {
    transform: rotate(90deg) scale(0.716);
}

.synergy-pair-img.face-rot-ccw .synergy-pair-thumb {
    transform: rotate(-90deg) scale(0.716);
}

.synergy-pair-img.face-rot-180 .synergy-pair-thumb {
    transform: rotate(180deg);
}

.synergy-pair-name {
    font-size: 0.82em;
    font-weight: 600;
    color: var(--text-color);
    line-height: 1.2;
}

.synergy-pair-direction {
    display: flex;
    align-items: center;
    justify-content: center;
    /* Centred between two previews whose height now follows their width. */
    align-self: center;
    opacity: 0.9;
    flex-shrink: 0;
    color: var(--accent-color);
}

@media (max-width: 768px) {
    .synergy-pair-row {
        --synergy-row-pad-x: var(--space-3);
    }

    .synergy-pair-cards {
        gap: var(--space-2);
    }
}

.synergy-pair-why {
    width: 100%;
    font-size: 0.95em;
    font-weight: 600;
    color: var(--text-color);
    line-height: 1.35;
    text-align: center;
}

/* Summary line under every entry's title: "N cards · M synergies". */
.synergy-pair-subtitle {
    font-size: 0.78em;
    font-weight: 600;
    color: var(--text-muted);
    line-height: 1.2;
    text-align: center;
}

/* Relational "pairing line": one sentence describing how the two cards' axes fit together. */
.synergy-pair-description {
    width: 100%;
    max-width: 46ch;
    margin: var(--space-0-5) auto 0;
    font-size: 0.8em;
    font-weight: 500;
    color: var(--text-muted);
    line-height: 1.35;
    text-align: center;
    font-style: italic;
}

/* ── Consolidated shared-trait group row ─────────────────────────────────────
   A clique of cards that all share one trait (every flier, every Goblin) shown as a single
   entry: same row/header shell as a pair, with a wrapping grid of every card so the volume of
   the collection reads at a glance. Thumbnails are sized to match the pair widget's images
   exactly (so cards never look inconsistently scaled) — the group simply grows taller. */
.synergy-group-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-start;
    gap: var(--space-3);
    width: 100%;
}

.synergy-group-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1-5);
    /* Width chosen so the height-auto image lands at the same height as `.synergy-pair-thumb`
       (≈ 0.716 card aspect × that height clamp), keeping every preview the same size. */
    width: var(--synergy-group-card-width);
    max-width: 100%;
    padding: 0;
    background: none;
    border: none;
    appearance: none;
    cursor: pointer;
}

.synergy-group-thumb {
    width: 100%;
    height: auto;
    display: block;
    border-radius: var(--radius-md);
    transition: transform 0.12s, box-shadow 0.12s;
}

@media (hover: hover) and (pointer: fine) {
    .synergy-group-card:hover .synergy-group-thumb {
        transform: translateY(-2px);
        box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
    }
}

.synergy-group-name {
    font-size: 0.82em;
    font-weight: 600;
    color: var(--text-color);
    line-height: 1.2;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

/* Action bar (search · filter · sort) */
.synergy-action-bar {
    display: flex;
    align-items: center;
    gap: var(--space-2-5);
    margin-bottom: var(--space-3-5);
    flex-wrap: wrap;
}

/* `.standard-text-field` is `width: 100%`, so as a flex item in a wrapping row it claimed the
   whole first line and pushed Filter and Sort onto their own -- at every width, however much
   room was left over. As a sharing flex item it gives them the space when there is space. */
.synergy-action-bar > .standard-text-field {
    position: relative;
    flex: 1 1 240px;
    width: auto;
    min-width: 0;
}

/* Keep Filter + Sort as one responsive unit. When the full toolbar no longer fits, this unit
   wraps intact below the search field and the flexible search field fills its first row. */
.synergy-action-controls {
    display: flex;
    align-items: center;
    gap: var(--space-2-5);
    flex: 0 1 auto;
    min-width: 0;
    max-width: 100%;
}

.synergy-action-controls .synergy-sort-select {
    min-width: 0;
}

.synergy-search-input {
    flex: 1;
    min-width: 160px;
    padding: 9px 12px;
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: var(--radius-md);
    color: var(--text-color);
    font-size: 0.95em;
}

.synergy-search-input:focus {
    outline: none;
    border-color: var(--accent-color);
}

.synergy-filter-wrap {
    position: relative;
}

.synergy-toolbar-btn {
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
    padding: 9px 12px;
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: var(--radius-md);
    color: var(--text-color);
    font-size: 0.9em;
    cursor: pointer;
    white-space: nowrap;
}

.synergy-toolbar-btn:hover {
    border-color: var(--accent-color);
}

.synergy-toolbar-btn.active {
    border-color: var(--accent-color);
    color: var(--accent-color);
}

.synergy-filter-popover {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 120;
    min-width: min(280px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    max-height: 60vh;
    box-sizing: border-box;
    overflow-y: auto;
    background: var(--widget-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-xl);
    padding: var(--space-3-5);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
}

.synergy-filter-popover.combo-filter-popover {
    min-width: min(360px, calc(100vw - 24px));
    width: min(420px, calc(100vw - 24px));
}

.combo-filter-popover .synergy-filters-grid {
    grid-template-columns: 1fr;
    gap: var(--space-1-5);
}

.combo-filter-popover .filter-item {
    gap: var(--space-2-5);
    padding: 7px 8px;
}

.combo-filter-popover .filter-name {
    line-height: 1.25;
    white-space: normal;
    overflow-wrap: anywhere;
}

.combo-filter-popover .filter-count {
    margin-left: auto;
    flex-shrink: 0;
}

.synergy-sort-select {
    padding: 9px 12px;
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: var(--radius-md);
    color: var(--text-color);
    font-size: 0.9em;
    cursor: pointer;
}

.synergy-sort-select:focus {
    outline: none;
    border-color: var(--accent-color);
}

.synergy-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

/* Stats Synergy Container */
.stats-synergy-container {
    background: var(--widget-bg);
    border-radius: var(--radius-lg);
    padding: var(--space-3);
    margin-top: var(--space-2);
}

.stats-synergy-container .info-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-3);
    padding-bottom: var(--space-2);
    border-bottom: 1px solid var(--border-color);
}

.stats-synergy-container .info-header .info-name {
    font-weight: bold;
    color: var(--accent-color);
    font-size: 1.1em;
}

.stats-synergy-container .info-header .info-synergies {
    color: var(--text-secondary);
    font-size: 0.9em;
}

/* Portrait phones step the figures down (this sat in goldfish-panels.css and
   reached every stat widget from there). */
@media (max-width: 768px) and (max-aspect-ratio: 1/1) {
    .stat-value {
        font-size: 1.1em;
    }

    .stat-label {
        font-size: 0.7em;
    }
}
