:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #d9dde3;
  --text: #1f2933;
  --muted: #66727f;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --error-bg: #fdecec;
  --error-text: #9b1c1c;
  --canvas: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181c;
    --panel: #1d2126;
    --border: #333a42;
    --text: #e4e7eb;
    --muted: #98a2ad;
    --accent: #3b82f6;
    --error-bg: #3a1d1d;
    --error-text: #fca5a5;
    --canvas: #262b31;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

button, select {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
}

button:hover { border-color: var(--accent); }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 16px; margin-right: 8px; color: var(--accent); }

.field { display: flex; align-items: center; gap: 4px; color: var(--muted); }

.spacer { flex: 1; }

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(280px, 2fr) 3fr;
  gap: 12px;
  padding: 12px 16px;
  min-height: 0;
}

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}

.doc-title { font-weight: 600; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.zoom { display: flex; gap: 4px; }
.zoom button { padding: 2px 8px; }

#code {
  flex: 1;
  width: 100%;
  resize: none;
  border: 0;
  outline: none;
  padding: 12px;
  background: var(--panel);
  color: var(--text);
  font: 13px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  tab-size: 4;
}

.error {
  margin: 0;
  max-height: 35%;
  overflow: auto;
  padding: 8px 12px;
  background: var(--error-bg);
  color: var(--error-text);
  font: 12px/1.4 ui-monospace, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

.viewport {
  flex: 1;
  overflow: auto;
  background: var(--canvas);
}

.preview {
  display: inline-block;
  padding: 16px;
  transform-origin: 0 0;
}

.preview.stale { opacity: 0.4; }

.preview svg { display: block; max-width: none !important; }

.footer {
  padding: 6px 16px;
  color: var(--muted);
  font-size: 12px;
}

.footer a { color: var(--muted); }

dialog {
  width: min(480px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
}

dialog h2 { margin: 0 0 4px; font-size: 16px; }

.hint { margin: 0 0 12px; color: var(--muted); font-size: 12px; }

.saved-list { list-style: none; margin: 0 0 12px; padding: 0; max-height: 50vh; overflow: auto; }

.saved-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.saved-list .name { flex: 1; background: none; border: 0; text-align: left; padding: 0; }
.saved-list .name:hover { color: var(--accent); }
.saved-list .date { color: var(--muted); font-size: 12px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
}

@media (max-width: 800px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: minmax(220px, 40vh) minmax(320px, 1fr); }
  body { height: auto; min-height: 100%; }
}

/* ---------- forms, cards, extra pages ---------- */

input[type="text"], input[type="password"] {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  min-width: 0;
}

button:disabled, select:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

a { color: var(--accent); }

dialog h3 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }

.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.row input[type="text"] { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }

.saved-list li.empty { color: var(--muted); }
.saved-list li select { padding: 2px 6px; }
.saved-list span.name { flex: 1; }

.user-btn { font-weight: 600; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
}

.card h2 { margin: 0 0 12px; font-size: 16px; }

.center-page { align-items: center; justify-content: center; padding: 16px; }
.center-page .card { width: min(340px, 100%); }
.center-page h1 { margin: 0 0 4px; text-align: center; font-size: 22px; }

.form-error { margin: 0; color: var(--error-text); }

.page { height: auto; min-height: 100%; }
.content { display: flex; flex-direction: column; gap: 16px; padding: 16px; max-width: 900px; width: 100%; margin: 0 auto; }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.table .actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

.viewer { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 12px 16px; }
.viewer .viewport { border: 1px solid var(--border); border-radius: 8px; }
.view-error { margin: 32px auto; color: var(--muted); }

@media (max-width: 600px) {
  .table th:nth-child(4), .table td:nth-child(4) { display: none; }
}

/* ---------- start page ---------- */

a.brand { text-decoration: none; }
.muted { color: var(--muted); font-size: 13px; }

a.button {
  display: inline-block;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
}
a.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.section-title { margin: 8px 0 10px; font-size: 15px; }

.recent-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr));
  gap: 16px;
}

.recent-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--text);
  text-decoration: none;
  transition: border-color .15s ease, transform .15s ease;
}
.recent-card:hover, .recent-card:focus-visible { border-color: var(--accent); transform: translateY(-2px); outline: none; }

.recent-preview {
  height: 150px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: #ffffff;
  border-radius: 6px;
  border: 1px solid var(--border);
  padding: 8px;
}
.recent-preview svg { max-width: 100% !important; max-height: 134px; width: auto !important; height: auto; }
.recent-preview.no-preview { color: #66727f; font-size: 13px; }
.recent-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty-state { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; }
.empty-state p { margin: 0; }
.table a { color: var(--accent); text-decoration: none; }
.table a:hover { text-decoration: underline; }

/* ---------- help page ---------- */

.help .card h2 { margin-bottom: 8px; }
.help .card h3 { margin: 18px 0 6px; font-size: 14px; }
.help p, .help li { line-height: 1.55; }
.help ul { padding-left: 20px; }
.help code { font: 12.5px ui-monospace, "SF Mono", Menlo, Consolas, monospace; background: var(--bg); padding: 1px 4px;
  border-radius: 4px; }
kbd { font: 12px ui-monospace, Menlo, monospace; border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }

.toc { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.toc a { text-decoration: none; }
.help section { scroll-margin-top: 12px; }

.example {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  margin: 10px 0 14px;
}
.example-code {
  margin: 0;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font: 12.5px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  overflow-x: auto;
}
.example-preview, .shape-preview {
  display: grid;
  place-items: center;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  overflow: hidden;
  color: #66727f;
  font-size: 12px;
}
.example-preview svg { max-width: 100% !important; max-height: 260px; width: auto !important; height: auto; }
.example-actions { grid-column: 1 / -1; display: flex; gap: 8px; }

.shape-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 190px), 1fr));
  gap: 10px;
  margin: 10px 0 16px;
}
.shape { margin: 0; display: flex; flex-direction: column; gap: 6px; }
.shape-preview { height: 96px; }
.shape-preview svg { max-width: 100% !important; max-height: 80px; width: auto !important; height: auto; }
.shape code { display: block; white-space: pre-wrap; overflow-wrap: anywhere; }
.shape .example-actions button { padding: 2px 8px; font-size: 12px; }

@media (max-width: 700px) {
  .example { grid-template-columns: 1fr; }
}

/* ---------- mouse editing ---------- */

.pane-view { position: relative; }
.visual-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.palette { display: flex; flex-wrap: wrap; gap: 4px; }
.pal { padding: 3px 5px; line-height: 0; cursor: grab; }
.pal svg { width: 28px; height: 20px; fill: #eef2ff; stroke: #6366f1; stroke-width: 1.4; }
.visual-bar .field select { padding: 3px 6px; }
#undo, #redo { padding: 3px 9px; }
.visual-hint { margin: 0; padding: 6px 10px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); }

.viewport { position: relative; }
.viewport.drop-target { outline: 2px dashed var(--accent); outline-offset: -4px; }
.preview g.node[data-flow-id] { cursor: pointer; }
.preview .flow-hit { fill: none; stroke: transparent; stroke-width: 14px; cursor: pointer; pointer-events: stroke; }
.preview g.edgeLabel[data-edge] { cursor: pointer; }
.preview g.node.flow-selected rect, .preview g.node.flow-selected polygon, .preview g.node.flow-selected circle,
.preview g.node.flow-selected path, .preview g.node.flow-selected ellipse {
  stroke: #f59e0b !important;
  stroke-width: 3px !important;
}
.preview path.flow-selected { stroke: #f59e0b !important; stroke-width: 3px !important; }
.preview g.node.flow-connect-source rect, .preview g.node.flow-connect-source polygon,
.preview g.node.flow-connect-source path, .preview g.node.flow-connect-source circle {
  stroke: #10b981 !important;
  stroke-width: 3px !important;
  stroke-dasharray: 5 3;
}

.connect-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 1px var(--accent);
  cursor: crosshair;
  touch-action: none;
  z-index: 2;
}
.drag-line { position: absolute; left: 0; top: 0; pointer-events: none; z-index: 1; }
.drag-line line { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; }

.pane-view.has-inspector .viewport { margin-right: 272px; }
.inspector {
  position: absolute;
  right: 8px;
  top: 92px;
  width: 256px;
  max-height: calc(100% - 100px);
  overflow: auto;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .12);
  z-index: 3;
}
.inspector h3 { margin: 0 0 8px; font-size: 13px; padding-right: 20px; }
.inspector h3 code { font-weight: 400; color: var(--muted); }
.stack-label { display: flex; flex-direction: column; gap: 3px; margin-bottom: 8px; font-size: 12px; color: var(--muted); }
.stack-label textarea, .stack-label input, .stack-label select {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 7px;
}
.stack-label textarea { resize: vertical; }
.color-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-bottom: 8px; font-size: 12px; }
.color-row > span { width: 100%; color: var(--muted); }
.swatch { width: 20px; height: 20px; padding: 0; border-radius: 4px; border: 1px solid var(--border); }
.swatch.on { outline: 2px solid var(--accent); outline-offset: 1px; }
.color-row .color-picker { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.color-row { position: relative; }
.swatch-reset { font-size: 11px; padding: 2px 6px; }
.swatch-reset.on { border-color: var(--accent); }
.inspector .row { margin: 4px 0 0; }
.inspector button.on { border-color: #10b981; color: #047857; }
button.danger { color: #b91c1c; }
.insp-close { position: absolute; top: 6px; right: 6px; padding: 0 7px; border: 0; font-size: 16px; }

@media (max-width: 800px) {
  .pane-view.has-inspector .viewport { margin-right: 0; }
  .inspector { position: relative; top: auto !important; right: auto; width: auto; max-height: none; margin: 8px;
    box-shadow: none; }
}

/* ---------- hide code ---------- */

.pane-title { display: flex; align-items: center; gap: 8px; }
#toggle-code { padding: 2px 8px; font-size: 12px; }
.workspace.code-hidden { grid-template-columns: 1fr; }
.workspace.code-hidden .pane-code { display: none; }
@media (max-width: 800px) {
  .workspace.code-hidden { grid-template-rows: minmax(420px, 1fr); }
}
