:root {
  --bg: #e8eef5;
  --panel: #f7f9fc;
  --panel-border: #d3dce6;
  --ink: #1a2b3c;
  --muted: #5b6b7c;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --canvas: #f4f7fb;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.06);
  --radius: 10px;
  font-family: "Segoe UI", "IBM Plex Sans", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); }
html, body, #app, .workspace, #canvas, .toolbar, .palette, .tool, .btn {
  -webkit-user-select: none;
  user-select: none;
}
input, textarea, select, .text-editor {
  -webkit-user-select: text;
  user-select: text;
}

#app {
  height: 100%;
  display: grid;
  grid-template-columns: 196px 1fr;
  grid-template-rows: 56px 1fr 28px;
  grid-template-areas:
    "toolbar toolbar"
    "palette workspace"
    "legal legal";
}

.toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  background: #ffffff;
  border-bottom: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  z-index: 3;
}

.brand {
  display: flex;
  align-items: center;
  min-width: 168px;
  text-decoration: none;
}
.brand-logo {
  height: 36px;
  width: auto;
  display: block;
}

.toolbar-group { display: flex; align-items: center; gap: 6px; }
.toolbar-sep { width: 1px; height: 28px; background: var(--panel-border); margin: 0 4px; }
.spacer { flex: 1; }

.btn {
  border: 1px solid var(--panel-border);
  background: #fff;
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  line-height: 1.2;
}
.btn:hover { background: #f3f6fa; border-color: #b7c4d3; }
.btn.primary { background: var(--accent); color: #fff; border-color: #1d4ed8; }
.btn.primary:hover { background: #1d4ed8; }
.btn.icon { min-width: 32px; padding: 6px 8px; font-size: 14px; }
.btn.toggle.on { background: var(--accent-soft); border-color: #93c5fd; color: #1e40af; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn:disabled:hover { background: #fff; border-color: var(--panel-border); }

.props-inline label {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
}
.props-inline input[type="color"] {
  width: 28px; height: 24px; padding: 0; border: 1px solid var(--panel-border);
  border-radius: 6px; background: #fff; cursor: pointer;
}
.props-inline input[type="number"],
.props-inline select {
  height: 26px; border: 1px solid var(--panel-border);
  border-radius: 6px; padding: 0 6px; font: inherit; font-size: 12px;
  background: #fff; color: var(--ink);
}
.props-inline input[type="number"] { width: 48px; }
.props-inline select { width: 92px; }
#snap-mode {
  height: 28px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 0 6px;
  font: inherit;
  font-size: 12px;
  background: #fff;
  color: var(--ink);
  max-width: 158px;
}

.zoom-readout { font-size: 12px; color: var(--muted); min-width: 42px; text-align: right; }
.status { font-size: 12px; color: var(--muted); max-width: 280px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.palette {
  grid-area: palette;
  background: var(--panel);
  border-right: 1px solid var(--panel-border);
  overflow: auto;
  padding: 10px 8px 24px;
}
.pack {
  margin: 8px 4px 4px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: #fff;
}
.pack > summary {
  cursor: pointer;
  list-style: none;
  padding: 7px 8px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.pack > summary::-webkit-details-marker { display: none; }
.pack[open] > summary { border-bottom: 1px solid var(--panel-border); }
.palette h2 {
  margin: 14px 8px 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.tool {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  padding: 4px 6px;
  cursor: grab;
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  text-align: left;
}
.tool svg { width: 44px; height: 32px; flex: 0 0 auto; color: #334155; }
.tool:hover { background: #fff; border-color: var(--panel-border); }
.tool.on { background: var(--accent-soft); border-color: #93c5fd; color: #1e3a8a; }

.saved-list { padding: 0 4px; display: flex; flex-direction: column; gap: 4px; }
.saved-list .hint { font-size: 11px; color: var(--muted); margin: 4px; }
.saved-item {
  display: flex; align-items: center; justify-content: space-between;
  gap: 6px; font-size: 12px; padding: 6px 8px;
  border-radius: 7px; border: 1px solid var(--panel-border); background: #fff;
}
.saved-item button {
  border: 0; background: transparent; color: var(--accent); cursor: pointer; font: inherit; font-size: 12px;
}

.workspace {
  grid-area: workspace;
  position: relative;
  overflow: hidden;
  background: var(--canvas);
  cursor: default;
}
#canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}
#canvas.select { cursor: default; }
#canvas.connector { cursor: crosshair; }
#canvas.pan { cursor: grab; }
#canvas.placing { cursor: copy; }

.text-editor {
  position: absolute;
  z-index: 5;
  resize: none;
  border: 2px solid var(--accent);
  border-radius: 6px;
  padding: 6px 8px;
  font: 14px "Segoe UI", system-ui, sans-serif;
  text-align: center;
  background: #fff;
  outline: none;
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.18);
}
.text-editor.hidden { display: none; }

.handle {
  fill: #fff;
  stroke: #2563eb;
  stroke-width: 1.5;
}
.sel-box {
  fill: rgba(37, 99, 235, 0.06);
  stroke: #2563eb;
  stroke-width: 1.25;
  stroke-dasharray: 5 3;
}
.marquee {
  fill: rgba(37, 99, 235, 0.08);
  stroke: #2563eb;
  stroke-width: 1;
  stroke-dasharray: 4 3;
}
.port {
  fill: #fff;
  stroke: #2563eb;
  stroke-width: 1.5;
}
.ghost { opacity: 0.55; pointer-events: none; }
.guide {
  stroke: #fb7185;
  stroke-width: 1;
  stroke-dasharray: 4 3;
  pointer-events: none;
}
.guide-center {
  stroke: #38bdf8;
}
.legal {
  grid-area: legal;
  display: flex;
  align-items: center;
  padding: 0 14px;
  font-size: 11px;
  color: var(--muted);
  background: #fff;
  border-top: 1px solid var(--panel-border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.legal a {
  color: #1d4ed8;
  text-decoration: none;
  font-weight: 600;
  margin: 0 0.35em;
}
.legal a:hover { text-decoration: underline; }

.palette-action {
  width: calc(100% - 8px);
  margin: 4px;
  text-align: left;
}
.lib-list { padding: 0 4px 8px; display: flex; flex-direction: column; gap: 4px; }
.lib-list .hint { font-size: 11px; color: var(--muted); margin: 4px; }
.lib-item {
  display: flex; align-items: center; justify-content: space-between;
  gap: 6px; border: 1px solid var(--panel-border); background: #fff;
  border-radius: 7px; padding: 4px 6px; font-size: 12px;
}
.lib-item button { border: 0; background: transparent; color: var(--accent); cursor: pointer; font: inherit; font-size: 12px; }

.modal {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(15, 23, 42, 0.45);
  display: grid; place-items: center;
}
.modal.hidden { display: none; }
.modal-panel {
  width: 90vw; height: 90vh;
  background: #fff; border-radius: 12px;
  box-shadow: 0 20px 50px rgba(15,23,42,0.25);
  display: grid; grid-template-rows: auto 1fr auto;
  overflow: hidden;
}
.modal-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 14px; border-bottom: 1px solid var(--panel-border);
}
.modal-bar h2 { margin: 0; font-size: 16px; min-width: 180px; }
.modal-canvas-wrap { background: var(--canvas); overflow: hidden; }
#element-canvas { width: 100%; height: 100%; display: block; }
.modal-hint { margin: 0; padding: 8px 14px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--panel-border); }
.btn.etool.on { background: var(--accent-soft); border-color: #93c5fd; color: #1e40af; }

.elements-panel {
  width: 80vw; height: 80vh;
  grid-template-rows: auto auto 1fr auto;
}
#elements-search {
  flex: 1; min-width: 180px; max-width: 360px;
  height: 30px; border: 1px solid var(--panel-border);
  border-radius: 8px; padding: 0 10px; font: inherit;
}
.elements-filters {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 8px 14px; border-bottom: 1px solid var(--panel-border);
}
.chip {
  border: 1px solid var(--panel-border); background: #fff;
  border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer;
}
.chip.on { background: var(--accent-soft); border-color: #93c5fd; color: #1e40af; }
.elements-grid {
  overflow: auto; padding: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 10px;
}
.el-card {
  border: 1px solid var(--panel-border); background: #fff;
  border-radius: 10px; padding: 8px 8px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  cursor: grab;
}
.el-card svg { width: 100%; height: 72px; display: block; }
.el-card small { font-size: 11px; color: var(--muted); text-align: center; line-height: 1.2; }
.el-card b { font-size: 12px; font-weight: 650; text-align: center; }
