:root {
  --chart-primary: #3b82f6;
  --chart-secondary: #10b981;
  --chart-text: #1f2937;
  --chart-grid: #e5e7eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --chart-primary: #60a5fa;
    --chart-secondary: #34d399;
    --chart-text: #f3f4f6;
    --chart-grid: #374151;
  }
}

/* Chart Container - Layout for charts with legends */
.chart-container {
  display: flex;
  flex-direction: column;
  inline-size: 100%;
  block-size: 100%;
  min-block-size: 0;
}

.chart-container > canvas {
  flex: 1;
  min-block-size: 0;
}

/* Row Charts Layout */
/* Override turbo-frame height constraint for row charts */
.report-view-item turbo-frame:has(.row-charts-container) {
  block-size: auto;
}

.row-charts-container {
  display: flex;
  flex-direction: column;
  inline-size: 100%;
}

.row-charts-grid {
  display: grid;
  gap: 0.25rem;
  padding: 1rem;
  inline-size: 100%;
  block-size: 100%;
}

.row-chart-panel {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.3rem;
  background: var(--surface);
  min-inline-size: 0;
  /* Allow panels to shrink below intrinsic size */

  >.row-chart-header {

    font-size: 0.6rem;
    font-weight: bold;
    color: light-dark(var(--zinc-600), var(--zinc-300));
    display: flex;
    overflow: hidden;
    flex-wrap: nowrap;
    gap: .2rem;
    align-items: start;
    margin-block-end: 0.4rem;

    >.row-chart-label {
      background: light-dark(#e8e8e8, var(--zinc-800));
      border-radius: 2rem;
      padding: 0.1rem 0.5rem;
      text-overflow: ellipsis;
      text-wrap-mode: nowrap;
      overflow: hidden;
      cursor: pointer;
    }
  }
}

.row-chart-panel .chart-container {
  flex: 1;
  min-inline-size: 0;
  /* Allow container to shrink */
}

.row-chart-panel canvas {
  inline-size: 100% !important;
  block-size: 100% !important;
}

/* Chart Tooltip - External tooltip for Chart.js */
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  z-index: 9999;

  /* Match card pattern */
  background-color: var(--color-surface);
  border-radius: var(--rounded-md);
  border-width: 1px;
  border-style: solid;
  border-color: var(--color-border);
  box-shadow: var(--shadow-md);

  /* Sizing */
  max-inline-size: 320px;
  min-inline-size: 120px;

  /* Smooth animation for position and opacity changes */
  opacity: 0;
  transition: left 0.15s ease-out,
              top 0.15s ease-out,
              opacity 0.15s ease-out;
}

.chart-tooltip__content {
  padding: var(--size-2);
  display: flex;
  flex-direction: column;
  gap: var(--size-2);
  font-size: var(--text-sm);
  color: var(--color-text);
}

.chart-tooltip__title {
  font-weight: var(--font-medium);
  color: var(--color-text);
  padding-block-end: var(--size-1);
  border-block-end: 1px solid var(--color-border-light);
}

.chart-tooltip__title-item {
  display: flex;
  align-items: center;
  gap: var(--size-1_5);
  padding-block: 2px;
}

.chart-tooltip__body {
  display: flex;
  flex-direction: column;
  gap: var(--size-1);
}

.chart-tooltip__item {
  display: flex;
  align-items: center;
  gap: var(--size-1_5);
  color: var(--color-text);
}

.chart-tooltip__item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chart-tooltip__color-icon {
  --icon-size: 0.75rem;
  flex-shrink: 0;
}

/* Create circle icon using CSS */
.chart-tooltip__color-icon.icon--circle {
  border-radius: 50%;
  background-color: var(--icon-color, currentColor);
  mask-image: none;
}

.chart-tooltip__separator {
  block-size: 1px;
  background-color: var(--color-border-light);
  margin-block: var(--size-1);
}

.chart-tooltip__footer {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  display: flex;
  flex-direction: column;
  gap: var(--size-0_5);
  padding-block-start: var(--size-1);
}

.chart-tooltip__footer-item {
  display: flex;
  align-items: center;
}

/* Chart Legend - Custom legend implementation */
.chart-legend {
  display: flex;
  align-items: center;
  gap: var(--size-1_5);
  padding: var(--size-1_5) var(--size-2);
  font-size: var(--text-xs);
  flex-shrink: 0; /* Prevent legend from shrinking */
}

.chart-legend--top {
  padding-block-end: var(--size-2);
}

.chart-legend--bottom {
  padding-block-start: var(--size-2);
}

.chart-legend__items {
  display: flex;
  flex-wrap: nowrap;
  gap: var(--size-2);
  align-items: center;
  overflow: hidden;
  flex: 1;
}

.chart-legend__item {
  display: flex;
  align-items: center;
  gap: var(--size-1);
  cursor: pointer;
  user-select: none;
  padding: var(--size-0_5) var(--size-1);
  border-radius: var(--rounded-sm);
  transition: background-color 0.15s ease;
  white-space: nowrap;
  flex-shrink: 0;
}

.chart-legend__item:hover {
  background-color: var(--color-surface-hover, light-dark(#f3f4f6, #374151));
}

.chart-legend__item--hidden {
  opacity: 0.4;
}

.chart-legend__item--hidden .chart-legend__label {
  text-decoration: line-through;
}

.chart-legend__color {
  display: block;
  inline-size: 10px;
  block-size: 10px;
  border-radius: 2px;
  border: 1px solid transparent;
  flex-shrink: 0;
}

.chart-legend__label {
  color: var(--color-text);
  font-size: var(--text-xs);
  overflow: hidden;
  text-overflow: ellipsis;
}

.chart-legend__more {
  background: none;
  border: none;
  color: var(--color-link, #3b82f6);
  font-size: var(--text-xs);
  cursor: pointer;
  padding: var(--size-0_5) var(--size-1);
  border-radius: var(--rounded-sm);
  text-decoration: underline;
  flex-shrink: 0;
  transition: background-color 0.15s ease;
}

.chart-legend__more:hover {
  background-color: var(--color-surface-hover, light-dark(#f3f4f6, #374151));
}

/* Legend Popup */
.chart-legend__popup {
  position: fixed;
  z-index: 9999;
  background-color: var(--color-surface);
  border-radius: var(--rounded-md);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-lg);
  min-inline-size: 200px;
  max-inline-size: 320px;
  display: flex;
  flex-direction: column;
  gap: var(--size-2);
  padding: var(--size-2);
}

.chart-legend__search {
  inline-size: 100%;
  padding: var(--size-1_5) var(--size-2);
  border: 1px solid var(--color-border);
  border-radius: var(--rounded-sm);
  font-size: var(--text-sm);
  background-color: var(--color-surface);
  color: var(--color-text);
}

.chart-legend__search:focus {
  outline: 2px solid var(--color-primary, #3b82f6);
  outline-offset: 0;
}

.chart-legend__search::placeholder {
  color: var(--color-text-subtle);
}

.chart-legend__popup-items {
  display: flex;
  flex-direction: column;
  gap: var(--size-0_5);
  overflow-y: auto;
  overflow-x: hidden;
}

.chart-legend__popup-item {
  display: flex;
  align-items: center;
  gap: var(--size-1);
  cursor: pointer;
  user-select: none;
  padding: var(--size-1) var(--size-1_5);
  border-radius: var(--rounded-sm);
  transition: background-color 0.15s ease;
  position: relative;
}

.chart-legend__popup-item:hover {
  background-color: var(--color-surface-hover, light-dark(#f3f4f6, #374151));
}

.chart-legend__popup-item.chart-legend__item--hidden {
  opacity: 0.4;
}

.chart-legend__popup-item.chart-legend__item--hidden .chart-legend__label {
  text-decoration: line-through;
}

.chart-legend__only {
  position: absolute;
  right: var(--size-1_5);
  top: 50%;
  transform: translateY(-50%);
  font-size: var(--text-xs);
  color: var(--color-link, #3b82f6);
  text-decoration: underline;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  z-index: 10;
  background-color: var(--color-surface);
  padding: var(--size-0_5) var(--size-1);
  border-radius: var(--rounded-sm);
}

.chart-legend__popup-item:hover .chart-legend__only {
  opacity: 1;
  pointer-events: auto;
}

.chart-legend__only:hover {
  color: var(--color-link-hover, #2563eb);
}
