/* ==========================================================================
   Charts — bar, line, area and donut charts drawn by moey.js from a plain table,
   plus sparklines for stat tiles.

   <figure class="chart" data-moey-chart="bar">
     <figcaption>Revenue by month</figcaption>
     <table>
       <thead><tr><th>Month</th><th>Online</th><th>In store</th></tr></thead>
       <tbody><tr><td>Jan</td><td>4200</td><td>3100</td></tr> …</tbody>
     </table>
   </figure>

   The palette: eight colors in a fixed order (--mo-chart-1 … 8), checked for color-blind
   readers and for contrast on light and dark surfaces. A series keeps its color when others
   are hidden. <th data-color="green"> picks a color by name.
   ========================================================================== */

:root {
  /* Light steps on the light surface, darker/lighter steps on the dark one (validated per mode) */
  --mo-chart-blue:   var(--mo-blue-500);
  --mo-chart-orange: light-dark(var(--mo-orange-500), var(--mo-orange-600));
  --mo-chart-teal:   var(--mo-teal-600);
  --mo-chart-amber:  light-dark(var(--mo-amber-600), color-mix(in oklch, var(--mo-black) 17%, var(--mo-amber)));
  --mo-chart-pink:   var(--mo-pink-500);
  --mo-chart-green:  light-dark(var(--mo-green-600), var(--mo-green-500));
  --mo-chart-purple: var(--mo-purple-500);
  --mo-chart-red:    var(--mo-red-500);
  --mo-chart-gray:   light-dark(var(--mo-gray-400), var(--mo-gray-500));   /* "Other" */
  /* The order is the color-blind safety: neighbors always differ in lightness and hue */
  --mo-chart-1: var(--mo-chart-blue);
  --mo-chart-2: var(--mo-chart-orange);
  --mo-chart-3: var(--mo-chart-teal);
  --mo-chart-4: var(--mo-chart-amber);
  --mo-chart-5: var(--mo-chart-pink);
  --mo-chart-6: var(--mo-chart-green);
  --mo-chart-7: var(--mo-chart-purple);
  --mo-chart-8: var(--mo-chart-red);
}

.chart {
  --mo-chart-height: 16rem;
  --mo-chart-bg: var(--mo-surface-bg, var(--mo-bg-surface));   /* the rings and gaps around marks */
  display: grid;
  gap: .75rem;
  min-width: 0;
  margin: 0;
}
.chart > figcaption { font-weight: var(--mo-font-weight-semibold); }
.chart > figcaption small { display: block; font-weight: var(--mo-font-weight-normal); color: var(--mo-fg-3); }
.chart-type-donut { --mo-chart-height: 13rem; }

/* Before moey.js runs (or without it) the table is the chart */
.chart:not(:has(> .chart-plot)) table { width: 100%; }

.chart-plot {
  position: relative;
  min-width: 0;
  height: var(--mo-chart-height);
  touch-action: pan-y;                       /* a sideways drag reads the values, scrolling still works */
  -webkit-tap-highlight-color: transparent;
  border-radius: var(--mo-radius-control);
  outline: none;
}
.chart-plot:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 4px; }
.chart-plot > svg { position: absolute; inset: 0; display: block; overflow: visible; }
.chart.is-table .chart-plot { display: none; }

/* Grid and axes: hairlines, quiet text */
.chart-grid line { stroke: color-mix(in oklab, var(--mo-fg-body) 9%, transparent); stroke-width: 1; shape-rendering: crispEdges; }
.chart-grid line.is-zero { stroke: color-mix(in oklab, var(--mo-fg-body) 28%, transparent); }
.chart-axis text,
.chart-value { font-size: 11px; font-variant-numeric: tabular-nums; fill: var(--mo-fg-3); }
.chart-value { font-weight: var(--mo-font-weight-medium); fill: var(--mo-fg-2); }

/* Marks: the series color, nothing else */
.chart-bar,
.chart-slice { fill: var(--c); transition: opacity var(--mo-duration) var(--mo-ease); }
.chart-slice { stroke: var(--mo-chart-bg); stroke-width: 2; stroke-linejoin: round; }   /* the 2px gap between slices */
.chart-mark.is-dim { opacity: .38; }
.chart-line { fill: none; stroke: var(--c); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chart-area { fill: var(--c); fill-opacity: .1; stroke: none; }
.chart-dot { fill: var(--c); stroke: var(--mo-chart-bg); stroke-width: 2; }
.chart-hover-dots .chart-dot { r: 5; }
.chart-cross { stroke: color-mix(in oklab, var(--mo-fg-body) 35%, transparent); stroke-width: 1; shape-rendering: crispEdges; pointer-events: none; }
.chart-center-value { font-size: 1.375rem; font-weight: var(--mo-font-weight-semibold); fill: var(--mo-fg-body); }
.chart-center-label { font-size: .75rem; fill: var(--mo-fg-3); }
.chart svg text { user-select: none; }

/* Tooltip: values lead, names follow; a short line keys each series */
.chart-tip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  display: grid;
  gap: .25rem;
  min-width: 7.5rem;
  max-width: min(16rem, 100%);
  padding: .5rem .625rem;
  font-size: .8125rem;
  line-height: 1.3;
  color: var(--mo-fg-body);
  pointer-events: none;
  background: var(--mo-bg-raised);
  border-radius: var(--mo-radius-control);
  box-shadow: var(--mo-shadow-overlay);
}
.chart-tip[hidden] { display: none; }
.chart-tip-head { font-size: .75rem; color: var(--mo-fg-3); }
.chart-tip-row { display: flex; gap: .5rem; align-items: center; }
.chart-tip-row b { font-weight: var(--mo-font-weight-semibold); font-variant-numeric: tabular-nums; }
.chart-tip-name { min-width: 0; overflow: hidden; color: var(--mo-fg-2); text-overflow: ellipsis; white-space: nowrap; }
.chart-tip-key { flex: none; width: .75rem; height: 2px; background: var(--c); border-radius: 1px; }
.chart-tip-row.is-total { padding-top: .25rem; margin-top: .125rem; border-top: 1px solid var(--mo-border-color); }

/* Legend (2+ series) and the table switch */
.chart-foot { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; }
.chart-legend { display: flex; flex-wrap: wrap; gap: .25rem .875rem; min-width: 0; }
.chart-legend[hidden] { display: none; }
.chart-legend-item {
  display: inline-flex;
  gap: .4375rem;
  align-items: center;
  min-height: 1.75rem;
  padding: 0;
  font: inherit;
  font-size: .8125rem;
  color: var(--mo-fg-2);
  cursor: pointer;
  background: none;
  border: 0;
  border-radius: var(--mo-radius-1, 4px);
}
.chart-legend-item:hover { color: var(--mo-fg-body); }
.chart-legend-item:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 2px; }
.chart-legend-item[aria-pressed="false"] { color: var(--mo-fg-3); text-decoration: line-through; }
.chart-legend-item[aria-pressed="false"] .chart-key { background: transparent; box-shadow: inset 0 0 0 1.5px var(--c); }
.chart-legend-value { font-variant-numeric: tabular-nums; color: var(--mo-fg-3); }
.chart-key { flex: none; width: .625rem; height: .625rem; background: var(--c); border-radius: 2px; }
.chart-key-line { height: 2px; width: .875rem; border-radius: 1px; }
.chart-legend-item[aria-pressed="false"] .chart-key-line { height: 0; box-shadow: none; border-top: 2px dotted var(--c); }
.chart-switch {
  display: inline-flex;
  gap: .375rem;
  align-items: center;
  min-height: 1.75rem;
  padding: 0 .5rem;
  margin-inline-start: auto;
  font: inherit;
  font-size: .8125rem;
  color: var(--mo-fg-3);
  cursor: pointer;
  background: none;
  border: 0;
  border-radius: var(--mo-radius-control);
}
.chart-switch:hover { color: var(--mo-fg-body); background: color-mix(in oklab, var(--mo-fg-body) 6%, transparent); }
.chart-switch:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 1px; }
.chart-switch[aria-pressed="true"] { color: var(--mo-fg-body); }

/* The table: read by screen readers always, shown with the switch */
.chart > .chart-table:not(.is-shown) { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.chart.is-table > .chart-table { position: static; width: auto; height: auto; overflow: auto; clip-path: none; white-space: normal; }
.chart-table .table { margin: 0; font-variant-numeric: tabular-nums; }
.chart-table td { text-align: end; }

/* Grow in the first time the chart scrolls into view */
.chart.is-waiting .chart-marks { opacity: 0; }
@media (prefers-reduced-motion: no-preference) {
  .chart.is-entering .chart-bar { transform-box: fill-box; transform-origin: bottom; animation: mo-chart-grow .6s var(--mo-ease) both; }
  .chart.is-entering.chart-type-bar[data-moey-horizontal] .chart-bar { transform-origin: left; animation-name: mo-chart-grow-x; }
  .chart.is-entering .chart-line { stroke-dasharray: 1; animation: mo-chart-draw .8s var(--mo-ease) both; }
  .chart.is-entering :is(.chart-area, .chart-dot, .chart-slice) { animation: mo-chart-fade .6s var(--mo-ease) both; }
}
@keyframes mo-chart-grow { from { scale: 1 0; } }
@keyframes mo-chart-grow-x { from { scale: 0 1; } }
@keyframes mo-chart-draw { from { stroke-dashoffset: 1; } }
@keyframes mo-chart-fade { from { opacity: 0; } }

/* High contrast: marks take the system colors, the legend and table carry the names */
@media (forced-colors: active) {
  .chart-bar, .chart-slice, .chart-key { fill: CanvasText; background: CanvasText; }
  .chart-line { stroke: CanvasText; }
}

/* Sparkline: a small trend line, in the current text color (or a theme-* color) */
.chart-spark {
  position: relative;
  display: inline-block;
  width: 6rem;
  height: 1.75rem;
  color: var(--mo-theme-accent, currentcolor);
  vertical-align: middle;
}
.chart-spark > svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart-spark-line { fill: none; stroke: currentcolor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.chart-spark-area { fill: currentcolor; fill-opacity: .1; }
.chart-spark-dot { position: absolute; inset-inline-end: 0; width: 6px; height: 6px; margin: -3px -3px 0 0; background: currentcolor; border-radius: 50%; box-shadow: 0 0 0 2px var(--mo-surface-bg, var(--mo-bg-surface)); }
.chart-spark-dot:dir(rtl) { margin: -3px 0 0 -3px; }
/* The palette as swatches (legends you build yourself, color keys in docs) */
.chart-swatch-1 { background: var(--mo-chart-1); } .chart-swatch-2 { background: var(--mo-chart-2); }
.chart-swatch-3 { background: var(--mo-chart-3); } .chart-swatch-4 { background: var(--mo-chart-4); }
.chart-swatch-5 { background: var(--mo-chart-5); } .chart-swatch-6 { background: var(--mo-chart-6); }
.chart-swatch-7 { background: var(--mo-chart-7); } .chart-swatch-8 { background: var(--mo-chart-8); }
