/* Interactive Visual Model Map & Causal Explorer (Light & Dark Semantic Theme) */

.model-relationships {
  container-type: inline-size;
  margin: 1rem 0;
  padding: 1rem;
  border: 1px solid var(--line, #cbd5e1);
  border-radius: .75rem;
  background: var(--surface-card, #fff);
  color: var(--ink, #172033);
}

.model-relationships.is-fullscreen {
  position: fixed;
  inset: 14px;
  z-index: 1200;
  margin: 0;
  overflow: auto;
  box-shadow: 0 24px 64px rgba(15, 23, 42, .45);
}

.model-relationships-heading,
.model-relationships-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: .65rem .85rem;
}

.model-relationships-heading {
  justify-content: space-between;
  align-items: center;
  margin-bottom: .5rem;
}
.model-relationships-heading h3 { margin: 0; }

/* Top Mode & Tools Toolbar */
.mm-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: .6rem;
  padding: .55rem .7rem;
  margin-bottom: .75rem;
  border: 1px solid var(--line, #dfe5ef);
  border-radius: .6rem;
  background: var(--surface-subtle, #f7f9fc);
}

.mm-mode-tabs {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  border-radius: 8px;
  border: 1px solid var(--line, #dfe5ef);
  background: var(--surface-card, #fff);
}

.mm-mode-tab {
  padding: 5px 11px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted, #64748b);
  font-size: .78rem;
  font-weight: 700;
  cursor: pointer;
}

.mm-mode-tab.is-active {
  background: var(--brand, #2563eb);
  color: #fff;
}

.mm-tool-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .4rem;
}

.mm-search-input {
  min-width: 165px;
  padding: 5px 9px;
  border: 1px solid var(--line, #cbd5e1);
  border-radius: 6px;
  background: var(--surface-card, #fff);
  color: var(--ink, #172033);
  font-size: .8rem;
}

.mm-compact-select {
  padding: 4px 8px;
  border: 1px solid var(--line, #cbd5e1);
  border-radius: 6px;
  background: var(--surface-card, #fff);
  color: var(--ink, #172033);
  font-size: .78rem;
}

/* Controls & Timeline Scrubber */
.model-relationships-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: .4rem;
  min-width: 180px;
  max-width: 100%;
}

.model-relationships-controls label {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: .2rem;
  min-width: 0;
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted, #64748b);
}

.model-relationships-controls select {
  box-sizing: border-box;
  min-width: 0;
  max-width: 100%;
  width: 100%;
}

.mm-timeline-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .5rem;
  margin-top: .7rem;
  padding: .5rem .75rem;
  border: 1px solid var(--line, #dfe5ef);
  border-radius: .55rem;
  background: var(--surface-subtle, #f7f9fc);
}

.mm-timeline-slider {
  flex: 1 1 200px;
  min-width: 140px;
  accent-color: var(--brand, #2563eb);
  cursor: pointer;
}

.mm-timeline-bound {
  font-size: .74rem;
  font-weight: 700;
  color: var(--muted, #64748b);
}

.mm-timeline-badge {
  padding: 3px 9px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .04em;
  background: var(--accent-soft, #eef1ff);
  color: var(--brand, #2563eb);
}

.mm-timeline-badge.is-forecast {
  background: rgba(13, 148, 136, .16);
  color: #0f766e;
}

/* Main Split Workspace: Visual Map + Docked Inspector */
.mm-workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 336px;
  gap: 1rem;
  margin-top: .75rem;
  align-items: start;
}

.mm-workspace.is-trace {
  grid-template-columns: 1fr;
}

@container (max-width: 960px) {
  .mm-workspace { grid-template-columns: 1fr; }
}

.mm-stage { min-width: 0; }

/* Interactive Layered Canvas */
.mm-canvas-shell {
  border: 1px solid var(--line, #cbd5e1);
  border-radius: .65rem;
  overflow: hidden;
  background: var(--surface-subtle, #f8fafc);
}

.mm-canvas-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: .45rem .65rem;
  border-bottom: 1px solid var(--line, #dfe5ef);
  background: var(--surface-card, #fff);
}

.mm-canvas-controls {
  display: flex;
  align-items: center;
  gap: .35rem;
}

.mm-icon-btn {
  min-width: 28px;
  padding: 3px 7px !important;
}

.mm-zoom-readout {
  min-width: 42px;
  text-align: center;
  font-size: .75rem;
  font-weight: 700;
  color: var(--muted, #64748b);
}

.mm-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .65rem;
  font-size: .72rem;
  color: var(--muted, #64748b);
}

.mm-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.mm-swatch {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 3px;
  border: 1.5px solid var(--line-strong, #94a3b8);
}

.mm-swatch.is-exo { border-radius: 999px; background: rgba(13, 148, 136, .18); border-color: #0d9488; }
.mm-swatch.is-eq { background: rgba(37, 99, 235, .14); border-color: var(--brand, #2563eb); }
.mm-swatch.is-id { border-style: dashed; background: transparent; border-color: #64748b; }
.mm-swatch.is-lag { height: 0; border-width: 1.5px 0 0; border-style: dashed; border-color: #d97706; }
.mm-swatch.is-heat { background: linear-gradient(90deg, #dc2626, #16a34a); border-color: transparent; }

.mm-canvas-viewport {
  position: relative;
  width: 100%;
  height: 540px;
  overflow: hidden;
  cursor: grab;
  user-select: none;
  background-image: radial-gradient(var(--line, #cbd5e1) 1px, transparent 1px);
  background-size: 20px 20px;
}

.model-relationships.is-fullscreen .mm-canvas-viewport {
  height: calc(100vh - 280px);
  min-height: 460px;
}

.mm-canvas-viewport.is-dragging { cursor: grabbing; }

.mm-canvas-world {
  position: relative;
  transform-origin: 0 0;
}

.mm-layer-header {
  position: absolute;
  text-align: center;
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--muted, #64748b);
  pointer-events: none;
}

.mm-sim-group {
  position: absolute;
  border: 1.8px dashed var(--brand, #2563eb);
  border-radius: 14px;
  background: rgba(37, 99, 235, .05);
  pointer-events: none;
}

.mm-sim-group-label {
  position: absolute;
  top: 6px;
  left: 12px;
  font-size: .7rem;
  font-weight: 800;
  letter-spacing: .03em;
  color: var(--brand, #2563eb);
}

/* Edges SVG */
.mm-edges-svg {
  position: absolute;
  inset: 0;
  overflow: visible;
  pointer-events: none;
}

.mm-edge {
  fill: none;
  stroke: var(--muted, #64748b);
  stroke-width: 1.4;
  opacity: .68;
  transition: opacity .15s ease, stroke-width .15s ease;
}

.mm-edge.is-simultaneous { stroke: var(--brand, #2563eb); stroke-width: 1.8; opacity: .82; }
.mm-edge.is-lag, .mm-edge.is-self-lag { stroke: #d97706; stroke-dasharray: 5 3; }
.mm-edge.is-cone-up { stroke: #2563eb; stroke-width: 2.3; opacity: 1; }
.mm-edge.is-cone-down { stroke: #0d9488; stroke-width: 2.3; opacity: 1; }
.mm-edge.is-dimmed { opacity: .12; }

.mm-arrow-default { fill: var(--muted, #64748b); }
.mm-arrow-up { fill: #2563eb; }
.mm-arrow-down { fill: #0d9488; }
.mm-arrow-lag { fill: #d97706; }

.mm-edge-lag-label {
  fill: #b45309;
  font-size: 9.5px;
  font-weight: 800;
}

/* Interactive Node Cards */
.mm-node {
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 8px 11px;
  box-sizing: border-box;
  border: 1.5px solid var(--line-strong, #cbd5e1);
  border-radius: 10px;
  background: var(--surface-card, #fff);
  color: var(--ink, #172033);
  text-align: left;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(15, 23, 42, .06);
  transition: opacity .15s ease, border-color .15s ease, box-shadow .15s ease;
}

.mm-node:hover {
  border-color: var(--brand, #2563eb);
  box-shadow: 0 4px 12px rgba(37, 99, 235, .16);
}

.mm-node.is-exogenous {
  border-radius: 24px;
  border-color: rgba(13, 148, 136, .55);
  background: #f0fdfa;
}

.mm-node.is-identity {
  border-style: dashed;
}

.mm-node.is-endogenous:not(.is-identity) {
  border-left: 4px solid var(--brand, #2563eb);
}

.mm-node.is-cone-up {
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, .18);
}

.mm-node.is-cone-down {
  border-color: #0d9488;
  box-shadow: 0 0 0 2px rgba(13, 148, 136, .18);
}

.mm-node.is-selected {
  border-color: var(--brand, #2563eb);
  border-width: 2.2px;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .28);
  z-index: 3;
}

.mm-node.is-dimmed {
  opacity: .22;
}

.mm-node.is-heat-up {
  background: rgba(22, 163, 74, var(--heat-alpha, .18));
  border-color: #16a34a;
}

.mm-node.is-heat-down {
  background: rgba(220, 38, 38, var(--heat-alpha, .18));
  border-color: #dc2626;
}

.mm-node-top,
.mm-node-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  min-width: 0;
}

.mm-node-name {
  font-size: .76rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mm-node-tag {
  flex: none;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: .62rem;
  font-weight: 800;
  letter-spacing: .04em;
  background: var(--surface-subtle, #eef2f7);
  color: var(--muted, #64748b);
}

.mm-node-val {
  font-size: .78rem;
  font-weight: 700;
  color: var(--ink, #172033);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mm-node-spark {
  width: 72px;
  height: 22px;
  flex: none;
}

.mm-node-spark polyline {
  stroke: var(--brand, #2563eb);
  stroke-width: 1.6;
}

.mm-node-spark circle {
  fill: var(--brand, #2563eb);
}

.mm-node-heat-pill,
.mm-scenario-pill {
  padding: 2px 6px;
  border-radius: 999px;
  font-size: .68rem;
  font-weight: 800;
  white-space: nowrap;
}

.mm-node-heat-pill.is-up, .mm-scenario-pill.is-up { background: rgba(22, 163, 74, .18); color: #15803d; }
.mm-node-heat-pill.is-down, .mm-scenario-pill.is-down { background: rgba(220, 38, 38, .18); color: #b91c1c; }
.mm-node-heat-pill.is-flat, .mm-scenario-pill.is-flat { background: var(--surface-subtle, #eef2f7); color: var(--muted, #64748b); }

/* Docked Node Inspector */
.mm-inspector {
  display: flex;
  flex-direction: column;
  gap: .7rem;
  padding: .85rem;
  border: 1px solid var(--line, #cbd5e1);
  border-radius: .65rem;
  background: var(--surface-subtle, #f8fafc);
}

.mm-inspector-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .4rem;
}

.mm-inspector-head h4 { margin: 0; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted, #64748b); }

.mm-role-badge {
  padding: 2px 7px;
  border-radius: 999px;
  font-size: .68rem;
  font-weight: 700;
  background: var(--accent-soft, #eef1ff);
  color: var(--brand, #2563eb);
}

.mm-role-badge.is-exogenous { background: rgba(13, 148, 136, .16); color: #0f766e; }
.mm-role-badge.is-sim { background: rgba(217, 119, 6, .16); color: #b45309; }

.mm-inspector-selected {
  min-height: auto !important;
  padding: .6rem .75rem !important;
  font-size: .92rem;
}

.mm-inspector-chart-box {
  padding: .55rem .65rem;
  border: 1px solid var(--line, #dfe5ef);
  border-radius: 8px;
  background: var(--surface-card, #fff);
}

.mm-inspector-chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: .72rem;
  font-weight: 700;
  color: var(--muted, #64748b);
  margin-bottom: 4px;
}

.mm-chart-legend-note { font-weight: 500; font-size: .68rem; }
.mm-inspector-chart { display: block; width: 100%; height: auto; }
.mm-chart-grid { stroke: var(--line, #dfe5ef); stroke-dasharray: 2 3; }
.mm-chart-axis { fill: var(--muted, #64748b); font-size: 9.5px; }
.mm-chart-forecast-band { fill: rgba(13, 148, 136, .08); }
.mm-chart-baseline { stroke: var(--muted, #94a3b8); stroke-width: 1.5; stroke-dasharray: 4 3; }
.mm-chart-main { stroke: var(--brand, #2563eb); stroke-width: 2; }
.mm-chart-cursor { stroke: var(--brand, #2563eb); stroke-width: 1; stroke-dasharray: 2 2; opacity: .7; }
.mm-chart-dot { fill: var(--brand, #2563eb); }

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

.mm-formula-code {
  display: block;
  margin-top: 6px;
  font-size: .76rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.mm-token {
  display: inline;
  padding: 1px 4px;
  margin: 0 1px;
  border-radius: 4px;
  border: 0;
  font: inherit;
  font-weight: 700;
}

.mm-token.is-lhs { background: var(--accent-soft, #eef1ff); color: var(--brand, #2563eb); cursor: pointer; }
.mm-token.is-var { background: rgba(13, 148, 136, .14); color: #0f766e; cursor: pointer; }
.mm-token.is-var:hover { text-decoration: underline; }
.mm-token.is-param { background: rgba(217, 119, 6, .12); color: #b45309; cursor: help; }

.mm-param-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.mm-param-chip {
  padding: 2px 6px;
  border-radius: 4px;
  font-size: .68rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-card, #fff);
  border: 1px solid var(--line, #dfe5ef);
  color: var(--muted, #475569);
}

.mm-inspector-deps {
  display: grid;
  gap: .65rem;
}

.mm-dep-group h4 {
  margin: 0 0 .35rem;
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted, #64748b);
}

/* Block Order View */
.mm-blocks-view {
  display: grid;
  gap: .7rem;
}

.mm-block-card {
  padding: .75rem .9rem;
  border: 1px solid var(--line, #dfe5ef);
  border-radius: .6rem;
  background: var(--surface-subtle, #f8fafc);
}

.mm-block-card.is-simultaneous {
  border-color: var(--brand, #2563eb);
  background: rgba(37, 99, 235, .05);
}

.mm-block-card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .5rem;
  margin-bottom: .5rem;
}

.mm-block-deps { font-size: .74rem; color: var(--muted, #64748b); }
.mm-block-vars { display: flex; flex-wrap: wrap; gap: .4rem; }

/* Classic Focused 3-Column Trace View */
.model-relationships-diagram { position: relative; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr); gap: clamp(3rem, 6vw, 6rem); margin-top: 1rem; }
.model-relationships-edges { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.model-relationships-edges > path { fill: none; stroke: var(--model-edge, #526174); stroke-width: 1.5; }
.model-relationships-edges .model-relationships-arrow { fill: var(--model-edge, #526174); }
.model-relationships-column { position: relative; min-width: 0; }
.model-relationships-inputs { grid-column: 1; grid-row: 1; }
.model-relationships-center { grid-column: 2; grid-row: 1; }
.model-relationships-outputs { grid-column: 3; grid-row: 1; }
.model-relationships-column h4 { margin: 0 0 .75rem; }
.model-relationships-column .model-relationships-list { display: grid; gap: .7rem; }
.model-relationships-node, .model-relationships-selected { display: block; width: 100%; min-width: 0; box-sizing: border-box; overflow-wrap: anywhere; white-space: normal; text-align: left; }
.model-relationships-node { position: relative; background: var(--surface-card, #fff); }
.model-relationships-node:hover { transform: none; }
.model-relationships-selected { display: flex; align-items: center; min-height: max(3rem, calc((var(--model-input-count, 2) + 1) * 10px)); padding: .8rem; color: #fff; background: #2458a5; border: 2px solid #17427e; border-radius: .4rem; }
.model-relationships-selected:focus-visible { outline: 3px solid #2458a5; outline-offset: 3px; }
.model-relationships-observation { display: flex; flex-wrap: wrap; gap: .25rem .65rem; margin-top: .35rem; overflow-wrap: anywhere; align-items: center; }
.model-relationships-observation-value { font-weight: 700; }
.model-relationships-column p { overflow-wrap: anywhere; }
.model-relationships-list { display: flex; flex-wrap: wrap; gap: .4rem; }
.model-relationships-list button { max-width: 100%; overflow-wrap: anywhere; white-space: normal; }
.model-relationships-list button[aria-pressed="true"] { outline: 2px solid #2458a5; outline-offset: 2px; }
.model-relationships-all { margin-top: .9rem; }
.mm-var-drawer summary { cursor: pointer; font-weight: 700; font-size: .82rem; color: var(--muted, #64748b); margin-bottom: .45rem; }
.model-relationships-status { margin: .45rem 0; }
.model-relationships-value { margin: .55rem 0; font-size: .78rem; color: var(--muted, #64748b); overflow-wrap: anywhere; }

/* Studio Context Help */
.studio-context-help { display: inline-block; position: relative; vertical-align: middle; }
.studio-context-help > button { display: grid; place-items: center; width: 1.3rem; height: 1.3rem; padding: 0; color: inherit; background: transparent; border: 1px solid currentColor; border-radius: 50%; font: inherit; font-size: .8rem; font-weight: 700; cursor: pointer; }
.studio-context-help > button:focus-visible { outline: 2px solid #2458a5; outline-offset: 2px; }
.studio-context-help > span { position: fixed; z-index: 30; top: var(--help-top, 1rem); left: var(--help-left, 1rem); box-sizing: border-box; width: min(18rem, calc(100vw - 1rem)); max-height: calc(100vh - 1rem); overflow: auto; padding: .65rem; color: #172033; background: #fff; border: 1px solid #94a3b8; border-radius: .35rem; box-shadow: 0 .25rem .8rem #0003; font-size: .875rem; font-weight: 400; line-height: 1.35; }

/* Dark Theme Overrides */
html[data-theme="dark"] .model-relationships { --model-edge: var(--accent, #83a5ff); color: var(--ink, #e5ebf3); }
html[data-theme="dark"] .mm-canvas-viewport { background-color: #111827; background-image: radial-gradient(#243244 1px, transparent 1px); }
html[data-theme="dark"] .mm-node { background: #1a2332; border-color: #334155; color: #edf2f7; }
html[data-theme="dark"] .mm-node.is-exogenous { background: #132833; border-color: #2dd4bf; }
html[data-theme="dark"] .mm-node.is-heat-up { background: rgba(22, 163, 74, var(--heat-alpha, .22)); border-color: #4ade80; }
html[data-theme="dark"] .mm-node.is-heat-down { background: rgba(220, 38, 38, var(--heat-alpha, .22)); border-color: #f87171; }
html[data-theme="dark"] .mm-node-tag { background: #243044; color: #b6c2d3; }
html[data-theme="dark"] .mm-node-val { color: #edf2f7; }
html[data-theme="dark"] .mm-node-heat-pill.is-up, html[data-theme="dark"] .mm-scenario-pill.is-up { color: #4ade80; }
html[data-theme="dark"] .mm-node-heat-pill.is-down, html[data-theme="dark"] .mm-scenario-pill.is-down { color: #f87171; }
html[data-theme="dark"] .mm-role-badge.is-exogenous, html[data-theme="dark"] .mm-token.is-var { color: #5eead4; }
html[data-theme="dark"] .mm-role-badge.is-sim, html[data-theme="dark"] .mm-token.is-param, html[data-theme="dark"] .mm-edge-lag-label { color: #fbbf24; }
html[data-theme="dark"] .mm-timeline-badge.is-forecast { color: #5eead4; }
html[data-theme="dark"] .model-relationships-selected { color: var(--ink, #e5ebf3); background: var(--brand-soft, #28334f); border-color: var(--accent, #83a5ff); }
html[data-theme="dark"] .model-relationships-selected:focus-visible,
html[data-theme="dark"] .model-relationships-list button[aria-pressed="true"] { outline-color: var(--accent, #83a5ff); }
html[data-theme="dark"] .studio-context-help > button:focus-visible { outline-color: var(--accent, #83a5ff); }
html[data-theme="dark"] .studio-context-help > span { color: var(--ink, #e5ebf3); background: var(--panel-soft, #202a39); border-color: var(--accent, #83a5ff); }

@container (max-width: 700px) {
  .model-relationships-diagram { grid-template-columns: 1fr; gap: 2rem; }
  .model-relationships-column { padding-inline: 2rem; grid-column: 1; grid-row: auto; }
  .model-relationships-selected { min-height: 3rem; }
}
