:root {
  --modal-controls-clearance: 30px;
  --wrap-pad: clamp(8px, 1.4vw, 12px);
  --dashboard-accent: var(--accent, #ff9f1c);
  --panel-min-height: 320px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100vh;
  background: #000;
  color: var(--fg, #f1f5f7);
}

.wrap {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: var(--wrap-pad);
  padding-bottom: var(--wrap-pad);
  display: grid;
  gap: 10px;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  align-content: start;
  grid-template-rows: var(--wrap-grid-template-rows, none);
}

@media (max-width: 1100px) {
  .wrap {
    height: auto;
    min-height: 100vh;
  }
}

/* Minimum height for panels to prevent them from becoming too small */
.panel {
  min-height: var(--panel-min-height);
}

/* Keep KPI/topbar in natural content height (no forced extra height). */
.topbar {
  align-self: start;
  position: relative;
}

body.embedded-in-modal .wrap,
html.embedded-in-modal body .wrap {
  padding-top: calc(var(--wrap-pad) + var(--modal-controls-clearance));
}

@media (max-width: 860px) {
  .wrap {
    padding-bottom: calc(var(--wrap-pad) + env(safe-area-inset-bottom, 0px) + 56px);
  }
}

.title,
h1 {
  margin: 0;
  font-size: clamp(1.05rem, 2vw, 1.5rem);
  font-weight: 700;
  letter-spacing: 0.015em;
  text-wrap: balance;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  position: relative;
}

.info-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.info-btn {
  width: 18px;
  height: 18px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.32);
  background: transparent;
  color: #d6e1e6;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.66rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.info-btn:hover,
.info-btn:focus-visible {
  color: var(--dashboard-accent);
  border-color: var(--dashboard-accent);
  outline: none;
}

.info-popover {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: auto;
  z-index: 50;
  width: min(420px, calc(100vw - 24px));
  max-width: min(420px, calc(100vw - 24px));
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  background: #000;
  color: #d9e5ea;
  padding: 10px 12px;
  font-size: 0.82rem;
  line-height: 1.45;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.35);
  font-family: "IBM Plex Mono", monospace;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(-2px);
  transition: opacity 120ms ease, transform 120ms ease, visibility 0ms linear 120ms;
}

.info-wrap:hover .info-popover,
.info-wrap:focus-within .info-popover {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  transition: opacity 120ms ease, transform 120ms ease;
}

.chips {
  display: flex;
  gap: 8px 10px;
  flex-wrap: wrap;
  align-items: center;
}

.controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.topbar.ui-locked .toggle,
.topbar.ui-locked label.chip,
.topbar.ui-locked .chip-menu-wrap,
.topbar.ui-locked .info-wrap,
.topbar.ui-locked .swap-panels-control {
  opacity: 0.55;
}

.topbar.ui-locked button,
.topbar.ui-locked select,
.topbar.ui-locked input,
.topbar.ui-locked textarea,
.topbar.ui-locked label {
  cursor: not-allowed;
}

/* Shared row-wrapping and overflow guards for dashboard topbars. */
.topbar > * {
  min-width: 0;
  max-width: 100%;
}

.source-row {
  margin: 0;
  color: var(--muted);
  font-size: clamp(0.74rem, 0.70rem + 0.22vw, 0.82rem);
  font-family: "IBM Plex Mono", monospace;
  line-height: 1.4;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0.35ch;
}

.source-row a {
  color: #d6e1e6;
  font-size: inherit;
  line-height: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  display: inline-block;
  max-width: min(100%, 78vw);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
}

.source-row a:hover,
.source-row a:focus-visible {
  color: var(--dashboard-accent);
  outline: none;
}

.chips-inline {
  display: flex;
  gap: 8px 10px;
  flex-wrap: wrap;
  align-items: center;
  min-width: 0;
  max-width: 100%;
}

#kpiRow,
#statusChips,
.controls {
  max-width: 100%;
  min-width: 0;
}

#kpiRow,
#statusChips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: flex-start;
  width: 100%;
}

#statusChips {
  flex: 1 1 100%;
  gap: 8px 10px;
}

#statusChips .chip,
#statusChips .chip-menu-wrap,
.controls .chip,
.controls .toggle,
.controls .swap-panels-control,
.controls > * {
  flex: 0 0 auto;
  max-width: 100%;
  min-width: 0;
}

#statusChips .chip {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.controls .chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  white-space: nowrap;
  overflow: hidden;
}

.controls .chip > span,
.controls .chip > select,
.controls .chip > input {
  max-width: 100%;
  min-width: 0;
}

@media (max-width: 1100px) {
  html,
  body {
    max-width: 100%;
    overflow-x: hidden;
  }

  .wrap,
  .topbar,
  .grid,
  .panel,
  .chips,
  .chips-inline,
  .controls,
  #statusChips {
    max-width: 100%;
    min-width: 0;
  }
}

@media (max-width: 860px) {
  .swap-panels-control .label {
    max-width: 52vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #d8e4ea;
  font-size: 0.84rem;
  user-select: none;
  cursor: pointer;
}

.toggle input {
  width: 15px;
  height: 15px;
  accent-color: var(--dashboard-accent);
  cursor: pointer;
  margin: 0;
}

.chip {
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  background: transparent;
  color: #d6e1e6;
  padding: 5px 9px;
  font-size: 0.8rem;
  font-family: "IBM Plex Mono", monospace;
  white-space: nowrap;
}

.swap-panels-control {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  background: transparent;
  color: #d8e4ea;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  font: inherit;
  font-size: 0.84rem;
  line-height: 1;
}

.swap-panels-control:hover,
.swap-panels-control:focus-visible {
  color: var(--dashboard-accent);
  outline: none;
}

.swap-panels-control:disabled {
  color: rgba(216, 228, 234, 0.5);
  cursor: not-allowed;
}

.swap-panels-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 6px;
  background: transparent;
  color: #d8e4ea;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}

.swap-panels-control:hover .swap-panels-btn,
.swap-panels-control:focus-visible .swap-panels-btn {
  border-color: var(--dashboard-accent);
}

.swap-panels-control:disabled .swap-panels-btn {
  border-color: rgba(255, 255, 255, 0.14);
}

.swap-panels-btn svg {
  width: 12px;
  height: 12px;
  display: block;
  stroke: currentColor;
  stroke-width: 1.5;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.swap-panels-control .label {
  white-space: nowrap;
}

.panel-resize-handle {
  position: absolute;
  right: 8px;
  bottom: 8px;
  width: 20px;
  height: 20px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.72);
  cursor: nwse-resize;
  z-index: 4;
  touch-action: none;
  padding: 0;
}

.panel-resize-handle::before {
  content: "";
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 11px;
  height: 11px;
  background: linear-gradient(135deg, transparent 0 34%, rgba(214, 225, 230, 0.75) 34% 43%, transparent 43% 57%, rgba(214, 225, 230, 0.75) 57% 66%, transparent 66% 100%);
  pointer-events: none;
}

.panel-resize-handle:hover,
.panel-resize-handle:focus-visible {
  border-color: var(--dashboard-accent);
  outline: none;
}

.panel-resize-handle:disabled {
  border-color: rgba(255, 255, 255, 0.14);
  cursor: not-allowed;
}

.panel-resize-handle:disabled::before {
  opacity: 0.5;
}

:root[data-theme="light"] select:hover,
:root[data-theme="light"] select:focus,
:root[data-theme="light"] select:focus-visible,
:root[data-theme="light"] .script-trigger:hover,
:root[data-theme="light"] .script-trigger:focus,
:root[data-theme="light"] .script-trigger:focus-visible,
:root[data-theme="light"] .chip-menu-wrap.single-select:hover .chip-kpi-display,
:root[data-theme="light"] .chip-menu-wrap.single-select:focus-within .chip-kpi-display {
  background: var(--surface, #fff) !important;
  background-color: var(--surface, #fff) !important;
}

.dashboard-inline-tooltip {
  position: fixed;
  z-index: 70;
  width: fit-content;
  max-width: min(420px, calc(100vw - 24px));
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 10px;
  background: #ffffff;
  color: #1c1b19;
  padding: 10px 12px;
  font-size: 0.82rem;
  line-height: 1.45;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
  font-family: "IBM Plex Mono", monospace;
  white-space: pre-line;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(-2px);
  transition: opacity 120ms ease, transform 120ms ease, visibility 0ms linear 120ms;
}

:root[data-theme="dark"] .dashboard-inline-tooltip {
  border-color: rgba(255, 255, 255, 0.18);
  background: #000;
  color: #d9e5ea;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.35);
}

.dashboard-inline-tooltip.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  transition: opacity 120ms ease, transform 120ms ease;
}

/* Consistent keyboard focus highlight across dashboard controls. */
.wrap button:focus-visible,
.wrap a:focus-visible,
.wrap select:focus-visible,
.wrap input[type="checkbox"]:focus-visible,
.wrap input[type="radio"]:focus-visible,
.wrap .chip-menu-wrap.single-select:focus-within,
.wrap .chip:focus-within,
.wrap .toggle:focus-within {
  outline: 2px solid #ff9800 !important;
  outline-offset: 2px;
  box-shadow: 0 0 0 2px rgba(255, 152, 0, 0.25);
}

.panel-loader {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  background: rgba(0, 0, 0, 0.72);
  z-index: 2;
  transition: opacity 180ms ease, visibility 0ms linear 180ms;
  opacity: 1;
  visibility: visible;
  pointer-events: none;
}

.panel-loader.hidden {
  opacity: 0;
  visibility: hidden;
}

.panel-loader .circle {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 3px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--dashboard-accent);
  animation: dash-spin 850ms linear infinite;
}

.panel-loader .label {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.78rem;
  color: #d6e1e6;
  letter-spacing: 0.01em;
}

/* Minimum height for panels to prevent them from becoming too small */
.panel {
  min-height: var(--panel-min-height);
}

@keyframes dash-spin {
  to {
    transform: rotate(360deg);
  }
}

/* ===========================
   LIGHT MODE — shared dashboard component overrides
   Activated when html[data-theme="light"] is set by theme JS.
   Each dashboard's local :root[data-theme="light"] block supplies
   --bg, --panel, --panel-border, --fg, --muted, --fg-dim, --link.
   =========================== */
:root[data-theme="light"] body {
  background: var(--bg, #f7f4ee);
  color: var(--fg, #1c1b19);
}

:root[data-theme="light"] .topbar {
  background: var(--panel, #ffffff);
  border-color: var(--panel-border, rgba(0, 0, 0, 0.11));
}

:root[data-theme="light"] .chip {
  border-color: var(--panel-border, rgba(0, 0, 0, 0.11));
  color: var(--fg-dim, #514d47);
  background: transparent;
}

:root[data-theme="light"] .toggle {
  color: var(--fg, #1c1b19);
}

:root[data-theme="light"] .controls {
  border-top-color: rgba(0, 0, 0, 0.08);
}

:root[data-theme="light"] .swap-panels-control {
  color: var(--fg, #1c1b19);
}

:root[data-theme="light"] .swap-panels-btn {
  border-color: rgba(0, 0, 0, 0.18);
  color: var(--fg, #1c1b19);
}

:root[data-theme="light"] .panel-resize-handle {
  border-color: rgba(0, 0, 0, 0.18);
  background: rgba(255, 255, 255, 0.88);
}

:root[data-theme="light"] .panel-resize-handle::before {
  background: linear-gradient(
    135deg,
    transparent 0 34%,
    rgba(60, 55, 50, 0.6) 34% 43%,
    transparent 43% 57%,
    rgba(60, 55, 50, 0.6) 57% 66%,
    transparent 66% 100%
  );
}

:root[data-theme="light"] .panel-loader {
  background: rgba(248, 245, 240, 0.9);
}

:root[data-theme="light"] .panel-loader .label {
  color: var(--muted, #6f685f);
}

:root[data-theme="light"] .panel-loader .circle {
  border-color: rgba(0, 0, 0, 0.1);
  border-top-color: var(--dashboard-accent, #ff9f1c);
}

:root[data-theme="light"] .info-btn {
  border-color: rgba(0, 0, 0, 0.26);
  color: var(--muted, #6f685f);
}

:root[data-theme="light"] .info-popover {
  background: var(--panel, #ffffff);
  border-color: rgba(0, 0, 0, 0.12);
  color: var(--fg, #1c1b19);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
}

:root[data-theme="light"] .source-row {
  color: var(--muted, #6f685f);
}

:root[data-theme="light"] .source-row a {
  color: var(--link, #1f4f78);
}
