/* 卡片按可用宽度自适应铺开，窄屏退成单列且不跟同行最高的卡片等高拉伸。 */
.kxed-widgets {
  --kxed-widgets-track: 28rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(var(--kxed-widgets-track), 100%), 1fr));
  align-items: start;
  gap: var(--kxed-space-4);
}

/* 跨度按 auto-fit 当前能放下的列数封顶：超过显式列数的跨度会生出隐式窄列，把单列页面撑出横向滚动。 */
.kxed-widgets > .kxed-widget {
  grid-column: span min(var(--kxed-widget-span, 1), max(1, round(down, (100cqi + var(--kxed-space-4)) / (var(--kxed-widgets-track) + var(--kxed-space-4)))));
}

.kxed-widget {
  display: grid;
  align-content: start;
  gap: var(--kxed-space-4);
  min-width: 0;
  border: 1px solid var(--kxed-divider);
  border-radius: var(--kxed-radius-md);
  padding: var(--kxed-space-4);
  background: var(--kxed-surface);
  box-shadow: var(--kxed-shadow-xs);
}

/* 统计卡片组里每块数字自己就是一张贴地的指标卡，外壳只保留标题与页脚，避免卡片套卡片。 */
.kxed-widget:has(> .kxed-widget-body > .kxed-stat-grid) {
  border: 0;
  padding: 0;
  background: none;
  box-shadow: none;
}

.kxed-widget-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  column-gap: var(--kxed-space-3);
  row-gap: var(--kxed-space-1);
  align-items: start;
}

.kxed-widget-heading,
.kxed-widget-description {
  grid-column: 1;
}

.kxed-widget-heading {
  margin: 0;
  color: var(--kxed-text);
  font-size: var(--kxed-font-size-md);
  font-weight: var(--kxed-font-weight-semibold);
  line-height: var(--kxed-line-height-md);
}

.kxed-widget-description {
  margin: 0;
  color: var(--kxed-muted);
  font-size: var(--kxed-font-size-sm);
  line-height: var(--kxed-line-height-sm);
}

.kxed-widget-extra {
  display: flex;
  grid-column: 2;
  grid-row: 1 / span 2;
  align-items: center;
  gap: var(--kxed-space-2);
  font-size: var(--kxed-font-size-sm);
  line-height: var(--kxed-line-height-sm);
}

.kxed-widget-body {
  display: grid;
  gap: var(--kxed-space-3);
  min-width: 0;
}

/* 页脚是通栏分割线下的辅助信息，负边距让分割线贴齐卡片两侧。 */
.kxed-widget-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--kxed-space-2);
  margin-inline: calc(-1 * var(--kxed-space-4));
  border-top: 1px solid var(--kxed-divider);
  padding: var(--kxed-space-3) var(--kxed-space-4) 0;
  color: var(--kxed-muted);
  font-size: var(--kxed-font-size-sm);
  line-height: var(--kxed-line-height-sm);
}

/* 单块卡片的状态说明与更新时间：辅助文字，读取失败取危险色，过期取警告色，与真实读数区分。
   加载中的说明不做透明度呼吸：kxed-pulse 的低谷让辅助文字对比度低于 4.5:1。 */
.kxed-widget-status {
  margin: 0;
  color: var(--kxed-muted);
  font-size: var(--kxed-font-size-sm);
  line-height: var(--kxed-line-height-sm);
}

.kxed-widget-status[role="alert"] {
  color: var(--kxed-danger-text);
}

.kxed-widget-updated {
  margin-inline-start: auto;
  font-size: var(--kxed-font-size-xs);
  line-height: var(--kxed-line-height-xs);
}

.kxed-widget[data-kxed-stale] .kxed-widget-updated {
  color: var(--kxed-warning-text);
}

.kxed-widget:has(> .kxed-widget-body > .kxed-stat-grid) > .kxed-widget-footer {
  margin-inline: 0;
  border-top: 0;
  padding: 0;
}

.kxed-stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: var(--kxed-space-4);
}

.kxed-stat {
  position: relative;
  display: grid;
  align-content: start;
  gap: var(--kxed-space-1);
  min-width: 0;
  border: 1px solid var(--kxed-divider);
  border-radius: var(--kxed-radius-md);
  padding: var(--kxed-space-4);
  background: var(--kxed-surface);
  transition: background-color var(--kxed-duration-fast) var(--kxed-ease-out);
}

.kxed-stat-label {
  color: var(--kxed-muted);
  font-size: var(--kxed-font-size-sm);
  font-weight: var(--kxed-font-weight-medium);
  line-height: var(--kxed-line-height-sm);
}

/* 图标脱离文档流贴右上角，标签让出图标的宽度，数字基线不被图标高度推动。 */
.kxed-stat:has(> .kxed-stat-icon) > .kxed-stat-label {
  padding-inline-end: var(--kxed-space-5);
}

.kxed-stat-value {
  font-size: var(--kxed-font-size-h2);
  font-weight: var(--kxed-font-weight-semibold);
  line-height: var(--kxed-line-height-h2);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.kxed-stat-description {
  color: var(--kxed-muted);
  font-size: var(--kxed-font-size-xs);
  line-height: var(--kxed-line-height-xs);
}

.kxed-stat-icon {
  position: absolute;
  top: var(--kxed-space-4);
  right: var(--kxed-space-4);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--kxed-icon-size-lg);
  height: var(--kxed-icon-size-lg);
  color: var(--kxed-muted);
  line-height: 1;
}

/* 下钻链接只包住标签文字，伪元素铺满卡片，整张卡片可点而读屏只念一次标签。 */
.kxed-stat-link {
  color: inherit;
  text-decoration: none;
}

.kxed-stat-link::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
}

.kxed-stat-link:focus-visible {
  outline: none;
}

.kxed-stat-link:focus-visible::after {
  outline: var(--kxed-focus-outline);
  outline-offset: var(--kxed-focus-offset);
}

.kxed-stat:has(> .kxed-stat-label > .kxed-stat-link):hover {
  background: var(--kxed-default-hover);
}

.kxed-stat-chart {
  display: block;
  width: 100%;
  height: var(--kxed-row-height-default);
  margin-block: var(--kxed-space-2) var(--kxed-space-1);
  overflow: visible;
}

.kxed-chart {
  display: grid;
  gap: var(--kxed-space-3);
  min-width: 0;
}

/* 服务端把坐标写死在 viewBox 里，SVG 随卡片等比缩放；宽度上下限把刻度文字的缩放比
   限制在可读范围，宽卡片里图形不再无限放大。 */
.kxed-chart-svg {
  display: block;
  width: 100%;
  min-width: min(26rem, 100%);
  max-width: 40rem;
  height: auto;
  margin-inline: auto;
  overflow: visible;
}

/* 空态占住图形的高度，数据到达后卡片不跳动。 */
.kxed-chart-empty {
  display: grid;
  place-items: center;
  margin: 0;
  aspect-ratio: 640 / 240;
  color: var(--kxed-muted);
  font-size: var(--kxed-font-size-sm);
  line-height: var(--kxed-line-height-sm);
  text-align: center;
}

.kxed-chart-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--kxed-space-1) var(--kxed-space-4);
  margin: 0;
  padding: 0;
  font-size: var(--kxed-font-size-xs);
  line-height: var(--kxed-line-height-xs);
  list-style: none;
}

.kxed-chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: var(--kxed-space-2);
}

/* 图例文字保持中性色，只有色块取序列色，color 仍由 currentcolor 传给色块。 */
.kxed-chart-legend-item::before {
  content: "";
  flex: none;
  width: var(--kxed-dot-size-lg);
  height: var(--kxed-dot-size-lg);
  border-radius: 50%;
  background: currentcolor;
}

.kxed-chart-legend-label {
  color: var(--kxed-muted);
}

/* SVG 文字的 font-size 落在 640 宽 viewBox 的用户单位里，随图形缩放；lg 字号是 20 个单位，svg 宽度
   夹在 26rem 到 40rem 之间时换算成 13px 到 20px，手机单列时约 10px。 */
.kxed-chart-axis {
  fill: var(--kxed-muted);
  stroke: var(--kxed-border);
  stroke-width: 1;
  font-size: var(--kxed-font-size-lg);
  font-variant-numeric: tabular-nums;
}

/* SVG 文本继承 stroke 会给字形描边，刻度文字必须显式关掉。 */
.kxed-chart-axis text {
  stroke: none;
}

.kxed-chart-grid {
  stroke: var(--kxed-divider);
  stroke-dasharray: 5 5;
}

.kxed-chart-series {
  fill: none;
  stroke: currentcolor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* 序列色按序号取 Mantine 命名色：light 方案 6 级，dark 方案 8 级。序号与数据无关，
   inline style 里的显式颜色覆盖这里的取值。 */
.kxed-chart-series,
.kxed-chart-legend-item {
  color: var(--kxed-chart-color, var(--kxed-blue-6));
}

:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="0"] { --kxed-chart-color: var(--kxed-blue-6); --kxed-chart-color: light-dark(var(--kxed-blue-6), var(--kxed-blue-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="1"] { --kxed-chart-color: var(--kxed-cyan-6); --kxed-chart-color: light-dark(var(--kxed-cyan-6), var(--kxed-cyan-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="2"] { --kxed-chart-color: var(--kxed-teal-6); --kxed-chart-color: light-dark(var(--kxed-teal-6), var(--kxed-teal-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="3"] { --kxed-chart-color: var(--kxed-green-6); --kxed-chart-color: light-dark(var(--kxed-green-6), var(--kxed-green-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="4"] { --kxed-chart-color: var(--kxed-lime-6); --kxed-chart-color: light-dark(var(--kxed-lime-6), var(--kxed-lime-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="5"] { --kxed-chart-color: var(--kxed-yellow-6); --kxed-chart-color: light-dark(var(--kxed-yellow-6), var(--kxed-yellow-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="6"] { --kxed-chart-color: var(--kxed-orange-6); --kxed-chart-color: light-dark(var(--kxed-orange-6), var(--kxed-orange-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="7"] { --kxed-chart-color: var(--kxed-red-6); --kxed-chart-color: light-dark(var(--kxed-red-6), var(--kxed-red-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="8"] { --kxed-chart-color: var(--kxed-pink-6); --kxed-chart-color: light-dark(var(--kxed-pink-6), var(--kxed-pink-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="9"] { --kxed-chart-color: var(--kxed-grape-6); --kxed-chart-color: light-dark(var(--kxed-grape-6), var(--kxed-grape-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="10"] { --kxed-chart-color: var(--kxed-violet-6); --kxed-chart-color: light-dark(var(--kxed-violet-6), var(--kxed-violet-8)); }
:is(.kxed-chart-series, .kxed-chart-legend-item)[data-series="11"] { --kxed-chart-color: var(--kxed-indigo-6); --kxed-chart-color: light-dark(var(--kxed-indigo-6), var(--kxed-indigo-8)); }
