:root {
  --bg: #e8edf2;
  --panel: #ffffff;
  --line: #d0d7de;
  --text: #1f2933;
  --muted: #6b7785;
  --accent: #0f6f8c;
  --accent-2: #0a5570;
  --danger: #c62828;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  font-family: "Segoe UI", Calibri, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-rows: auto auto 1fr;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 36px; height: 36px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff; font-size: 18px;
}
.doc-title { font-weight: 700; font-size: 15px; }
.muted { font-size: 11px; color: var(--muted); }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.toolbar {
  display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center;
  padding: 8px 12px;
  background: #f7f9fb;
  border-bottom: 1px solid var(--line);
}
.group { display: flex; gap: 6px; align-items: center; }
.group.grow { margin-left: auto; gap: 14px; }
.tool, .btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12.5px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.tool:hover, .btn:hover { border-color: #9aa7b5; }
.tool.active {
  background: var(--accent);
  border-color: var(--accent-2);
  color: #fff;
}
.tool.danger:hover { background: #ffebee; border-color: var(--danger); color: var(--danger); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent-2);
  color: #fff;
}
.btn.block { width: 100%; margin-top: 8px; }
.zoom-label { min-width: 48px; text-align: center; font-size: 12px; color: var(--muted); }
.status { font-size: 12px; color: var(--muted); }
.chk { font-size: 12px; color: var(--muted); display: inline-flex; gap: 6px; align-items: center; }

.main {
  display: grid;
  grid-template-columns: 300px 1fr;
  min-height: 0;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 14px;
  overflow: auto;
}
.sidebar h3 {
  margin: 16px 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.sidebar h3:first-child { margin-top: 0; }
.field {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: var(--muted); margin-bottom: 10px;
}
.field input[type="text"],
.field input[type="number"],
.field textarea,
.field select {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 8px;
  font: inherit;
  color: var(--text);
  background: #fff;
}
.field textarea { resize: vertical; min-height: 72px; }
.field input[type="color"] {
  width: 100%; height: 34px; padding: 2px; border-radius: 6px; border: 1px solid var(--line);
  cursor: pointer;
}
.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
    .rich-tools { display: flex; gap: 6px; margin: -4px 0 12px; }
.row-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.preset {
  width: 28px; height: 28px; border-radius: 6px; border: 2px solid #333;
  cursor: pointer; padding: 0;
}
.hint { font-size: 12.5px; color: var(--muted); line-height: 1.4; }
.help {
  margin: 0; padding-left: 18px;
  font-size: 12px; color: var(--muted); line-height: 1.55;
}

.viewport {
  position: relative;
  overflow: auto;
  background:
    linear-gradient(90deg, rgba(0,0,0,0.03) 1px, transparent 1px),
    linear-gradient(rgba(0,0,0,0.03) 1px, transparent 1px);
  background-size: 20px 20px;
  background-color: #dfe5eb;
  cursor: default;
  overscroll-behavior: contain;
}
.viewport.mode-pan { cursor: grab; }
.viewport.mode-pan:active { cursor: grabbing; }
.viewport.mode-connect { cursor: crosshair; }
.viewport.mode-select { cursor: default; }

.world {
  position: relative;
  min-width: 100%;
  min-height: 100%;
}

.board {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  box-shadow: 0 8px 30px rgba(0,0,0,0.12);
}

.marquee {
  position: absolute;
  border: 1px solid #2563eb;
  background: rgba(37, 99, 235, 0.15);
  pointer-events: none;
  z-index: 20;
  box-sizing: border-box;
}

.lines {
  position: absolute;
  inset: 0;
  pointer-events: auto;
  z-index: 1;
  overflow: visible;
}
.lines .link-path {
  pointer-events: none;
}
.lines .link-hit {
  pointer-events: stroke;
}

.node {
  position: absolute;
  z-index: 2;
  border: 1.5px solid;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 6px;
  user-select: none;
  cursor: grab;
  box-shadow: 0 1px 2px rgba(0,0,0,0.12);
  overflow: visible;
}
.node:active { cursor: grabbing; }
.node.selected {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
  z-index: 3;
}
.node.marquee-hit {
  outline: 2px solid #60a5fa;
  outline-offset: 2px;
  z-index: 3;
}
.node.selected .sel-h,
.node.marquee-hit .sel-h {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #fff;
  border: 1.5px solid #2563eb;
  box-sizing: border-box;
  z-index: 5;
  pointer-events: none;
}
.sel-h.nw { left: -5px; top: -5px; }
.sel-h.n  { left: calc(50% - 4px); top: -5px; }
.sel-h.ne { right: -5px; top: -5px; }
.sel-h.e  { right: -5px; top: calc(50% - 4px); }
.sel-h.se { right: -5px; bottom: -5px; }
.sel-h.s  { left: calc(50% - 4px); bottom: -5px; }
.sel-h.sw { left: -5px; bottom: -5px; }
.sel-h.w  { left: -5px; top: calc(50% - 4px); }

.link-handle,
.seg-handle {
  position: absolute;
  width: 10px;
  height: 10px;
  background: #fff;
  border: 2px solid #2563eb;
  border-radius: 2px;
  z-index: 6;
  cursor: move;
  box-sizing: border-box;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.seg-handle {
  border-radius: 50%;
  background: #2563eb;
  border-color: #fff;
  box-shadow: 0 0 0 1px #2563eb;
}
.seg-handle.horiz { cursor: ns-resize; }
.seg-handle.vert { cursor: ew-resize; }

.end-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #fff;
  z-index: 9;
  cursor: move;
  box-sizing: border-box;
  box-shadow: 0 0 0 2px #2563eb, 0 1px 4px rgba(0,0,0,0.3);
}
.end-handle.from {
  background: #16a34a;
}
.end-handle.to {
  background: #dc2626;
}
.end-handle:hover {
  transform: scale(1.15);
}

.conn-port {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #2563eb;
  z-index: 8;
  cursor: crosshair;
  box-sizing: border-box;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
}
.conn-port:hover,
.conn-port.target {
  background: #2563eb;
  transform: scale(1.25);
}
.conn-port.source {
  background: #16a34a;
  border-color: #15803d;
  transform: scale(1.3);
}
.node.connect-source {
  outline: 2px dashed #16a34a;
  outline-offset: 3px;
}
.viewport.mode-connect .node {
  cursor: crosshair;
}
.end-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #fff;
  z-index: 9;
  cursor: move;
  box-sizing: border-box;
  box-shadow: 0 0 0 2px #2563eb, 0 1px 4px rgba(0,0,0,0.3);
}
.end-handle.from {
  background: #16a34a;
}
.end-handle.to {
  background: #dc2626;
}
.end-handle:hover {
  transform: scale(1.2);
}
.node-text {
  width: 100%;
  line-height: 1.25;
  word-break: break-word;
}

.legend-box {
  position: absolute;
  left: 16px;
  top: 12px;
  z-index: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  align-items: center;
  max-width: 920px;
  padding: 8px 12px;
  background: rgba(255,255,255,0.92);
  border: 1px solid #cfd6de;
  border-radius: 8px;
  font-size: 11.5px;
  pointer-events: none;
}
.legend-box strong {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 10px;
  color: #667;
  margin-right: 4px;
}
.leg-item { display: inline-flex; align-items: center; gap: 6px; color: #333; }
.leg-item i {
  width: 18px; height: 12px; border-radius: 2px; border: 1px solid rgba(0,0,0,0.25);
  display: inline-block;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(20px);
  background: #1f2933;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: 0.2s ease;
  z-index: 50;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}
