@font-face { font-family: 'Caveat'; font-style: normal; font-weight: 400 700; font-display: swap; src: url('fonts/caveat-latin-ext.woff2') format('woff2'); unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C4, U+2113, U+2C60-2C7F, U+A720-A7FF; }
@font-face { font-family: 'Caveat'; font-style: normal; font-weight: 400 700; font-display: swap; src: url('fonts/caveat-latin.woff2') format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }

:root {
  --bg: #f7f7f8; --dot: #d4d4d8;
  --surface: #ffffff; --surface-2: #f4f4f5; --surface-3: #ececee;
  --ink: #18181b; --ink-2: #52525b; --ink-3: #a1a1aa;
  --line: #e4e4e7; --line-2: #d4d4d8;
  --accent: #5b5bf6; --accent-soft: rgba(91, 91, 246, .12); --accent-ink: #fff;
  --guide: #f0508c; --edge: #9a9aa6; --danger: #e5484d;
  --shadow: 0 1px 2px rgba(24, 24, 27, .05), 0 6px 20px rgba(24, 24, 27, .06);
  --shadow-lg: 0 12px 48px rgba(24, 24, 27, .18), 0 2px 8px rgba(24, 24, 27, .08);
  --pill: 0 1px 2px rgba(24, 24, 27, .06), 0 8px 24px rgba(24, 24, 27, .08);
  --font: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --hand: 'Caveat', 'Kalam', 'Segoe Print', 'Bradley Hand', 'Comic Sans MS', cursive;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --iz: 1;
}
html[data-theme="dark"] {
  --bg: #0e0e10; --dot: #2a2a2f;
  --surface: #18181b; --surface-2: #1f1f23; --surface-3: #27272c;
  --ink: #f4f4f5; --ink-2: #a1a1aa; --ink-3: #6b6b75;
  --line: #2a2a30; --line-2: #3a3a42;
  --accent: #8584ff; --accent-soft: rgba(133, 132, 255, .16); --accent-ink: #0e0e10;
  --edge: #6f6f7b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .3);
  --shadow-lg: 0 12px 48px rgba(0, 0, 0, .6), 0 2px 8px rgba(0, 0, 0, .4);
  --pill: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font: 14px/1.5 var(--font); color: var(--ink); background: var(--bg);
  -webkit-font-smoothing: antialiased; user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { opacity: .35; cursor: default; pointer-events: none; }
input, textarea { font: inherit; color: inherit; }
kbd { font: 11px var(--mono); background: var(--surface-3); border-radius: 4px; padding: 1px 5px; color: var(--ink-2); }
.ic { display: block; flex: none; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; }

/* ---------- viewport ---------- */
#viewport {
  position: absolute; inset: 0; overflow: hidden; touch-action: none; background-color: var(--bg);
  background-image: radial-gradient(circle, var(--dot) 1.1px, transparent 1.3px);
  background-size: 24px 24px;
}
#viewport[data-tool="hand"], #viewport.pan-ready { cursor: grab; }
#viewport.panning, #viewport.panning * { cursor: grabbing !important; }
#viewport[data-tool="sticky"], #viewport[data-tool="rect"], #viewport[data-tool="ellipse"], #viewport[data-tool="diamond"], #viewport[data-tool="text"],
#viewport[data-tool="chat"], #viewport[data-tool="table"], #viewport[data-tool="frame"], #viewport[data-tool="arrow"] { cursor: crosshair; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#world.anim { transition: transform .3s cubic-bezier(.2, .7, .2, 1); }
#layer-frames, #layer-nodes, #overlay { position: absolute; left: 0; top: 0; width: 0; height: 0; overflow: visible; }
#layer-frames { z-index: 1; } #edges { z-index: 2; } #layer-nodes { z-index: 3; } #overlay { z-index: 4; pointer-events: none; }
#edges, #guides { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }

#empty-hint {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); text-align: center; color: var(--ink-3);
  pointer-events: none; opacity: 0; transition: opacity .3s; max-width: 380px; font-size: 13px;
}
#empty-hint.show { opacity: 1; }
#empty-hint .big { font-size: 20px; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; letter-spacing: -.01em; }

/* ---------- edges ---------- */
.edge .hit { fill: none; stroke: transparent; stroke-width: 14; vector-effect: non-scaling-stroke; pointer-events: stroke; cursor: pointer; }
.edge .ln, .edge .hd, .edge .tl { fill: none; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.edge.sel .ln, .edge.sel .hd, .edge.sel .tl { stroke: var(--accent) !important; }
.edge.preview .ln, .edge.preview .hd { stroke: var(--accent) !important; opacity: .7; }
.edge.preview .hit { display: none; }
.ehandle { fill: var(--surface); stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; pointer-events: all; cursor: crosshair; }
.guide { stroke: var(--guide); stroke-width: 1; vector-effect: non-scaling-stroke; }
.anchor { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.anchor.on { fill: var(--accent); }

/* ---------- nodes ---------- */
.node { position: absolute; user-select: none; }
.node.selected::after {
  content: ''; position: absolute; inset: -3px; border: calc(1.5px * var(--iz)) solid var(--accent);
  border-radius: 14px; pointer-events: none;
}
.node.t-shape.selected::after { border-radius: 4px; inset: -2px; }
.node .card {
  position: absolute; inset: 0; display: flex; flex-direction: column; overflow: hidden;
  background: var(--n-fill, var(--surface)); border: 1px solid var(--n-stroke, var(--line)); border-radius: 12px; box-shadow: var(--shadow);
}
.node.autoh .card { position: relative; inset: auto; }
.node.t-image .card { padding: 0; border-radius: 10px; background: var(--surface-2); }
.node.t-image img { width: 100%; height: 100%; display: block; object-fit: fill; pointer-events: none; user-select: none; }

.hdr {
  display: flex; align-items: center; gap: 8px; height: 36px; flex: none; padding: 0 8px 0 12px;
  font-size: 12px; font-weight: 500; color: var(--ink-2); cursor: grab; border-bottom: 1px solid var(--line);
  letter-spacing: .01em;
}
.node.collapsed .hdr { border-bottom-color: transparent; }
.hdr .hic { color: var(--ink-3); display: flex; }
.hdr .hic.accent { color: var(--accent); }
.hdr .ttl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.node.collapsed .hdr .ttl { color: var(--ink); }
.hdr .sp, .ftr .sp { flex: 1; }
.hdr .stat { font-size: 11px; color: var(--ink-3); font-weight: 400; white-space: nowrap; }
.hdr .stat.pulse { color: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.hdr .chip { font: 10.5px var(--mono); color: var(--ink-3); background: var(--surface-2); padding: 1px 6px; border-radius: 99px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hdr .cnt { font-size: 11px; color: var(--ink-3); font-weight: 400; }
.col svg { transition: transform .18s; }
.node.collapsed .col svg { transform: rotate(-90deg); }
.node.collapsed .body, .node.collapsed .ftr { display: none; }
.ftr { display: flex; align-items: center; gap: 6px; flex: none; height: 44px; padding: 0 10px 0 12px; border-top: 1px solid var(--line); cursor: grab; }
.ftr .meta { font-size: 11.5px; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.ibtn {
  width: 32px; height: 32px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center; color: var(--ink-2);
  transition: background .12s, color .12s; flex: none;
}
.ibtn:hover { background: var(--surface-3); color: var(--ink); }
.ibtn.on { background: var(--accent-soft); color: var(--accent); }
.ibtn.sm { width: 26px; height: 26px; border-radius: 7px; }
.ibtn.danger:hover { background: rgba(229, 72, 77, .14); color: var(--danger); }

.btn {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 14px; border-radius: 10px;
  font-weight: 500; font-size: 13.5px; transition: background .12s, transform .06s, opacity .12s; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--ink); color: var(--surface); }
.btn.primary:hover { opacity: .88; }
.btn.ghost { color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-3); color: var(--ink); }
.btn.ghost.on { background: var(--accent-soft); color: var(--accent); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.sm { height: 26px; padding: 0 9px; font-size: 12px; gap: 5px; border-radius: 7px; }

/* chat */
.node.t-chat .body { flex: 1; min-height: 0; display: flex; }
textarea.prompt {
  flex: 1; width: 100%; resize: none; border: 0; outline: 0; background: transparent; padding: 12px 14px;
  font-size: 14px; line-height: 1.55; user-select: text; -webkit-user-select: text;
}
textarea.prompt::placeholder { color: var(--ink-3); }
.run {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 12px 0 10px; border-radius: 99px;
  background: var(--ink); color: var(--surface); font-size: 12.5px; font-weight: 500; transition: opacity .12s, transform .06s; flex: none;
}
.run:hover { opacity: .86; } .run:active { transform: scale(.97); }
.run.stop { background: rgba(229, 72, 77, .12); color: var(--danger); }
.node.busy .card { border-color: var(--accent); }

/* reply + markdown */
.node.t-reply .body { flex: 1; min-height: 0; padding: 12px 14px 8px; overflow: auto; user-select: text; -webkit-user-select: text; cursor: text; }
.node.autoh.t-reply .body { max-height: 520px; flex: none; }
.node.t-reply .ftr { height: 38px; }
.node.failed .card { border-color: rgba(229, 72, 77, .5); }
.md { font-size: 14px; line-height: 1.6; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: .55em 0; } .md h1, .md h2, .md h3, .md h4 { margin: 1em 0 .4em; line-height: 1.3; letter-spacing: -.01em; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.18em; } .md h3, .md h4 { font-size: 1.04em; }
.md ul, .md ol { margin: .5em 0; padding-left: 1.4em; } .md li { margin: .15em 0; }
.md code { font: .88em var(--mono); background: var(--surface-3); padding: .1em .35em; border-radius: 5px; }
.md pre { background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; overflow: auto; margin: .7em 0; }
.md pre code { background: none; padding: 0; font-size: 12.5px; line-height: 1.55; }
.md blockquote { margin: .6em 0; padding: .1em 0 .1em .9em; border-left: 3px solid var(--line-2); color: var(--ink-2); }
.md a { color: var(--accent); text-decoration: none; } .md a:hover { text-decoration: underline; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md table { border-collapse: collapse; margin: .7em 0; font-size: 13px; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; text-align: left; }
.md th { background: var(--surface-2); font-weight: 600; }
.caret { display: inline-block; width: 7px; height: 1.05em; vertical-align: text-bottom; background: var(--accent); margin-left: 2px; border-radius: 1px; animation: blink 1s steps(2) infinite; }
.thinking { display: flex; gap: 5px; padding: 6px 0; }
.thinking i { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); animation: bounce 1.2s infinite ease-in-out; }
.thinking i:nth-child(2) { animation-delay: .15s; } .thinking i:nth-child(3) { animation-delay: .3s; }
.errbox { margin-top: 8px; padding: 9px 12px; border-radius: 9px; background: rgba(229, 72, 77, .1); color: var(--danger); font-size: 12.5px; word-break: break-word; }
.errbox a { color: inherit; font-weight: 600; }
.note { margin-top: 8px; font-size: 12px; color: var(--ink-3); font-style: italic; }
@keyframes pulse { 50% { opacity: .45; } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); opacity: .4; } 40% { transform: translateY(-4px); opacity: 1; } }

/* table */
.tbl-wrap { flex: 1; min-height: 0; overflow: auto; }
.node.autoh .tbl-wrap { max-height: 480px; flex: none; }
table.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
table.grid td { border: 1px solid var(--line); padding: 7px 10px; min-width: 70px; outline: 0; user-select: text; -webkit-user-select: text; cursor: text; vertical-align: top; white-space: pre-wrap; }
table.grid tr:first-child td { border-top: 0; } table.grid td:first-child { border-left: 0; } table.grid td:last-child { border-right: 0; } table.grid tr:last-child td { border-bottom: 0; }
table.grid.has-head tr:first-child td { font-weight: 600; background: var(--surface-2); }
table.grid td:focus { background: var(--accent-soft); box-shadow: inset 0 0 0 1.5px var(--accent); }

/* frame (collapsible section) */
.node.t-frame { pointer-events: none; }
.node.t-frame .card { background: var(--n-fill, rgba(128, 128, 145, .06)); border: 1px dashed var(--n-stroke, var(--line-2)); box-shadow: none; pointer-events: none; }
.node.t-frame .hdr { pointer-events: auto; border-bottom: 0; color: var(--ink-2); }
.node.t-frame .hdr .ttl { font-weight: 600; font-size: 13px; color: var(--ink); }
.node.t-frame.collapsed .card { background: var(--surface); border-style: solid; border-color: var(--line); box-shadow: var(--shadow); }
.node.t-frame .conn { pointer-events: auto; }
.node.t-frame.selected::after { pointer-events: none; }

/* shapes */
.node.t-shape svg.shp { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.shp-text { position: absolute; inset: 0; display: flex; align-items: center; overflow: hidden; }
.node.is-text .shp-text { position: relative; inset: auto; overflow: visible; }
.txt { width: 100%; outline: 0; white-space: pre-wrap; word-break: break-word; line-height: 1.35; cursor: inherit; }
.txt.editing { user-select: text; -webkit-user-select: text; cursor: text; }
.node.is-text .txt:empty::before { content: 'Text'; color: var(--ink-3); }
.node.is-text.selected::after { border-radius: 4px; }

/* connection dots */
.conn {
  position: absolute; width: 12px; height: 12px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent);
  transform: translate(-50%, -50%) scale(var(--iz)); opacity: 0; cursor: crosshair; z-index: 6; transition: opacity .12s;
}
.conn.t { left: 50%; top: 0; } .conn.b { left: 50%; top: 100%; } .conn.l { left: 0; top: 50%; } .conn.r { left: 100%; top: 50%; }
.node:hover > .conn, .node.selected > .conn { opacity: 1; }
body.dragging .conn, body.dragging .node:hover > .conn { opacity: 0; }
#viewport[data-tool="hand"] .conn { display: none; }

/* handles */
#handles { position: absolute; pointer-events: none; display: none; }
.hnd {
  position: absolute; width: 10px; height: 10px; background: var(--surface); border: 1.5px solid var(--accent); border-radius: 3px;
  transform: translate(-50%, -50%) scale(var(--iz)); pointer-events: auto;
}
.hnd.nw { left: 0; top: 0; cursor: nwse-resize; } .hnd.n { left: 50%; top: 0; cursor: ns-resize; } .hnd.ne { left: 100%; top: 0; cursor: nesw-resize; }
.hnd.e { left: 100%; top: 50%; cursor: ew-resize; } .hnd.se { left: 100%; top: 100%; cursor: nwse-resize; } .hnd.s { left: 50%; top: 100%; cursor: ns-resize; }
.hnd.sw { left: 0; top: 100%; cursor: nesw-resize; } .hnd.w { left: 0; top: 50%; cursor: ew-resize; }

#ghost { position: absolute; display: none; border: calc(1.5px * var(--iz)) dashed var(--accent); background: var(--accent-soft); border-radius: 10px; }
#ghost.k-ellipse { border-radius: 50%; } #ghost.k-text { border-radius: 4px; }
#marquee { position: absolute; display: none; border: calc(1px * var(--iz)) solid var(--accent); background: var(--accent-soft); }

/* ---------- floating chrome ---------- */
.pill {
  position: absolute; display: flex; align-items: center; gap: 2px; padding: 4px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--pill); z-index: 20;
}
.pill .sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; flex: none; }
#brand { left: 14px; top: 14px; }
#proj-name {
  border: 0; outline: 0; background: transparent; font-weight: 600; font-size: 13.5px; width: 170px; height: 32px; padding: 0 10px 0 4px;
  text-overflow: ellipsis; letter-spacing: -.005em; user-select: text; -webkit-user-select: text; border-radius: 8px;
}
#proj-name:hover { background: var(--surface-2); } #proj-name:focus { background: var(--surface-2); box-shadow: 0 0 0 2px var(--accent-soft); }
#toolbar { left: 50%; top: 14px; transform: translateX(-50%); max-width: calc(100vw - 480px); }
.tool {
  height: 36px; min-width: 36px; padding: 0 8px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  color: var(--ink-2); transition: background .12s, color .12s; font-size: 13px; font-weight: 500; flex: none;
}
.tool:hover { background: var(--surface-3); color: var(--ink); }
.tool.active { background: var(--ink); color: var(--surface); }
.tool.wide { padding: 0 11px 0 9px; }
.tool.accent:not(.active) { color: var(--accent); }
.tool.accent:not(.active):hover { background: var(--accent-soft); }
#topright { right: 14px; top: 14px; }
#zoombar { left: 14px; bottom: 14px; }
.zlabel { min-width: 52px; height: 32px; border-radius: 8px; font-size: 12.5px; font-weight: 500; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.zlabel:hover { background: var(--surface-3); color: var(--ink); }
#savestate { position: absolute; right: 18px; bottom: 18px; font-size: 11.5px; color: var(--ink-3); z-index: 10; transition: opacity .4s; pointer-events: none; }
#savestate.saved { opacity: .8; } #savestate.saving { opacity: 1; }

body.tb-dragging, body.tb-dragging * { cursor: grabbing !important; }
.tb-ghost {
  position: fixed; left: 0; top: 0; z-index: 200; pointer-events: none; display: flex; align-items: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--accent); color: var(--accent); border-radius: 10px; padding: 8px 12px;
  box-shadow: var(--shadow-lg); font-weight: 500; font-size: 13px; opacity: .95;
}

#tip {
  position: fixed; left: 0; top: 0; z-index: 400; background: var(--ink); color: var(--surface); font-size: 12px; padding: 5px 9px;
  border-radius: 7px; pointer-events: none; opacity: 0; transition: opacity .12s; max-width: 280px; display: flex; gap: 8px; align-items: center;
}
#tip.show { opacity: 1; }
#tip kbd { background: rgba(128, 128, 128, .3); color: inherit; }

/* properties */
#props {
  position: absolute; right: 14px; top: 68px; width: 236px; max-height: calc(100vh - 150px); overflow: auto; z-index: 15;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--pill); padding: 0 14px 10px;
}
.psec { padding: 10px 0; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.psec:last-child { border-bottom: 0; }
.plabel { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .07em; color: var(--ink-3); }
.help { font-size: 11.5px; color: var(--ink-3); }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch {
  width: 22px; height: 22px; border-radius: 50%; border: 1.5px solid rgba(128, 128, 140, .35); padding: 0; position: relative; cursor: pointer;
  transition: transform .1s; overflow: hidden; background-clip: padding-box;
}
.swatch:hover { transform: scale(1.12); }
.swatch.on { box-shadow: 0 0 0 2px var(--surface), 0 0 0 3.5px var(--accent); }
.swatch.none::after { content: ''; position: absolute; left: -2px; right: -2px; top: 50%; height: 1.5px; background: var(--danger); transform: rotate(-45deg); }
input.swatch.custom { -webkit-appearance: none; appearance: none; background: conic-gradient(#e5484d, #f5be1e, #30a46c, #3e63dd, #8e4ec6, #e5484d); padding: 0; }
input.swatch.custom::-webkit-color-swatch-wrapper { padding: 0; opacity: 0; } input.swatch.custom::-webkit-color-swatch { border: 0; }
.seg { display: flex; background: var(--surface-2); border-radius: 9px; padding: 2px; gap: 2px; }
.segbtn { flex: 1; min-height: 28px; border-radius: 7px; font-size: 12px; color: var(--ink-2); display: inline-flex; align-items: center; justify-content: center; padding: 0 6px; font-weight: 500; }
.segbtn:hover { color: var(--ink); } .segbtn.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, .12); }
.icrow { display: flex; gap: 2px; flex-wrap: wrap; } .icrow .ibtn { width: 34px; }
.slider { width: 100%; accent-color: var(--accent); }

/* ---------- modals ---------- */
.overlay-modal {
  position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: rgba(244, 244, 245, .6); backdrop-filter: blur(14px) saturate(1.2); -webkit-backdrop-filter: blur(14px) saturate(1.2);
}
html[data-theme="dark"] .overlay-modal { background: rgba(10, 10, 12, .62); }
#settings { z-index: 110; }
.dialog {
  position: relative; width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow-lg);
  animation: pop .22s cubic-bezier(.2, .8, .2, 1);
}
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }
.dialog .close { position: absolute; right: 14px; top: 14px; }
.welcome-dialog { max-width: 720px; padding: 30px 30px 18px; max-height: calc(100vh - 40px); display: flex; flex-direction: column; }
.w-head { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.w-head h1 { margin: 0; font-size: 26px; letter-spacing: -.025em; line-height: 1.1; }
.w-head p { margin: 2px 0 0; color: var(--ink-2); }
.logo { width: 46px; height: 46px; border-radius: 13px; background: var(--ink); position: relative; flex: none; }
.logo::before, .logo::after { content: ''; position: absolute; border-radius: 4px; background: var(--surface); }
.logo::before { left: 11px; top: 14px; width: 15px; height: 11px; }
.logo::after { left: 21px; top: 26px; width: 15px; height: 11px; opacity: .55; }
.new-row { display: flex; gap: 10px; }
.new-row input {
  flex: 1; height: 44px; border-radius: 12px; border: 1px solid var(--line-2); background: var(--surface); padding: 0 14px; outline: 0; font-size: 15px;
  user-select: text; -webkit-user-select: text; transition: border-color .12s, box-shadow .12s;
}
.new-row input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.new-row .btn { height: 44px; border-radius: 12px; padding: 0 18px; }
.w-label { margin: 26px 0 10px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-3); }
#project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; overflow: auto; padding: 3px; margin: -3px; min-height: 60px; }
.empty-projects { grid-column: 1 / -1; padding: 28px; text-align: center; color: var(--ink-3); border: 1px dashed var(--line-2); border-radius: 14px; }
.pcard { position: relative; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); cursor: pointer; overflow: hidden; transition: border-color .12s, box-shadow .15s, transform .15s; outline: 0; }
.pcard:hover, .pcard:focus-visible { border-color: var(--line-2); box-shadow: var(--shadow); transform: translateY(-1px); }
.pcard.current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.pthumb { height: 118px; background: var(--surface-2); background-image: radial-gradient(circle, var(--dot) 1px, transparent 1.2px); background-size: 14px 14px; color: var(--ink); display: flex; }
.pthumb svg { width: 100%; height: 100%; }
.pmeta { padding: 10px 12px 11px; border-top: 1px solid var(--line); }
.pname { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pname-input { width: 100%; font: 600 13.5px var(--font); border: 0; outline: 0; background: var(--surface-2); border-radius: 6px; padding: 0 4px; margin: 0 -4px; height: 21px; user-select: text; }
.psub { font-size: 12px; color: var(--ink-3); }
.pactions { position: absolute; right: 6px; top: 6px; display: flex; gap: 2px; opacity: 0; transition: opacity .12s; }
.pcard:hover .pactions, .pcard:focus-within .pactions { opacity: 1; }
.pactions .ibtn { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.confirm { position: absolute; inset: 0; background: var(--surface); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 12px; text-align: center; font-weight: 500; font-size: 13px; z-index: 2; cursor: default; }
.confirm .row { display: flex; gap: 8px; }
.w-foot { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--ink-3); font-size: 12.5px; }

.settings-dialog { max-width: 480px; padding: 26px 26px 22px; max-height: calc(100vh - 40px); overflow: auto; }
.settings-dialog h2 { margin: 0; font-size: 19px; letter-spacing: -.015em; }
.settings-dialog .sub { margin: 4px 0 18px; color: var(--ink-2); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field .lbl { font-size: 12px; font-weight: 600; color: var(--ink-2); }
.field input, .field textarea {
  width: 100%; border: 1px solid var(--line-2); background: var(--surface); border-radius: 10px; padding: 9px 12px; outline: 0; font-size: 13.5px;
  user-select: text; -webkit-user-select: text; resize: vertical; transition: border-color .12s, box-shadow .12s;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field .seg .segbtn { min-height: 32px; }
.note-box { background: var(--surface-2); border-radius: 10px; padding: 11px 13px; font-size: 13px; color: var(--ink-2); margin: 0 0 4px; }

#toasts { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; align-items: center; z-index: 300; pointer-events: none; }
.toast { background: var(--ink); color: var(--surface); padding: 9px 15px; border-radius: 10px; font-size: 13px; box-shadow: var(--shadow-lg); animation: pop .2s ease-out; transition: opacity .3s; }
.toast.err { background: var(--danger); color: #fff; } .toast.out { opacity: 0; }

@media (max-width: 1100px) { .tool.wide span { display: none; } .tool.wide { padding: 0 8px; } #toolbar { max-width: calc(100vw - 380px); } }
@media (max-width: 760px) {
  #proj-name { width: 100px; } #toolbar { top: auto; bottom: 66px; max-width: calc(100vw - 20px); overflow-x: auto; }
  #props { top: auto; bottom: 120px; right: 10px; max-height: 40vh; } #zoombar { bottom: 10px; left: 10px; }
  .welcome-dialog { padding: 22px 18px 14px; } .new-row { flex-direction: column; }
}

/* ---------- touch / iOS Safari ---------- */
html { -webkit-text-size-adjust: 100%; }
* { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }   /* no double-tap zoom anywhere */
body { -webkit-touch-callout: none; }
#viewport, #viewport * { touch-action: none; }                                /* the canvas handles its own gestures (pan / pinch) */
#viewport textarea, #viewport .md, #viewport .md *, #viewport .tbl-wrap, #viewport .tbl-wrap * { touch-action: pan-x pan-y; }   /* …but boxes can still scroll (children included) */
#props, #welcome .dialog, #settings .dialog { touch-action: pan-y; }

@media (pointer: coarse) {
  /* iOS zooms the page when a field under 16px gets focus */
  input, textarea, .txt, table.grid td, textarea.prompt, .reply-edit, .new-row input, .field input, .field textarea, #proj-name { font-size: 16px !important; }
  .edge .hit { stroke-width: 40; }                       /* fat invisible stroke: arrows are easy to tap */
  .hnd { width: 18px; height: 18px; border-radius: 5px; }
  .conn { width: 20px; height: 20px; }
  .ibtn { width: 38px; height: 38px; } .ibtn.sm { width: 32px; height: 32px; }
  .tool { height: 42px; min-width: 42px; }
  .segbtn { min-height: 36px; }
  .swatch { width: 28px; height: 28px; }
  .swatches { gap: 9px; }
}

/* ---------- branch / edited replies ---------- */
.ftr .ibtn.again { flex: none; }
.ftr .meta { flex: 0 1 auto; min-width: 0; }
.ftr .sp { min-width: 0; }
.node.t-chat .ftr { overflow: hidden; }
.node.t-reply .hdr .chip.edited { color: #8a6d00; background: rgba(245, 190, 30, .25); font-family: var(--font); }
html[data-theme="dark"] .node.t-reply .hdr .chip.edited { color: #f5d36a; }
.md .hl {
  background: rgba(245, 190, 30, .2); border-left: 3px solid #f5be1e; border-radius: 4px;
  padding: .05em .6em; margin: .45em 0;
}
.md .hl > :first-child { margin-top: .3em; } .md .hl > :last-child { margin-bottom: .3em; }
.reply-edit {
  display: block; width: 100%; min-height: 90px; border: 0; outline: 0; resize: none; background: transparent;
  font: 13.5px/1.6 var(--mono); user-select: text; -webkit-user-select: text; color: var(--ink); padding: 0;
}
.node.editing-reply .card { border-color: #f5be1e; }
.node.editing-reply .view-only { display: none !important; }
.ftr .edit-only { display: none; }
.node.editing-reply .ftr .edit-only { display: inline-flex; }
.node.editing-reply .hdr .ibtn:not(.col) { visibility: hidden; }
.node.t-reply .ftr .btn.view-only { flex: none; }

/* ---------- style panel: collapsible ---------- */
.phead {
  position: sticky; top: 0; z-index: 3; display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0 6px; background: var(--surface);
}
.phead .pchev { margin-right: -5px; }
.ptitle { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .07em; color: var(--ink-3); }
.pchev svg { transition: transform .18s; }
#props.collapsed { padding-bottom: 4px; overflow: hidden; width: 176px; }
#props.collapsed .psec { display: none; }
#props.collapsed .pchev svg { transform: rotate(-90deg); }

/* ---------- "scroll back to content" ---------- */
#back-hint { position: absolute; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 25; animation: pop .25s ease-out; }
#back-hint .btn { background: var(--surface); color: var(--ink); border: 1px solid var(--line); box-shadow: var(--pill); height: 40px; padding: 0 16px 0 13px; border-radius: 99px; }
#back-hint .btn:hover { background: var(--surface-2); }
@media (max-width: 760px) { #back-hint { bottom: 124px; } }

/* ---------- sticky note ---------- */
.node.t-sticky.selected::after { border-radius: 4px; inset: -3px; }
.sticky { position: absolute; inset: 0; filter: drop-shadow(0 1px 1px rgba(40, 30, 0, .14)) drop-shadow(0 10px 14px rgba(40, 30, 0, .16)); }
.sticky .note {
  position: absolute; inset: 0; border-radius: 3px 3px 0 3px;
  background: linear-gradient(172deg, rgba(255, 255, 255, .42), rgba(255, 255, 255, 0) 48%), var(--note, #ffe36e);
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - 26px), calc(100% - 26px) 100%, 0 100%);   /* the tucked-in corner */
}
.sticky .fold {
  position: absolute; right: 0; bottom: 0; width: 26px; height: 26px; border-bottom-left-radius: 3px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--note, #ffe36e) 70%, #000) 0 50%, transparent 50%);
}
.sticky .fold::after { content: ''; position: absolute; inset: 0; background: linear-gradient(135deg, rgba(255, 255, 255, .28), transparent 55%); clip-path: polygon(0 0, 100% 0, 0 100%); }
.note-text { position: absolute; inset: 0; display: flex; align-items: center; padding: 20px; overflow: hidden; }
.t-sticky .txt { font-family: var(--hand); line-height: 1.12; color: #33301f; font-weight: 500; }
.t-sticky .txt:empty::before { content: 'Write something…'; opacity: .4; }
.t-sticky .txt.editing { cursor: text; }

/* ---------- collaboration ---------- */
#collab { left: 14px; top: 66px; padding: 4px 6px 4px 4px; gap: 8px; }
#btn-share { background: var(--ink); color: var(--surface); height: 30px; padding: 0 12px 0 9px; border-radius: 9px; }
#collab.shared #btn-share { background: var(--accent-soft); color: var(--accent); }
#collab.offline #btn-share { background: rgba(245, 190, 30, .25); color: #8a6d00; }
.pdot { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: 0 0 0 2px var(--surface); }
#peers { display: flex; gap: 5px; align-items: center; padding-right: 4px; }
.rcursor {
  position: absolute; pointer-events: none; z-index: 5; transform: scale(var(--iz)); transform-origin: 0 0;
  transition: left .09s linear, top .09s linear; will-change: left, top;
}
.rcursor svg { display: block; fill: var(--c); filter: drop-shadow(0 1px 2px rgba(0, 0, 0, .3)); }
.rname {
  position: absolute; left: 14px; top: 16px; background: var(--c); color: #fff; font: 600 11px/1 var(--font);
  padding: 4px 7px; border-radius: 6px; white-space: nowrap; box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
}
.name-dialog { max-width: 380px; padding: 26px; }
.name-dialog h2 { margin: 0; font-size: 18px; letter-spacing: -.015em; }
.name-dialog .sub { margin: 4px 0 14px; color: var(--ink-2); font-size: 13px; }
.name-dialog input { width: 100%; height: 42px; border: 1px solid var(--line-2); border-radius: 10px; padding: 0 12px; font-size: 15px; outline: 0; user-select: text; background: var(--surface); }
.name-dialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.name-dialog .row { display: flex; justify-content: flex-end; margin-top: 14px; }
.provgrid { display: flex; flex-direction: column; gap: 6px; }

/* ---------- model picker, split button, retry, annotations ---------- */
.ftr select.model {
  appearance: none; -webkit-appearance: none; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%23999' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 8px center;
  color: var(--ink-2); font: 500 11.5px var(--font); height: 28px; padding: 0 24px 0 9px; min-width: 0; max-width: 168px; text-overflow: ellipsis; cursor: pointer; outline: 0;
}
.ftr select.model:hover, .ftr select.model:focus { border-color: var(--line-2); color: var(--ink); }
.hdr .meta { font-size: 11px; font-weight: 400; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.split { display: inline-flex; border-radius: 99px; overflow: hidden; background: var(--ink); color: var(--surface); flex: none; }
.split button { display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 11px; font-size: 12.5px; font-weight: 500; color: inherit; transition: background .12s; }
.split button.sa { padding-left: 12px; border-right: 1px solid rgba(128, 128, 128, .45); }
.split button.sb { padding-right: 13px; }
.split button:hover { background: rgba(128, 128, 128, .28); }
.split button:disabled { opacity: .4; }
.errbox .row { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.errbox .btn { background: var(--danger); color: #fff; height: 26px; }
.errbox a { font-weight: 500; }
.note a { color: var(--accent); }
.md .anno, .md .anno-hover { border-radius: 4px; box-shadow: -10px 0 0 -7px var(--accent), -3px 0 0 0 transparent; background: var(--accent-soft); outline: 6px solid var(--accent-soft); }
.md .anno-hover { background: rgba(91, 91, 246, .22); outline-color: rgba(91, 91, 246, .22); }
.md mark.hl { background: rgba(245, 190, 30, .45); color: inherit; border-radius: 3px; padding: 0 1px; }
body.dragging .md, body.dragging .node { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.welcome-dialog .new-row .btn, .welcome-dialog .new-row input { height: 46px; }
@media (max-width: 760px) { .new-row input, .new-row .btn { width: 100%; justify-content: center; } }

/* ---------- rich text format bar ---------- */
#fmtbar {
  position: fixed; left: 0; top: 0; z-index: 90; display: flex; align-items: center; gap: 2px; padding: 4px; flex-wrap: wrap; max-width: min(560px, calc(100vw - 16px));
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow-lg);
}
#fmtbar .fb { min-width: 28px; height: 28px; padding: 0 6px; border-radius: 7px; display: inline-flex; align-items: center; justify-content: center; gap: 4px; color: var(--ink-2); font-size: 13px; }
#fmtbar .fb:hover { background: var(--surface-3); color: var(--ink); } #fmtbar .fb.on { background: var(--accent-soft); color: var(--accent); }
#fmtbar .fcol { width: 16px; min-width: 16px; height: 16px; border-radius: 50%; padding: 0; margin: 0 2px; border: 1.5px solid rgba(128, 128, 140, .4); }
#fmtbar .fsep { width: 1px; height: 18px; background: var(--line); margin: 0 3px; }
#fmtbar .clr { font-size: 11.5px; }
.t-shape.is-text .txt a { color: var(--accent); }

/* ---------- tutorial guide ---------- */
.w-actions { display: inline-flex; gap: 4px; }
#tut-hl {
  position: fixed; left: 0; top: 0; z-index: 120; pointer-events: none; border-radius: 12px; display: none;
  border: 2px solid var(--accent); box-shadow: 0 0 0 4px var(--accent-soft), 0 0 28px rgba(91, 91, 246, .35);
  transition: left .35s cubic-bezier(.2, .8, .2, 1), top .35s cubic-bezier(.2, .8, .2, 1), width .35s, height .35s; animation: tutpulse 1.6s ease-in-out infinite;
}
@keyframes tutpulse { 50% { box-shadow: 0 0 0 9px rgba(91, 91, 246, .1), 0 0 30px rgba(91, 91, 246, .25); } }
#tut-cursor { position: fixed; left: 0; top: 0; z-index: 130; pointer-events: none; transition: transform .85s cubic-bezier(.22, .85, .2, 1); --c: #5b5bf6; }
#tut-cursor svg { display: block; fill: var(--c); filter: drop-shadow(0 2px 4px rgba(0, 0, 0, .3)); animation: tutwiggle 2.4s ease-in-out infinite; transform-origin: 2px 2px; }
@keyframes tutwiggle { 0%, 100% { transform: rotate(0); } 50% { transform: rotate(-6deg) translate(1px, 1px); } }
#tut-cursor .rname { left: 18px; top: 20px; }
#tut-bubble {
  position: fixed; left: 0; top: 0; z-index: 130; width: 270px; padding: 13px 15px 10px; pointer-events: none; border-radius: 14px; background: var(--surface);
  border: 1px solid var(--line); box-shadow: var(--shadow-lg); transition: transform .85s cubic-bezier(.22, .85, .2, 1);
  border-left: 3px solid var(--accent); font-size: 13.5px; line-height: 1.45; user-select: none;
}
#tut-bubble.pop .tb-text { animation: tutin .3s ease-out; }
@keyframes tutin { from { opacity: 0; transform: translateY(4px); } }
#tut-bubble .tb-text b { color: var(--accent); font-weight: 600; }
#tut-bubble .tb-act { margin-top: 9px; pointer-events: auto; }
.tb-foot { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 11px; color: var(--ink-3); }
.tb-dots { display: inline-flex; gap: 3px; flex: 1; }
.tb-dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--line-2); }
.tb-dots i.d { background: var(--accent); opacity: .55; } .tb-dots i.c { background: var(--accent); }
.tb-skip { color: var(--ink-3); font-size: 11px; pointer-events: auto; text-decoration: underline; text-underline-offset: 2px; } .tb-skip:hover { color: var(--ink); }
@media (max-width: 760px) { #tut-bubble { width: min(270px, calc(100vw - 24px)); } }
@media (max-width: 760px) {
  .new-row input { flex: none; height: 46px; }
  .new-row .btn { flex: none; height: 46px; }
  .w-foot { flex-direction: column; align-items: flex-start; gap: 8px; }
  .w-actions { flex-wrap: wrap; margin-left: -10px; }
}

/* ---------- share button, calmer + consistent with the other pills ---------- */
#collab { left: 14px; top: 66px; padding: 4px; gap: 0; }
#btn-share {
  height: 36px; padding: 0 13px 0 11px; border-radius: 10px; gap: 7px; background: transparent; color: var(--ink-2); font-size: 13px; font-weight: 500;
}
#btn-share:hover { background: var(--surface-3); color: var(--ink); }
#collab.shared #btn-share { background: var(--accent-soft); color: var(--accent); }
#collab.offline #btn-share { background: rgba(245, 190, 30, .22); color: #8a6d00; }
#peers { display: flex; gap: 5px; align-items: center; padding: 0 9px 0 8px; }
#peers:empty { display: none; }

/* ---------- download menu + duplicate dialog ---------- */
.dlmenu {
  position: absolute; right: 6px; top: 38px; z-index: 5; width: 124px; padding: 4px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-lg); animation: pop .15s ease-out;
}
.dlmenu button { display: flex; align-items: baseline; justify-content: space-between; width: 100%; padding: 7px 9px; border-radius: 7px; font-size: 13px; color: var(--ink); }
.dlmenu button:hover { background: var(--surface-3); }
.dlmenu button span { font-size: 11.5px; color: var(--ink-3); }
.dup-dialog { max-width: 520px; padding: 26px; }
.dup-dialog h2 { margin: 0; font-size: 18px; letter-spacing: -.015em; }
.dup-dialog .sub { margin: 4px 0 16px; color: var(--ink-2); font-size: 13px; }
.dupgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dupcard { border: 1px solid var(--line); border-radius: 12px; padding: 12px 13px; background: var(--surface-2); }
.dupcard.newer { border-color: var(--accent); background: var(--accent-soft); }
.duptitle { font-weight: 600; font-size: 13.5px; display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; flex-wrap: wrap; }
.dupbadge { font-size: 10.5px; font-weight: 600; color: #fff; background: var(--accent); padding: 2px 7px; border-radius: 99px; }
.dupmeta { font-size: 12px; color: var(--ink-2); }
.dupnote { margin: 12px 0 14px; font-size: 12.5px; color: var(--ink-2); }
.duprow { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.duprow .btn { justify-content: center; height: 36px; }
.duprow .dupbtn { background: var(--surface-3); color: var(--ink); }
.dupfoot { margin: 12px 0 0; font-size: 11.5px; color: var(--ink-3); }

/* ---------- other people's selections ---------- */
#rsel { position: absolute; left: 0; top: 0; width: 0; height: 0; overflow: visible; }
.rsel { position: absolute; border: calc(2px * var(--iz)) solid var(--c); border-radius: 9px; pointer-events: none; background: color-mix(in srgb, var(--c) 9%, transparent); transition: left .08s linear, top .08s linear, width .08s linear, height .08s linear; }
.rsel span {
  position: absolute; left: calc(-2px * var(--iz)); bottom: 100%; margin-bottom: calc(3px * var(--iz)); background: var(--c); color: #fff;
  font: 600 11px/1 var(--font); padding: 3px 6px; border-radius: 5px; white-space: nowrap; transform: scale(var(--iz)); transform-origin: 0 100%;
}
.edge.rsel-edge .ln, .edge.rsel-edge .hd, .edge.rsel-edge .tl { stroke: var(--rc) !important; stroke-width: 3.2px; }
