:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #5d6676;
  --border: #d9dde4;
  --accent: #2459d6;
  --accent-text: #ffffff;
  --error: #b42318;
  --ok: #1a7f37;
  --code-bg: #fbfbfc;
  --canvas-bg: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --panel: #1c2027;
    --text: #e4e7ec;
    --muted: #9aa3b2;
    --border: #2f3540;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --error: #ff8a80;
    --ok: #5fd38d;
    --code-bg: #171a20;
    --canvas-bg: #1a1d23;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
button, select, input, textarea { font: inherit; color: inherit; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 650; }
.topbar h1 span { font-weight: 400; color: var(--muted); font-size: 14px; margin-left: 6px; }
.example-picker { display: flex; align-items: center; gap: 6px; color: var(--muted); }
select {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
}
.status {
  margin-left: auto;
  font-size: 12.5px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status.ready { color: var(--ok); }
.status.busy { color: var(--accent); }
.status.error { color: var(--error); }

.layout { flex: 1; display: flex; min-height: 0; }
.viewport { position: relative; flex: 1; min-width: 0; background: var(--canvas-bg); }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#canvas.tool-pan { cursor: grab; }
#canvas.tool-pan.dragging { cursor: grabbing; }
#canvas.tool-draw { cursor: crosshair; }

.toolbar {
  position: absolute;
  z-index: 2;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  pointer-events: none;
}
.toolbar .group {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.08);
}
.toolbar .group label { display: flex; align-items: center; gap: 4px; padding: 3px 8px; cursor: pointer; }
.toolbar button {
  border: 0;
  background: transparent;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.toolbar button:hover { background: var(--bg); }
.toolbar button.active { background: var(--accent); color: var(--accent-text); }

.hint {
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: min(420px, calc(100% - 32px));
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}
.hint.error { color: var(--error); text-align: left; white-space: pre-wrap; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; }

.readout {
  position: absolute;
  left: 10px;
  bottom: 10px;
  font: 12px ui-monospace, Menlo, Consolas, monospace;
  color: var(--muted);
  background: var(--panel);
  border-radius: 6px;
  padding: 2px 8px;
  border: 1px solid var(--border);
}
.readout:empty { display: none; }
.legend {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: grid;
  grid-template-columns: auto 140px auto;
  align-items: center;
  gap: 2px 6px;
  font: 12px ui-monospace, Menlo, Consolas, monospace;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  color: var(--muted);
}
.legend .bar {
  height: 10px;
  border-radius: 3px;
  background: linear-gradient(90deg, #3b4cc0, #8db0fe, #f2f2f2, #f49a7b, #b40426);
}
.legend.sequential .bar { background: linear-gradient(90deg, #fff7bc, #41b6c4, #253494); }
.toolbar select { border: 0; padding: 2px 4px; margin-right: 4px; }
.legend small { grid-column: 1 / -1; text-align: center; font-family: system-ui, sans-serif; }

.splitter { width: 6px; cursor: col-resize; background: var(--border); flex: none; }
.splitter:hover, .splitter.dragging { background: var(--accent); }

.side {
  width: 440px;
  min-width: 280px;
  max-width: 70vw;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  padding: 12px 16px;
  gap: 8px;
  min-height: 0;
  overflow: auto;
}
.side-head { display: flex; align-items: center; gap: 6px; }
.side-head h2 { font-size: 15px; margin: 0 auto 0 0; }
.side h3 { font-size: 13px; margin: 6px 0 0; color: var(--muted); font-weight: 600; }
.help { margin: 0; color: var(--muted); font-size: 12.5px; }
.help code { font-size: 12px; }
textarea#code {
  flex: 1 1 320px;
  min-height: 240px;
  resize: vertical;
  width: 100%;
  font: 13px/1.5 ui-monospace, Menlo, Consolas, monospace;
  tab-size: 4;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  white-space: pre;
  overflow: auto;
}
textarea#code:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.side button {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
}
.side button:hover:not(:disabled) { border-color: var(--accent); }
.side button:disabled { opacity: 0.5; cursor: default; }
.side button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(4, auto); gap: 2px 12px; margin: 0; font-size: 12.5px; justify-content: start; }
.stats:empty { display: none; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; font-variant-numeric: tabular-nums; }
.log {
  margin: 0;
  min-height: 60px;
  max-height: 180px;
  overflow: auto;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font: 12px/1.4 ui-monospace, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}
.log .err { color: var(--error); }

@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .viewport { flex: none; height: 60vh; }
  .splitter { display: none; }
  .side { width: auto !important; max-width: none; min-width: 0; overflow: visible; }
  .topbar { padding: 8px 16px; gap: 8px; }
  .topbar h1 span { display: none; }
  .status { margin-left: 0; }
}
