/* Interactive charts (chart.js + chart-interact.js). Colours only through tokens. */

.ichart-tone--brand { --tone: var(--brand); }
.ichart-tone--violet { --tone: var(--accent-2); }
.ichart-tone--orange { --tone: var(--accent-1); }
.ichart-tone--green { --tone: var(--success); }

.ichart {
  position: relative;
  display: block;
  width: 100%;
}

/* ---------- header: legend (also the series switches) + readout ---------- */

.ichart__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2) var(--s-4);
  min-height: 30px;
  margin-bottom: var(--s-3);
}

.ichart__legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.ichart__legend-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 11px 4px 9px;
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: var(--fs-xs);
  line-height: 1.3;
  cursor: pointer;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease), color var(--dur) var(--ease);
}

.ichart__legend-item:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: var(--surface-2);
}

.ichart__legend-item:disabled {
  cursor: default;
}

.ichart__legend-item.is-off {
  color: var(--text-faint);
}

.ichart__legend-item.is-off .ichart__swatch {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--text-faint);
}

.ichart__legend-val {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.ichart__legend-val:empty {
  display: none;
}

.ichart__swatch {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: var(--r-full);
  background: var(--tone);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--tone) 22%, transparent);
}

.ichart__readout {
  margin-left: auto;
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ---------- plot ---------- */

.ichart__plot {
  position: relative;
  border-radius: var(--r-md);
  cursor: crosshair;
  touch-action: pan-y; /* a horizontal drag scrubs the chart, a vertical one still scrolls the page */
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.ichart__svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}

.ichart__grid {
  stroke: var(--border);
  stroke-width: 1;
  stroke-dasharray: 2 5;
}

.ichart__grid--base {
  stroke: var(--border-strong);
  stroke-dasharray: none;
}

.ichart__label {
  fill: var(--text-faint);
  font-family: var(--font);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.ichart__line {
  stroke: var(--tone);
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ichart__line.is-lead {
  stroke-width: 2.25;
  filter: drop-shadow(0 0 7px color-mix(in srgb, var(--tone) 55%, transparent));
}

.ichart__stop-area-a { stop-color: var(--tone); stop-opacity: 0.14; }
.ichart__stop-area-a.is-lead { stop-opacity: 0.34; }
.ichart__stop-area-b { stop-color: var(--tone); stop-opacity: 0; }

.ichart__stop-bar-a { stop-color: var(--tone); stop-opacity: 1; }
.ichart__stop-bar-b { stop-color: var(--tone); stop-opacity: 0.55; }

.ichart__stop-cross-a { stop-color: var(--text-muted); stop-opacity: 0.05; }
.ichart__stop-cross-b { stop-color: var(--text-muted); stop-opacity: 0.6; }

.ichart__dot {
  fill: var(--surface-1);
  stroke: var(--tone);
  stroke-width: 2;
}

.ichart__bar {
  transition: filter var(--dur) var(--ease), opacity var(--dur) var(--ease);
}

.ichart__svg:has(.ichart__cursor.is-on) .ichart__bar:not(.is-active) {
  opacity: 0.5;
}

.ichart__bar.is-active {
  filter: brightness(1.2) drop-shadow(0 0 8px color-mix(in srgb, var(--tone) 60%, transparent));
}

/* ---------- cursor: crosshair, glowing points, bar band ---------- */

.ichart__cursor {
  opacity: 0;
  pointer-events: none;
  transform: translate(calc(var(--cx, 0) * 1px), 0);
  transition: opacity 120ms ease-out;
}

.ichart__cursor.is-on { opacity: 1; }

.ichart__cursor.is-glide {
  transition: opacity 120ms ease-out, transform 90ms ease-out;
}

.ichart__cross {
  stroke-width: 1;
}

.ichart__band {
  fill: var(--text);
  opacity: 0.05;
}

.ichart__pt {
  transform: translate(0, calc(var(--cy, 0) * 1px));
}

.ichart__cursor.is-glide .ichart__pt {
  transition: transform 90ms ease-out;
}

.ichart__halo {
  fill: var(--tone);
  opacity: 0.22;
}

.ichart__core {
  fill: var(--surface-1);
  stroke: var(--tone);
  stroke-width: 2.5;
}

/* ---------- tooltip (glass) ---------- */

.ichart__tip {
  position: absolute;
  top: 6px;
  left: 0;
  z-index: 5;
  min-width: 150px;
  max-width: 240px;
  padding: 10px 12px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  background: color-mix(in srgb, var(--surface-2) 84%, transparent);
  -webkit-backdrop-filter: blur(12px) saturate(1.4);
  backdrop-filter: blur(12px) saturate(1.4);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  opacity: 0;
  pointer-events: none;
  transform: translate(calc(var(--x, 0) * 1px + 16px), 4px);
}

.ichart__tip.is-on {
  opacity: 1;
  transform: translate(calc(var(--x, 0) * 1px + 16px), 0);
}

.ichart__tip.is-on.is-flip {
  transform: translate(calc(var(--x, 0) * 1px - 100% - 16px), 0);
}

.ichart__tip.is-glide {
  transition: opacity 120ms ease-out, transform 100ms ease-out;
}

.ichart__tip-title {
  margin-bottom: 7px;
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 500;
}

.ichart__tip-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--fs-sm);
}

.ichart__tip-row + .ichart__tip-row { margin-top: 4px; }

.ichart__tip-label { flex: 1; color: var(--text); }

.ichart__tip-val {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.ichart__tip-delta,
.ichart__tip-note {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: var(--fs-xs);
}

.ichart__tip-delta { padding-top: 8px; border-top: 1px solid var(--border); }
.ichart__tip-delta b { font-variant-numeric: tabular-nums; }
.ichart__tip-delta--up b { color: var(--success); }
.ichart__tip-delta--down b { color: var(--danger); }

.ichart__tip-note { margin-top: 6px; }

.ichart__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- entrance: one draw-in when the chart first appears ---------- */

@media (prefers-reduced-motion: no-preference) {
  .ichart__svg:not(.is-static) .ichart__line {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation: ichart-draw 950ms var(--ease) forwards;
  }

  .ichart__svg:not(.is-static) .ichart__area,
  .ichart__svg:not(.is-static) .ichart__dot {
    opacity: 0;
    animation: ichart-fade 700ms 300ms ease-out forwards;
  }

  .ichart__svg:not(.is-static) .ichart__bar {
    transform-box: fill-box;
    transform-origin: 50% 100%;
    transform: scaleY(0);
    animation: ichart-rise 650ms var(--ease) forwards;
  }
}

@keyframes ichart-draw { to { stroke-dashoffset: 0; } }
@keyframes ichart-fade { to { opacity: 1; } }
@keyframes ichart-rise { to { transform: scaleY(1); } }

@media (max-width: 640px) {
  .ichart__readout { margin-left: 0; white-space: normal; }
}
