/* charts.css — chart cards, table view (accessible twin of every chart), fallback message.
   Uses only the app tokens from app.css; every var() carries a light-theme fallback so the
   file also works on its own (tests/charts_demo.html). */

/* ---------------------------------------------------------------- card */

.chart-card {
  --chart-h: 300px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  margin: 0;
  padding: 16px;
  background: var(--surface, #fcfcfb);
  color: var(--text, #0b0b0b);
  border: 1px solid var(--border, rgba(11, 11, 11, 0.1));
  border-radius: var(--radius, 10px);
  box-shadow: var(--shadow-sm, none);
  font-family: var(--font, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif);
}

.chart-card *,
.chart-card *::before,
.chart-card *::after {
  box-sizing: border-box;
}

/* Guard against app-level display rules overriding the hidden attribute. */
.chart-card [hidden] {
  display: none !important;
}

.chart-card__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.chart-card__titles {
  flex: 1 1 auto;
  min-width: 0;
}

.chart-card__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.35;
  color: var(--text, #0b0b0b);
  overflow-wrap: anywhere;
}

.chart-card__subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-2, #52514e);
  overflow-wrap: anywhere;
}

.chart-card__toggle {
  flex: 0 0 auto;
  appearance: none;
  -webkit-appearance: none;
  margin: 0;
  min-height: 28px;
  padding: 4px 11px;
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.2;
  color: var(--text-2, #52514e);
  background: transparent;
  border: 1px solid var(--border, rgba(11, 11, 11, 0.1));
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.chart-card__toggle:hover {
  color: var(--text, #0b0b0b);
  background: var(--surface-2, #f3f2ee);
}

.chart-card__toggle[aria-pressed="true"] {
  color: var(--text, #0b0b0b);
  background: var(--surface-2, #f3f2ee);
  border-color: var(--axis, #c3c2b7);
}

.chart-card__toggle:focus-visible {
  outline: 2px solid var(--accent, #2a78d6);
  outline-offset: 2px;
}

.chart-card__body {
  position: relative;
  min-width: 0;
}

/* The plot height INCLUDES the axis band: Chart.js fills this box completely
   (maintainAspectRatio:false), so the card never gets a nested scroll. */
.chart-card__plot {
  position: relative;
  width: 100%;
  min-width: 0;
  height: var(--chart-h, 300px);
}

.chart-card__plot > canvas {
  display: block;
  max-width: 100%;
}

/* No-data note over the plot (chartCard().setEmpty(text)); the empty axes stay faintly visible. */
.chart-card__empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  text-align: center;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-2, #52514e);
  background: color-mix(in srgb, var(--surface, #fcfcfb) 78%, transparent);
  border-radius: var(--radius-sm, 6px);
}

.chart-card.is-empty .chart-card__plot > canvas {
  opacity: 0.35;
}

/* ---------------------------------------------------------------- table view */

.chart-card__table {
  min-height: var(--chart-h, 300px);
  max-height: max(360px, var(--chart-h, 300px));
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  border-radius: calc(var(--radius, 10px) - 4px);
}

.chart-card__table:focus-visible {
  outline: 2px solid var(--accent, #2a78d6);
  outline-offset: 2px;
}

.chart-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  line-height: 1.4;
  font-variant-numeric: tabular-nums;
  color: var(--text, #0b0b0b);
}

.chart-table th,
.chart-table td {
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--grid, #e1e0d9);
  white-space: nowrap;
}

.chart-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2, #52514e);
  background: var(--surface, #fcfcfb);
  border-bottom-color: var(--axis, #c3c2b7);
  white-space: normal;
}

.chart-table tbody th {
  min-width: 8em;
  font-weight: 500;
  color: var(--text, #0b0b0b);
  white-space: normal;
}

.chart-table .is-num {
  text-align: right;
}

.chart-table tbody tr:hover > th,
.chart-table tbody tr:hover > td {
  background: var(--surface-2, #f3f2ee);
}

.chart-table tbody tr:last-child > th,
.chart-table tbody tr:last-child > td {
  border-bottom: 0;
}

.chart-table-empty {
  margin: 0;
  padding: 32px 8px;
  text-align: center;
  font-size: 13px;
  color: var(--muted, #898781);
}

/* ---------------------------------------------------------------- fallback */

/* Shown in place of the canvas when Chart.js could not be loaded (or a chart failed). */
.chart-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  min-height: 120px;
  padding: 16px;
  text-align: center;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-2, #52514e);
  background: var(--surface-2, #f3f2ee);
  border-radius: calc(var(--radius, 10px) - 4px);
}

/* ---------------------------------------------------------------- utilities */

.chart-sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* Optional layout helper for views: responsive grid of chart cards. */
.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr));
  gap: 16px;
  min-width: 0;
}

.chart-grid > .chart-card--wide {
  grid-column: 1 / -1;
}

/* ---------------------------------------------------------------- responsive */

@media (max-width: 600px) {
  .chart-card {
    gap: 10px;
    padding: 12px;
  }

  .chart-card__title {
    font-size: 14px;
  }

  .chart-card__subtitle {
    font-size: 12px;
  }

  .chart-table {
    font-size: 12px;
  }

  .chart-table th,
  .chart-table td {
    padding: 5px 8px;
  }

  .chart-grid {
    gap: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chart-card__toggle {
    transition: none;
  }
}

@media (forced-colors: active) {
  .chart-card {
    border: 1px solid CanvasText;
  }

  .chart-card__toggle[aria-pressed="true"] {
    outline: 2px solid Highlight;
  }
}

@media print {
  .chart-card {
    break-inside: avoid;
    box-shadow: none;
  }

  .chart-card__toggle {
    display: none;
  }

  .chart-card__table {
    max-height: none;
    overflow: visible;
  }

  /* charts.js redraws charts for print (≤ A4 width, light chrome); the print CSS may still
     scale a wide canvas down (canvas { height:auto }), so the plot box follows the canvas
     instead of keeping its fixed screen height (no empty band under the chart). */
  .chart-card__plot {
    height: auto !important;
  }
}
