:root {
  --bg: #0f1216; --panel: #161a20; --panel2: #1c2129; --line: #2a313b; --text: #e7ebf0; --muted: #8b95a3;
  --accent: #22c55e; --accent2: #16a34a; --blue: #3b82f6; --amber: #f59e0b; --red: #ef4444; --violet: #a78bfa; --cyan: #22d3ee;
  --radius: 10px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .xs { font-size: 11px; } .grow { flex: 1; }
.err { color: var(--red); min-height: 1.2em; }
button { font: inherit; color: inherit; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; width: 100%; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 80px; }
textarea.mono, .mono { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
label { display: block; margin: 10px 0 4px; color: var(--muted); font-size: 12.5px; }
label > input, label > textarea, label > select { margin-top: 4px; color: var(--text); font-size: 14px; }
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #3a4350; }
.btn.primary { background: var(--accent2); border-color: var(--accent2); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { background: #3a1416; border-color: #6b1d22; color: #fecaca; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.row.wrap { flex-wrap: wrap; }

/* Đăng nhập */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; }
.login-box { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 12px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand.big { font-size: 22px; }
.logo { display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 7px; background: var(--accent); color: #fff; font-size: 12px; }

/* Khung chính */
#app { display: flex; flex-direction: column; height: 100%; }
.top { display: flex; align-items: center; gap: 16px; padding: 8px 14px; border-bottom: 1px solid var(--line); background: var(--panel); }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab { background: none; border: 0; padding: 7px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--panel2); color: var(--text); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--amber); color: #111; font-size: 11px; font-weight: 700; text-align: center; }
.view { flex: 1; display: flex; min-height: 0; }
.side { width: 250px; flex-shrink: 0; border-right: 1px solid var(--line); background: var(--panel); overflow-y: auto; padding-bottom: 20px; }
.side-h { display: flex; justify-content: space-between; align-items: center; padding: 12px 12px 6px; font-weight: 600; font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.list { list-style: none; margin: 0; padding: 0 6px; }
.list li { padding: 8px 10px; border-radius: 8px; cursor: pointer; margin-bottom: 2px; }
.list li:hover { background: var(--panel2); }
.list li.active { background: #20303a; box-shadow: inset 3px 0 0 var(--accent); }
.list li .sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list li .t { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }

/* Bảng khối */
.palette { display: grid; gap: 6px; padding: 4px 10px; }
.pal { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel2); cursor: grab; user-select: none; }
.pal .ic { font-size: 18px; }
.pal .d { font-size: 11.5px; color: var(--muted); }

/* Sơ đồ */
.canvas-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.toolbar { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--panel); }
.name-input { max-width: 340px; font-weight: 600; background: transparent; border-color: transparent; }
.name-input:hover { border-color: var(--line); }
.dirty { color: var(--amber); font-size: 12px; }
#drawflow, .run-graph { flex: 1; position: relative; overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(#252b34 1px, transparent 1px); background-size: 22px 22px; }
.menu { position: absolute; right: 120px; top: 44px; z-index: 20; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 4px; display: grid; min-width: 200px; box-shadow: 0 10px 30px #0008; }
.menu button { background: none; border: 0; text-align: left; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--panel); }
.menu .danger { color: #fca5a5; }

/* Khối trong Drawflow */
.drawflow .drawflow-node {
  width: 220px; min-height: 0; padding: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--text); box-shadow: 0 4px 14px #0006;
}
.drawflow .drawflow-node.selected { background: var(--panel); border-color: var(--accent); box-shadow: 0 0 0 2px #22c55e55, 0 4px 14px #0006; }
.drawflow .drawflow-node .input, .drawflow .drawflow-node .output { width: 14px; height: 14px; background: var(--panel2); border: 2px solid #56606e; }
.drawflow .drawflow-node .input { left: -8px; } .drawflow .drawflow-node .output { right: -8px; }
.drawflow .drawflow-node .output:hover, .drawflow .drawflow-node .input:hover { background: var(--accent); border-color: var(--accent); }
.drawflow .connection .main-path { stroke: #4b5563; stroke-width: 3px; }
.drawflow .connection .main-path:hover, .drawflow .connection .main-path.selected { stroke: var(--accent); }
.drawflow-delete { background: var(--red); border: 0; color: #fff; line-height: 28px; }
.drawflow_content_node { width: 100%; }
.nd { border-top: 3px solid var(--c, var(--muted)); border-radius: var(--radius); padding: 9px 11px; }
.nd-h { display: flex; gap: 7px; align-items: center; font-weight: 600; font-size: 13.5px; }
.nd-h .t { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.nd-s { margin-top: 3px; font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nd .st { margin-left: auto; font-size: 13px; }
.drawflow-node.start .nd { --c: var(--accent); }
.drawflow-node.ai .nd { --c: var(--violet); }
.drawflow-node.script .nd { --c: var(--cyan); }
.drawflow-node.approve .nd { --c: var(--amber); }
.drawflow-node.flow .nd { --c: var(--blue); }
.drawflow-node.off { opacity: .45; }
.drawflow-node.off .nd-h .t { text-decoration: line-through; }
/* trạng thái khi chạy */
.drawflow-node.st-running { border-color: var(--blue); box-shadow: 0 0 0 3px #3b82f655; animation: pulse 1.6s infinite; }
.drawflow-node.st-done { border-color: #1f7a45; }
.drawflow-node.st-waiting { border-color: var(--amber); box-shadow: 0 0 0 3px #f59e0b55; }
.drawflow-node.st-error { border-color: var(--red); box-shadow: 0 0 0 3px #ef444455; }
.drawflow-node.st-skipped { opacity: .4; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px #3b82f622; } }

/* Bảng thuộc tính */
.inspector { width: 360px; flex-shrink: 0; border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto; padding: 14px 16px 40px; }
.inspector h3 { margin: 0 0 4px; display: flex; gap: 8px; align-items: center; }
.inspector .help { font-size: 12px; color: var(--muted); background: var(--bg); border: 1px dashed var(--line); border-radius: 8px; padding: 8px 10px; margin-top: 12px; }
.inspector .help code { color: var(--cyan); }
.check { display: flex; gap: 8px; align-items: center; margin-top: 12px; color: var(--text); font-size: 13.5px; }
.check input { width: auto; }
textarea.prompt { min-height: 260px; }

/* Lần chạy */
.run-main { flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.run-head { padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.run-head h2 { margin: 0; font-size: 17px; }
.run-body { flex: 1; display: flex; min-height: 0; }
.run-left { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.run-graph { min-height: 220px; max-height: 42%; border-bottom: 1px solid var(--line); }
.steps { flex: 1; overflow-y: auto; padding: 8px; }
.step { display: flex; gap: 10px; align-items: center; padding: 9px 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.step:hover { background: var(--panel2); }
.step.active { background: #20303a; border-color: #2c4554; }
.step .ic { width: 22px; text-align: center; }
.step .t { flex: 1; min-width: 0; }
.step .sub { font-size: 12px; color: var(--muted); }
.detail { width: 48%; min-width: 360px; border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.detail-h { padding: 12px 14px; border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.detail-h h3 { margin: 0; font-size: 15px; flex: 1; }
.detail-b { flex: 1; overflow-y: auto; padding: 12px 14px; }
.log { background: #0b0d10; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: 12px/1.5 ui-monospace, Consolas, monospace; white-space: pre-wrap; word-break: break-word; max-height: 420px; overflow-y: auto; }
.log .tool { color: var(--cyan); } .log .bad { color: #fca5a5; } .log .ok { color: #86efac; }
.box { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 12px; background: var(--panel2); }
.box.wait { border-color: #6b4d12; background: #221a0b; }
.box.bad { border-color: #6b1d22; background: #220f10; }
.box h4 { margin: 0 0 8px; }
.result { white-space: pre-wrap; font-size: 13px; }
.pill { display: inline-flex; gap: 5px; align-items: center; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--panel2); border: 1px solid var(--line); }
.pill.running { color: #93c5fd; border-color: #1e3a8a; } .pill.waiting { color: #fcd34d; border-color: #78350f; }
.pill.done { color: #86efac; border-color: #14532d; } .pill.error, .pill.stopped { color: #fca5a5; border-color: #7f1d1d; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin-top: 10px; }
.thumbs a { display: block; } .thumbs img { width: 100%; aspect-ratio: 16/9; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }
.thumbs .cap { font-size: 11px; color: var(--muted); margin-top: 2px; }
.files { display: grid; gap: 4px; margin-top: 6px; }
.files a { color: #93c5fd; text-decoration: none; font-size: 13px; }
video { width: 100%; border-radius: 8px; background: #000; margin-top: 8px; }
.empty { margin: auto; color: var(--muted); padding: 30px; text-align: center; }

/* Cài đặt */
.settings { flex: 1; overflow-y: auto; padding: 20px; display: grid; gap: 16px; align-content: start; max-width: 760px; margin: 0 auto; width: 100%; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; }
.card h2 { margin: 0 0 6px; font-size: 17px; } .card h3 { margin: 18px 0 4px; font-size: 14px; }
details { margin-top: 10px; } summary { cursor: pointer; color: var(--muted); }
.flow-status { margin: 10px 0; padding: 10px 12px; border-radius: 8px; background: var(--bg); border: 1px solid var(--line); font-size: 13px; }
.flow-status.ok { border-color: #14532d; color: #86efac; } .flow-status.bad { border-color: #78350f; color: #fcd34d; }

/* Hộp thoại + thông báo */
.modal { position: fixed; inset: 0; background: #000a; display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; }
.modal-box h2 { margin: 0 0 8px; font-size: 18px; }
.tpl { display: block; width: 100%; text-align: left; padding: 10px 12px; margin-top: 8px; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.tpl:hover { border-color: var(--accent); }
.tpl small { display: block; color: var(--muted); margin-top: 2px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #111827; border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px; z-index: 60; box-shadow: 0 10px 30px #0008; max-width: calc(100% - 32px); }
.toast.bad { border-color: var(--red); }

/* Điện thoại / màn hình hẹp */
@media (max-width: 1100px) {
  .inspector { width: 300px; }
  .detail { min-width: 300px; }
}
@media (max-width: 860px) {
  .top { gap: 8px; padding: 8px 10px; } .brand-t { display: none; }
  .tab { padding: 7px 9px; }
  .view { flex-direction: column; }
  .side { width: 100%; max-height: 32vh; border-right: 0; border-bottom: 1px solid var(--line); }
  #view-editor .side { max-height: 26vh; }
  .palette { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  .pal .d { display: none; }
  .inspector { position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-height: 62vh; z-index: 30; border-left: 0; border-top: 2px solid var(--accent); box-shadow: 0 -10px 30px #0009; }
  .inspector.empty-insp { display: none; }
  .toolbar { flex-wrap: wrap; } .name-input { max-width: none; flex-basis: 100%; }
  .run-body { flex-direction: column; overflow-y: auto; }
  .run-graph { display: none; }
  .steps { flex: none; overflow: visible; }
  .detail { width: auto; min-width: 0; border-left: 0; border-top: 1px solid var(--line); }
  .detail-b { overflow: visible; }
  .menu { right: 10px; }
  .settings { padding: 12px; }
}
