:root {
  --bg: #0b0f17;
  --panel: #121826;
  --panel-2: #182134;
  --line: #24304a;
  --txt: #dfe7f5;
  --muted: #8b9ab5;
  --accent: #3d8bfd;
  --ok: #2ecc8f;
  --warn: #f0b429;
  --bad: #f2555a;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { max-width: 100%; overflow-x: hidden; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--txt);
  font: 14px/1.55 -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
}

/* 防溢出：任何一层都不许把页面撑宽 */
header, nav.tabs, main, section.tab, .panel, .row, .grid2, .stat-grid, .table-wrap {
  max-width: 100%;
  min-width: 0;
}

a { color: var(--accent); }

/* ---------------- 顶栏 ---------------- */
header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 40;
  flex-wrap: wrap;
}
header h1 { font-size: 15px; margin: 0; font-weight: 600; letter-spacing: .5px; white-space: nowrap; }
header .spacer { flex: 1 1 auto; }

/* ---------------- 页签 ---------------- */
nav.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 16px 0;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs button {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  padding: 9px 12px;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
  flex: 0 0 auto;
}
nav.tabs button.active { color: var(--txt); border-bottom-color: var(--accent); }

main { padding: 14px 16px 70px; }
section.tab { display: none; }
section.tab.active { display: block; }

/* ---------------- 面板 ---------------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 14px;
}
.panel h2 { margin: 0 0 10px; font-size: 14px; font-weight: 600; }
.panel h2 .sub { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 6px; }

.section-title {
  margin: 16px 0 8px;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: .4px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

/* ---------------- 布局 ---------------- */
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.row.actions { margin-top: 16px; margin-bottom: 0; }
.row.actions button { min-width: 108px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px; }
.grow { flex: 1 1 200px; }
.spacer { flex: 1 1 auto; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); min-width: 0; }
label.field > span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
label.field.inline { flex-direction: row; align-items: center; gap: 6px; }

input, select, textarea, button {
  font: inherit;
  color: var(--txt);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  outline: none;
  max-width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { min-height: 74px; font-family: var(--mono); font-size: 12px; width: 100%; }
label.field input, label.field select { width: 100%; }

button { cursor: pointer; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); border-color: rgba(242, 85, 90, .45); }
button.ghost { background: transparent; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.sm { padding: 6px 11px; font-size: 13px; }
button.block { width: 100%; margin-top: 4px; }

/* ---------------- 表格 ---------------- */
.table-wrap { width: 100%; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tbody tr:hover { background: rgba(61, 139, 253, .06); }
td.wrap, .wrap { white-space: normal; word-break: break-all; }
td.mono, .mono { font-family: var(--mono); font-size: 12px; }
.empty { color: var(--muted); text-align: center; padding: 24px; }
td.ops { white-space: normal; }
td.ops button { margin: 2px 4px 2px 0; }

/* ---------------- 统计卡 ---------------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); gap: 8px; }
.stat { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; }
.stat .k { color: var(--muted); font-size: 11px; letter-spacing: .3px; }
.stat .v { font-size: 19px; font-family: var(--mono); margin-top: 2px; }
.stat .v.ok { color: var(--ok); }
.stat .v.bad { color: var(--bad); }

/* ---------------- 图表 / 日志 ---------------- */
canvas.chart { width: 100%; height: 180px; display: block; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; }
.log {
  font-family: var(--mono);
  font-size: 12px;
  background: #080b12;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  height: 180px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ---------------- 弹窗 ---------------- */
dialog {
  background: var(--panel);
  color: var(--txt);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0;
  width: min(720px, 94vw);
  max-height: 92vh;
  flex-direction: column;
}
dialog[open] { display: flex; }
dialog::backdrop { background: rgba(0, 0, 0, .65); }
dialog .dlg-head { padding: 13px 16px; border-bottom: 1px solid var(--line); font-weight: 600; flex: 0 0 auto; }
dialog .dlg-body { padding: 14px 16px; overflow: auto; flex: 1 1 auto; }
dialog .dlg-foot {
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex: 0 0 auto;
}
dialog .dlg-foot button { min-width: 92px; }

details.adv { margin: 12px 0; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
details.adv summary { cursor: pointer; color: var(--muted); font-size: 13px; }
details.adv[open] summary { margin-bottom: 10px; }

/* ---------------- 杂项 ---------------- */
.hint { color: var(--muted); font-size: 12px; margin-top: 4px; }
.hint.mono { font-family: var(--mono); word-break: break-all; }
.chk { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--txt); white-space: nowrap; }
.chk input { width: auto; padding: 0; }
.nodes { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 8px; }
.nodes label {
  display: flex;
  gap: 6px;
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 6px 11px;
  border-radius: 18px;
  font-size: 12px;
  cursor: pointer;
}
.nodes label.sel { border-color: var(--accent); background: rgba(61, 139, 253, .13); }
.nodes label input { width: auto; padding: 0; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.badge.on { color: var(--ok); border-color: rgba(46, 204, 143, .4); background: rgba(46, 204, 143, .1); }
.badge.off { color: var(--bad); border-color: rgba(242, 85, 90, .4); background: rgba(242, 85, 90, .1); }
.badge.mid { color: var(--warn); border-color: rgba(240, 180, 41, .4); background: rgba(240, 180, 41, .1); }
.badge.blue { color: var(--accent); border-color: rgba(61, 139, 253, .4); background: rgba(61, 139, 253, .1); }

.dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
  background: var(--muted);
}
.dot.on { background: var(--ok); box-shadow: 0 0 7px rgba(46, 204, 143, .85); }
.dot.off { background: var(--bad); box-shadow: 0 0 7px rgba(242, 85, 90, .7); }
.dot.grey { background: #46536b; }
.dot.mid { background: var(--warn); box-shadow: 0 0 7px rgba(240, 180, 41, .8); }

.bar { display: inline-block; width: 60px; height: 7px; border-radius: 4px; background: #202b42; overflow: hidden; vertical-align: middle; margin-right: 6px; }
.bar-fill { height: 100%; border-radius: 4px; transition: width .3s ease; }
.bar-fill.ok { background: var(--ok); }
.bar-fill.warn { background: var(--warn); }
.bar-fill.bad { background: var(--bad); }

tr.selected { background: rgba(61, 139, 253, .14) !important; }
tr.row-bad td { background: rgba(242, 85, 90, .07); }

.login-wrap { max-width: 340px; margin: 10vh auto; }

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  left: 16px;
  z-index: 99;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  padding: 11px 14px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 8px 26px rgba(0, 0, 0, .5);
  max-width: 460px;
  margin-left: auto;
  word-break: break-word;
}
.toast.bad { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--ok); }
.hidden { display: none !important; }

/* ================= 手机端 ================= */
@media (max-width: 760px) {
  header { padding: 8px 10px; gap: 6px; }
  header h1 { font-size: 13px; }
  header .badge { font-size: 11px; padding: 1px 7px; }
  header button.sm { padding: 5px 9px; font-size: 12px; }
  main { padding: 10px 10px 64px; }
  nav.tabs { padding: 4px 10px 0; }
  nav.tabs button { padding: 9px 9px; font-size: 13px; }
  .panel { padding: 11px; border-radius: 9px; margin-bottom: 11px; }

  .grid2 { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: 6px; }
  .stat { padding: 7px 8px; }
  .stat .k { font-size: 10px; }
  .stat .v { font-size: 15px; }

  /* 按钮在手机上撑满，点起来舒服 */
  .row { gap: 7px; }
  .row.actions { flex-direction: column; align-items: stretch; }
  .row.actions button { width: 100%; min-width: 0; }
  .row.actions .badge { align-self: center; }
  .row > button.sm { flex: 1 1 auto; }
  #srvNew, #tgtNew { flex: 1 1 100%; }
  .spacer { flex: 1 1 100%; height: 0; }

  dialog { width: 100vw; max-width: 100vw; max-height: 100vh; height: 100%; border-radius: 0; margin: 0; }
  dialog .dlg-body { padding: 12px; }
  dialog .dlg-foot { padding: 10px 12px; gap: 8px; }
  dialog .dlg-foot button { flex: 1 1 0; min-width: 0; }

  canvas.chart { height: 150px; }
  .log { height: 150px; }
  .toast { left: 10px; right: 10px; max-width: none; }
  .login-wrap { margin: 8vh auto; }

  /* ---- 表格转卡片：彻底不再横向溢出 ---- */
  .table-wrap { overflow-x: visible; }
  table.responsive thead { display: none; }
  table.responsive,
  table.responsive tbody,
  table.responsive tr,
  table.responsive td { display: block; width: 100%; }
  table.responsive tr {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-2);
    padding: 9px 11px;
    margin-bottom: 8px;
  }
  table.responsive tr.selected { border-color: var(--accent); }
  table.responsive td {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    border: none;
    padding: 3px 0;
    white-space: normal;
    word-break: break-word;
    text-align: right;
    font-size: 13px;
  }
  table.responsive td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 12px;
    flex: 0 0 auto;
    text-align: left;
    padding-right: 4px;
  }
  table.responsive td:empty { display: none; }
  table.responsive td.ops {
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 6px;
    padding-top: 8px;
    margin-top: 6px;
    border-top: 1px solid var(--line);
  }
  table.responsive td.ops::before { flex: 1 1 100%; padding-bottom: 2px; }
  table.responsive td.ops button { flex: 1 1 auto; min-width: 78px; padding: 7px 10px; }
}

@media (max-width: 420px) {
  header h1 { display: none; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
  nav.tabs button { padding: 9px 8px; font-size: 12.5px; }
  table.responsive td { font-size: 12.5px; }
}
