:root {
  --navy: #102A3B; --blue: #2D82B5; --mist: #E6F0F5; --cloud: #F4F8FA; --paper: #FAFCFD;
  --ink: #1E303B; --muted: #617580; --line: #D5E1E7; --bad: #C0392B; --ok: #16AB2D;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 Inter, system-ui, sans-serif; color: var(--ink); background: var(--paper); }
.hidden { display: none !important; }
.dim { color: var(--muted); }

/* login */
#login-view { display: flex; min-height: 100vh; align-items: center; justify-content: center; background: linear-gradient(135deg, var(--navy), #173D55); }
.login-card { width: 360px; background: #fff; border-radius: 10px; padding: 28px; box-shadow: 0 24px 60px rgba(0,0,0,.35); }
.brand { font-weight: 800; letter-spacing: .06em; color: var(--navy); font-size: 18px; }
.brand span { font-weight: 500; color: var(--muted); font-size: 13px; }
.login-card h3 { margin: 6px 0 18px; font-size: 13px; color: var(--muted); font-weight: 500; }
.login-card label { display: block; margin-bottom: 12px; }
.login-card label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; background: #fff; }
#login-msg { color: var(--bad); font-size: 12px; min-height: 16px; margin: 8px 0 0; }

/* buttons */
.btn { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 7px 14px; font: inherit; font-weight: 600; cursor: pointer; color: var(--ink); }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.small { padding: 4px 10px; font-size: 12px; }

/* topbar */
.topbar { display: flex; gap: 10px; align-items: center; padding: 8px 14px; background: #fff; border-bottom: 1px solid var(--line); }
.brand.small { font-size: 14px; }
.topbar select { width: 260px; }
.tabs { display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.tab { border: 0; background: #fff; padding: 7px 14px; font: inherit; cursor: pointer; }
.tab.active { background: var(--navy); color: #fff; }
.spacer { flex: 1; }

/* layout */
.layout { display: flex; height: calc(100vh - 53px); }
.palette { width: 210px; background: #fff; border-right: 1px solid var(--line); overflow-y: auto; padding: 10px; }
.pal-group { margin-bottom: 14px; }
.pal-title { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 6px; }
.pal-item { display: flex; gap: 8px; align-items: center; border: 1px solid var(--line); border-left: 3px solid var(--c); border-radius: 6px; padding: 6px 8px; margin-bottom: 6px; cursor: grab; background: var(--cloud); font-size: 12.5px; }
.pal-item:active { cursor: grabbing; }
.pal-icon { filter: grayscale(.2); }

/* canvas */
.canvas-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.canvas-tools { display: flex; gap: 8px; align-items: center; padding: 6px 10px; background: var(--cloud); border-bottom: 1px solid var(--line); }
.hint { font-size: 12px; color: var(--muted); }
#canvas { position: relative; flex: 1; overflow: auto; background: radial-gradient(circle, #dbe7ee 1px, transparent 1px) 0 0/22px 22px, var(--paper); }
#wires { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1; }
#wires.wires-top { z-index: 4; }
#wires path { stroke: #7d97ab; stroke-width: 1.6; }
#wires path.agg { stroke-dasharray: 5 4; stroke: #2D82B5; }
#wires text { font-size: 10px; fill: var(--muted); }
#wires circle { fill: #7d97ab; pointer-events: auto; cursor: pointer; }

/* groups */
.group-box { position: absolute; border: 1.5px dashed #9db8ca; border-radius: 12px; background: rgba(45,130,181,.045); z-index: 0; }
.group-head { display: flex; align-items: center; gap: 6px; padding: 4px 10px; font-size: 11px; font-weight: 700; color: var(--navy); letter-spacing: .04em; }
.group-toggle { border: 1px solid var(--line); background: #fff; border-radius: 4px; width: 18px; height: 18px; cursor: pointer; line-height: 1; }
.group-count { background: var(--navy); color: #fff; border-radius: 8px; padding: 0 7px; font-size: 10px; }
.group-chip { border-left: 4px solid var(--navy); cursor: pointer; background: #FDFEFF; z-index: 2; }
.node { position: absolute; width: 168px; background: #fff; border: 1px solid var(--line); border-top: 3px solid var(--c); border-radius: 8px; box-shadow: 0 3px 10px rgba(16,42,59,.10); padding: 8px 10px; cursor: grab; user-select: none; z-index: 2; }
.node.dim { z-index: 2; }
.node.focus-node, .node.sel { z-index: 5; }
.group-box.dim { opacity: .35; }
.node.sel { outline: 2px solid var(--blue); }
.node.captured { background: #F2F8F2; }
.node-type { font-size: 10px; text-transform: uppercase; letter-spacing: .07em; color: var(--c); font-weight: 700; }
.node-label { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-counts { font-size: 10px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-x { position: absolute; top: 2px; right: 4px; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 11px; }

/* props */
.props { width: 240px; background: #fff; border-left: 1px solid var(--line); padding: 12px; overflow-y: auto; }
.props h4 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; color: var(--muted); }
.props label { display: block; margin-bottom: 8px; }
.props label span { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; }

/* chat */
.chat { width: 320px; background: #fff; border-left: 1px solid var(--line); display: flex; flex-direction: column; }
.chat-head { padding: 10px 12px; font-weight: 700; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.chat-log { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.msg { padding: 8px 10px; border-radius: 8px; font-size: 13px; white-space: pre-wrap; max-width: 92%; }
.msg.user { align-self: flex-end; background: var(--blue); color: #fff; }
.msg.assistant { align-self: flex-start; background: var(--mist); }
.chat-suggest { border-top: 1px solid var(--line); padding: 10px; font-size: 12.5px; display: flex; flex-direction: column; gap: 6px; }
#chat-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }
#chat-input { flex: 1; }

/* toast */
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--navy); color: #fff; padding: 9px 18px; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.3); z-index: 99; }
.toast.bad { background: var(--bad); }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(16,42,59,.45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { width: 380px; background: #fff; border-radius: 10px; padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal-card h3 { margin: 0 0 14px; font-size: 15px; color: var(--navy); }
.modal-card label { display: block; margin-bottom: 10px; }
.modal-card label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* system colors + focus */
.group-box { border-color: var(--sys, #9db8ca); background: color-mix(in srgb, var(--sys, #9db8ca) 6%, transparent); }
.group-box.focus { border-width: 2.5px; box-shadow: 0 0 0 4px color-mix(in srgb, var(--sys) 18%, transparent); }
.group-icon { font-size: 13px; }
.group-inspect { border: 1px solid var(--line); background: #fff; border-radius: 4px; width: 18px; height: 18px; cursor: pointer; line-height: 1; font-size: 10px; margin-left: auto; }
.group-inspect:hover { background: var(--mist); }
.node.dim { opacity: .28; }
#wires path.dim { opacity: .18; }
#wires path.hot { stroke: var(--blue); stroke-width: 2.6; }

/* connections panel */
.conn-panel { position: absolute; right: 12px; top: 44px; width: 300px; max-height: calc(100% - 60px); overflow: auto; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 34px rgba(16,42,59,.22); z-index: 30; }
.conn-head { display: flex; gap: 8px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.conn-head b { font-size: 13px; }
.conn-head .dim { font-size: 11px; }
#conn-close { margin-left: auto; border: 0; background: none; cursor: pointer; color: var(--muted); }
.conn-body { padding: 8px 12px 12px; font-size: 12.5px; }
.conn-k { color: var(--muted); margin: 4px 0 8px; }
.conn-h { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--navy); margin: 10px 0 4px; }
.conn-panel ul { list-style: none; margin: 0; padding: 0; }
.conn-panel li { display: flex; align-items: center; gap: 6px; padding: 3px 0; }
.conn-panel .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.conn-panel .to { color: var(--muted); font-size: 11px; margin-left: auto; text-align: right; }

/* node focus */
.node.focus-node { outline: 2.5px solid var(--blue); box-shadow: 0 0 0 5px rgba(45,130,181,.18); }
.group-chat { border: 1px solid var(--line); background: #fff; border-radius: 4px; width: 18px; height: 18px; cursor: pointer; line-height: 1; font-size: 10px; margin-left: 2px; }
.group-chat:hover { background: var(--mist); }
/* props AI */
.node-ai { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 8px; }
.node-ai-log { max-height: 180px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; margin: 6px 0; }
.node-ai-log .msg { font-size: 12px; padding: 6px 8px; border-radius: 6px; white-space: pre-wrap; }
#p-ai-form { display: flex; gap: 6px; }
#p-ai-apply { margin-top: 6px; width: 100%; }
/* versions */
.ver-list { list-style: none; margin: 0 0 10px; padding: 0; max-height: 300px; overflow-y: auto; }
.ver-list li { display: flex; align-items: center; gap: 8px; padding: 7px 4px; border-bottom: 1px solid var(--line); font-size: 13px; }
.ver-list .ver-k { color: var(--muted); font-size: 11.5px; }
.ver-list li .btn { margin-left: auto; }

/* popout window (node AI) */
.popout { position: fixed; z-index: 60; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 24px 60px rgba(16,42,59,.35); display: flex; flex-direction: column; min-width: 420px; min-height: 300px; }
.popout-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: grab; user-select: none; border-radius: 10px 10px 0 0; background: var(--cloud); }
.popout-head:active { cursor: grabbing; }
.popout-head b { font-size: 13px; }
.nodeai-sub { font-size: 11px; }
#nodeai-close { margin-left: auto; border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 13px; }
.popout-body { flex: 1; display: flex; flex-direction: column; gap: 8px; padding: 12px; min-height: 0; }
.popout .node-ai-log { flex: 1; max-height: none; overflow-y: auto; }
.popout .msg { font-size: 13px; padding: 8px 11px; border-radius: 8px; white-space: pre-wrap; line-height: 1.5; }
.nodeai-form { display: flex; gap: 8px; }
.popout-resize { position: absolute; right: 0; bottom: 0; width: 18px; height: 18px; cursor: nwse-resize; background: linear-gradient(135deg, transparent 50%, var(--line) 50%); border-radius: 0 0 10px 0; }
#p-ai-popout { border: 1px solid var(--line); background: #fff; border-radius: 4px; cursor: pointer; font-size: 10px; padding: 1px 5px; margin-left: 6px; }
#p-ai-popout:hover { background: var(--mist); }
.group-box { }
.group-head { cursor: grab; }
.group-box.dragging, .group-box.dragging .group-head { cursor: grabbing; }
.group-box.dragging { border-style: solid; box-shadow: 0 10px 30px rgba(16,42,59,.25); }
