:root {
  --bg: #eef0ec;
  --paper: #fbfbf7;
  --panel: #ffffff;
  --panel2: #f4f5f1;
  --ink: #23262b;
  --muted: #6b7078;
  --line: #d4d7d0;
  --wire: #8a8f96;
  --plus: #d32f2f;
  --minus: #1565c0;
  --mid: #8e3fae;
  --dead: #a9adb3;
  --iso: #e08a00;
  --flow: #ffcc00;
  --flow-edge: #5b4300;
  --on: #2e9e48;
  --on-fill: #c9efd2;
  --pull: #e0a800;
  --rel: #ef7d00;
  --rel-fill: #ffe0bd;
  --trip: #e53935;
  --accent: #0b6bcb;
  --accent-soft: #dcebfb;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .05);
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
:root[data-theme="dark"] {
  --bg: #15181c;
  --paper: #1b1f24;
  --panel: #20252b;
  --panel2: #262c33;
  --ink: #e6e8eb;
  --muted: #9aa1aa;
  --line: #343b44;
  --wire: #7a828c;
  --plus: #ff5a52;
  --minus: #4ea3ff;
  --mid: #c47ae0;
  --dead: #555d67;
  --iso: #ffab2e;
  --flow: #fff176;
  --flow-edge: #fff176;
  --on: #4cd06a;
  --on-fill: #1f4a2a;
  --pull: #ffd54f;
  --rel: #ff9e40;
  --rel-fill: #4d3317;
  --trip: #ff5a52;
  --accent: #5aa9ff;
  --accent-soft: #1e3550;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181c; --paper: #1b1f24; --panel: #20252b; --panel2: #262c33; --ink: #e6e8eb; --muted: #9aa1aa;
    --line: #343b44; --wire: #7a828c; --plus: #ff5a52; --minus: #4ea3ff; --mid: #c47ae0; --dead: #555d67;
    --iso: #ffab2e; --flow: #fff176; --flow-edge: #fff176; --on: #4cd06a; --on-fill: #1f4a2a; --pull: #ffd54f;
    --rel: #ff9e40; --rel-fill: #4d3317; --trip: #ff5a52; --accent: #5aa9ff; --accent-soft: #1e3550;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.35 var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }

/* ---------- верхняя панель */
.top {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 15px; margin: 0; font-weight: 650; letter-spacing: .01em; }
.top .sub { color: var(--muted); font-size: 12px; }
.top .spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 5px 10px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 11px; cursor: pointer;
}
.btn:hover, .seg button:hover { background: var(--panel2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.clock { font-family: var(--mono); color: var(--muted); min-width: 92px; text-align: right; }

/* ---------- раскладка */
.layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 340px;
  grid-template-rows: minmax(0, 1fr) 340px;
  grid-template-areas: "pult scheme mech" "pult bottom mech";
  height: calc(100vh - 49px);
  gap: 8px; padding: 8px;
}
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); min-height: 0; }
.pult { grid-area: pult; overflow: auto; padding: 10px 12px; }
.scheme { grid-area: scheme; position: relative; overflow: hidden; background: var(--paper); }
.mech { grid-area: mech; overflow: hidden; display: flex; flex-direction: column; }
.bottom { grid-area: bottom; display: grid; grid-template-columns: minmax(0, 1fr); min-height: 0; }
.logwrap { flex: 1; min-height: 140px; display: flex; flex-direction: column; border-top: 1px solid var(--line); }
.bottom .card { overflow: hidden; display: flex; flex-direction: column; }

h2 { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 0 6px; font-weight: 650; }
h2:first-child { margin-top: 2px; }

/* ---------- пульт */
.row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.tgl {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 9px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel2); user-select: none; min-width: 64px;
}
.tgl .led { width: 9px; height: 9px; border-radius: 50%; background: var(--dead); box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.tgl.on { border-color: color-mix(in srgb, var(--on) 50%, var(--line)); }
.tgl.on .led { background: var(--on); }
.tgl.bad { border-color: var(--trip); color: var(--trip); }
.tgl.bad .led { background: var(--trip); }
.push {
  padding: 8px 14px; border-radius: 22px; border: 2px solid var(--line); cursor: pointer; font-weight: 650;
  user-select: none; touch-action: none; background: var(--panel2);
}
.push.go { border-color: #2e7d32; color: #2e7d32; }
.push.stop { border-color: #c62828; color: #c62828; }
.push.pressed { transform: translateY(1px); filter: brightness(.9); background: var(--accent-soft); }
.estop {
  width: 46px; height: 46px; border-radius: 50%; border: 0; cursor: pointer; color: #fff; font-weight: 700; font-size: 11px;
  background: radial-gradient(circle at 35% 30%, #ff6b5f, #c62828 70%); box-shadow: 0 2px 0 #7f1414;
}
.estop.pressed { transform: translateY(2px); box-shadow: 0 0 0 3px #ffb300; }
.doors { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.doors .tgl { min-width: 0; justify-content: flex-start; }
.hint { color: var(--muted); font-size: 11.5px; margin: 6px 0 0; }
.prepare { width: 100%; margin-top: 4px; }

/* командоконтроллер */
.kk { display: grid; grid-template-columns: 82px 1fr; gap: 10px; align-items: start; }
.lever { position: relative; height: 290px; user-select: none; touch-action: none; }
.lever .slot { position: absolute; left: 44px; top: 8px; bottom: 8px; width: 8px; border-radius: 4px; background: var(--panel2); border: 1px solid var(--line); }
.lever .notch { position: absolute; left: 0; width: 74px; height: 27px; display: flex; align-items: center; cursor: pointer; font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
.lever .notch span { width: 24px; text-align: right; }
.lever .notch.cur span { color: var(--accent); font-weight: 700; }
.lever .notch.zero span { color: var(--ink); font-weight: 700; }
.lever .knob {
  position: absolute; left: 32px; width: 32px; height: 22px; border-radius: 6px; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 11px; cursor: grab;
  box-shadow: 0 2px 5px rgba(0,0,0,.25); transition: top .08s;
}
.lever .dir { position: absolute; left: 66px; font-size: 10px; font-weight: 700; letter-spacing: .06em; color: var(--muted); writing-mode: vertical-rl; }
.kktable { border-collapse: collapse; font-family: var(--mono); font-size: 10.5px; }
.kktable th, .kktable td { width: 16px; height: 17px; text-align: center; padding: 0; }
.kktable th { color: var(--muted); font-weight: 600; font-size: 9.5px; }
.kktable td.n { width: 22px; text-align: right; padding-right: 4px; color: var(--muted); }
.kktable td.dot::after { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--ink); opacity: .55; }
.kktable .cur { background: var(--accent-soft); }
.kktable tr.closed td.n { color: var(--accent); font-weight: 700; }
.kktable td.cur.dot::after { background: var(--accent); opacity: 1; }
.kkpos { font-size: 18px; font-weight: 700; margin: 2px 0 6px; }
.kkpos small { font-size: 12px; color: var(--muted); font-weight: 500; }

.load { display: flex; align-items: center; gap: 8px; }
.load input[type=range] { flex: 1; accent-color: var(--accent); }
.load .val { font-family: var(--mono); min-width: 52px; text-align: right; font-weight: 650; }

/* ---------- схема */
.scheme svg { width: 100%; height: 100%; display: block; cursor: grab; user-select: none; }
.scheme svg.dragging { cursor: grabbing; }
.scheme .tools { position: absolute; top: 8px; left: 8px; display: flex; gap: 6px; flex-wrap: wrap; z-index: 2; }
.scheme .tools .seg button { background: color-mix(in srgb, var(--panel) 92%, transparent); }
.legend {
  position: absolute; right: 8px; bottom: 8px; z-index: 2; background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px; font-size: 11px; display: flex; gap: 10px; flex-wrap: wrap;
}
.legend i { display: inline-block; width: 16px; height: 3px; vertical-align: middle; margin-right: 4px; border-radius: 2px; }
.scanview { position: absolute; inset: 0; overflow: hidden; background: #fff; display: none; cursor: grab; }
.scanview img { position: absolute; left: 0; top: 0; transform-origin: 0 0; user-select: none; -webkit-user-drag: none; }
.scheme.show-scan .scanview { display: block; }
.scheme.show-scan svg, .scheme.show-scan .legend { display: none; }

/* SVG элементы */
.w { stroke: var(--wire); stroke-width: 1.6; stroke-linecap: round; fill: none; }
.w.plus { stroke: var(--plus); }
.w.minus { stroke: var(--minus); }
.w.mid { stroke: var(--mid); }
.w.dead { stroke: var(--dead); }
.w.iso { stroke: var(--iso); }
.w.pw { stroke-width: 2.6; }
.fl { stroke: var(--flow); stroke-width: 2.2; stroke-dasharray: 3 9; fill: none; stroke-linecap: round; opacity: 0; pointer-events: none; }
.fl.pw { stroke-width: 3.2; }
.fl.on { opacity: .95; animation: flow linear infinite; }
.fl.rev { animation-direction: reverse; }
@keyframes flow { to { stroke-dashoffset: -24; } }
.jd { fill: var(--ink); }
.sym { stroke: var(--ink); stroke-width: 1.5; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.symf { fill: var(--ink); stroke: none; }
.lbl { font: 600 12.5px var(--sans); fill: var(--ink); }
.lbl.small { font-size: 10.5px; font-weight: 500; fill: var(--muted); }
.nlbl { font: 10.5px var(--mono); fill: var(--muted); pointer-events: none; paint-order: stroke; stroke: var(--paper); stroke-width: 3px; }
.ttl { font: 700 15px var(--sans); fill: var(--muted); letter-spacing: .06em; }
.note { font: italic 12px var(--sans); fill: var(--muted); }
.frame { fill: none; stroke: var(--line); stroke-width: 1.2; stroke-dasharray: 6 4; }
.mlink { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.el { cursor: pointer; }
.el .hit { fill: transparent; stroke: none; }
.el:hover .hit { fill: var(--accent-soft); opacity: .6; }
.el.xref .hit { fill: var(--accent); opacity: .22; }
.el.blade-closed .blade { stroke: var(--ink); }
.coilbox { fill: var(--paper); stroke: var(--ink); stroke-width: 1.5; }
.el.st-on .coilbox { fill: var(--on-fill); stroke: var(--on); stroke-width: 2.2; }
.el.st-pull .coilbox { fill: var(--rel-fill); stroke: var(--pull); }
.el.st-rel .coilbox { fill: var(--rel-fill); stroke: var(--rel); stroke-width: 2.2; }
.el.tripped .coilbox, .el.tripped .sym { stroke: var(--trip); }
.timer { font: 700 12px var(--mono); fill: var(--rel); }
.resbox { fill: var(--paper); stroke: var(--ink); stroke-width: 1.4; }
.kkcell { fill: none; stroke: var(--line); stroke-width: .8; }
.kkcell.d { fill: var(--muted); stroke: none; }
.kkcell.cur { stroke: var(--accent); stroke-width: 1.6; }
.kkcell.d.cur { fill: var(--accent); }
.lampc { fill: var(--paper); stroke: var(--ink); stroke-width: 1.4; }
.el.lit .lampc { fill: #ff5252; }
.el.ring .sym { stroke: var(--rel); }
.brk { fill: var(--paper); stroke: var(--ink); stroke-width: 1.5; }
.el.att .brk { fill: var(--on-fill); stroke: var(--on); }
.motor { fill: var(--paper); stroke: var(--ink); stroke-width: 1.8; }
.rotor { stroke: var(--accent); stroke-width: 2; }

/* подсказка */
.tip {
  position: fixed; z-index: 50; pointer-events: none; max-width: 340px; background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 22px rgba(0,0,0,.18); padding: 8px 10px; font-size: 12px; display: none;
}
.tip b { display: block; margin-bottom: 3px; font-size: 12.5px; }
.tip .st { font-family: var(--mono); font-size: 11.5px; margin-top: 5px; color: var(--muted); }

/* ---------- механизм */
.mech canvas { width: 100%; height: 380px; display: block; flex: none; }
.meters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; padding: 8px 10px; border-top: 1px solid var(--line); }
.meter { background: var(--panel2); border-radius: 8px; padding: 5px 8px; }
.meter .k { font-size: 10.5px; color: var(--muted); }
.meter .v { font: 650 15px var(--mono); }
.meter .v small { font-size: 10.5px; color: var(--muted); font-weight: 500; }
.meter.warn .v { color: var(--trip); }
.devs { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; padding: 8px 10px 10px; border-top: 1px solid var(--line); }
.dev {
  font: 600 11px var(--mono); text-align: center; padding: 4px 0 3px; border-radius: 6px; background: var(--panel2);
  color: var(--muted); border: 1px solid transparent; cursor: pointer; position: relative;
}
.dev.on { background: var(--on-fill); color: var(--on); border-color: color-mix(in srgb, var(--on) 45%, transparent); }
.dev.pull { background: var(--rel-fill); color: var(--pull); }
.dev.rel { background: var(--rel-fill); color: var(--rel); border-color: color-mix(in srgb, var(--rel) 45%, transparent); }
.dev.trip { background: var(--trip); color: #fff; }
.dev .tm { display: block; font-size: 9px; font-weight: 500; height: 10px; line-height: 10px; }

/* ---------- графики и журнал */
.cardhead { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-bottom: 1px solid var(--line); }
.cardhead b { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.cardhead .spacer { flex: 1; }
.cardhead .seg button { padding: 2px 8px; font-size: 11.5px; }
.charts { flex: 1; min-height: 0; position: relative; }
.charts canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.log { flex: 1; overflow: auto; font-size: 12px; padding: 4px 0; }
.log div { padding: 2px 10px; display: grid; grid-template-columns: 58px 1fr; gap: 6px; }
.log div:nth-child(odd) { background: var(--panel2); }
.log .t { font-family: var(--mono); color: var(--muted); font-size: 11px; }
.log .on { color: var(--on); }
.log .off { color: var(--muted); }
.log .trip { color: var(--trip); font-weight: 650; }
.log .kk { color: var(--accent); font-weight: 600; }
.log .brake { color: var(--rel); }
.log .man { color: var(--mid); }

/* ---------- модальное окно */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: none; align-items: center; justify-content: center; z-index: 60; padding: 16px; }
.modal.open { display: flex; }
.modal .box { background: var(--panel); border-radius: 12px; max-width: 760px; width: 100%; max-height: 86vh; overflow: auto; padding: 18px 22px; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal h3 { margin: 0 0 8px; font-size: 17px; }
.modal h4 { margin: 16px 0 6px; font-size: 13px; }
.modal ul { padding-left: 18px; margin: 6px 0; }
.modal li { margin: 4px 0; }
.modal table { border-collapse: collapse; font-size: 12px; }
.modal td, .modal th { border: 1px solid var(--line); padding: 3px 7px; text-align: left; }

/* ---------- узкие экраны */
@media (max-width: 1200px) {
  .layout { grid-template-columns: 280px minmax(0, 1fr); grid-template-rows: 640px 360px auto; grid-template-areas: "pult scheme" "bottom bottom" "mech mech"; height: auto; }
  .mech { flex-direction: row; flex-wrap: wrap; overflow: visible; }
  .mech canvas { width: 360px; }
  .mech .side { flex: 1; min-width: 280px; }
  .logwrap { flex-basis: 100%; height: 260px; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 520px 360px auto; grid-template-areas: "pult" "scheme" "bottom" "mech"; padding: 6px; }
  .mech canvas { width: 100%; }
  .top .sub { display: none; }
}
