:root {
  /* Layout: toolbar across the top; palette | rungs | side panel below it; the trend drawer opens under the rungs. */
  --bg: #e9edf2;
  --panel: #ffffff;
  --panel-2: #f3f5f8;
  --ink: #17202b;
  --muted: #566475;
  --line: #cdd5df;
  --accent: #0b5fc2;
  --accent-ink: #ffffff;
  --accent-soft: #d9e8fb;
  --wire: #6f7c8c;
  --on: #0d8539;
  --on-soft: #cdeed9;
  --bad: #c22525;
  --bad-soft: #fbe0e0;
  --warn: #8a4f00;
  --warn-soft: #ffe8bd;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11161c; --panel: #1a2129; --panel-2: #222b35; --ink: #e6ebf1; --muted: #9ba8b7; --line: #35414e;
    --accent: #62a8f7; --accent-ink: #0a131f; --accent-soft: #1c3351;
    --wire: #8996a6; --on: #40d078; --on-soft: #17412b;
    --bad: #ff7070; --bad-soft: #4b2020; --warn: #f3b550; --warn-soft: #463413;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11161c; --panel: #1a2129; --panel-2: #222b35; --ink: #e6ebf1; --muted: #9ba8b7; --line: #35414e;
  --accent: #62a8f7; --accent-ink: #0a131f; --accent-soft: #1c3351;
  --wire: #8996a6; --on: #40d078; --on-soft: #17412b;
  --bad: #ff7070; --bad-soft: #4b2020; --warn: #f3b550; --warn-soft: #463413;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--ink);
  font: 15px/1.35 var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
h2, h3, p { margin: 0; }

/* ---------- controls ---------- */
button, select, input, textarea { font: inherit; color: inherit; }
button, .filebtn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); cursor: pointer;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
button:disabled { opacity: 0.42; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.danger { color: var(--bad); border-color: var(--bad); }
button.icon { width: 44px; padding: 0; justify-content: center; }
button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
button svg { width: 18px; height: 18px; fill: currentColor; flex: none; }
button.icon svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input[type="text"], select, textarea {
  min-height: 44px; padding: 0 10px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
  min-width: 0;
}
textarea { width: 100%; padding: 8px 10px; font: 12px/1.4 var(--mono); resize: vertical; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
label.inline { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

/* ---------- toolbar ---------- */
.bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line);
}
.grp { display: flex; align-items: center; gap: 6px; }
.grp.end { margin-left: auto; }
.proj { flex-direction: column; align-items: flex-start; gap: 0; padding: 3px 12px; line-height: 1.15; max-width: 170px; }
.bar .grp.run button { padding: 0 12px; }
.proj-kicker { font-size: 11px; color: var(--muted); }
#btn-project-name { font-weight: 600; max-width: 100%; overflow-wrap: anywhere; text-align: left; }
#btn-run .i-pause, #btn-run.is-playing .i-play { display: none; }
#btn-run.is-playing .i-pause { display: block; }
.status { display: flex; align-items: center; gap: 10px; min-width: 0; }
.pill { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; background: var(--panel-2); color: var(--muted); }
.pill[data-mode="run"] { background: var(--on-soft); color: var(--on); }
.pill[data-mode="pause"] { background: var(--warn-soft); color: var(--warn); }
.readout { display: flex; flex-wrap: wrap; gap: 2px 12px; font: 13px var(--mono); font-variant-numeric: tabular-nums; color: var(--muted); }
.readout .fault { color: var(--bad); }
.pill-btn { border-radius: 999px; font-size: 13px; font-weight: 600; color: var(--muted); }
.pill-btn:disabled { opacity: 1; }
.pill-btn[data-level="e"] { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.pill-btn[data-level="w"] { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }

#phone-nav { display: none; }

/* ---------- main layout ---------- */
main { flex: 1; min-height: 0; display: grid; grid-template-columns: 124px minmax(0, 1fr) 296px; }

#palette {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px; align-content: start;
  padding: 8px; overflow-y: auto; background: var(--panel); border-right: 1px solid var(--line);
}
#palette hr { grid-column: 1 / -1; width: 100%; margin: 1px 0; border: 0; border-top: 1px solid var(--line); }
.tool { flex-direction: column; justify-content: center; gap: 0; min-height: 50px; padding: 3px 2px; font-size: 10.5px; color: var(--muted); }
.tool svg { width: 40px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; color: var(--ink); }
body.online #palette { opacity: 0.38; pointer-events: none; }

#work { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#messages { flex: none; max-height: 30%; overflow-y: auto; background: var(--panel); border-bottom: 1px solid var(--line); }
#messages ul { list-style: none; margin: 0; padding: 4px 8px; }
.msg { width: 100%; flex-wrap: wrap; gap: 2px 8px; padding: 6px 8px; border: 0; background: none; text-align: left; font-size: 14px; }
.msg b { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.msg.e b { color: var(--bad); }
.msg.w b { color: var(--warn); }
.msg .where { font-weight: 600; }

#rungs, #side, #palette, #trend { touch-action: manipulation; }
#rungs {
  flex: 1; min-height: 0; overflow: auto; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 12px; padding: 12px 16px 28px;
}
.rung { flex: none; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.rung.err { border-color: var(--bad); }
.rung.flash { box-shadow: 0 0 0 4px var(--accent-soft); }
.rung header { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.rung-no { min-width: 30px; text-align: center; font: 700 14px var(--mono); color: var(--muted); }
.rung-comment { flex: 1; border-color: transparent !important; background: transparent !important; font-size: 15px !important; }
.rung-comment:disabled { color: var(--muted); opacity: 1; -webkit-text-fill-color: var(--muted); }
.rung header .icon { border-color: transparent; background: none; color: var(--muted); font-size: 13px; }
.rung-body { padding: 4px 8px 6px; overflow-x: auto; }
.rung-svg { display: block; width: 100%; height: auto; min-width: 660px; }
.add-rung { flex: none; align-self: flex-start; border-style: dashed; color: var(--muted); }
.rung-adds { flex: none; display: flex; flex-wrap: wrap; gap: 8px; }
.add-rung.paste, .rung header .icon.paste { color: var(--accent); }

/* ---------- ladder drawing ---------- */
.w { stroke: var(--wire); stroke-width: 2; fill: none; stroke-linecap: round; }
.w.on { stroke: var(--on); stroke-width: 3.6; }
.s { stroke: var(--ink); stroke-width: 2.2; fill: none; stroke-linecap: round; }
.rail { stroke: var(--ink); stroke-width: 3; stroke-linecap: round; }
.rail.on { stroke: var(--on); }
.node { fill: var(--wire); }
.node.on { fill: var(--on); }
.glow { fill: none; }
.contact.on .glow, .coil.on .glow { fill: var(--on-soft); }
.contact.on .s, .coil.on .s { stroke: var(--on); stroke-width: 3; }
.box { fill: var(--panel-2); stroke: var(--ink); stroke-width: 1.6; }
.lb, .bt, .bp, .bv, .mk { text-anchor: middle; font-family: var(--mono); }
.lb { font-size: 12px; fill: var(--ink); }
.bt { font-size: 11px; font-weight: 700; fill: var(--ink); }
.bp { font-size: 10.5px; fill: var(--muted); }
.bv { font-size: 10px; font-weight: 700; fill: var(--on); }
.mk { font-size: 11px; font-weight: 700; fill: var(--ink); }
.dot { fill: var(--line); }
.hit { fill: transparent; cursor: pointer; }
.hit.err { fill: var(--bad-soft); fill-opacity: 0.45; stroke: var(--bad); stroke-width: 1.5; stroke-dasharray: 5 4; }
body:not(.online) .hit:active { fill: var(--accent-soft); fill-opacity: 0.55; }
.hit.cur { fill: var(--accent-soft); fill-opacity: 0.5; stroke: var(--accent); stroke-width: 2.5; }
.fb-next { fill: var(--accent-soft); fill-opacity: 0.35; stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 5; pointer-events: none; }
.lh circle { fill: var(--panel); stroke: var(--accent); stroke-width: 2; }
.lh.has circle { fill: var(--accent); }
.lh .ghost { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 3 5; opacity: 0.55; }
.lh.has .ghost { opacity: 0; }

/* ---------- first-visit tip and help ---------- */
#welcome { flex: none; display: flex; align-items: center; gap: 10px; margin: 10px 16px 0; padding: 6px 6px 6px 14px; border: 1px solid var(--accent); border-radius: 10px; background: var(--accent-soft); }
#welcome p { flex: 1; min-width: 0; font-size: 14px; }
#welcome .icon { border-color: transparent; background: none; }
button.help { border-radius: 50%; font-weight: 700; color: var(--muted); }
ol.help { margin: 0 0 12px; padding-left: 22px; display: flex; flex-direction: column; gap: 8px; font-size: 14.5px; }
dialog a { color: var(--accent); }

/* ---------- program tabs ---------- */
#tabs { flex: none; display: flex; gap: 6px; padding: 8px 16px 0; overflow-x: auto; }
.tab { flex: none; gap: 8px; border-radius: 10px 10px 0 0; border-bottom-color: transparent; background: var(--panel-2); color: var(--muted); font-weight: 600; }
.tab[aria-selected="true"] { background: var(--panel); color: var(--ink); border-color: var(--line); border-bottom-color: var(--panel); }
.tab small { font-weight: 500; font-size: 11px; color: var(--muted); }
.tab-n { font: 700 12px var(--mono); color: var(--muted); }
.tab.err { color: var(--bad); }
.tab.add { border-style: dashed; background: none; font-weight: 500; }

/* ---------- function block diagram ---------- */
#fbd { flex: 1; min-height: 0; overflow: auto; -webkit-overflow-scrolling: touch; background: var(--panel); border-top: 1px solid var(--line); }
#fbd-svg { display: block; min-width: 100%; min-height: 100%; touch-action: pan-x pan-y; }
#fbd-svg, .rung-svg { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.gdot { fill: var(--line); }
.fbd-bg { cursor: crosshair; }
.fb-box { fill: var(--panel); stroke: var(--ink); stroke-width: 1.6; }
.fb-box.tag { fill: var(--panel-2); }
.fb-box.tag.on { fill: var(--on-soft); stroke: var(--on); stroke-width: 2.4; }
.fb.err .fb-box { stroke: var(--bad); stroke-width: 2.2; stroke-dasharray: 6 4; }
.fb.dragging .fb-box { stroke: var(--accent); stroke-width: 2.4; }
.fb-rule { stroke: var(--line); stroke-width: 1; }
.fb-title, .fb-name, .pin-l, .pin-r, .pin-const, .pin-v, .ord-n { font-family: var(--mono); }
.fb-title { font-size: 12px; font-weight: 700; fill: var(--ink); text-anchor: middle; }
.fb-name { font-size: 13px; font-weight: 600; fill: var(--ink); text-anchor: middle; }
.pin-l, .pin-r { font-size: 10.5px; fill: var(--muted); }
.pin-r { text-anchor: end; }
.pin-const { font-size: 11.5px; fill: var(--accent); text-anchor: end; }
.pin-v { font-size: 11px; font-weight: 700; fill: var(--on); }
.fw { stroke: var(--wire); stroke-width: 2; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.fw.on { stroke: var(--on); stroke-width: 3.6; }
.fw-hit { stroke: transparent; stroke-width: 26; fill: none; cursor: pointer; }
.pt { fill: var(--panel); stroke: var(--wire); stroke-width: 1.8; }
.pt.wired, .pt.const { fill: var(--wire); }
.pt.opt { stroke-dasharray: 2 2; }
.pt.pending { fill: var(--accent); stroke: var(--accent); }
.ord { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1.2; }
.ord-n { font-size: 10.5px; font-weight: 700; fill: var(--accent); text-anchor: middle; }
.rubber { stroke: var(--accent); stroke-width: 2.5; stroke-dasharray: 6 5; pointer-events: none; }
#fbd .hit, #fbd .fw-hit { touch-action: none; }
body:not(.online) #fbd .hit.pin:active { fill: var(--accent-soft); fill-opacity: 0.6; }

/* ---------- trend ---------- */
#trend { flex: none; max-height: 46%; display: flex; flex-direction: column; background: var(--panel); border-top: 1px solid var(--line); }
.trend-head { display: flex; align-items: flex-start; gap: 10px; padding: 8px 12px; }
#trend-chips { flex: 1; display: flex; flex-wrap: wrap; gap: 6px; min-width: 0; }
.chip { min-height: 44px; padding: 0 12px; border-radius: 999px; font: 12.5px var(--mono); }
.trend-plot { flex: 1; min-height: 0; overflow-y: auto; }
#trend-canvas { display: block; width: 100%; }

/* ---------- side panel ---------- */
#side { overflow-y: auto; padding: 10px 12px 28px; background: var(--panel); border-left: 1px solid var(--line); }
#side h2 { margin: 16px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
#side h2:first-child { margin-top: 2px; }
.empty { color: var(--muted); font-size: 14px; padding: 4px 0; }
.vars { display: flex; flex-direction: column; gap: 8px; }
.var { display: grid; grid-template-columns: minmax(0, 1fr) 96px 44px; gap: 6px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.var.err { border-color: var(--bad); }
.var .v-name { font-family: var(--mono); font-weight: 600; }
.var .icon { grid-column: 3; grid-row: 1; }
.var select[data-f="type"] { grid-column: 2; grid-row: 1; padding: 0 6px; }
.var select[data-f="kind"] { grid-column: 1; }
.var .v-init { grid-column: 2 / 4; }
.var .v-note { grid-column: 1 / -1; }
.var .v-note { align-self: center; color: var(--muted); font-size: 13px; }
.var .icon { border-color: transparent; background: none; color: var(--muted); }
button.wide { width: 100%; justify-content: center; margin-top: 10px; }
.io-row { display: flex; align-items: center; gap: 10px; min-height: 50px; border-bottom: 1px solid var(--line); }
.tag { flex: 1; min-width: 0; font: 14px var(--mono); overflow-wrap: anywhere; }
.val { font: 14px var(--mono); font-variant-numeric: tabular-nums; }
.led { flex: none; width: 18px; height: 18px; border-radius: 50%; background: var(--panel-2); border: 2px solid var(--wire); }
.led.on { background: var(--on); border-color: var(--on); box-shadow: 0 0 0 4px var(--on-soft); }
.switch { position: relative; flex: none; width: 68px; padding: 0; border-radius: 999px; background: var(--panel-2); }
.switch span { position: absolute; left: 5px; top: 50%; width: 32px; height: 32px; border-radius: 50%; background: var(--wire); transform: translateY(-50%); transition: transform 0.12s; }
.switch[aria-pressed="true"] { background: var(--on-soft); border-color: var(--on); }
.switch[aria-pressed="true"] span { background: var(--on); transform: translate(24px, -50%); }
input.num { width: 112px; text-align: right; font-family: var(--mono); }

/* ---------- side tabs, watch table, forcing, alarms, script ---------- */
#side { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
#side-tabs { flex: none; display: flex; gap: 4px; padding: 8px 8px 0; border-bottom: 1px solid var(--line); }
.stab { flex: 1; justify-content: center; gap: 4px; padding: 0 2px; border-radius: 8px 8px 0 0; border-bottom-color: transparent; background: none; color: var(--muted); font-size: 13px; font-weight: 600; }
.stab[aria-selected="true"] { background: var(--accent-soft); border-color: var(--accent); border-bottom-color: transparent; color: var(--ink); }
.stab .n { min-width: 18px; padding: 0 5px; border-radius: 999px; background: var(--bad); color: var(--panel); font-size: 11px; line-height: 18px; }
#side-body { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 12px 28px; }
#side-body > .hint { margin-bottom: 10px; }
.badge { flex: none; padding: 2px 7px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.badge.forced { background: var(--warn-soft); color: var(--warn); }
.io-row { flex-wrap: wrap; }
.io-row .badge.forced { order: 9; flex: 0 0 100%; margin-top: -6px; padding: 0 0 8px; background: none; }
button.force { font: 700 13px var(--mono); color: var(--muted); }
button.force.active { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
button.release { margin: 0 0 4px; background: var(--warn-soft); border-color: var(--warn); color: var(--warn); font-weight: 600; }
.contact.forced .glow, .coil.forced .glow { stroke: var(--warn); stroke-width: 2.5; stroke-dasharray: 4 3; }
.w-row { padding: 8px 0; border-bottom: 1px solid var(--line); }
.w-top { display: flex; align-items: baseline; gap: 8px; }
.w-sub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.rule, .step { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px; gap: 6px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.rule.err { border-color: var(--bad); }
.rule > :first-child, .step > :first-child { grid-column: 1 / 3; }
.rule .icon, .step .icon { border-color: transparent; background: none; color: var(--muted); }
.rule .span { grid-column: 1 / -1; }
.rule .to-end, .step .to-end { grid-column: 2 / -1; }
.step input:disabled, .step select:disabled { opacity: 1; color: var(--muted); -webkit-text-fill-color: var(--muted); }
.al-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.al-main { flex: 1; min-width: 0; }
.al-text { font: 14px var(--mono); overflow-wrap: anywhere; }
.al-dot { flex: none; width: 14px; height: 14px; border-radius: 50%; background: var(--panel-2); border: 2px solid var(--wire); }
.al-row.active .al-dot { background: var(--bad); border-color: var(--bad); box-shadow: 0 0 0 4px var(--bad-soft); }
.al-row.acked .al-dot { background: var(--warn); border-color: var(--warn); }
.ev { display: flex; gap: 10px; padding: 4px 0; font: 12.5px var(--mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.ev b { min-width: 58px; }
.ev.up b { color: var(--bad); }
.ref { font: 14px var(--mono); margin-bottom: 4px; }

/* ---------- dialogs ---------- */
dialog {
  width: min(440px, calc(100% - 32px)); max-height: calc(100% - 32px); overflow-y: auto;
  padding: 18px; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--ink);
}
dialog::backdrop { background: rgba(8, 14, 22, 0.5); }
dialog h2 { font-size: 18px; margin-bottom: 14px; text-wrap: balance; }
dialog h3 { font-size: 14px; margin: 18px 0 4px; }
.fld { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.fld label { font-size: 13px; color: var(--muted); }
.hint { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.fld .hint { margin: 0; }
.newvar { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.newvar input { flex: 1; min-width: 130px; font-family: var(--mono); }
.form-error { color: var(--bad); font-size: 14px; margin-bottom: 10px; }
.actions { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.filebtn { position: relative; overflow: hidden; }
.filebtn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

#toast {
  display: none; position: fixed; inset: auto auto 24px 50%; transform: translateX(-50%);
  width: max-content; max-width: min(520px, calc(100% - 32px)); margin: 0;
  padding: 11px 16px; border: 0; border-radius: 10px;
  background: var(--ink); color: var(--panel); font-size: 14px;
  pointer-events: none; z-index: 20;
}
#toast.show { display: block; }

/* ---------- narrower screens ---------- */
@media (max-width: 900px) {
  main { grid-template-columns: 124px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  #side { grid-column: 1 / -1; max-height: 260px; border-left: 0; border-top: 1px solid var(--line); }
}
/* Phones, upright or on their side: one pane at a time, chosen with the bar at the bottom.
   The phone is for watching and operating a program; the edit tools are one tap away. */
@media (max-width: 600px), (max-height: 500px) {
  .bar { gap: 6px; padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .proj { max-width: 104px; padding: 3px 8px; }
  .proj-kicker { display: none; }
  body:not(.tools-open) #btn-undo, body:not(.tools-open) #btn-redo { display: none; }
  #btn-step span, #btn-stop span { display: none; }
  .bar .grp.run button { padding: 0 11px; }
  #sel-speed { padding: 0 4px; }
  .grp.end { margin-left: auto; }
  main { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  #palette { display: none; grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: 60px; overflow-x: auto; overflow-y: hidden; border-right: 0; border-bottom: 1px solid var(--line); }
  body.tools-open:not(.online) #palette { display: grid; }
  #palette hr { display: none; }
  #work, #side { grid-column: 1; grid-row: 2; }
  #side { max-height: none; border: 0; }
  body[data-view="panel"] #work, body:not([data-view="panel"]) #side { display: none; }
  #rungs { padding: 10px 8px 20px; }
  #welcome { margin: 8px 8px 0; flex-wrap: wrap; }
  #tabs { padding: 6px 8px 0; }
  /* Watching: the whole rung fits the screen, coils included; pinch to zoom in.
     Editing: cells keep a size a finger can hit, and the rung scrolls sideways. */
  .rung-svg { min-width: 0; }
  body.tools-open .rung-svg { min-width: 620px; }
  #phone-nav {
    flex: none; display: flex; gap: 6px;
    padding: 6px max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    background: var(--panel); border-top: 1px solid var(--line);
  }
  #phone-nav button { flex: 1; justify-content: center; font-weight: 600; }
  body.online #btn-tools { display: none; }
  /* while online the toolbar keeps to the controls and the readout */
  body.online .proj, body.online #btn-msgs[data-level="ok"] { display: none; }
  /* rung housekeeping belongs to editing */
  body:not(.tools-open) .rung header .icon, body:not(.tools-open) .rung-adds { display: none; }
  /* while editing, the comment keeps a full row and the rung buttons go below it */
  body.tools-open .rung header { flex-wrap: wrap; }
  body.tools-open .rung-comment { flex: 1 0 calc(100% - 44px); }
  body.tools-open .rung header .icon[data-act="up"] { margin-left: auto; }
  #trend { max-height: 58%; }
  .trend-head { padding: 6px 8px; }
  #trend-chips { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; }
  .chip { flex: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}

/* numeric inputs: signal settings in the variable table, slider in the I/O panel */
.var .v-signal { grid-column: 1 / -1; min-height: 40px; padding: 4px 10px; text-align: left; font-size: 13px; color: var(--muted); }
.var .v-signal.gen { color: var(--accent); font-weight: 600; }
.io-row .slider { order: 8; flex: 0 0 100%; min-height: 36px; margin: -4px 0 6px; accent-color: var(--accent); touch-action: pan-y; }
.io-row .io-note { order: 8; flex: 0 0 100%; margin-top: -6px; padding-bottom: 8px; font-size: 12px; color: var(--muted); }

/* project list and example list in the project dialog */
.plist { display: flex; flex-direction: column; gap: 6px; margin: 6px 0 10px; }
.pitem { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.pitem.cur { border-color: var(--accent); }
.pitem .pname { flex: 1; min-width: 0; font-weight: 600; overflow-wrap: anywhere; }
.pitem .pname small { display: block; font-weight: 400; font-size: 12.5px; color: var(--muted); }
.pitem .pnote { font-size: 12px; color: var(--accent); padding: 0 6px; }
.pitem button { min-height: 40px; padding: 0 10px; font-size: 13px; }
@media (max-width: 600px) { .pitem { flex-wrap: wrap; } .pitem .pname { flex: 0 0 100%; } }

/* program checks in the problems list */
.msg.c b { color: var(--accent); }
#btn-msgs[data-level="c"] { border-color: var(--accent); color: var(--accent); }
.var.flash { outline: 3px solid var(--accent); }

/* ---------- report ---------- */
#report { position: fixed; inset: 0; z-index: 50; overflow: auto; background: var(--panel); color: var(--ink); -webkit-overflow-scrolling: touch; }
#report[hidden] { display: none; }
.rep-bar { position: sticky; top: 0; z-index: 1; display: flex; gap: 8px; padding: 10px 16px; background: var(--panel-2); border-bottom: 1px solid var(--line); }
#report-body { max-width: 1000px; margin: 0 auto; padding: 20px 16px 60px; }
.rep-head h1 { margin: 0 0 4px; font-size: 26px; }
.rep-head p { margin: 0; color: var(--muted); font-size: 13px; }
.rep-table + p { margin-top: 6px; }
#report-body h2 { margin: 28px 0 10px; padding-bottom: 4px; border-bottom: 2px solid var(--ink); font-size: 17px; break-after: avoid; }
.rep-block { margin-bottom: 14px; break-inside: avoid; page-break-inside: avoid; }
.rep-rung { margin: 0 0 2px; font-size: 13px; }
.rep-empty { color: var(--muted); }
#report-body svg { display: block; width: 100%; height: auto; }
.rep-fbd { border: 1px solid var(--line); border-radius: 6px; }
.rep-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.rep-table th, .rep-table td { padding: 5px 8px; border: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
.rep-table th { background: var(--panel-2); }
.rep-table td:first-child { font-family: var(--mono); }
.rep-table tr { break-inside: avoid; }

@media print {
  /* paper is white whatever the theme on screen */
  html {
    --bg: #ffffff !important; --panel: #ffffff !important; --panel-2: #f3f5f8 !important; --ink: #000000 !important; --muted: #444444 !important;
    --line: #999999 !important; --accent: #0b5fc2 !important; --accent-soft: #d9e8fb !important; --wire: #333333 !important;
    --on: #0d8539 !important; --on-soft: #cdeed9 !important;
    color-scheme: light !important;
  }
  html, body { height: auto !important; overflow: visible !important; background: #fff !important; }
  body.reporting { display: block !important; }
  body.reporting > *:not(#report) { display: none !important; }
  #report { position: static !important; overflow: visible !important; }
  .rep-bar { display: none !important; }
  #report-body { max-width: none; padding: 0; }
  @page { margin: 14mm; }
}

/* reminder to save projects to a file when the browser may clear its storage */
.hint.warn { padding: 6px 10px; border-left: 3px solid var(--warn); background: var(--warn-soft); color: var(--ink); }

/* copy and paste of FBD blocks */
.clipbar { flex: none; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 10px; background: var(--accent-soft); border-top: 1px solid var(--line); }
.clipbar .clip-msg { flex: 1; min-width: 12em; font-size: 13.5px; }
.clipbar button { min-height: 40px; }
#fbd .hit.sel { fill: var(--accent-soft); fill-opacity: 0.45; stroke: var(--accent); stroke-width: 2.5; stroke-dasharray: 6 4; }
#fbd-svg.pasting { cursor: copy; }
