/**
 * Chart palette, readable on the light and dark surfaces
 */
:root {
  --chart-1: #2364c2;
  --chart-2: #0e8a6a;
  --chart-3: #c2410c;
  --chart-4: #7442c8;
  --chart-5: #c0267a;
  --chart-6: #4d7c0f;
  --chart-7: #0e7490;
  --chart-8: #a16207;
  --chart-9: #be123c;
  --chart-10: #475569;
  --chart-grid: color-mix(in srgb, var(--border) 80%, transparent);
  --chart-crosshair: color-mix(in srgb, var(--text) 45%, transparent);
}

[data-theme='dark'] {
  --chart-1: #60a5fa;
  --chart-2: #34d399;
  --chart-3: #fb923c;
  --chart-4: #b794f6;
  --chart-5: #f472b6;
  --chart-6: #a3e635;
  --chart-7: #22d3ee;
  --chart-8: #facc15;
  --chart-9: #fb7185;
  --chart-10: #94a3b8;
  --chart-crosshair: color-mix(in srgb, var(--text) 55%, transparent);
}

/**
 * Time series chart
 */
.ts-chart {
  margin: 0;
  min-width: 0;
}

.ts-chart-header {
  align-items: baseline;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin-bottom: 6px;
}

.ts-chart-title {
  font-weight: 600;
}

.ts-chart-value {
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.ts-chart-actions {
  align-items: center;
  display: inline-flex;
  gap: 6px;
  margin-left: auto;
}

.ts-chart-plot {
  cursor: crosshair;
  outline: none;
  position: relative;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  width: 100%;
}

.ts-chart-plot:focus-visible {
  border-radius: var(--radius-small);
  box-shadow: 0 0 0 2px var(--accent);
}

.ts-chart-plot.is-loading .ts-chart-svg {
  opacity: 0.55;
  transition: opacity 0.2s ease;
}

.ts-chart-plot.is-selecting {
  cursor: col-resize;
}

.ts-chart-svg {
  display: block;
  left: 0;
  overflow: visible;
  position: absolute;
  top: 0;
}

.ts-chart-grid line {
  shape-rendering: crispEdges;
  stroke: var(--chart-grid);
  stroke-dasharray: 3 3;
  stroke-width: 1;
}

.ts-chart-axis {
  shape-rendering: crispEdges;
  stroke: var(--border-strong);
  stroke-width: 1;
}

.ts-chart-labels {
  color: var(--text-muted);
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  inset: 0;
  line-height: 1;
  pointer-events: none;
  position: absolute;
}

.ts-chart-label-value {
  left: 0;
  overflow: hidden;
  position: absolute;
  text-align: right;
  transform: translateY(-50%);
  white-space: nowrap;
}

.ts-chart-label-time {
  position: absolute;
  transform: translateX(-50%);
  white-space: nowrap;
}

.ts-chart-series {
  --series-color: var(--chart-1);
}

.ts-chart-line {
  fill: none;
  stroke: var(--series-color);
  stroke-linejoin: round;
  stroke-width: 1.75;
}

.ts-chart-area {
  fill: color-mix(in srgb, var(--series-color) 14%, transparent);
  stroke: none;
}

.ts-chart-area.is-stacked {
  fill: color-mix(in srgb, var(--series-color) 45%, transparent);
}

.ts-chart-dot {
  fill: var(--series-color);
}

.ts-chart-overlay {
  inset: 0;
  pointer-events: none;
  position: absolute;
}

.ts-chart-crosshair {
  border-left: 1px dashed var(--chart-crosshair);
  position: absolute;
  width: 0;
}

.ts-chart-selection {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-left: 1px solid color-mix(in srgb, var(--accent) 60%, transparent);
  border-right: 1px solid color-mix(in srgb, var(--accent) 60%, transparent);
  position: absolute;
}

.ts-chart-marker {
  background: var(--series-color);
  border: 2px solid var(--surface);
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--series-color);
  height: 9px;
  position: absolute;
  transform: translate(-50%, -50%);
  width: 9px;
}

.ts-chart-crosshair[hidden], .ts-chart-selection[hidden], .ts-chart-tooltip[hidden] {
  display: none;
}

.ts-chart-message {
  align-items: center;
  color: var(--text-muted);
  display: flex;
  font-size: 0.85rem;
  gap: 8px;
  inset: 0;
  justify-content: center;
  padding: 12px;
  position: absolute;
  text-align: center;
}

.ts-chart-message.tone-danger {
  color: var(--danger);
}

.ts-chart-spinner {
  animation: ts-chart-spin 0.8s linear infinite;
  border: 2px solid var(--border);
  border-radius: 50%;
  border-top-color: var(--accent);
  height: 14px;
  width: 14px;
}

@keyframes ts-chart-spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .ts-chart-spinner {
    animation: none;
  }
}

/**
 * Tooltip, positioned by the chart script within the viewport
 */
.ts-chart-tooltip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  box-shadow: var(--shadow-lg);
  color: var(--text);
  font-size: 0.78rem;
  left: 0;
  max-width: min(360px, calc(100vw - 16px));
  padding: 8px 10px;
  pointer-events: none;
  position: fixed;
  top: 0;
  z-index: 1000;
}

.ts-chart-tooltip-title {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  margin-bottom: 6px;
  white-space: nowrap;
}

.ts-chart-tooltip-row {
  align-items: center;
  display: grid;
  gap: 8px;
  grid-template-columns: auto minmax(0, 1fr) auto;
  line-height: 1.5;
}

.ts-chart-tooltip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ts-chart-tooltip-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  text-align: right;
  white-space: nowrap;
}

.ts-chart-tooltip-more {
  color: var(--text-muted);
  margin-top: 4px;
}

.ts-chart-swatch {
  background: var(--series-color, var(--chart-1));
  border-radius: 2px;
  display: inline-block;
  flex: none;
  height: 10px;
  width: 10px;
}

/**
 * Legend
 */
.ts-chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 6px;
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
}

.ts-chart-legend-item {
  align-items: center;
  background: none;
  border: 0;
  border-radius: var(--radius-small);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  flex-wrap: wrap;
  font: inherit;
  font-size: 0.78rem;
  gap: 2px 8px;
  padding: 3px 6px;
  text-align: left;
}

.ts-chart-legend-item:hover {
  background: var(--surface-3);
}

.ts-chart-legend-item:focus-visible {
  box-shadow: 0 0 0 2px var(--accent);
  outline: none;
}

.ts-chart-legend-item.is-hidden {
  color: var(--text-muted);
  opacity: 0.6;
}

.ts-chart-legend-item.is-hidden .ts-chart-swatch {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--series-color);
}

.ts-chart-legend-name {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.ts-chart-stat {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.ts-chart-stat > span {
  font-size: 0.7rem;
  margin-right: 2px;
  text-transform: uppercase;
}

/**
 * Time range picker
 */
.time-range-picker {
  align-items: center;
  display: inline-flex;
  gap: 2px;
}

.time-range-toggle {
  gap: 6px;
  max-width: 100%;
}

.time-range-label {
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.time-range-back {
  transform: rotate(90deg);
}

.time-range-forward {
  transform: rotate(-90deg);
}

.time-range-menu {
  display: grid;
  gap: 8px;
  grid-template-columns: minmax(150px, auto) minmax(220px, 1fr);
  min-width: 420px;
}

.time-range-presets {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding-right: 8px;
}

.time-range-custom {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.time-range-custom .button {
  align-self: flex-start;
}

@media (max-width: 640px) {
  .time-range-menu {
    grid-template-columns: 1fr;
    max-width: calc(100vw - 24px);
    min-width: min(300px, calc(100vw - 24px));
  }

  .time-range-presets {
    border-bottom: 1px solid var(--border);
    border-right: 0;
    padding: 0 0 8px;
  }

  .time-range-label {
    max-width: 46vw;
  }

  .ts-chart-legend-item {
    font-size: 0.74rem;
  }
}
