:root {
  --bg: #f4f3ef;
  --panel: #ffffff;
  --panel-2: #f8f7f4;
  --text: #1f2328;
  --muted: #6b6f76;
  --border: #dcdad3;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --code-bg: #fbfbf9;
  --line-hl: #fff2a8;
  --line-hl-border: #e0b400;
  --line-err: #ffd9d6;
  --bp: #d23b3b;
  --canvas: #fcfcfa;
  --canvas-grid: #efeee9;
  --kw: #8b2bb9;
  --str: #0f7b3b;
  --com: #8a8f98;
  --num: #b5530c;
  --fn: #1f5fbf;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171a; --panel: #1d2024; --panel-2: #23272c; --text: #e6e7e9; --muted: #9aa0a8;
    --border: #33383f; --accent: #6b8cff; --accent-text: #0d1020; --code-bg: #191c20;
    --line-hl: #4a4113; --line-hl-border: #c9a400; --line-err: #5a2420; --bp: #ff6b6b;
    --kw: #d39bff; --str: #7fd49a; --com: #7c838d; --num: #f0a868; --fn: #8cb4ff;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171a; --panel: #1d2024; --panel-2: #23272c; --text: #e6e7e9; --muted: #9aa0a8;
  --border: #33383f; --accent: #6b8cff; --accent-text: #0d1020; --code-bg: #191c20;
  --line-hl: #4a4113; --line-hl-border: #c9a400; --line-err: #5a2420; --bp: #ff6b6b;
  --kw: #d39bff; --str: #7fd49a; --com: #7c838d; --num: #f0a868; --fn: #8cb4ff;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
.mono, .code, .code-edit { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

button, select, input[type=number], input[type=text] {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 10px;
}
button { cursor: pointer; white-space: nowrap; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.small { padding: 2px 8px; font-size: 12px; }
button.link { border: none; background: none; color: var(--accent); padding: 0 4px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: .2px; display: flex; align-items: baseline; gap: 6px; }
.brand .logo { color: var(--accent); }
.brand .sub { font-weight: 400; font-size: 12px; color: var(--muted); }
.field { display: flex; align-items: center; gap: 6px; }
.field label { color: var(--muted); font-size: 12px; }
.btn-group { display: flex; gap: 4px; }
.spacer { flex: 1; }
.status { font-size: 12px; color: var(--muted); max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.ok::before { content: "● "; color: #2da44e; }
.status.load::before { content: "● "; color: #d4a72c; }
.status.err::before { content: "● "; color: var(--bp); }

.layout {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: minmax(340px, 40%) 1fr; gap: 12px; padding: 12px 16px 16px;
}
.pane {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}

/* --- code pane --- */
.tabs { display: flex; align-items: center; gap: 2px; padding: 6px 8px 0; border-bottom: 1px solid var(--border); }
.tab { border: none; background: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 6px 10px; color: var(--muted); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tabs .small { margin-bottom: 4px; margin-left: 4px; }
.code-wrap { flex: 1; min-height: 0; position: relative; background: var(--code-bg); }
.code { position: absolute; inset: 0; overflow: auto; padding: 6px 0; font-size: 13px; line-height: 1.6; }
.code .ln { display: flex; white-space: pre; border-left: 3px solid transparent; }
.code .ln.cur { background: var(--line-hl); border-left-color: var(--line-hl-border); }
.code .ln.err { background: var(--line-err); border-left-color: var(--bp); }
.code .gut {
  flex: 0 0 52px; text-align: right; padding-right: 12px; color: var(--muted); cursor: pointer;
  user-select: none; position: relative;
}
.code .gut:hover::before { content: ""; position: absolute; left: 8px; top: 50%; width: 9px; height: 9px; margin-top: -5px; border-radius: 50%; background: var(--bp); opacity: .3; }
.code .ln.bp .gut::before { content: ""; position: absolute; left: 8px; top: 50%; width: 9px; height: 9px; margin-top: -5px; border-radius: 50%; background: var(--bp); opacity: 1; }
.code .src { padding-right: 16px; }
.code .kw { color: var(--kw); font-weight: 600; }
.code .str { color: var(--str); }
.code .com { color: var(--com); font-style: italic; }
.code .num { color: var(--num); }
.code .fn { color: var(--fn); }
.code-edit {
  position: absolute; inset: 0; width: 100%; height: 100%; resize: none; border: none; outline: none;
  padding: 6px 12px; font-size: 13px; line-height: 1.6; background: var(--code-bg); color: var(--text); tab-size: 4;
}
.about { position: absolute; inset: 0; overflow: auto; padding: 14px 18px; }
.about h3 { margin: 0 0 8px; }
.about p { margin: 0 0 10px; color: var(--text); }
.about .meta { color: var(--muted); font-size: 12px; }

.controls { border-top: 1px solid var(--border); padding: 8px 10px; display: flex; flex-direction: column; gap: 8px; background: var(--panel-2); }
.btn-row { display: flex; gap: 4px; flex-wrap: wrap; }
.timeline, .speed { display: flex; align-items: center; gap: 8px; }
.timeline input { flex: 1; }
.speed label { color: var(--muted); font-size: 12px; }
.speed input[type=range] { width: 120px; }
.check { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); cursor: pointer; }
input.seed { width: 64px; padding: 1px 4px; font-size: 12px; }
#stepLabel { font-size: 12px; color: var(--muted); min-width: 120px; text-align: right; }

/* --- graph pane --- */
.graph-toolbar { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.seg { display: flex; }
.seg .tool { border-radius: 0; margin-left: -1px; padding: 3px 9px; font-size: 12px; }
.seg .tool:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg .tool:last-child { border-radius: 0 6px 6px 0; }
.seg .tool.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); position: relative; }
.canvas-wrap { flex: 1; min-height: 240px; position: relative; background: var(--canvas); }
#graph { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#graph.tool-vertex { cursor: crosshair; }
#graph .v { cursor: grab; }
#graph.tool-delete .v, #graph.tool-delete .e { cursor: not-allowed; }
#graph .e .hit { stroke: transparent; stroke-width: 14; fill: none; }
#graph.tool-delete .e:hover .vis, #graph.tool-weight .e:hover .vis { stroke-dasharray: 6 4; }
#graph .vlabel { font: 600 12px system-ui, sans-serif; pointer-events: none; text-anchor: middle; dominant-baseline: central; }
#graph .annot { font: 600 12px system-ui, sans-serif; fill: #1f2328; paint-order: stroke; stroke: #fcfcfa; stroke-width: 4px; stroke-linejoin: round; pointer-events: none; }
#graph .wlabel { font: 11px system-ui, sans-serif; fill: #6b6f76; paint-order: stroke; stroke: #fcfcfa; stroke-width: 4px; pointer-events: none; text-anchor: middle; dominant-baseline: central; }
#graph .eannot { text-anchor: middle; dominant-baseline: central; fill: #8a1c1c; }
#graph .pickable circle.body { stroke: #2f5bd3; stroke-width: 3; stroke-dasharray: 4 3; }
#graph .pickable { cursor: pointer; }
#graph .sel circle.body { stroke: #d4a72c; stroke-width: 4; }
#graph .blink { animation: blink .35s ease-in-out 3; }
@keyframes blink { 50% { opacity: .15; } }

.pick-banner {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: var(--accent-text); padding: 6px 8px 6px 14px; border-radius: 20px;
  display: flex; gap: 8px; align-items: center; box-shadow: 0 4px 16px rgba(0,0,0,.18); font-weight: 600; max-width: calc(100% - 24px);
}
.pick-banner button { background: rgba(255,255,255,.2); color: inherit; border-color: transparent; }
.weight-input { position: absolute; width: 70px; padding: 2px 6px; font-size: 12px; z-index: 5; }
.busy { position: absolute; inset: 0; display: flex; gap: 10px; align-items: center; justify-content: center; background: color-mix(in srgb, var(--canvas) 70%, transparent); color: #333; font-weight: 600; }
.spinner { width: 18px; height: 18px; border: 3px solid #c9c9c9; border-top-color: #2f5bd3; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 6px 10px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); min-height: 30px; }
.legend .item { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw-v { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(0,0,0,.2); }
.legend .sw-e { width: 18px; height: 4px; border-radius: 2px; }
.log-wrap { border-top: 1px solid var(--border); display: flex; flex-direction: column; height: 130px; }
.log-head { font-size: 12px; color: var(--muted); padding: 4px 10px; display: flex; justify-content: space-between; }
.log { flex: 1; overflow: auto; padding: 0 10px 6px; font-size: 12px; white-space: pre-wrap; }
.log .err { color: var(--bp); }
.log .sys { color: var(--muted); }

dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text); padding: 0; box-shadow: 0 12px 40px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.3); }
.dlg { display: flex; flex-direction: column; gap: 10px; padding: 18px 20px; min-width: 300px; }
.dlg h2 { margin: 0; font-size: 17px; }
.dlg .hint { margin: 0; color: var(--muted); font-size: 12px; }
.dlg label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.dlg label.check { flex-direction: row; }
.dlg .row { display: flex; gap: 10px; }
.dlg .row label { flex: 1; }
.dlg input[type=number] { width: 100%; }
.dlg menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 6px 0 0; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 46vh 80vh; padding: 8px 16px 16px; }
  .topbar .sub { display: none; }
  .canvas-wrap { min-height: 320px; }
}
