/* =========================================
   Chart styling (shared/base)
   Owns the markup emitted by DynamicChartEngine
   (static/base/js/chart_engine.js) and the classes of its opt-in
   extensions. Charts on any page inherit this file; app-specific
   presentation stays in the app's own stylesheet.
   ========================================= */

/* The dashboard wrapper */
.chart-dashboard-grid {
    --chart-tone: var(--primary-color, 78, 97, 160);
    --chart-secondary: 16, 185, 129;
    --chart-border: var(--dash-border, rgba(var(--chart-tone), 0.14));
    --chart-radius: var(--dash-radius, 12px);
    --chart-surface: #ffffff;
    --chart-muted: rgba(var(--text-color, 31, 41, 55), 0.62);
    --chart-shadow: var(--dash-shadow, 0 0.45em 1.1em rgba(var(--text-color, 31, 41, 55), 0.06));

    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: var(--dash-gap, 24px);
    /* Cards in the same row (the donut pair) share the row height. */
    align-items: stretch;
    position: relative;
}

/* ------------------------------------------------------------------
   Tinted theme (opt-in)
   Add `chart-theme-tinted` to the grid wrapper for a soft brand-tinted
   canvas: light primary wash, white cards using the dashboard's border,
   radius and shadow tokens, and dotted gridlines. The primary color is
   the source for every surface, so the palette follows the brand. Every
   rule is scoped to the theme, so chart pages that do not opt in keep
   the flat style above.
   ------------------------------------------------------------------ */

.chart-dashboard-grid.chart-theme-tinted {
    --chart-grid-ink: var(--accent-blue-80, 62, 77, 128);
    --chart-canvas: var(--accent-light-blue, 241, 247, 255);
    --chart-canvas-tint: var(--light-primary-color-full-opacity, 236, 240, 250);
    --chart-tone: var(--primary-color, 78, 97, 160);
    --chart-secondary: var(--secondary-color, 16, 185, 129);
    --chart-border: rgba(var(--primary-color, 78, 97, 160), 0.16);
    --chart-radius: var(--dash-radius, 12px);
    --chart-surface: #ffffff;
    --chart-shadow: var(--dash-shadow, 0 0.45em 1.1em rgba(var(--text-color, 31, 41, 55), 0.06));
    --chart-muted: var(--dash-muted, rgba(var(--text-color, 31, 41, 55), 0.62));

    padding: clamp(0.85em, 1.6vw, 1.35em);
    border: 1px solid rgba(var(--primary-color, 78, 97, 160), 0.12);
    border-radius: calc(var(--chart-radius) + 0.4em);
    background-color: rgb(var(--chart-canvas-tint));
    background-image: radial-gradient(rgba(var(--primary-color, 78, 97, 160), 0.14) 1px, transparent 1.2px);
    background-size: 16px 16px;
    background-position: -1px -1px;
}

/* The dashboard cards stay flat: no gradient accent line. */
.chart-theme-tinted .chart-container::before {
    display: none;
}

.chart-theme-tinted .chart-title {
    border-bottom-color: rgba(var(--primary-color, 78, 97, 160), 0.12);
    color: rgb(var(--text-color, 32, 34, 36));
}

/* Soft primary wash inside the card */
.chart-theme-tinted .chart-scroll-wrapper {
    border-radius: calc(var(--chart-radius) - 0.2em);
    background: rgba(var(--chart-canvas), 0.65);
}

/* Dotted gridlines: a zero-length dash with a round cap draws a dot. */
.chart-theme-tinted .ct-chart {
    --ct-label-color: var(--chart-muted);
    --ct-grid-color: rgba(var(--chart-grid-ink), 0.16);
    --ct-grid-width: 2px;
    --ct-grid-dasharray: 0 9px;
}

.chart-theme-tinted .ct-chart .ct-grid {
    stroke-linecap: round;
}

.chart-theme-tinted .ct-chart .ct-line {
    stroke-width: 3px;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.chart-theme-tinted .ct-chart .ct-point {
    stroke-width: 8px;
}

.chart-theme-tinted .ct-chart .ct-area {
    fill-opacity: 0.22;
}

/* The absolute overlay that covers the grid while charts load */
.loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--chart-radius);
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(2px);
}

/* Individual chart cards */
.chart-container {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    min-width: 0;
    padding: 1.35em 1.4em 1.15em;
    border: 1px solid var(--chart-border);
    border-radius: var(--chart-radius);
    background: var(--chart-surface);
    box-shadow: var(--chart-shadow);

    /* Consume the CSS variable dynamically injected by JS */
    grid-column: span var(--col-span, 12);

    transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;

    /* Entry is staggered per card by the engine (--chart-enter-delay). */
    animation: chart-card-in 0.5s cubic-bezier(0.22, 0.61, 0.36, 1) var(--chart-enter-delay, 0ms) backwards;

    /* Wide donut cards can move their legend beside the ring (@container). */
    container-type: inline-size;
}

@keyframes chart-card-in {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

/* Scroll reveal (opt-in `revealOnScroll`): the card stays hidden and the
   engine re-applies the entrance animation when it enters the viewport. */
@media (prefers-reduced-motion: no-preference) {
    .chart-container.is-awaiting-reveal {
        animation: none;
        opacity: 0;
    }
}

/* Subtle tone accent along the top edge of every card */
.chart-container::before {
    content: '';
    position: absolute;
    inset: -1px -1px auto;
    height: 3px;
    border-radius: var(--chart-radius) var(--chart-radius) 0 0;
    background: linear-gradient(
        90deg,
        rgb(var(--chart-tone)) 0%,
        rgb(var(--chart-secondary)) 62%,
        #FAC710 100%
    );
    opacity: 0.9;
    pointer-events: none;
}

/* Chart title */
.chart-title {
    margin: 0 0 0.75em;
    padding: 0 0 0.55em;
    border-bottom: 1px solid rgba(var(--chart-tone), 0.12);
    color: rgb(var(--text-color, 31, 41, 55));
    font-family: inherit;
    font-size: 1.02rem;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
    text-align: left;
}

/* Plot + legend stack by default. */
.chart-body {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Wide donut cards use the horizontal space: ring on the left, labels on the
   right. The query uses the card's content box, so cards that are too narrow
   keep the legend underneath. */
@container (min-width: 40em) {
    .chart-container.chart-donut .chart-body {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(150px, max-content);
        gap: 1.5em;
        align-items: center;
    }

    .chart-container.chart-donut .chart-legend {
        flex-direction: column;
        flex-wrap: nowrap;
        align-items: flex-start;
        justify-content: center;
        margin-top: 0;
        gap: 0.4em;
    }
}

/* Isolates the chart from the card and title. It stays a scroll container
   for charts that opt into a min-width, but the page keeps scrolling when the
   wheel is over the chart (no `overscroll-behavior: contain`). */
.chart-scroll-wrapper {
    width: 100%;
    overflow: auto;
    overscroll-behavior: auto;

    /* Smooth scrolling for touch devices */
    -webkit-overflow-scrolling: touch;

    /* A cleaner, thinner scrollbar */
    scrollbar-width: thin;
    scrollbar-color: rgba(var(--chart-tone), 0.25) transparent;
}

.chart-scroll-wrapper::-webkit-scrollbar {
    height: 8px;
}

.chart-scroll-wrapper::-webkit-scrollbar-track {
    background: transparent;
}

.chart-scroll-wrapper::-webkit-scrollbar-thumb {
    border-radius: 4px;
    background-color: rgba(var(--chart-tone), 0.25);
}

/* Canvas wrapper: holds the Chartist host; tooltips are positioned inside it */
.canvas-wrapper {
    position: relative;
    width: 100%;

    /* Consume the CSS variables for size and scroll-triggering minimums */
    height: var(--chart-height, 400px);
    min-width: var(--min-width, 0);
    min-height: var(--min-height, 0);
}

/* Chartist host: fills the wrapper; the SVG is rebuilt on every render */
.ct-chart {
    --ct-label-color: #5a6478;
    --ct-label-size: 0.8125rem;
    --ct-grid-color: #e9edf5;
    --ct-grid-dasharray: 3px 6px;
    --ct-bar-width: 16px;
    position: relative;
    width: 100%;
    height: 100%;
}

.ct-chart svg {
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
    touch-action: pan-y;
    transition: opacity 0.35s ease;
}

.ct-chart .ct-label {
    font-weight: 500;
    letter-spacing: 0;
}

/* Chartist anchors value-axis labels to the bottom of their tick band, so
   the text floats above its gridline. Nudge it half a line down to centre it
   on the tick. Category labels of horizontal bars are already centred. */
.ct-chart-bar:not(.ct-horizontal-bars) .ct-label.ct-vertical,
.ct-chart-line .ct-label.ct-vertical {
    transform: translateY(0.5em);
}

/* Chartist series variables are per-series; keep numbers aligned */
.ct-chart .ct-label.ct-vertical {
    font-variant-numeric: tabular-nums;
}

/* Bar chart: fuller color than the stock 0.75, rounded ends. The engine
   insets each line by half the bar width, so the cap lands on the value and
   the baseline instead of sticking out of the plot. */
.ct-chart .ct-bar {
    stroke-opacity: 0.88;
    stroke-linecap: round;
    transition: stroke-opacity 0.18s ease;
}

/* Zero bars are hidden (a zero-length round line would be a stray dot) but
   stay focusable for keyboard users. */
.ct-chart .ct-bar.is-zero {
    stroke-opacity: 0;
}

.ct-chart .ct-bar.is-zero:focus-visible {
    stroke-opacity: 1;
}

/* Donut rings and pie fills: slightly richer than stock */
.ct-chart .ct-slice-donut {
    stroke-opacity: 0.95;
    transition: stroke-opacity 0.18s ease, filter 0.18s ease;
}

.ct-chart .ct-slice-pie {
    fill-opacity: 0.92;
    transition: fill-opacity 0.18s ease, filter 0.18s ease;
}

.ct-chart .is-hidden {
    display: none;
}

/* Filtered bar series fade out as a clone while the rest re-centre */
.ct-series-ghost {
    pointer-events: none;
}

/* Snapshot used to crossfade a donut when its legend filters change */
.ct-chart-snapshot {
    z-index: 1;
    transform-origin: center;
}

/* Crowded category labels: rotate instead of wrapping mid-word.
   Chartist puts each label in a box as wide as its category step, with the
   bar centred in that box. Rotating around the box's right edge therefore
   anchors the label half a step right of the bar, and its angled text ends
   up over the plot. `translateX(-50%)` re-centres the rotation origin on the
   bar, and `direction: rtl` keeps the text's right end at that origin even
   when the label is wider than its box (long role/unit names), so every
   label swings down-left from its own tick instead of into the chart. */
.ct-chart.is-dense .ct-horizontal.ct-label {
    display: inline-block;
    direction: rtl;
    text-align: right;
    white-space: nowrap;
    transform-box: border-box;
    transform: translateX(-50%) rotate(-45deg);
    transform-origin: right top;
}

/* Realtime: the samples fade briefly while the chart redraws */
.ct-chart.is-realtime svg {
    opacity: 0.6;
}

/* Keyboard focus: emphasise the focused node, never remove the outline silently */
.ct-chart [role="graphics-symbol"]:focus-visible {
    outline: none;
    stroke-width: 14px;
    filter: drop-shadow(0 0 5px rgba(var(--chart-tone), 0.55));
}

/* Legend under the chart */
.chart-legend {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.4em 0.55em;
    margin: 0.85em 0 0;
    padding: 0;
    font-size: 0.78rem;
    color: var(--chart-muted);
}

.chart-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.45em;
    padding: 0.28em 0.65em;
    border: 1px solid transparent;
    border-radius: 999px;
    background: rgba(var(--chart-tone), 0.06);
    font: inherit;
    color: inherit;
}

button.chart-legend-item {
    cursor: pointer;
    transition: background 0.18s ease, border-color 0.18s ease, opacity 0.18s ease, transform 0.12s ease;
}

button.chart-legend-item:hover {
    border-color: rgba(var(--chart-tone), 0.22);
    background: rgba(var(--chart-tone), 0.12);
}

button.chart-legend-item:active {
    transform: scale(0.96);
}

button.chart-legend-item:focus-visible {
    outline: 2px solid rgb(var(--chart-tone));
    outline-offset: 2px;
}

button.chart-legend-item.is-off {
    opacity: 0.55;
    background: rgba(var(--chart-tone), 0.03);
    text-decoration: line-through;
}

button.chart-legend-item.is-off .chart-legend-swatch {
    filter: grayscale(1);
}

.chart-legend-swatch {
    display: inline-block;
    flex: none;
    width: 0.7em;
    height: 0.7em;
    border-radius: 50%;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.85);
}

/* Label filters (opt-in `labelFilter` feature): category labels and pie/donut
   legend entries act as filter buttons. The dotted underline is the quiet
   affordance; hover/focus tint with the chart's theme tone. */
.ct-chart .ct-label.is-filter-label {
    cursor: pointer;
    text-decoration: underline dotted;
    text-decoration-color: rgba(var(--chart-tone), 0.45);
    text-underline-offset: 0.2em;
}

@media (hover: hover) and (pointer: fine) {
    .ct-chart .ct-label.is-filter-label:hover {
        fill: rgb(var(--chart-tone));
        color: rgb(var(--chart-tone));
    }
}

.ct-chart .ct-label.is-filter-label:focus-visible {
    outline: 2px solid rgb(var(--chart-tone));
    outline-offset: 1px;
}

/* When the payload marks which label is applied (`labelFilter.active`), the
   applied one highlights and the other filterable labels mute + strike — the
   same vocabulary as the externalLegend `is-off` state — so the filtered-out
   options stay visible and clickable to switch the filter. */
.ct-chart .ct-label.is-filter-out {
    opacity: 0.55;
    text-decoration: line-through;
}

.ct-chart .ct-label.is-filter-active {
    fill: rgb(var(--chart-tone));
    color: rgb(var(--chart-tone));
    font-weight: 600;
}

.chart-legend-item.is-filter-out {
    opacity: 0.55;
    background: rgba(var(--chart-tone), 0.03);
    text-decoration: line-through;
}

.chart-legend-item.is-filter-out .chart-legend-swatch {
    filter: grayscale(1);
}

.chart-legend-item.is-filter-active {
    border-color: rgba(var(--chart-tone), 0.45);
    background: rgba(var(--chart-tone), 0.16);
    color: rgb(var(--chart-tone));
    font-weight: 600;
}

/* --- Chart system messages --- */
.chart-loading-msg,
.chart-error-msg {
    grid-column: span 12; /* Take up full width of the grid */
    padding: 40px;
    border: 1px solid var(--chart-border);
    border-radius: var(--chart-radius);
    background: var(--chart-surface);
    text-align: center;
    font-family: inherit;
}

.chart-loading-msg {
    color: var(--chart-muted);
    font-style: italic;
}

.chart-error-msg {
    color: #DC3545;
    font-weight: 600;
}

.chart-update-spinner {
    display: none; /* Hidden by default */
    position: absolute;
    top: 1.35em;
    right: 1.35em;
    width: 18px;
    height: 18px;
    border: 2.5px solid rgba(var(--chart-tone), 0.18);
    border-top-color: rgb(var(--chart-tone));
    border-radius: 50%;
    animation: spin 1s ease-in-out infinite;
    z-index: 5;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Pointer devices: lift the card and full-opacity the hovered column/slice */
@media (hover: hover) and (pointer: fine) {
    .chart-container:hover {
        transform: translateY(-2px);
        border-color: rgba(var(--chart-tone), 0.28);
        box-shadow:
            0 0.25em 0.6em rgba(var(--text-color, 31, 41, 55), 0.05),
            0 0.9em 2em rgba(var(--text-color, 31, 41, 55), 0.09);
    }

    .ct-chart .ct-bar:not(.is-zero):hover {
        stroke-opacity: 1;
    }

    .ct-chart .ct-slice-donut:hover,
    .ct-chart .ct-slice-pie:hover {
        stroke-opacity: 1;
        fill-opacity: 1;
        filter: drop-shadow(0 0 6px var(--ct-color, rgba(78, 97, 160, 0.65)));
    }
}

/* Touch screens have no hover: drop the sticky lift and hover-only depth */
@media (hover: none) {
    .chart-container:hover {
        transform: none;
        border-color: var(--chart-border);
        box-shadow: var(--chart-shadow);
    }
}

/* =========================================
   Mobile responsiveness
   ========================================= */
@media (max-width: 50em) {
    .chart-container {
        /* Swap to mobile variables */
        grid-column: span var(--mobile-col-span, 12) !important;
        padding: 1.1em 1.1em 1em;
    }

    .canvas-wrapper {
        /* Swap to mobile variables */
        height: var(--mobile-chart-height, 300px) !important;
        min-width: var(--mobile-min-width, 0) !important;
        min-height: var(--mobile-min-height, 0) !important;
    }

    .chart-title {
        font-size: 1.05em;
    }

    .ct-chart {
        --ct-label-size: 0.625rem;
        --ct-bar-width: 12px;
    }

    .ct-chart.is-rotated-labels .ct-horizontal.ct-label {
        display: inline-block;
        direction: rtl;
        text-align: right;
        white-space: nowrap;
        transform-box: border-box;
        transform: translateX(-50%) rotate(-40deg);
        transform-origin: right top;
    }
}

/* ---- Chart extensions (static/base/js/chart_engine.js, features) ---- */

/* Hover tooltip: one reusable element per chart */
.ct-tooltip {
    position: absolute;
    z-index: 6;
    pointer-events: none;
    min-width: 9.5em;
    max-width: 18em;
    padding: 0.55em 0.75em;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 10px;
    background: rgba(23, 29, 45, 0.92);
    color: #ffffff;
    font-size: 0.8125rem;
    line-height: 1.4;
    box-shadow: 0 0.6em 1.4em rgba(15, 23, 42, 0.28);
    backdrop-filter: blur(6px);
    animation: ct-tooltip-in 0.12s ease-out;
    will-change: left, top;
}

.ct-tooltip[hidden] {
    display: none;
}

.ct-tooltip-title {
    margin-bottom: 0.3em;
    color: rgba(255, 255, 255, 0.72);
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.ct-tooltip-row {
    display: flex;
    align-items: center;
    gap: 0.45em;
    min-width: 0;
}

.ct-tooltip-row + .ct-tooltip-row {
    margin-top: 0.15em;
}

.ct-tooltip-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ct-tooltip-row strong {
    font-variant-numeric: tabular-nums;
    font-weight: 700;
}

.ct-tooltip-swatch {
    flex: none;
    width: 0.6em;
    height: 0.6em;
    border-radius: 50%;
}

@keyframes ct-tooltip-in {
    from { opacity: 0; transform: translateY(2px); }
    to { opacity: 1; transform: none; }
}

/* Crosshair and reference / threshold lines */
.ct-crosshair {
    stroke: rgba(var(--chart-tone), 0.4);
    stroke-width: 1px;
    stroke-dasharray: 4 3;
    pointer-events: none;
}

.ct-reference {
    stroke: #DC3545;
    stroke-width: 1.5px;
    stroke-dasharray: 6 4;
}

.ct-reference-label {
    fill: #DC3545;
    font-size: 0.6875rem;
    font-weight: 600;
}

/* Custom markers and pulsing rings */
.ct-marker {
    fill: var(--ct-color, #4E61A0);
    stroke: var(--ct-color, #4E61A0);
    stroke-width: 2px;
}

.ct-marker-ring {
    fill: #ffffff;
}

.ct-marker-star {
    stroke: none;
}

.ct-pulse-ring {
    fill: none;
    stroke: var(--ct-color, #4E61A0);
    stroke-width: 2px;
    transform-box: fill-box;
    transform-origin: center;
    animation: ct-pulse 1.8s ease-out infinite;
}

@keyframes ct-pulse {
    0% { transform: scale(1); opacity: 0.8; }
    100% { transform: scale(3); opacity: 0; }
}

/* Motion is decoration: drop it when the user asks for less */
@media (prefers-reduced-motion: reduce) {
    .chart-container,
    .ct-chart .ct-bar,
    .ct-chart .ct-slice-donut,
    .ct-chart .ct-slice-pie,
    button.chart-legend-item,
    .ct-chart svg {
        transition: none;
    }

    .chart-container {
        animation: none;
    }

    .chart-container:hover {
        transform: none;
    }

    .ct-tooltip {
        animation: none;
    }

    .ct-chart .ct-slice-donut:hover,
    .ct-chart .ct-slice-pie:hover {
        filter: none;
    }

    .ct-pulse-ring {
        animation: none;
    }
}
