
/* ===========================================================================
   ee_charts.html -- the EE data-visualisation plane.

   WHY THIS EXISTS. EE summary cards were counter chrome: a coloured header, a
   number, and a table that is empty for most records. The owner's objection is
   exactly that ("all widgets should have graphs or tables right these are
   ugly"). This is the ExactOperator sparkline plane ported into EE so a summary
   card can show the SHAPE of the figures it already prints, not just the
   figures. It visualises values that are ALREADY on the page -- it never
   invents a series.

   PORTED FROM, not copied from:
     /root/ExactOperator/src/exactoperator/web/app.js:3724-3750  sparklineSvg()
     /root/ExactOperator/src/exactoperator/web/app.css:2200-2263 .ops-sparkline

   THE PORT IS AN INVERSION, AND THE INVERSION IS MEASURED. EO's chrome is dark,
   so it draws its surface with background rgba(255,255,255,0.03) and border
   rgba(255,255,255,0.07) -- white films that are INVISIBLE on EE's white card
   (--ee-bg-card: #ffffff). Worse, EO's stroke tones map onto EE tokens that do
   not survive a light background. Contrast ratios measured against the real
   token values in ee_tokens.html, WCAG relative luminance:

       tone            hex        vs #ffffff   vs #1e293b (EE dark card)
       --ee-teal       #14b8a6       2.49         5.88
       --ee-success    #10b981       2.54         5.77
       --ee-warning    #f59e0b       2.15         6.81
       --ee-danger     #ef4444       3.76         3.89
       --ee-indigo     #3f51b5       6.87         2.13

   Three of the five FAIL the 3:1 floor for non-text graphics on a white card,
   and indigo fails on the dark card. A naive port would therefore have shipped
   washed-out lines -- the very defect being fixed. So the tone tokens SWITCH by
   mode: light mode strokes with the -dark ramp (teal #0d9488 = 3.74,
   success #059669 = 3.77, warning #b45309 = 5.02, danger #dc2626 = 4.83,
   indigo #3f51b5 = 6.87), dark mode strokes with the bright ramp that measures
   5.77-6.81 there. Every one of those clears 3:1 in the mode it is used in.

   Do not "simplify" these to the bare --ee-teal / --ee-success tokens. That is
   the regression this comment exists to prevent.
   =========================================================================== */

:root {
    /* Chart surface: a slate tint, NOT a white film. On a white card a white
       film is nothing at all -- that is EO's dark-chrome assumption leaking. */
    --ee-chart-surface: #f8fafc;
    --ee-chart-border: #e2e8f0;
    --ee-chart-grid: #eef2f6;

    /* Light-mode stroke ramp. Measured >= 3.74:1 on #ffffff. */
    --ee-chart-accent: #3f51b5;
    --ee-chart-teal: #0d9488;
    --ee-chart-success: #059669;
    --ee-chart-warning: #b45309;
    --ee-chart-danger: #dc2626;

    --ee-chart-area-opacity: 0.16;
    --ee-chart-draw-ms: 900ms;
}

/* Dark is the person's own choice only: `.ee-dark` on <html>/<body>, set by the
   toolbar's Theme control and the Angular ThemeService (light by default, owner
   2026-11-08: "why is the page dark?"), never the device's colour scheme. The
   Django admin's own toggle sets [data-theme="dark"]. Dark returns to the bright
   ramp, which measures 5.77-6.81:1 on the #1e293b card while the light ramp
   would sink into it. */
.ee-dark,
[data-theme="dark"] {
    --ee-chart-surface: rgba(255, 255, 255, 0.04);
    --ee-chart-border: rgba(255, 255, 255, 0.10);
    --ee-chart-grid: rgba(255, 255, 255, 0.08);
    --ee-chart-accent: #818cf8;
    --ee-chart-teal: #14b8a6;
    --ee-chart-success: #10b981;
    --ee-chart-warning: #f59e0b;
    --ee-chart-danger: #ef4444;
    --ee-chart-area-opacity: 0.22;
}

/* --------------------------------------------------------------- sparkline */

.ee-sparkline {
    width: 100%;
    height: 56px;
    border-radius: var(--ee-radius-lg, 12px);
    background: var(--ee-chart-surface);
    border: 1px solid var(--ee-chart-border);
    padding: 4px;
    box-sizing: border-box;
}

.ee-sparkline svg { width: 100%; height: 100%; display: block; }

.ee-sparkline-line {
    fill: none;
    stroke-width: 2.4;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke: var(--ee-chart-accent);
}

.ee-sparkline-area {
    opacity: var(--ee-chart-area-opacity);
    fill: var(--ee-chart-accent);
}

/* TONE CLASSES ARE NAMESPACED, AND THAT IS NOT STYLE PEDANTRY.
   ExactOperator writes these as bare words -- `.ops-sparkline.accent`,
   `.ops-sparkline.warm` -- which is safe inside EO, whose stylesheet is the
   only one on the page. EE is not that. EE still carries legacy Materialize
   colour utilities, so a bare `.teal` already means something here.
   Measured on staging (unit 170) with the first version of this file: a row
   rendered as a div carrying ee-bar-row plus the bare word teal picked up
   background-color rgb(0,150,136) -- Materialize's #009688 -- and painted a
   solid teal block across the whole row, label and value included, with the
   track sitting on top of it. It looked exactly like a broken layout, and the
   geometry was in fact perfect: getBoundingClientRect reported the track at
   x=1217.8 w=337.2 and the fill at w=209.1 (62%), precisely right. Two hours
   could go into "fixing" a layout that was never wrong.
   `.ee-bar-row.accent` was also picking up color rgb(255,68,0) from a foreign
   `.accent`. Hence `ee-tone-*`. Do not shorten these back. */
.ee-sparkline.ee-tone-teal    .ee-sparkline-line { stroke: var(--ee-chart-teal); }
.ee-sparkline.ee-tone-teal    .ee-sparkline-area { fill:   var(--ee-chart-teal); }
.ee-sparkline.ee-tone-success .ee-sparkline-line { stroke: var(--ee-chart-success); }
.ee-sparkline.ee-tone-success .ee-sparkline-area { fill:   var(--ee-chart-success); }
.ee-sparkline.ee-tone-warm    .ee-sparkline-line { stroke: var(--ee-chart-warning); }
.ee-sparkline.ee-tone-warm    .ee-sparkline-area { fill:   var(--ee-chart-warning); }
.ee-sparkline.ee-tone-danger  .ee-sparkline-line { stroke: var(--ee-chart-danger); }
.ee-sparkline.ee-tone-danger  .ee-sparkline-area { fill:   var(--ee-chart-danger); }

/* An absent series says so. It does NOT draw a flat line at zero, because a
   flat line reads as a real measurement of nothing happening. */
.ee-sparkline.empty {
    display: grid;
    place-items: center;
    color: var(--ee-text-muted, #64748b);
    font-size: var(--ee-text-xs, 0.75rem);
    letter-spacing: 0.02em;
}

/* ------------------------------------------------- labelled bar comparison */

/* For the common EE case that is NOT a time series: three or four currency
   figures already printed on a card (a cost breakdown, a pricing summary).
   A line chart over non-sequential categories would be a lie about the data;
   a labelled bar is the honest form. */
.ee-barchart {
    display: flex;
    flex-direction: column;
    gap: var(--ee-space-2, 8px);
    width: 100%;
}

.ee-bar-row {
    display: grid;
    grid-template-columns: minmax(84px, 30%) 1fr auto;
    align-items: center;
    gap: var(--ee-space-3, 12px);
    font-size: var(--ee-text-xs, 0.75rem);
}

.ee-bar-label {
    color: var(--ee-text-muted, #64748b);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ee-bar-track {
    position: relative;
    height: 10px;
    border-radius: var(--ee-radius-full, 9999px);
    background: var(--ee-chart-grid);
    overflow: hidden;
}

.ee-bar-fill {
    position: absolute;
    inset: 0 auto 0 0;
    width: 0;
    border-radius: inherit;
    background: var(--ee-chart-accent);
}

.ee-bar-row.ee-tone-teal    .ee-bar-fill { background: var(--ee-chart-teal); }
.ee-bar-row.ee-tone-success .ee-bar-fill { background: var(--ee-chart-success); }
.ee-bar-row.ee-tone-warm    .ee-bar-fill { background: var(--ee-chart-warning); }
.ee-bar-row.ee-tone-danger  .ee-bar-fill { background: var(--ee-chart-danger); }

/* A credit reads as a credit: hatched rather than solid, in the danger tone,
   with the value coloured to match. The stripe is what carries the meaning at
   a glance -- colour alone would fail for a red-green colour-blind reader, and
   the sign in the value is too small to be the only signal. */
.ee-bar-row.ee-bar-negative .ee-bar-fill {
    background: repeating-linear-gradient(
        135deg,
        var(--ee-chart-danger) 0,
        var(--ee-chart-danger) 4px,
        transparent 4px,
        transparent 8px);
    border: 1px solid var(--ee-chart-danger);
    box-sizing: border-box;
}

.ee-bar-row.ee-bar-negative .ee-bar-value {
    color: var(--ee-chart-danger);
}

.ee-bar-value {
    font-variant-numeric: tabular-nums;
    font-weight: 700;
    color: var(--ee-text-primary, #0f172a);
    white-space: nowrap;
}

.ee-chart-empty {
    display: grid;
    place-items: center;
    min-height: 44px;
    padding: var(--ee-space-2, 8px);
    border-radius: var(--ee-radius-md, 8px);
    background: var(--ee-chart-surface);
    border: 1px dashed var(--ee-chart-border);
    color: var(--ee-text-muted, #64748b);
    font-size: var(--ee-text-xs, 0.75rem);
    font-style: italic;
}

/* ------------------------------------------------------------------ frames */

.ee-chart-frame {
    display: flex;
    flex-direction: column;
    gap: var(--ee-space-2, 8px);
    padding: var(--ee-space-3, 12px);
    border: 1px solid var(--ee-chart-border);
    border-radius: var(--ee-radius-lg, 12px);
    background: var(--ee-bg-card, #ffffff);
}

/* Inside an existing dashboard section that already supplies its own outer
   padding, the frame only needs to separate itself from the grid above it. */
.ee-chart-frame-tight {
    margin-top: var(--ee-space-3, 12px);
    padding: var(--ee-space-3, 12px);
}

.ee-chart-caption {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--ee-space-2, 8px);
    font-size: var(--ee-text-xs, 0.75rem);
    color: var(--ee-text-muted, #64748b);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}

/* Two charts side by side on a wide card, stacked on a narrow one. auto-fit
   with a 260px floor rather than a fixed two-column grid, because a bar chart
   squeezed under 260px loses its labels to the ellipsis and reads as chrome. */
.ee-chart-pair {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: var(--ee-space-3, 12px);
    margin-top: var(--ee-space-3, 12px);
}

.ee-chart-caption strong {
    font-size: var(--ee-text-lg, 1.125rem);
    color: var(--ee-text-primary, #0f172a);
    text-transform: none;
    letter-spacing: normal;
    font-variant-numeric: tabular-nums;
}

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

/* Draw-on. The line is dashed to its own length and the dash offset is walked
   to zero, so the stroke appears to be drawn rather than to pop. The area
   fades under it a beat later. Both are driven by a class the renderer adds,
   so a chart that is never rendered never animates into a half state. */
.ee-sparkline-line {
    stroke-dasharray: var(--ee-spark-len, 1000);
    stroke-dashoffset: var(--ee-spark-len, 1000);
}

.ee-chart-animate .ee-sparkline-line {
    animation: ee-spark-draw var(--ee-chart-draw-ms) cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.ee-chart-animate .ee-sparkline-area {
    opacity: 0;
    transform-origin: bottom;
    animation: ee-spark-fill 520ms ease-out 320ms forwards;
}

.ee-chart-animate .ee-bar-fill {
    animation: ee-bar-grow 720ms cubic-bezier(0.22, 1, 0.36, 1) forwards;
    animation-delay: calc(var(--ee-bar-i, 0) * 90ms);
}

/* ONCE SETTLED, DROP THE ANIMATION AND KEEP THE FINAL FRAME STATICALLY.
   An `animation-fill-mode: forwards` rule holds the element on its own
   compositor layer indefinitely after the animation ends. Measured: with the
   layer still live, Chromium's ELEMENT screenshot composited a finished bar at
   the wrong offset -- the Monthly Subsidy fill painted across its own label --
   while getBoundingClientRect for the same element reported the correct
   geometry (track x=1217.8 w=337.2, fill w=209.1, 62%). The pixels disagreed
   with the layout, so this was never a layout bug and no amount of waiting
   fixed it. Retiring the animation once the chart has settled returns these to
   ordinary painted elements. It also means a print or a PDF export gets the
   finished chart rather than a frame of one. */
[data-ee-charts-settled="1"] .ee-sparkline-line {
    animation: none;
    stroke-dasharray: none;
    stroke-dashoffset: 0;
}

[data-ee-charts-settled="1"] .ee-sparkline-area {
    animation: none;
    opacity: var(--ee-chart-area-opacity);
    transform: none;
}

[data-ee-charts-settled="1"] .ee-bar-fill {
    animation: none;
    width: var(--ee-bar-w, 0%);
}

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

@keyframes ee-spark-fill {
    from { opacity: 0; transform: scaleY(0.72); }
    to   { opacity: var(--ee-chart-area-opacity); transform: scaleY(1); }
}

@keyframes ee-bar-grow { to { width: var(--ee-bar-w, 0%); } }

/* Reduced motion is not a downgrade path, it is the same chart with the final
   frame as the only frame. Without this branch the draw-on is an
   accessibility defect, not a flourish. */
@media (prefers-reduced-motion: reduce) {
    .ee-sparkline-line { stroke-dasharray: none; stroke-dashoffset: 0; }
    .ee-chart-animate .ee-sparkline-line,
    .ee-chart-animate .ee-sparkline-area,
    .ee-chart-animate .ee-bar-fill {
        animation: none;
    }
    .ee-chart-animate .ee-sparkline-area { opacity: var(--ee-chart-area-opacity); }
    .ee-chart-animate .ee-bar-fill { width: var(--ee-bar-w, 0%); }
}
