:root {
  --bg: #eef0f3;
  --paper: #fff;
  --ink: #1f2430;
  --muted: #6b7280;
  --line: #d7dbe2;
  --lane: #5b5f66;
  --after: #2563eb;
  --after-bg: #dbeafe;
  --req: #e07b00;
  --req-bg: #fff1d6;
  --ok: #16a34a;
  --start-bg: #e6f6e8;
  --start-bd: #3c9a4a;
  --key-bg: #fbeceb;
  --key-bd: #a33a36;
  --norm-bd: #a8bbdc;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-columns: minmax(0, 1fr);
  overflow: hidden;
}
button { font: inherit; color: inherit; }

/* ── Верхняя панель ── */
.top {
  display: flex; align-items: center; flex-wrap: wrap; gap: 10px 18px;
  padding: 8px 14px; background: var(--paper); border-bottom: 1px solid var(--line);
  position: relative; z-index: 5;
}
body.editing .top { background: #fff8e1; }
.brand { font-weight: 700; font-size: 16px; }
.group { display: flex; align-items: center; gap: 8px; }
.layers label { display: flex; align-items: center; gap: 4px; font-size: 13px; cursor: pointer; user-select: none; }
.layers .l-after { color: var(--after); }
.layers .l-req { color: var(--req); }
.progress { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.progress .bar { width: 110px; height: 7px; border-radius: 4px; background: #e5e7eb; overflow: hidden; }
.progress .bar i { display: block; height: 100%; background: var(--ok); width: 0; transition: width .3s; }
#zoomv { min-width: 42px; text-align: center; font-size: 13px; color: var(--muted); }
.status { font-size: 12px; color: var(--muted); margin-left: auto; }
.status.ok { color: var(--ok); }
.status.err { color: #c62828; font-weight: 600; }
.status.local { color: #b26a00; }

.btn {
  border: 1px solid var(--line); background: var(--paper); border-radius: 7px;
  padding: 5px 11px; cursor: pointer; font-size: 13px; white-space: nowrap;
}
.btn:hover { background: #f3f4f6; }
.btn.icon { width: 30px; padding: 5px 0; font-weight: 700; }
.btn.primary { background: var(--after); border-color: var(--after); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.orange { background: var(--req); border-color: var(--req); color: #fff; }
.btn.orange:hover { background: #c46a00; }
.btn.ghost { background: transparent; }
.btn.danger { color: #c62828; border-color: #f0c4c4; }
.btn.active { background: #fde68a; border-color: #e0b400; }

.menu { position: relative; }
.menu-list {
  display: none; position: absolute; right: 0; top: 100%; margin-top: 4px; min-width: 240px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.12); padding: 4px; z-index: 20;
}
.menu:focus-within .menu-list, .menu:hover .menu-list { display: block; }
.menu-list button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.menu-list button:hover { background: #f3f4f6; }

/* ── Схема ── */
main { display: flex; min-height: 0; position: relative; }
#viewport { flex: 1; min-width: 0; overflow: auto; position: relative; cursor: grab; }
#viewport.panning { cursor: grabbing; }
#sizer { position: relative; }
#stage {
  position: absolute; left: 0; top: 0; transform-origin: 0 0;
  background: var(--paper); box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
#lanes, #blocks { position: absolute; inset: 0; }
.lane { position: absolute; top: 0; bottom: 0; border-left: 1.5px solid var(--lane); }
.lane:last-child { border-right: 1.5px solid var(--lane); }
.lane-h {
  display: flex; align-items: center; justify-content: center;
  border-bottom: 1.5px solid var(--lane); border-top: 1.5px solid var(--lane);
  font-size: 30px; color: #333;
}
#edges { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
#edges path { fill: none; transition: opacity .15s; }
#edges .orig { stroke: #8b9099; stroke-width: 1.6; }
#edges .after { stroke: var(--after); stroke-width: 2.6; }
#edges .req { stroke: var(--req); stroke-width: 2.4; stroke-dasharray: 8 5; }
#edges .par { stroke: #0d9488; stroke-width: 2.6; stroke-dasharray: 2 6; stroke-linecap: round; }
#edges .after.soft { stroke-dasharray: 10 6; }
#edges .faded { opacity: .13; }
#edges .hl.orig { stroke: #4b5058; stroke-width: 2.2; }
#edges .hl.after, #edges .hl.req { stroke-width: 3.4; }

.block {
  position: absolute; display: flex; align-items: center; justify-content: center;
  text-align: center; font-size: 13px; line-height: 1.15; padding: 3px 7px;
  background: #fff; border: 1.6px solid var(--norm-bd); border-radius: 9px;
  box-shadow: 0 1px 2px rgba(0,0,0,.08); cursor: pointer; user-select: none;
  transition: box-shadow .12s, background .12s, opacity .15s, transform .1s;
  overflow: hidden;
}
.block span { pointer-events: none; }
.block:hover { box-shadow: 0 0 0 3px rgba(37,99,235,.25), 0 3px 8px rgba(0,0,0,.12); }
.block.t-s { background: var(--start-bg); border-color: var(--start-bd); }
.block.t-k { background: var(--key-bg); border-color: var(--key-bd); }
.block.done::after {
  content: "✓"; position: absolute; top: 2px; right: 3px; width: 15px; height: 15px; border-radius: 50%;
  background: var(--ok); color: #fff; font-size: 10px; line-height: 15px; font-weight: 700;
}
.block.sel { box-shadow: 0 0 0 4px var(--after), 0 4px 12px rgba(0,0,0,.2); z-index: 3; }
.block.rel-after { background: var(--after-bg); box-shadow: 0 0 0 3px var(--after); z-index: 2; }
.block.rel-req { background: var(--req-bg); box-shadow: 0 0 0 3px var(--req); z-index: 2; }
.block.rel-par { background: #d5f5f0; box-shadow: 0 0 0 3px #0d9488; z-index: 2; }
.block.dim { opacity: .3; }
.bdg { position: absolute; left: 3px; top: 2px; display: flex; gap: 2px; font-style: normal; pointer-events: none; }
.bdg.static { position: static; }
.bdg b {
  width: 15px; height: 15px; border-radius: 4px; color: #fff; font-size: 10px; line-height: 15px;
  text-align: center; font-weight: 700;
}
.b-must { background: #dc2626; }
.b-par { background: #0d9488; }
.b-cond { background: #7c3aed; }
.block.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

#stage.picking .block { cursor: crosshair; }
#stage.picking .block:hover { transform: scale(1.06); z-index: 4; }
#stage.picking .block.picked { box-shadow: 0 0 0 4px var(--pick, var(--after)); }
#stage.pick-req { --pick: var(--req); }
body.editing .block { cursor: move; }

/* ── Баннер режима выбора ── */
#banner {
  position: absolute; top: 12px; left: calc((100% - 380px) / 2); transform: translateX(-50%);
  background: #111827; color: #fff; padding: 9px 14px; border-radius: 10px; z-index: 10;
  display: flex; align-items: center; gap: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.25);
  max-width: min(720px, calc(100% - 420px)); font-size: 14px;
}
#banner[hidden] { display: none; }
#banner b { color: #fde68a; }
#banner .btn { border-color: #4b5563; background: #374151; color: #fff; }
#banner .btn.go { background: var(--ok); border-color: var(--ok); }

/* ── Боковая панель ── */
#panel {
  width: 380px; flex: none; overflow: auto; background: var(--paper);
  border-left: 1px solid var(--line); padding: 16px 18px 30px;
}
.p-head { position: relative; padding-right: 30px; margin-bottom: 14px; }
.p-lane { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.p-head h2 { font-size: 19px; margin: 3px 0 0; line-height: 1.25; }
.p-head .x { position: absolute; right: -4px; top: -4px; border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }
#panel h3 { font-size: 15px; margin: 18px 0 8px; }
#panel p { margin: 6px 0; color: #374151; }
.muted { color: var(--muted); font-size: 13px; }

.q { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 12px; transition: border-color .15s, box-shadow .15s; }
.q.active { border-color: var(--after); box-shadow: 0 0 0 3px rgba(37,99,235,.15); }
.q.q-req.active { border-color: var(--req); box-shadow: 0 0 0 3px rgba(224,123,0,.15); }
.q-t { font-weight: 600; display: flex; gap: 8px; align-items: flex-start; }
.q-t small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; margin-top: 2px; }
.n { flex: none; width: 22px; height: 22px; border-radius: 50%; color: #fff; font-size: 12px; display: grid; place-items: center; }
.n.after { background: var(--after); }
.n.req { background: var(--req); }
.n.cond { background: #7c3aed; }

.reqs { display: grid; gap: 8px; margin: 10px 0; }
.req-item { border: 1px solid #fbd49a; background: #fffaf0; border-radius: 9px; padding: 8px 9px; display: grid; gap: 7px; }
.req-item.par { border-color: #99e0d6; background: #f0fbf9; }
.req-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; font-weight: 600; font-size: 14px; }
.req-top a { cursor: pointer; }
.req-top a:hover { text-decoration: underline; }
.req-top .rm { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 17px; line-height: 1; }
.req-top .rm:hover { color: #c62828; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; background: #fff; }
.seg button { flex: 1; border: 0; background: none; padding: 5px 6px; font-size: 12.5px; cursor: pointer; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on.must { background: var(--req); color: #fff; }
.seg button.on.parallel { background: #0d9488; color: #fff; }
.req-item input { padding: 5px 8px; font-size: 13px; }

.flags { display: grid; gap: 8px; margin: 10px 0 4px; }
.chk { display: flex; gap: 9px; align-items: flex-start; cursor: pointer; font-size: 14px; }
.chk input { margin-top: 3px; width: 16px; height: 16px; flex: none; accent-color: #7c3aed; }
.chk small { display: block; color: var(--muted); font-size: 12px; }
#cond { margin-top: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip {
  display: inline-flex; align-items: center; gap: 2px; border-radius: 14px; padding: 3px 4px 3px 10px;
  font-size: 13px; border: 1px solid;
}
.chip.after { background: var(--after-bg); border-color: #93c5fd; }
.chip.req { background: var(--req-bg); border-color: #fbbf77; }
.chip.start { background: var(--start-bg); border-color: #9ad3a3; }
.chip a { cursor: pointer; }
.chip a:hover { text-decoration: underline; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 15px; line-height: 1; padding: 0 5px; }
.chip button:hover { color: #c62828; }
.empty { color: var(--muted); font-style: italic; font-size: 13px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: space-between; margin-top: 14px; }

textarea, input[type=text], input[type=number], select {
  width: 100%; font: inherit; font-size: 14px; border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px;
}
textarea { min-height: 70px; resize: vertical; }
label.f { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }

.links { list-style: none; padding: 0; margin: 4px 0 0; }
.links li { padding: 3px 0; font-size: 13px; }
.links a { cursor: pointer; color: var(--after); }
.links a:hover { text-decoration: underline; }
.lane-list summary { cursor: pointer; font-weight: 600; margin: 10px 0 4px; }
.legend { display: grid; gap: 6px; font-size: 13px; margin-top: 8px; }
.legend div { display: flex; align-items: center; gap: 8px; }
.legend svg { flex: none; }
.sw { width: 26px; height: 14px; border-radius: 4px; border: 1.6px solid; flex: none; }

/* ── Подсказка ── */
#tip {
  position: fixed; z-index: 50; pointer-events: none; max-width: 320px;
  background: #111827; color: #f9fafb; border-radius: 8px; padding: 8px 11px; font-size: 13px;
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
#tip .t { font-weight: 700; margin-bottom: 4px; }
#tip div + div { margin-top: 3px; }
#tip .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
#tip .dot.after { background: #60a5fa; }
#tip .dot.req { background: #fbbf24; }
#tip .dot.par { background: #2dd4bf; }
#tip .m { color: #9ca3af; }

@media (max-width: 900px) {
  #panel { position: absolute; right: 0; top: 0; bottom: 0; width: min(380px, 92vw); box-shadow: -6px 0 20px rgba(0,0,0,.15); }
  #banner { left: 50%; max-width: calc(100% - 24px); }
}
