/* Widget system (js/widgets.js, js/ui/charts.js).
   Everything a widget draws is a class on the theme tokens, so a theme or accent
   change reaches every widget and none carries its own colours. The card is a
   size container: type scales with the card (cqh / cqw), not the screen. */

/* ---- Card ---- */
.widget-card-outer {
  container-type: size;
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--sf);
  border: 1px solid var(--ln);
  border-radius: var(--r-lg);
  user-select: none;
  transition: border-color var(--t-out);
}
.widget-card-outer[data-selected="true"] { border-color: var(--ac); }
.widget-card-outer[data-editing="true"] { cursor: grab; }

.w-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  height: 36px;
  padding: 0 var(--sp-3);
}
.w-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font: 500 var(--fs-sm) / 1 var(--font-body);
  color: var(--tx2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.w-tag {
  flex: none;
  padding: 2px 6px;
  border-radius: var(--r-pill);
  font: 500 var(--fs-2xs) / 1.2 var(--font-body);
  color: var(--tx2);
  background: var(--sf2);
  border: 1px solid var(--ln);
}
.w-tag--warn { color: var(--amb-ink); background: var(--warn-bg); border-color: var(--warn-ln); }
.w-tag--bad { color: var(--red-ink); background: var(--bad-bg); border-color: var(--bad-ln); }
.w-cfg {
  flex: none;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border: 1px solid var(--ln);
  border-radius: var(--r-sm);
  background: var(--sf2);
  color: var(--tx2);
  cursor: pointer;
}
.w-cfg:hover { color: var(--tx); }
.w-resize-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 16px;
  height: 16px;
  margin: 4px;
  cursor: nwse-resize;
  border-right: 2px solid var(--tx2);
  border-bottom: 2px solid var(--tx2);
  border-radius: 0 0 var(--r-md) 0;
}
.w-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0 var(--sp-3) var(--sp-3);
}
.w-scroll { overflow-y: auto; }

/* ---- Empty and message states ---- */
.w-empty {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 10px 14px 14px;
  text-align: center;
}
.w-empty__title { font: 500 var(--fs-sm) / 1.3 var(--font-body); color: var(--tx2); }
.w-empty__sub { font: 450 var(--fs-xs) / 1.4 var(--font-body); color: var(--tx2); opacity: 0.75; max-width: 28ch; }

/* ---- Status tones, shared by value text, dots and bars ---- */
.w-tone--ok { --w-tone: var(--ok-ink); --w-fill: var(--ok); }
.w-tone--warn { --w-tone: var(--amb-ink); --w-fill: var(--amb); }
.w-tone--bad { --w-tone: var(--red-ink); --w-fill: var(--red); }
.w-tone--idle { --w-tone: var(--tx2); --w-fill: var(--tx2); }

/* ---- KPI number ---- */
.w-kpi {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
}
.w-kpi--center { align-items: center; text-align: center; }
.w-kpi__value {
  font: 700 clamp(24px, min(24cqh, 17cqw), 60px) / 1 var(--font-display);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: var(--w-tone, var(--ok-ink));
}
.w-kpi__sub { font: 450 var(--fs-xs) / 1.3 var(--font-body); color: var(--tx2); }
.w-kpi__spark { margin-top: auto; padding-top: var(--sp-2); height: clamp(0px, 26cqh, 48px); flex: none; }
.w-spark { display: block; width: 100%; height: 100%; }
.w-spark__line { fill: none; stroke: var(--w-fill, var(--ac)); stroke-width: 1.5; }
.w-spark__area { fill: var(--w-fill, var(--ac)); opacity: 0.12; }

/* ---- Gauge and donut ---- */
.w-gauge { flex: 1; min-height: 0; position: relative; display: flex; align-items: flex-end; justify-content: center; }
.w-gauge svg { width: 100%; max-height: 100%; display: block; }
.w-gauge__track { fill: none; stroke: var(--sf2); stroke-width: 9; stroke-linecap: round; }
.w-gauge__fill { fill: none; stroke: var(--w-fill, var(--ac)); stroke-width: 9; stroke-linecap: round; }
.w-gauge__read { position: absolute; left: 0; right: 0; bottom: 14%; display: flex; flex-direction: column; align-items: center; }
.w-gauge__value { font: 700 clamp(16px, 13cqh, 32px) / 1 var(--font-display); font-variant-numeric: tabular-nums; color: var(--tx); }
.w-gauge__sub { font: 450 var(--fs-2xs) / 1.3 var(--font-body); color: var(--tx2); }
.w-gauge__scale { position: absolute; left: 12%; right: 12%; bottom: 0; display: flex; justify-content: space-between; font: 450 var(--fs-2xs) / 1 var(--font-body); color: var(--tx2); }
.w-donut { flex: 1; min-height: 0; display: grid; place-items: center; }
.w-donut__ring { position: relative; height: 92%; aspect-ratio: 1; max-width: 100%; }
.w-donut svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.w-donut__read { position: absolute; inset: 0; display: grid; place-items: center; }

/* ---- Charts ---- */
.w-chart {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr auto;
  column-gap: var(--sp-2);
  row-gap: 4px;
}
.w-chart__y {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  text-align: right;
  font: 450 var(--fs-2xs) / 1 var(--font-body);
  color: var(--tx2);
  font-variant-numeric: tabular-nums;
}
.w-chart__plot { position: relative; min-height: 0; }
.w-chart__plot svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; overflow: visible; }
.w-chart__grid { stroke: var(--ln); stroke-width: 1; }
.w-chart__grid--base { stroke: color-mix(in srgb, var(--tx) 22%, transparent); }
.w-chart__thr { stroke-width: 1; stroke-dasharray: 3 3; }
.w-chart__thr--warn { stroke: var(--amb); }
.w-chart__thr--bad { stroke: var(--red); }
.w-chart__line { fill: none; stroke: var(--w-fill, var(--ac)); stroke-width: 1.75; stroke-linejoin: round; stroke-linecap: round; }
.w-chart__area { fill: var(--w-fill, var(--ac)); opacity: 0.13; }
.w-chart__dot {
  position: absolute;
  width: 7px;
  height: 7px;
  margin: -3.5px 0 0 -3.5px;
  border-radius: 50%;
  background: var(--w-fill, var(--ac));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--w-fill, var(--ac)) 25%, transparent);
}
.w-chart__x {
  grid-column: 2;
  display: flex;
  justify-content: space-between;
  font: 450 var(--fs-2xs) / 1 var(--font-body);
  color: var(--tx2);
}
.w-chart__plot--bars { display: flex; }
.w-chart__bars { position: relative; flex: 1; display: flex; align-items: flex-end; gap: 3px; padding: 0 2px; }
.w-chart__bar { flex: 1; min-width: 2px; background: var(--w-fill, var(--ac)); border-radius: 2px 2px 0 0; opacity: 0.9; }
.w-chart__gridline { position: absolute; left: 0; right: 0; height: 0; border-top: 1px solid var(--ln); }
.w-chart__gridline--base { border-top-color: color-mix(in srgb, var(--tx) 22%, transparent); }
.w-chart__head { display: flex; align-items: baseline; gap: var(--sp-2); padding-bottom: 4px; }
.w-chart__now { font: 700 var(--fs-xl) / 1 var(--font-display); font-variant-numeric: tabular-nums; color: var(--tx); }
.w-chart__unit { font: 450 var(--fs-xs) / 1 var(--font-body); color: var(--tx2); }

/* ---- Summary line above a list or grid ---- */
.w-meta { flex: none; display: flex; flex-wrap: wrap; gap: 2px var(--sp-3); padding-bottom: var(--sp-2); font: 450 var(--fs-xs) / 1.3 var(--font-body); color: var(--tx2); }
.w-meta b { font-weight: 600; color: var(--tx); font-variant-numeric: tabular-nums; }

/* ---- Rows (agents, stat lists) ---- */
.w-rows { flex: 1; min-height: 0; overflow-y: auto; }
.w-row {
  display: grid;
  grid-template-columns: 8px minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--sp-2);
  min-height: 28px;
  padding: 0 var(--sp-1);
  border-radius: var(--r-sm);
}
.w-row:hover { background: var(--row-hover); }
.w-row + .w-row { border-top: 1px solid var(--ln); }
.w-row__name { min-width: 0; font: 500 var(--fs-sm) / 1.2 var(--font-body); color: var(--tx); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-row__state { font: 450 var(--fs-xs) / 1 var(--font-body); color: var(--tx2); }
.w-row--plain { grid-template-columns: minmax(0, 1fr) auto; }
.w-row__value { font: 600 var(--fs-sm) / 1 var(--font-body); font-variant-numeric: tabular-nums; color: var(--w-tone, var(--tx)); }
.w-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--w-fill, var(--ac)); }

/* ---- Presence grid ---- */
.w-presence { flex: 1; min-height: 0; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); gap: 4px; align-content: start; }
.w-ext {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 var(--sp-2);
  border-radius: var(--r-sm);
  background: var(--sf2);
  font: 500 var(--fs-xs) / 1 var(--font-body);
  font-variant-numeric: tabular-nums;
  color: var(--tx);
}
.w-ext .w-dot { width: 7px; height: 7px; flex: none; }

/* ---- Tables ---- */
.w-table { width: 100%; border-collapse: collapse; font: 450 var(--fs-xs) / 1.3 var(--font-body); }
.w-table th {
  position: sticky;
  top: 0;
  background: var(--sf);
  padding: 4px var(--sp-2) 6px;
  text-align: left;
  font-weight: 450;
  color: var(--tx2);
  border-bottom: 1px solid var(--ln);
}
.w-table td { padding: 6px var(--sp-2); color: var(--tx); border-bottom: 1px solid var(--ln); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 0; }
.w-table tr:last-child td { border-bottom: none; }
.w-table .is-num { text-align: right; font-variant-numeric: tabular-nums; }
.w-table .is-mute { color: var(--tx2); }
.w-table .is-ok { color: var(--ok-ink); }
.w-table .is-bad { color: var(--red-ink); }

/* ---- Horizontal bars, segments, columns, notes ---- */
.w-hbar { display: grid; grid-template-columns: minmax(48px, 30%) 1fr auto; align-items: center; gap: var(--sp-2); min-height: 24px; }
.w-hbar__label { font: 450 var(--fs-xs) / 1 var(--font-body); color: var(--tx2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-hbar__track { height: 8px; background: var(--sf2); border-radius: var(--r-pill); overflow: hidden; }
.w-hbar__fill { display: block; height: 100%; background: var(--w-fill, var(--ac)); border-radius: var(--r-pill); }
.w-hbar__value { min-width: 28px; text-align: right; font: 600 var(--fs-xs) / 1 var(--font-body); font-variant-numeric: tabular-nums; color: var(--tx); }
.w-seg { flex: 1; min-height: 0; display: flex; flex-direction: column; justify-content: center; gap: var(--sp-3); }
.w-seg__bar { display: flex; height: 12px; border-radius: var(--r-pill); overflow: hidden; background: var(--sf2); }
.w-seg__part { display: block; height: 100%; }
.w-seg__legend { display: flex; flex-wrap: wrap; gap: 4px var(--sp-3); }
.w-seg__key { display: flex; align-items: center; gap: 6px; font: 450 var(--fs-xs) / 1 var(--font-body); color: var(--tx2); }
.w-seg__key b { color: var(--tx); font-weight: 600; font-variant-numeric: tabular-nums; }
.w-seg__swatch { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.w-cols { flex: 1; min-height: 0; display: flex; align-items: stretch; gap: 6px; padding-top: var(--sp-1); }
.w-col { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.w-col__track { flex: 1; width: 100%; display: flex; align-items: flex-end; }
.w-col__bar { width: 100%; min-height: 2px; background: var(--w-fill, var(--ac)); border-radius: 3px 3px 0 0; }
.w-col__label { font: 450 var(--fs-2xs) / 1 var(--font-body); color: var(--tx2); max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-note { flex: 1; min-height: 0; overflow-y: auto; font: 450 var(--fs-sm) / 1.5 var(--font-body); color: var(--tx2); }
.w-placeholder { flex: 1; display: grid; place-items: center; text-align: center; font: 450 var(--fs-xs) / 1.4 var(--font-body); color: var(--tx2); }
