:root {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --border: #dfe3e8;
  --border-strong: #c3cad3;
  --text: #1f2937;
  --text-dim: #6b7280;
  --primary: #1f6feb;
  --primary-soft: #e8f1fe;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
  --radius: 6px;
}

* { box-sizing: border-box; }

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

.app { display: flex; flex-direction: column; height: 100%; }

/* ---------- 顶部工具栏 ---------- */
/* 工具栏恒定单行：不换行，宽度不够时横向滚动（细滚动条），按钮不被压扁 */
.toolbar {
  display: flex; align-items: center; gap: 4px; flex-wrap: nowrap;
  padding: 6px 10px; background: var(--panel);
  border-bottom: 1px solid var(--border); box-shadow: var(--shadow);
  z-index: 20; overflow-x: auto; overflow-y: hidden; scrollbar-width: thin;
}
.toolbar > * { flex: 0 0 auto; }
.toolbar .spacer { flex: 1 1 auto; min-width: 6px; }
.toolbar::-webkit-scrollbar { height: 5px; }
.toolbar::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
.toolbar::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
.toolbar .brand { font-weight: 700; font-size: 13px; margin-right: 4px; letter-spacing: .3px; white-space: nowrap; }
.toolbar .brand span { color: var(--primary); }
.toolbar .title-input {
  flex: 0 1 auto; width: 190px; min-width: 110px; padding: 4px 8px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 12.5px; font-family: inherit;
}
.toolbar .title-input:focus { outline: none; border-color: var(--primary); }
.sep { width: 1px; height: 20px; background: var(--border); margin: 0 3px; }
.toolbar .lbl { font-size: 11.5px; color: var(--text-dim); margin: 0 1px 0 3px; white-space: nowrap; }
.spacer { flex: 1; }

button.btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 8px; border: 1px solid var(--border-strong); background: #fff;
  border-radius: var(--radius); cursor: pointer; font-size: 12px;
  font-family: inherit; color: var(--text); white-space: nowrap;
}
button.btn:hover { background: #f0f4f9; border-color: var(--primary); color: var(--primary); }
button.btn:active { transform: translateY(1px); }
button.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.btn.primary:hover { background: #1a5fd0; color: #fff; }
button.btn.danger:hover { border-color: var(--danger); color: var(--danger); background: #fef2f2; }
button.btn.active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
button.btn:disabled { opacity: .45; cursor: not-allowed; }
button.btn kbd { font-size: 10px; color: var(--text-dim); border: 1px solid var(--border); border-radius: 3px; padding: 0 3px; }

select.btn, input.btn { padding: 4px 6px; }

/* 窄屏优化：优先牺牲「品牌名 / 分组标签」等纯说明性文字，并逐步收紧间距，
 * 最后才让「图标题输入框」收缩——保证所有功能按钮始终排在同一行 */
@media (max-width: 1440px) {
  .toolbar .brand { display: none; }
  .toolbar { gap: 3px; }
  .toolbar button.btn, .toolbar select.btn { padding: 4px 6px; }
}
@media (max-width: 1400px) {
  .toolbar .lbl { display: none; }
  .toolbar select.btn { flex: 0 1 auto; min-width: 72px; }
}
@media (max-width: 1200px) {
  .toolbar button.btn, .toolbar select.btn { padding: 3px 5px; font-size: 11.5px; }
  .toolbar .sep { margin: 0 2px; }
}

/* ---------- 主体三栏 ---------- */
.main { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 248px; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}

/* 侧栏头部：标题 + 设备总数 + 收起按钮 */
.side-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 10px 9px 12px; border-bottom: 1px solid var(--border);
}
.side-title {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; font-weight: 700; color: var(--text); letter-spacing: .3px;
}
.side-count {
  font-style: normal; font-weight: 500; font-size: 10.5px; line-height: 1;
  color: var(--text-dim); background: #eef2f7; border-radius: 9px; padding: 2px 6px;
}
.mini {
  font: inherit; font-size: 10.5px; font-weight: 500; line-height: 1;
  padding: 3px 6px; border: 1px solid var(--border); border-radius: 4px;
  background: #fff; color: var(--text-dim); cursor: pointer;
}
.mini:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }
.mini.on { background: var(--primary); color: #fff; border-color: var(--primary); }

/* 上传图标弹窗 */
.up-ic { display: flex; flex-direction: column; gap: 12px; padding: 4px 2px; }
.up-file { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; color: var(--text-dim); }
.up-file input { font-size: 12px; }
.up-prev { width: 72px; height: 72px; border: 1px dashed var(--border-strong); border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center; background: #fafbfc; overflow: hidden; }
.up-prev .up-ph { font-size: 12px; color: #94a3b8; }
.up-row { display: flex; align-items: center; gap: 10px; }
.up-row label { width: 44px; font-size: 12.5px; color: var(--text-dim); flex: 0 0 auto; }
.up-row input, .up-row select { flex: 1; padding: 6px 8px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 12.5px; font-family: inherit; }
.up-row input:focus, .up-row select:focus { outline: none; border-color: var(--primary); }
.up-hint { font-size: 11.5px; color: #94a3b8; line-height: 1.6; background: var(--primary-soft);
  border-radius: var(--radius); padding: 8px 10px; }

/* 搜索框 + 分段工具条：统一内边距，纵向更紧凑 */
.side-tools { padding: 9px 10px 0; display: flex; flex-direction: column; gap: 7px; }
.search-box { position: relative; }
.search-box input {
  width: 100%; padding: 6px 8px 6px 27px; border: 1px solid var(--border);
  border-radius: 14px; font-size: 12.5px; font-family: inherit;
  background-color: #fff; transition: border-color .12s, box-shadow .12s;
}
.search-box input:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft);
}
.search-box input::placeholder { color: #9aa4b2; }
/* 放大镜图标（内嵌 SVG，不依赖字体） */
.search-box::before {
  content: ''; position: absolute; left: 9px; top: 50%; width: 13px; height: 13px;
  margin-top: -6.5px; pointer-events: none; opacity: .8;
  background: no-repeat center/contain
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.2' stroke-linecap='round'><circle cx='11' cy='11' r='7'/><path d='M20 20l-4.3-4.3'/></svg>");
}
/* 三合一的分段按钮：紧凑 / 全展开 / 全折叠 */
.seg { display: flex; }
.seg .mini {
  flex: 1; padding: 5px 0; border-radius: 0; margin-left: -1px; text-align: center;
}
.seg .mini:first-child { border-radius: 4px 0 0 4px; margin-left: 0; }
.seg .mini:last-child { border-radius: 0 4px 4px 0; }
.seg .mini:hover { position: relative; z-index: 1; }
.seg .mini.on { position: relative; z-index: 2; }

/* 分类筛选条：做成一块内嵌的浅色面板，与搜索/列表分层 */
.cat-bar {
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: 4px;
  margin: 9px 10px 0; padding: 8px;
  border: 1px solid var(--border); border-radius: var(--radius); background: #fafbfc;
  max-height: 92px; overflow-y: auto;
}
.chip {
  font: inherit; font-size: 10.5px; line-height: 1.5; padding: 2px 8px;
  border: 1px solid var(--border); border-radius: 10px; background: #fff;
  color: var(--text-dim); cursor: pointer; white-space: nowrap;
  transition: border-color .12s, color .12s, background .12s;
}
.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip.on { background: var(--primary); border-color: var(--primary); color: #fff; }

.palette { flex: 1; overflow-y: auto; padding: 0 10px 20px; }
/* 分类标题吸附在顶部，长列表滚动时始终知道在哪一类 */
.palette .cat-title {
  position: sticky; top: 0; z-index: 3;
  font-size: 11.5px; color: var(--text-dim); font-weight: 600;
  display: flex; align-items: center; gap: 5px;
  margin: 10px 0 7px; padding: 5px 2px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.palette .cat-title.foldable { cursor: pointer; user-select: none; }
.palette .cat-title.foldable:hover { color: var(--primary); background: #f4f8fe; }
.palette .cat-title .arw { font-size: 9px; width: 10px; opacity: .7; }
.palette .cat-title .cnt {
  margin-left: auto; font-weight: 400; font-size: 10.5px; color: #94a3b8;
  background: #eef2f7; border-radius: 8px; padding: 1px 6px;
}
.pal-empty {
  font-size: 12px; color: var(--text-dim); padding: 16px 8px;
  line-height: 1.7; text-align: center;
}
.palette .grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.dev {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 9px 4px 7px; border: 1px solid var(--border); border-radius: 8px;
  cursor: grab; background: #fff; user-select: none;
  transition: border-color .12s, background .12s, box-shadow .12s, transform .12s;
}
.dev:hover {
  border-color: var(--primary); background: var(--primary-soft);
  box-shadow: 0 2px 6px rgba(31, 111, 235, .14); transform: translateY(-1px);
}
.dev:active { cursor: grabbing; transform: translateY(0); }
.dev svg { width: 36px; height: 36px; }
.dev .nm {
  font-size: 11px; text-align: center; line-height: 1.3; color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.dev.hide { display: none; }

/* 紧凑模式：图标变小方格、4 列、隐藏名称，靠 title 提示（buildPalette 给 .dev 加了 title） */
.palette.compact .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
.palette.compact .dev { padding: 5px 2px; gap: 0; border-radius: 6px; }
.palette.compact .dev svg { width: 28px; height: 28px; }
.palette.compact .dev .nm { display: none; }

/* 细滚动条，避免默认宽滚动条挤占侧栏 */
.palette::-webkit-scrollbar, .cat-bar::-webkit-scrollbar { width: 8px; }
.palette::-webkit-scrollbar-thumb, .cat-bar::-webkit-scrollbar-thumb {
  background: #cbd5e1; border: 2px solid var(--panel); border-radius: 4px;
}
.palette::-webkit-scrollbar-thumb:hover, .cat-bar::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* 侧栏 / 属性面板折叠：把画布空间让出来 */
.sidebar.collapsed, .props.collapsed { display: none; }
.edge-tab {
  display: none; align-items: center; justify-content: center;
  width: 18px; padding: 0; border: 0; cursor: pointer;
  background: #eef2f7; color: var(--text-dim); font: inherit; font-size: 11px;
  writing-mode: vertical-rl; letter-spacing: 1px;
}
.edge-tab:hover { background: var(--primary-soft); color: var(--primary); }
.edge-tab.show { display: flex; }          /* 由 JS 切换，避免依赖 DOM 顺序 */
.edge-tab.left { border-right: 1px solid var(--border); }
.edge-tab.right { border-left: 1px solid var(--border); }
.mini.fold { padding: 3px 5px; }

/* ---------- 画布 ---------- */
.canvas-wrap {
  flex: 1; position: relative; overflow: hidden; background: #eef1f5;
  background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 20px 20px;
}
#stage { position: absolute; transform-origin: 0 0; }
#svg { background: #fff; box-shadow: 0 2px 12px rgba(16,24,40,.12); display: block; }

/* 选中与控制点 */
.sel-box { fill: rgba(31,111,235,.06); stroke: var(--primary); stroke-width: 1.2; stroke-dasharray: 4 3; pointer-events: none; }
.port { fill: #fff; stroke: var(--primary); stroke-width: 1.5; cursor: crosshair; }
.port:hover { fill: var(--primary); }
/* 连线选中后出现的端点重连手柄（圆点）与折点手柄（方块） */
.ep-handle { fill: var(--primary); stroke: #fff; stroke-width: 1.5; cursor: pointer; }
.ep-handle:hover { fill: #1d4ed8; }
.pth-handle { fill: #fff; stroke: var(--primary); stroke-width: 1.6; cursor: move; }
.pth-handle:hover { fill: #dbeafe; }
/* 选中设备的四角缩放手柄 */
.rz-handle { pointer-events: all; }
.rz-handle[data-rz="nw"], .rz-handle[data-rz="se"] { cursor: nwse-resize; }
.rz-handle[data-rz="ne"], .rz-handle[data-rz="sw"] { cursor: nesw-resize; }
.node-hit { cursor: move; }
/* 连线与区域框的透明命中层：visiblePainted 下 transparent 描边不被浏览器命中，
 * 必须显式声明 pointer-events，否则真实点击会穿透到背景（连线/分区框选不中）。 */
.edge-hit { pointer-events: stroke; cursor: pointer; }
/* 分区框命中层只用描边带（14px）做命中区；不能用 all，否则 fill=none 的矩形内部
 * 整片可点，会吞掉框内连线/设备的点击（分区框在图层顺序上位于连线之下，但 all 仍会抢命中）。 */
.group-hit { pointer-events: stroke; cursor: pointer; }
.marquee { fill: rgba(31,111,235,.10); stroke: var(--primary); stroke-width: 1; stroke-dasharray: 4 3; }

.zoom-bar {
  position: absolute; right: 14px; bottom: 14px; display: flex; gap: 4px;
  background: #fff; padding: 4px; border-radius: 8px; box-shadow: var(--shadow);
  border: 1px solid var(--border); align-items: center;
}
.zoom-bar button { width: 28px; height: 26px; border: none; background: transparent; cursor: pointer; border-radius: 4px; font-size: 14px; }
.zoom-bar button:hover { background: var(--primary-soft); color: var(--primary); }
.zoom-bar .zv { font-size: 12px; color: var(--text-dim); min-width: 46px; text-align: center; }

.hint-bar {
  position: absolute; left: 14px; bottom: 14px; background: rgba(31,41,55,.86); color: #fff;
  padding: 6px 10px; border-radius: 6px; font-size: 12px; max-width: 60%; line-height: 1.5;
}

/* ---------- 右侧属性面板 ---------- */
.props {
  width: 268px; background: var(--panel); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.props .body { flex: 1; overflow-y: auto; padding: 10px 12px 30px; }
.props h3 {
  margin: 0; padding: 10px 12px; font-size: 12.5px; font-weight: 600; color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}
.field { margin-bottom: 10px; }
.field label { display: block; font-size: 11.5px; color: var(--text-dim); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 12.5px; font-family: inherit; color: var(--text); background: #fff;
}
.field textarea { resize: vertical; min-height: 52px; }
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--primary); }
.field .row2 { display: flex; gap: 6px; }
.field .row2 > div { flex: 1; }
.field input[type=color] { padding: 2px; height: 30px; }
.swatches { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 5px; }
.swatches i { width: 20px; height: 20px; border-radius: 4px; border: 1px solid var(--border); cursor: pointer; display: block; }
.empty-tip { color: var(--text-dim); font-size: 12px; line-height: 1.7; padding: 6px 2px; }
.props .sec-title { font-size: 11.5px; font-weight: 600; color: var(--text-dim); margin: 14px 0 8px; padding-bottom: 5px; border-bottom: 1px dashed var(--border); }

/* ---------- 弹窗 ---------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(15,23,42,.45); display: none;
  align-items: center; justify-content: center; z-index: 100;
}
.modal-mask.show { display: flex; }
.modal {
  background: #fff; border-radius: 10px; width: 560px; max-width: 92vw; max-height: 84vh;
  display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(16,24,40,.25);
}
.modal header { padding: 14px 18px; font-size: 15px; font-weight: 600; border-bottom: 1px solid var(--border); }
.modal .content { padding: 14px 18px; overflow-y: auto; }
.modal footer { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.tpl-list { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tpl {
  border: 1px solid var(--border); border-radius: 8px; padding: 10px; cursor: pointer; transition: .12s;
}
.tpl:hover { border-color: var(--primary); background: var(--primary-soft); }
.tpl b { display: block; font-size: 13px; margin-bottom: 4px; }
.tpl span { font-size: 11.5px; color: var(--text-dim); line-height: 1.5; }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: 6px 10px; font-size: 12.5px; }
.kv b { color: var(--text-dim); font-weight: 500; }
.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 9px 16px; border-radius: 6px; font-size: 13px;
  z-index: 200; opacity: 0; transition: opacity .22s; pointer-events: none;
}
.toast.show { opacity: 1; }
