* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0b1220;
  --panel: #11192a;
  --text: #e8eef6;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --root: #8b5cf6;
  --mediator: #22c55e;
  --outcome: #ef4444;
  --edge-pos: #22c55e;
  --edge-neg: #ef4444;
  --intervene: #f59e0b;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 320px;
  grid-template-rows: auto 1fr auto;
  height: 100%;
  min-height: 700px;
}

header {
  grid-column: 1 / -1;
  grid-row: 1;
  padding: 1rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid #1e293b;
}

header h1 {
  font-size: 1.25rem;
  margin-bottom: 0.25rem;
  letter-spacing: -0.02em;
}

header p {
  color: var(--muted);
  font-size: 0.85rem;
}

aside#controls {
  grid-column: 1;
  grid-row: 2;
  background: var(--panel);
  border-right: 1px solid #1e293b;
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

aside#controls h2 {
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
  color: var(--text);
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.control-group label {
  font-size: 0.8rem;
  color: var(--muted);
}

.model-info {
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.5;
}

.intervention-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.85rem;
}

.intervention-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 6px;
}

.do-badge {
  display: inline-block;
  background: var(--intervene);
  color: #000;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  letter-spacing: 0.05em;
}

.arrow {
  color: var(--muted);
  font-size: 0.85rem;
}

.effect {
  color: #60a5fa; /* lighter than --accent to read on the dark item background */
  font-size: 0.8rem;
}

.muted {
  color: var(--muted);
  font-size: 0.8rem;
}

.intervention-card {
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 0.75rem;
  gap: 0.6rem;
}

.intervention-card h3 {
  font-size: 0.9rem;
  color: var(--text);
  margin: 0;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#control-slider {
  flex: 1;
  accent-color: var(--accent);
}

.slider-value {
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  min-width: 44px;
  text-align: right;
  color: var(--text);
}

.button-row {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

button,
.btn {
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  background: #1e293b;
  color: var(--text);
  border: 1px solid #334155;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  min-height: 40px;
  text-align: left;
}

button:hover,
.btn:hover {
  background: #334155;
  border-color: var(--accent);
}

.btn-full {
  width: 100%;
  text-align: center;
}

.btn-danger {
  background: #7f1d1d;
  border-color: #991b1b;
  color: #fecaca;
}
.btn-danger:hover {
  background: #991b1b;
  border-color: var(--accent);
}

.btn-outline {
  background: transparent;
}

/* ===== Main graph area: force graph on top, DAG below ===== */

#graph-container {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 600px;
  background: #0a0f1a;
  border: 1px solid #1e293b;
  display: flex;
  flex-direction: column;
}

#force-section {
  height: 380px;
  min-height: 380px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.knowledge-graph {
  display: block;
  width: 100%;
  height: 340px;
  background: #0a0f1a;
  border-bottom: 1px solid #1e293b;
}

#dag-section {
  flex: 1 1 0;
  min-height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

#graph {
  display: block;
  width: 100%;
  height: 100%;
}

/* ===== Force graph (knowledge network) styling ===== */

.force-link {
  stroke: #334155;
  stroke-opacity: 0.6;
  transition: stroke 0.2s, stroke-width 0.2s;
}
.force-link.pos { stroke: var(--edge-pos); }
.force-link.neg { stroke: var(--edge-neg); }
.force-link.active { stroke-width: 3; stroke-opacity: 1; }

.force-node {
  cursor: pointer;
}

.force-node circle {
  stroke: #1e293b;
  stroke-width: 1.5;
}
.force-node.intervened circle {
  stroke: var(--intervene);
  stroke-width: 3;
}
.force-node.highlighted circle {
  stroke: var(--accent);
  stroke-width: 4;
}

.force-label {
  fill: var(--text);
  font-size: 16px;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
  letter-spacing: 0.03em;
}

.force-value {
  fill: var(--accent);
  font-size: 14.5px;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
}

/* ===== Hierarchical DAG styling ===== */

.link {
  fill: none;
  stroke-width: 2;
  stroke: #334155;
  transition: stroke 0.2s;
}
.link.pos { stroke: var(--edge-pos); }
.link.neg { stroke: var(--edge-neg); }
.link.active { stroke-width: 3; opacity: 1; }

.node {
  cursor: pointer;
}

.node-bg {
  fill: #1e293b;
  stroke: #334155;
  stroke-width: 1.5;
  rx: 8;
  ry: 8;
}
.node.intervened .node-bg {
  stroke: var(--intervene);
  stroke-width: 2.5;
  fill: #1c140a;
}
.node.highlighted .node-bg {
  stroke: var(--accent);
  stroke-width: 3;
  fill: #0b172a;
}

.node-label {
  fill: var(--text);
  font-size: 16px;
  font-weight: 700;
  pointer-events: none;
  letter-spacing: 0.03em;
}

.node-value {
  fill: var(--accent);
  font-size: 14.5px;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  pointer-events: none;
}

.node-type {
  fill: var(--muted);
  font-size: 14.5px;
  font-weight: 600;
  pointer-events: none;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.node-intervene-badge {
  fill: var(--intervene);
  font-size: 14.5px;
  font-weight: 800;
  pointer-events: none;
}

#reasoning-panel {
  grid-column: 3;
  grid-row: 2;
  background: var(--panel);
  border-left: 1px solid #1e293b;
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

#reasoning-panel h2 {
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
  color: var(--text);
}

#reasoning {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  flex: 1;
}

.reasoning-section {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.reasoning-section h3 {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.2rem;
}

.reasoning-section p {
  font-size: 0.9rem;
  color: var(--text);
  line-height: 1.5;
}

.path {
  font-size: 0.85rem;
  padding: 0.3rem 0.5rem;
  border-radius: 4px;
  background: #1e293b;
  border: 1px solid #334155;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.path.positive .path-sign { color: var(--edge-pos); }
.path.negative .path-sign { color: #f87171; }

.path-label {
  color: var(--text);
}

.path-sign {
  font-size: 0.75rem;
  font-weight: 700;
}

.counterfactual {
  background: #1c140a;
  border: 1px solid var(--intervene);
  border-radius: 6px;
  padding: 0.5rem;
}

.counterfactual p {
  font-size: 0.85rem;
  color: #fbbf24;
  margin: 0;
  line-height: 1.5;
}

footer {
  grid-column: 1 / -1;
  grid-row: 3;
  padding: 0.6rem 1rem;
  background: var(--panel);
  border-top: 1px solid #1e293b;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

/* Legend */
.legend-items {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: var(--text);
}
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.dot.root { background: var(--root); }
.dot.mediator { background: var(--mediator); }
.dot.outcome { background: var(--outcome); }
.dot.intervened { background: var(--intervene); }
.edge {
  width: 20px;
  height: 2px;
  display: inline-block;
}
.edge.pos { background: var(--edge-pos); }
.edge.neg { background: var(--edge-neg); }

/* Medium (e.g. embedded in the site's content column): two columns, with
   the reasoning panel under the controls so the graphs keep their width. */
@media (max-width: 1100px) {
  #app {
    grid-template-columns: 240px minmax(0, 1fr);
    grid-template-rows: auto auto 1fr auto;
  }
  /* A scroll container's auto row collapses to nothing, so let the controls
     take their full height and scroll the reasoning panel instead. */
  aside#controls { grid-column: 1; grid-row: 2; overflow-y: visible; }
  #reasoning-panel {
    grid-column: 1;
    grid-row: 3;
    border-left: none;
    border-right: 1px solid #1e293b;
    border-top: 1px solid #1e293b;
  }
  #graph-container { grid-column: 2; grid-row: 2 / 4; }
  footer { grid-row: 4; }
}

/* Mobile */
@media (max-width: 701px) {
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto auto auto;
  }
  aside#controls {
    grid-row: 2;
    border-right: none;
    border-bottom: 1px solid #1e293b;
    max-height: 35vh;
  }
  #graph-container {
    grid-column: 1;
    grid-row: 3;
    min-height: 400px;
  }
  #reasoning-panel {
    grid-column: 1;
    grid-row: 4;
    border-left: none;
    border-top: 1px solid #1e293b;
    max-height: 30vh;
  }
  footer { grid-row: 5; }

  /* Let the stacked layout grow to its content (the page frame is sized to
     fit) and size both graphs from their viewBox aspect ratio. */
  #app { height: auto; min-height: 0; }
  /* Capped so turning on interventions doesn't grow the page past the
     phone frame; each panel scrolls inside itself instead. */
  aside#controls { max-height: 440px; overflow-y: auto; }
  #reasoning-panel { max-height: 320px; overflow-y: auto; }
  #graph-container { height: auto; min-height: 0; }
  #force-section { height: auto; min-height: 0; }
  #dag-section { flex: none; min-height: 0; }
  /* The diagrams are drawn 720 units wide; shrunk to a phone's width their
     labels become unreadable. Keep them 480px wide and let each panel scroll
     sideways instead (the page itself never does). */
  #force-section, #dag-section { overflow-x: auto; justify-content: safe center; }
  .knowledge-graph, #graph { flex: none; width: 480px; height: auto; }
}
