/* Daybook charts (js/core/charts.js) — chart chrome, marks, tooltip, legend, donut, heatmap, sparkline.
 *
 * Every color goes through private custom properties resolved from the design tokens (with fallbacks), so a
 * chart can be re-themed locally by setting the public token on any ancestor, e.g. style="--chart-1: …".
 * Optional public overrides (not in tokens.css): --chart-other, --chart-profit, --chart-loss, --chart-empty,
 * --chart-surface (the color of dot rings / label halos — set it when a chart sits on --surface-2).
 *
 * Mark specs follow the dataviz method: 2px lines, ≥8px dots with a 2px surface ring, bars ≤24px with a 4px
 * rounded data-end, 2px surface gaps, hairline solid grid, dashed line only for targets (a threshold).
 * Profit/loss marks use the --profit/--loss tokens directly (the dark --loss is tuned to stay distinct from
 * --profit under deuteranopia). Text never wears the series color.
 */

.chart,
.chart-legend {
  --_c1: var(--chart-1, #2a78d6);
  --_c2: var(--chart-2, #eb6834);
  --_c3: var(--chart-3, #1baf7a);
  --_c4: var(--chart-4, #eda100);
  --_c5: var(--chart-5, #e87ba4);
  --_c6: var(--chart-6, #008300);
  --_c7: var(--chart-7, #6250d6);
  --_c8: var(--chart-8, #e34948);
  --_other: var(--chart-other, #a3a9b7);
  --_profit: var(--chart-profit, var(--profit, #067d4c));
  --_loss: var(--chart-loss, var(--loss, #d0291f));
  --_grid: var(--chart-grid, #eceef3);
  --_axis: var(--chart-axis, #6b7385);
  --_surface: var(--chart-surface, var(--surface, #ffffff));
  --_empty: var(--chart-empty, #ebedf2);
  --_hover: var(--chart-hover, rgba(16, 24, 40, 0.05));
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) :is(.chart, .chart-legend) {
    --_other: var(--chart-other, #5f6677);
    --_empty: var(--chart-empty, #222631);
    --_hover: var(--chart-hover, rgba(255, 255, 255, 0.055));
  }
}
:root[data-theme="dark"] :is(.chart, .chart-legend) {
  --_other: var(--chart-other, #5f6677);
  --_empty: var(--chart-empty, #222631);
  --_hover: var(--chart-hover, rgba(255, 255, 255, 0.055));
}

/* ── Wrapper ─────────────────────────────────────────────────────────────────────────────────── */

.chart {
  position: relative;
  display: block;
  width: 100%;
  min-width: 0;
  color: var(--text-2);
  font-family: var(--font);
  font-size: 11px;
  line-height: 1.35;
  -webkit-tap-highlight-color: transparent;
}
.chart:focus { outline: none; }
.chart:focus-visible {
  outline: 2px solid var(--focus, #5b54f0);
  outline-offset: 4px;
  border-radius: var(--r-sm, 8px);
}
.chart-body {
  position: relative;
  min-width: 0;
  -webkit-user-select: none;
  user-select: none;
}
/* Horizontal drags scrub the crosshair on touch; vertical drags still scroll the page. */
:is(.chart-line, .chart-bar, .chart-stacked, .chart-histogram) .chart-body { touch-action: pan-y; }

.chart-svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}
.chart-svg text { font-family: var(--font); }

/* Screen-reader data table + live region. */
.chart-sr {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ── Axes, grid, reference lines ─────────────────────────────────────────────────────────────── */

.chart-tick {
  fill: var(--_axis);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.chart-grid-line {
  stroke: var(--_grid);
  stroke-width: 1;
  shape-rendering: crispEdges;
}
.chart-axis-line {
  stroke: var(--border-strong, #d2d6df);
  stroke-width: 1;
  shape-rendering: crispEdges;
}
.chart-zero {
  stroke: var(--_axis);
  stroke-opacity: 0.6;
  stroke-width: 1;
  shape-rendering: crispEdges;
}
.chart-target {
  stroke: var(--text-2, #475069);
  stroke-opacity: 0.75;
  stroke-width: 1.25;
  stroke-dasharray: 4 4;
  shape-rendering: crispEdges;
}
.chart-target-label {
  fill: var(--text-2, #475069);
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  paint-order: stroke;
  stroke: var(--_surface);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.chart-value-label {
  fill: var(--text-2, #475069);
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  paint-order: stroke;
  stroke: var(--_surface);
  stroke-width: 3px;
  stroke-linejoin: round;
}
.chart-cat-label {
  fill: var(--text, #101828);
  font-size: 12.5px;
}

/* ── Line / area ─────────────────────────────────────────────────────────────────────────────── */

.chart-line {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chart-area { stroke: none; }
.chart-dot {
  stroke: var(--_surface);
  stroke-width: 2;
}
.chart-crosshair {
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-fast, 150ms) var(--ease, ease-out);
}
.has-active .chart-crosshair { opacity: 1; }
.chart-crosshair-line {
  stroke: var(--_axis);
  stroke-opacity: 0.55;
  stroke-width: 1;
}
.chart-dot-active { stroke-width: 2.5; }

/* ── Bars ────────────────────────────────────────────────────────────────────────────────────── */

.chart-bar {
  transition: opacity var(--dur-fast, 150ms) var(--ease, ease-out), filter var(--dur-fast, 150ms) var(--ease, ease-out);
}
.has-active .chart-bar:not(.is-active) { opacity: 0.5; }
.chart-bar.is-active { filter: saturate(1.1) brightness(1.06); }
.chart-hover-band {
  fill: var(--_hover);
  opacity: 0;
  transition: opacity var(--dur-fast, 150ms) var(--ease, ease-out);
  pointer-events: none;
}
.has-active .chart-hover-band { opacity: 1; }
.chart-bar-h .chart-hover-band { fill: var(--_hover); }
:is(.chart-bar, .chart-histogram, .chart-stacked)[tabindex] .chart-body { cursor: default; }

/* ── Tooltip (one per chart, positioned inside the chart by charts.js) ───────────────────────── */

.chart .chart-tooltip {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 30;
  min-width: 88px;
  max-width: min(280px, 100%);
  padding: 8px 11px 9px;
  border: 1px solid var(--border, #e5e7ed);
  border-radius: 10px;
  background: var(--surface-raised, #fff);
  color: var(--text, #101828);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px -6px rgba(16, 24, 40, 0.18);
  font-size: 12px;
  line-height: 1.35;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 120ms var(--ease, ease-out), transform 140ms var(--ease, ease-out);
  will-change: transform;
}
:root[data-theme="dark"] .chart .chart-tooltip {
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.03), 0 10px 28px -6px rgba(0, 0, 0, 0.65);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .chart .chart-tooltip {
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.03), 0 10px 28px -6px rgba(0, 0, 0, 0.65);
  }
}
.chart .chart-tooltip.is-visible { opacity: 1; }
.chart-tooltip-title {
  margin-bottom: 5px;
  color: var(--text-2, #475069);
  font-size: 11.5px;
  font-weight: 500;
}
.chart-tooltip-rows { display: grid; gap: 3px; }
.chart-tooltip-row {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}
.chart-tooltip-row.is-total {
  margin-top: 3px;
  padding-top: 5px;
  border-top: 1px solid var(--border, #e5e7ed);
}
.chart-tooltip-key {
  flex: none;
  width: 12px;
  height: 3px;
  border-radius: 2px;
  background: var(--key, currentColor);
}
.chart-tooltip-key[data-shape="rect"] {
  width: 9px;
  height: 9px;
  border-radius: 2.5px;
}
.chart-tooltip-key[data-shape="none"] { background: transparent; }
.chart-tooltip-value {
  color: var(--text, #101828);
  font-size: 13px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.005em;
}
.chart-tooltip-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-2, #475069);
}
.chart-tooltip-note {
  max-width: 240px;
  margin-top: 5px;
  color: var(--text-3, #5d6679);
  font-size: 11.5px;
  white-space: normal;
}
.chart-tooltip-rows + .chart-tooltip-note { margin-top: 6px; }
.chart-tooltip-hint {
  margin-top: 6px;
  color: var(--text-3, #5d6679);
  font-size: 11px;
}

/* ── Legend ──────────────────────────────────────────────────────────────────────────────────── */

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin: 0 0 12px;
  padding: 0;
  color: var(--text-2, #475069);
  font-size: 12.5px;
  line-height: 1.3;
}
.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}
.chart-legend-swatch {
  flex: none;
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: var(--sw, var(--_c1));
}
.chart-legend-swatch[data-shape="line"] {
  width: 14px;
  height: 3px;
  border-radius: 2px;
}
.chart-legend-swatch[data-shape="dot"] {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.chart-legend-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chart-legend-value {
  color: var(--text, #101828);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ── Donut ───────────────────────────────────────────────────────────────────────────────────── */

.chart-donut-wrap {
  display: flex;
  align-items: center;
  gap: var(--s-6, 24px);
  min-width: 0;
}
.chart-donut.is-stacked .chart-donut-wrap {
  flex-direction: column;
  align-items: stretch;
  gap: var(--s-4, 16px);
}
.chart-donut-svg {
  flex: none;
  align-self: center;
}
.chart-arc {
  fill: none;
  stroke-linecap: butt;
  transition: opacity var(--dur-fast, 150ms) var(--ease, ease-out);
}
.has-active .chart-arc:not(.is-active) { opacity: 0.32; }
.chart-donut-value {
  fill: var(--text, #101828);
  font-weight: 650;
  letter-spacing: -0.02em;
}
.chart-donut-label {
  fill: var(--text-2, #475069);
  font-weight: 500;
}
.chart-donut-sub {
  fill: var(--text-3, #5d6679);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.chart-donut-legend {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
.chart-donut-row {
  display: grid;
  grid-template-columns: 10px minmax(0, 1fr) auto 3.4em;
  align-items: center;
  gap: 10px;
  min-height: 34px;
  padding: 5px 8px;
  border-radius: var(--r-sm, 8px);
  font-size: 13px;
  transition: background-color var(--dur-fast, 150ms) var(--ease, ease-out);
}
.chart-donut-row.is-active { background: var(--_hover); }
.chart-donut.is-clickable .chart-donut-row,
.chart-donut.is-clickable .chart-arc { cursor: pointer; }
.chart-donut-name {
  min-width: 0;
  overflow: hidden;
  color: var(--text, #101828);
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chart-donut-amt {
  color: var(--text, #101828);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.chart-donut-pct {
  color: var(--text-3, #5d6679);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ── Heatmap ─────────────────────────────────────────────────────────────────────────────────── */

.chart-heatmap-row {
  display: flex;
  align-items: flex-start;
  min-width: 0;
}
.chart-heatmap-labels { flex: none; }
.chart-scroll {
  flex: 1 1 auto;
  min-width: 0;
}
.chart-scroll.is-scroll {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding-bottom: 6px;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.chart-heatmap-grid { flex: none; }
.chart-month { fill: var(--text-2, #475069); }
.chart-cell {
  stroke: transparent;
  stroke-width: 0;
  transition: opacity var(--dur-fast, 150ms) var(--ease, ease-out);
}
.chart-cell.is-today {
  stroke: var(--text-2, #475069);
  stroke-width: 1.25;
  stroke-opacity: 0.7;
}
.chart-cell.is-active {
  stroke: var(--text, #101828);
  stroke-width: 1.75;
  stroke-opacity: 1;
}
.chart-heatmap-grid.is-clickable .chart-cell { cursor: pointer; }
.chart-heatmap-foot {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.chart-scale {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  color: var(--text-3, #5d6679);
  font-size: 11px;
}
.chart-scale > span:first-child { margin-right: 4px; }
.chart-scale > span:last-child { margin-left: 4px; }
.chart-scale-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2.5px;
}

/* ── Sparkline ───────────────────────────────────────────────────────────────────────────────── */

.chart.chart-sparkline {
  display: inline-block;
  flex: none;
  vertical-align: middle;
  line-height: 0;
}
.chart-sparkline .chart-svg { width: auto; }
.chart-spark-line { stroke-width: 1.75; }
.chart-spark-dot { stroke-width: 1.5; }
.chart-spark-empty {
  stroke: var(--_grid);
  stroke-width: 2;
  stroke-linecap: round;
}

/* ── Empty state ─────────────────────────────────────────────────────────────────────────────── */

.chart-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: var(--s-4, 16px);
  border-radius: var(--r, 12px);
  background: var(--surface-2, #f1f3f6);
  color: var(--text-3, #5d6679);
  font-size: 13px;
  text-align: center;
}
.chart-empty-art { display: block; }
.chart-empty-art rect { fill: var(--surface-3, #e7eaf0); }
.chart-empty-base {
  fill: none;
  stroke: var(--border-strong, #d2d6df);
  stroke-width: 1;
}
.chart-empty-text { font-weight: 500; }

/* ── Entrance animation (first render only; off with prefers-reduced-motion) ─────────────────── */

@media (prefers-reduced-motion: no-preference) {
  .chart-anim .chart-line:not(.chart-spark-line) {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation: chart-draw 900ms cubic-bezier(0.33, 0.9, 0.3, 1) forwards;
    animation-delay: calc(var(--i, 0) * 90ms);
  }
  .chart-anim .chart-area,
  .chart-anim .chart-dot:not(.chart-dot-active) {
    animation: chart-fade 600ms var(--ease, ease-out) 260ms both;
  }
  .chart-anim .chart-bar,
  .chart-anim .chart-stack {
    transform-box: fill-box;
    transform-origin: 50% 100%;
    animation: chart-grow-y 560ms cubic-bezier(0.25, 0.9, 0.3, 1) both;
    animation-delay: calc(var(--i, 0) * 16ms);
  }
  .chart-anim .chart-bar.is-neg { transform-origin: 50% 0%; }
  .chart-anim .chart-bar-h {
    transform-origin: 0% 50%;
    animation-name: chart-grow-x;
    animation-delay: calc(var(--i, 0) * 30ms);
  }
  .chart-anim .chart-bar-h.is-neg { transform-origin: 100% 50%; }
  .chart-anim .chart-value-label,
  .chart-anim .chart-target-label {
    animation: chart-fade 400ms var(--ease, ease-out) 380ms both;
  }
  .chart-anim .chart-arc {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation: chart-draw var(--arc-dur, 0.3s) linear var(--arc-delay, 0s) forwards;
  }
  .chart-anim .chart-cell {
    animation: chart-fade 380ms var(--ease, ease-out) both;
    animation-delay: calc(var(--c, 0) * 9ms);
  }
}

@keyframes chart-draw { to { stroke-dashoffset: 0; } }
@keyframes chart-fade { from { opacity: 0; } }
@keyframes chart-grow-y { from { transform: scaleY(0); } }
@keyframes chart-grow-x { from { transform: scaleX(0); } }

@media (prefers-reduced-motion: reduce) {
  .chart *,
  .chart .chart-tooltip { animation: none !important; transition: none !important; }
}

/* Forced colors (Windows high contrast): keep marks visible with system colors. */
@media (forced-colors: active) {
  .chart-line, .chart-target, .chart-crosshair-line { stroke: CanvasText; }
  .chart-bar, .chart-arc { forced-color-adjust: none; }
  .chart .chart-tooltip { border-color: CanvasText; }
}
