/* ═══════════════ 流程图实验室 全局样式 ═══════════════ */
:root {
  --blue: #4472C4;
  --blue-dark: #223962;
  --amber: #f59e0b;
  --red: #dc2626;
  --green: #16a34a;
  --ink: #1f2937;
  --line: #d5dbe5;
  --bg: #f3f5f9;
  --panel: #ffffff;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  font-size: 13px;
  color: var(--ink);
  background: var(--bg);
}

button {
  font: inherit;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  transition: background .12s, border-color .12s, box-shadow .12s;
  white-space: nowrap;
}
button:hover { background: #eef2fb; border-color: #b9c6e2; }
button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: default; }
button.primary {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
  font-weight: 600;
}
button.primary:hover { background: #3561b3; }
button.ghost { background: transparent; }
button.mini { padding: 2px 8px; font-size: 12px; }

/* ═══════════════ 顶栏 ═══════════════ */
#toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 44px;
  padding: 0 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: none;
  overflow-x: auto;
  overflow-y: hidden;
}
.tb-brand { font-weight: 700; font-size: 14px; margin-right: 4px; flex: none; white-space: nowrap; }
.tb-brand .logo { color: var(--blue); }
.tb-group { display: flex; align-items: center; gap: 3px; flex: none; }
.tb-group::before, .tb-group::after {
  content: ""; width: 1px; height: 20px; background: var(--line); margin: 0 4px;
}
.tb-group:first-of-type::before { display: none; }
.tb-group.view::after { display: none; }
.tb-spacer { flex: 1; }
#toolbar select {
  font: inherit; padding: 4px 4px; border: 1px solid var(--line);
  border-radius: 6px; background: #fff; max-width: 140px;
}
#toolbar button { padding: 4px 8px; }
#zoomLabel { min-width: 40px; text-align: center; color: #6b7280; flex: none; }
#exportMenu { position: relative; flex: none; }
#exportMenu summary {
  list-style: none;
  cursor: pointer;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 8px;
  white-space: nowrap;
}
#exportMenu summary::-webkit-details-marker { display: none; }
#exportMenu[open] summary { background: #eef2fb; border-color: #b9c6e2; }
.export-options {
  position: absolute;
  top: calc(100% + 5px);
  right: 0;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 128px;
  padding: 6px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(15, 23, 42, .16);
}
.export-options button { text-align: left; }

/* ═══════════════ 主体三栏 ═══════════════ */
#main { display: flex; height: calc(100% - 44px - 28px); }

/* —— 左：积木 —— */
#palette {
  width: 168px;
  flex: none;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 10px;
  overflow-y: auto;
}
#palette .palette-head { display: flex; align-items: center; justify-content: space-between; }
#palette .palette-head h2 { margin: 8px 0 6px; }
#btnTogglePalette { padding: 1px 7px; color: #64748b; }
#palette h2 { font-size: 12px; color: #6b7280; margin: 8px 0 6px; }
.pal-tip { font-size: 11px; color: #9ca3af; margin-bottom: 8px; }
.pal-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; margin-bottom: 6px;
  border: 1px solid var(--line); border-radius: 8px;
  background: #fff; cursor: grab; user-select: none;
  transition: box-shadow .12s, border-color .12s, transform .12s;
}
.pal-item:hover {
  border-color: var(--blue);
  box-shadow: 0 2px 8px rgba(68, 114, 196, .25);
  transform: translateY(-1px);
}
.pal-item:active { cursor: grabbing; }
.pal-item svg { flex: none; }
.pal-item .pal-name { font-size: 12px; }
.help-list { list-style: none; font-size: 11px; color: #6b7280; line-height: 1.9; }
.help-list b { color: var(--ink); }

/* —— 中：画布 —— */
#canvasWrap { flex: 1; position: relative; overflow: hidden; background: #fbfcfe; }
#svg { width: 100%; height: 100%; display: block; cursor: default; touch-action: none; }
/* 拖拽/框选时禁止浏览器原生文字划选（否则节点文字被批量选中高亮）；行内编辑框除外 */
#canvasWrap, #palette { user-select: none; -webkit-user-select: none; }
#canvasWrap #nodeEditor { user-select: text; -webkit-user-select: text; }
#svg.panning { cursor: grabbing; }
#svg.connecting { cursor: crosshair; }

/* SVG 内的节点/线（大部分样式走属性，这里放交互态） */
g.node { cursor: move; }
g.node.collide .shape-outline { stroke: var(--red) !important; stroke-width: 2.5 !important; }
g.node.selected .shape-outline { stroke: var(--amber) !important; stroke-width: 2.5 !important; }
g.node.flash .shape-outline { animation: flashStroke 1s ease 2; }
@keyframes flashStroke {
  0%, 100% { stroke: inherit; }
  50% { stroke: var(--red); stroke-width: 3; }
}
g.node.running .shape-outline {
  stroke: var(--amber) !important;
  stroke-width: 3 !important;
  filter: drop-shadow(0 0 6px rgba(245, 158, 11, .9));
}
path.edge { fill: none; pointer-events: none; }
path.edge-hit { fill: none; stroke: transparent; stroke-width: 14; cursor: pointer; }
g.edge-g.selected path.edge { stroke: var(--amber); stroke-width: 2.4; }
g.edge-g.selected path.edge-hit { stroke: rgba(245, 158, 11, .15); }
g.edge-g.active path.edge { stroke: var(--amber); stroke-width: 3; }
g.edge-g path.edge-bad { stroke: var(--red) !important; stroke-dasharray: 5 4; }
g.edge-label { cursor: pointer; }
g.edge-label rect { fill: #fff; stroke: #4472C4; stroke-width: 1.2; }
g.edge-label text { font-size: 12px; font-weight: 700; fill: var(--blue-dark); }
g.edge-label:hover rect { fill: #eef2fb; }
circle.port {
  fill: #fff; stroke: var(--blue); stroke-width: 2; r: 5.5;
  cursor: crosshair;
}
circle.port:hover { fill: var(--blue); }
circle.wire-handle {
  fill: #fff; stroke: var(--amber); stroke-width: 2.5;
  cursor: grab; filter: drop-shadow(0 1px 2px rgba(0,0,0,.3));
}
rect.wire-handle {
  fill: #fff; stroke: var(--amber); stroke-width: 2;
  cursor: crosshair; filter: drop-shadow(0 1px 2px rgba(0,0,0,.3));
}
circle.seg-handle {
  fill: #fff; stroke: var(--amber); stroke-width: 2;
  cursor: move; filter: drop-shadow(0 1px 2px rgba(0,0,0,.25));
}
path.seg-handle {
  fill: #fff; stroke: #b45309; stroke-width: 1.8;
  cursor: move; filter: drop-shadow(0 1px 2px rgba(0,0,0,.25));
}
#rubber { fill: rgba(68, 114, 196, .08); stroke: var(--blue); stroke-dasharray: 4 3; }
#tempEdge { stroke: var(--blue); stroke-width: 2; stroke-dasharray: 6 4; fill: none; }

/* ─── 连线模式 ─── */
#svg.connect-mode { cursor: crosshair; }
#svg.connect-mode g.node { cursor: pointer; }
#connectPreview { stroke: var(--amber); stroke-width: 2; stroke-dasharray: 6 4; fill: none; pointer-events: none; }
g.node.connect-src .shape-outline {
  stroke: var(--amber) !important;
  stroke-width: 3 !important;
  filter: drop-shadow(0 0 8px rgba(245, 158, 11, .85));
}
g.node.connect-hover .shape-outline { stroke: var(--amber) !important; stroke-width: 2.5 !important; }
button.active-tool { background: #eef2fb; border-color: var(--blue); color: var(--blue-dark); font-weight: 700; }

/* ─── 拖动虚像 ─── */
.ghost-g { pointer-events: none; }
.ghost-shape { opacity: .78; }
.ghost-shape .shape-outline { stroke-dasharray: 7 4; }
.ghost-g.collide .ghost-shape .shape-outline {
  stroke: var(--red) !important;
  stroke-dasharray: 7 4;
}
.ghost-stub { stroke: var(--amber); stroke-width: 1.5; stroke-dasharray: 5 4; opacity: .75; }
.guide-line { stroke: var(--amber); stroke-width: 1; stroke-dasharray: 4 4; opacity: .95; pointer-events: none; }

/* 端口呼吸（提高可发现性） */
@keyframes portPulse { 0%, 100% { r: 5.5; } 50% { r: 7.5; } }
circle.port { animation: portPulse 1.8s ease-in-out infinite; }

/* 节点浮动工具条 */
#nodeToolbar {
  position: absolute;
  display: flex; gap: 2px;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  padding: 2px; box-shadow: 0 4px 14px rgba(0, 0, 0, .14);
  z-index: 20;
}
#nodeToolbar button { border: none; background: transparent; font-size: 14px; padding: 4px 7px; }
#nodeToolbar button:hover { background: #eef2fb; border-radius: 6px; }
#edgeToolbar {
  position: absolute;
  display: flex;
  gap: 3px;
  padding: 3px;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .2);
  z-index: 25;
}
#edgeToolbar button {
  width: 30px;
  height: 28px;
  padding: 0;
  border-color: transparent;
  font-size: 15px;
  font-weight: 700;
}
#edgeToolbar button[data-act="delete"] { color: var(--red); }
#edgeToolbar button[data-act="auto"] { color: #475569; }
#edgeToolbar button:hover { background: #eef2fb; border-color: #b9c6e2; }
#btnPanelRestore, #btnExitPresent {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 35;
  padding: 6px 10px;
  background: rgba(255,255,255,.96);
  box-shadow: 0 3px 12px rgba(15,23,42,.16);
}

/* 画布右下浮动操作条：生成代码 / 撤销 / 重做 */
#canvasActions {
  position: absolute; right: 12px; bottom: 12px; z-index: 30;
  display: flex; gap: 6px;
}
#canvasActions button {
  background: rgba(255, 255, 255, .95); border: 1px solid var(--line);
  border-radius: 8px; padding: 5px 10px; font-size: 12px; color: var(--ink);
  box-shadow: 0 4px 14px rgba(15, 23, 42, .12);
}
#canvasActions button:hover { background: #eef2fb; border-color: var(--blue); }
#canvasActions button:disabled { opacity: .45; cursor: default; box-shadow: none; }

/* 行内文字编辑框 */
#nodeEditor {
  position: absolute;
  z-index: 30;
  font-family: Consolas, "Microsoft YaHei", monospace;
  text-align: center;
  border: 2px solid var(--blue);
  border-radius: 6px;
  outline: none;
  resize: none;
  background: #fff;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .18);
  padding: 4px;
  overflow: hidden;
}

/* —— 右：面板（宽度由 --panel-w 控制，分割条可拖） —— */
#panelSplitter {
  flex: none; width: 7px; margin: 0 -3px; cursor: col-resize;
  position: relative; z-index: 5; background: transparent;
  border-left: 1px solid transparent;
}
#panelSplitter:hover, #panelSplitter.dragging {
  background: var(--blue); opacity: .35;
}
#panel {
  width: var(--panel-w, 320px); flex: none; min-width: 0;
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
}
#panelTabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }
#panelTabs button {
  flex: 1; border: none; border-radius: 0; background: transparent;
  padding: 8px 0; color: #6b7280; border-bottom: 2px solid transparent;
}
#panelTabs button.active { color: var(--blue); border-bottom-color: var(--blue); font-weight: 600; }
#panelTabs #btnPanelCollapse { flex: none; padding: 8px 10px; }
.tab-page { display: none; flex: 1; min-height: 0; flex-direction: column; }
.tab-page.active { display: flex; }

.page-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; font-size: 12px; color: #6b7280; flex: none; min-width: 0;
}
#tab-code .page-head > span:first-child { white-space: nowrap; }
.code-tools { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
#codeOut {
  flex: 1; overflow: auto; margin: 0 10px;
  background: #f8fafc; border: 1px solid var(--line); border-radius: 8px;
  padding: 10px; font-family: Consolas, "Microsoft YaHei", monospace;
  font-size: var(--code-font, 13px); line-height: 1.7; white-space: pre; color: #0f172a;
  resize: none; outline: none; display: block; tab-size: 2;
}
#codeOut:focus { border-color: var(--blue); background: #fff; }
#codeOut.dirty { border-color: #f59e0b; background: #fffbeb; }
.code-msg { font-size: 11px; padding: 4px 10px 0; margin: 0; line-height: 1.6; flex: none;
  min-height: 14px; color: #9ca3af; }
.code-msg.warn { color: #b45309; }
.code-msg.err { color: var(--red); }
.tab-tip { font-size: 11px; color: #9ca3af; padding: 8px 10px; line-height: 1.8; flex: none; }
.tab-tip code { background: #eef2fb; padding: 0 4px; border-radius: 4px; color: var(--blue-dark); }

/* 模拟页 */
.sim-controls { display: flex; align-items: center; gap: 6px; padding: 8px 10px; flex: none; }
.sim-controls .speed { display: flex; align-items: center; gap: 4px; font-size: 12px; color: #6b7280; margin-left: auto; }
#simStatus {
  padding: 6px 10px; font-size: 12px; color: #6b7280;
  background: #f8fafc; border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line); flex: none; min-height: 28px;
}
#simStatus.err { color: var(--red); }
#simStatus.ok { color: var(--green); }
.panel-sub { padding: 8px 10px; flex: none; }
.panel-sub.grow { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.panel-sub h3 {
  font-size: 12px; color: #6b7280; margin-bottom: 6px;
  display: flex; justify-content: space-between; align-items: center;
}
#varTable { width: 100%; border-collapse: collapse; font-size: 12px; }
#varTable th, #varTable td {
  border: 1px solid var(--line); padding: 3px 6px; text-align: left;
  font-family: Consolas, "Microsoft YaHei", monospace;
}
#varTable th { background: #f1f5f9; color: #64748b; }
#varTable td .type-tag {
  font-size: 10px; padding: 0 5px; border-radius: 8px;
  background: #e0e9f7; color: var(--blue-dark);
}
#varTable tr.changed td { background: #fff7e0; }
.empty { font-size: 12px; color: #b0b8c4; padding: 6px 2px; }
#console {
  flex: 1; overflow-y: auto;
  background: #0f172a; color: #dbe4f0; border-radius: 8px;
  font-family: Consolas, monospace; font-size: 12.5px; line-height: 1.8;
  padding: 8px 10px; min-height: 60px; white-space: pre-wrap; word-break: break-all;
}
#console .c-in { color: #7dd3fc; }
#console .c-err { color: #fca5a5; }
#console .c-sys { color: #94a3b8; }

/* 模拟页双栏：左 变量+控制台，右 代码跟随（运行行高亮） */
#simBody { display: flex; flex: 1; min-height: 0; }
.sim-col { flex: 1 1 50%; min-width: 0; display: flex; flex-direction: column; }
.sim-col:first-child { border-right: 1px solid var(--line); }
.sim-col .page-head { padding: 8px 10px 0; }
.mini-tip { font-size: 11px; color: #94a3b8; font-weight: 400; margin-left: 6px; }
#simCode {
  flex: 1; min-height: 0; margin: 8px 10px 10px; overflow: auto;
  background: #f8fafc; border: 1px solid var(--line); border-radius: 8px;
  font-family: Consolas, "Microsoft YaHei", monospace;
  font-size: var(--code-font, 13px);
  line-height: 1.6; padding: 6px 0;
}
#simCode .cl { display: flex; padding: 0 8px; white-space: pre; }
#simCode .cl .no {
  flex: none; width: 26px; margin-right: 10px; text-align: right;
  color: #94a3b8; user-select: none;
}
#simCode .cl .tx { flex: 1; }
#simCode .cl.cur { background: #fef3c7; box-shadow: inset 2px 0 0 #f59e0b; }
#simCode .cl.cur .no { color: #b45309; }
#simCode .cl.err-line { white-space: normal; padding: 6px 10px; color: #b45309; }

/* 输入面板：右侧停靠（遮罩近乎透明），不遮画布 */
#modalMask.clear { background: rgba(15, 23, 42, .06); align-items: flex-start; justify-content: flex-end; }
#modalMask.clear #inputModal {
  width: 330px; max-width: 92vw; margin: 172px 16px 16px 0;
  border: 1px solid var(--line);
  animation: dockIn .18s ease;
}
@keyframes dockIn { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }

/* 检查页 */
#checkList { flex: 1; overflow-y: auto; padding: 4px 10px; }
.check-item {
  border: 1px solid var(--line); border-left: 4px solid var(--red);
  border-radius: 6px; padding: 6px 8px; margin-bottom: 6px;
  font-size: 12px; cursor: pointer; background: #fff;
}
.check-item:hover { background: #f8fafc; }
.check-item.warn { border-left-color: var(--amber); }
.check-item.ok { border-left-color: var(--green); cursor: default; }
.check-item .ci-title { font-weight: 600; }
.check-item.warn .ci-title { color: #b45309; }
.check-item.ok .ci-title { color: var(--green); }
.check-item .ci-title.error-c { color: var(--red); }

/* ═══════════════ 底栏 ═══════════════ */
#statusbar {
  height: 28px; flex: none;
  display: flex; align-items: center; gap: 10px;
  padding: 0 12px;
  background: var(--panel); border-top: 1px solid var(--line);
  font-size: 12px; color: #6b7280;
}
#statusbar .sp { flex: 1; }
#statCounts { color: #9ca3af; }
#saveStatus { white-space: nowrap; color: #64748b; }
#saveStatus.pending { color: #b45309; }
#saveStatus.error { color: var(--red); }
#snapLabel { display: flex; align-items: center; gap: 4px; cursor: pointer; user-select: none; }

/* 可折叠侧栏 */
#main.palette-collapsed #palette { width: 38px; padding: 8px 4px; overflow: hidden; }
#main.palette-collapsed #palette > :not(.palette-head) { display: none; }
#main.palette-collapsed #palette .palette-head { justify-content: center; }
#main.palette-collapsed #palette .palette-head h2 { display: none; }
#main.palette-collapsed #btnTogglePalette { padding: 3px 7px; }
#main.panel-collapsed #panel,
#main.panel-collapsed #panelSplitter { display: none; }
#main.panel-collapsed #canvasWrap { flex: 1; }

/* 演示模式：不更改图数据，仅隐藏编辑界面并扩大画布 */
body.presentation #toolbar,
body.presentation #palette,
body.presentation #panelSplitter,
body.presentation #panel,
body.presentation #statusbar,
body.presentation #canvasActions,
body.presentation #nodeToolbar,
body.presentation #edgeToolbar { display: none !important; }
body.presentation #main { height: 100%; }
body.presentation #canvasWrap { width: 100%; }
body.presentation #btnExitPresent { display: block !important; }

@media (max-width: 1500px) {
  #toolbar { gap: 2px; padding: 0 4px; }
  .tb-brand { font-size: 12px; margin-right: 1px; }
  .tb-group { gap: 1px; }
  .tb-group::before, .tb-group::after { display: none; }
  #toolbar select { max-width: 104px; font-size: 11px; }
  #toolbar button { padding: 3px 5px; font-size: 11px; }
  #exportMenu summary { padding: 3px 5px; font-size: 11px; }
  .tb-spacer { display: none; }
  #panel .page-head { align-items: flex-start; flex-wrap: wrap; }
}

@media (max-width: 900px) {
  #palette { width: 138px; }
  #panel { width: min(var(--panel-w, 320px), 34vw); }
  .code-tools { width: 100%; justify-content: flex-start; }
  #statusbar { gap: 5px; padding: 0 6px; font-size: 10px; }
}

/* ═══════════════ 弹层 / 杂项 ═══════════════ */
.hidden { display: none !important; }

/* 面板宽度拖拽中：禁文字选中 */
body.resizing { cursor: col-resize; user-select: none; }

#modalMask {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .45);
  z-index: 100; display: flex; align-items: center; justify-content: center;
}
.modal {
  width: 520px; max-width: 92vw;
  background: #fff; border-radius: 12px; padding: 16px 18px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .3);
}
.modal h3 { font-size: 15px; margin-bottom: 6px; }
.modal-tip { font-size: 12px; color: #6b7280; margin-bottom: 10px; line-height: 1.6; }
.modal textarea {
  width: 100%; height: 260px; resize: vertical;
  font-family: Consolas, "Microsoft YaHei", monospace; font-size: 13px;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px;
  outline: none; line-height: 1.6;
}
.modal textarea:focus, .modal input:focus { border-color: var(--blue); }
.modal input[type="text"] {
  width: 100%; font-size: 14px; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: 8px; outline: none;
  font-family: Consolas, monospace;
}
.modal-btns {
  display: flex; align-items: center; gap: 8px; margin-top: 12px; justify-content: flex-end;
}
.modal-btns .err { color: var(--red); font-size: 12px; margin-right: auto; }

#toast {
  position: fixed; left: 50%; bottom: 44px; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 8px 18px; border-radius: 20px;
  font-size: 13px; box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
  z-index: 200; transition: opacity .25s; opacity: 1;
}
#toast.fade { opacity: 0; }

/* 拖拽幽灵 */
#ghost {
  position: fixed; z-index: 300; pointer-events: none;
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; background: rgba(255, 255, 255, .92);
  border: 1.5px dashed var(--blue); border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .18);
  transform: translate(-50%, -50%);
  font-size: 12px;
}

/* 滚动条 */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #c4cdd9; border-radius: 5px; border: 2px solid #f3f5f9; }
::-webkit-scrollbar-thumb:hover { background: #9fb0c9; }
::-webkit-scrollbar-track { background: transparent; }
