/*
 * Chart interaction layer — HBS review items 1 and 2.
 *
 * Loaded after styles.css, which holds the charts' own appearance. Nothing
 * here restyles a chart; it adds the tooltip, the emphasis state and the entry
 * animation, so the two concerns stay separable and this file can be removed
 * without taking the charts with it.
 *
 * The reference the review named (UNESCWA's Social Expenditure Monitor) runs
 * Apache ECharts with its stock palette. The behaviour below is modelled on
 * it; the colours deliberately are not — they stay True Cost of Debt's.
 */

/* --- tooltip ------------------------------------------------------------ */

/*
 * Positioned by transform rather than top/left: it moves on every pointer
 * event, and transform is the only one of those the compositor can do without
 * a layout pass.
 */
.debt-chart-tip {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 4;
    pointer-events: none;
    max-width: 17rem;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.3rem 0.9rem;
    padding: 0.5rem 0.7rem;
    border-radius: 7px;
    border: 1px solid var(--chart-border-lit);
    background: #0C1430;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
    font-family: 'Inter', sans-serif;
    font-size: var(--t-xs);
    line-height: 1.35;
    color: var(--chart-text);
    will-change: transform;
    transition: transform 120ms cubic-bezier(0.22, 0.61, 0.36, 1);
}

.debt-chart-tip[hidden] { display: none; }

.debt-chart-tip .ro-period {
    flex-basis: 100%;
    font-weight: 700;
    color: var(--chart-heading);
    letter-spacing: 0.02em;
}

.debt-chart-tip .ro-unit {
    flex-basis: 100%;
    color: var(--chart-muted);
    font-size: var(--t-xs);
}

/*
 * The readout keeps its flex rules from styles.css, which include a min-height
 * that would otherwise hold a 24px gap open inside a box meant to measure 1px.
 */
.debt-chart-readout.visually-hidden { min-height: 0; }

/* --- emphasis ----------------------------------------------------------- */

/*
 * Opacity only. De-emphasising by changing a colour would make the legend
 * swatch disagree with the line it names.
 */
.c-series,
.c-bar,
.debt-chart-legend-item {
    transition: opacity 140ms ease-out;
}

.c-series.is-dimmed,
.c-bar.is-dimmed { opacity: 0.22; }

.debt-chart-legend-item.is-dimmed { opacity: 0.45; }

.debt-chart-legend-item { cursor: default; }

/* --- entry animation ---------------------------------------------------- */

/*
 * Added by charts.js when the chart first scrolls into view, never on load, so
 * a chart below the fold still animates when it is actually seen.
 *
 * --c-line-length is measured from the path in JavaScript: CSS cannot ask an
 * SVG path how long it is.
 */
.debt-chart.is-entering .c-line:not(.c-line--reference) {
    stroke-dasharray: var(--c-line-length);
    stroke-dashoffset: var(--c-line-length);
    animation: c-draw 700ms cubic-bezier(0.22, 0.61, 0.36, 1) forwards;
}

@keyframes c-draw {
    to { stroke-dashoffset: 0; }
}

/*
 * transform-box: fill-box makes the origin the bar's own box rather than the
 * SVG's, which is what lets a bar grow from its baseline instead of from the
 * top-left of the chart.
 */
.debt-chart.is-entering .c-bar,
.debt-chart.is-entering .c-hbar {
    transform-box: fill-box;
    animation: c-grow 520ms cubic-bezier(0.22, 0.61, 0.36, 1) backwards;
    animation-delay: calc(min(var(--c-index, 0) * 22, 400) * 1ms);
}

.debt-chart.is-entering .c-bar {
    transform-origin: bottom;
}

.debt-chart.is-entering .c-hbar {
    transform-origin: left;
}

@keyframes c-grow {
    from { transform: scale(0); }
    to { transform: scale(1); }
}

/* The printed end-of-series values arrive after the line that leads to them. */
.debt-chart.is-entering .c-callout {
    animation: c-fade 400ms ease-out 560ms backwards;
}

@keyframes c-fade {
    from { opacity: 0; }
}

/* --- reduced motion ----------------------------------------------------- */

/*
 * Switched off rather than shortened. charts.js also skips the observer
 * entirely under this query, so `is-entering` is never applied; this is the
 * belt to that braces, and covers the tooltip's own easing too.
 */
@media (prefers-reduced-motion: reduce) {
    .debt-chart-tip { transition: none; }

    .c-series,
    .c-bar,
    .debt-chart-legend-item,
    .debt-chart-action { transition: none; }

    .debt-chart.is-entering .c-line:not(.c-line--reference),
    .debt-chart.is-entering .c-bar,
    .debt-chart.is-entering .c-hbar,
    .debt-chart.is-entering .c-callout {
        animation: none;
        stroke-dasharray: none;
        stroke-dashoffset: 0;
    }
}

/* --- card header actions ------------------------------------------------ */

/*
 * Export on the card, as the reference dashboard has it, rather than in the
 * footer where it was. Same routes, same files; only the position changed.
 */
.debt-chart-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem 1rem;
    flex-wrap: wrap;
}

.debt-chart-heading { min-width: 0; }

.debt-chart-actions {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    flex-shrink: 0;
    /* Sits beside the per-chart control, which is also in the header. */
    margin-left: auto;
}

.debt-chart-action {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    /* 32px tall at the smallest; the row sits inside a 44px band on touch. */
    min-height: 2rem;
    padding: 0.3rem 0.55rem;
    border: 1px solid var(--chart-border);
    border-radius: 999px;
    background: transparent;
    font-family: 'Inter', sans-serif;
    font-size: var(--t-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    color: var(--chart-muted);
    text-decoration: none;
    cursor: pointer;
    transition: color 120ms ease-out, border-color 120ms ease-out,
        background-color 120ms ease-out;
}

.debt-chart-action svg {
    width: 13px;
    height: 13px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.debt-chart-action:hover,
.debt-chart-action:focus-visible {
    color: var(--chart-accent);
    border-color: var(--chart-border-lit);
    background: var(--chart-surface);
}

.debt-chart-action[disabled] { opacity: 0.5; cursor: progress; }

/*
 * 44px on touch. A 32px pill is comfortable with a mouse and a miss with a
 * thumb, and these sit in a row three abreast.
 */
@media (pointer: coarse) {
    .debt-chart-action {
        min-height: 2.75rem;
        padding-left: 0.85rem;
        padding-right: 0.85rem;
    }
}

/*
 * The ground the exported PNG is painted on. Declared here so the colour is a
 * design decision in the stylesheet rather than a constant in the script.
 */
.debt-chart {
    /* The record pass — a chart is content, not a control. */
    border-radius: var(--r-none); --chart-export-bg: #0D1430; }

/* --- how this is measured ----------------------------------------------- */

/*
 * Always present, unlike the amber notes, which fire only when the import
 * finds something wrong in the source. Kept visually quieter than those for
 * the same reason: this is reference, those are warnings.
 */
.debt-chart-method {
    margin: 0.7rem 0 0;
    padding-left: 0.7rem;
    border-left: 2px solid var(--chart-border);
}

.debt-chart-method-title {
    margin: 0 0 0.15rem;
    font-family: 'Inter', sans-serif;
    font-size: var(--t-xs);
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--chart-muted);
}

.debt-chart-method-body {
    max-width: var(--measure);
    margin: 0;
    font-family: 'Inter', sans-serif;
    font-size: var(--t-xs);
    line-height: 1.5;
    color: var(--chart-text);
}

/* --- abbreviations ------------------------------------------------------ */

.debt-chart-abbr {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 0.25rem 0.5rem;
    margin: 0.6rem 0 0;
    font-family: 'Inter', sans-serif;
    font-size: var(--t-xs);
    color: var(--chart-muted);
}

.debt-chart-abbr svg {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
    align-self: center;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
}

.debt-chart-abbr abbr {
    font-weight: 700;
    color: var(--chart-text);
    text-decoration: none;
}

.debt-chart-downloads-note {
    margin: 0;
    font-family: 'Inter', sans-serif;
    font-size: var(--t-xs);
    font-style: italic;
    color: var(--chart-muted);
}


/*
 * DESIGN-02 — the measure on the generated notes.
 *
 * The class is `.debt-chart-notes`, not `.debt-chart-footnotes`. The first
 * version of this rule guessed the name from the template variable and
 * silently matched nothing, which a measurement caught and reading would
 * not have: those list items were still running 124 characters.
 */
.debt-chart-notes li,
.debt-chart-notes p {
    max-width: var(--measure);
}

/* The record pass — a chart is content; its download buttons are not. */
.debt-chart-action,
.debt-chart-option select { border-radius: var(--r-control); }
