:root {
  --bg: #f5f6f8;
  --canvas-dot: #dde1e8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #1c2230;
  --muted: #687185;
  --line: #d8dce4;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --accent-soft: rgba(47, 111, 222, 0.14);
  --edge: #8a93a6;
  --root-bg: #ffffff;
  --danger: #c62f3c;
  --warn-bg: #fff4d6;
  --warn-ink: #6b4a00;
  --shadow: 0 8px 28px rgba(20, 30, 50, 0.14), 0 1px 3px rgba(20, 30, 50, 0.08);
  --topbar-h: 52px;
  --toolbar-h: 62px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --canvas-dot: #262a33;
    --panel: #1d2028;
    --panel-2: #262a34;
    --text: #e5e8ee;
    --muted: #9aa2b4;
    --line: #353b47;
    --accent: #6f9dff;
    --accent-ink: #0c1530;
    --accent-soft: rgba(111, 157, 255, 0.2);
    --edge: #6d7588;
    --root-bg: #1d2028;
    --danger: #ff6b76;
    --warn-bg: #3a3014;
    --warn-ink: #ffdc8a;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg.i { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------------------------------------------------------------- login */
#login {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 16px;
}
#login form {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 14px;
}
#login h1 { margin: 0; font-size: 22px; display: flex; align-items: center; gap: 10px; }
#login h1 img { width: 32px; height: 32px; }
#login p { margin: 0; color: var(--muted); font-size: 14px; }
.field { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); outline: none; }
.field:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 8px 14px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.err { color: var(--danger); font-size: 14px; min-height: 1em; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: fixed; left: 0; right: 0; top: 0; z-index: 20;
  height: calc(var(--topbar-h) + var(--safe-top)); padding: var(--safe-top) 8px 0;
  display: flex; align-items: center; gap: 4px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}
.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center; border: 0; border-radius: 10px;
  background: transparent; cursor: pointer; color: var(--text); flex: none;
}
.icon-btn:hover { background: var(--panel-2); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
.icon-btn.on { background: var(--accent-soft); color: var(--accent); }
.title-wrap { min-width: 0; display: flex; align-items: baseline; gap: 10px; padding: 0 4px; flex: 1 1 auto; }
.map-title {
  min-width: 0; max-width: 100%; border: 0; background: transparent; padding: 4px 6px; border-radius: 8px;
  font-weight: 650; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer;
}
.map-title:hover { background: var(--panel-2); }
.save-state { color: var(--muted); font-size: 12.5px; white-space: nowrap; flex: none; }
.map-ref {
  flex: none; font: 600 11.5px/1 ui-monospace, "Cascadia Mono", Consolas, monospace; color: var(--muted);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 3px 6px; white-space: nowrap;
}

/* Node numbers: hover tooltip, optional badges, details panel */
.ref-tip {
  position: fixed; z-index: 55; transform: translate(-50%, calc(-100% - 8px));
  display: flex; align-items: baseline; gap: 8px; padding: 6px 10px; border-radius: 9px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow); white-space: nowrap; cursor: pointer;
}
.ref-tip.below { transform: translate(-50%, 8px); }
.ref-tip b { font: 650 15px ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: 0.02em; }
.ref-tip span { font-size: 12px; opacity: 0.72; }
/* Link tooltip: what the link is, then where it goes */
.ref-tip.link-tip { flex-direction: column; align-items: flex-start; gap: 1px; max-width: min(380px, calc(100vw - 16px)); white-space: normal; }
.ref-tip.link-tip b { font-family: inherit; font-size: 13.5px; font-weight: 600; line-height: 1.35; letter-spacing: 0; overflow-wrap: anywhere; }
.ref-tip.link-tip span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.n-ref {
  display: inline-block; margin-right: 5px; padding: 0 4px; border-radius: 5px; vertical-align: 1px;
  font: 600 10.5px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; font-style: normal;
  color: var(--muted); background: var(--panel-2); border: 1px solid var(--line);
}
.root .n-ref { vertical-align: 3px; }
.insp-ref {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 12px; border-radius: 12px; background: var(--accent-soft);
}
.insp-ref .lbl { margin-bottom: 2px; }
.insp-ref b { font: 650 20px ui-monospace, "Cascadia Mono", Consolas, monospace; color: var(--accent); }
.save-state.bad { color: var(--danger); }
.top-actions { display: flex; align-items: center; gap: 2px; flex: none; }

#searchbar {
  position: fixed; z-index: 19; top: calc(var(--topbar-h) + var(--safe-top) + 8px); right: 8px;
  width: min(420px, calc(100% - 16px)); display: flex; gap: 4px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px; box-shadow: var(--shadow);
}
#searchbar input { flex: 1; min-width: 0; border: 0; outline: none; background: transparent; padding: 6px 8px; font-size: 16px; }
#search-count { color: var(--muted); font-size: 13px; padding: 0 4px; white-space: nowrap; }

#banner {
  position: fixed; z-index: 18; left: 50%; transform: translateX(-50%);
  top: calc(var(--topbar-h) + var(--safe-top) + 8px); width: min(560px, calc(100% - 16px));
  background: var(--warn-bg); color: var(--warn-ink); border-radius: 12px; padding: 10px 12px; box-shadow: var(--shadow);
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 14px;
}
#banner span { flex: 1 1 220px; }

/* ---------------------------------------------------------------- canvas */
#canvas {
  position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: grab;
  background-color: var(--bg);
  background-image: radial-gradient(var(--canvas-dot) 1px, transparent 1px);
  background-size: 22px 22px;
}
#canvas.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#edges { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#edges path { fill: none; stroke-linecap: round; }
/* Arrowheads take the colour of the line they end (the rule above must not empty them). */
#edges marker path { fill: var(--edge); fill: context-stroke; stroke: none; }
#edges path.arrow { stroke-dasharray: 5 4; stroke-width: 1.4; }

.node {
  position: absolute; left: 0; top: 0; max-width: 300px; width: max-content;
  padding: 3px 7px 4px; border-radius: 7px; font-size: 14px; line-height: 1.32;
  white-space: pre-wrap; overflow-wrap: anywhere; user-select: none; -webkit-user-select: none;
  cursor: default; color: var(--text); border: 1.5px solid transparent;
}
.node.l1 { font-size: 15.5px; }
.node.root {
  font-size: 19px; font-weight: 650; padding: 10px 18px; border-radius: 16px; max-width: 360px;
  background: var(--root-bg); border: 2px solid var(--accent); box-shadow: var(--shadow); text-align: center;
}
.node.bubble { border-color: var(--edge-c, var(--edge)); background: var(--panel); border-radius: 11px; padding: 4px 10px 5px; }
.node.bold { font-weight: 650; }
.node.italic { font-style: italic; }
.node.empty .n-text::before { content: "\00a0\00a0\00a0\00a0"; }
.node.sel { background: var(--accent-soft); box-shadow: 0 0 0 2px var(--accent); }
.node.root.sel { box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow); }
.node.match { box-shadow: 0 0 0 2px #f5b400; }
.node.match.sel { box-shadow: 0 0 0 2px var(--accent), 0 0 0 5px rgba(245, 180, 0, 0.5); }
.node.dragging { opacity: 0.35; }
.node.drop-child { box-shadow: 0 0 0 2px var(--accent); background: var(--accent-soft); }
.node.drop-before { box-shadow: inset 0 3px 0 var(--accent); }
.node.drop-after { box-shadow: inset 0 -3px 0 var(--accent); }
.n-icons { margin-right: 3px; letter-spacing: 1px; }
.n-icon-name { font-size: 10px; padding: 0 3px; border-radius: 4px; background: var(--panel-2); color: var(--muted); margin-right: 3px; vertical-align: 2px; }
.n-badges { margin-left: 4px; color: var(--muted); font-size: 0.85em; }
.n-link { cursor: pointer; }
.n-link + .n-link { margin-left: 2px; }
.n-link:hover { color: var(--accent); }
.n-more {
  display: inline-block; margin-left: 3px; padding: 0 5px; border-radius: 8px; cursor: pointer; vertical-align: 1px;
  font: 600 10.5px/1.5 ui-monospace, "Cascadia Mono", Consolas, monospace; background: var(--panel-2); border: 1px solid var(--line);
}
.n-more:hover { color: var(--accent); border-color: var(--accent); }
.n-hid {
  display: inline-flex; align-items: center; gap: 2px; margin-left: 4px; padding: 0 5px 0 3px; border-radius: 8px; cursor: pointer;
  font: 600 10.5px/1.5 ui-monospace, "Cascadia Mono", Consolas, monospace; font-style: normal; vertical-align: 1px;
  color: var(--muted); background: var(--panel-2); border: 1px dashed var(--line);
}
.n-hid svg { width: 1.15em; height: 1.15em; }
.n-hid:hover { color: var(--accent); border-color: var(--accent); }
.n-fold {
  position: absolute; bottom: -9px; width: 18px; height: 18px; border-radius: 50%;
  border: 1.5px solid var(--edge-c, var(--edge)); background: var(--bg); color: var(--muted);
  font-size: 9.5px; line-height: 15px; text-align: center; font-weight: 600; cursor: pointer; font-style: normal;
}
.side-r .n-fold { right: -16px; }
.side-l .n-fold { left: -16px; }
.root .n-fold, .bubble .n-fold { bottom: calc(50% - 9px); }

.drag-ghost {
  position: fixed; z-index: 40; pointer-events: none; padding: 5px 10px; border-radius: 8px; max-width: 220px;
  background: var(--accent); color: var(--accent-ink); font-size: 13px; box-shadow: var(--shadow);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transform: translate(12px, -50%);
}

.editor {
  position: fixed; z-index: 45; margin: 0; resize: none; overflow: hidden;
  border: 2px solid var(--accent); border-radius: 8px; padding: 3px 7px; line-height: 1.32;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow); outline: none;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

#empty {
  position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none; padding: 16px;
}
#empty > div {
  pointer-events: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px;
  text-align: center; max-width: 380px; box-shadow: var(--shadow); display: grid; gap: 12px;
}
#empty h2 { margin: 0; font-size: 19px; }
#empty p { margin: 0; color: var(--muted); }
#empty .row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------------------------------------------------------------- toolbar */
#toolbar {
  position: fixed; z-index: 20; left: 50%; transform: translateX(-50%);
  bottom: calc(14px + var(--safe-bottom)); display: flex; gap: 2px; padding: 5px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
}
#toolbar button {
  min-width: 64px; height: 50px; border: 0; border-radius: 11px; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11.5px; color: var(--text);
}
#toolbar button:hover { background: var(--panel-2); }
#toolbar button:disabled { opacity: 0.35; cursor: default; background: transparent; }
#toolbar button.on { color: var(--accent); background: var(--accent-soft); }
#toolbar .sep { width: 1px; background: var(--line); margin: 6px 3px; }
@media (min-width: 641px) { body.insp-open #toolbar { left: calc(50% - 170px); } }

@media (max-width: 640px) {
  #toolbar {
    left: 0; right: 0; bottom: 0; transform: none; border-radius: 0; border-width: 1px 0 0;
    padding: 4px 4px calc(4px + var(--safe-bottom)); justify-content: space-around; box-shadow: none;
  }
  #toolbar button { min-width: 0; flex: 1; height: 54px; font-size: 11px; }
  #toolbar .sep { display: none; }
  .hide-narrow { display: none !important; }
  .save-state { font-size: 11.5px; }
}

/* ---------------------------------------------------------------- panels */
.scrim { position: fixed; inset: 0; z-index: 29; background: rgba(10, 15, 25, 0.32); }
#drawer {
  position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(340px, 88vw);
  background: var(--panel); border-right: 1px solid var(--line); box-shadow: var(--shadow);
  display: flex; flex-direction: column; padding-top: var(--safe-top);
  transform: translateX(-105%); transition: transform 0.18s ease;
}
#drawer.open { transform: none; }
.panel-head { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 16px; border-bottom: 1px solid var(--line); }
.panel-head h2 { margin: 0; font-size: 16px; flex: 1; }
.drawer-tools { display: flex; gap: 8px; padding: 10px 12px; flex-wrap: wrap; }
.drawer-tools .field { flex: 1 1 100%; padding: 8px 10px; }
.drawer-tools .btn { flex: 1; }
#map-list { list-style: none; margin: 0; padding: 4px 8px 16px; overflow-y: auto; flex: 1; }
#map-list li {
  padding: 10px 10px; border-radius: 10px; cursor: pointer; display: flex; gap: 8px; align-items: center;
}
#map-list li:hover { background: var(--panel-2); }
#map-list li.current { background: var(--accent-soft); }
#map-list .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#map-list .m { font-size: 12.5px; color: var(--muted); }
#map-list .grow { flex: 1; min-width: 0; }
.list-note { color: var(--muted); font-size: 13px; padding: 8px 10px; }
.drawer-foot { border-top: 1px solid var(--line); padding: 8px 12px calc(8px + var(--safe-bottom)); display: flex; justify-content: space-between; }

#inspector {
  position: fixed; z-index: 25; right: 0; top: calc(var(--topbar-h) + var(--safe-top)); bottom: 0; width: 340px;
  background: var(--panel); border-left: 1px solid var(--line); box-shadow: var(--shadow);
  display: flex; flex-direction: column; transform: translateX(105%); transition: transform 0.18s ease;
}
#inspector.open { transform: none; }
.insp-body { overflow-y: auto; padding: 12px 14px 24px; display: grid; gap: 14px; align-content: start; flex: 1; }
.insp-body label, .lbl { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; display: block; margin-bottom: 5px; }
.insp-body textarea { width: 100%; min-height: 64px; resize: vertical; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); outline: none; }
.insp-body textarea#insp-note { min-height: 120px; }
.insp-body textarea:focus, .insp-body input:focus { border-color: var(--accent); }
.row-inline { display: flex; gap: 6px; align-items: center; }
.row-inline .field { padding: 8px 10px; }
.links-edit { display: grid; gap: 8px; }
.links-edit:not(:empty) + .btn { margin-top: 8px; }
.link-row {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto; grid-template-areas: "url open remove" "title title title";
  gap: 4px 4px; align-items: center; padding: 6px; border: 1px solid var(--line); border-radius: 12px;
}
.link-row .field { padding: 7px 10px; min-width: 0; }
.link-row [data-f=url] { grid-area: url; }
.link-row [data-f=title] { grid-area: title; font-size: 13px; }
.link-row [data-act=open] { grid-area: open; }
.link-row [data-act=remove] { grid-area: remove; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch {
  width: 28px; height: 28px; border-radius: 50%; border: 1.5px solid var(--line); cursor: pointer; padding: 0;
  display: grid; place-items: center; font-size: 13px; color: var(--muted); background: var(--panel);
}
.swatch.on { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--accent); }
.swatch input { opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 7px 12px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
/* Share links: the chip in the top bar, what a link can't do, the owner's Share dialog. */
.share-chip { flex: none; border: 1px solid var(--line); background: var(--warn-bg); color: var(--warn-ink); border-radius: 999px; padding: 3px 10px; font: 600 12.5px/1.3 inherit; cursor: pointer; white-space: nowrap; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; }
body.shared #btn-maps, body.shared #drawer, body.shared #drawer-scrim { display: none !important; }
body.shared #map-title { pointer-events: none; }
body.shared .m-texsel option[value="__upload"], body.shared #insp-model [data-act="textures"] { display: none !important; }
body.read-only :is(#tb-child, #tb-sibling, #tb-f-step, #tb-f-decision, #tb-f-connect, #tb-m-add, #tb-m-dup, #tb-m-rot, #tb-m-params, #tb-edit, #tb-delete, #tb-undo, #toolbar .sep, #btn-undo, #btn-redo) { display: none !important; }
body.read-only .f-handle, body.read-only .edit-only { display: none !important; }
body.read-only #inspector :is(input, textarea, select) { pointer-events: none; }
body.read-only #inspector :is(.swatches, .icon-grid, #insp-shape, #insp-style, #insp-font, #insp-estyle, #insp-earrow) button { pointer-events: none; }
body.read-only #inspector :is(#insp-add-link, .link-row.draft, .link-row [data-act="remove"], #insp-ereverse, #insp-einsert, #insp-edelete, #insp-model button[data-act]) { display: none !important; }
.share-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px 12px; align-items: end; margin: 4px 0 12px; }
.share-form .share-tag { grid-column: 1 / -1; }
.share-form .seg { justify-self: start; width: max-content; }
.share-form #sh-create { grid-column: 1 / -1; justify-self: start; }
.share-new { border: 1px solid var(--accent); background: var(--accent-soft); border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; }
.share-new p { margin: 0 0 8px; }
.share-new .row-inline { display: flex; gap: 8px; }
.share-new input { flex: 1; min-width: 0; font: 12.5px ui-monospace, "Cascadia Mono", Consolas, monospace; }
.share-list li.off { opacity: 0.55; }
.share-mode { font-size: 11.5px; padding: 1px 7px; border-radius: 999px; background: var(--panel-2, #eef0f3); color: var(--muted); margin-left: 4px; }
.share-mode.edit { background: var(--warn-bg); color: var(--warn-ink); }

/* Flowchart subprocesses: the badge on a step, the preview card, the way back to the parent. */
.f-subflow { color: var(--accent); }
.parent-chip { flex: none; max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; border: 1px solid var(--line); background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 3px 10px; font: 600 12.5px/1.3 inherit; cursor: pointer; }
.parent-chip:hover { filter: brightness(1.05); }
/* The preview card: 760 x 560 to start (capped to the window), resizable from its corner grip; the drawing
   fills the space left and scales with it. */
.sub-preview { position: fixed; z-index: 21; top: calc(var(--topbar-h) + var(--safe-top) + 10px); left: 10px;
  width: 760px; height: 560px; min-width: 260px; min-height: 220px;
  max-width: calc(100vw - 20px); max-height: calc(100vh - var(--topbar-h) - var(--safe-top) - 20px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 10px 12px 12px;
  display: flex; flex-direction: column; gap: 6px; box-sizing: border-box; }
.pv-grip { position: absolute; right: 3px; bottom: 3px; width: 20px; height: 20px; cursor: nwse-resize; touch-action: none; border-radius: 0 0 11px 0;
  background: linear-gradient(135deg, transparent 0 50%, var(--muted) 50% 56%, transparent 56% 68%, var(--muted) 68% 74%, transparent 74% 86%, var(--muted) 86% 92%, transparent 92%); opacity: 0.7; }
.pv-grip:hover, .pv-grip:focus-visible { opacity: 1; outline: none; }
body.pv-resizing, body.pv-resizing * { cursor: nwse-resize !important; user-select: none; }
.pv-head-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.pv-kicker { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pv-kicker svg { color: var(--accent); flex: none; }
.pv-title { font-weight: 650; font-size: 15px; }
.pv-body { cursor: pointer; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); flex: 1 1 auto; min-height: 60px; display: grid; place-items: center; overflow: hidden; }
.pv-body .hint { margin: 12px; }
.pv-svg { width: 100%; height: 100%; display: block; }
.pv-head-row, .pv-title, .pv-actions { flex: none; }
.pv-svg .pv-shape { fill: var(--f-process, var(--panel)); stroke: var(--f-stroke, var(--edge)); stroke-width: 1.5; }
.pv-svg .pv-shape.fs-decision { fill: var(--f-decision, var(--panel)); }
.pv-svg .pv-shape.fs-terminal { fill: var(--f-terminal, var(--panel)); }
.pv-svg .pv-shape.fs-io { fill: var(--f-io, var(--panel)); }
.pv-svg .pv-shape.fs-subprocess { fill: var(--f-subprocess, var(--panel)); }
.pv-svg .pv-shape.fs-document { fill: var(--f-document, var(--panel)); }
.pv-svg .pv-shape.fs-database { fill: var(--f-database, var(--panel)); }
.pv-svg .pv-line { fill: none; stroke: var(--edge); stroke-width: 1.6; }
.pv-svg .pv-head { fill: var(--edge); }
.pv-svg .pv-text { font: 600 17px system-ui, sans-serif; fill: var(--text); }
.pv-actions { display: flex; align-items: center; gap: 10px; }
.sub-box { display: grid; gap: 6px; }
.sub-name { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.sub-name svg { color: var(--accent); }

/* 3D models: perspective / isometric switch, over the top-left of the view. */
#m-viewbar { position: fixed; z-index: 18; top: calc(var(--topbar-h) + var(--safe-top) + 10px); left: 10px; display: flex; gap: 8px; align-items: center; }
#m-viewmode { background: var(--panel); box-shadow: var(--shadow); font-size: 13px; }
#m-viewmode button { padding: 6px 11px; color: var(--text); }
body.vr-on #m-viewbar { display: none !important; }
/* Edit switch: off, dragging only turns the view; on, a selected part can be dragged and changed. */
.m-edit { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); font: inherit; font-size: 13px; cursor: pointer; }
.m-edit svg { width: 15px; height: 15px; }
.m-edit[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
body.read-only .m-edit { display: none; }
.m-viewonly { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; margin-bottom: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--accent-soft); font-size: 13px; }
.seg button:disabled { opacity: 0.45; cursor: default; }
.seg button:disabled.on { background: transparent; color: inherit; font-weight: inherit; }
.hint { font-size: 12.5px; color: var(--muted); margin: 0; }
.lbl-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.lbl-row .lbl { margin-bottom: 0; }
.kids-all { display: inline-flex; gap: 6px; }
.kids-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 340px; overflow-y: auto; }
.kid { display: flex; align-items: center; gap: 4px; padding: 1px 4px 1px 0; border-radius: 8px; }
.kid:hover { background: var(--panel-2); }
.kid .icon-btn { flex: none; }
.kid-name {
  flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 6px 2px; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: inherit; color: inherit;
}
.kid-ref { flex: none; font: 600 10.5px ui-monospace, "Cascadia Mono", Consolas, monospace; color: var(--muted); }
.kid.off .kid-name { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--line); cursor: default; }
.kid.off .kid-eye { color: var(--muted); }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); gap: 4px; }
.icon-grid button {
  height: 36px; border: 1px solid transparent; border-radius: 8px; background: var(--panel-2); cursor: pointer; font-size: 18px;
}
.icon-grid button:hover { border-color: var(--line); }
.icon-grid button.on { border-color: var(--accent); background: var(--accent-soft); }
.meta { font-size: 12.5px; color: var(--muted); display: grid; gap: 4px; }
.meta code { font-size: 12px; background: var(--panel-2); padding: 1px 5px; border-radius: 5px; user-select: all; }

@media (max-width: 640px) {
  #inspector {
    top: auto; left: 0; width: auto; height: min(62vh, 560px); border-left: 0; border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0; transform: translateY(105%); padding-bottom: var(--safe-bottom);
  }
  #inspector.open { transform: none; }
}

/* ---------------------------------------------------------------- menus, dialogs */
.popover {
  position: fixed; z-index: 50; min-width: 220px; max-width: calc(100vw - 16px); max-height: calc(100vh - 16px); overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 5px;
}
.popover button {
  width: 100%; display: flex; align-items: center; gap: 10px; border: 0; background: transparent; text-align: left;
  padding: 9px 10px; border-radius: 8px; cursor: pointer; font-size: 14.5px;
}
.popover button:hover, .popover button:focus-visible { background: var(--panel-2); outline: none; }
.popover button.danger { color: var(--danger); }
.popover button .k { margin-left: auto; color: var(--muted); font-size: 12px; }
.popover .mi-two { display: flex; flex-direction: column; min-width: 0; max-width: 300px; }
.popover .mi-two > span, .popover .mi-two small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.popover .mi-two small { color: var(--muted); font-size: 12px; }
.popover hr { border: 0; border-top: 1px solid var(--line); margin: 5px 4px; }
@media (pointer: coarse) { .popover button { padding: 12px 12px; font-size: 15.5px; } }

dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--panel); color: var(--text);
  width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 48px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(10, 15, 25, 0.4); }
.dlg-body { padding: 4px 18px 18px; overflow-y: auto; max-height: calc(100vh - 140px); }
.dlg-body p { margin: 8px 0; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; padding: 0 18px 18px; flex-wrap: wrap; }
.kbd-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.kbd-table td { padding: 5px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.kbd-table td:first-child { white-space: nowrap; color: var(--muted); width: 40%; }
kbd { font: 12px ui-monospace, monospace; background: var(--panel-2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; }
pre.code { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; overflow-x: auto; font-size: 12.5px; white-space: pre; }
.versions { list-style: none; margin: 0; padding: 0; }
.versions li { display: flex; align-items: center; gap: 10px; padding: 9px 2px; border-bottom: 1px solid var(--line); }
.versions .grow { flex: 1; min-width: 0; }
.versions .m { font-size: 12.5px; color: var(--muted); }
h3.dlg-title { margin: 0; padding: 16px 18px 6px; font-size: 17px; }

#toast {
  position: fixed; z-index: 60; left: 50%; bottom: calc(var(--toolbar-h) + 30px + var(--safe-bottom));
  transform: translateX(-50%) translateY(10px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 10px; font-size: 14px;
  transition: opacity 0.18s, transform 0.18s; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

.drop-overlay {
  position: fixed; inset: 10px; z-index: 70; border: 3px dashed var(--accent); border-radius: 18px;
  background: var(--accent-soft); display: grid; place-items: center; font-size: 20px; font-weight: 600; color: var(--accent); pointer-events: none;
}

/* ---------------------------------------------------------------- flowcharts */
:root {
  --f-stroke: #7b8496; --f-process: #ffffff; --f-decision: #fff4cf; --f-terminal: #dff3e5; --f-io: #e3edff;
  --f-document: #f1e9ff; --f-subprocess: #e2f2f5; --f-database: #eceff4; --f-person: #fde6ec; --f-connector: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --f-stroke: #8891a5; --f-process: #222632; --f-decision: #3a321a; --f-terminal: #1c3326; --f-io: #1e2a42;
    --f-document: #2b2340; --f-subprocess: #1c3034; --f-database: #262a33; --f-person: #3a222b; --f-connector: #222632;
  }
}
body:not(.kind-flow) .flow-only { display: none !important; }
body.kind-flow .mm-only { display: none !important; }
body.kind-flow.sel-edge .node-only { display: none !important; }
body:not(.sel-edge) .edge-only { display: none !important; }

#edges path.f-shape { stroke: var(--f-stroke); stroke-width: 1.5; pointer-events: visiblePainted; cursor: default; stroke-linejoin: round; }
#edges path.f-shape.fs-process { fill: var(--f-process); }
#edges path.f-shape.fs-decision { fill: var(--f-decision); }
#edges path.f-shape.fs-terminal { fill: var(--f-terminal); }
#edges path.f-shape.fs-io { fill: var(--f-io); }
#edges path.f-shape.fs-document { fill: var(--f-document); }
#edges path.f-shape.fs-subprocess { fill: var(--f-subprocess); }
#edges path.f-shape.fs-database { fill: var(--f-database); }
#edges path.f-shape.fs-person { fill: var(--f-person); }
#edges path.f-shape.fs-connector { fill: var(--f-connector); }
#edges path.f-shape.sel { stroke: var(--accent); stroke-width: 3; }
#edges path.f-shape.match { stroke: #f5b400; stroke-width: 3; }
#edges path.f-shape.pick { stroke: var(--accent); stroke-dasharray: 5 4; stroke-width: 2.5; }
#edges path.f-shape.drop-target { stroke: var(--accent); stroke-width: 3.5; }
#edges .f-lane { stroke: var(--line); stroke-width: 1; }
#edges .f-edge path.f-line { stroke: var(--edge); stroke-width: 1.6; fill: none; stroke-linejoin: round; }
#edges .f-edge.dashed path.f-line { stroke-dasharray: 6 5; }
#edges .f-edge.thick path.f-line { stroke-width: 3.2; }
#edges .f-edge path.f-hit { stroke: transparent; stroke-width: 14; fill: none; pointer-events: stroke; cursor: pointer; }
#edges .f-edge.sel path.f-line { stroke: var(--accent); stroke-width: 2.6; }
#edges .f-edge:hover path.f-line { stroke: var(--accent); }
#edges path.f-rubber { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; fill: none; }

.node.fnode {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  min-width: 84px; max-width: 220px; padding: 9px 13px; text-align: center; border: 0; border-radius: 0;
  background: transparent; white-space: pre-wrap; font-size: 13.5px;
}
.node.fnode .n-text { font-weight: 600; }
.node.fnode .f-sub { font-size: 11.5px; color: var(--muted); font-weight: 400; line-height: 1.25; }
.node.fnode.fs-decision { max-width: 150px; min-width: 50px; padding: 2px 4px; }
.node.fnode.fs-connector { min-width: 24px; padding: 4px; }
.node.fnode.fs-terminal, .node.fnode.fs-person { padding: 8px 14px; }
.node.fnode .f-person { color: var(--muted); line-height: 0; }
.node.fnode .f-person svg { width: 15px; height: 15px; }
.node.fnode.sel, .node.fnode.match, .node.fnode.sel.match { background: transparent; box-shadow: none; }
.node.fnode .n-badges { position: absolute; top: -10px; right: -12px; margin: 0; padding: 0 3px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); line-height: 1.3; }
.node.fnode .n-ref { position: absolute; top: -9px; left: -6px; margin: 0; }
.node.fnode .f-handle {
  position: absolute; left: 50%; bottom: -24px; width: 22px; height: 22px; margin-left: -11px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); cursor: crosshair;
  opacity: 0; transition: opacity 0.12s; box-shadow: var(--shadow); line-height: 0;
}
.node.fnode .f-handle svg { width: 14px; height: 14px; }
body.flow-lr .node.fnode .f-handle { left: auto; right: -26px; bottom: auto; top: 50%; margin: -11px 0 0 0; }
.node.fnode.fs-decision .f-handle { bottom: calc(-24px - var(--dec-pad, 0px)); }
.node.fnode:hover .f-handle, .node.fnode.sel .f-handle { opacity: 1; }
@media (pointer: coarse) { .node.fnode.sel .f-handle { width: 30px; height: 30px; margin-left: -15px; bottom: -32px; } }
.node.fnode.dragging-f { opacity: 0.85; }
body.flow-connecting #canvas { cursor: crosshair; }

.f-label {
  position: absolute; left: 0; top: 0; max-width: 180px; padding: 1px 7px; border-radius: 9px; font-size: 12px; line-height: 1.35;
  background: var(--bg); border: 1px solid var(--line); color: var(--text); white-space: pre-wrap; text-align: center; cursor: pointer;
  user-select: none; -webkit-user-select: none;
}
.f-label.sel { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.f-lane-title {
  position: absolute; transform: translate(-50%, -50%); font-size: 13px; font-weight: 700; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--muted); white-space: nowrap; pointer-events: none; user-select: none;
}

.shape-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.shape-grid button {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px; border: 1px solid transparent; border-radius: 8px;
  background: var(--panel-2); cursor: pointer; font-size: 11px; color: var(--text);
}
.shape-grid button.on { border-color: var(--accent); background: var(--accent-soft); }
.shape-i { width: 26px; height: 20px; fill: var(--panel); stroke: var(--f-stroke); stroke-width: 1.4; }
.popover .shape-i { width: 20px; height: 16px; }
.conn-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.conn-list li { display: flex; align-items: center; gap: 6px; font-size: 13px; min-width: 0; }
.conn-list .conn { flex: none; border: 1px solid var(--line); background: var(--panel-2); border-radius: 7px; padding: 2px 6px; font-size: 11.5px; cursor: pointer; color: var(--muted); }
.conn-list .conn-name { flex: 1; min-width: 0; text-align: left; border: 0; background: none; padding: 4px 0; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: inherit; font: inherit; }
.conn-list .conn-label { flex: none; font-size: 11.5px; color: var(--muted); border: 1px solid var(--line); border-radius: 8px; padding: 0 6px; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kind-tag { display: inline-flex; vertical-align: -3px; margin-right: 5px; color: var(--accent); }
.kind-tag svg { width: 16px; height: 16px; }
.lane-edit { display: grid; gap: 6px; margin: 8px 0 10px; }
.lane-row { display: flex; align-items: center; gap: 4px; }
.lane-row .field { flex: 1; min-width: 0; padding: 7px 10px; }
.lane-row input[type=color] { width: 34px; height: 34px; padding: 0; border: 1px solid var(--line); border-radius: 8px; background: none; cursor: pointer; }

/* ---------------------------------------------------------------- 3D models */
body:not(.kind-model) .model-only { display: none !important; }
body.kind-model .mm-only, body.kind-model .no-model { display: none !important; }
body.kind-model.model-nosel .model-sel, body.kind-model.model-nosel #insp-meta { display: none !important; }
#scene3d { position: absolute; inset: 0; width: 100%; height: 100%; display: none; touch-action: none; outline: none; }
body:not(.kind-model) #scene3d { display: none !important; } /* never behind a mind map or flowchart */
body.kind-model #scene3d { display: block; }
body.kind-model #world { display: none; }
body.kind-model #canvas { background-image: none; }

#insp-model { display: grid; gap: 10px; }
#insp-model .lbl { margin: 4px 0 0; }
.m-type { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.m-type svg { width: 20px; height: 20px; color: var(--accent); }
.m-type .hint { margin-left: auto; }
.m-error { margin: 0; padding: 8px 10px; border-radius: 10px; background: var(--warn-bg); color: var(--warn-ink); font-size: 13px; }
.m-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 8px; }
.m-field { display: grid; gap: 2px; min-width: 0; text-transform: none !important; letter-spacing: 0 !important; font-weight: 400 !important; }
.m-field.wide { grid-column: 1 / -1; }
.m-field > span { font-size: 12px; color: var(--muted); }
.m-field .field { padding: 7px 9px; font-variant-numeric: tabular-nums; }
.m-field .field.bad, .open-row .field.bad, .param-row .field.bad { border-color: var(--danger); }
.m-val { font-size: 11.5px; color: var(--muted); min-height: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-check { display: flex; align-items: center; gap: 6px; font-size: 13.5px; text-transform: none !important; letter-spacing: 0 !important; font-weight: 400 !important; color: var(--text) !important; }
.m-colors { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; }
.m-color { display: flex; align-items: center; gap: 6px; font-size: 13px; text-transform: none !important; letter-spacing: 0 !important; font-weight: 400 !important; color: var(--text) !important; margin: 0 !important; }
.m-color input { width: 30px; height: 26px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; cursor: pointer; }
/* Colours and textures: one row per colour part (swatch, texture, size, turn). */
.m-texlist { display: grid; gap: 8px; }
.m-texrow { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; }
.m-texrow .m-color { min-width: 74px; }
.m-texsel { flex: 1 1 130px; min-width: 0; padding: 6px 8px; }
.m-texnum { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted) !important; text-transform: none !important; letter-spacing: 0 !important; font-weight: 400 !important; margin: 0 !important; }
.m-texnum .field { width: 64px; padding: 5px 7px; font-variant-numeric: tabular-nums; }
.m-texnum select.field { width: auto; }
.m-texnum .field.bad { border-color: var(--danger); }
.m-texlist > .btn { justify-self: start; }
.tex-head { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 8px; }
.tex-lib { display: grid; gap: 8px; }
.tex-item { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 10px; align-items: center; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.tex-item img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2, #0001); }
.tex-fields { display: grid; gap: 4px; min-width: 0; }
.tex-fields .field { padding: 6px 8px; min-width: 0; }
.tex-fields .field.bad { border-color: var(--danger); }
.tex-w { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.tex-w .field { width: 80px; font-variant-numeric: tabular-nums; }
.tex-builtins { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 10px; margin: 4px 0 6px; }
.tex-builtins figure { margin: 0; display: grid; gap: 3px; font-size: 12px; text-align: center; }
.tex-builtins img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.tex-builtins small { display: block; color: var(--muted); font-size: 11px; }
.open-list { display: grid; gap: 6px; }
.open-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)) auto; gap: 4px 6px; padding: 6px; border: 1px solid var(--line); border-radius: 10px; align-items: end; }
.open-row select { grid-column: 1 / -2; padding: 6px 8px; }
.open-row label { display: grid; gap: 1px; font-size: 11px; color: var(--muted); text-transform: none !important; letter-spacing: 0 !important; font-weight: 400 !important; margin: 0 !important; }
.open-row .field { padding: 6px 7px; min-width: 0; }
.open-row .icon-btn { grid-row: 1; grid-column: -2; }
.param-list { display: grid; gap: 8px; margin: 8px 0; }
.param-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) auto auto; gap: 4px 6px; align-items: center; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.param-row .field { padding: 7px 9px; min-width: 0; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 13px; }
.param-row .p-note { grid-column: 1 / -1; font-family: inherit; }
.param-row .p-val { font: 600 13px ui-monospace, "Cascadia Mono", Consolas, monospace; color: var(--muted); min-width: 40px; text-align: right; }
.param-err { color: var(--danger) !important; min-height: 1em; }

/* ---------------------------------------------------------------- VR */
#vr-stage { position: fixed; inset: 0; z-index: 90; background: #1c212b; }
#vr-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#vr-stage .vr-exit { position: absolute; top: calc(12px + var(--safe-top)); right: 12px; z-index: 2; }
#vr-stage .vr-hud {
  position: absolute; left: 12px; right: 140px; top: calc(12px + var(--safe-top)); z-index: 2; color: #e5e8ee; font-size: 13px;
  background: rgba(20, 24, 32, 0.75); border-radius: 10px; padding: 8px 12px; line-height: 1.45; pointer-events: none;
}
