/* v3 chart kit (reference §7; plan §11 #69, #70, #103–#106). Marks take role
   tokens through classes (SVG attributes carry the lengths in %, never
   style=""). Numbers are always text: direct labels, legend, the cells, or
   "Show as table". Mark specs (dataviz): bars ≤ 1.5rem thick, rounded at
   the value end; 2px lines; 8px dots with a 2px surface ring; 2px surface
   gaps between touching marks; hairline solid gridlines.
   Macros: core/components/_lg_chart.html; tooltips: js/v3/chart-tips.js. */

/* ---------- Frame ---------- */
.lg-chart {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0;
  margin: 0;
  container: lg-chart / inline-size;     /* rows switch layout by the chart's own width */
}
.lg-chart__head { display: flex; flex-direction: column; gap: var(--space-1); }
.lg-chart__title {
  margin: 0;
  font-size: var(--size-card-title);
  line-height: var(--leading-card-title);
  font-weight: 600;
}
.lg-chart__summary { margin: 0; color: var(--text-muted); font-size: var(--size-label); line-height: var(--leading-label); }
.lg-chart__empty {
  margin: 0;
  padding: var(--space-8) var(--space-4);
  border-radius: var(--radius-control);
  background: var(--neutral-tint);
  color: var(--text-muted);
  text-align: center;
}
/* Series flat at zero, named instead of drawn (dataviz, phase 6). */
.lg-chart__flat { margin: 0; color: var(--text-muted); font-size: var(--size-caption); line-height: var(--leading-caption); }
.lg-chart__body { position: relative; min-width: 0; border-radius: var(--radius-control); }
.lg-chart__body:focus-visible, .lg-spark:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* "Show as table": a <details>, works without JS. */
.lg-chart__toggle {
  inline-size: fit-content;
  padding-block: var(--space-1);
  color: var(--primary);
  font-size: var(--size-label);
  line-height: var(--leading-label);
  cursor: pointer;
}
.lg-chart__toggle:hover { text-decoration: underline; text-underline-offset: 0.2em; }
.lg-chart__scroll { margin-top: var(--space-2); overflow-x: auto; }
.lg-ctable { min-inline-size: 100%; border-collapse: collapse; font-size: var(--size-label); line-height: var(--leading-label); }
/* Names start-aligned and wrapping, values end-aligned on one line (decision 292). */
.lg-ctable th, .lg-ctable td { padding: var(--space-1) var(--space-3); border-bottom: 1px solid var(--border); text-align: start; vertical-align: top; }
.lg-ctable .lg-ctable__num { text-align: end; white-space: nowrap; }
.lg-ctable thead th { border-bottom-color: var(--border-strong); color: var(--text-muted); font-weight: 500; }
.lg-ctable tbody th { font-weight: 400; }
.lg-ctable .lg-ctable__group { color: var(--text-muted); }

/* ---------- View switches above a chart (Show by Day · Week · Month) ---------- */
.lg-chart-tools { display: flex; flex-wrap: wrap; gap: var(--space-3) var(--space-6); }
.lg-switch { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); min-inline-size: 0; max-inline-size: 100%; }
.lg-switch__label { color: var(--text-muted); font-size: var(--size-label); line-height: var(--leading-label); }
/* Why an option is off, as text where there is no hover tooltip (touch screens). */
.lg-switch__why { display: none; flex-basis: 100%; margin: 0; color: var(--text-muted);
  font-size: var(--size-caption); line-height: var(--leading-caption); }
@media (hover: none) { .lg-switch__why { display: block; } }
/* Show by Day · Week · Month (decision 361): only the chart's card reloads; it dims
   while it does (js/v3/progress.js sets aria-busy on the card). */
[aria-busy="true"] > .lg-chart, [aria-busy="true"] > .lg-chart-tools { opacity: 0.55; transition: opacity var(--dur-exit) var(--ease); }
[aria-busy="true"] > .lg-chart-tools { pointer-events: none; }
/* Ledger 2 (decision 308): the segmented filter control has the tabs' pill
   look (track + raised pill), never a dark outline. */
.lg-switch__opts {
  display: inline-flex;
  gap: var(--space-1);
  max-inline-size: 100%;
  min-inline-size: 0;
  padding: var(--space-1);
  border-radius: var(--radius-card);
  background: var(--neutral-tint);
}
/* The measured row (decision 308, js/v3/tabs.js): options that don't fit move into
   "More ▾" (the last pill in the track), never clipped and never scrolled away. */
.lg-switch__list { display: flex; gap: var(--space-1); min-inline-size: 0; overflow-x: clip; overflow-y: visible; }   /* clip, not hidden: the 44px tap areas reach above and below */
.lg-switch__item { display: contents; }
.lg-switch .lg-tabs__moretab { gap: var(--space-1); }
.lg-switch__opt {
  display: inline-flex;
  align-items: center;
  min-block-size: 2rem;
  padding: 0 var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius-control);
  color: var(--text-muted);
  font-size: var(--size-label);
  line-height: var(--leading-label);
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  touch-action: manipulation;
}
@media (hover: hover) and (pointer: fine) {
  a.lg-switch__opt:hover { color: var(--text); }
}
.lg-switch__opt[aria-current="true"] {
  border-color: var(--raised-border);
  background: var(--raised);
  box-shadow: var(--shadow-raised);
  color: var(--text);
  font-weight: 600;
}
.lg-switch__opt[aria-disabled="true"] { color: var(--disabled-text); cursor: not-allowed; }
.lg-switch__opt:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Legend + keys (also the tooltip's line keys) ---------- */
.lg-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: var(--size-label);
  line-height: var(--leading-label);
}
.lg-legend__item { display: inline-flex; align-items: center; gap: var(--space-2); }
.lg-legend__value { font-weight: 600; }
.lg-key { display: inline-block; flex: none; inline-size: 0.75rem; block-size: 0.75rem; }
.lg-key--line { inline-size: 0.875rem; block-size: 0.125rem; }
.lg-key--1 { background: var(--chart-1); }
.lg-key--2 { background: var(--chart-2); }
.lg-key--3 { background: var(--chart-3); }
.lg-key--4 { background: var(--chart-4); }
.lg-key--muted { background: var(--chart-muted); }
.lg-key--soft { background: var(--chart-1-soft); }
.lg-key--track { background: var(--neutral-tint); box-shadow: inset 0 0 0 1px var(--border-control); }
.lg-key--seq-1 { background: var(--chart-seq-1); }
.lg-key--seq-2 { background: var(--chart-seq-2); }
.lg-key--seq-3 { background: var(--chart-seq-3); }
.lg-key--seq-4 { background: var(--chart-seq-4); }
.lg-key--up { background: var(--chart-up); }
.lg-key--down { background: var(--chart-down); }
.lg-key--good { background: var(--success); }
.lg-key--bad { background: var(--danger); }
.lg-key--zero { background: var(--border-strong); }

/* ---------- Mark colours ---------- */
.lg-fill-1 { fill: var(--chart-1); }
.lg-fill-2 { fill: var(--chart-2); }
.lg-fill-3 { fill: var(--chart-3); }
.lg-fill-4 { fill: var(--chart-4); }
.lg-fill-muted { fill: var(--chart-muted); }
.lg-fill-soft { fill: var(--chart-1-soft); }
.lg-fill-track { fill: var(--neutral-tint); }
.lg-fill-seq-1 { fill: var(--chart-seq-1); }
.lg-fill-seq-2 { fill: var(--chart-seq-2); }
.lg-fill-seq-3 { fill: var(--chart-seq-3); }
.lg-fill-seq-4 { fill: var(--chart-seq-4); }
.lg-stroke-1 { stroke: var(--chart-1); }
.lg-stroke-2 { stroke: var(--chart-2); }
.lg-stroke-3 { stroke: var(--chart-3); }
.lg-stroke-4 { stroke: var(--chart-4); }
.lg-stroke-muted { stroke: var(--chart-muted); }
.lg-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.lg-area { fill: var(--chart-wash); }
.lg-dot { stroke: var(--surface); stroke-width: 2; }
.lg-gap { stroke: var(--surface); stroke-width: 2; }
.lg-hit { fill: transparent; pointer-events: all; }
.lg-hit.is-active { stroke: var(--border-control); stroke-width: 1; }
.lg-div--up { fill: var(--chart-up); }
.lg-div--down { fill: var(--chart-down); }
.lg-div--good { fill: var(--success); }
.lg-div--bad { fill: var(--danger); }

/* ---------- Column / grouped / line / area plot ---------- */
.lg-plot {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: 11rem auto;
  column-gap: var(--space-2);
  padding-top: var(--space-5);       /* room for the top tick and value labels */
}
.lg-plot__y {
  grid-column: 1;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: var(--text-muted);
  /* Axis numbers: sans with tabular figures (reference §7, dataviz review). */
  font-variant-numeric: tabular-nums;
  font-size: var(--size-caption);
  line-height: var(--leading-caption);
  text-align: end;
}
/* Zero-height rows: each tick's text centres on its gridline. */
.lg-plot__tick { display: flex; align-items: center; justify-content: flex-end; block-size: 0; white-space: nowrap; }
.lg-plot__area { position: relative; grid-column: 2; grid-row: 1; min-width: 0; }
.lg-plot__grid, .lg-plot__lines, .lg-plot__ends {
  position: absolute;
  inset: 0;
  inline-size: 100%;
  block-size: 100%;
  overflow: visible;
  pointer-events: none;
}
.lg-plot__rule { stroke: var(--border); stroke-width: 1; }
.lg-plot__base { stroke: var(--border-strong); stroke-width: 1; }
.lg-plot__label {
  fill: var(--text);
  font-size: var(--size-caption);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 0.1875rem;
  stroke-linejoin: round;
}
.lg-plot__bands { position: absolute; inset: 0; display: flex; }
.lg-plot__band {
  position: relative;
  display: flex;
  flex: 1 1 0;
  justify-content: center;
  min-width: 0;
}
.lg-plot__band.is-active { background: var(--row-hover); }
.lg-plot__group { display: flex; justify-content: center; gap: 0.125rem; inline-size: 60%; min-inline-size: 0; max-inline-size: 1.5rem; }
.lg-plot--grouped .lg-plot__group { inline-size: 80%; max-inline-size: none; }
/* min-inline-size: 0, or an <svg> flex item keeps its 300px default width. */
.lg-plot__bar { display: block; flex: 1 1 0; min-inline-size: 0; max-inline-size: 1.5rem; block-size: 100%; overflow: visible; }
.lg-plot__hover { position: absolute; inset: 0; display: none; inline-size: 100%; block-size: 100%; overflow: visible; pointer-events: none; }
.lg-plot__band.is-active > .lg-plot__hover { display: block; }
.lg-plot__cross { stroke: var(--border-strong); stroke-width: 1; }
.lg-plot__x {
  grid-column: 2;
  grid-row: 2;
  display: flex;
  padding-top: var(--space-2);
  color: var(--text-muted);
  font-size: var(--size-caption);
  line-height: var(--leading-caption);
}
.lg-plot__xl { display: flex; flex: 1 1 0; justify-content: center; min-width: 0; white-space: nowrap; visibility: hidden; }
/* The last column's label stays centred while it fits its column and ends
   at the plot edge when it doesn't: `safe center` falls back to "start",
   which `direction: rtl` makes the right edge (the text itself stays ltr).
   With the step counted from the highlighted column (#126) "today" is
   always labelled. */
.lg-plot__xl:last-child { direction: rtl; justify-content: flex-start; justify-content: safe center; }
.lg-plot__xl:last-child > span { direction: ltr; }
.lg-plot__xl--on { visibility: visible; }

/* ---------- Sparkline (KPI band slot, #106) ---------- */
.lg-spark { position: relative; display: block; inline-size: 100%; block-size: 2rem; }
.lg-spark__line, .lg-spark__end {
  position: absolute;
  inset: 0;
  inline-size: 100%;
  block-size: 100%;
  overflow: visible;
  pointer-events: none;
}
.lg-spark__bands { position: absolute; inset: 0; display: flex; }

/* ---------- Meter ---------- */
.lg-meter { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.lg-meter__head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--space-1) var(--space-3);
  margin: 0;
  font-size: var(--size-label);
  line-height: var(--leading-label);
}
.lg-meter__text { color: var(--text-muted); }
.lg-meter__svg { display: block; inline-size: 100%; block-size: 0.5rem; }
.lg-meter__fill { fill: var(--chart-1); }
.lg-meter--success .lg-meter__fill { fill: var(--success); }
.lg-meter--warning .lg-meter__fill { fill: var(--warning); }
.lg-meter--danger .lg-meter__fill { fill: var(--danger); }

/* ---------- Horizontal rows: stacked, diverging, funnel, tiers ----------
   All rows share ONE set of columns (subgrid), so every bar starts and ends
   at the same place and lengths compare across rows. */
.lg-hbars {
  display: grid;
  grid-template-columns: minmax(6rem, 30%) minmax(0, 1fr) auto;
  row-gap: var(--space-2);
  column-gap: var(--space-3);
  margin: 0;
  padding: 0;
  list-style: none;
}
.lg-hbars__row {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: subgrid;
  align-items: center;
  font-size: var(--size-label);
  line-height: var(--leading-label);
}
.lg-hbars__label { overflow-wrap: anywhere; }
.lg-hbars__svg { display: block; inline-size: 100%; block-size: 1.5rem; overflow: visible; }
.lg-hbars__value { display: flex; flex-direction: column; align-items: flex-end; text-align: end; }
.lg-funnel__note { color: var(--text-muted); font-size: var(--size-caption); line-height: var(--leading-caption); white-space: nowrap; }

/* ---------- Tier sales against capacity (grouped by category or flat) ---------- */
/* Categories are <details> blocks (WebKit won't lay out a <details> as a
   subgrid), so every tier list uses the same fixed columns instead: the
   number column is wide enough for "Sold out, 1,200 of 1,200", and bars line
   up across categories. */
.lg-tiers { display: flex; flex-direction: column; gap: var(--space-5); }
.lg-tiers__group { display: block; }
.lg-tiers .lg-hbars { grid-template-columns: minmax(6rem, 30%) minmax(0, 1fr) 11rem; }
/* Category heading = the <summary> of its <details>: the subtotal always
   shows; the chevron turns when open. */
.lg-tiers__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-1) var(--space-3);
  margin: 0 0 var(--space-2);
  padding-block: var(--space-1);
  border-bottom: 1px solid var(--border);
  font-size: var(--size-label);
  line-height: var(--leading-label);
  list-style: none;
  cursor: pointer;
  touch-action: manipulation;
}
.lg-tiers__head::-webkit-details-marker { display: none; }
.lg-tiers__head:hover .lg-tiers__cat { color: var(--primary); }
.lg-tiers__head:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.lg-tiers__group:not([open]) > .lg-tiers__head { margin-bottom: 0; }
.lg-tiers__cat { display: inline-flex; align-items: center; gap: var(--space-1); font-weight: 600; }
.lg-tiers__cat svg { flex: none; color: var(--text-muted); transition: transform var(--dur-fast) var(--ease); }
.lg-tiers__group[open] .lg-tiers__cat svg { transform: rotate(90deg); }
@media (prefers-reduced-motion: reduce) {
  .lg-tiers__cat svg { transition: none; }
}
.lg-tiers__sub { color: var(--text-muted); }
.lg-tiers__text { white-space: nowrap; }

/* ---------- Heatmap: a real table ---------- */
.lg-heat__scroll { overflow-x: auto; }
.lg-heat { border-collapse: separate; border-spacing: 0.125rem; font-size: var(--size-caption); line-height: var(--leading-caption); }
.lg-heat th { padding: 0 var(--space-2); color: var(--text-muted); font-weight: 500; white-space: nowrap; }
.lg-heat thead th { text-align: center; }
.lg-heat tbody th { text-align: end; }
.lg-heat__cell {
  min-inline-size: 2.25rem;
  block-size: 1.75rem;
  padding: 0 var(--space-1);
  background: var(--neutral-tint);
  color: var(--text-muted);
  text-align: center;
}
.lg-heat--1 { background: var(--chart-seq-1); color: var(--text); }
.lg-heat--2 { background: var(--chart-seq-2); color: var(--text); }
.lg-heat--3 { background: var(--chart-seq-3); color: var(--on-primary); }
.lg-heat--4 { background: var(--chart-seq-4); color: var(--on-primary); }
/* Dark: the ramp flips (darker = less), so step 2 needs dark text too (#105). */
[data-theme="dark"] .lg-heat--2 { color: var(--on-primary); }
.lg-heat__cell.is-active { outline: 2px solid var(--focus); outline-offset: -2px; }
.lg-heat__scale {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin: var(--space-2) 0 0;
  color: var(--text-muted);
  font-size: var(--size-caption);
  line-height: var(--leading-caption);
}
.lg-heat__swatch { display: inline-block; inline-size: 1rem; block-size: 0.75rem; }

/* ---------- Tooltip (js/v3/chart-tips.js, #104) ---------- */
.lg-tip {
  position: fixed;
  z-index: var(--layer-menu);
  max-inline-size: 16rem;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--float-border);
  border-radius: var(--radius-control);
  background: var(--raised);
  color: var(--text);
  box-shadow: var(--shadow-menu);
  font-size: var(--size-label);
  line-height: var(--leading-label);
}
.lg-tip[hidden] { display: none; }
.lg-tip__t { margin: 0 0 var(--space-1); color: var(--text-muted); font-size: var(--size-caption); line-height: var(--leading-caption); }
.lg-tip__rows { display: flex; flex-direction: column; gap: var(--space-1); margin: 0; padding: 0; list-style: none; }
.lg-tip__rows li { display: flex; align-items: center; gap: var(--space-2); }
.lg-tip__rows strong { font-variant-numeric: tabular-nums; }
.lg-tip__rows strong + span { color: var(--text-muted); }

/* ---------- Ranked horizontal bar list: label · bar · count · % ---------- */
.lg-barlist { inline-size: 100%; border-collapse: collapse; font-size: var(--size-label); }
.lg-barlist th, .lg-barlist td { padding: var(--space-1) 0; vertical-align: middle; }
.lg-barlist__label {
  inline-size: 34%;
  max-inline-size: 16rem;
  padding-inline-end: var(--space-3);
  font-weight: 400;
  text-align: start;
  overflow-wrap: anywhere;
}
.lg-barlist__bar { inline-size: 50%; }
.lg-barlist__svg { display: block; inline-size: 100%; block-size: 0.5rem; }
.lg-barlist__track { fill: var(--neutral-tint); }
.lg-barlist__fill { fill: var(--chart-1); }
.lg-barlist__n { inline-size: 3rem; text-align: end; font-weight: 600; }
.lg-barlist__pct { inline-size: 3.5rem; text-align: end; color: var(--text-muted); }

/* ---------- Phones ---------- */
@media (max-width: 39.99em) {
  .lg-plot { grid-template-rows: 9rem auto; }
  .lg-tiers { gap: var(--space-2); }
  .lg-tiers__head { min-block-size: var(--touch-target); }
  .lg-chart__toggle { display: flex; align-items: center; min-block-size: var(--touch-target); }
  /* Same as phone buttons (decision 284): the whole switch looks 2.5rem, each
     option keeps a 44px tap area through an invisible ::before. */
  .lg-switch__opt { position: relative; min-block-size: calc(2.5rem - 0.25rem - 2px); font-size: var(--size-body); }
  .lg-switch__opt::before { content: ""; position: absolute; inset: calc((var(--touch-target) - 100%) / -2) 0; }
  /* "Show as table" as blocks (decision 292): category (muted) over the name,
     then the values in label-over-number cells, 3 a row at 360-400px. The label
     wraps (2026-10-09: "Admin console", "Server errors (5xx)" ran into the next
     cell at 3.25rem with the number's nowrap); the number stays on one line. */
  .lg-chart__scroll { overflow: visible; }
  .lg-ctable, .lg-ctable tbody { display: block; }
  .lg-ctable thead { position: absolute; inline-size: 1px; block-size: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .lg-ctable tbody tr {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(5.5rem, 1fr));
    gap: var(--space-1) var(--space-2);
    padding-block: var(--space-3);
    border-bottom: 1px solid var(--border);
  }
  .lg-ctable tbody th, .lg-ctable tbody td { padding: 0; border: 0; }
  .lg-ctable .lg-ctable__group { grid-column: 1 / -1; font-size: var(--size-caption); line-height: var(--leading-caption); }
  .lg-ctable .lg-ctable__name { grid-column: 1 / -1; font-weight: 600; font-size: var(--size-body); line-height: var(--leading-body); }
  .lg-ctable td.lg-ctable__num { display: flex; flex-direction: column; text-align: start; }
  .lg-ctable td.lg-ctable__num::before { content: attr(data-label); font-family: var(--font-sans); font-size: var(--size-caption); line-height: var(--leading-caption); color: var(--text-muted);
    white-space: normal; overflow-wrap: break-word; }
  .lg-barlist__label { inline-size: 40%; }
  .lg-barlist__bar { inline-size: 36%; }
}

/* ---------- X-label density by the chart's own width (wide / medium / narrow) ---------- */
@container lg-chart (max-width: 56rem) {
  .lg-plot__xl--on { visibility: hidden; }
  .lg-plot__xl--mid { visibility: visible; }
}
@container lg-chart (max-width: 36rem) {
  .lg-plot__xl--mid { visibility: hidden; }
  .lg-plot__xl--phone { visibility: visible; }
}

/* ---------- Narrow chart (a phone, or a half-width card on desktop) ----------
   Decided by the chart's own width, not the screen: label and number on one
   line, the bar full width under them, so every bar has the same length and
   rows still compare. */
@container lg-chart (max-width: 36rem) {
  .lg-hbars, .lg-tiers .lg-hbars { grid-template-columns: minmax(0, 1fr) auto; }
  .lg-hbars__row { row-gap: var(--space-1); }
  .lg-hbars__label { grid-column: 1; grid-row: 1; }
  .lg-hbars__value { grid-column: 2; grid-row: 1; }
  .lg-hbars__svg { grid-column: 1 / -1; grid-row: 2; }
}

/* ---------- Two charts on one row (#109) ----------
   Only on wide screens and only when the page says both are compact
   (`charts.tier_sales(...)["fits_beside"]`); cards stretch to the taller one. */
.lg-chart-row { display: grid; gap: var(--space-4); }
@media (min-width: 80em) {
  .lg-chart-row--pair { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* The shorter card's plot grows to fill the row (never below its normal
     11rem), so both cards end on one line (#110). Bounded: a pair only
     happens with ≤ 8 tier rows (#109). */
  .lg-chart-row--pair > .lg-card { display: flex; flex-direction: column; }
  .lg-chart-row--pair .lg-chart,
  .lg-chart-row--pair .lg-chart__body { display: flex; flex: 1 1 auto; flex-direction: column; }
  .lg-chart-row--pair .lg-plot { flex: 1 1 auto; grid-template-rows: minmax(11rem, 1fr) auto; }
}
