:root {
  --bg: #0f1115; --panel: #171a21; --panel2: #1e222b; --line: #2a2f3a; --text: #e6e8ee; --muted: #8b93a5;
  --accent: #ff7a59; --ok: #22c55e; --err: #ef4444; --warn: #f59e0b;
  --t-image: #3b82f6; --t-video: #f59e0b; --t-audio: #ec4899; --t-text: #94a3b8; --t-any: #ef4444;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-size: 13px; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
button:hover { border-color: #3b4252; background: #252a35; }
button.primary { background: var(--accent); border-color: var(--accent); color: #111; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: #fca5a5; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { background: #11141a; border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; width: 100%; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 54px; }
label.field { display: block; margin: 6px 0; color: var(--muted); font-size: 11.5px; }
label.field > span { display: block; margin-bottom: 3px; }
label.check { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 12px; margin: 6px 0; }
label.check input { width: auto; }

/* Đăng nhập */
.login { height: 100%; display: grid; place-items: center; background: radial-gradient(circle at 30% 20%, #1d2333, var(--bg)); }
.login form { width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 18px; color: var(--muted); }
.login .err { color: #fca5a5; min-height: 18px; margin-top: 8px; }
.login button { width: 100%; margin-top: 10px; padding: 9px; }

/* Khung chính */
.shell { display: grid; grid-template-rows: 48px minmax(0, 1fr); grid-template-columns: 220px minmax(0, 1fr) 300px; height: 100vh; overflow: hidden; }
.shell > * { min-height: 0; }
.topbar { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; padding: 0 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar .brand { font-weight: 700; letter-spacing: .3px; margin-right: 8px; }
.topbar .brand b { color: var(--accent); }
.topbar input.wfname { width: 220px; background: transparent; border-color: transparent; font-weight: 600; }
.topbar input.wfname:hover, .topbar input.wfname:focus { border-color: var(--line); }
.topbar .spacer { flex: 1; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: #3a2a10; color: #fbbf24; border: 1px solid #5c4413; }
.palette { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 10px; }
.palette h4 { margin: 14px 4px 6px; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .6px; }
.palette .item { display: flex; align-items: center; gap: 8px; padding: 7px 9px; border-radius: 8px; cursor: grab; border: 1px solid transparent; user-select: none; }
.palette .item:hover { background: var(--panel2); border-color: var(--line); }
.palette .dot { width: 9px; height: 9px; border-radius: 3px; flex: none; }
.palette .paid { margin-left: auto; font-size: 10px; color: var(--muted); }
.palette .hint { color: var(--muted); font-size: 11.5px; line-height: 1.5; margin: 16px 4px; }

.side { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 12px; }
.side h3 { margin: 4px 0 10px; font-size: 13px; }
.side .empty { color: var(--muted); font-size: 12px; line-height: 1.6; }
.result { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px; margin-bottom: 10px; }
.result .meta { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 11px; margin-bottom: 6px; }
.result img, .result video { width: 100%; border-radius: 6px; display: block; background: #000; }
.result audio { width: 100%; }
.result a { color: var(--accent); text-decoration: none; font-size: 11px; }
.runlog { font-size: 12px; margin-top: 14px; }
.runlog div { padding: 4px 0; border-bottom: 1px dashed var(--line); display: flex; gap: 6px; }
.runlog .st { width: 14px; text-align: center; }

/* Canvas */
.canvas { position: relative; overflow: hidden; background-color: var(--bg);
  background-image: radial-gradient(circle, #232733 1px, transparent 1px); background-size: 22px 22px; cursor: default; }
.canvas.panning { cursor: grabbing; }
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
svg.wires { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
svg.wires path { fill: none; stroke-width: 2.5; pointer-events: stroke; cursor: pointer; }
svg.wires path:hover { stroke-width: 5; }
svg.wires path.temp { stroke-dasharray: 6 4; pointer-events: none; }
.zoomctl { position: absolute; right: 12px; bottom: 12px; display: flex; gap: 6px; z-index: 5; }
.zoomctl button { padding: 4px 10px; }

.node { position: absolute; width: 270px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
.node.selected { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255,122,89,.25), 0 8px 24px rgba(0,0,0,.35); }
.node .head { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 12px 12px 0 0; cursor: move; user-select: none; border-bottom: 1px solid var(--line); }
.node .head .bar { width: 4px; height: 16px; border-radius: 2px; }
.node .head .title { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.node .head .x { background: none; border: none; color: var(--muted); padding: 0 4px; font-size: 15px; }
.node .head .x:hover { color: var(--err); }
.node .status { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--panel2); color: var(--muted); }
.node.st-running { border-color: var(--warn); }
.node.st-running .status { color: var(--warn); animation: pulse 1.2s infinite; }
.node.st-done .status { color: var(--ok); }
.node.st-error { border-color: var(--err); }
.node.st-error .status, .node.st-skipped .status { color: #fca5a5; }
@keyframes pulse { 50% { opacity: .4; } }
.node .ports { display: flex; justify-content: space-between; padding: 6px 0; }
.node .ins, .node .outs { display: flex; flex-direction: column; gap: 4px; }
.port { display: flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--muted); position: relative; height: 18px; }
.ins .port { padding-left: 10px; } .outs .port { padding-right: 10px; flex-direction: row-reverse; }
.port .pin { width: 12px; height: 12px; border-radius: 50%; border: 2px solid; background: var(--bg); cursor: crosshair; position: absolute; }
.ins .port .pin { left: -7px; } .outs .port .pin { right: -7px; }
.ins .port { padding-left: 12px; } .outs .port { padding-right: 12px; }
.port .pin.connected { background: currentColor; }
.port .pin.hot { transform: scale(1.4); }
.port.req::after { content: '*'; color: var(--accent); }
.node .body { padding: 2px 10px 10px; }
.node .msg { font-size: 11px; color: var(--muted); margin-top: 6px; word-break: break-word; }
.node .msg.err { color: #fca5a5; }
.node .preview { margin-top: 8px; }
.node .preview img, .node .preview video { width: 100%; border-radius: 6px; display: block; max-height: 170px; object-fit: contain; background: #000; }
.node .preview audio { width: 100%; height: 32px; }
.assetpick { display: flex; gap: 6px; }
.assetpick button { flex: 1; padding: 5px 6px; font-size: 12px; }
.assetthumb { margin-top: 6px; }
.assetthumb img, .assetthumb video { width: 100%; max-height: 180px; object-fit: contain; border-radius: 6px; background: #000; }
.assetthumb audio { width: 100%; height: 32px; }

/* Menu chuột phải */
.ctxmenu { position: fixed; z-index: 50; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 4px; min-width: 180px; box-shadow: 0 10px 30px rgba(0,0,0,.5); }
.ctxmenu div { padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.ctxmenu div:hover { background: #2a3040; }

/* Hộp thoại */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 40; }
.modal { width: min(760px, 94vw); max-height: 86vh; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; }
.modal h2 { margin: 0 0 12px; font-size: 16px; display: flex; justify-content: space-between; }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.tabs button.on { border-color: var(--accent); color: var(--accent); }
table { width: 100%; border-collapse: collapse; }
td, th { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 11.5px; }
.row { display: flex; gap: 8px; align-items: center; }
.ok { color: var(--ok); } .no { color: var(--muted); }
.note { color: var(--muted); font-size: 12px; line-height: 1.55; margin: 8px 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.lib { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
.lib .cell { border: 1px solid var(--line); border-radius: 8px; padding: 4px; cursor: pointer; background: var(--panel2); }
.lib .cell:hover { border-color: var(--accent); }
.lib img, .lib video { width: 100%; height: 100px; object-fit: cover; border-radius: 6px; display: block; }
.lib .cap { font-size: 10.5px; color: var(--muted); padding: 3px 2px 0; }

.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: var(--panel2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: 80vw; }
.toast.err { border-color: var(--err); color: #fecaca; }
