:root {
  --bg: #f5f2ea;
  --panel: #fffdf8;
  --ink: #172428;
  --muted: #657377;
  --line: #d8d1c4;
  --teal: #087b75;
  --teal-soft: #dff1ee;
  --blue: #315f88;
  --blue-soft: #e3edf5;
  --amber: #b8791e;
  --amber-soft: #f6ead1;
  --red: #b9412e;
  --red-soft: #f4dfda;
  --green: #28795b;
  --green-soft: #dfeee6;
  --purple: #7254a8;
  --shadow: 0 10px 26px rgba(23, 36, 40, 0.10);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: light;
  font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  scroll-behavior: smooth;
}

body {
  min-width: 320px;
  margin: 0;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) 14px 12px;
  border-bottom: 1px solid var(--line);
  background: #fbfaf6;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--teal);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.app-header h1 {
  margin: 0;
  max-width: 820px;
  font-size: clamp(24px, 7.4vw, 42px);
  line-height: 1.04;
  letter-spacing: 0;
}

.subtitle {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.paper-badge {
  flex: 0 0 82px;
  min-height: 58px;
  display: grid;
  place-content: center;
  gap: 2px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: 0 8px 18px rgba(23, 36, 40, 0.08);
  text-align: center;
}

.paper-badge strong {
  font-size: 13px;
}

.paper-badge span {
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  line-height: 1.2;
}

.command-bar {
  position: sticky;
  top: 0;
  z-index: 50;
  padding: 8px 8px 9px;
  border-bottom: 1px solid rgba(216, 209, 196, 0.86);
  background: rgba(245, 242, 234, 0.96);
  box-shadow: 0 12px 24px rgba(23, 36, 40, 0.12);
  backdrop-filter: blur(9px);
}

.metrics-strip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(112px, 1fr);
  gap: 5px;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding-bottom: 7px;
  scrollbar-width: none;
}

.metrics-strip::-webkit-scrollbar,
.button-row::-webkit-scrollbar {
  display: none;
}

.metric {
  min-width: 0;
  min-height: 66px;
  padding: 7px 6px;
  border: 1px solid #c9d6d5;
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(255,255,255,0.98), rgba(247,249,249,0.95));
  box-shadow: 0 7px 16px rgba(23, 36, 40, 0.08);
}

.metric span,
.metric small {
  display: block;
  color: var(--muted);
  font-size: 8.5px;
  font-weight: 800;
  line-height: 1.2;
}

.metric strong {
  display: block;
  margin: 4px 0 3px;
  font-size: 15px;
  line-height: 1.05;
  overflow-wrap: anywhere;
}

.metric--risk {
  border-color: #b8d8d4;
  background: var(--teal-soft);
}

.metric--status {
  border-color: #e4cb92;
  background: var(--amber-soft);
}

.metric--status.is-safe {
  border-color: #b8d8c5;
  background: var(--green-soft);
}

.metric--status.is-danger {
  border-color: #dda298;
  background: var(--red-soft);
}

.control-strip {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.button-row {
  display: flex;
  gap: 8px;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
}

.case-button,
.scenario-button,
.decision-button,
.evidence-button,
.command-button {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  font-weight: 900;
  white-space: nowrap;
}

.case-button.is-active,
.scenario-button.is-active,
.decision-button.is-active,
.evidence-button.is-active {
  border-color: var(--teal);
  background: var(--teal);
  color: #fff;
}

.decision-button.is-active {
  background: var(--blue);
  border-color: var(--blue);
}

.command-button {
  min-height: 42px;
  border-color: var(--teal);
  background: var(--teal);
  color: #fff;
}

.command-button--quiet {
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
}

.app-shell {
  width: min(100% - 16px, 1180px);
  margin: 12px auto 42px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.app-shell > * {
  min-width: 0;
}

.card,
.stage-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.card {
  padding: 13px;
}

.stage-card {
  padding: 13px;
  background:
    linear-gradient(135deg, rgba(223, 241, 238, 0.98), rgba(227, 237, 245, 0.96)),
    repeating-linear-gradient(90deg, rgba(8,123,117,0.08) 0 1px, transparent 1px 42px);
}

.stage-card h2,
.card h2 {
  margin: 0;
  font-size: clamp(20px, 5.8vw, 28px);
  line-height: 1.16;
  letter-spacing: 0;
}

.stage-card p,
.card-note {
  margin: 9px 0 0;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.55;
}

.stage-flow {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 13px 0 0;
  padding: 0;
  list-style: none;
}

.stage-flow li {
  min-height: 42px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.74);
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.stage-flow li.is-active {
  border-color: var(--teal);
  background: #fff;
  color: var(--ink);
  box-shadow: inset 0 0 0 2px rgba(8,123,117,0.16);
}

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

.tag {
  flex: 0 0 auto;
  max-width: 122px;
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: #254c70;
  font-size: 11px;
  font-weight: 900;
  text-align: center;
}

.visual-frame,
.canvas-frame {
  position: relative;
  min-height: 250px;
  overflow: hidden;
  border: 1px solid #cbd5d7;
  border-radius: var(--radius);
  background: #fbfdfe;
}

.pipe-frame {
  min-height: 300px;
}

#pipeCanvas,
#riskChart {
  display: block;
  width: 100%;
  height: 100%;
}

#pipeCanvas {
  min-height: 300px;
}

#riskChart {
  min-height: 270px;
}

.pipe-callout {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  display: grid;
  gap: 3px;
  padding: 9px 10px;
  border: 1px solid rgba(216,209,196,0.92);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.86);
  backdrop-filter: blur(7px);
}

.pipe-callout strong {
  font-size: 14px;
}

.pipe-callout span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  line-height: 1.35;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

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

.legend i {
  width: 18px;
  height: 5px;
  border-radius: 999px;
}

.legend-uniform {
  background: var(--amber);
}

.legend-localized {
  background: var(--red);
}

.legend-erosion {
  background: var(--blue);
}

.legend-mic {
  background: var(--purple);
}

.formula {
  margin-bottom: 10px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  font-size: 13px;
  font-weight: 900;
  overflow-wrap: anywhere;
}

.bayes-network {
  display: block;
  width: 100%;
  min-height: 252px;
  border: 1px solid #d7dde0;
  border-radius: var(--radius);
  background:
    linear-gradient(180deg, #fbfdfe, #f3f8f9),
    repeating-linear-gradient(90deg, rgba(49,95,136,0.06) 0 1px, transparent 1px 36px);
  touch-action: none;
}

.bayes-network marker path {
  fill: #acb6bb;
}

.bn-link {
  fill: none;
  stroke: #acb6bb;
  stroke-width: 1.6;
}

.bn-link.is-active {
  stroke: var(--teal);
  stroke-width: 3;
}

[data-bn] {
  cursor: grab;
}

[data-bn].is-dragging {
  cursor: grabbing;
}

.bn-node {
  fill: #fff;
  stroke: #7f9097;
  stroke-width: 2;
  filter: drop-shadow(0 4px 6px rgba(23,36,40,0.16));
}

[data-bn].is-active .bn-node {
  fill: var(--teal-soft);
  stroke: var(--teal);
  stroke-width: 4;
}

[data-bn="pof"].is-active .bn-node,
[data-bn="decision"].is-active .bn-node {
  fill: var(--amber-soft);
  stroke: var(--amber);
}

.bn-label,
.bn-prob {
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}

.bn-label {
  fill: #213238;
  font-size: 11px;
  font-weight: 900;
}

.bn-prob {
  fill: var(--muted);
  font-size: 10px;
  font-weight: 800;
}

.decision-visual {
  display: grid;
  gap: 5px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    linear-gradient(90deg, rgba(8,123,117,0.18) var(--decision-fill, 28%), rgba(255,255,255,0.92) var(--decision-fill, 28%));
}

.decision-visual span,
.decision-visual small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.decision-visual strong {
  font-size: clamp(28px, 11vw, 46px);
  line-height: 1;
}

.decision-flow {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.flow-chip {
  display: grid;
  grid-template-columns: 0.8fr 1.2fr;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.82);
}

.flow-chip b {
  font-size: 12px;
}

.flow-chip span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  line-height: 1.35;
}

.rank-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.rank-row {
  display: grid;
  grid-template-columns: minmax(104px, 1fr) 1.2fr 32px;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.rank-bar {
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: #e7ecec;
}

.rank-bar span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--blue), var(--teal));
}

.action-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 12px;
}

.decision-log {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  line-height: 1.45;
}

.settings-card {
  display: grid;
  gap: 12px;
}

.slider {
  display: grid;
  gap: 8px;
}

.slider span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
}

.slider b {
  color: var(--ink);
}

.slider input {
  width: 100%;
  accent-color: var(--teal);
}

.story-grid {
  display: grid;
  gap: 9px;
}

.story-grid article {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 4px 10px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.78);
}

.story-grid article span {
  grid-row: 1 / span 2;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  background: var(--teal-soft);
  color: var(--teal);
  font-weight: 900;
}

.story-grid strong {
  font-size: 13px;
}

.story-grid p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.45;
}

.evidence-tabs {
  margin-bottom: 10px;
}

.evidence-layout {
  display: grid;
  gap: 10px;
}

.source-notes {
  display: grid;
  gap: 8px;
}

.source-notes p {
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.78);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.source-notes strong {
  color: var(--ink);
}

.paper-image {
  width: 100%;
  max-height: 720px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.site-footer {
  padding: 0 14px max(22px, env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

@media (min-width: 720px) {
  .app-header,
  .command-bar {
    padding-inline: 18px;
  }

  .metrics-strip {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-auto-flow: initial;
  }

  .control-strip {
    grid-template-columns: auto 1fr auto;
    align-items: center;
  }

  .app-shell {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
  }

  .stage-card,
  .decision-card,
  .settings-card,
  .story-card,
  .evidence-card {
    grid-column: 1 / -1;
  }

  .action-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .story-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .evidence-layout {
    grid-template-columns: 0.88fr 1.12fr;
    align-items: start;
  }
}

@media (min-width: 1024px) {
  .app-header,
  .command-bar {
    padding-inline: 28px;
  }

  .app-shell {
    width: min(100% - 56px, 1280px);
  }

  .card {
    padding: 16px;
  }

  .metric strong {
    font-size: 21px;
  }
}

@media (max-width: 410px) {
  .app-header {
    gap: 8px;
  }

  .subtitle {
    display: none;
  }

  .paper-badge {
    flex-basis: 74px;
    min-height: 52px;
    padding: 6px;
  }

  .metric {
    min-height: 62px;
    padding: 6px 5px;
  }

  .metric strong {
    font-size: 13px;
  }

  .metric span,
  .metric small {
    font-size: 8px;
  }

  .stage-flow {
    grid-template-columns: 1fr;
  }

  .flow-chip,
  .rank-row {
    grid-template-columns: 1fr;
  }
}
