/* Desktop external legend panel (Plotly legend disabled). */
.chart-with-legend {
  display: flex;
  gap: 12px;
  align-items: stretch;
  min-height: 0;
  flex: 1 1 auto;
  position: relative;
}

.chart-with-legend .plot-wrapper {
  flex: 1 1 auto;
  min-width: 0;
}

.external-legend {
  width: 280px;
  min-width: 260px;
  max-width: 320px;
  border: 1px solid rgba(120, 120, 120, 0.35);
  border-radius: 12px;
  padding: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--surface-overlay);
  height: 100%;
  transition: width 160ms ease, min-width 160ms ease, max-width 160ms ease, padding 160ms ease;
}

.external-legend__title {
  font-size: 14px;
  font-weight: 900;
  letter-spacing: 0.01em;
  margin: 2px 2px 10px;
  color: var(--text-color-white);
}

html[data-theme="light"] .external-legend__title {
  color: var(--text-contrast);
}

.external-legend__controls {
  display: flex;
  gap: 8px;
  flex-wrap: nowrap;
  align-items: center;
  margin-bottom: 10px;
  width: 100%;
}

.external-legend-btn {
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: var(--time-lens-button-bg);
  color: var(--time-lens-button-text);
  border-radius: 10px;
  padding: 6px 8px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  white-space: nowrap;
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex: 1 1 0;
}

.external-legend-btn--collapse {
  flex: 0 0 34px;
  padding: 6px 0;
}

.external-legend-btn:hover,
.external-legend-btn:focus-visible {
  background: var(--time-lens-button-hover);
  outline: none;
}

.external-legend__body {
  overflow-y: auto;
  padding-right: 4px;
  min-height: 0;
  flex: 1 1 auto;
}

.external-legend-chips {
  position: absolute;
  top: 28px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  gap: 28px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  user-select: none;
  max-width: min(720px, calc(100% - 140px));
}

.legend-chip {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 4px;
  border-radius: 999px;
  border: none;
  background: transparent;
  backdrop-filter: none;
  color: var(--text-color-white);
  font-size: 15px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
}

html[data-theme="light"] .legend-chip {
  color: var(--text-contrast);
}

.legend-chip:hover {
  opacity: 0.9;
}

.legend-chip:focus-visible {
  outline: 2px solid rgba(148, 163, 184, 0.45);
  outline-offset: 2px;
}

.legend-chip--hidden {
  opacity: 0.55;
}

.legend-chip-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

html[data-theme="light"] .legend-chip-dot {
  border-color: rgba(0, 0, 0, 0.2);
}

.legend-chip-label {
  max-width: none;
  overflow: visible;
  text-overflow: unset;
  white-space: normal;
  overflow-wrap: anywhere;
}

.legend-chip-value {
  font-variant-numeric: tabular-nums;
  opacity: 0.85;
}

.legend-chip-toggle {
  appearance: none;
  border: none;
  background: transparent;
  color: inherit;
  border-radius: 0;
  width: 20px;
  height: 20px;
  padding: 0;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.legend-chip-toggle:hover,
.legend-chip-toggle:focus-visible {
  opacity: 0.85;
  outline: none;
}

.legend-group {
  margin-bottom: 10px;
}

.legend-group__title {
  font-size: 12px;
  font-weight: 700;
  opacity: 0.85;
  margin: 6px 2px;
  color: var(--text-color-lightgrey);
}

.legend-row {
  display: grid;
  grid-template-columns: 14px 1fr auto 34px;
  align-items: center;
  gap: 8px;
  padding: 6px 6px;
  border-radius: 10px;
  border: 1px solid transparent;
}

.legend-row:hover {
  border-color: rgba(148, 163, 184, 0.28);
  background: rgba(148, 163, 184, 0.08);
}

.legend-row--hidden {
  opacity: 0.55;
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  border: 1px solid rgba(0, 0, 0, 0.25);
}

html:not([data-theme="light"]) .legend-swatch {
  border-color: rgba(255, 255, 255, 0.2);
}

.legend-label {
  overflow: visible;
  text-overflow: unset;
  white-space: normal;
  overflow-wrap: anywhere;
  font-size: 14px;
  font-weight: 800;
  color: var(--text-color-white);
}

html[data-theme="light"] .legend-label {
  color: var(--text-contrast);
}

.legend-value {
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-color-lightgrey);
  min-width: 52px;
  text-align: right;
}

html[data-theme="light"] .legend-value {
  color: var(--text-contrast);
  opacity: 0.8;
}

.legend-toggle {
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: transparent;
  color: var(--text-color-lightgrey);
  border-radius: 10px;
  width: 30px;
  height: 28px;
  padding: 0;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.legend-toggle:hover,
.legend-toggle:focus-visible {
  color: var(--text-color-white);
  border-color: rgba(148, 163, 184, 0.45);
  outline: none;
}

html[data-theme="light"] .legend-toggle {
  color: var(--text-contrast);
}

html[data-theme="light"] .external-legend {
  background: rgba(248, 250, 252, 0.85);
  border-color: rgba(51, 65, 85, 0.18);
}

html[data-theme="light"] .external-legend-btn {
  background: var(--time-lens-button-bg-light);
  color: var(--time-lens-button-text-light);
  border-color: rgba(51, 65, 85, 0.18);
}

html[data-theme="light"] .external-legend-btn:hover,
html[data-theme="light"] .external-legend-btn:focus-visible {
  background: rgba(148, 163, 184, 0.35);
}

html[data-theme="light"] .legend-group__title {
  color: var(--text-contrast);
  opacity: 0.75;
}

html[data-theme="light"] .legend-row:hover {
  border-color: rgba(51, 65, 85, 0.18);
  background: rgba(51, 65, 85, 0.06);
}

/* Collapsed side panel: keep only the chevron button. */
.external-legend--collapsed {
  position: absolute;
  top: 10px;
  right: 10px;
  width: auto;
  min-width: 0;
  max-width: none;
  padding: 0;
  border: none;
  background: transparent;
  overflow: visible;
  height: auto;
  z-index: 40;
}

.external-legend--collapsed .external-legend__body {
  display: none;
}

.external-legend--collapsed .external-legend__title {
  display: none;
}

.external-legend--collapsed .external-legend__controls {
  margin-bottom: 0;
  width: auto;
}

.external-legend--collapsed .external-legend-btn:not(.external-legend-btn--collapse) {
  display: none;
}

.external-legend--collapsed .external-legend-btn--collapse {
  flex: 0 0 34px;
  width: 34px;
}

@media (max-width: 1024px) {
  .external-legend {
    display: none;
  }
}

/* Mobile legend sheet (bottom offcanvas). */
.legend-sheet {
  --bs-offcanvas-height: 70vh;
  background: var(--paper-color);
  color: var(--text-color-white);
  border-top: 1px solid rgba(148, 163, 184, 0.28);
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
}

.legend-sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.18);
}

.legend-sheet-title {
  font-size: 14px;
  font-weight: 900;
}

.legend-sheet-close {
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: transparent;
  color: var(--text-color-lightgrey);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.legend-sheet-body {
  padding: 10px 12px calc(14px + env(safe-area-inset-bottom));
}
