:root {
  --bg: #0f1420; --panel: #161d2c; --panel2: #1c2537; --line: #2a3650; --text: #e6ebf5; --muted: #8e9bb5;
  --accent: #e0a030; --primary: #3d7dff; --primary-text: #fff; --good: #3fb97a; --warn: #e0a030; --bad: #ef5d5d;
  --node: #1b2436; --grid: #1a2233; --shadow: 0 6px 22px rgba(0, 0, 0, .35); --input: #0f1522;
}
:root[data-theme="light"] {
  --bg: #eef1f6; --panel: #ffffff; --panel2: #f4f6fa; --line: #d5dbe6; --text: #1b2333; --muted: #5d6a82;
  --node: #ffffff; --grid: #dfe4ec; --shadow: 0 4px 16px rgba(30, 40, 70, .14); --input: #ffffff;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); display: flex; flex-direction: column; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0 0 .6em; }
h3 { font-size: 14px; margin-top: 1em; }
code { background: var(--panel2); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; font-size: 12.5px; }
[hidden] { display: none !important; }

/* ---- buttons */
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: var(--primary); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.small { padding: 3px 9px; font-size: 12.5px; }
.btn.icon { padding: 6px 10px; }
.btn.block { display: block; width: 100%; margin-top: 8px; }
.btn.danger:hover { border-color: var(--bad); color: var(--bad); }
.btn.good { color: var(--good); } .btn.meh { color: var(--warn); border-color: var(--warn); } .btn.bad { color: #fff; background: var(--bad); border-color: var(--bad); font-weight: 600; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.link { background: none; border: 0; padding: 0; color: var(--primary); text-decoration: underline; font-size: 12.5px; }
.icon-btn { background: none; border: 0; color: var(--muted); padding: 2px 6px; border-radius: 6px; line-height: 1; }
.icon-btn:hover:not(:disabled) { color: var(--text); background: var(--line); }
.icon-btn:disabled { opacity: .3; cursor: default; }
.chip, .chip-btn { display: inline-block; font-size: 11.5px; padding: 1px 8px; border-radius: 99px; background: var(--panel2); border: 1px solid var(--line); color: var(--muted); }
.chip-btn:hover { color: var(--text); border-color: var(--primary); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.err { background: var(--bad); } .dot.warn { background: var(--warn); }
.ico { flex: none; } .ico.big { font-size: 28px; }

/* ---- layout */
#topbar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); flex: none; }
.brand { color: inherit; text-decoration: none; font-weight: 700; font-size: 15px; white-space: nowrap; } .logo { color: var(--accent); }
.bar-group { display: flex; gap: 6px; align-items: center; } .spacer { flex: 1; }
#main { flex: 1; display: flex; min-height: 0; }
#sidebar { width: 250px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 10px; gap: 8px; min-height: 0; }
#inspector { width: 400px; flex: none; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 12px 14px 60px; }
#stage { flex: 1; position: relative; overflow: hidden; min-width: 0; user-select: none; touch-action: none; cursor: grab;
  background-image: radial-gradient(var(--grid) 1.2px, transparent 1.2px); background-size: 24px 24px; }
#stage.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#wires { position: absolute; left: 0; top: 0; overflow: visible; }

/* ---- sidebar */
.tabs { display: flex; background: var(--panel2); border-radius: 8px; padding: 3px; }
.tabs button { flex: 1; background: none; border: 0; padding: 5px 4px; border-radius: 6px; color: var(--muted); font-size: 13px; }
.tabs button.on { background: var(--primary); color: #fff; font-weight: 600; }
#side-search, input[type=text], input[type=number], input[type=search], select, textarea { width: 100%; background: var(--input); border: 1px solid var(--line); border-radius: 7px; padding: 6px 8px; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
textarea { resize: vertical; min-height: 62px; }
#side-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; min-height: 0; }
.row { display: flex; align-items: center; gap: 6px; background: none; border: 0; border-radius: 6px; padding: 5px 6px; text-align: left; width: 100%; }
.row:hover { background: var(--panel2); } .row.on { background: var(--primary); color: #fff; } .row.off { opacity: .5; }
.row-id { font-size: 11.5px; color: var(--muted); flex: none; min-width: 34px; } .row.on .row-id, .row.on .row-meta { color: #dfe8ff; }
.row-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-meta { font-size: 11.5px; color: var(--muted); flex: none; }
.empty-note { color: var(--muted); padding: 14px 6px; font-size: 13px; }

/* ---- nodes */
.node { position: absolute; width: 250px; background: var(--node); border: 1px solid var(--line); border-top: 4px solid var(--accent); border-radius: 10px; box-shadow: var(--shadow); cursor: pointer; }
.node:hover { border-color: var(--muted); border-top-color: var(--accent); }
.node.sel { outline: 2px solid var(--primary); outline-offset: 2px; }
.node.off { opacity: .55; border-top-color: var(--muted); }
.node-head { display: flex; align-items: center; gap: 6px; padding: 8px 12px 2px; }
.node-id { font-size: 11px; color: var(--muted); flex: none; }
.node-title { font-weight: 650; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-flag { flex: none; font-size: 11px; font-weight: 700; min-width: 18px; text-align: center; border-radius: 99px; padding: 0 5px; color: #fff; }
.node-flag.err { background: var(--bad); } .node-flag.warn { background: var(--warn); color: #201400; }
.node-who { font-size: 12px; color: var(--muted); padding: 0 12px 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-steps { border-top: 1px solid var(--line); padding: 6px 12px; }
.node-step { display: flex; gap: 6px; font-size: 12.5px; height: 21px; align-items: center; }
.node-step .txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-step.more { color: var(--muted); font-style: italic; } .node-step.missing { color: var(--bad); }
.node-foot { padding: 0 12px 8px; display: flex; gap: 4px; flex-wrap: wrap; }
.port { position: absolute; top: 20px; width: 14px; height: 14px; border-radius: 50%; background: var(--panel); border: 2px solid var(--muted); }
.port.in { left: -8px; } .port.out { right: -8px; cursor: crosshair; border-color: var(--primary); background: var(--primary); }
.port.out:hover { transform: scale(1.45); }
#stage.linking .node:hover { outline: 2px dashed var(--primary); outline-offset: 2px; }

.wire { fill: none; stroke: var(--muted); stroke-width: 2.2; }
.wire.auto { stroke: var(--good); } .wire.soft { stroke-dasharray: 7 6; }
.wire.sel { stroke: var(--primary); stroke-width: 3.5; } .wire.temp { stroke: var(--primary); stroke-dasharray: 5 5; pointer-events: none; }
.wire-arrow { fill: var(--muted); } .wire-arrow.sel { fill: var(--primary); }
.wire-hit { fill: none; stroke: transparent; stroke-width: 16; cursor: pointer; pointer-events: stroke; }
.wire-label { font-size: 10.5px; fill: var(--good); paint-order: stroke; stroke: var(--bg); stroke-width: 4px; pointer-events: none; }

#zoomctl { position: absolute; right: 12px; bottom: 12px; display: flex; align-items: center; gap: 2px; background: var(--panel); border: 1px solid var(--line); border-radius: 9px; padding: 3px; cursor: default; }
#zoomctl button { background: none; border: 0; padding: 3px 9px; border-radius: 6px; } #zoomctl button:hover { background: var(--panel2); }
#zoom-label { font-size: 12px; color: var(--muted); min-width: 40px; text-align: center; }
#legend { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 14px; font-size: 11.5px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 9px; padding: 5px 10px; cursor: default; flex-wrap: wrap; max-width: calc(100% - 220px); }
.lg { display: inline-block; width: 22px; height: 0; border-top: 2px solid var(--muted); vertical-align: middle; margin-right: 3px; } .lg.auto { border-top-color: var(--good); } .lg.dash { border-top: 2px dashed var(--good); }

#welcome { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; cursor: default; background: var(--bg); overflow: auto; }
#welcome h1 { font-size: 24px; max-width: 640px; } #welcome p { color: var(--muted); max-width: 560px; }
.welcome-cards { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; margin-top: 18px; }
.welcome-cards button, .type-card { display: flex; flex-direction: column; gap: 5px; align-items: flex-start; text-align: left; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; }
.welcome-cards button { width: 220px; }
.welcome-cards button:hover, .type-card:hover { border-color: var(--primary); transform: translateY(-2px); }
.welcome-cards span:last-child, .type-card span { color: var(--muted); font-size: 12.5px; }

/* ---- inspector */
.insp-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.kind { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); font-weight: 700; }
.head-actions { display: flex; gap: 5px; }
.fold > summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 8px; }
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary::before { content: "▸"; color: var(--muted); font-size: 11px; width: 10px; flex: none; }
.fold[open] > summary::before { content: "▾"; }
.section { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; background: var(--panel2); }
.section > summary { padding: 9px 12px; } .sec-title { font-weight: 650; } .sec-sub { color: var(--muted); font-size: 12px; margin-left: auto; }
.sec-body { padding: 4px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.adv { margin-top: 10px; } .adv > summary { color: var(--muted); font-size: 12.5px; padding: 2px 0 6px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 10px; }
.f { min-width: 0; } .f.wide { grid-column: 1 / -1; }
.f-label { font-size: 12.5px; font-weight: 600; margin-bottom: 3px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 3px; } .hint.inline { margin-left: 8px; }
.note { font-size: 12.5px; background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--primary); border-radius: 6px; padding: 7px 9px; margin-bottom: 8px; }
.note.bad { border-left-color: var(--bad); } .note.good { border-left-color: var(--good); }
.switch { display: flex; align-items: center; gap: 9px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.track { flex: none; width: 34px; height: 19px; border-radius: 99px; background: var(--line); position: relative; transition: background .15s; }
.track::after { content: ""; position: absolute; left: 2px; top: 2px; width: 15px; height: 15px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + .track { background: var(--primary); } .switch input:checked + .track::after { transform: translateX(15px); }
.switch input:focus-visible + .track { outline: 2px solid var(--primary); outline-offset: 2px; }
.switch-text { font-size: 13px; }
.vec { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; flex: 1; }
.vec label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); }
.vec-line { display: flex; align-items: flex-end; gap: 6px; margin-bottom: 6px; } .vec-n { color: var(--muted); font-size: 12px; width: 16px; padding-bottom: 7px; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; background: var(--input); border: 1px solid var(--line); border-radius: 7px; padding: 4px; max-height: 190px; overflow-y: auto; }
.tags input { border: 0; background: none; flex: 1; min-width: 150px; padding: 3px 4px; outline: none; }
.tag { display: inline-flex; align-items: center; gap: 2px; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 1px 2px 1px 7px; font-size: 12px; }
.tag button { background: none; border: 0; color: var(--muted); padding: 0 4px; } .tag button:hover { color: var(--bad); }
.presets { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 5px; align-items: center; }
.list { display: flex; flex-direction: column; gap: 7px; align-items: flex-start; } .list > * { width: 100%; } .list > .btn { width: auto; }
.list-item { position: relative; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 9px 30px 9px 9px; }
.list-item > .icon-btn { position: absolute; right: 4px; top: 6px; }
details.list-item { padding: 0; } details.list-item > summary { padding: 7px 9px; } details.list-item > .list-body { padding: 4px 9px 9px; }
.sum-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.group { border-left: 3px solid var(--line); padding-left: 10px; }
.map-row { display: grid; grid-template-columns: 1fr 90px auto; gap: 6px; }
.color-row { display: flex; align-items: center; gap: 8px; } .color-row input { width: 44px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; }
.picker { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; } .picker.col { flex-direction: column; align-items: flex-start; }
.ref { display: inline-flex; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.ref.bad { border-color: var(--bad); }
.ref button { background: none; border: 0; padding: 3px 7px; } .ref button:hover { background: var(--line); }
.ref-name { color: var(--primary); font-weight: 600; background: none; border: 0; text-align: left; }
.ref-add { width: auto; max-width: 100%; color: var(--muted); }
.link-pair { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; padding: 9px; background: var(--panel2); border-radius: 8px; }
.steps { display: flex; flex-direction: column; gap: 6px; }
.step { border: 1px solid var(--line); border-radius: 9px; background: var(--panel); }
.step.open { border-color: var(--primary); } .step.missing { border-color: var(--bad); }
.step-head { display: flex; align-items: center; gap: 7px; padding: 7px 6px 7px 9px; cursor: pointer; }
.step-n { width: 18px; height: 18px; border-radius: 50%; background: var(--line); font-size: 11px; display: grid; place-items: center; flex: none; }
.step-text { flex: 1; min-width: 0; display: flex; flex-direction: column; } .step-text b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; } .step-text small { color: var(--muted); }
.step-body { padding: 4px 10px 10px; border-top: 1px solid var(--line); padding-top: 10px; }
.issues { margin-bottom: 10px; display: flex; flex-direction: column; gap: 4px; }
.issue-line { display: flex; gap: 7px; align-items: baseline; font-size: 12.5px; padding: 6px 8px; border-radius: 6px; background: var(--panel2); border: 1px solid var(--line); }
.stats { display: flex; gap: 8px; margin: 12px 0; } .stats div { flex: 1; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; text-align: center; color: var(--muted); font-size: 12px; } .stats b { display: block; font-size: 20px; color: var(--text); }
.howto ol, .steps-list { padding-left: 20px; margin: 6px 0; } .howto li, .steps-list li { margin-bottom: 7px; }

/* ---- modal, toast, drop */
.modal-back { position: fixed; inset: 0; background: rgba(5, 8, 15, .6); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; width: min(520px, 100%); max-height: 90vh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal.wide { width: min(780px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px 0; } .modal-head h2 { font-size: 18px; margin: 0; }
.modal-body { padding: 12px 18px 18px; overflow-y: auto; }
.type-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.reuse { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 12px; }
.json { background: var(--input); border: 1px solid var(--line); border-radius: 8px; padding: 10px; max-height: 50vh; overflow: auto; font-size: 12px; user-select: text; }
.issue { display: flex; gap: 8px; align-items: baseline; width: 100%; text-align: left; background: var(--panel2); border: 1px solid var(--line); border-radius: 7px; padding: 7px 10px; margin-bottom: 5px; }
.issue:hover:not(:disabled) { border-color: var(--primary); } .issue:disabled { cursor: default; }
#toasts { position: fixed; bottom: 54px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 60; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); border-radius: 9px; padding: 8px 14px; box-shadow: var(--shadow); display: flex; gap: 12px; max-width: 80vw; pointer-events: auto; font-size: 13px; }
.toast .link { font-size: 13px; font-weight: 700; }
#drop-hint { display: none; position: fixed; inset: 10px; border: 3px dashed var(--primary); border-radius: 16px; background: rgba(61, 125, 255, .15); z-index: 70; place-items: center; font-size: 22px; font-weight: 700; pointer-events: none; }
body.dropping #drop-hint { display: grid; }

@media (max-width: 1100px) { #sidebar { width: 200px; } #inspector { width: 340px; } #btn-new, #legend { display: none; } }
@media (max-width: 760px) { #sidebar { display: none; } #inspector { position: fixed; right: 0; left: 0; bottom: 0; width: auto; height: 46vh; border-left: 0; border-top: 1px solid var(--line); z-index: 5; } .brand { display: none; } }
