.unit-curves-panel {
  position: relative;
  overflow: hidden;
}

.unit-curves-heading {
  padding-right: 260px;
}

.unit-range-tabs {
  position: absolute;
  right: 18px;
  top: 16px;
  display: flex;
  gap: 4px;
}

.unit-range-tabs .button {
  min-width: 42px;
  min-height: 32px;
  height: 32px;
  padding: 0 9px;
  font-size: 11px;
}

.unit-range-tabs .button.active {
  border-color: var(--accent, #159766);
  background: color-mix(in srgb, var(--accent, #159766) 13%, transparent);
  color: var(--accent, #159766);
}

.unit-chart-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(330px, 380px);
  gap: 20px;
  padding: 20px 22px 14px;
}

.unit-chart-stage {
  position: relative;
  min-width: 0;
}

.unit-chart {
  display: block;
  width: 100%;
  height: auto;
  min-height: 420px;
  border: 1px solid var(--line, #d7dfdc);
  background: color-mix(in srgb, var(--panel, #fff) 92%, var(--accent, #159766));
  cursor: crosshair;
  touch-action: none;
}

.unit-chart text {
  fill: var(--text-muted, #68737c);
  font: 11px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.unit-grid-line {
  stroke: var(--line, #d7dfdc);
  stroke-width: 1;
}

.unit-zero-line {
  stroke: var(--text-muted, #68737c);
  stroke-width: 1.4;
}

.unit-hover-layer {
  pointer-events: none;
}

.unit-hover-layer line {
  stroke: var(--text-muted, #68737c);
  stroke-width: 1;
  stroke-dasharray: 4 4;
}

.unit-hover-tooltip {
  position: absolute;
  z-index: 3;
  top: 14px;
  width: min(330px, calc(100% - 24px));
  padding: 10px 12px;
  transform: translateX(-50%);
  border: 1px solid var(--line, #d7dfdc);
  background: color-mix(in srgb, var(--panel, #fff) 96%, transparent);
  box-shadow: 0 8px 28px rgba(16, 35, 30, .13);
  pointer-events: none;
  backdrop-filter: blur(7px);
}

.unit-hover-tooltip > b {
  display: block;
  margin-bottom: 7px;
  color: var(--text-muted, #68737c);
  font: 10px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: .06em;
}

.unit-hover-tooltip > div {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 2px 8px;
  align-items: center;
  padding: 4px 0;
}

.unit-hover-tooltip i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.unit-hover-tooltip span {
  overflow: hidden;
  color: var(--text, #18211f);
  font-size: 12px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unit-hover-tooltip strong {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.unit-hover-tooltip small {
  grid-column: 2 / -1;
  color: var(--text-muted, #68737c);
  font: 10px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.unit-curve-legend {
  display: grid;
  align-content: start;
  gap: 6px;
}

.unit-legend-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 3px 10px;
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line, #d7dfdc);
  background: var(--panel, #fff);
  color: inherit;
  font: inherit;
  text-align: left;
  appearance: none;
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease, opacity 140ms ease, transform 140ms ease;
}

.unit-legend-card:hover,
.unit-legend-card:focus-visible {
  border-color: var(--series-color, var(--accent, #159766));
  transform: translateY(-1px);
}

.unit-legend-card:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent, #159766) 45%, transparent);
  outline-offset: 2px;
}

.unit-legend-card.is-visible {
  border-left: 3px solid var(--series-color, var(--accent, #159766));
  background: color-mix(in srgb, var(--panel, #fff) 94%, var(--accent, #159766));
}

.unit-legend-card.is-hidden {
  opacity: .48;
  filter: grayscale(.7);
  border-left: 3px solid transparent;
}

.unit-legend-card.empty.is-visible {
  opacity: .72;
}

.unit-legend-heading {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  min-width: 0;
  gap: 8px;
}

.unit-legend-name {
  display: grid;
  min-width: 0;
  gap: 2px;
}

.unit-legend-card i {
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-radius: 50%;
}

.unit-legend-card b {
  line-height: 1.25;
  overflow-wrap: anywhere;
  white-space: normal;
}

.unit-legend-card span,
.unit-legend-card small {
  color: var(--text-muted, #68737c);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  letter-spacing: .04em;
}

.unit-legend-card .unit-selection-state {
  align-self: start;
  padding: 1px 4px;
  border: 1px solid currentColor;
  font-size: 8px;
  letter-spacing: .08em;
}

.unit-legend-card.is-visible .unit-selection-state {
  color: var(--series-color, var(--accent, #159766));
}

.unit-selection-empty {
  border: 1px solid var(--line, #d7dfdc);
  background: color-mix(in srgb, var(--panel, #fff) 96%, var(--accent, #159766));
}

.unit-legend-card strong {
  justify-self: end;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.unit-legend-card strong.positive {
  color: #159766;
}

.unit-legend-card strong.negative {
  color: #cf3154;
}

.unit-legend-card small {
  grid-column: 1;
  align-self: center;
}

.unit-empty-state {
  display: grid;
  place-items: center;
  min-height: 210px;
  padding: 32px;
  text-align: center;
  border-top: 1px solid var(--line, #d7dfdc);
}

.unit-empty-state p {
  margin: 8px 0 0;
  color: var(--text-muted, #68737c);
}

.unit-definition {
  padding: 12px 22px;
  border-top: 1px solid var(--line, #d7dfdc);
  color: var(--text-muted, #68737c);
  font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: .04em;
}

/* Compact selectable legend: retain all metrics and non-color visibility state.
   Existing series, scale, palette and click handlers are unchanged. */
.unit-curves-panel .unit-curve-legend .unit-legend-card {
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto auto;
  min-height: 0;
  height: auto;
  padding: 8px 10px;
  gap: 5px 10px;
  align-items: center;
  font-size: 13px;
  line-height: 1.3;
}
.unit-curves-panel .unit-legend-card .unit-legend-heading {
  grid-column: 1 / -1;
  grid-row: 1;
  align-items: center;
  gap: 7px;
}
.unit-curves-panel .unit-legend-card .unit-legend-name {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 7px;
}
.unit-curves-panel .unit-legend-card .unit-legend-name b {
  font-size: 13px;
  line-height: 1.3;
}
.unit-curves-panel .unit-legend-card .unit-legend-name span {
  font-size: 9px;
  line-height: 1.3;
}
.unit-curves-panel .unit-legend-card > strong {
  grid-column: 2;
  grid-row: 2;
  margin: 0;
  font-size: 14px;
  line-height: 1.3;
  white-space: nowrap;
}
.unit-curves-panel .unit-legend-card > small {
  grid-column: 1;
  grid-row: 2;
  min-width: 0;
  margin: 0;
  font-size: 10px;
  line-height: 1.4;
  letter-spacing: 0;
  white-space: normal;
  overflow-wrap: anywhere;
}
.unit-curves-panel .unit-legend-card .unit-selection-state {
  padding: 1px 3px;
  font-size: 8px;
  line-height: 1.3;
  align-self: center;
}

@media (max-width: 980px) {
  .unit-curves-heading {
    padding-right: 0;
  }

  .unit-range-tabs {
    position: static;
    padding: 12px 18px 0;
  }

  .unit-chart-wrap {
    grid-template-columns: 1fr;
  }

  .unit-curve-legend {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  }

  .unit-hover-tooltip {
    top: 10px;
  }
}

@media (max-width: 600px) {
  .unit-curves-heading {
    min-height: 66px;
    padding: 10px 12px !important;
  }

  .unit-curves-heading > div:first-child {
    align-items: flex-start;
    gap: 10px;
  }

  .unit-curves-heading .panel-index {
    display: grid !important;
    width: 38px;
    min-width: 38px;
    height: 38px;
  }

  .unit-curves-heading h2 {
    display: block !important;
    font-size: 14px !important;
    line-height: 1.2;
  }

  .unit-curves-heading p {
    display: block !important;
    margin-top: 3px;
    font-size: 9px !important;
    line-height: 1.45;
    letter-spacing: .03em;
  }

  .unit-range-tabs {
    position: relative !important;
    inset: auto !important;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    align-items: center;
    width: 100%;
    min-height: 42px;
    margin: 0;
    padding: 5px 10px;
    gap: 5px;
    border-bottom: 1px solid var(--line, #d7dfdc);
    background: var(--panel, #fff);
  }

  .unit-range-tabs .button {
    min-width: 0;
    min-height: 30px;
    height: 30px;
    padding: 0;
    font-size: 11px;
  }

  .unit-chart-wrap {
    gap: 10px;
    padding: 10px;
  }

  .unit-chart {
    width: 100%;
    height: auto;
    min-height: 0;
    aspect-ratio: 1000 / 420;
    touch-action: pan-y;
  }

  .unit-chart text {
    font-size: 14px;
  }

  .unit-hover-tooltip {
    top: auto;
    right: 8px;
    bottom: 8px;
    left: 8px !important;
    width: auto;
    padding: 7px 9px;
    transform: none;
  }

  .unit-curve-legend {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .unit-legend-card {
    gap: 2px 8px;
    min-height: 0;
    padding: 6px 8px;
  }

  .unit-legend-heading {
    gap: 6px;
  }

  .unit-legend-card i {
    width: 7px;
    height: 7px;
  }

  .unit-legend-card b {
    font-size: 12px;
    line-height: 1.15;
  }

  .unit-legend-card strong {
    font-size: 13px;
    line-height: 1.15;
  }

  .unit-legend-card span,
  .unit-legend-card small {
    font-size: 8px;
    line-height: 1.25;
  }

  .unit-legend-card .unit-selection-state {
    padding: 1px 4px;
    font-size: 7px;
  }

  .unit-definition {
    padding: 9px 10px;
    font-size: 9px;
    line-height: 1.5;
    overflow-wrap: anywhere;
  }
}
