/* ==========================================================================
   Tradestie Interactive SVG Bubble Chart Engine (ts_bubble_chart.css)
   Zero-dependency, WCAG AA Light & Dark compliant, 375px mobile safe
   ========================================================================== */

:root {
  --tsb-bg: #ffffff;
  --tsb-surface: #f8fafc;
  --tsb-surface-hover: #f1f5f9;
  --tsb-border: #cbd5e1;
  --tsb-border-subtle: #e2e8f0;

  --tsb-text: #0f172a;
  --tsb-text-muted: #475569;
  --tsb-axis-text: #334155;
  --tsb-quad-text: #475569;

  --tsb-grid: #e2e8f0;
  --tsb-ref-line: #64748b;
  --tsb-ref-bg: #f1f5f9;
  --tsb-ref-text: #334155;

  --tsb-accent: #0e7490;
  --tsb-accent-hover: #155e75;
  --tsb-accent-text: #ffffff;
  --tsb-accent-soft: #ecfeff;
  --tsb-accent-soft-text: #0e7490;
  --tsb-accent-soft-border: #a5f3fc;

  --tsb-highlight-ring: #b45309;
  --tsb-highlight-bg: #fef3c7;
  --tsb-highlight-text: #92400e;
  --tsb-highlight-border: #f59e0b;

  --tsb-badge-bg: #f1f5f9;
  --tsb-badge-text: #334155;
  --tsb-badge-border: #cbd5e1;

  --tsb-tooltip-bg: #ffffff;
  --tsb-tooltip-border: #cbd5e1;
  --tsb-tooltip-text: #0f172a;
  --tsb-tooltip-muted: #475569;
  --tsb-tooltip-divider: #e2e8f0;
  --tsb-tooltip-shadow: 0 12px 28px -6px rgba(15, 23, 42, 0.18), 0 4px 10px -2px rgba(15, 23, 42, 0.08);

  --tsb-bubble-stroke: #ffffff;
  --tsb-label-halo: rgba(255, 255, 255, 0.94);

  --tsb-radius: 12px;
  --tsb-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

html[data-theme="dark"],
[data-bs-theme="dark"],
.tsb-force-dark {
  --tsb-bg: #0f172a;
  --tsb-surface: #1e293b;
  --tsb-surface-hover: #334155;
  --tsb-border: #334155;
  --tsb-border-subtle: #1e293b;

  --tsb-text: #f8fafc;
  --tsb-text-muted: #cbd5e1;
  --tsb-axis-text: #cbd5e1;
  --tsb-quad-text: #94a3b8;

  --tsb-grid: #1e293b;
  --tsb-ref-line: #64748b;
  --tsb-ref-bg: #1e293b;
  --tsb-ref-text: #e2e8f0;

  --tsb-accent: #38bdf8;
  --tsb-accent-hover: #7dd3fc;
  --tsb-accent-text: #0f172a;
  --tsb-accent-soft: #164e63;
  --tsb-accent-soft-text: #e0f2fe;
  --tsb-accent-soft-border: #0e7490;

  --tsb-highlight-ring: #fbbf24;
  --tsb-highlight-bg: #451a03;
  --tsb-highlight-text: #fde68a;
  --tsb-highlight-border: #d97706;

  --tsb-badge-bg: #1e293b;
  --tsb-badge-text: #e2e8f0;
  --tsb-badge-border: #475569;

  --tsb-tooltip-bg: #0b1220;
  --tsb-tooltip-border: #475569;
  --tsb-tooltip-text: #f8fafc;
  --tsb-tooltip-muted: #cbd5e1;
  --tsb-tooltip-divider: #1e293b;
  --tsb-tooltip-shadow: 0 14px 32px rgba(0, 0, 0, 0.6);

  --tsb-bubble-stroke: #0f172a;
  --tsb-label-halo: rgba(15, 23, 42, 0.94);
}

/* Outer Card */
.tsb-card {
  font-family: var(--tsb-font);
  background: var(--tsb-bg);
  color: var(--tsb-text);
  border: 1px solid var(--tsb-border);
  border-radius: var(--tsb-radius);
  padding: 16px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  position: relative;
}

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

/* Header */
.tsb-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px 16px;
  margin-bottom: 12px;
}

.tsb-heading-wrap {
  min-width: 0;
  flex: 1 1 240px;
}

.tsb-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--tsb-text);
}

.tsb-subtitle {
  margin: 4px 0 0;
  font-size: 0.85rem;
  line-height: 1.45;
  color: var(--tsb-text-muted);
}

/* View Tabs */
.tsb-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.tsb-tab {
  appearance: none;
  border: 1px solid var(--tsb-border);
  background: var(--tsb-surface);
  color: var(--tsb-text);
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  line-height: 1.2;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.tsb-tab:hover {
  background: var(--tsb-surface-hover);
  border-color: var(--tsb-accent);
}

.tsb-tab.is-active {
  background: var(--tsb-accent);
  border-color: var(--tsb-accent);
  color: var(--tsb-accent-text);
}

/* Controls Row (Axes + Meta Badges) */
.tsb-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  margin-bottom: 10px;
}

.tsb-axes {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.tsb-axis-btn {
  appearance: none;
  border: 1px solid var(--tsb-border);
  background: var(--tsb-surface);
  color: var(--tsb-text);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.2;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.tsb-axis-btn:hover {
  background: var(--tsb-surface-hover);
  border-color: var(--tsb-accent);
}

.tsb-axis-btn.is-active {
  background: var(--tsb-accent-soft);
  border-color: var(--tsb-accent);
  color: var(--tsb-accent-soft-text);
}

.tsb-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.tsb-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.76rem;
  font-weight: 600;
  line-height: 1.2;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--tsb-badge-bg);
  color: var(--tsb-badge-text);
  border: 1px solid var(--tsb-badge-border);
  font-variant-numeric: tabular-nums;
}

.tsb-badge-highlight {
  background: var(--tsb-highlight-bg);
  color: var(--tsb-highlight-text);
  border-color: var(--tsb-highlight-border);
}

.tsb-ring-swatch {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px dashed var(--tsb-highlight-ring);
  display: inline-block;
  flex-shrink: 0;
}

/* Chart Stage */
.tsb-stage {
  position: relative;
  width: 100%;
  background: var(--tsb-surface);
  border: 1px solid var(--tsb-border-subtle);
  border-radius: 10px;
  overflow: visible;
}

.tsb-svg {
  display: block;
  width: 100%;
  height: auto;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

/* SVG Elements */
.tsb-grid-line {
  stroke: var(--tsb-grid);
  stroke-width: 1;
}

.tsb-axis-line {
  stroke: var(--tsb-border);
  stroke-width: 1.2;
}

.tsb-ref-line {
  stroke: var(--tsb-ref-line);
  stroke-width: 1.2;
  stroke-dasharray: 4 4;
}

.tsb-ref-pill-bg {
  fill: var(--tsb-ref-bg);
  stroke: var(--tsb-border);
  stroke-width: 1;
}

.tsb-ref-pill-text {
  fill: var(--tsb-ref-text);
  font-size: 10.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.tsb-tick-text {
  fill: var(--tsb-axis-text);
  font-size: 11px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.tsb-axis-title {
  fill: var(--tsb-text);
  font-size: 12px;
  font-weight: 700;
}

.tsb-quad-label {
  fill: var(--tsb-quad-text);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.tsb-bubble-group {
  cursor: pointer;
  transition: opacity 0.16s ease;
  outline: none;
}

.tsb-bubble-group.is-dimmed {
  opacity: 0.14;
}

.tsb-bubble-circle {
  stroke: var(--tsb-bubble-stroke);
  stroke-width: 1.6;
  transition: stroke-width 0.14s ease, filter 0.14s ease;
}

.tsb-bubble-group:hover .tsb-bubble-circle,
.tsb-bubble-group:focus-visible .tsb-bubble-circle,
.tsb-bubble-group.is-selected .tsb-bubble-circle {
  stroke: var(--tsb-text);
  stroke-width: 2.5;
}

.tsb-bubble-ring {
  fill: none;
  stroke: var(--tsb-highlight-ring);
  stroke-width: 2;
  stroke-dasharray: 4 3;
  pointer-events: none;
}

.tsb-label-inside {
  font-size: 10.5px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.tsb-label-outside {
  fill: var(--tsb-text);
  font-size: 11px;
  font-weight: 700;
  paint-order: stroke;
  stroke: var(--tsb-label-halo);
  stroke-width: 3.5px;
  stroke-linejoin: round;
  pointer-events: none;
}

.tsb-empty-title {
  fill: var(--tsb-text);
  font-size: 15px;
  font-weight: 700;
  text-anchor: middle;
}

.tsb-empty-sub {
  fill: var(--tsb-text-muted);
  font-size: 12.5px;
  font-weight: 500;
  text-anchor: middle;
}

/* Floating Tooltip */
.tsb-tooltip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 30;
  display: none;
  width: max-content;
  min-width: 200px;
  max-width: min(295px, calc(100% - 16px));
  background: var(--tsb-tooltip-bg);
  color: var(--tsb-tooltip-text);
  border: 1px solid var(--tsb-tooltip-border);
  border-radius: 10px;
  padding: 10px 12px;
  box-shadow: var(--tsb-tooltip-shadow);
  font-size: 0.8rem;
  line-height: 1.4;
  pointer-events: none;
  word-wrap: break-word;
}

.tsb-tooltip.is-visible {
  display: block;
}

.tsb-tooltip.is-pinned {
  pointer-events: auto;
}

.tsb-tt-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.tsb-tt-title {
  font-weight: 700;
  font-size: 0.86rem;
  color: var(--tsb-tooltip-text);
}

.tsb-tt-tag {
  flex-shrink: 0;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--tsb-highlight-bg);
  color: var(--tsb-highlight-text);
  border: 1px solid var(--tsb-highlight-border);
}

.tsb-tt-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--tsb-tooltip-muted);
  margin-bottom: 8px;
}

.tsb-tt-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.tsb-tt-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px 12px;
  padding-top: 6px;
  border-top: 1px solid var(--tsb-tooltip-divider);
}

.tsb-tt-k {
  color: var(--tsb-tooltip-muted);
  font-size: 0.76rem;
}

.tsb-tt-v {
  color: var(--tsb-tooltip-text);
  font-weight: 700;
  font-size: 0.78rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.tsb-tt-hint {
  margin-top: 7px;
  padding-top: 6px;
  border-top: 1px solid var(--tsb-tooltip-divider);
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--tsb-accent);
}

/* Interactive Group Legend */
.tsb-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
}

.tsb-legend-chip {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--tsb-border);
  background: var(--tsb-surface);
  color: var(--tsb-text);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.tsb-legend-chip:hover {
  background: var(--tsb-surface-hover);
  border-color: var(--tsb-accent);
}

.tsb-legend-chip.is-active {
  background: var(--tsb-accent-soft);
  border-color: var(--tsb-accent);
  color: var(--tsb-accent-soft-text);
}

.tsb-legend-chip.is-muted {
  opacity: 0.55;
}

.tsb-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.tsb-legend-count {
  font-size: 0.72rem;
  color: var(--tsb-text-muted);
  font-variant-numeric: tabular-nums;
}

.tsb-legend-chip.is-active .tsb-legend-count {
  color: var(--tsb-accent-soft-text);
}

/* Footer (Gated Note / CTA) */
.tsb-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--tsb-border-subtle);
}

.tsb-note {
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--tsb-text-muted);
  margin: 0;
  flex: 1 1 220px;
}

.tsb-cta {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 6px 14px;
  border-radius: 8px;
  background: var(--tsb-accent);
  color: var(--tsb-accent-text);
  font-size: 0.82rem;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 0.15s ease;
}

.tsb-cta:hover,
.tsb-cta:focus-visible {
  background: var(--tsb-accent-hover);
  color: var(--tsb-accent-text);
  text-decoration: none;
}

/* Focus rings */
.tsb-tab:focus-visible,
.tsb-axis-btn:focus-visible,
.tsb-legend-chip:focus-visible,
.tsb-cta:focus-visible {
  outline: 2px solid var(--tsb-accent);
  outline-offset: 2px;
}

/* Mobile 375px refinements */
@media (max-width: 576px) {
  .tsb-card {
    padding: 12px;
    border-radius: 10px;
  }

  .tsb-title {
    font-size: 0.98rem;
  }

  .tsb-subtitle {
    font-size: 0.8rem;
  }

  .tsb-meta {
    margin-left: 0;
  }

  .tsb-tooltip {
    min-width: 180px;
    padding: 9px 10px;
  }
}
