:root {
  --bg: #05070b; --panel: #0e1118; --panel2: #171b26; --line: #2b3140;
  --text: #eceef4; --muted: #8d95a8; --accent: #ffcc1a; --on: #34d27b; --warn: #ffcc1a; --bad: #ff5c5c;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, "Segoe UI", sans-serif; display: flex; flex-direction: column; }

.top { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 17px; }
.brand small { font-weight: 400; color: var(--muted); margin-left: 6px; font-size: 12px; }
.spacer { flex: 1; }
.run-ctl, .file-ctl, .seg { display: flex; align-items: center; gap: 6px; }
.clock { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 80px; }
.speed { color: var(--muted); font-size: 12px; display: flex; gap: 4px; align-items: center; }

.btn { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; font: inherit; cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #04121f; font-weight: 600; }
.btn.danger { color: var(--bad); }
.btn.active, .seg .btn.active { background: var(--accent); color: #04121f; border-color: var(--accent); }
.btn.running { background: var(--bad); border-color: var(--bad); color: #fff; }
select, input[type=number] { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; font: inherit; }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 270px 1fr 250px; gap: 10px; padding: 10px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow: auto; min-height: 0; }
.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 0 8px; }
.panel h2:first-child { margin-top: 0; }
.tasks select { width: 100%; }
.desc { color: var(--text); background: var(--panel2); border-radius: 8px; padding: 10px; margin: 10px 0; }
.row { display: flex; gap: 8px; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0; }
.results { list-style: none; padding: 0; margin: 12px 0 0; font-size: 13px; }
.results li { padding: 5px 8px; margin-bottom: 4px; border-radius: 6px; background: var(--panel2); border-left: 4px solid var(--line); }
.results li.ok { border-left-color: var(--on); }
.results li.bad { border-left-color: var(--bad); }
.results li.summary { font-weight: 700; font-size: 14px; }

.editor { padding: 0; display: flex; }
.host { flex: 1; min-width: 0; overflow: auto; padding: 12px; }
.fbd-host { padding: 0; display: none; }

/* IO panel */
.io-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.inp { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.inp .tag, .lamp .tag { font-size: 11px; color: var(--muted); }
.sw { width: 38px; height: 22px; border-radius: 12px; background: var(--panel2); border: 1px solid var(--line); position: relative; cursor: pointer; padding: 0; }
.sw::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: .12s; }
.sw.on { background: #14452b; border-color: var(--on); }
.sw.on::after { left: 18px; background: var(--on); }
.push { font-size: 10px; padding: 1px 8px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel2); color: var(--muted); cursor: pointer; user-select: none; touch-action: none; }
.push.on { border-color: var(--on); color: var(--on); }
.lamp { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.lamp i { width: 22px; height: 22px; border-radius: 50%; background: #2a323c; border: 1px solid var(--line); }
.lamp.on i { background: var(--on); box-shadow: 0 0 12px var(--on); border-color: var(--on); }
.lamp.m.on i { background: var(--warn); box-shadow: 0 0 10px var(--warn); border-color: var(--warn); }
.tc { display: grid; gap: 4px; font-size: 12px; font-variant-numeric: tabular-nums; }
.tc div { display: flex; justify-content: space-between; background: var(--panel2); border-radius: 6px; padding: 3px 8px; color: var(--muted); }
.tc div.on { color: var(--on); }

/* ladder */
.rungs { display: flex; flex-direction: column; gap: 14px; }
.rung { display: flex; align-items: center; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 8px; }
.rung.powered { border-color: #1f6b42; }
.rung-no { width: 24px; text-align: center; color: var(--muted); font-weight: 700; }
.rail { width: 4px; align-self: stretch; background: var(--muted); border-radius: 2px; }
.branch-col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; align-items: flex-start; }
.branches { width: 100%; display: flex; flex-direction: column; gap: 8px; }
.branch { display: flex; align-items: center; width: 100%; }
.w { display: inline-block; width: 14px; height: 3px; background: var(--line); flex: none; }
.w.grow { flex: 1; min-width: 14px; }
.w.live { background: var(--on); }
.coil-wrap { display: flex; align-items: center; }
.cell { display: flex; flex-direction: column; align-items: center; min-width: 62px; padding: 3px 8px; background: var(--panel2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; cursor: pointer; font: inherit; }
.cell:hover { border-color: var(--accent); }
.cell .addr { font-size: 11px; color: var(--muted); }
.cell .sym { font-family: ui-monospace, Consolas, monospace; font-size: 15px; white-space: nowrap; }
.cell.live { border-color: var(--on); }
.cell.live .sym { color: var(--on); }
.mini { background: transparent; color: var(--muted); border: 1px solid var(--line); border-radius: 5px; padding: 1px 7px; margin-left: 5px; cursor: pointer; font: inherit; }
.mini:hover { color: var(--text); border-color: var(--accent); }
.mini.danger:hover { color: var(--bad); border-color: var(--bad); }
.mini.link { border: 0; color: var(--accent); margin: 2px 0 0; }
.rung-tools { display: flex; flex-direction: column; gap: 3px; }
.add-rung { align-self: flex-start; }

/* dialog */
dialog.dlg { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 18px; min-width: 300px; }
dialog.dlg::backdrop { background: rgba(0, 0, 0, .55); }
dialog.dlg h3 { margin: 0 0 12px; }
.frow { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.frow span { color: var(--muted); font-size: 12px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

/* FBD */
.palette { width: 144px; flex: none; padding: 10px; border-right: 1px solid var(--line); overflow: auto; display: flex; flex-direction: column; gap: 5px; }
.palette h4 { margin: 0 0 4px; color: var(--muted); font-size: 12px; text-transform: uppercase; }
.palette .btn.pal { padding: 4px 8px; text-align: left; font-size: 13px; }
.fbd-scroll { flex: 1; overflow: auto; background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 20px 20px; }
.fbd-svg { display: block; }
.node { cursor: grab; }
.nbody { fill: var(--panel2); stroke: var(--line); stroke-width: 1.5; }
.nhead { fill: #2a3745; }
.ntitle { fill: var(--text); font-size: 12px; font-weight: 700; }
.nedit { fill: var(--muted); font-size: 13px; cursor: pointer; }
.nedit:hover { fill: var(--accent); }
.nlabel { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; pointer-events: none; }
.node.on .nbody { stroke: var(--on); }
.node.on .nlabel { fill: var(--on); }
.port { fill: var(--bg); stroke: var(--muted); stroke-width: 2; }
.port.out { cursor: crosshair; }
.port.out:hover, .port.in:hover { stroke: var(--accent); fill: var(--accent); }
.plabel { fill: var(--muted); font-size: 10px; pointer-events: none; }
.wire { fill: none; stroke: var(--muted); stroke-width: 2.5; pointer-events: none; }
.wire.live { stroke: var(--on); }
.wire.tmp { stroke: var(--accent); stroke-dasharray: 5 4; }
.wire-hit { fill: none; stroke: transparent; stroke-width: 12; cursor: pointer; }
.wire-hit:hover { stroke: rgba(255, 92, 92, .35); }

/* drag & drop */
.ladder-host { padding: 0; display: flex; overflow: hidden; }
.ladder-main { flex: 1; min-width: 0; overflow: auto; padding: 12px; }
.palette h5 { margin: 8px 0 2px; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.palette .btn.pal { touch-action: none; user-select: none; cursor: grab; }
.psym { display: inline-block; min-width: 30px; font-family: ui-monospace, Consolas, monospace; color: var(--accent); }
.ghost { position: fixed; z-index: 1000; pointer-events: none; transform: translate(-50%, -50%); background: var(--accent); color: #04121f; font-weight: 600; padding: 5px 12px; border-radius: 8px; box-shadow: 0 6px 18px rgba(0, 0, 0, .5); }
body.dragging { cursor: grabbing; user-select: none; }
.drop-hot { outline: 2px dashed var(--accent); outline-offset: 2px; }
.fbd-scroll.drop-hot { outline-offset: -3px; background-color: rgba(59, 158, 255, .06); }

/* delete buttons */
.cw { position: relative; display: inline-flex; }
.xbtn { position: absolute; top: -8px; right: -8px; width: 17px; height: 17px; border-radius: 50%; background: var(--bad); color: #fff; font-size: 13px; font-weight: 700; line-height: 16px; text-align: center; cursor: pointer; opacity: .75; z-index: 2; user-select: none; }
.xbtn:hover { opacity: 1; transform: scale(1.15); }
.cell.coil.empty { border-style: dashed; color: var(--muted); }
.ndel { cursor: pointer; }
.ndel rect { fill: transparent; }
.ndel text { fill: var(--bad); font-size: 16px; font-weight: 700; }
.ndel:hover rect { fill: rgba(255, 92, 92, .25); }

@media (max-width: 1000px) {
  #tools { overflow-y: auto; }
  .layout { grid-template-columns: 1fr; flex: none; }
  .editor { min-height: 420px; }
}
