:root {
  --panel-bg: rgba(255, 255, 255, 0.92);
  --panel-border: rgba(24, 39, 75, 0.2);
  --text: #1f2937;
  --muted: #4b5563;
  --control-bg: rgba(255, 255, 255, 0.96);
  --control-border: rgba(15, 23, 42, 0.14);
  --control-shadow: 0 10px 24px rgba(15, 23, 42, 0.16);
  --control-radius: 16px;
  --nli-bar-height: 0px;
}

* {
  box-sizing: border-box;
}

html,
body,
#map {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  font-family: "IBM Plex Sans", "Segoe UI", Tahoma, sans-serif;
  color: var(--text);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.is-hidden {
  display: none !important;
}

.nli-bar {
  position: fixed;
  top: var(--ha-nav-height, 0px);
  left: 0;
  right: 0;
  z-index: 99;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1e3a5f;
  color: #e2e8f0;
  font-size: 13px;
  gap: 4px;
}

.nli-bar a {
  color: #93c5fd;
  text-decoration: underline;
}

.map-controls-right {
  position: fixed;
  top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 12px);
  right: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  z-index: 2;
}

.map-controls-left {
  position: fixed;
  top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 12px);
  left: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 12px;
  z-index: 2;
}

.dataset-panel {
  position: fixed;
  top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 12px);
  left: 50%;
  z-index: 4;
  width: min(420px, calc(100vw - 144px));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.zoom-controls {
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 44px;
  padding: 0;
  border: 0;
  border-radius: 22px;
  background: var(--control-bg);
  box-shadow: var(--control-shadow);
  overflow: hidden;
}

.map-control-button {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--control-border);
  background: var(--control-bg);
  color: var(--text);
  box-shadow: var(--control-shadow);
  cursor: pointer;
  font-family: "IBM Plex Sans", "Segoe UI", Tahoma, sans-serif;
}

.map-control-button:hover,
.map-control-button:focus,
.map-control-button:active {
  border-color: var(--control-border);
  background: var(--control-bg);
  color: var(--text);
  box-shadow: var(--control-shadow);
}

.map-control-button:focus-visible {
  outline: 2px solid rgba(17, 24, 39, 0.18);
  outline-offset: 2px;
}

.map-control-square {
  width: 44px;
  height: 44px;
  border-radius: 0;
  font-size: 1.6rem;
  font-weight: 500;
  line-height: 1;
}

.zoom-controls .map-control-button {
  border: 0;
  box-shadow: none;
}

.zoom-controls .map-control-button + .map-control-button {
  border-top: 1px solid rgba(15, 23, 42, 0.12);
}

.map-control-pill {
  min-width: 44px;
  height: 44px;
  padding: 0 18px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1;
}

.map-control-icon-button {
  width: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.map-control-icon {
  width: 18px;
  height: 18px;
  display: block;
}

.map-control-icon-whole-usa {
  width: 24px;
  height: 24px;
}

.map-control-icon-layers {
  width: 33px;
  height: 33px;
}

.map-control-circle {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  font-size: 1rem;
  line-height: 1;
}

.panel {
  position: relative;
  width: min(220px, calc(100vw - 24px));
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--control-radius);
  box-shadow: 0 8px 26px rgba(15, 23, 42, 0.14);
  padding: 12px;
  backdrop-filter: blur(3px);
  z-index: 2;
}

.legend-panel {
  width: fit-content;
  max-width: calc(100vw - 24px);
  min-width: 0;
  transform-origin: top left;
}

.loading-indicator {
  position: fixed;
  left: 50%;
  bottom: 16px;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: min(calc(100vw - 32px), 320px);
  padding: 8px 12px;
  border: 1px solid rgba(15, 23, 42, 0.08);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.8);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.1);
  backdrop-filter: blur(10px);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  pointer-events: none;
  transform: translate(-50%, 0);
  transition: opacity 180ms ease, transform 180ms ease, visibility 180ms ease;
}

.loading-indicator.is-hidden {
  opacity: 0;
  visibility: hidden;
  transform: translate(-50%, 8px);
}

.loading-indicator-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 999px;
  background: #111827;
  opacity: 0.72;
  animation: loading-pulse 1.2s ease-in-out infinite;
}

.loading-indicator-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@keyframes loading-pulse {
  0%,
  100% {
    transform: scale(0.82);
    opacity: 0.4;
  }

  50% {
    transform: scale(1);
    opacity: 0.95;
  }
}

.opacity-control {
  position: relative;
}

.opacity-panel {
  position: absolute;
  top: 0;
  right: calc(100% + 10px);
  width: 72px;
  padding: 12px 10px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: var(--panel-bg);
  border: 1px solid var(--control-border);
  border-radius: 22px;
  box-shadow: var(--control-shadow);
  backdrop-filter: blur(3px);
  transform-origin: top right;
  transition: opacity 180ms ease, transform 180ms ease, visibility 180ms ease;
}

.opacity-control.is-collapsed .opacity-panel {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateX(10px) scale(0.96);
}

.opacity-slider-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
}

.opacity-label,
.opacity-value {
  line-height: 1;
}

.opacity-slider {
  appearance: slider-vertical;
  -webkit-appearance: slider-vertical;
  width: 18px;
  height: 160px;
  margin: 0;
  writing-mode: vertical-lr;
  direction: rtl;
  accent-color: #111827;
  cursor: pointer;
}

.market-type-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 44px;
  padding: 0 16px;
  text-align: center;
}

.market-type-icon {
  width: 14px;
  height: 14px;
  display: block;
}

.market-type-current {
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1;
}

.dataset-panel-row {
  display: flex;
  gap: 6px;
  align-items: stretch;
}

.dataset-toggle {
  flex: 1;
  min-width: 0;
  padding: 0 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.dataset-current {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1.2;
  padding-bottom: 1px;
}

/* --- Dataset picker dropdown --- */

.dataset-picker-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  width: 100%;
  z-index: 5;
  max-height: min(70vh, 560px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--control-radius);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
  backdrop-filter: blur(3px);
  transition: opacity 160ms ease, transform 160ms ease, visibility 160ms ease;
}

.dataset-picker-dropdown.is-hidden {
  display: none;
  opacity: 0;
}

/* --- Upgrade popup overlay --- */

.upgrade-popup {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.25);
  backdrop-filter: blur(2px);
  cursor: pointer;
}

.upgrade-popup-content {
  background: #fff;
  border: 1px solid rgba(15, 23, 42, 0.12);
  border-radius: 10px;
  box-shadow: 0 8px 26px rgba(15, 23, 42, 0.16);
  padding: 20px 24px;
  text-align: center;
  max-width: 260px;
  cursor: default;
}

.upgrade-popup-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: #c0392b;
  margin-bottom: 6px;
}

.upgrade-popup-label {
  font-size: 0.75rem;
  color: var(--text);
  margin-bottom: 10px;
}

.upgrade-popup-levels {
  font-size: 0.65rem;
  color: var(--muted);
  margin-bottom: 14px;
}

.upgrade-popup-level {
  display: inline-block;
  margin-left: 4px;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  background: rgba(192, 57, 43, 0.08);
  color: #c0392b;
}

.upgrade-popup-close {
  padding: 5px 20px;
  border: 1px solid rgba(15, 23, 42, 0.15);
  border-radius: 6px;
  background: #f9fafb;
  color: var(--text);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.1s;
}

.upgrade-popup-close:hover {
  background: #f1f3f5;
}

.picker-card-stack {
  border: 1px solid rgba(15, 23, 42, 0.1);
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
}

.picker-card-stack .cat-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  border: none;
  border-top: 1px solid rgba(15, 23, 42, 0.08);
  background: #f3f4f6;
  color: var(--text);
  font: inherit;
  font-size: 0.7rem;
  font-weight: 700;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
}

.picker-card-stack > .cat-card:first-child {
  border-top: none;
}

.picker-card-stack .cat-card:hover {
  background: #e5e7eb;
}

.picker-card-stack .cat-card.is-open {
  background: #dde0e8;
}

.picker-card-stack .cat-card-left {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.picker-card-stack .cat-meta {
  font-size: 0.6rem;
  color: var(--muted);
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
}

.picker-card-stack .cat-chevron {
  color: var(--muted);
  font-size: 12px;
  transition: transform 0.2s;
}

.picker-card-stack .cat-card.is-open .cat-chevron {
  transform: rotate(180deg);
}

.picker-card-stack .ds-items {
  display: none;
}

.picker-card-stack .ds-items.is-visible {
  display: block;
}

.picker-card-stack .ds-card {
  display: grid;
  gap: 3px;
  width: 100%;
  padding: 7px 12px;
  border: none;
  border-top: 1px solid rgba(15, 23, 42, 0.06);
  background: #fff;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.1s;
}

.picker-card-stack .ds-card:hover {
  background: #f8f9fb;
}

.picker-card-stack .ds-card.is-selected {
  background: rgba(17, 24, 39, 0.06);
  box-shadow: inset 3px 0 0 #111827;
}

.picker-card-stack .ds-card-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.ds-fav-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--muted);
  opacity: 0.45;
  transition: opacity 0.15s, transform 0.15s;
  line-height: 1;
}

.ds-fav-btn:hover {
  opacity: 1;
  transform: scale(1.2);
}

.ds-fav-btn.is-fav {
  opacity: 1;
  color: #e74c3c;
}

.cat-fav-icon {
  display: inline-flex;
  color: #e74c3c;
  margin-right: 2px;
}

.picker-card-stack .ds-label {
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.3;
}

.picker-card-stack .ds-var-count {
  font-size: 0.6rem;
  color: var(--muted);
  white-space: nowrap;
}

.picker-card-stack .ds-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}

.picker-card-stack .pill {
  display: inline-flex;
  align-items: center;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.55rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}

.picker-card-stack .pill--cat {
  background: rgba(59, 91, 219, 0.08);
  color: #3b5bdb;
}

.picker-card-stack .pill--upgrade {
  background: rgba(192, 57, 43, 0.08);
  color: #c0392b;
}

.picker-card-stack .pill--access {
  background: rgba(192, 57, 43, 0.08);
  color: #c0392b;
}

.picker-card-stack .pill--mkt {
  background: rgba(230, 126, 34, 0.10);
  color: #d35400;
}

.picker-card-stack .ds-card.is-disabled {
  opacity: 0.45;
  cursor: default;
}

.picker-variation-section {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.picker-section-title {
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.picker-var-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.picker-var-chips .var-chip {
  padding: 5px 9px;
  border-radius: 6px;
  border: 1px solid rgba(15, 23, 42, 0.12);
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 0.72rem;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

.picker-var-chips .var-chip:hover {
  border-color: #111827;
  background: rgba(17, 24, 39, 0.04);
}

.picker-var-chips .var-chip.is-active {
  border-color: #111827;
  background: #111827;
  color: #f9fafb;
  font-weight: 700;
}

.picker-var-chips .var-chip.is-upgrade {
  color: #c0392b;
}

.picker-var-chips .var-chip.is-disabled {
  opacity: 0.35;
  pointer-events: none;
}

.var-chip .access-pill {
  display: inline-block;
  margin-left: 4px;
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 0.5rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  background: rgba(192, 57, 43, 0.10);
  color: #c0392b;
  vertical-align: middle;
  line-height: 1.3;
}

.picker-var-empty {
  font-size: 0.75rem;
  color: var(--muted);
  padding: 4px 0;
}

/* --- Dataset / variation popover trigger --- */

.ds-popover-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 15px;
  height: 15px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: #144e9a;
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  vertical-align: middle;
  transition: background 120ms ease;
}

.ds-popover-btn:hover,
.ds-popover-btn[aria-expanded="true"] {
  background: #0f3f7d;
}

.ds-popover-btn:focus-visible {
  outline: 2px solid #8cb4e8;
  outline-offset: 2px;
}

/* Wrapper that pairs a var-chip with its optional popover trigger */
.var-chip-wrap {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.var-picker-dropdown .var-chip-wrap {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 4px;
}

.var-picker-dropdown .var-chip-wrap .var-chip {
  flex: 1;
}

/* Floating popover panel (body-appended, fixed-positioned) */
.picker-popover-panel {
  position: fixed;
  z-index: 9999;
  padding: 12px 14px;
  border: 1px solid rgba(20, 78, 154, 0.14);
  border-radius: 10px;
  background: #fff;
  color: #243241;
  box-shadow: 0 10px 30px rgba(25, 41, 66, 0.18);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
}

.picker-popover-panel[hidden] {
  display: none;
}

.picker-popover-title {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.3;
  color: #16375d;
}

.picker-popover-body {
  margin: 0;
  font-size: 12px;
  line-height: 1.5;
  color: #405466;
  white-space: pre-line;
}

/* --- Compact variation picker (external) --- */

.var-picker {
  position: relative;
}

.var-picker-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 100%;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  box-shadow: var(--control-shadow);
  color: var(--text);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

.var-picker-toggle::after {
  content: "\25BE";
  font-size: 10px;
  color: var(--muted);
  transition: transform 0.2s;
}

.var-picker-toggle.is-open::after {
  transform: rotate(180deg);
}

.var-picker-toggle:hover {
  border-color: rgba(15, 23, 42, 0.25);
  background: rgba(255, 255, 255, 1);
}

.var-picker-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 100%;
  z-index: 6;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
  backdrop-filter: blur(3px);
}

.var-picker-dropdown.is-hidden {
  display: none;
}

.var-picker-dropdown .var-chip,
.filter-var-dropdown-portal .var-chip {
  display: block;
  width: 100%;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.75rem;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.1s;
}

.var-picker-dropdown .var-chip:hover,
.filter-var-dropdown-portal .var-chip:hover {
  background: rgba(15, 23, 42, 0.06);
}

.var-picker-dropdown .var-chip.is-active,
.filter-var-dropdown-portal .var-chip.is-active {
  background: #111827;
  color: #f9fafb;
  font-weight: 700;
}

.var-picker-dropdown .var-chip.is-upgrade,
.filter-var-dropdown-portal .var-chip.is-upgrade {
  color: #c0392b;
}

.var-picker-dropdown .var-chip.is-disabled {
  opacity: 0.35;
  pointer-events: none;
}

.market-type-options {
  transition: opacity 160ms ease, transform 160ms ease, visibility 160ms ease;
}

.market-type-options.is-hidden {
  display: none;
  opacity: 0;
}

.layer-switcher {
  display: grid;
  grid-template-columns: 1fr;
  justify-items: start;
  gap: 6px;
}

.layer-toggle {
  display: inline-flex;
  align-items: center;
  width: auto;
  min-width: 140px;
  justify-content: center;
  font-size: 0.86rem;
  font-weight: 600;
}

.layer-toggle.is-active {
  background: #111827;
  border-color: #111827;
  color: #f9fafb;
}

.market-panel.is-expanded .market-type-toggle {
  display: none;
}

.panel h1 {
  margin: 0 0 6px;
  font-size: 1rem;
}

.panel p {
  margin: 0;
  font-size: 0.9rem;
}

.legend {
  display: grid;
  gap: 4px;
}

.legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.legend-title {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.legend-toggle {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: auto;
  min-height: 50px;
  padding: 8px 16px 9px;
}

.legend-toggle-label {
  display: block;
  line-height: 1;
}

.legend-toggle-swatches {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  margin-top: 4px;
}

.legend-toggle-swatch {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  border: 1px solid transparent;
}

.legend-collapse {
  width: 30px;
  height: 30px;
  position: relative;
  flex: 0 0 auto;
  box-shadow: none;
}

.legend-collapse::before {
  content: "";
  position: absolute;
  top: 48%;
  left: 50%;
  width: 10px;
  height: 10px;
  border-top: 1.5px solid currentColor;
  border-left: 1.5px solid currentColor;
  transform: translate(-50%, -32%) rotate(45deg);
}

.legend-endpoint {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 800;
  line-height: 1.1;
  text-align: center;
}

#legend-top-label {
  margin-bottom: 12px;
}

#legend-bottom-label {
  margin-top: 12px;
}

.legend-endpoint.is-hidden {
  display: none;
}

.legend-row {
  display: grid;
  grid-template-columns: 14px 1fr;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  border: 1px solid rgba(0, 0, 0, 0.18);
}

.legend-swatch-missing {
  background: #d1d5db;
}

.legend-missing-row {
  display: grid;
  grid-template-columns: 14px 1fr;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  font-size: 0.82rem;
  font-weight: 700;
}

.hint {
  color: var(--muted);
}

/* --- Table card overlay --- */

.overlay-card {
  position: fixed;
  z-index: 3;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--control-radius);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
  backdrop-filter: blur(3px);
}

.table-card {
  top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 67px);
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  width: fit-content;
  min-width: 520px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
}

.table-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  flex: 0 0 auto;
}


.table-header-btn {
  height: 30px;
  min-width: auto;
  padding: 0 12px;
  font-size: 0.7rem;
}

.table-close-btn {
  width: 30px;
  height: 30px;
  font-size: 1rem;
}

.table-scroll-container {
  overflow: auto;
  flex: 1 1 auto;
  min-height: 0;
}

#data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

#data-table thead {
  position: sticky;
  top: 0;
  z-index: 1;
}

#data-table th {
  padding: 8px 12px;
  background: #f3f4f6;
  border-bottom: 2px solid rgba(15, 23, 42, 0.1);
  font-weight: 700;
  font-size: 0.7rem;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

#data-table th:hover {
  background: #e5e7eb;
}

#data-table th:first-child {
  position: sticky;
  left: 0;
  z-index: 2;
  background: #f3f4f6;
}

#data-table td {
  padding: 6px 12px;
  border-bottom: 1px solid rgba(15, 23, 42, 0.05);
  white-space: nowrap;
}

#data-table td:first-child {
  position: sticky;
  left: 0;
  background: var(--panel-bg);
  font-weight: 600;
  z-index: 1;
}

#data-table tbody tr:hover td {
  background: rgba(17, 24, 39, 0.03);
}

#data-table tbody tr:hover td:first-child {
  background: rgba(243, 244, 246, 0.95);
}

.th-sort-arrow {
  display: inline-block;
  margin-left: 4px;
  font-size: 0.6rem;
  opacity: 0.4;
}

.th-sort-arrow.is-active {
  opacity: 1;
}

.th-remove {
  display: inline-block;
  margin-left: 6px;
  font-size: 0.93rem;
  /* opacity: 0.7; */
  cursor: pointer;
  vertical-align: middle;
  line-height: 1;
  position: relative;
  top: -1px;
}

.th-remove:hover {
  opacity: 1;
  color: #dc2626;
}

.th-label {
  cursor: pointer;
}

.th-label:hover {
  text-decoration: underline;
}

.th-var-picker {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
}

.th-var-picker .var-picker-toggle {
  font-size: 0.68rem;
  padding: 1px 10px;
  height: auto;
}

.th-var-picker .var-picker-dropdown,
.filter-var-picker .var-picker-dropdown {
  left: 0;
  right: auto;
  min-width: 120px;
}

.filter-var-picker {
  margin-bottom: 2px;
}

.filter-var-dropdown-portal {
  position: fixed;
  z-index: 100;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
  min-width: 120px;
}

.filter-var-dropdown-portal.is-hidden {
  display: none;
}


.table-header-palette {
  flex: 1;
  min-width: 0;
}

.table-palette-bar {
  display: flex;
  width: 100%;
  max-width: 480px;
  height: 30px;
  border-radius: 4px;
  overflow: hidden;
}

.table-palette-seg {
  flex: 1;
  display: flex;
  align-items: center;
}

.table-palette-seg:first-child {
  padding-left: 8px;
}

.table-palette-seg:last-child {
  justify-content: flex-end;
  padding-right: 8px;
}

.table-palette-label {
  font-weight: 700;
  font-size: 0.72rem;
  color: #0f172a;
}

.table-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 14px;
  border-top: 1px solid rgba(15, 23, 42, 0.08);
  flex: 0 0 auto;
  font-size: 0.72rem;
  color: var(--muted);
}

.table-pagination button {
  appearance: none;
  border: 1px solid var(--control-border);
  background: var(--control-bg);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 10px;
  font: inherit;
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
}

.table-pagination button:disabled {
  opacity: 0.35;
  cursor: default;
}

/* --- Filter card overlay --- */

.filter-card {
  top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 10px);
  right: 10px;
  /* bottom: 10px; */
  left: auto;
  transform: none;
  width: 330px;
  max-height: calc(100vh - var(--ha-nav-height, 0px) - var(--nli-bar-height, 0px) - 20px);
  overflow-x: hidden;
  overflow-y: hidden;
  display: flex;
  flex-direction: column;
  z-index: 4;
}

@media (max-width: 720px) {
  .filter-card {
    top: 50%;
    right: auto;
    bottom: auto;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(92vw, 400px);
    max-height: 80vh;
  }
}

.filter-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  flex: 0 0 auto;
}

.filter-card-title {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  flex: 1;
}

.filter-enable-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.filter-header-btn {
  height: 30px;
  min-width: auto;
  padding: 0 12px;
  font-size: 0.7rem;
}

.filter-close-btn {
  width: 30px;
  height: 30px;
  font-size: 1rem;
}

.filter-rows {
  overflow-x: hidden;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.filter-add-row-btn {
  margin: 8px 14px 12px;
  height: 34px;
  font-size: 0.72rem;
}

/* Single filter row */
.filter-row {
  display: grid;
  gap: 6px;
}

.filter-row-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.filter-row-label {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text);
}

.filter-row-remove {
  appearance: none;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 0 4px;
  border-radius: 4px;
  line-height: 1;
}

.filter-row-remove:hover {
  color: #c0392b;
  background: rgba(192, 57, 43, 0.06);
}

.filter-row-slider {
  padding: 8px 6px 4px;
}

.filter-row-inputs {
  display: flex;
  align-items: center;
  gap: 6px;
}

.filter-row-input {
  flex: 1;
  appearance: none;
  border: 1px solid var(--control-border);
  background: var(--control-bg);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 8px;
  font: inherit;
  font-size: 0.7rem;
  text-align: center;
}

.filter-row-inputs .filter-sep {
  font-size: 0.7rem;
  color: var(--muted);
}

/* noUiSlider overrides */
.filter-row-slider .noUi-target {
  height: 6px;
  border: none;
  background: rgba(15, 23, 42, 0.08);
  border-radius: 3px;
  box-shadow: none;
}

.filter-row-slider .noUi-connect {
  background: #111827;
  border-radius: 3px;
}

.filter-row-slider .noUi-handle {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #111827;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  top: -6px;
  right: -8px;
  cursor: grab;
}

.filter-row-slider .noUi-handle::before,
.filter-row-slider .noUi-handle::after {
  display: none;
}

.filter-row-slider .noUi-tooltip {
  font-size: 0.6rem;
  font-weight: 700;
  padding: 2px 6px;
  border: none;
  background: #111827;
  color: #fff;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}

.filter-row-slider .noUi-handle.noUi-active .noUi-tooltip {
  opacity: 1;
}

/* Toggle button active state */
.map-control-pill.is-active {
  background: #111827;
  border-color: #111827;
  color: #f9fafb;
}

@media (max-width: 720px) {
  .map-controls-right {
    top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 60px);
    right: 8px;
    gap: 10px;
  }

  .map-controls-left {
    top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 60px);
    left: 8px;
    gap: 10px;
  }

  .dataset-panel {
    top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 8px);
    width: calc(100vw - 16px);
  }

  .panel {
    width: min(132px, calc(100vw - 16px));
    padding: 8px 10px;
  }

  .legend-panel {
    width: fit-content;
    max-width: calc(100vw - 16px);
    transition: opacity 180ms ease, transform 180ms ease, visibility 180ms ease, max-height 180ms ease,
      margin 180ms ease, padding 180ms ease, border-width 180ms ease;
    max-height: 420px;
    overflow: hidden;
  }

  .legend-panel.is-collapsed {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(-8px) scale(0.96);
    max-height: 0;
    margin-top: -10px;
    padding-top: 0;
    padding-bottom: 0;
    border-top-width: 0;
    border-bottom-width: 0;
  }

  .legend-toggle {
    display: inline-flex;
  }

  .market-type-toggle {
    padding: 0 14px;
  }

  .dataset-toggle {
    padding: 0 16px;
  }

  .dataset-current {
    font-size: 0.84rem;
  }

  .dataset-picker-dropdown {
    max-height: min(60vh, 440px);
    padding: 8px;
  }

  .opacity-panel {
    width: 68px;
    padding: 10px 8px 8px;
  }

  .opacity-slider {
    height: 132px;
  }

  .loading-indicator {
    bottom: 12px;
    max-width: calc(100vw - 24px);
    padding: 7px 11px;
    font-size: 0.7rem;
  }

  .table-card {
    min-width: 0;
    width: calc(100vw - 16px);
    top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 52px);
    bottom: 8px;
  }

  #data-table th:first-child,
  #data-table td:first-child {
    max-width: calc(100vw - 160px);
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .report-card {
    width: calc(100vw - 16px);
    height: 70vh;
    max-height: 80vh;
  }
}

/* --- Report card overlay --- */

.report-card {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 520px;
  max-width: 90vw;
  height: 360px;
  max-height: 80vh;
  z-index: 5;
  display: flex;
  flex-direction: column;
}

.report-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  flex: 0 0 auto;
}

.report-card-title {
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.report-market-id {
  font-size: 0.65rem;
  font-weight: 400;
  color: var(--muted);
  margin-left: 6px;
}

/* Clickable market name in table */
.market-name-link {
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  border-bottom: 1px dashed rgba(15, 23, 42, 0.3);
}

.market-name-link:hover {
  color: #2563eb;
  border-bottom-color: #2563eb;
}

.market-name-cell {
  display: flex;
  align-items: center;
  gap: 6px;
}

.market-name-cell .market-name-link {
  flex: 1 1 auto;
  min-width: 0;
}

.market-report-icon-link,
.market-chart-icon-link,
.market-zillow-icon-link {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  transition: transform 0.15s;
  text-decoration: none;
}

.market-report-icon-link {
  margin-left: auto;
}

.market-report-icon-link:hover,
.market-chart-icon-link:hover,
.market-zillow-icon-link:hover {
  transform: scale(1.25);
}

.market-report-icon-link img,
.market-chart-icon-link img,
.market-zillow-icon-link img {
  display: block;
}

.report-card-footer {
  padding: 6px 14px 8px;
  border-top: 1px solid rgba(15, 23, 42, 0.08);
  flex: 0 0 auto;
  display: flex;
  justify-content: flex-end;
}

.report-open-link {
  font-size: 0.78rem;
  color: #2563eb;
  text-decoration: none;
}

.report-open-link:hover {
  text-decoration: underline;
}

/* --- Mini chart --- */

.report-chart-container {
  padding: 4px 8px 8px;
  flex: 1 1 auto;
  min-height: 0;
}

.minichart-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* --- Magic Buttons panel --- */

.map-control-icon-magic {
  width: 22px;
  height: 22px;
}

.magic-card {
  top: calc(var(--ha-nav-height, 0px) + var(--nli-bar-height, 0px) + 10px);
  right: 10px;
  left: auto;
  transform: none;
  width: 290px;
  z-index: 4;
  display: flex;
  flex-direction: column;
}

@media (max-width: 720px) {
  .magic-card {
    top: 50%;
    right: auto;
    bottom: auto;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(92vw, 340px);
  }
}

.magic-card-header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 14px 8px;
  position: relative;
  flex: 0 0 auto;
}

.magic-card-title {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-align: center;
  flex: 1;
}

.magic-mode-toggle {
  display: flex;
  border-top: 1px solid rgba(15, 23, 42, 0.08);
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  flex: 0 0 auto;
}

.magic-mode-btn {
  flex: 1;
  background: transparent;
  border: none;
  padding: 10px 12px;
  cursor: pointer;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: background 0.15s, color 0.15s;
  color: var(--muted);
}

.magic-mode-btn + .magic-mode-btn {
  border-left: 1px solid rgba(15, 23, 42, 0.08);
}

.magic-mode-btn.is-active {
  background: rgba(37, 99, 235, 0.07);
  color: #1d4ed8;
}

.magic-mode-label {
  font-size: 0.8rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 5px;
}

.magic-mode-sub {
  font-size: 0.64rem;
  opacity: 0.75;
  line-height: 1.3;
}

.magic-pro-tag {
  display: inline-block;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: #d97706;
  color: #fff;
  padding: 1px 5px;
  border-radius: 4px;
  vertical-align: middle;
}

.magic-close-btn {
  width: 30px;
  height: 30px;
  font-size: 1rem;
}

.magic-buttons-list {
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.magic-strategy-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  transition: filter 0.15s, transform 0.1s;
}

.magic-strategy-btn:hover {
  filter: brightness(1.08);
  transform: translateY(-1px);
}

.magic-strategy-btn:active {
  transform: translateY(0);
  filter: brightness(0.96);
}

.magic-strategy-btn.is-level-locked {
  opacity: 0.45;
  filter: grayscale(30%);
}

.magic-btn-pro-badge {
  position: absolute;
  top: 7px;
  right: 8px;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  display: none;
}

.magic-card.is-pro-mode .magic-btn-pro-badge {
  display: block;
}

.magic-btn-label {
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1.2;
}

.magic-btn-details {
  font-size: 0.68rem;
  opacity: 0.8;
  line-height: 1.3;
}

.magic-btn-style-01 { background: linear-gradient(160deg, #b03a2a 0%, #6b2018 100%); color: #fff; }
.magic-btn-style-02 { background: linear-gradient(160deg, #3d7838 0%, #1f4822 100%); color: #fff; }
.magic-btn-style-03 { background: linear-gradient(160deg, #c87010 0%, #7a3e0a 100%); color: #fff; }
.magic-btn-style-04 { background: linear-gradient(160deg, #1c7ea8 0%, #1a4878 100%); color: #fff; }

.minichart-loading,
.minichart-error {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 120px;
  color: var(--muted);
  font-size: 0.82rem;
}

.minichart-error {
  color: #dc2626;
}
