:root {
  --bg: #eceef1;
  --panel: #ffffff;
  --panel-2: #f5f6f8;
  --line: #e3e6eb;
  --line-strong: #d4d9e0;
  --text: #16203a;
  --muted: #6a7280;
  --primary: #1b4fd8;
  --primary-hover: #1740b0;
  --danger: #d8323c;
  --danger-hover: #b8262f;
  --header-dark: #0c1b33;
  --radius: 8px;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", "Cascadia Mono",
          Menlo, Consolas, "Liberation Mono", monospace;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--mono); font-size: 13px; line-height: 1.5;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center;
  height: 62px; padding: 0 24px; background: var(--panel);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-logo { height: 38px; width: auto; display: block; }
.brand-divider { width: 1px; height: 26px; background: var(--line-strong); margin: 0 15px; }
.brand-app { font-size: 15px; color: var(--text); font-weight: 600; letter-spacing: .01em; }

/* ---------- content ---------- */
.content { min-width: 0; padding: 26px 32px; max-width: 1600px; margin: 0 auto; }

/* ---------- headings / page head ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 22px; }
.page-head h1 { font-size: 26px; font-weight: 700; margin: 0; }
.page-head .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
h2, h3 { margin: 0 0 12px; font-weight: 700; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hint { color: var(--muted); font-size: 11px; }

/* ---------- buttons ---------- */
.btn {
  background: var(--panel); border: 1px solid var(--line-strong); color: var(--text);
  padding: 8px 15px; border-radius: 7px; cursor: pointer; font: inherit; font-size: 13px;
  display: inline-flex; align-items: center; gap: 7px;
}
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); color: #fff; }
.btn.ghost-danger { border-color: #eecfd1; color: var(--danger); background: var(--panel); }
.btn.ghost-danger:hover { background: #fdeef0; }
.btn.tiny { padding: 4px 10px; font-size: 12px; }
.btn.plus::before { content: "+"; font-size: 16px; }

/* ---------- inputs ---------- */
input, textarea, select {
  background: var(--panel); border: 1px solid var(--line-strong); color: var(--text);
  border-radius: 6px; padding: 9px 11px; font: inherit; font-size: 13px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--primary); }
.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); flex-wrap: wrap; }

/* ---------- scenario cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 18px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; display: flex; flex-direction: column;
}
.card-head {
  background: var(--header-dark); color: #fff; padding: 12px 15px;
  display: flex; align-items: center; justify-content: space-between;
}
.card-head .title { font-weight: 700; font-size: 14px; letter-spacing: .02em; }
.card-head .kebab { color: #9fb0cc; cursor: pointer; position: relative; padding: 0 4px; }
.card-body { padding: 15px; display: flex; flex-direction: column; gap: 12px; flex: 1; }
.stat-row { display: flex; gap: 22px; }
.stat .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.stat .v { font-size: 14px; font-weight: 700; }
.card-desc {
  color: var(--muted); font-size: 12px; flex: 1;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3;
  line-clamp: 3; overflow: hidden;
}
.card .open-btn { width: 100%; justify-content: center; }

/* dropdown menu on card */
.menu {
  position: absolute; right: 0; top: 20px; background: var(--panel);
  border: 1px solid var(--line-strong); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.12);
  min-width: 160px; z-index: 30; overflow: hidden; display: none;
}
.menu.open { display: block; }
.menu a, .menu button {
  display: block; width: 100%; text-align: left; padding: 9px 13px; background: none;
  border: none; font: inherit; font-size: 13px; color: var(--text); cursor: pointer;
}
.menu a:hover, .menu button:hover { background: var(--panel-2); text-decoration: none; }
.menu .danger { color: var(--danger); }

.empty { color: var(--muted); padding: 40px; text-align: center; }

/* ---------- scenario detail head ---------- */
.scn-head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 8px; }
.scn-head .title-edit { font-size: 22px; font-weight: 700; max-width: 420px; border: 1px solid transparent; background: transparent; }
.scn-head .title-edit:hover { border-color: var(--line-strong); }
.scn-meta { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.tag-filter-wrap { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.tag-filter-wrap select { width: auto; padding: 4px 8px; font-size: 12px; }
.scn-desc-wrap { display: block; width: 100%; margin-bottom: 18px; }
.scn-desc-edit {
  width: 100%; resize: none; overflow: hidden; font-size: 14px; color: var(--text);
  font-family: var(--mono); background: transparent; line-height: 1.5;
  border: 1px solid transparent; border-radius: 6px; padding: 6px 8px;
}
.scn-desc-edit::placeholder { color: var(--muted); }
.scn-desc-edit:hover { border-color: var(--line-strong); }
.scn-desc-edit:focus { outline: none; border-color: var(--primary); background: var(--panel); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- gantt chain ---------- */
.chain {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); margin-bottom: 20px; overflow: visible;
}
.chain-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  background: var(--panel-2); border-bottom: 1px solid var(--line);
  border-radius: var(--radius) var(--radius) 0 0;
}
.chain-grip {
  cursor: grab; color: #aeb4bf; font-size: 16px; line-height: 1;
  padding: 0 2px; user-select: none; letter-spacing: -1px;
}
.chain-grip:hover { color: var(--text); }
.chain.drag-src { opacity: .5; }
.chain.drop-before { box-shadow: 0 -3px 0 0 var(--primary); }
.chain.drop-after { box-shadow: 0 3px 0 0 var(--primary); }
.chain-dot { width: 12px; height: 12px; border-radius: 3px; display: inline-block; flex: 0 0 auto; }
.chain-head .chain-name { font-weight: 700; font-size: 14px; }
.chain-head .edit-chain { padding: 2px 8px; }
.chain-head .spacer { flex: 1; }
.chain-count { color: var(--muted); font-size: 12px; }
.chain-desc {
  padding: 7px 14px; color: var(--muted); font-size: 12px;
  background: var(--panel); border-bottom: 1px solid var(--line); white-space: pre-wrap;
}

/* chart layout: gutter + timeline */
.chart-row { display: flex; align-items: stretch; }
.gutter { position: relative; flex: 0 0 172px; width: 172px; border-right: 1px solid var(--line); background: var(--panel); z-index: 2; }
.gutter-spacer { border-bottom: 1px solid var(--line); background: var(--panel-2); }
.lane-label {
  height: 38px; display: flex; align-items: center; justify-content: space-between;
  gap: 6px; padding: 0 8px 0 10px; border-bottom: 1px dashed var(--line); overflow: hidden;
}
.lane-label:hover { background: var(--panel-2); }
.lane-main { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; cursor: pointer; }
.lane-del {
  flex: 0 0 auto; width: 20px; height: 20px; border-radius: 5px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; color: var(--danger); opacity: 0;
}
.lane-label:hover .lane-del { opacity: .6; }
.lane-del:hover { opacity: 1; background: #fdeef0; }
.zoom-level { font-size: 12px; color: var(--muted); min-width: 42px; text-align: center; }

/* insert-row strips between gutter rows: a line with a circled "+" centred on it.
   The chain no longer clips (see .chain), so the bottom-edge strip overflows
   onto the grey gap instead of being cut off. */
.row-insert {
  position: absolute; left: 0; right: 0; height: 16px; transform: translateY(-50%);
  z-index: 4; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.row-insert::before {
  content: ""; position: absolute; left: 6px; right: 6px; top: 50%; height: 2px;
  transform: translateY(-50%); background: var(--primary); border-radius: 2px; opacity: 0;
}
.row-insert-plus {
  position: relative; z-index: 1; width: 16px; height: 16px; border-radius: 50%;
  background: var(--primary); color: #fff; font-size: 12px; line-height: 16px;
  text-align: center; opacity: 0; box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
.row-insert:hover::before, .row-insert:hover .row-insert-plus { opacity: 1; }
.lane-pill {
  font-size: 12px; font-weight: 700; color: #fff; padding: 3px 10px; border-radius: 5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
.lane-add { font-size: 12px; color: var(--muted); opacity: .45; }
.lane-label:hover .lane-add { opacity: 1; }

.gantt-scroll { overflow-x: auto; overflow-y: hidden; flex: 1; }
.gantt { position: relative; min-width: 100%; }
.axis { position: relative; height: 26px; border-bottom: 1px solid var(--line); background: var(--panel-2); }
.axis .tick { position: absolute; top: 0; height: 100%; border-left: 1px solid var(--line); }
.axis .tick span { position: absolute; left: 4px; top: 6px; font-size: 11px; color: var(--muted); white-space: nowrap; }

/* hooks band (point diamonds) */
.hooks-band { position: relative; background: #fafbfc; border-bottom: 1px solid var(--line); }
.hook {
  position: absolute; top: 0; height: 100%; width: 26px;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  transform: translateX(-50%); cursor: grab; user-select: none; overflow: visible;
}
.hook-code { font-size: 10px; font-weight: 700; color: var(--muted); margin-bottom: 1px; white-space: nowrap; pointer-events: none; }
.hook-diamond { width: 13px; height: 13px; transform: rotate(45deg); margin-bottom: 5px; box-shadow: 0 1px 2px rgba(0,0,0,.25); }

.lanes { position: relative; }
.lane-row { position: relative; height: 38px; border-bottom: 1px dashed var(--line); }
.gridline { position: absolute; top: 0; bottom: 0; border-left: 1px solid #eef0f3; }
.add-hint { position: absolute; pointer-events: none; color: var(--muted); font-size: 12px; left: 12px; top: 50%; transform: translateY(-50%); }

.bar {
  position: absolute; height: 28px; top: 5px; background: var(--primary);
  border: 1px solid rgba(0,0,0,.12); border-radius: 6px;
  color: #fff; font-size: 12px; cursor: grab;
  display: flex; align-items: center; overflow: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,.18); user-select: none;
}
.bar.dragging { opacity: .9; z-index: 20; }
.bar .label { padding: 0 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none; }
.bar .mitre-chip { background: rgba(0,0,0,.28); border-radius: 4px; padding: 1px 5px; margin-left: 4px; font-size: 10px; pointer-events: none; }
.bar .handle { position: absolute; top: 0; width: 8px; height: 100%; cursor: ew-resize; }
.bar .handle.l { left: 0; } .bar .handle.r { right: 0; }
.bar .handle:hover { background: rgba(255,255,255,.3); }

/* ---------- tooltip ---------- */
.tooltip {
  position: fixed; z-index: 200; pointer-events: none;
  background: var(--header-dark); border: 1px solid #1c2f4d; border-radius: 8px;
  padding: 8px 11px; font-size: 12px; color: #eef2f8; max-width: 320px;
  box-shadow: 0 6px 20px rgba(0,0,0,.35); opacity: 0; transition: opacity .08s; line-height: 1.5;
}
.tooltip.show { opacity: 1; }
.tooltip .tip-mitre { color: #86b8ff; }
.tooltip .tip-time { color: #9fb0cc; }

/* ---------- swatches ---------- */
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.sw { width: 26px; height: 26px; border-radius: 6px; cursor: pointer; border: 2px solid transparent; }
.sw.sel { border-color: var(--text); box-shadow: 0 0 0 2px var(--primary); }
.sw-default { background: var(--panel-2); border: 1px dashed var(--line-strong); font-size: 10px; color: var(--muted); display: flex; align-items: center; justify-content: center; }
.sw-default.sel { border-color: var(--text); color: var(--text); }

/* ---------- modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(12,27,51,.45);
  display: none; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--panel); border-radius: 12px; width: 620px; max-width: 100%;
  max-height: 90vh; overflow-y: auto; padding: 26px; box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.modal h3 { display: flex; justify-content: space-between; align-items: center; font-size: 18px; }
.modal .modal-sub { color: var(--muted); font-size: 12px; margin: -6px 0 14px; }
.modal .close { cursor: pointer; color: var(--muted); font-size: 22px; line-height: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

.msg { min-height: 18px; }
.msg.err { color: var(--danger); }

@media (max-width: 820px) {
  .content { padding: 18px 16px; }
  .grid2 { grid-template-columns: 1fr; }
}

/* ======================= Tabs ======================= */
.tabs {
  display: flex; gap: 4px; margin: 4px 0 18px;
  border-bottom: 1px solid var(--line-strong);
}
.tabs .tab {
  appearance: none; border: 1px solid transparent; border-bottom: none;
  background: transparent; color: var(--muted); font-family: var(--mono);
  font-size: 13px; font-weight: 600; padding: 9px 18px; cursor: pointer;
  border-radius: 8px 8px 0 0; margin-bottom: -1px;
}
.tabs .tab:hover { color: var(--text); }
.tabs .tab.active {
  color: var(--primary); background: var(--panel);
  border-color: var(--line-strong); border-bottom: 1px solid var(--panel);
}
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.scn-subhead { margin-bottom: 10px; }

/* ======================= Topology Planner ======================= */
.topo-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.topo-hint { max-width: 60%; }

.topo-wrap {
  display: flex; gap: 12px; align-items: stretch;
  height: calc(100vh - 230px); min-height: 460px;
}
.topo-palette {
  flex: 0 0 150px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 6px;
}
.pal-group {
  font-size: 10px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); margin: 6px 2px 2px;
}
.pal-item {
  display: flex; align-items: center; gap: 9px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: 7px; cursor: grab;
  background: var(--panel); user-select: none;
}
.pal-item:hover { border-color: var(--primary); background: var(--panel-2); }
.pal-item:active { cursor: grabbing; }
.pal-item img { width: 30px; height: 30px; object-fit: contain; pointer-events: none; }
.pal-label { font-size: 12px; color: var(--text); }
.pal-net-box {
  width: 30px; height: 22px; border: 2px solid var(--primary);
  border-radius: 5px; background: #eef3fc; display: inline-block;
}

.topo-canvas-scroll {
  flex: 1 1 auto; position: relative; overflow: auto;
  background: var(--panel);
  background-image: radial-gradient(var(--line) 1px, transparent 1px);
  background-size: 22px 22px;
  border: 1px solid var(--line); border-radius: var(--radius);
}
#topo-svg { display: block; }
.topo-empty {
  position: absolute; top: 40px; left: 0; right: 0; text-align: center;
  color: var(--muted); font-size: 13px; pointer-events: none;
}

/* networks */
.topo-net { cursor: move; }
.topo-net .net-body { fill: #eef3fc; stroke: #2c5aa0; stroke-width: 1.5; }
.topo-net .net-label { fill: #1b3a6b; font: 700 13px var(--mono); pointer-events: none; }
.topo-net .net-resize { fill: #2c5aa0; opacity: 0; cursor: nwse-resize; transition: opacity .1s; }
.topo-net:hover .net-resize { opacity: .55; }
.topo-net .net-resize:hover { opacity: .9; }
.topo-net.color-red .net-body { fill: #fdeeec; stroke: #b23321; }
.topo-net.color-red .net-label { fill: #8f2719; }
.topo-net.color-red .net-resize { fill: #b23321; }
.topo-net.color-gray .net-body { fill: #f1f2f4; stroke: #7a7f87; }
.topo-net.color-gray .net-label { fill: #4b5059; }
.topo-net.color-gray .net-resize { fill: #7a7f87; }

/* machines */
.topo-machine { cursor: move; }
.topo-machine .machine-img { pointer-events: auto; }
.topo-machine .machine-label { fill: var(--text); font: 12px var(--mono); text-anchor: middle; pointer-events: none; }
.topo-machine .machine-handle {
  fill: var(--primary); stroke: #fff; stroke-width: 1.5;
  opacity: 0; cursor: crosshair; transition: opacity .1s;
}
.topo-machine:hover .machine-handle { opacity: 1; }

/* edges */
.topo-edge .edge-line { stroke: #8aa0c8; stroke-width: 1.6; fill: none; }
.topo-edge .edge-hit { stroke: transparent; stroke-width: 12; fill: none; }
/* drag-preview line (lives directly in #topo-edges, not inside .topo-edge) */
.edge-line.temp { stroke: var(--primary); stroke-width: 1.8; stroke-dasharray: 5 4; fill: none; }
.topo-edge:hover .edge-line { stroke: var(--primary); }
.topo-edge .edge-x { display: none; cursor: pointer; }
.topo-edge:hover .edge-x { display: block; }
.topo-edge .edge-x-bg { fill: var(--danger); }
.topo-edge .edge-x-t { fill: #fff; font: 700 13px var(--mono); text-anchor: middle; }

/* node modal pickers */
.topo-icon-pick { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.topo-icon-pick .ti {
  border: 1px solid var(--line); border-radius: 7px; background: var(--panel);
  padding: 4px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.topo-icon-pick .ti img { width: 34px; height: 34px; object-fit: contain; }
.topo-icon-pick .ti:hover { border-color: var(--primary); }
.topo-icon-pick .ti.sel { border-color: var(--primary); background: #eef3fc; box-shadow: 0 0 0 1px var(--primary) inset; }
.topo-color-pick { display: flex; gap: 10px; }
.topo-color-pick .tc {
  width: 30px; height: 30px; border-radius: 50%; cursor: pointer;
  border: 2px solid var(--line-strong);
}
.topo-color-pick .tc[data-color="blue"] { background: #2680ff; }
.topo-color-pick .tc[data-color="red"] { background: #e23b26; }
.topo-color-pick .tc[data-color="gray"] { background: #9aa0a8; }
.topo-color-pick .tc.sel { border-color: var(--text); box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--text); }

/* ======================= Auth ======================= */
.topbar-user { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.logout-form { margin: 0; }

.login-wrap { display: flex; justify-content: center; padding: 60px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 28px 26px;
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; color: var(--text); }
.login-form { margin-top: 18px; gap: 14px; }
.login-form .btn { margin-top: 4px; width: 100%; justify-content: center; }
.login-error {
  margin-top: 14px; padding: 10px 12px; border-radius: 7px;
  background: #fdecee; border: 1px solid #f3c7cd; color: var(--danger);
  font-size: 12px;
}
