/* ==========================================================================
   Chart Components — AGENT-48
   Shared chart styles for line, bar, donut, KPI card, and heatmap components.
   Color palette per Amber's design spec.
   ========================================================================== */

:root {
  /* Chart color palette — modern palette */
  --chart-indigo: #6366f1;
  --chart-purple: #8b5cf6;
  --chart-pink: #ec4899;
  --chart-cyan: #06b6d4;
  --chart-green: #22c55e;
  --chart-green-light: #bbf7d0;
  --chart-blue: #3b82f6;
  --chart-blue-light: #bfdbfe;
  --chart-violet: #a855f7;
  --chart-rose: #f43f5e;
  --chart-rose-light: #ffe4e6;
  --chart-gray: #6b7280;
  --chart-gray-light: #e5e7eb;

  /* Chart layout */
  --chart-font-family: ui-sans-serif, system-ui, -apple-system, sans-serif;
  --chart-font-size: 12px;
  --chart-tooltip-bg: #ffffff;
  --chart-tooltip-color: #1e293b;
  --chart-tooltip-radius: 8px;
  --chart-grid-color: #e5e7eb;
}

/* ---------- Shared container ---------- */
.chart-container {
  position: relative;
  width: 100%;
  font-family: var(--chart-font-family);
}

.chart-container svg {
  width: 100%;
  height: auto;
  display: block;
  overflow: visible;
}

/* Horizontal scroll wrapper for mobile */
.chart-scroll-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.chart-scroll-wrapper::-webkit-scrollbar {
  height: 4px;
}

.chart-scroll-wrapper::-webkit-scrollbar-thumb {
  background: var(--chart-gray-light);
  border-radius: 2px;
}

/* ---------- Tooltip ---------- */
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: white;
  color: #1e293b;
  font-size: var(--chart-font-size);
  padding: 10px 14px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 50;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.15s ease;
  transform: translate(-50%, 0);
}

.chart-tooltip.visible {
  opacity: 1;
}

.chart-tooltip-label {
  font-weight: 600;
  margin-bottom: 6px;
  font-size: 13px;
  color: #64748b;
}

.chart-tooltip-series {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  font-size: 13px;
}

.chart-tooltip-comp-row {
  margin-top: 1px;
  opacity: 0.75;
  font-size: 12px;
}

.chart-tooltip-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}

.chart-tooltip-dot--comp {
  opacity: 0.6;
}

.chart-tooltip-series-name {
  flex: 1;
}

.chart-tooltip-series-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.chart-tooltip-pct-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 4px;
  white-space: nowrap;
}

.chart-tooltip-pct-badge.positive {
  color: #166534;
  background: rgba(34, 197, 94, 0.2);
}

.chart-tooltip-pct-badge.negative {
  color: #9f1239;
  background: rgba(244, 63, 94, 0.2);
}

/* ---------- Line chart ---------- */
.chart-line-path {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chart-line-path.comparison {
  stroke-dasharray: 6 4;
  opacity: 0.6;
}

.chart-line-area {
  opacity: 0.1;
}

/* ---------- Crosshair hover ---------- */
.chart-hover-overlay {
  pointer-events: all !important;
  fill: rgba(0,0,0,0) !important;
  cursor: crosshair;
}

.chart-crosshair-line {
  stroke: var(--chart-gray-light);
  stroke-width: 1;
  pointer-events: none;
}

.chart-crosshair-dot {
  fill: white;
  stroke-width: 2;
  pointer-events: none;
}

.chart-crosshair-group {
  pointer-events: none;
}

/* ---------- Bar chart ---------- */
.chart-bar {
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.chart-bar:hover {
  opacity: 0.8;
}

/* ---------- Donut chart ---------- */
.chart-donut-segment {
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.15s ease;
  transform-origin: center;
}

.chart-donut-segment:hover {
  opacity: 0.85;
}

.chart-donut-center-value {
  font-size: 24px;
  font-weight: 700;
  fill: #1f2937;
  text-anchor: middle;
  dominant-baseline: central;
}

.chart-donut-center-label {
  font-size: 12px;
  fill: var(--chart-gray);
  text-anchor: middle;
}

.chart-donut-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 12px;
  font-size: var(--chart-font-size);
}

.chart-donut-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chart-donut-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* ---------- KPI card ---------- */
.kpi-card {
  background: white;
  border: 1px solid var(--chart-gray-light);
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.kpi-card-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--chart-gray);
  text-transform: uppercase;
  letter-spacing: 0.025em;
}

.kpi-card-value-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.kpi-card-value {
  font-size: 32px;
  font-weight: 700;
  color: #111827;
  line-height: 1;
}

.kpi-card-delta {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  font-weight: 500;
  padding: 1px 5px;
  border-radius: 4px;
}

.kpi-card-delta.positive {
  color: #166534;
  background: var(--chart-green-light);
}

.kpi-card-delta.negative {
  color: #9f1239;
  background: var(--chart-rose-light);
}

.kpi-card-delta.neutral {
  color: #4b5563;
  background: var(--chart-gray-light);
}

.kpi-card-delta-arrow {
  width: 10px;
  height: 10px;
  display: inline-block;
  vertical-align: middle;
}

.kpi-card-comparison {
  font-size: 12px;
  color: var(--chart-gray);
}

.kpi-card-sparkline {
  margin-top: 8px;
}

.kpi-card-sparkline svg {
  width: 100%;
  height: 32px;
}

.kpi-card-sparkline-line {
  fill: none;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.kpi-card-sparkline-area {
  opacity: 0.1;
}

/* ---------- Heatmap ---------- */
.chart-heatmap-cell {
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.chart-heatmap-cell:hover {
  stroke: #1f2937;
  stroke-width: 1.5;
}

.chart-heatmap-label {
  font-size: 11px;
  fill: var(--chart-gray);
}

/* ---------- Axis labels ---------- */
.chart-axis-label {
  font-size: var(--chart-font-size);
  fill: var(--chart-gray);
  font-family: var(--chart-font-family);
}

.chart-axis-line {
  stroke: var(--chart-grid-color);
  stroke-width: 1;
}

.chart-grid-line {
  stroke: var(--chart-grid-color);
  stroke-width: 0.5;
  stroke-dasharray: 2 2;
}

/* ---------- No data state ---------- */
.chart-no-data {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--chart-gray);
  font-size: 11px;
}

/* ==========================================================================
   Stripe-style Date Range Picker (sdp-*)
   Single row of pill controls with dropdown panel.
   ========================================================================== */

.sdp-form {
  position: relative;
}

/* ── Pill bar — single horizontal row ── */
.sdp-pill-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  flex-wrap: nowrap;
}

/* ── Base pill ── */
.sdp-pill {
  display: inline-flex;
  align-items: center;
  height: 32px;
  padding: 0 12px;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
  font-size: 13px;
  color: #374151;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
  white-space: nowrap;
  gap: 0;
  position: relative;
}

.sdp-pill:hover {
  border-color: #d1d5db;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

/* Combo pill: label | value ▾ */
.sdp-pill--combo {
  gap: 0;
  padding: 0;
}

.sdp-pill__label {
  color: #6b7280;
  font-size: 13px;
  font-weight: 400;
  padding: 0 10px 0 14px;
}

.sdp-pill__sep {
  width: 1px;
  height: 18px;
  background: #e5e7eb;
  flex-shrink: 0;
}

.sdp-pill__value {
  color: #111827;
  font-weight: 500;
  font-size: 13px;
  padding: 0 4px 0 10px;
}

.sdp-pill__chevron {
  width: 14px;
  height: 14px;
  color: #9ca3af;
  flex-shrink: 0;
  margin-right: 10px;
}

.sdp-pill__btn {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: #111827;
  padding: 0 4px;
}

.sdp-pill__btn--enable {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}

/* ── Select-style pill ── */
.sdp-pill--select {
  padding: 0;
  position: relative;
}

.sdp-pill--select > .sdp-pill__btn {
  padding: 0 10px;
  height: 100%;
}

/* ── Compare pill ── */
.sdp-pill--compare {
  gap: 4px;
  padding: 0 12px;
}

.sdp-pill--compare .sdp-pill__value {
  padding: 0;
}

.sdp-pill--dimmed {
  opacity: 0.55;
}

.sdp-pill--dimmed:hover {
  opacity: 0.8;
}

.sdp-pill__x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #f3f4f6;
  border: none;
  cursor: pointer;
  padding: 0;
  color: #6b7280;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.sdp-pill__x:hover {
  background: #e5e7eb;
  color: #374151;
}

.sdp-pill__x svg {
  width: 12px;
  height: 12px;
}

/* ── Dashed pill (custom range) ── */
.sdp-pill--dashed {
  border-style: dashed;
  border-color: #d1d5db;
  background: transparent;
}

.sdp-pill--dashed .sdp-pill__value {
  color: #6b7280;
  font-weight: 400;
}

/* ── Mini menus (interval, compare mode) ── */
.sdp-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 110;
  min-width: 140px;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
  padding: 4px;
  overflow: hidden;
}

.sdp-menu.hidden {
  display: none;
}

.sdp-menu__item {
  display: block;
  width: 100%;
  padding: 7px 12px;
  font-size: 13px;
  color: #374151;
  background: none;
  border: none;
  border-radius: 6px;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s;
}

.sdp-menu__item:hover {
  background: #f3f4f6;
}

.sdp-menu__item--active {
  background: rgba(99, 102, 241, 0.1);
  color: var(--chart-indigo);
  font-weight: 500;
}

/* ═══ Dropdown panel ═══ */
.sdp-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: auto;
  z-index: 100;
  display: flex;
  max-width: calc(100vw - 32px);
  width: 580px;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

.sdp-dropdown.hidden {
  display: none;
}

/* ── Left: presets sidebar ── */
.sdp-dropdown__presets {
  display: flex;
  flex-direction: column;
  padding: 10px 6px;
  border-right: 1px solid #f3f4f6;
  min-width: 150px;
  gap: 2px;
}

.sdp-dropdown__preset {
  padding: 7px 14px;
  font-size: 13px;
  color: #374151;
  background: none;
  border: none;
  border-radius: 8px;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s;
  white-space: nowrap;
}

.sdp-dropdown__preset:hover {
  background: #f3f4f6;
}

.sdp-dropdown__preset--active {
  background: rgba(99, 102, 241, 0.1);
  color: var(--chart-indigo);
  font-weight: 500;
}

/* ── Right: calendar area ── */
.sdp-dropdown__calendar-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
}

/* Date inputs row */
.sdp-dropdown__date-inputs {
  display: flex;
  gap: 12px;
}

.sdp-date-input {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.sdp-date-input__label {
  font-size: 12px;
  font-weight: 500;
  color: #6b7280;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.sdp-date-input__field {
  height: 34px;
  padding: 0 10px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-size: 13px;
  color: #111827;
  background: white;
  transition: border-color 0.15s;
}

.sdp-date-input__field:focus {
  outline: none;
  border-color: var(--chart-indigo);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.15);
}

/* Flatpickr container */
.sdp-dropdown__flatpickr {
  width: 100%;
}

/* Override flatpickr inline styles for Stripe look */
.sdp-dropdown__flatpickr .flatpickr-calendar {
  box-shadow: none !important;
  border: none !important;
  width: 100% !important;
  max-width: 100% !important;
}

.sdp-dropdown__flatpickr .flatpickr-months {
  margin-bottom: 4px;
}

.sdp-dropdown__flatpickr .flatpickr-month {
  height: 32px;
}

.sdp-dropdown__flatpickr .flatpickr-current-month {
  font-size: 11px;
  font-weight: 600;
}

.sdp-dropdown__flatpickr .flatpickr-day {
  border-radius: 8px;
  font-size: 13px;
  line-height: 34px;
  height: 34px;
  max-width: 36px;
}

.sdp-dropdown__flatpickr .flatpickr-day.selected,
.sdp-dropdown__flatpickr .flatpickr-day.startRange,
.sdp-dropdown__flatpickr .flatpickr-day.endRange {
  background: var(--chart-indigo) !important;
  border-color: var(--chart-indigo) !important;
  color: white !important;
}

.sdp-dropdown__flatpickr .flatpickr-day.inRange {
  background: rgba(99, 102, 241, 0.1) !important;
  border-color: rgba(99, 102, 241, 0.1) !important;
  box-shadow: -5px 0 0 rgba(99, 102, 241, 0.1), 5px 0 0 rgba(99, 102, 241, 0.1) !important;
}

.sdp-dropdown__flatpickr .flatpickr-day.today:not(.selected):not(.startRange):not(.endRange) {
  border-color: var(--chart-indigo);
}

/* Action buttons row */
.sdp-dropdown__actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid #f3f4f6;
}

.sdp-btn {
  height: 32px;
  padding: 0 16px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
  border: none;
}

.sdp-btn--ghost {
  background: transparent;
  color: #6b7280;
}

.sdp-btn--ghost:hover {
  background: #f3f4f6;
  color: #374151;
}

.sdp-btn--primary {
  background: var(--chart-indigo);
  color: white;
}

.sdp-btn--primary:hover {
  background: #4f46e5;
  box-shadow: 0 1px 3px rgba(99, 102, 241, 0.3);
}

/* ═══ Compare calendar dropdown ═══ */
.sdp-compare-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 100;
  width: 580px;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sdp-compare-dropdown.hidden {
  display: none;
}

/* ═══ Responsive ═══ */
@media (max-width: 768px) {
  .sdp-pill-bar {
    flex-wrap: wrap;
    height: auto;
    gap: 6px;
  }

  .sdp-dropdown {
    width: calc(100vw - 24px);
    flex-direction: column;
  }

  .sdp-dropdown__presets {
    flex-direction: row;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid #f3f4f6;
    min-width: 0;
    padding: 8px;
  }

  .sdp-compare-dropdown {
    width: calc(100vw - 24px);
  }
}

/* ---------- Print ---------- */
@media print {
  .chart-tooltip {
    display: none !important;
  }

  .chart-container svg {
    max-width: 100%;
  }

  .kpi-card {
    border: 1px solid #d1d5db;
    box-shadow: none;
    break-inside: avoid;
  }

  .chart-scroll-wrapper {
    overflow: visible;
  }
}

/* Mobile chart responsiveness */
@media (max-width: 640px) {
  .chart-container, [data-controller*="chart"] {
    max-width: 100vw;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .chart-container svg, [data-controller*="chart"] svg {
    min-width: 350px;
  }
}

/* Mobile date picker — full width bottom sheet */
@media (max-width: 768px) {
  .sdp-dropdown {
    position: fixed !important;
    top: auto !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    max-width: 100vw !important;
    max-height: 85vh;
    overflow-y: auto;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 30px rgba(0,0,0,0.15);
    flex-direction: column;
  }
  
  .sdp-dropdown__presets {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
    padding: 12px;
    border-right: none;
    border-bottom: 1px solid #e5e7eb;
    min-width: 0;
  }
  
  .sdp-dropdown__presets button {
    padding: 6px 14px;
    font-size: 13px;
  }
  
  .sdp-dropdown__calendar-area {
    padding: 12px;
  }
  
  /* Force flatpickr to single month on mobile */
  .sdp-dropdown__flatpickr .flatpickr-months {
    max-width: 100%;
  }
  .sdp-dropdown__flatpickr .flatpickr-month {
    max-width: 100%;
  }
  .sdp-dropdown__flatpickr .dayContainer {
    max-width: 100%;
    min-width: auto;
  }
  
  .sdp-compare-dropdown {
    position: fixed !important;
    top: auto !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    max-height: 70vh;
    overflow-y: auto;
    border-radius: 16px 16px 0 0;
  }
}

/* Neutral delta (0% change) */
.kpi-card-delta.neutral {
  color: #9ca3af;
}

/* Mobile KPI card fixes */
@media (max-width: 640px) {
  .kpi-card {
    padding: 12px;
    min-width: 0;
    overflow: hidden;
  }
  .kpi-card-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .kpi-card-value {
    font-size: 20px;
  }
  .kpi-card-value-row {
    flex-wrap: wrap;
    gap: 2px;
  }
  .kpi-card-delta {
    font-size: 11px;
    white-space: nowrap;
  }
  .kpi-card-subtitle {
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

/* Ensure grid doesn't overflow on mobile */
@media (max-width: 640px) {
  .grid {
    max-width: 100%;
    overflow: hidden;
  }
  .grid > * {
    min-width: 0;
  }
}

/* KPI card value overflow fix */
.kpi-card {
  overflow: hidden;
  min-width: 0;
}
.kpi-card-value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
  font-size: clamp(18px, 4vw, 32px);
}
