/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * Copyright © 2026 Marcel Rymenhout and IniqDash contributors */
/* ==========================================================================
   Chart / History Widget V1. Header (name · current value), the plot (SVG drawn at the plot's own
   layout size), footer (period · min / max · demo label).
     full     everything
     compact  (< 200 px tall) smaller header, footer on one quiet line
     mini     (< 200 px wide or < 130 px tall) value + sparkline only: no axes, no footer
   ========================================================================== */
.ch {
  position: relative;
  container: ch / size;
  --fit-ref-w: 360; --fit-ref-h: 220; --fit-min: .7px; --fit-max: 1.3px;
  --ch-line: var(--accent);
  min-width: 0; min-height: 72px;
  overflow: hidden;
  border-radius: var(--radius-device);
  color: var(--text-primary);
  font-family: var(--font);
  background: radial-gradient(120% 90% at 100% 0%, rgba(255, 255, 255, .03), transparent 60%), linear-gradient(165deg, var(--surface-primary-top) 0%, var(--surface-primary) 58%, var(--surface-secondary) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .055), inset 0 0 0 1px rgba(255, 255, 255, .03), 0 12px 26px -16px rgba(0, 0, 0, .75);
}
.ch__in { height: 100%; padding: clamp(9px, calc(14 * var(--fit)), 20px); display: grid; grid-template-rows: auto minmax(0, 1fr) auto; row-gap: calc(8 * var(--fit)); }
.ch__head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; min-width: 0; }
.ch__name { margin: 0; min-width: 0; font-size: max(11.5px, calc(14.5 * var(--fit))); font-weight: var(--iq-fw-title); color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch__now { margin: 0; flex: none; display: flex; align-items: baseline; gap: 4px; white-space: nowrap; }
.ch__val { font-size: max(16px, calc(26 * var(--fit))); font-weight: var(--iq-fw-display); letter-spacing: -.4px; }
.ch__unit { font-size: max(11px, calc(13 * var(--fit))); color: var(--text-secondary); }
.ch[data-now="unavailable"] .ch__val, .ch[data-now="none"] .ch__val { color: var(--text-muted); font-size: max(13px, calc(17 * var(--fit))); }

.ch__plot { position: relative; min-height: 0; min-width: 0; }
.ch__svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.ch__msg { position: absolute; inset: 0; margin: 0; display: grid; place-items: center; text-align: center; font-size: max(11px, calc(12 * var(--fit))); color: var(--text-muted); pointer-events: none; }
.ch__msg:empty { display: none; }
.ch[data-status="error"] .ch__msg { color: var(--warning); }

.ch__grid line { stroke: rgba(255, 255, 255, .06); stroke-width: 1; shape-rendering: crispEdges; }
.ch__grid text { fill: var(--text-muted); font-family: var(--font); }
.ch__line { stroke: var(--ch-line); stroke-width: var(--ch-lw, 2); stroke-linejoin: round; stroke-linecap: round; }
.ch__dot { fill: var(--ch-line); }
.ch__last { fill: var(--ch-line); stroke: var(--surface-primary); stroke-width: 2; }
.ch__stop-a { stop-color: var(--ch-line); stop-opacity: var(--ch-fill-a, .32); }
.ch__stop-b { stop-color: var(--ch-line-b, var(--ch-line)); stop-opacity: 0; }
.ch__bstop-a { stop-color: var(--ch-line); }
.ch__bstop-b { stop-color: var(--ch-line-b, var(--ch-line)); }
.ch__data--bar rect { fill: var(--ch-line); opacity: var(--ch-bar-o, .5); }
.ch[data-ch-glow] .ch__line, .ch[data-ch-glow] .ch__data--bar rect.ch__bar-now { filter: drop-shadow(0 0 4px var(--ch-line)); }
.ch__data--bar rect.ch__bar-now { opacity: 1; }
.ch[data-status="loading"] .ch__data, .ch[data-status="error"] .ch__data { opacity: .45; }

.ch__foot { display: flex; align-items: center; gap: 10px; min-width: 0; font-size: max(11px, calc(11.5 * var(--fit))); color: var(--text-muted); white-space: nowrap; }
.ch__mm { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.ch__mm:empty { display: none; }
.ch__demo { margin-left: auto; padding: 1px 8px; border-radius: var(--radius-pill); background: rgba(255, 255, 255, .06); color: var(--text-secondary); }
.ch[data-demo="false"] .ch__demo { display: none; }

.ch[data-fit-arr="compact"] .ch__in { row-gap: 5px; }
.ch[data-fit-arr="compact"] .ch__val { font-size: max(15px, calc(21 * var(--fit))); }
.ch[data-fit-arr="mini"] .ch__in { padding: clamp(7px, calc(10 * var(--fit)), 14px); row-gap: 4px; grid-template-rows: auto minmax(0, 1fr); }
.ch[data-fit-arr="mini"] .ch__head { flex-direction: column; align-items: stretch; gap: 0; }
.ch[data-fit-arr="mini"] .ch__foot { display: none; }
.ch[data-fit-arr="mini"] .ch__last { r: 2.6px; }
/* demo data stays labelled even when small: a small tag in the corner (the sparkline keeps its room) */
.ch[data-fit-arr="mini"][data-demo="true"] .ch__foot { display: flex; position: absolute; right: 6px; bottom: 6px; z-index: 1; }
.ch[data-fit-arr="mini"] .ch__range, .ch[data-fit-arr="mini"] .ch__mm { display: none; }
.ch[data-fit-arr="mini"] .ch__demo { margin-left: 0; padding: 0 6px; font-size: 11px; background: rgba(20, 22, 27, .82); box-shadow: 0 0 0 1px rgba(255, 255, 255, .07); }
.ch__grid .ch__tick { stroke: rgba(255, 255, 255, .16); }

/* SEVERAL SERIES: a legend row (only when shown), each series its own colour (--ch-line on its group), the zero line, a tooltip */
.ch[data-legend="true"]:not([data-fit-arr="mini"]) .ch__in { grid-template-rows: auto auto minmax(0, 1fr) auto; }
.ch__legend { display: flex; flex-wrap: wrap; gap: 2px 12px; margin: 0; padding: 0; list-style: none; min-width: 0; overflow: hidden; line-height: 1.3; max-height: calc(2.6em + 2px); }
.ch__legend[hidden] { display: none; }
.ch__key { display: inline-flex; align-items: center; gap: 5px; min-width: 0; font-size: max(11px, calc(11.5 * var(--fit))); color: var(--text-secondary); white-space: nowrap; }
.ch__sw { flex: none; width: 10px; height: 10px; border-radius: 3px; background: var(--ch-line); }
.ch__kn { overflow: hidden; text-overflow: ellipsis; }
.ch__kv { font-weight: var(--iq-fw-title, 600); color: var(--text-primary); font-variant-numeric: tabular-nums; }
.ch__key[data-error="true"] .ch__kv { color: var(--warning); font-weight: 500; }
.ch__grid .ch__zero { stroke: rgba(255, 255, 255, .28); }
.ch__cur { position: absolute; width: 1px; background: rgba(255, 255, 255, .35); pointer-events: none; }
.ch__tip { position: absolute; z-index: 2; display: flex; flex-direction: column; gap: 2px; padding: 6px 9px; border-radius: 10px; font-size: 11.5px; line-height: 1.25; white-space: nowrap; pointer-events: none;
  background: var(--surface-elevated, rgba(24, 26, 32, .95)); color: var(--text-primary); box-shadow: 0 8px 20px -8px rgba(0, 0, 0, .7), inset 0 0 0 1px var(--border-subtle); }
.ch__tip b { font-weight: var(--iq-fw-title, 600); color: var(--text-secondary); }
.ch__tr { display: flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.ch__tr::before { content: ''; width: 8px; height: 8px; border-radius: 2px; background: var(--ch-line); }
:is(.ch__cur, .ch__tip)[hidden] { display: none; }
.ch[data-fit-arr="mini"] :is(.ch__legend, .ch__tip, .ch__cur) { display: none; }
/* the current value LARGE UNDER THE TITLE (valuePlace: 'below'), with its trend line */
.ch[data-place="below"] .ch__head { flex-direction: column; align-items: flex-start; justify-content: flex-start; gap: 6px; }
.ch[data-place="below"] .ch__now .ch__val { font-size: clamp(22px, 9cqi, 36px); font-weight: 600; letter-spacing: -.01em; }
.ch[data-place="below"] .ch__now .ch__unit { font-size: clamp(16px, 6cqi, 28px); font-weight: 600; }
.ch__trend { margin: 0; font-size: 14px; color: var(--text-secondary); white-space: nowrap; }
.ch__trend[hidden] { display: none; }
.ch__trend[data-dir="down"] { color: var(--success); }
.ch__trend[data-dir="up"] { color: var(--warning); }
