:root {
  --bg: #f2efe8;
  --panel: #fffdf7;
  --ink: #2c2a28;
  --muted: #746d64;
  --accent: #0d5c63;
  --accent-soft: #d7eef0;
  --warn: #a4372e;
  --edge: #6f6a64;
  --flow: #d65a31;
  --good: #2a9d8f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Space Grotesk", sans-serif;
  color: var(--ink);
  overflow-x: clip;
  background:
    radial-gradient(circle at 20% 15%, #ffffff 0%, #f2efe8 40%, #e8e2d8 100%),
    repeating-linear-gradient(
      -45deg,
      rgba(13, 92, 99, 0.03) 0,
      rgba(13, 92, 99, 0.03) 10px,
      transparent 10px,
      transparent 20px
    );
}

.app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px;
}

.menu-panel {
  width: calc(100vw - 50px);
  margin-left: calc(50% - 50vw + 25px);
  background: var(--panel);
  border: 1px solid #d3cec7;
  border-radius: 12px;
  padding: 6px 8px;
}

.menu-panel > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  color: var(--ink);
  font-family: "IBM Plex Mono", monospace;
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-panel > summary::-webkit-details-marker {
  display: none;
}

.menu-panel > summary::before {
  content: "▾";
  font-size: 0.95rem;
  color: var(--accent);
}

.menu-panel:not([open]) > summary::before {
  content: "▸";
}

.menu-content {
  margin-top: 6px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1rem, 1.5vw, 1.35rem);
}

.topbar p {
  margin: 2px 0 6px;
  font-size: 0.84rem;
  color: var(--muted);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 6px;
  background: var(--panel);
  border: 1px solid #d3cec7;
  border-radius: 12px;
}

.controls label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}

.controls input,
.controls select,
.controls button {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.82rem;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid #b6afa7;
  background: #fff;
  color: var(--ink);
}

.controls button {
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.controls button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(44, 42, 40, 0.15);
}

.side-panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(24, 21, 19, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
  z-index: 40;
}

.side-panel-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.side-panel {
  position: fixed;
  top: 12px;
  right: 2vw;
  width: 96vw;
  height: calc(100vh - 24px);
  padding: 10px;
  background: #fffdf8;
  border-left: 1px solid #d3cec7;
  border-radius: 12px;
  box-shadow: -8px 0 24px rgba(23, 21, 20, 0.18);
  transform: translateX(100%);
  transition: transform 200ms ease;
  display: grid;
  grid-template-rows: auto auto auto 1fr auto;
  gap: 10px;
  z-index: 50;
}

.side-panel.open {
  transform: translateX(0);
}

.side-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.side-panel-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.side-panel-header button,
.side-panel-footer button {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.88rem;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid #b6afa7;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}

.side-panel-help {
  margin: 0;
  font-size: 0.84rem;
  color: var(--muted);
}

.side-panel-error {
  min-height: 1.2rem;
  font-size: 0.82rem;
  color: var(--warn);
}

.side-panel-body {
  overflow: hidden;
  padding-right: 0;
  display: grid;
  gap: 12px;
}

.p-matrix-wrap {
  border: 1px solid #d6d0c8;
  border-radius: 10px;
  background: #fff;
  overflow: hidden;
}

.p-matrix {
  border-collapse: collapse;
  width: 100%;
  table-layout: fixed;
}

.p-matrix th,
.p-matrix td {
  border: 1px solid #e2dcd4;
  padding: 4px;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.64rem;
  white-space: normal;
}

.p-matrix thead th {
  position: sticky;
  top: 0;
  background: #f7f3ec;
  z-index: 1;
}

.p-matrix tbody th {
  background: #fbf9f4;
  color: #595349;
}

.p-matrix td input {
  width: 100%;
  min-width: 0;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.7rem;
  padding: 3px 4px;
  border-radius: 7px;
  border: 1px solid #b6afa7;
}

.p-matrix td.self-calculated {
  color: #5d574f;
  background: #faf7f2;
}

.p-matrix td.no-edge {
  background: #fcfbf8;
}

.p-matrix td.prob-cell-highlight {
  background: #fff5cf;
  outline: 2px solid #e2b43f;
  outline-offset: -2px;
}

.p-matrix input.prob-input-highlight {
  border-color: #e2b43f;
  box-shadow: 0 0 0 2px rgba(226, 180, 63, 0.25);
}

.p-matrix .row-validation {
  min-width: 72px;
  color: transparent;
}

.p-matrix .row-validation.invalid {
  color: var(--warn);
  font-weight: 600;
}

.p-ref-section {
  border: 1px solid #d6d0c8;
  border-radius: 10px;
  background: #fff;
  padding: 8px;
  display: grid;
  gap: 8px;
}

.p-ref-section h3 {
  margin: 0;
  font-size: 0.8rem;
  color: #5c554b;
}

.p-ref-row {
  display: grid;
  grid-template-columns: 1fr 120px;
  align-items: center;
  gap: 8px;
  padding: 4px;
  border-radius: 8px;
}

.p-ref-row label {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.74rem;
}

.p-ref-row input {
  width: 100%;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.76rem;
  padding: 4px 6px;
  border-radius: 7px;
  border: 1px solid #b6afa7;
}

.p-group {
  border: 1px solid #d6d0c8;
  border-radius: 10px;
  background: #fff;
  padding: 9px;
  display: grid;
  gap: 8px;
}

.p-group h3 {
  margin: 0;
  font-size: 0.88rem;
  color: var(--muted);
}

.p-row {
  display: grid;
  grid-template-columns: 1fr 108px;
  align-items: center;
  gap: 8px;
}

.p-row label {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.78rem;
  color: #3f3a34;
}

.p-row input {
  width: 100%;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.86rem;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid #b6afa7;
  background: #fff;
}

.side-panel-footer {
  display: flex;
  justify-content: flex-end;
}

.metrics {
  margin-top: 8px;
  display: grid;
  gap: 6px;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
}

.trend-panel {
  margin-top: 8px;
  max-height: 0;
  opacity: 0;
  transform: translateY(-6px);
  overflow: hidden;
  transition: max-height 220ms ease, opacity 180ms ease, transform 180ms ease;
}

.trend-panel.open {
  max-height: 360px;
  opacity: 1;
  transform: translateY(0);
}

#trendChart {
  width: 100%;
  height: 320px;
  display: block;
  border: 1px solid #d3cec7;
  border-radius: 10px;
  background: linear-gradient(180deg, #fffdf9 0%, #f6f0e5 100%);
}

.trend-axis {
  stroke: #7a736a;
  stroke-width: 1.3;
}

.trend-grid {
  stroke: rgba(116, 109, 100, 0.28);
  stroke-width: 1;
}

.trend-tick {
  fill: #5f584f;
  font-family: "IBM Plex Mono", monospace;
  font-size: 11px;
}

.trend-line {
  fill: none;
  stroke-width: 2.1;
  vector-effect: non-scaling-stroke;
}

.metric-card {
  background: var(--panel);
  border: 1px solid #d3cec7;
  border-radius: 10px;
  padding: 9px 10px;
}

.metric-card .k {
  display: block;
  font-size: 0.76rem;
  color: var(--muted);
}

.metric-card .v {
  display: block;
  margin-top: 3px;
  font-family: "IBM Plex Mono", monospace;
  font-size: 1rem;
  font-weight: 500;
}

.graph-wrap {
  margin-top: 12px;
  width: calc(100vw - 50px);
  margin-left: calc(50% - 50vw + 25px);
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid #d3cec7;
  background: linear-gradient(180deg, #f9f7f2 0%, #efebe2 100%);
}

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

.node rect,
.node ellipse,
.node path {
  fill: #ece8e2;
  stroke: #5f5b55;
  stroke-width: 1.8;
}

.node text {
  text-anchor: middle;
  fill: #191715;
  pointer-events: none;
}

.node .name {
  font-size: 20px;
  font-weight: 500;
}

.node .count {
  font-size: 18px;
  font-family: "IBM Plex Mono", monospace;
}

.node.small .name {
  font-size: 15px;
}

.node.small .count {
  font-size: 14px;
}

.edge {
  fill: none;
  stroke: var(--edge);
  stroke-width: 2;
  marker-end: url(#arrowHead);
  opacity: 0.95;
}

.edge.referral {
  stroke-dasharray: 8 6;
  stroke: var(--good);
}

.edge.revenue {
  stroke-dasharray: 4 6;
  stroke: var(--warn);
}

.edge.external {
  stroke-dasharray: 5 8;
  stroke: #3c5870;
}

.edge-label {
  font-size: 12px;
  fill: #2d2a27;
  text-anchor: middle;
  font-family: "IBM Plex Mono", monospace;
  paint-order: stroke;
  stroke: #f7f4ef;
  stroke-width: 4px;
  stroke-linejoin: round;
}

.prob-click-target {
  cursor: pointer;
}

.flow-dot {
  fill: var(--flow);
  opacity: 0.9;
}

.flow-dot.referral {
  fill: var(--good);
}

.flow-dot.revenue {
  fill: var(--warn);
}

@media (max-width: 900px) {
  #graph {
    width: 100%;
    height: auto;
  }

  .node .name {
    font-size: 15px;
  }

  .node .count {
    font-size: 13px;
  }
}
