:root {
  --bg: #eef1f7;
  --card: #ffffff;
  --ink: #1f2233;
  --muted: #5d6178;
  --line: #dfe3ee;
  --accent: #3b3f8f;
  --accent-dark: #2e2a6b;
  --accent-soft: #eef0fa;
  --swap: #e05252;
  --pur: #4c8dff;
  --good: #1b7f4b;
  --bad: #b23b3b;
  --shadow: 0 1px 2px rgba(20, 20, 60, 0.05), 0 6px 20px rgba(20, 20, 60, 0.07);
  /* the stage is dark in both themes, like a lab display */
  --stage: #2d3650;
  --stage-edge: #aab4e6;
  --stage-node: #f4f5ff;
  --stage-ink: #ffffff;
  --stage-muted: #c3c9e8;
  --run: #2f9e6a; --next: #3a7bd5; --play: #6c5ce7; --end: #4b8bd6; --reset: #2f3b52; --compare: #d68a2f;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12131a;
    --card: #1c1e28;
    --ink: #e8e9f2;
    --muted: #a3a7bf;
    --line: #2d3040;
    --accent: #8b8ff0;
    --accent-dark: #b4b7ff;
    --accent-soft: #262a45;
    --good: #5ccf8a;
    --bad: #f08c8c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
    --stage: #232a3d;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
body.modal-open { overflow: hidden; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; letter-spacing: -0.01em; margin: 0; }
h2 { font-size: 1rem; font-weight: 650; }
h3 { font-size: 1.1rem; font-weight: 650; margin-bottom: 0.5rem; }
kbd {
  font: 600 0.72rem/1 inherit; padding: 2px 5px; border-radius: 4px;
  background: rgba(255,255,255,0.22); color: inherit; margin-left: 6px; vertical-align: 1px;
}
.card { background: var(--card); border-radius: 12px; box-shadow: var(--shadow); }
.app { max-width: 1240px; margin: 0 auto; padding: 18px 20px 60px; display: grid; gap: 16px; }
.app > *, .main > *, .row2 > *, .ops-grid > * { min-width: 0; }
.controls label.nowrap { white-space: normal; }

/* title & actions */
.title-card { text-align: center; padding: 22px 20px 18px; }
.ico-defs { position: absolute; width: 0; height: 0; overflow: hidden; }
.title-links { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 15px; }
.pill {
  display: inline-flex; align-items: center; gap: 7px; padding: 7px 15px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--card); color: var(--accent);
  font: inherit; font-size: 0.88rem; font-weight: 600; text-decoration: none; white-space: nowrap;
  cursor: pointer; transition: background 0.15s, border-color 0.15s, transform 0.08s;
}
.pill:hover { background: var(--accent-soft); border-color: var(--accent); }
.pill:active { transform: translateY(1px); }
.pill .ico { width: 1.05em; height: 1.05em; }
.title-card h1 { font-size: 1.7rem; font-weight: 700; }
.subtitle { margin: 6px 0 0; color: var(--muted); font-size: 0.92rem; }
.action-bar { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; padding: 12px 16px; }
.act {
  border: none; border-radius: 8px; padding: 9px 16px; color: #fff; font: inherit; font-weight: 600;
  cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.15); transition: transform 0.08s, filter 0.15s;
}
.act:hover:not(:disabled) { filter: brightness(1.08); }
.act:active:not(:disabled) { transform: translateY(1px); }
.act:disabled { opacity: 0.4; cursor: default; }
.act.run { background: var(--run); }
.act.next { background: var(--next); }
.act.play { background: var(--play); }
.act.end { background: var(--end); }
.act.reset { background: var(--reset); }
.act.compare { background: var(--compare); }

/* main split: controls and the result card share the left column, the stage
   takes the right one. On narrow screens the stage comes first instead. */
.main { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 16px; align-items: start; }
.main > .controls { grid-column: 1; grid-row: 1; }
.main > .stage-col { grid-column: 2; grid-row: 1 / span 2; }
.main > .results { grid-column: 1; grid-row: 2; }

.controls, .results { padding: 14px 16px; }
.controls-head, .card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.help-btn {
  width: 26px; height: 26px; flex: none; border-radius: 50%; border: none; background: var(--accent); color: #fff;
  font-weight: 700; cursor: pointer; font-size: 0.85rem; line-height: 1;
}
.help-btn:hover { background: var(--accent-dark); }
/* small inline "?" next to a label */
.help-dot {
  display: inline-flex; align-items: center; justify-content: center; vertical-align: 1px;
  width: 18px; height: 18px; padding: 0; margin-left: 5px; border-radius: 50%;
  border: 1px solid var(--accent); background: none; color: var(--accent);
  font: 700 0.68rem/1 inherit; cursor: pointer;
}
.help-dot:hover { background: var(--accent); color: #fff; }
.controls details { border-top: 1px solid var(--line); padding: 8px 0 10px; }
.controls summary { cursor: pointer; font-weight: 600; color: var(--accent); font-size: 0.92rem; padding: 4px 0; list-style: none; display: flex; justify-content: space-between; }
.controls summary::-webkit-details-marker { display: none; }
.controls summary::after { content: "▾"; color: var(--muted); }
.controls details:not([open]) summary::after { content: "▸"; }
.controls label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); margin: 8px 0; }
.controls label.nowrap { white-space: nowrap; }
select, input[type=number], button.small, button.ghost {
  font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px;
}
button.small, button.ghost { cursor: pointer; font-size: 0.85rem; }
button.small:hover, button.ghost:hover { border-color: var(--accent); }
button.danger { color: var(--bad); }
.tools { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.tools button { font: inherit; font-size: 0.83rem; padding: 5px 10px; border-radius: 7px; border: 1px solid var(--line); background: var(--card); color: var(--ink); cursor: pointer; }
.tools button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tools button.ghost { margin-left: auto; }
.hint, .caption { color: var(--muted); font-size: 0.8rem; margin: 4px 0; }
.edge-panel { margin-top: 8px; padding: 10px 12px; border-radius: 10px; background: var(--accent-soft); }
.edge-head { font-size: 0.88rem; }
.edge-panel label { flex-direction: row; align-items: center; justify-content: space-between; margin: 6px 0 2px; }
#edge-f-num { width: 90px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.status { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0; }
.stat { background: var(--bg); border-radius: 8px; padding: 6px 10px; font-size: 0.8rem; color: var(--muted); }
.stat b { display: block; color: var(--ink); font-size: 1rem; font-variant-numeric: tabular-nums; }
.stat.big { grid-column: 1 / -1; text-align: center; padding: 10px; }
.stat.big b { font-size: 2rem; color: var(--accent-dark); line-height: 1.1; }
.status.compact { margin: 10px 0 2px; }
.status.compact .stat { font-size: 0.78rem; padding: 5px 9px; }
.result-label { font-size: 0.82rem; color: var(--muted); margin: 4px 0; }
.history { border-top: none !important; padding: 0 !important; }
.history summary { font-weight: 500 !important; color: var(--muted) !important; font-size: 0.82rem !important; justify-content: flex-start !important; align-items: center; cursor: pointer; }
.history summary::after { content: "" !important; }
.history ol { font-size: 0.8rem; padding-left: 18px; color: var(--muted); margin: 4px 0; }
.history li.merge { color: var(--ink); }
.history li.done { color: var(--accent-dark); font-weight: 600; }

/* stage */
.stage { background: var(--stage); padding: 8px; position: relative; }
#net { width: 100%; height: auto; display: block; touch-action: none; user-select: none; }
#net[data-tool="node"] { cursor: copy; }
#net[data-tool="delete"] { cursor: not-allowed; }
#net[data-tool="edge"], #net[data-tool="source"], #net[data-tool="target"] { cursor: crosshair; }
.stage-help {
  position: absolute; top: 12px; right: 12px; width: 27px; height: 27px; border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.4); background: rgba(255, 255, 255, 0.13); color: #fff;
  font: 700 0.85rem/1 inherit; cursor: pointer; z-index: 2;
}
.stage-help:hover { background: rgba(255, 255, 255, 0.28); }
.message { position: absolute; left: 12px; bottom: 8px; margin: 0; color: #ffd0d0; background: rgba(0,0,0,0.35); padding: 4px 10px; border-radius: 6px; font-size: 0.85rem; }
.step-caption { margin: 8px 4px 2px; min-height: 1.5em; font-size: 0.95rem; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 0.8rem; color: var(--muted); padding: 2px 4px; }
.sw { display: inline-block; width: 24px; height: 0; border-top: 3px solid var(--accent); vertical-align: middle; margin-right: 4px; }
.sw.swap { border-color: var(--swap); border-top-width: 4px; }
.sw.pur { border-color: var(--pur); border-top-width: 4px; border-top-style: dashed; }
.sw.unspent { border-top-style: dotted; opacity: 0.6; }
.sw.faded { opacity: 0.2; }

/* graph drawing on the dark stage */
.edge .edge-hit { fill: none; stroke: transparent; stroke-width: 16; cursor: pointer; }
.edge .edge-line { fill: none; stroke: var(--stage-edge); stroke-width: 2.5; transition: opacity 0.4s, stroke 0.3s; pointer-events: none; }
.edge .edge-label { fill: var(--stage-ink); font-size: 11px; font-variant-numeric: tabular-nums; pointer-events: none; transition: opacity 0.4s; }
.edge .edge-label-bg { fill: var(--stage); opacity: 0.85; pointer-events: none; transition: opacity 0.4s; }
.edge.selected .edge-line { stroke: #fff; stroke-width: 4.5; }
.edge.consumed .edge-line, .edge.consumed .edge-label, .edge.consumed .edge-label-bg { opacity: 0.12; }
.edge.unspent .edge-line { stroke-dasharray: 2 5; opacity: 0.5; }
.edge.unspent .edge-label { opacity: 0.5; }
.edge.next .edge-line { stroke-width: 5; stroke: #fff; animation: pulse 1s ease-in-out infinite; }
.vedge .vedge-line { fill: none; stroke-width: 4.5; stroke-linecap: round; animation: grow 0.5s ease-out; }
.vedge.swap .vedge-line { stroke: var(--swap); }
.vedge.purify .vedge-line { stroke: var(--pur); stroke-dasharray: 9 6; animation: none; }
.vedge .vedge-label { font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; }
.vedge.swap .vedge-label { fill: #ffb3b3; }
.vedge.purify .vedge-label { fill: #b9d3ff; }
.vedge .vedge-label-bg { fill: var(--stage); opacity: 0.9; }
.vedge.next .vedge-line { stroke-width: 6.5; animation: pulse 1s ease-in-out infinite; }
.vedge.final .vedge-line { stroke-width: 6.5; filter: drop-shadow(0 0 6px currentColor); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes grow { from { stroke-dasharray: 0 1000; } to { stroke-dasharray: 1000 0; } }
.node { cursor: grab; }
.node circle { fill: var(--stage-node); stroke: var(--stage-edge); stroke-width: 2.5; transition: stroke-width 0.2s; }
.node text { fill: #1f2233; font-size: 14px; font-weight: 700; pointer-events: none; }
.node text.small { font-size: 9px; }
.node.source circle, .node.target circle { fill: #ffd166; stroke: #ffd166; }
.node .node-role { font-size: 10px; font-weight: 600; fill: var(--stage-muted); letter-spacing: 0.06em; text-transform: uppercase; }
.node.selected circle, .node.pending circle { stroke: #fff; stroke-width: 4.5; }
.node.pending circle { stroke-dasharray: 4 3; }
.node.via circle { stroke: var(--swap); stroke-width: 4; }

/* explainer row */
.row2 { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 0.9fr); gap: 16px; }
.ops, .region-card { padding: 14px 16px; }
.ops-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: center; }
.sliders label { display: block; font-size: 0.85rem; color: var(--muted); margin: 6px 0; }
.sliders label span { display: block; margin-bottom: 2px; }
output { font-variant-numeric: tabular-nums; color: var(--ink); font-weight: 600; margin-left: 4px; }
.readouts { display: flex; gap: 8px; margin: 10px 0 6px; }
.readout { flex: 1; border-radius: 8px; padding: 8px 10px; color: #fff; }
.readout.swap { background: var(--swap); }
.readout.pur { background: var(--pur); }
.readout span { display: block; font-size: 0.75rem; opacity: 0.9; }
.readout b { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.readout small { display: block; font-size: 0.72rem; opacity: 0.9; }
.verdict { font-size: 0.82rem; margin: 4px 0; }
.verdict.good { color: var(--good); }
.verdict.bad { color: var(--bad); }
.ops-diagram { width: 100%; max-width: 330px; display: block; margin: 0 auto; }
.diag-title { font-weight: 600; fill: var(--muted); font-size: 13px; }
.diag-node circle { fill: var(--card); stroke: var(--accent); stroke-width: 2; }
.diag-node.relay circle { stroke-dasharray: 3 3; }
.diag-node text { fill: var(--ink); font-size: 13px; font-weight: 600; text-anchor: middle; dominant-baseline: central; }
.diag-edge line, .diag-edge path { fill: none; stroke: var(--accent); stroke-width: 2.5; }
.diag-edge.swap line { stroke: var(--swap); stroke-width: 3.5; }
.diag-edge.pur line { stroke: var(--pur); stroke-width: 3.5; stroke-dasharray: 8 5; }
.diag-edge text { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.diag-edge.swap text { fill: var(--swap); font-weight: 600; }
.diag-edge.pur text { fill: var(--pur); font-weight: 600; }
.diag-arrow { stroke: var(--muted); stroke-width: 1.5; }
.arrow-head { fill: var(--muted); }

.region-plot { position: relative; width: 280px; max-width: 100%; margin: 0 auto; padding: 0 0 22px 22px; }
.region-plot canvas { width: 100%; height: auto; display: block; border: 1px solid var(--line); cursor: crosshair; touch-action: none; }
.axis-y { position: absolute; left: 0; top: 50%; transform: translateY(-50%); color: var(--muted); }
.axis-x { position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); color: var(--muted); }
.tick { position: absolute; font-size: 0.72rem; color: var(--muted); }
.tick.tl { left: 6px; top: -4px; }
.tick.bl { left: 22px; bottom: 4px; }
.tick.br { right: 0; bottom: 4px; }

/* compare */
#compare { padding: 14px 16px; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.best td { background: var(--accent-soft); }
tr.best .strat-name { color: var(--accent-dark); }
.strat-name { font-weight: 600; }
.strat-blurb { color: var(--muted); font-size: 0.78rem; }
.bar { display: inline-block; width: 80px; height: 7px; background: var(--line); border-radius: 4px; vertical-align: middle; margin-right: 8px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); }
.empty-note { margin: 10px 2px; }

/* credits */
.credits { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 10px 20px; padding: 14px 18px; font-size: 0.9rem; align-items: center; }
.fine { font-size: 0.8rem; color: var(--muted); }
.links { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; }
.links > a, .links > .link-btn { display: inline-flex; align-items: center; gap: 6px; }
.ico { width: 1em; height: 1em; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.ico-solid { fill: currentColor; stroke: none; }
.link-btn { background: none; border: none; color: var(--accent); font: inherit; cursor: pointer; padding: 0; text-decoration: underline; }

/* help */
.fab {
  position: fixed; right: 22px; bottom: 22px; width: 48px; height: 48px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; font-size: 1.3rem; font-weight: 700; cursor: pointer; box-shadow: 0 4px 14px rgba(0,0,0,0.25); z-index: 20;
}
.modal[hidden] { display: none; }
.modal { position: fixed; inset: 0; background: rgba(10, 12, 30, 0.55); display: flex; align-items: center; justify-content: center; z-index: 30; padding: 20px; }
.modal-box { background: var(--card); color: var(--ink); border-radius: 14px; width: min(760px, 100%); max-height: 88vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,0.4); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px 0; border-bottom: 1px solid var(--line); }
.modal-tabs { display: flex; flex-wrap: wrap; gap: 2px; }
.modal-tabs button { background: none; border: none; border-bottom: 2px solid transparent; padding: 8px 10px; font: inherit; font-size: 0.88rem; color: var(--muted); cursor: pointer; }
.modal-tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.close { background: none; border: none; font-size: 1.6rem; line-height: 1; color: var(--muted); cursor: pointer; padding: 0 6px; }
.modal-body { padding: 16px 22px 22px; overflow-y: auto; line-height: 1.55; }
.modal-body p, .modal-body li { margin: 0.45rem 0; }
.modal-body ul, .modal-body ol { padding-left: 20px; }
.modal-body kbd { background: var(--accent-soft); color: var(--accent-dark); margin: 0 2px; }
.modal-body code { background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; font-size: 0.85em; }
.formula { font-family: "Times New Roman", Times, serif; font-size: 1.05rem; overflow-x: auto; }
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; margin: 0 4px; font-size: 0.9em; line-height: 1.25; }
.frac > span:first-child { border-bottom: 1px solid currentColor; padding: 0 4px; }

/* ------------------------------------------------------------------ *
 * Tablet: one column, stage first                                     *
 * ------------------------------------------------------------------ */
@media (max-width: 900px) {
  /* Reading order on a narrow screen: the network, what came out of it,
     and only then the controls that change it. */
  .main { display: flex; flex-direction: column; align-items: stretch; }
  .main > .stage-col { order: 1; }
  .main > .results { order: 2; }
  .main > .controls { order: 3; }
  .row2 { grid-template-columns: 1fr; }
  .title-card h1 { font-size: 1.4rem; }
}

@media (max-width: 700px) {
  .ops-grid { grid-template-columns: 1fr; gap: 10px; }
}

/* ------------------------------------------------------------------ *
 * Phones                                                              *
 * ------------------------------------------------------------------ */
@media (max-width: 700px) {
  .app { padding: 12px 12px 78px; gap: 12px; }
  .card { border-radius: 11px; }

  .title-card { padding: 16px 14px 14px; }
  .title-card h1 { font-size: 1.22rem; }
  .subtitle { font-size: 0.81rem; margin-top: 5px; }
  .title-links { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
  .pill { justify-content: center; padding: 7px 10px; font-size: 0.82rem; }

  /* Two full-width primary actions, the replay controls paired between them. */
  .action-bar { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 10px; }
  .act { padding: 11px 10px; }
  .act kbd { display: none; }
  #btn-run, #btn-compare { grid-column: 1 / -1; }

  .stage { padding: 5px; }
  .stage-help { top: 9px; right: 9px; width: 30px; height: 30px; }
  .step-caption { font-size: 0.88rem; margin: 7px 2px 0; }
  .legend { font-size: 0.75rem; gap: 2px 12px; }
  .sw { width: 18px; }

  /* The stage keeps its 640x440 coordinate system, so labels have to grow
     with it to stay legible once the whole graph is 350px wide. */
  .edge .edge-label { font-size: 17px; }
  .vedge .vedge-label { font-size: 18px; }
  .node text { font-size: 18px; }
  .node text.small { font-size: 13px; }
  .node .node-role { font-size: 14px; }
  .node circle { stroke-width: 3; }

  .controls, .results, .ops, .region-card, #compare { padding: 12px 13px; }
  .help-dot { width: 22px; height: 22px; font-size: 0.75rem; }
  .tools button { padding: 7px 11px; }
  .stat.big b { font-size: 1.85rem; }

  .region-plot { width: 300px; }
  .ops-diagram { max-width: 270px; }

  #compare-table { font-size: 0.84rem; }
  #compare-table th, #compare-table td { padding: 7px 6px; }
  .bar { display: none; }
  /* "vs. SP" and "Edges" are the first to go when there is no room. */
  #compare-table th:nth-child(3), #compare-table td:nth-child(3),
  #compare-table th:nth-child(4), #compare-table td:nth-child(4) { display: none; }

  .credits { padding: 12px 13px; font-size: 0.85rem; }

  .fab { right: 14px; bottom: calc(14px + env(safe-area-inset-bottom, 0px)); width: 44px; height: 44px; font-size: 1.2rem; }

  /* the help reads as a sheet rising from the bottom of the screen */
  .modal { padding: 0; align-items: flex-end; }
  .modal-box { width: 100%; max-height: 92vh; border-radius: 14px 14px 0 0; }
  .modal-head { padding: 6px 6px 0; align-items: flex-start; }
  .modal-tabs { gap: 0; }
  .modal-tabs button { padding: 6px 7px; font-size: 0.76rem; }
  .close { padding: 2px 4px; }
  .modal-body { padding: 14px 16px calc(24px + env(safe-area-inset-bottom, 0px)); }
  .formula { font-size: 0.98rem; }
}

@media (max-width: 700px) and (min-height: 500px) {
  .modal-box { max-height: 92dvh; }
}

@media (max-width: 380px) {
  .action-bar { grid-template-columns: 1fr; }
  .title-card h1 { font-size: 1.12rem; }
}
