:root {
  --bg: #eef0f3;
  --panel: #ffffff;
  --line: #d9dde3;
  --line-soft: #eceef2;
  --text: #1d2430;
  --muted: #6b7380;
  --accent: #2563eb;
  --accent-soft: #e6eeff;
  --danger: #c62828;
  --warn: #b26a00;
  --ok: #2e7d32;

  --term-bg: #0e1218;
  --term-fg: #c9d1d9;
  --term-dim: #7d8590;
  --term-tree: #4a5260;
  --term-sel: rgba(56, 139, 253, .35);

  --mono: "Cascadia Mono", "Cascadia Code", Consolas, "JetBrains Mono", "Noto Sans Mono CJK TC", "Microsoft JhengHei", monospace;
  --sans: "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
/* 整頁可往下捲：上方三欄佔滿第一個畫面，拓樸圖在其下方 */
html, body { margin: 0; }
html { height: 100%; }
body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--sans);
  display: flex;
  flex-direction: column;
}
body.welcome-mode { height: 100%; overflow: hidden; }
.mono { font-family: var(--mono); }
.muted { color: var(--muted); font-size: 12px; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* ---------- 控制項 ---------- */
button, select, input[type=text], input[type=search], textarea {
  font: inherit;
  color: inherit;
}
button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: #f4f6f9; border-color: #c4cad3; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #1d4fd0; }
button.danger { color: var(--danger); }
button.danger:hover { background: #fdecec; border-color: #f1b5b5; }
button.icon { padding: 3px 7px; min-width: 28px; }

input[type=text], input[type=search], select, textarea {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 7px;
  background: #fff;
  min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid rgba(37, 99, 235, .35);
  outline-offset: 0;
  border-color: var(--accent);
}
input:disabled { background: #f3f4f6; color: #9aa1ab; }
input.num { text-align: right; }

/* ---------- 頂部列 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 14px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; }
.logo { color: #f5a400; font-size: 18px; }
.tools { display: flex; align-items: center; gap: 6px; margin-left: auto; flex-wrap: wrap; }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.chip { border-radius: 999px; font-family: var(--mono); font-size: 12px; }
.chip.ok { color: var(--ok); border-color: #b8dcbb; background: #f1f9f1; }
.chip.warn { color: var(--warn); border-color: #f0d29c; background: #fff8ea; }
.chip.err { color: var(--danger); border-color: #f1b5b5; background: #fdecec; }
.lang { min-width: 56px; }

/* ---------- 三欄 ---------- */
.layout {
  flex: none;
  height: calc(100vh - var(--topbar-h, 47px));
  min-height: 480px;
  display: grid;
  grid-template-columns: minmax(360px, 27%) minmax(0, 1fr) minmax(320px, 31%);
  gap: 8px;
  padding: 8px;
}
.pane {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.pane-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  min-height: 38px;
}
.opt { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); cursor: pointer; }

/* ---------- 左欄 ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 6px; }
.tabs button {
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  padding: 9px 12px 7px;
  color: var(--muted);
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tabbody { flex: 1; min-height: 0; }
.tabbody.scroll { overflow: auto; padding: 10px 12px; }
.comp-tab { display: flex; flex-direction: column; }

.addbar { display: flex; gap: 6px; padding: 8px 10px; align-items: center; }
.addbar input[type=text], .addbar input[type=search] { flex: 1; }
.tabbody.scroll .addbar { padding: 0 0 8px; }
.list-head { padding: 0 12px 4px; font-size: 11px; color: var(--muted); }

.comp-list {
  flex: 0 0 34%;
  overflow: auto;
  border-top: 1px solid var(--line-soft);
  border-bottom: 1px solid var(--line);
}
.ci {
  display: grid;
  grid-template-columns: 20px minmax(90px, auto) 1fr;
  gap: 6px;
  align-items: center;
  padding: 3px 10px;
  cursor: pointer;
  border-left: 3px solid transparent;
}
.ci:hover { background: #f5f7fa; }
.ci.sel { background: var(--accent-soft); border-left-color: var(--accent); }
.ci.err .cid { color: var(--danger); }
.ci.warn .cid::after { content: " ⚠"; color: var(--warn); font-size: 11px; }
.ci.off { opacity: .55; }
.ci .ico { text-align: center; }
.cid { font-family: var(--mono); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.csum { font-family: var(--mono); font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.editor { flex: 1; overflow: auto; padding: 10px 12px 16px; }
.empty { color: var(--muted); padding: 24px 4px; text-align: center; }
.ed-head { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }
.ed-head .ico { font-size: 18px; }
.ed-type { font-weight: 600; font-size: 14px; }

.form { display: flex; flex-direction: column; gap: 5px; }
.frow { display: grid; grid-template-columns: 112px 1fr; gap: 8px; align-items: center; }
.flbl { color: #3d4654; font-size: 12px; }
.flbl .key { display: block; color: var(--muted); font-size: 10px; }
.req { color: var(--danger); font-weight: normal; }
.fctl { display: flex; align-items: center; gap: 6px; min-width: 0; }
.fctl > input[type=text], .fctl > select { flex: 1; }
.fbool .fctl input { margin: 0; }
.unit { color: var(--muted); font-size: 11px; min-width: 30px; font-family: var(--mono); }
.pair { display: flex; gap: 6px; flex: 1; min-width: 0; }
.pair input { flex: 1; }
.fgroup {
  margin: 10px 0 2px;
  padding-bottom: 3px;
  border-bottom: 1px solid var(--line-soft);
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}
.hint { color: var(--muted); font-size: 11.5px; margin: 2px 0 8px; }

/* FWS 端口 */
.ports { display: flex; flex-direction: column; gap: 6px; margin: 4px 0 6px; }
.port { border: 1px solid var(--line); border-left-width: 4px; border-radius: 6px; padding: 6px 8px; display: flex; flex-direction: column; gap: 5px; }
.port.off { opacity: .55; background: #fafbfc; }
.port.role-up { border-left-color: #6b7380; }
.port.role-branch { border-left-color: #f78c6c; }
.port.role-down { border-left-color: var(--accent); }
.port-head { display: flex; align-items: center; gap: 8px; }
.port-head label { display: flex; align-items: center; gap: 3px; cursor: pointer; }
.port-role { font-size: 11px; color: var(--muted); background: #f1f3f6; border-radius: 4px; padding: 0 6px; }
.no-lbl { margin-left: auto; font-size: 12px; }
.port-bus input { width: 100%; }
.port-feed { display: flex; gap: 6px; align-items: center; }
.port-feed input { flex: 1; }
.tie-ico { color: var(--muted); }

.extend { margin-top: 12px; }
.extend-row { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
.extend-row select { flex: 1; }
.arrow { color: var(--muted); }

.rawbox { margin-top: 14px; }
.rawbox summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.raw { width: 100%; margin-top: 6px; font-size: 12px; resize: vertical; }
.raw-act { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 4px; }
.raw-err { color: var(--danger); font-size: 12px; flex: 1; }

/* 表格（Bus / Linecodes） */
table.grid { width: 100%; border-collapse: collapse; font-size: 12px; }
.grid th { text-align: left; font-weight: 600; color: var(--muted); border-bottom: 1px solid var(--line); padding: 4px 6px; position: sticky; top: -10px; background: #fff; }
.grid td { border-bottom: 1px solid var(--line-soft); padding: 3px 6px; vertical-align: middle; }
.grid td.num { text-align: right; font-family: var(--mono); }
.grid tr.focus td { background: var(--accent-soft); }
.grid .bname a { color: var(--text); text-decoration: none; }
.grid .bname a:hover { color: var(--accent); text-decoration: underline; }
.badge { margin-left: 6px; font-size: 10px; color: #8a6d00; background: #fff4c2; border-radius: 4px; padding: 0 5px; font-family: var(--sans); }
.acts { display: flex; gap: 4px; align-items: center; justify-content: flex-end; }
.acts select { max-width: 110px; font-size: 12px; padding: 2px 4px; }
.grid.lc input { width: 100%; padding: 3px 5px; }
.grid.lc input.code { min-width: 54px; }
.tabbody.scroll > .frow { margin-bottom: 10px; }

/* ---------- Terminal 區 ---------- */
.term {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--term-bg);
  color: var(--term-fg);
  font: 12.5px/1.5 var(--mono);
  padding: 10px 0;
  position: relative;
}
.tl, .jl { white-space: pre; padding: 0 14px; min-height: 1.5em; }
.topo .tree { color: var(--term-tree); }
.topo .dim { color: var(--term-dim); }
.topo .bus { color: #e6edf3; font-weight: 600; cursor: pointer; }
.topo .bus:hover, .topo .id:hover { text-decoration: underline; }
.topo .tog { color: #9aa4b2; cursor: pointer; }
.topo .tog:hover { color: #fff; }
.topo .id { cursor: pointer; }
.topo .sel { background: var(--term-sel); border-radius: 3px; outline: 1px solid rgba(56, 139, 253, .7); }
.topo .root { color: #ffd54f; font-weight: 600; }
.topo .ptag { color: #d2a8ff; }
.topo .no { color: #ff6b81; font-weight: 600; }
.topo .tie { color: #ffab70; }
.topo .loop { color: #ff6b81; }
.topo .hdr { color: #ffb74d; }
.topo .off { opacity: .45; text-decoration: line-through; }
.c-tr { color: #c792ea; }
.c-line { color: #7fd1e8; }
.c-lbs { color: #ffcb6b; }
.c-fws { color: #f78c6c; }
.c-load { color: #ff7a90; }
.c-pv { color: #a5e075; }
.c-tws { color: #ffd580; }
.c-fcb { color: #ff9e64; }
.c-src { color: #ffd54f; font-weight: 600; }
.c-unk { color: #9aa4b2; }
/* 左側清單的圖示在白底上要深一點 */
.ci .c-tr { color: #8e44ad; }
.ci .c-line { color: #0b84a5; }
.ci .c-lbs { color: #b8860b; }
.ci .c-fws { color: #d9531e; }
.ci .c-load { color: #d6334f; }
.ci .c-pv { color: #3f8f1f; }
.ci .c-tws { color: #a0731c; }
.ci .c-fcb { color: #c4501a; }
.ci .c-src { color: #c08a00; }
.ci.src { background: #fffbea; }
.ci.src.sel { background: var(--accent-soft); }
.ed-head .c-tr { color: #8e44ad; }
.ed-head .c-line { color: #0b84a5; }
.ed-head .c-lbs { color: #b8860b; }
.ed-head .c-fws { color: #d9531e; }
.ed-head .c-load { color: #d6334f; }
.ed-head .c-pv { color: #3f8f1f; }
.ed-head .c-tws { color: #a0731c; }
.ed-head .c-fcb { color: #c4501a; }
.ed-head .c-src { color: #c08a00; }
.readonly { color: var(--muted); padding: 4px 0; }

.json { counter-reset: ln; }
.json .jl { padding-left: 52px; position: relative; }
.json .jl::before {
  counter-increment: ln;
  content: counter(ln);
  position: absolute;
  left: 0;
  width: 40px;
  text-align: right;
  color: #3d4450;
}
.json .jl.hl { background: rgba(56, 139, 253, .16); box-shadow: inset 3px 0 0 #388bfd; }
.jk { color: #79c0ff; }
.js { color: #a5d6a7; }
.jn { color: #ffab70; }
.jb { color: #ff7b9c; }

.issues-box { border-top: 1px solid var(--line); max-height: 32%; display: flex; flex-direction: column; }
.issues-box summary { padding: 6px 10px; cursor: pointer; user-select: none; }
.issues { overflow: auto; padding: 0 6px 6px; max-height: 180px; }
.iss { padding: 3px 8px; border-radius: 4px; cursor: pointer; font-size: 12px; }
.iss:hover { background: #f4f6f9; }
.iss.error { color: var(--danger); }
.iss.warn { color: var(--warn); }
.iss.info { color: var(--muted); }
.iss.ok { color: var(--ok); cursor: default; }

/* ---------- 起始畫面 ---------- */
.welcome { display: none; flex: 1; align-items: center; justify-content: center; padding: 24px 16px; overflow: auto; }
body.welcome-mode .welcome { display: flex; }
body.welcome-mode .layout, body.welcome-mode .diagram { display: none; }
body.welcome-mode .tools > :not(#btn-lang) { display: none; }
.welcome-card {
  width: 100%;
  max-width: 620px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 32px 28px 24px;
  text-align: center;
}
.welcome-logo { font-size: 36px; color: #f5a400; line-height: 1; }
.welcome-card h1 { font-size: 22px; margin: 10px 0 4px; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 24px 0 14px; }
.choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 22px 14px;
  border-radius: 10px;
  white-space: normal;
}
.choice:hover { border-color: var(--accent); background: var(--accent-soft); }
.choice-ico { font-size: 26px; color: var(--accent); line-height: 1.1; }
.choice b { font-size: 15px; }
.choice small { color: var(--muted); font-size: 12px; }
.resume { width: 100%; margin-bottom: 14px; color: var(--accent); border-style: dashed; white-space: normal; }
.small { font-size: 12px; }
@media (max-width: 520px) { .choices { grid-template-columns: 1fr; } }

/* ---------- 其他 ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 20px);
  background: #1d2430;
  color: #fff;
  padding: 8px 16px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: .2s;
  z-index: 10;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: var(--danger); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 30, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 30;
}
.modal-box {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border-radius: 10px;
  padding: 18px 18px 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
.modal-box p { margin: 0 0 12px; white-space: pre-wrap; }
.modal-box input { width: 100%; margin-bottom: 12px; }
.modal-act { display: flex; justify-content: flex-end; gap: 8px; }
.modal-act .primary.danger { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ---------- 下方拓樸圖 ---------- */
.diagram { margin: 0 8px 8px; scroll-margin-top: calc(var(--topbar-h, 47px) + 8px); overflow: visible; }
.diagram .pane-head { flex-wrap: wrap; position: sticky; top: var(--topbar-h, 47px); z-index: 2; background: var(--panel); border-radius: 8px 8px 0 0; }
.dg-zoom { min-width: 44px; text-align: center; font-size: 12px; color: var(--muted); }
#dg-fit.active { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.dg-note { font-size: 12px; }
.dg-note.warn { color: var(--warn); }
.dg-note.bad { color: var(--danger); }
.diagram-view {
  overflow-x: auto;
  padding: 16px;
  background: #fff;
  border-radius: 0 0 8px 8px;
  min-height: 160px;
}
.diagram-view img { display: block; max-width: none; margin: 0 auto; }
.diagram-view.stale img { opacity: .55; }

#dropmask {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(37, 99, 235, .12);
  border: 3px dashed var(--accent);
  z-index: 20;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: var(--accent);
  pointer-events: none;
}
body.dragging #dropmask { display: flex; }

@media (max-width: 1100px) {
  /* 單欄堆疊：列高依各欄高度，整頁捲動，避免下一欄蓋住上一欄 */
  .layout { flex: none; height: auto; min-height: 0; grid-template-columns: 1fr; grid-auto-rows: auto; }
  .pane.left { height: 640px; }
  .pane.mid, .pane.right { height: 520px; }
}

/* 站點座標 */
.grid.stn input { width: 100%; min-width: 72px; padding: 3px 5px; }
.grid.stn td.comps { color: var(--muted); max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grid.stn tr.unused td { color: var(--muted); }
.muted-badge { color: var(--muted); background: #f1f3f6; }
.maplink { text-decoration: none; font-size: 16px; color: var(--accent); padding: 0 4px; }
.maplink:hover { color: #1d4fd0; }
.grid.stn input::placeholder { color: #b8bec7; }
