/* Chart styles. Load after elements.css.
   The first sections are for all charts (legends, legend toggles, tooltips).
   Then there is one section for each chart template in components/charts/. */

/* LEGENDS */

/* Legend row under a chart (stacked bar and horizontal dots charts). Each chart sets its own gap. */
.chart-legend {
    display: flex;
    flex-wrap: wrap;
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-normal);
    line-height: var(--line-height-md);
    /* Reset direction for legend */
    direction: ltr;
}

/* Chart legend item that shows or hides a series on click. The negative margin
   cancels the padding, so the text stays where an item without padding is. */
.chart-legend-toggle {
    padding: 0 var(--p-1);
    margin: 0 calc(-1 * var(--p-1));
    border-radius: var(--border-radius-xs);
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.chart-legend-toggle:hover,
.chart-legend-toggle:focus-visible {
    background-color: var(--surface-hover);
    color: var(--text-primary);
}

/* One legend item: components/charts/_legend.html */
.legend-container {
    min-width: 0;

    .legend-color-swatch {
        width: 8px;
        height: 8px;
        min-width: 8px;
        border-radius: var(--border-radius-xs);
        flex-shrink: 0;
    }

    .legend-label {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        border: none;
        background: none;
        padding: 0;
    }
}

/* TOOLTIPS */

/* Tooltip box that the chart JS positions. It looks like a Bootstrap .tooltip-inner (bootstrap-overrides.css). */
.chart-tooltip {
    background-color: var(--surface-tertiary);
    border: var(--border-width) solid var(--border-default);
    border-radius: var(--border-radius-sm);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
    gap: 4px;
    padding: var(--p-2);
    max-width: 380px !important;
    pointer-events: none;
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
    white-space: normal !important;
    word-break: break-word !important;
    box-shadow: var(--shadow-elevated-sm);
}

/* Follows the hovered bar: content swaps instantly, the box glides on a critically damped spring (stiffness 100, damping 20)
   and pops in with a bouncier one (stiffness 300, damping 25) from scale .85 / 20px towards the bar. Fade-out is opacity only. */
.chart-tooltip-following {
    --chart-tooltip-spring: linear(0, 0.09, 0.264, 0.442, 0.594, 0.713, 0.801, 0.864, 0.908, 0.939, 0.96, 0.973, 0.983, 0.989, 1);
    --chart-tooltip-pop: linear(0, 0.096, 0.298, 0.517, 0.707, 0.851, 0.946, 1.002, 1.029, 1.038, 1.035, 1.028, 1.02, 1.012, 1);
    border: none;
    border-radius: var(--border-radius-default);
    padding: var(--primitive-3) var(--primitive-4);
    gap: var(--primitive-2);
    align-items: stretch;
    min-width: 200px;
    max-width: 280px !important;
    opacity: 0;
    visibility: hidden;
    transform: translateX(20px) scale(0.85);
    transition:
        left 0.7s var(--chart-tooltip-spring),
        top 0.7s var(--chart-tooltip-spring),
        opacity 0.1s ease,
        visibility 0s 0.1s;
}

.chart-tooltip-following.is-visible {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition:
        left 0.7s var(--chart-tooltip-spring),
        top 0.7s var(--chart-tooltip-spring),
        transform 0.4s var(--chart-tooltip-pop),
        opacity 0.1s ease;
}


/* STACKED BAR CHART: components/charts/stacked_bar_chart.html */

.stacked-bar-chart-container {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    /* Ensure container takes full width */
    direction: rtl;
    /* Make scroll start from right */
}

.stacked-bar-chart-container-inner {
    min-width: 532px;
    /* Apply min-width to all screen sizes */
    width: 100%;
    direction: ltr;
    /* Reset direction for content */
    position: relative;
}

.stacked-bar-chart-container canvas {
    min-width: 532px;
    width: 100%;
    height: 188px !important;
}

.stacked-bar-chart-container-legend {
    gap: 0 var(--p-2);
}

.stacked-bar-chart-container-inner .chart-skeleton-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface-secondary);
    display: none;
    flex-direction: column;
    justify-content: flex-end;
    padding: 0 8px 4px 8px;
    opacity: 1;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
    min-width: 532px;
    height: 188px;
    z-index: var(--z-index-above-content-1);
}

.stacked-bar-chart-container-inner .chart-skeleton-overlay.visible {
    display: flex;
}

.stacked-bar-chart-container-inner .chart-skeleton-overlay.fade-out {
    opacity: 0;
}

.chart-skeleton-bars {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    height: 148px;
    gap: 2px;
    margin-bottom: 8px;
}

.chart-skeleton-bar {
    flex: 1;
    min-width: 6px;
    max-width: 8px;
    border-radius: var(--border-radius-xs);
}

.chart-skeleton-labels {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 16px;
}


/* SIGNAL STACKED BAR CHART: components/charts/signal_stacked_bar_chart.html */

.signal-chart-container {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    direction: rtl;
}

.signal-chart-container-inner {
    min-width: 200px;
    width: 100%;
    direction: ltr;
    position: relative;
}

.signal-chart-container canvas {
    min-width: 200px;
    width: 100%;
    height: 120px !important;
}

.signal-chart-skeleton-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface-secondary);
    display: none;
    flex-direction: column;
    justify-content: flex-end;
    padding: 0 4px 4px 4px;
    opacity: 1;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
    min-width: 200px;
    height: 120px;
    z-index: var(--z-index-above-content-1);
}

.signal-chart-skeleton-overlay.visible {
    display: flex;
}

.signal-chart-skeleton-overlay.fade-out {
    opacity: 0;
}

.signal-chart-skeleton-bars {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    height: 100px;
    gap: 2px;
    margin-bottom: 4px;
}

.signal-chart-skeleton-bar {
    flex: 1;
    min-width: 4px;
    max-width: 6px;
    border-radius: var(--border-radius-xs);
}

.signal-chart-skeleton-labels {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 12px;
}


/* HORIZONTAL BAR CHART: components/charts/horizontal_bar_chart.html */

.horizontal-bar-chart {
    display: flex;
    flex-direction: column;
    gap: var(--p-2, 8px);
    position: relative;
}

.horizontal-bar-chart-header {
    display: flex;
    flex-direction: column;
    gap: var(--p-1, 4px);
}

.horizontal-bar-chart-title {
    font-size: var(--font-size-default);
    font-weight: var(--font-weight-medium);
    color: var(--text-primary);
}

.horizontal-bar-chart-subtitle {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

.horizontal-bar-chart-canvas {
    width: 100%;
    position: relative;
}

.chart-skeleton-overlay-horizontal {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface-secondary);
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
    z-index: 10;
}

.chart-skeleton-overlay-horizontal.visible {
    display: flex;
}

.chart-skeleton-overlay-horizontal.fade-out {
    opacity: 0;
}

.skeleton-bars {
    display: flex;
    flex-direction: column;
    gap: var(--p-2);
    width: 80%;
}

.skeleton-bar {
    height: 16px;
    background: var(--surface-primary);
    border-radius: 4px;
    animation: pulse-bar 1.5s ease-in-out infinite;
}

.skeleton-bar:nth-child(1) {
    width: 75%;
}

.skeleton-bar:nth-child(2) {
    width: 60%;
    animation-delay: 0.1s;
}

.skeleton-bar:nth-child(3) {
    width: 45%;
    animation-delay: 0.2s;
}

.skeleton-bar:nth-child(4) {
    width: 30%;
    animation-delay: 0.3s;
}

@keyframes pulse-bar {

    0%,
    100% {
        opacity: 0.4;
    }

    50% {
        opacity: 1;
    }
}


/* HORIZONTAL DOTS CHART: components/charts/horizontal_dots_chart.html */

.horizontal-dots-chart-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    /* Ensure container takes full width */
    direction: rtl;
    /* Make scroll start from right */
}

.horizontal-dots-chart-container-inner {
    min-width: 532px;
    /* Apply min-width to all screen sizes */
    width: 100%;
    direction: ltr;
    /* Reset direction for content */
}



.horizontal-dots-chart-container-legend {
    gap: var(--p-2) var(--p-3);
    margin-top: 8px;
    min-height: 20px;
}


/* DOUGHNUT CHART: components/charts/doughnut.html */

.doughnut {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--p-3, 16px) var(--p-6, 64px);

    .doughnut-chart {
        width: 112px;
        height: 112px;
    }
}


/* POLAR CHART: components/charts/polar_chart.html */

.polar-chart-clip {
    height: calc(var(--polar-chart-height) - var(--polar-chart-crop-bottom));
    overflow: hidden;
}

.polar-chart-container {
    width: 100%;
    height: var(--polar-chart-height);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

.polar-chart-container canvas {
    width: 100%;
    height: var(--polar-chart-height) !important;
    max-width: calc(var(--polar-chart-height) * 1.5);
}

.polar-chart-skeleton-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: var(--polar-chart-crop-bottom);
    background: var(--surface-secondary);
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
    z-index: var(--z-index-above-content-1);
}

.polar-chart-skeleton-overlay.visible {
    display: flex;
}

.polar-chart-skeleton-overlay.fade-out {
    opacity: 0;
}

.polar-chart-skeleton-rings {
    position: relative;
    width: 240px;
    height: 240px;
}

.polar-chart-skeleton-ring {
    position: absolute;
    border-radius: 50%;
    border: var(--border-width) solid var(--border-default);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}


/* SPARKLINE: components/charts/sparkline.html */

.sparkline {
    display: inline-block;
    width: 100%;
    height: 50px;
    position: relative;

    canvas {
        display: block;
        width: 100% !important;
        height: 50px !important;
    }
}


/* QUADRANT CHART: components/charts/quadrant_chart.html */

/* Give the chart a stable height so HTMX swaps / hidden tab renders
   don't result in occasional huge vertical sizing from Chart.js. */
.quadrant-chart-container-inner {
    height: clamp(320px, 21vw, 600px);
}

.quadrant-chart-container canvas {
    height: 100% !important;
}

.quadrant-chart-container {
    width: 100%;
    max-width: 100%;
    position: relative;

    .quadrant-chart-container-inner {
        width: 100%;
        max-width: 100%;
        position: relative;
    }

    canvas {
        width: 100%;
        max-width: 100%;
        height: 100%;
    }

    .search-and-filters {
        .search-input-wrapper {
            max-width: 300px;
        }
    }
}

.chart-skeleton-overlay-quadrant {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface-secondary);
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
    z-index: var(--z-index-above-content-1);

    &.visible {
        display: flex;
    }

    &.fade-out {
        opacity: 0;
    }

    .skeleton-dots {
        display: flex;
        gap: 10px;

        .skeleton-dot {
            width: 16px;
            height: 16px;
            background: var(--surface-primary);
            border-radius: 50%;
            animation: chart-skeleton-pulse 1.5s ease-in-out infinite;

            &:nth-child(2) {
                animation-delay: 0.2s;
            }

            &:nth-child(3) {
                animation-delay: 0.4s;
            }
        }
    }
}

@keyframes chart-skeleton-pulse {

    0%,
    100% {
        opacity: 0.4;
    }

    50% {
        opacity: 1;
    }
}

.quadrant-chart-avatars {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: var(--z-index-above-content-1);
}

.quadrant-chart-avatar {
    position: absolute;
    width: 24px;
    height: 24px;
    pointer-events: auto;
    transform: translate(-50%, -50%);
    z-index: var(--z-index-above-content-1);
    transition: z-index 0s;

    &:hover {
        z-index: var(--z-index-above-content-2);
    }
}


/* COLLABORATION NETWORK CHART: components/charts/collaboration_network_chart.html */

.collaboration-network-container {
    width: 100%;
    max-width: 100%;
    position: relative;

    .search-and-filters {
        .search-input-wrapper {
            max-width: 300px;
        }
    }
}

.collaboration-network-container-inner {
    height: clamp(320px, 21vw, 500px);
    width: 100%;
    position: relative;

    &.dot-mode {
        height: clamp(400px, 25vw, 700px);
    }

    .zoom-reset-btn {
        position: absolute;
        bottom: 8px;
        right: 8px;
        z-index: var(--z-index-above-content-1);
        background: var(--surface-primary);
        border: var(--border-width) solid var(--border-default);
        border-radius: var(--border-radius);
        color: var(--text-secondary);
        padding: 4px 8px;
        cursor: pointer;

        &:hover {
            background: var(--gray-alpha-200);
            color: var(--text-primary);
        }
    }
}

.collaboration-network-container svg {
    width: 100%;
    height: 100%;
}

.chart-skeleton-overlay-network {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface-secondary);
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
    z-index: 10;

    &.visible {
        display: flex;
    }

    &.fade-out {
        opacity: 0;
    }

    .skeleton-dots {
        display: flex;
        gap: 10px;

        .skeleton-dot {
            width: 16px;
            height: 16px;
            background: var(--surface-primary);
            border-radius: 50%;
            animation: chart-skeleton-pulse 1.5s ease-in-out infinite;

            &:nth-child(2) {
                animation-delay: 0.2s;
            }

            &:nth-child(3) {
                animation-delay: 0.4s;
            }
        }
    }
}


/* KNOWLEDGE GRAPH TREEMAP CHART: components/charts/knowledge_graph_treemap_chart.html */

.knowledge-graph-container {
    width: 100%;
    position: relative;

    .knowledge-graph-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        margin-bottom: 12px;
    }

    .knowledge-graph-breadcrumbs {
        display: flex;
        align-items: center;
        gap: 4px;
        flex-wrap: wrap;

        .breadcrumb-item {
            cursor: pointer;
            color: var(--text-secondary);
            padding: 2px 6px;
            border-radius: var(--border-radius);

            &:hover {
                color: var(--text-primary);
                background: var(--surface-hover);
            }

            &.active {
                color: var(--text-primary);
                cursor: default;

                &:hover {
                    background: transparent;
                }
            }
        }

        .breadcrumb-separator {
            color: var(--text-tertiary);
        }
    }

}

.knowledge-graph-treemap {
    width: 100%;
    height: clamp(400px, 45vw, 600px);
    position: relative;
}

.knowledge-graph-treemap-inner {
    width: 100%;
    height: 100%;
}

.treemap-tooltip {
    position: fixed;
    pointer-events: none;
    background: var(--surface-primary);
    border: var(--border-width) solid var(--border-default);
    border-radius: var(--border-radius-sm);
    box-shadow: var(--shadow-elevated-sm);
    padding: 8px 12px;
    z-index: var(--z-index-tooltip);
    width: max-content;
    display: none;

    .tooltip-path {
        color: var(--text-primary);
        margin-bottom: 4px;
    }

    .tooltip-stats {
        display: flex;
        gap: 12px;
        color: var(--text-secondary);
        margin-bottom: 4px;
    }

    .tooltip-contributors {
        color: var(--text-tertiary);

        .contributor-item {
            display: flex;
            justify-content: space-between;
            gap: 8px;
        }
    }
}

.treemap-legend {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    color: var(--text-tertiary);

    .legend-gradient {
        display: flex;
        align-items: center;
        gap: 4px;

        .gradient-bar {
            width: 120px;
            height: 8px;
            border-radius: 4px;
            background: linear-gradient(to right, var(--gray-700), var(--green-600));
        }
    }
}

.chart-skeleton-overlay-treemap {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface-secondary);
    display: none;
    border-radius: var(--border-radius);
    overflow: hidden;

    &.show {
        display: grid;
        grid-template-columns: 1fr 1fr 1.2fr;
        grid-template-rows: 1.5fr 1fr 1fr;
        gap: 3px;
        padding: 3px;
    }

    .skeleton-rect {
        background: var(--surface-primary);
        border-radius: var(--border-radius);
        animation: fade-pulse 2.4s ease-in-out infinite;

        &:nth-child(1) {
            grid-column: 1 / 3;
        }

        &:nth-child(4) {
            grid-column: 3;
            grid-row: 2 / 4;
        }

        &:nth-child(2) {
            animation-delay: 0.4s;
        }

        &:nth-child(3) {
            animation-delay: 0.8s;
        }

        &:nth-child(4) {
            animation-delay: 1.2s;
        }

        &:nth-child(5) {
            animation-delay: 1.6s;
        }
    }
}

@keyframes fade-pulse {

    0%,
    100% {
        opacity: 0.3;
    }

    50% {
        opacity: 1;
    }
}


/* COHORT GRID: components/charts/cohort_grid.html */

.cohort-grid {
    display: flex;
    flex-wrap: wrap;
    gap: var(--p-4);
}

/* A quarter of age columns needs the full width, so populations stack instead of sitting side by
   side; the shared column headers then line the two panels up vertically. */
.cohort-grid-panel {
    flex: 1 1 100%;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: var(--p-2);
}

.cohort-grid-panel-label {
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-sm);
}

.cohort-grid-table {
    display: grid;
    grid-template-columns:
        minmax(var(--cohort-grid-label-width, 110px), auto) minmax(44px, auto) repeat(var(--cohort-grid-value-columns), minmax(var(--cohort-grid-cell-width, 34px), 1fr));
    gap: 3px;
    align-items: stretch;
}

.cohort-grid-head {
    color: var(--text-tertiary);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    white-space: nowrap;
    padding-bottom: var(--p-1);
}

.cohort-grid-head-value {
    text-align: center;
}

.cohort-grid-head-volume,
.cohort-grid-row-volume {
    text-align: right;
    padding-right: var(--p-1);
}

.cohort-grid-row-label,
.cohort-grid-row-volume {
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    line-height: var(--cohort-grid-cell-height, 26px);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.cohort-grid-row-link {
    color: var(--text-secondary);
    text-decoration: none;
}

.cohort-grid-row-link:hover {
    color: var(--text-primary);
    text-decoration: underline;
}

.cohort-grid-cell {
    box-sizing: border-box;
    height: var(--cohort-grid-cell-height, 26px);
    border-radius: 3px;
    border: var(--border-width) solid transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-xs);
    transition: border-color 0.2s ease;
}

.cohort-grid-cell:hover {
    border-color: var(--border-high-contrast);
}

/* One hue, emphasis rising with the value, the way the matrix map ramps its brand scheme. Level 5
   leaves the ramp for the danger surface because it is the level worth stopping on. */
.cohort-grid [data-level="1"] {
    background-color: var(--cohort-grid-level-1, var(--brand-800));
    color: var(--cohort-grid-level-1-text, var(--text-inverse));
}

.cohort-grid [data-level="2"] {
    background-color: var(--cohort-grid-level-2, var(--brand-600));
    color: var(--cohort-grid-level-2-text, var(--text-inverse));
}

.cohort-grid [data-level="3"] {
    background-color: var(--cohort-grid-level-3, var(--brand-400));
    color: var(--cohort-grid-level-3-text, var(--text-primary));
}

.cohort-grid [data-level="4"] {
    background-color: var(--cohort-grid-level-4, var(--brand-200));
    color: var(--cohort-grid-level-4-text, var(--text-primary));
}

.cohort-grid [data-level="5"] {
    background-color: var(--cohort-grid-level-5, var(--semantic-percentile-low-surface));
    color: var(--cohort-grid-level-5-text, var(--semantic-percentile-low-text));
}

/* A cell with nothing to measure is hollow: it carries no number, so it cannot read as a zero. */
.cohort-grid-cell[data-state="immature"],
.cohort-grid-cell[data-state="sparse"] {
    background-color: var(--surface-primary);
    border-style: dashed;
}

.cohort-grid[data-collapsed="true"] .cohort-grid-overflow {
    display: none;
}

.cohort-grid-toggle {
    flex: 1 1 100%;
    align-self: start;
    padding: 0;
    border: 0;
    background: none;
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-sm);
}

.cohort-grid-toggle:hover {
    color: var(--text-primary);
    text-decoration: underline;
}

.cohort-grid-toggle[hidden] {
    display: none;
}

/* While calculating there are no panels yet, so the skeleton takes their place in the flow. */
.cohort-grid-skeleton {
    display: none;
    flex: 1 1 100%;
    pointer-events: none;
}

.cohort-grid-skeleton.visible {
    display: block;
}

.cohort-grid-legend {
    flex: 1 1 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--p-1) var(--p-2);
    margin-top: var(--p-2);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-md);
}

.cohort-grid-legend-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.cohort-grid-legend-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    border: var(--border-width) solid var(--border-default);
}

.cohort-grid-legend-color[data-state="unavailable"] {
    background-color: var(--surface-primary);
    border-style: dashed;
}

/* The control and the panel it selects each take a full row of the grid's flex flow. */
.cohort-grid-controls,
.cohort-grid-panes {
    flex: 1 1 100%;
}

/* On a phone the age columns are wider than the card: the grid scrolls inside its pane */
.cohort-grid-panes {
    min-width: 0;
    overflow-x: auto;
}
