:root {
  --bg: #1b1c1f;
  --panel: #232428;
  --panel-2: #2b2d31;
  --border: #3a3c42;
  --text: #e6e7ea;
  --muted: #9a9ea6;
  --accent: #4f8cff;
  --accent-2: #3a72e0;
  --danger: #e5534b;
  --ok: #3fb950;
  --warn: #d29922;
  --input: #1b1c1f;
  --hover: #33363c;
  --sel: #4fb8ff;
  --radius: 6px;
  color-scheme: dark;
}
[data-theme="light"] {
  --bg: #e9ebef;
  --panel: #ffffff;
  --panel-2: #f3f4f6;
  --border: #d3d6db;
  --text: #1f2328;
  --muted: #646a73;
  --input: #ffffff;
  --hover: #e8eaee;
  --sel: #1f6feb;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 13px/1.4 "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- buttons */
.btn {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 5px 11px; cursor: pointer; white-space: nowrap; line-height: 1.3;
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent-2); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.icon { padding: 4px 9px; min-width: 30px; font-size: 15px; }
.btn.link { background: none; border: none; color: var(--accent); padding: 2px 0; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.btn-row.end { justify-content: flex-end; }
.btn-row.spaced { margin-top: 18px; }
.btn-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin-bottom: 6px; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; display: inline-block; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  display: flex; align-items: center; gap: 14px; padding: 6px 12px;
  background: var(--panel); border-bottom: 1px solid var(--border); flex: none;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.doc { display: flex; align-items: baseline; gap: 10px; min-width: 0; flex: 1; }
.doc-name {
  background: none; border: 1px solid transparent; border-radius: var(--radius); padding: 3px 6px;
  font-size: 15px; font-weight: 600; cursor: text; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.doc-name:hover { border-color: var(--border); }
.doc-status { font-size: 12px; color: var(--muted); white-space: nowrap; }
.doc-status.ok { color: var(--ok); }
.doc-status.warn { color: var(--warn); }
.actions { display: flex; align-items: center; gap: 6px; }

details.menu { position: relative; }
details.menu > summary { list-style: none; }
details.menu > summary::-webkit-details-marker { display: none; }
.menu-items {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 50; min-width: 240px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.35); padding: 4px;
}
.menu-items button {
  display: block; width: 100%; text-align: left; background: none; border: 0; padding: 7px 10px; border-radius: 4px; cursor: pointer;
}
.menu-items button:hover { background: var(--hover); }
.menu-items hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

/* ---------------------------------------------------------------- layout */
.layout { flex: 1; display: grid; grid-template-columns: 300px 1fr 280px; min-height: 0; }
.panel { background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.panel.left { border-right: 1px solid var(--border); }
.panel.right { border-left: 1px solid var(--border); }
.panel-title { padding: 9px 12px; font-weight: 600; border-bottom: 1px solid var(--border); }
.panel-body { overflow-y: auto; padding: 8px 12px 20px; flex: 1; }
.left-tabs { display: flex; border-bottom: 1px solid var(--border); }
.left-tabs button {
  flex: 1; background: none; border: 0; border-bottom: 2px solid transparent; padding: 9px; cursor: pointer; font-weight: 600; color: var(--muted);
}
.left-tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

.center { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.stage-wrap { flex: 1; position: relative; min-height: 0; padding: 10px; }
#stage { width: 100%; height: 100%; display: block; user-select: none; touch-action: none; }
#stage[data-tool="draw"] { cursor: crosshair; }
#stage[data-tool="tether"] { cursor: cell; }
.tool-hint {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); pointer-events: none;
  background: rgba(0,0,0,.65); color: #fff; padding: 4px 10px; border-radius: 12px; font-size: 12px;
}
.tool-hint:empty { display: none; }

/* stage objects */
#stage .obj { cursor: move; }
#stage .obj.is-pinned { cursor: default; }
#stage[data-tool="draw"] .obj { cursor: crosshair; }
#stage[data-tool="tether"] .obj { cursor: pointer; }
#stage .sel-box { fill: none; stroke: var(--sel); stroke-width: 1.5; stroke-dasharray: 5 3; pointer-events: none; }
#stage .sel-box.pinned { stroke: var(--warn); }
#stage .marquee { fill: rgba(79,140,255,.12); stroke: var(--sel); stroke-width: 1; pointer-events: none; }
#stage .handle { fill: #fff; stroke: var(--sel); stroke-width: 2; cursor: grab; }
#stage .handle-rotate { fill: var(--sel); stroke: #fff; }
#stage .handle-inner { fill: var(--sel); }
#stage .handle-line { stroke: var(--sel); stroke-width: 1; pointer-events: none; }
#stage .tether-start { fill: none; stroke: #ff4fd8; stroke-width: 3; stroke-dasharray: 4 3; pointer-events: none; }

/* steps */
.steps {
  flex: none; display: flex; align-items: center; gap: 6px; padding: 8px 12px;
  border-top: 1px solid var(--border); background: var(--panel); overflow-x: auto;
}
.step-tabs { display: flex; gap: 4px; }
.step-tab {
  min-width: 32px; height: 30px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--panel-2); cursor: pointer; font-weight: 600;
}
.step-tab:hover { background: var(--hover); }
.step-tab.active { background: var(--accent); border-color: var(--accent-2); color: #fff; }
.step-tab.add { color: var(--accent); font-size: 16px; }

/* ---------------------------------------------------------------- palette */
.pal-section { border-bottom: 1px solid var(--border); padding: 4px 0 8px; }
.pal-section summary { cursor: pointer; font-weight: 600; padding: 6px 0; color: var(--muted); user-select: none; }
.pal-grid { display: grid; grid-template-columns: repeat(var(--cols, 4), 1fr); gap: 5px; }
.pal-item {
  aspect-ratio: 1; background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 5px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.pal-item:hover { background: var(--hover); border-color: var(--accent); }
.pal-item.active { background: var(--accent); border-color: var(--accent-2); color: #fff; }
.pal-item svg { width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.pal-item.tool svg { width: 70%; height: 70%; }

/* ---------------------------------------------------------------- forms */
.form h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 8px; }
.form h3:first-child { margin-top: 6px; }
.field { margin-bottom: 9px; }
.field > label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.field.inline { display: flex; align-items: center; gap: 7px; }
.field.inline > label { margin: 0; color: var(--text); }
.field input[type=text], .field input[type=number], .field select, .field textarea, .dlg input[type=text], .dlg input[type=search] {
  width: 100%; background: var(--input); border: 1px solid var(--border); border-radius: var(--radius); padding: 5px 7px;
}
.field textarea { resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .dlg input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.range-row { display: flex; align-items: center; gap: 6px; }
.range-row input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); }
.range-row .range-num { width: 62px !important; flex: none; }
.range-row .suffix { color: var(--muted); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.flag-row { margin-top: 6px; }
.swatches { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.swatch { width: 20px; height: 20px; border-radius: 4px; border: 1px solid rgba(128,128,128,.6); cursor: pointer; padding: 0; }
.swatch.active { outline: 2px solid var(--accent); outline-offset: 1px; }
.swatch.none { background: linear-gradient(45deg, transparent 45%, var(--danger) 45%, var(--danger) 55%, transparent 55%), var(--input); }
.swatches input[type=color] { width: 28px; height: 22px; padding: 0; border: 1px solid var(--border); border-radius: 4px; background: none; cursor: pointer; }
.preset-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.preset-grid .btn { white-space: normal; text-align: left; }
.empty p { margin: 6px 0 10px; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.pad { padding: 14px; }

/* ---------------------------------------------------------------- dialogs */
.dlg {
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
  padding: 18px; width: min(420px, 92vw); box-shadow: 0 16px 48px rgba(0,0,0,.5);
}
.dlg.wide { width: min(760px, 94vw); }
.dlg::backdrop { background: rgba(0,0,0,.5); }
.dlg h2 { margin: 0 0 12px; font-size: 17px; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.dlg-head h2 { margin: 0; }
.dlg form .btn-row { margin-top: 14px; }
.plan-list { margin-top: 10px; max-height: 60vh; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.plan-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.plan-row:last-child { border-bottom: 0; }
.plan-row.current { background: var(--panel-2); }
.plan-open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; cursor: pointer; padding: 4px; border-radius: 4px; }
.plan-open:hover { background: var(--hover); }
.plan-name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plan-meta { display: block; font-size: 12px; color: var(--muted); }
.plan-actions { display: flex; gap: 4px; flex: none; }
.tag { font-size: 10px; background: var(--accent); color: #fff; border-radius: 8px; padding: 1px 6px; margin-left: 6px; vertical-align: middle; }
.help { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.help ul { padding-left: 18px; margin: 0; }
.help li { margin-bottom: 6px; }
.help h3 { margin: 0 0 8px; font-size: 14px; }
.keys td { padding: 3px 8px 3px 0; vertical-align: top; }
kbd {
  background: var(--panel-2); border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px;
  padding: 0 5px; font-size: 11px; font-family: inherit;
}

/* ---------------------------------------------------------------- toasts */
.toasts { position: fixed; bottom: 64px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 100; align-items: center; }
.toast {
  background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  padding: 8px 14px; border-radius: var(--radius); box-shadow: 0 6px 20px rgba(0,0,0,.35); transition: opacity .35s; max-width: 70vw;
}
.toast.ok { border-left-color: var(--ok); }
.toast.warn { border-left-color: var(--warn); }
.toast.error { border-left-color: var(--danger); }
.toast.out { opacity: 0; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 250px 1fr 230px; }
  .help { grid-template-columns: 1fr; }
}

/* ---------------------------------------------------------------- login */
.login-page { align-items: center; justify-content: center; }
.login-card {
  width: min(340px, 92vw); background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 24px; display: flex; flex-direction: column; gap: 6px; box-shadow: 0 16px 48px rgba(0,0,0,.35);
}
.login-card .brand { margin-bottom: 12px; font-size: 18px; }
.login-card label { font-size: 12px; color: var(--muted); margin-top: 6px; }
.login-card input { background: var(--input); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 9px; }
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login-card .btn { margin-top: 12px; padding: 8px; }
.login-error { color: var(--danger); min-height: 18px; font-size: 12px; }
.user-chip { color: var(--muted); font-size: 12px; white-space: nowrap; }
.pw-form { display: flex; flex-direction: column; gap: 4px; }
.pw-form label { font-size: 12px; color: var(--muted); margin-top: 8px; }
.pw-form input { background: var(--input); border: 1px solid var(--border); border-radius: var(--radius); padding: 7px 9px; }
.pw-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
