:root {
  color-scheme: light;
  --bg: #f7f8fa;
  --fg: #202124;
  --muted: #6f747d;
  --panel: #ffffff;
  --line: #e1e4e8;
  --accent: #6956e8;
  --accent-soft: #eeebff;
  --dot: #dfe3e8;
  --shadow: 0 8px 24px rgba(30, 35, 48, .12), 0 1px 3px rgba(30, 35, 48, .08);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.topbar {
  height: 52px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 8px 16px;
  background: rgba(255,255,255,.96);
  border-bottom: 1px solid var(--line);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand-mark {
  width: 30px; height: 30px; display: grid; place-items: center;
  border-radius: 8px; background: var(--accent); color: white;
  border: 0;
  font-weight: 800; font-size: 14px; box-shadow: 0 3px 8px rgba(105,86,232,.22);
}
.brand-mark:hover { transform: translateY(-1px); }
.diagram-menu {
  position: absolute;
  z-index: 20;
  top: 12px;
  left: 12px;
  width: 280px;
  max-height: min(420px, calc(100% - 24px));
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.diagram-menu[hidden] { display: none; }
.diagram-menu-heading { height: 48px; display: flex; align-items: center; justify-content: space-between; padding: 8px 10px 8px 14px; border-bottom: 1px solid var(--line); }
.diagram-menu-heading button { height: 32px; border: 0; border-radius: 7px; background: var(--accent); color: #fff; padding: 0 10px; font-weight: 700; }
.diagram-list { overflow: auto; padding: 6px; }
.diagram-row { display: grid; grid-template-columns: 1fr 32px; gap: 4px; align-items: center; border-radius: 8px; }
.diagram-row.active { background: var(--accent-soft); }
.diagram-open { min-height: 44px; border: 0; background: transparent; text-align: left; padding: 6px 8px; border-radius: 7px; }
.diagram-open strong, .diagram-open small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.diagram-open small { color: var(--muted); margin-top: 1px; }
.diagram-delete { width: 30px; height: 30px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); opacity: 0; }
.diagram-row:hover .diagram-delete { opacity: 1; }
.diagram-delete:hover { background: #fde8e8; color: #c93636; }
.brand-name {
  width: min(280px, 32vw);
  height: 34px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  font-weight: 650;
  padding: 0 8px;
  text-overflow: ellipsis;
}
.brand-name:hover { border-color: var(--line); }
.brand-name:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.topbar-actions { display: flex; align-items: center; gap: 4px; }
.top-button, .primary-button {
  height: 34px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  padding: 0 10px;
  font-weight: 600;
}
.top-button:hover { background: #f1f2f4; }
.primary-button { background: var(--accent); color: white; padding-inline: 14px; }
.primary-button:hover { background: #5947d2; }
main { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.tool-rail {
  position: absolute;
  z-index: 5;
  top: 14px;
  left: 14px;
  width: 48px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.tool-rail button {
  position: relative;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  display: grid;
  place-items: center;
  color: #4a4e57;
  padding: 0;
}
.tool-rail button:hover { background: #f2f3f5; }
.tool-rail button.active { background: var(--accent-soft); color: var(--accent); }
.tool-icon { font-size: 19px; line-height: 1; }
.tool-rail kbd {
  position: absolute;
  right: -2px;
  bottom: 0;
  display: none;
  font: 8px/1 monospace;
  color: var(--muted);
}
.properties-panel {
  position: absolute;
  z-index: 6;
  top: 14px;
  left: 12px;
  width: 204px;
  max-height: calc(100% - 28px);
  overflow-y: auto;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.properties-panel[hidden] { display: none; }
.properties-section { padding: 0 0 12px; margin: 0 0 12px; border-bottom: 1px solid var(--line); }
.properties-section:last-child { margin-bottom: 0; border-bottom: 0; }
.properties-section > strong { display: block; margin-bottom: 8px; font-size: 12px; }
.color-grid { display: grid; grid-template-columns: repeat(6, 24px); gap: 6px; }
.color-swatch {
  width: 24px;
  height: 24px;
  border: 2px solid #fff;
  border-radius: 6px;
  background: var(--swatch, transparent);
  box-shadow: 0 0 0 1px var(--line);
  padding: 0;
}
.color-swatch.active { box-shadow: 0 0 0 2px var(--accent); }
.transparent-swatch { display: block; width: 100%; height: 100%; background: linear-gradient(135deg, transparent 47%, #e25555 48%, #e25555 52%, transparent 53%); }
.option-row, .action-row { display: flex; gap: 5px; }
.option-row button, .action-row button {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 0;
}
.option-row button.active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.option-row i, .action-row i { width: 16px; height: 16px; }
.width-preview, .line-preview { display: block; width: 20px; background: currentColor; }
.width-preview.thin { height: 1px; }
.width-preview.medium { height: 2px; }
.width-preview.bold { height: 4px; }
.line-preview.solid { height: 2px; }
.line-preview.dashed { height: 2px; background: repeating-linear-gradient(90deg, currentColor 0 6px, transparent 6px 9px); }
.line-preview.dotted { height: 2px; background: repeating-linear-gradient(90deg, currentColor 0 2px, transparent 2px 5px); }
#opacity { width: 100%; accent-color: var(--accent); }
.tool-rail i, .topbar i, .zoom-controls i, .brand-mark i {
  width: 18px;
  height: 18px;
  stroke-width: 1.8;
}
.brand-mark i { width: 17px; height: 17px; }
.zoom-controls {
  position: absolute;
  z-index: 5;
  left: 14px;
  bottom: 14px;
  height: 36px;
  display: flex;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  box-shadow: 0 4px 14px rgba(30,35,48,.08);
  overflow: hidden;
}
.zoom-controls button {
  width: 36px;
  height: 34px;
  border: 0;
  background: transparent;
  font-size: 18px;
}
.zoom-controls button:hover { background: #f2f3f5; }
.bottom-toolbar {
  position: absolute;
  z-index: 7;
  left: 50%;
  bottom: 14px;
  height: 48px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 5px;
  background: rgba(255,255,255,.96);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(30,35,48,.14), 0 1px 3px rgba(30,35,48,.08);
  backdrop-filter: blur(10px);
}
.bottom-toolbar button {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #4a4e57;
  padding: 0;
}
.bottom-toolbar button:hover { background: #f2f3f5; }
.bottom-toolbar button.active { background: var(--accent); color: #fff; box-shadow: 0 3px 8px rgba(105,86,232,.22); }
.bottom-toolbar i { width: 18px; height: 18px; stroke-width: 1.8; }
.bottom-divider { width: 1px; height: 24px; margin: 0 3px; background: var(--line); }
.shape-menu {
  position: absolute;
  z-index: 30;
  top: 12px;
  left: 12px;
  width: 250px;
  max-height: calc(100% - 24px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 10px;
}
.shape-menu[hidden] { display: none; }
.shape-menu-header { height: 38px; display: flex; align-items: center; justify-content: space-between; padding: 0 2px 6px; }
.shape-menu-header strong { font-size: 16px; }
.shape-menu-header button { width: 30px; height: 30px; display: grid; place-items: center; border: 0; border-radius: 7px; background: transparent; }
.shape-menu-header button:hover { background: #f2f3f5; }
.shape-menu-header i { width: 16px; height: 16px; }
.shape-search { height: 38px; display: flex; align-items: center; gap: 8px; border: 1px solid var(--accent); border-radius: 8px; padding: 0 9px; box-shadow: 0 0 0 2px var(--accent-soft); margin-bottom: 10px; }
.shape-search i { width: 16px; height: 16px; color: var(--accent); }
.shape-search input { width: 100%; height: 34px; border: 0; outline: 0; background: transparent; }
.shape-menu-section { border-top: 1px solid var(--line); padding: 8px 0 4px; }
.shape-section-heading { width: 100%; height: 28px; display: flex; align-items: center; justify-content: space-between; border: 0; background: transparent; font-weight: 700; padding: 0 2px; }
.shape-section-heading i { width: 14px; height: 14px; }
.shape-grid { display: grid; grid-template-columns: repeat(4, 44px); gap: 8px 5px; padding: 4px 2px 8px; }
.shape-grid button { width: 44px; height: 44px; display: grid; place-items: center; border: 0; border-radius: 8px; background: transparent; color: #3f434a; }
.shape-grid button:hover { background: #f2f3f5; }
.shape-grid i { width: 22px; height: 22px; stroke-width: 1.6; }
#zoom-level { min-width: 52px; text-align: center; color: var(--muted); font-size: 12px; font-weight: 650; }
#canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  background: var(--bg);
}
.dot { fill: var(--dot); }
.node .shape {
  filter: drop-shadow(0 2px 3px rgba(30,35,48,.06));
}
.node text {
  fill: #25282e;
  font: 14px Inter, system-ui, sans-serif;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}
.node.text text {
  text-anchor: start;
  dominant-baseline: auto;
  pointer-events: all;
  font-weight: 600;
}
.node-hit {
  pointer-events: all;
  fill: transparent;
  stroke: none;
}
.node.selected .shape { filter: drop-shadow(0 0 3px var(--accent)); }
.node.database .cylinder-lines { fill: none; pointer-events: none; }
.node-selection {
  stroke: var(--accent);
  stroke-width: 1;
  stroke-dasharray: 4 3;
  pointer-events: none;
  vector-effect: non-scaling-stroke;
}
.resize-handle {
  fill: var(--panel);
  stroke: var(--accent);
  stroke-width: 1.5;
  pointer-events: all;
  vector-effect: non-scaling-stroke;
}
.resize-handle[data-resize="n"], .resize-handle[data-resize="s"] { cursor: ns-resize; }
.resize-handle[data-resize="e"], .resize-handle[data-resize="w"] { cursor: ew-resize; }
.resize-handle[data-resize="nw"], .resize-handle[data-resize="se"] { cursor: nwse-resize; }
.resize-handle[data-resize="ne"], .resize-handle[data-resize="sw"] { cursor: nesw-resize; }
.image-item .image-frame { fill: #fff; stroke: #aeb4bd; stroke-width: 1; filter: drop-shadow(0 4px 10px rgba(30,35,48,.10)); }
.image-item image { pointer-events: none; }
.image-item.selected .image-frame { stroke: var(--accent); stroke-width: 3; }
.edge path { fill: none; }
.edge .hit { stroke: transparent; stroke-width: 16; }
.edge-selection {
  stroke: var(--accent) !important;
  stroke-width: 8 !important;
  opacity: .16;
  stroke-linecap: round;
  pointer-events: none;
}
.edge-handle {
  fill: var(--panel);
  stroke: var(--accent);
  stroke-width: 2;
  pointer-events: all;
  vector-effect: non-scaling-stroke;
  cursor: move;
}
.edge-handle[data-edge-handle="start"], .edge-handle[data-edge-handle="end"] { cursor: crosshair; }
.edge text {
  fill: #25282e;
  font-size: 12px;
  text-anchor: middle;
  paint-order: stroke;
  stroke: var(--bg);
  stroke-width: 5px;
}
.freehand { stroke: #25282e; stroke-width: 2.5; pointer-events: stroke; }
#ghost { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; }
#ghost[hidden] { display: none; }
.magnet-point {
  fill: var(--panel);
  stroke: var(--accent);
  stroke-width: 2;
  pointer-events: none;
}
#label-input {
  position: absolute;
  z-index: 12;
  transform: translate(-50%, -50%);
  width: 11rem;
  text-align: center;
  padding: 5px 8px;
  border: 2px solid var(--accent);
  border-radius: 6px;
  background: white;
  box-shadow: 0 4px 14px rgba(30,35,48,.12);
  min-height: 34px;
}
#code-panel {
  position: absolute;
  z-index: 8;
  inset-block: 12px 12px;
  inset-inline-end: 12px;
  width: min(380px, calc(100% - 24px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
#code-panel[hidden] { display: none; }
#code {
  flex: 1;
  min-height: 8rem;
  resize: none;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbfc;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.row { display: flex; gap: 6px; }
.row button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: white;
  padding: 0 11px;
}
#apply { flex: 1; background: var(--accent); color: white; border-color: var(--accent); }
#msg { margin: 0; min-height: 1.2em; color: var(--accent); font-size: 12px; }
#msg.err { color: #c93636; }
.export-popover {
  position: absolute;
  z-index: 12;
  top: 44px;
  right: 12px;
  width: 160px;
  padding: 6px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 9px;
  box-shadow: var(--shadow);
}
.export-popover[hidden] { display: none; }
.export-popover button {
  width: 100%;
  height: 36px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: left;
  padding: 0 9px;
}
.export-popover button:hover { background: #f2f3f5; }
#image-dialog {
  width: min(440px, calc(100% - 32px));
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(30,35,48,.22);
  padding: 0;
}
#image-dialog::backdrop { background: rgba(31,34,41,.28); backdrop-filter: blur(2px); }
#image-form { display: flex; flex-direction: column; gap: 10px; padding: 18px; }
.dialog-heading { display: flex; justify-content: space-between; align-items: flex-start; }
.dialog-heading p { margin: 3px 0 0; color: var(--muted); }
.dialog-close { width: 30px; height: 30px; border: 0; border-radius: 6px; background: transparent; font-size: 20px; }
.dialog-close:hover { background: #f2f3f5; }
#image-url { height: 40px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; }
#image-url:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.dialog-error { min-height: 18px; margin: 0; color: #c93636; font-size: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions button { height: 36px; border: 1px solid var(--line); border-radius: 7px; background: white; padding: 0 12px; }
.dialog-actions .primary-button { border-color: var(--accent); }
@media (max-width: 640px) {
  .brand-name { width: 150px; }
  .topbar { padding-left: 10px; }
  .top-button { padding-inline: 7px; }
  .tool-rail { top: 10px; left: 10px; }
  .zoom-controls { left: 10px; bottom: 10px; }
  #code-panel { inset: auto 10px 10px 68px; width: auto; height: 45%; }
  .properties-panel { display: none; }
  .bottom-toolbar { bottom: 10px; height: 44px; }
  .bottom-toolbar button { width: 34px; height: 34px; }
  .zoom-controls { display: none; }
}
