:root {
  --bg: #eef0f3;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #667085;
  --border: #d9dde3;
  --hover: #eef1f6;
  --btn: #f6f7f9;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --on: #ffd75e;
  --on-text: #3a2c00;
  --in-on: #16a34a;
  --canvas: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c; --panel: #1c2027; --text: #e6e8ec; --muted: #98a2b3; --border: #2e343e;
    --hover: #262b34; --btn: #252a33; --accent: #4f83f1; --danger: #ef5350; --canvas: #fbfbfa;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14171c; --panel: #1c2027; --text: #e6e8ec; --muted: #98a2b3; --border: #2e343e;
  --hover: #262b34; --btn: #252a33; --accent: #4f83f1; --danger: #ef5350; --canvas: #fbfbfa;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button, .btn, select, input, textarea { font: inherit; color: inherit; }
button, .btn {
  background: var(--btn); border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 10px; cursor: pointer; white-space: nowrap;
}
button:hover, .btn:hover { background: var(--hover); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
select, input[type=text], input[type=number], textarea {
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px;
}
textarea, input.mono { font-family: ui-monospace, Consolas, "Cascadia Mono", monospace; font-size: 13px; }

.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 800; letter-spacing: .08em; }
.proj { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 200px; }
.pname { font-weight: 600; }
.dirty { color: #e08a00; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

.layout { flex: 1; display: grid; grid-template-columns: auto 1fr 330px; min-height: 0; }
.tools {
  display: flex; flex-direction: column; gap: 4px; padding: 8px;
  background: var(--panel); border-right: 1px solid var(--border); overflow-y: auto;
}
.tools hr { width: 100%; border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
.tools button { display: flex; flex-direction: column; align-items: center; width: 70px; padding: 6px 2px; gap: 2px; }
.tools button b { font-size: 18px; line-height: 1; font-weight: 400; }
.tools button span { font-size: 11px; }
.tools button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.canvas-wrap { position: relative; min-width: 0; min-height: 0; background: var(--canvas); }
#canvas { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; }
.hintbar {
  position: absolute; left: 10px; bottom: 10px; padding: 4px 10px; border-radius: 6px;
  background: rgba(20, 24, 32, .78); color: #fff; font-size: 12px; pointer-events: none;
}
.hintbar:empty { display: none; }
.hintbar { max-width: calc(100% - 20px - 390px); }
.gridbar {
  position: absolute; right: 10px; bottom: 10px; display: flex; align-items: center; gap: 6px;
  padding: 4px 8px; border-radius: 8px; font-size: 12px; color: #1d2330;
  background: rgba(255, 255, 255, .94); border: 1px solid #d9dde3; box-shadow: 0 1px 4px rgba(0, 0, 0, .08);
}
.gridbar label { display: flex; align-items: center; gap: 3px; cursor: pointer; white-space: nowrap; }
.gridbar select, .gridbar button {
  background: #f6f7f9; color: #1d2330; border: 1px solid #d9dde3; border-radius: 5px; padding: 1px 6px; font-size: 12px;
}
.gridbar button { min-width: 24px; }
.gridbar #zoomInfo { min-width: 50px; }
.gridbar .gsep { width: 1px; height: 16px; background: #d9dde3; }
.gridbar .pos { min-width: 92px; color: #667085; font-family: ui-monospace, Consolas, monospace; font-size: 11.5px; }
#canvas[data-mode="step"], #canvas[data-mode="trans"], #canvas[data-mode="link"], #canvas[data-mode="action"] { cursor: crosshair; }
#canvas[data-mode="action"] .actbox { cursor: text; }
.inline-edit {
  position: absolute; z-index: 5; box-sizing: border-box; margin: 0; padding: 0 6px;
  border: 2px solid #2563eb; border-radius: 2px; background: #fff; color: #111; outline: none;
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace; box-shadow: 0 2px 8px rgba(37, 99, 235, .25);
}
.inline-edit.bad { border-color: #dc2626; box-shadow: 0 2px 8px rgba(220, 38, 38, .25); }
#canvas[data-mode="delete"] { cursor: not-allowed; }
#canvas.panready, #canvas.panning { cursor: grab; }
#canvas.panning { cursor: grabbing; }
@media (max-width: 1100px) {
  .hintbar { top: 10px; bottom: auto; max-width: calc(100% - 20px); }
  .gridbar .pos { display: none; }
}

.side { overflow-y: auto; background: var(--panel); border-left: 1px solid var(--border); }
.panel { padding: 12px 14px; border-bottom: 1px solid var(--border); }
.panel h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; }
.panel h3 { font-size: 12px; margin: 12px 0 6px; color: var(--muted); font-weight: 600; }
.panel h3 small { font-weight: 400; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field label { font-size: 12px; color: var(--muted); }
.field input, .field textarea { width: 100%; }
.check { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }
.row { display: flex; gap: 6px; flex-wrap: wrap; }
.row2 { display: flex; gap: 12px; align-items: flex-end; }
.row2 .field { flex: 0 0 110px; }
.insbar { display: flex; gap: 4px; flex-wrap: wrap; }
.insbar .ins {
  padding: 1px 7px; font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; min-width: 28px;
}
.err { color: var(--danger); font-size: 12px; min-height: 0; margin: -4px 0 8px; }
.err:empty { display: none; }
.muted { color: var(--muted); }
.warn { margin: 6px 0 0; padding-left: 18px; color: #b26a00; font-size: 12.5px; }
.ok { color: var(--in-on); font-size: 12.5px; }
.stats { font-size: 12.5px; color: var(--muted); }

.simbar { display: flex; gap: 6px; flex-wrap: wrap; }
.plantbtn { width: 100%; margin-top: 8px; }
.plantbtn.connected { border-color: var(--in-on); color: var(--in-on); }
.plantbtn.connected::after { content: " · verbunden"; font-size: 12px; }
.simstate { display: flex; justify-content: space-between; margin-top: 8px; font-size: 12.5px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:empty::after { content: "–"; color: var(--muted); }
.chip {
  font-family: ui-monospace, Consolas, monospace; font-size: 12.5px;
  padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--btn);
}
.chip.in.on { background: var(--in-on); border-color: var(--in-on); color: #fff; }
.chip.out { cursor: default; }
.chip.out.on { background: var(--on); border-color: #d9a400; color: var(--on-text); }

.help ul { padding-left: 18px; margin: 4px 0; font-size: 12.5px; }
.help table { border-collapse: collapse; font-size: 12.5px; width: 100%; }
.help td { padding: 3px 4px; border-top: 1px solid var(--border); vertical-align: top; }
.help code { font-size: 12px; background: var(--hover); padding: 0 3px; border-radius: 3px; }
.help summary { cursor: pointer; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: #1d2330; color: #fff; padding: 8px 14px; border-radius: 8px; opacity: 0;
  transition: opacity .2s, transform .2s; pointer-events: none; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); }

@media (max-width: 900px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 65vh auto; }
  .tools { flex-direction: row; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .tools hr { width: 1px; height: auto; border-top: 0; border-left: 1px solid var(--border); margin: 0 4px; }
  .tools button { flex: 0 0 auto; }
  .side { border-left: 0; overflow: visible; }
  .topbar { padding: 8px 16px; }
}

/* ---------- SPS-Export-Dialog ---------- */
.dlg {
  width: min(960px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 0;
  border: 1px solid var(--border); border-radius: 10px; background: var(--panel); color: var(--text);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
}
.dlg[open] { display: flex; flex-direction: column; }
.dlg-small { width: min(380px, calc(100vw - 32px)); }
.dlg-small form { display: flex; flex-direction: column; }
.dlg-small input { width: 100%; }
.dlg::backdrop { background: rgba(10, 14, 20, .45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.dlg-head h2 { margin: 0; font-size: 16px; }
.dlg-body { padding: 12px 16px; overflow-y: auto; flex: 1; }
.dlg-body h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 14px 0 6px; }
.dlg-row { display: flex; gap: 12px; flex-wrap: wrap; }
.dlg-row .field { flex: 1 1 220px; }
.dlg-row select, .dlg-row input { width: 100%; }
.dlg-foot { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 10px 16px; border-top: 1px solid var(--border); }
.small { font-size: 12.5px; }
.tblwrap { max-height: 300px; overflow: auto; border: 1px solid var(--border); border-radius: 6px; }
#symTbl { border-collapse: collapse; width: 100%; font-size: 13px; }
#symTbl th { position: sticky; top: 0; background: var(--btn); text-align: left; font-weight: 600; padding: 5px 6px; border-bottom: 1px solid var(--border); z-index: 1; }
#symTbl td { padding: 2px 4px; border-bottom: 1px solid var(--border); }
#symTbl td:first-child { font-family: ui-monospace, Consolas, monospace; font-weight: 600; white-space: nowrap; padding-left: 8px; }
#symTbl td:nth-child(2) { color: var(--muted); text-align: center; }
#symTbl input { width: 100%; min-width: 80px; padding: 3px 6px; border-radius: 4px; }
#symTbl input.mono { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
#symTbl tr.empty td:first-child { color: #b26a00; }
#pasteBox { margin-top: 10px; }
#pasteBox summary { cursor: pointer; font-size: 13px; color: var(--accent); }
#pasteBox textarea { width: 100%; margin: 6px 0; font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
.preview { width: 100%; font-family: ui-monospace, Consolas, monospace; font-size: 12px; line-height: 1.45; white-space: pre; }
@media (max-width: 640px) {
  #symTbl th:nth-child(5), #symTbl td:nth-child(5) { display: none; }
}

.chip.num { font-variant-numeric: tabular-nums; }
.chip.numin input { width: 64px; padding: 0 4px; margin-left: 2px; }

.trace-bar { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin-bottom: 8px; font-size: 13px; }
.trace-bar label { display: inline-flex; gap: 4px; align-items: center; }
.trace-chart { overflow-x: auto; background: #fff; border: 1px solid var(--border); border-radius: 6px; min-height: 80px; }
.trace-chart svg { display: block; }
.trace-chart p { padding: 12px; margin: 0; color: #555; }

.print-form label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); margin-bottom: 8px; flex: 1; }
.print-form input, .print-form select { width: 100%; }
.print-form .row2 { display: flex; gap: 10px; }

/* ---------- Hilfe-Popup mit Animationen ---------- */
.help-dlg { width: min(1060px, calc(100vw - 24px)); height: min(760px, calc(100vh - 24px)); }
.help-dlg[open] { animation: helpPop .32s cubic-bezier(.2, .9, .3, 1.15); }
.help-dlg[open]::backdrop { animation: helpFade .3s ease-out; }
@keyframes helpPop { from { opacity: 0; transform: translateY(18px) scale(.95); } to { opacity: 1; transform: none; } }
@keyframes helpFade { from { opacity: 0; } to { opacity: 1; } }
.help-body { display: grid; grid-template-columns: 200px 1fr; min-height: 0; flex: 1; }
.help-nav { border-right: 1px solid var(--border); padding: 8px; overflow-y: auto; display: flex; flex-direction: column; gap: 3px; }
.help-nav button { text-align: left; border-color: transparent; background: transparent; }
.help-nav button:hover { background: var(--hover); }
.help-nav button.on { background: var(--accent); color: var(--accent-text); }
.help-nav .hnum { display: inline-block; width: 22px; opacity: .6; font-variant-numeric: tabular-nums; }
.help-main { padding: 12px 18px 18px; overflow-y: auto; }
.help-main.enter { animation: helpTopic .3s ease-out; }
@keyframes helpTopic { from { opacity: 0; transform: translateX(14px); } to { opacity: 1; transform: none; } }
.help-main h3 { margin: 4px 0 8px; font-size: 18px; }
.help-main p { line-height: 1.5; margin: 0 0 12px; }
.help-main table { border-collapse: collapse; width: 100%; margin-top: 6px; }
.help-main td { border-bottom: 1px solid var(--border); padding: 5px 8px; vertical-align: top; }
.help-main td:first-child { white-space: nowrap; width: 1%; }
.help-main code { font-family: ui-monospace, Consolas, monospace; }
.help-ex { margin-top: 14px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.hanim { border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin-bottom: 12px; background: var(--btn); }
.hstage { background: #fff; border-radius: 6px; overflow-x: auto; display: flex; justify-content: center; padding: 6px; }
.hsvg text { font-family: ui-monospace, Consolas, monospace; font-size: 12px; fill: #111; }
.hw { fill: none; stroke: #111; stroke-width: 1.5; }
.har { fill: #111; }
.hs .hbox { fill: #fff; stroke: #111; stroke-width: 1.5; transition: fill .35s; }
.hs .hin { fill: none; stroke: #111; stroke-width: 1.5; }
.hs .hnum { font-weight: 700; font-size: 12.5px; }
.hs .htok { fill: #111; opacity: 0; transition: opacity .3s; }
.hs.on .hbox { fill: #bfeeb7; }
.hs.on .htok { opacity: 1; }
.ht path { stroke: #111; stroke-width: 4; transition: stroke .3s; }
.ht.fire path { stroke: #16a34a; animation: hFire .9s ease-out; }
@keyframes hFire { 0% { stroke-width: 10; } 100% { stroke-width: 4; } }
.ha rect { fill: #fff; stroke: #111; stroke-width: 1.5; transition: fill .3s; }
.ha.on rect { fill: #ffdf7e; }
.ha rect.hin2 { fill: none; stroke-width: 1.2; }
.hbarbg { fill: #e5e7eb; }
.hbar { fill: #e08a00; transition: width .55s linear; }
.heval { font-size: 12px; fill: #334155 !important; }
.hcnt { fill: #ede9fe; stroke: #7c3aed; }
.hcntv { font-size: 24px !important; font-weight: 700; fill: #4c1d95 !important; }
.hexp { fill: none; stroke: #94a3b8; stroke-dasharray: 6 4; }
.hg { fill: none; stroke: #64748b; stroke-dasharray: 8 5; stroke-width: 1.5; transition: stroke .3s; }
.hg.forced { stroke: #d97706; stroke-width: 2.5; }
.hglabel { font-weight: 700; }
.hsig { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 4px; min-height: 24px; }
.hcap { min-height: 42px; font-size: 14px; line-height: 1.45; padding: 4px 2px; }
.hcap.show { animation: hCap .45s ease-out; }
@keyframes hCap { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }
.hctl { display: flex; gap: 6px; align-items: center; }
.hctl .hpos { margin-left: 6px; font-size: 12px; color: var(--muted); }
@media (max-width: 700px) {
  .help-body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .help-nav { flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .help-nav button { white-space: nowrap; }
}
.hsig .chip { transition: background .3s, color .3s; }
.hsig .chip.on { background: var(--in-on); border-color: var(--in-on); color: #fff; }
.hstage svg { width: 100%; max-width: 600px; height: auto; }
