:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1c2129;
  --muted: #6b7380;
  --primary: #2563eb;
  --primary-hover: #1d4fd8;
  --primary-text: #ffffff;
  --danger: #d93a3a;
  --danger-bg: #fdecec;
  --ok: #16a34a;
  --ok-bg: #e7f6ec;
  --off: #9aa1ac;
  --warn: #b45309;
  --warn-bg: #fdf3e2;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #191d23;
    --surface-2: #20252d;
    --border: #2c323b;
    --text: #e6e9ee;
    --muted: #949cab;
    --primary: #4b83f5;
    --primary-hover: #6595f7;
    --danger: #f06262;
    --danger-bg: #3a1d1f;
    --ok: #3ccf74;
    --ok-bg: #16301f;
    --off: #6b7380;
    --warn: #f0a94a;
    --warn-bg: #3a2c16;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

h2 { font-size: 16px; margin: 0 0 6px; }
.muted { color: var(--muted); margin: 0 0 12px; }
.small-text { font-size: 12px; }

/* 按钮 */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { height: 28px; padding: 0 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }

/* 表单 */
label { display: block; margin-bottom: 12px; font-weight: 500; }
input, select {
  display: block; width: 100%; margin-top: 4px; height: 36px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit; font-weight: 400;
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { color: var(--danger); min-height: 0; margin: 0 0 10px; }
.form-error:empty { display: none; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 4px; margin: 0 0 12px; }
legend { padding: 0 4px; font-weight: 500; }
label.radio { display: flex; gap: 10px; align-items: flex-start; font-weight: 400; margin-bottom: 10px; cursor: pointer; }
label.radio input, label.check input { width: auto; height: auto; margin: 3px 0 0; }
label.radio b { display: block; font-weight: 500; }
label.radio small { color: var(--muted); }
label.check { display: flex; gap: 8px; align-items: flex-start; font-weight: 400; }
.custom-routes { margin: -4px 0 10px; }

/* 登录 */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px 24px; box-shadow: var(--shadow);
}
.login-card .brand { font-size: 20px; margin-bottom: 2px; }
.login-card .muted { margin-bottom: 20px; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }

/* 顶栏 */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 20px; padding: 0 24px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.tabs { display: flex; gap: 4px; flex: 1; }
.tab {
  height: 56px; padding: 0 12px; border: 0; background: none; color: var(--muted);
  font: inherit; font-weight: 500; cursor: pointer; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--primary); }
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); }

.page { max-width: 1200px; margin: 0 auto; padding: 20px 24px 48px; }

/* 统计 */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: flex; flex-direction: column; gap: 2px;
}
.stat-label { color: var(--muted); font-size: 13px; }
.stat-value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-value.ok { color: var(--ok); }
.stat-value.bad { color: var(--danger); }
.stat-sub { color: var(--muted); font-size: 12px; }

/* 工具栏 */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar input { width: 240px; margin: 0; }
.toolbar select { width: 130px; margin: 0; }
.spacer { flex: 1; }

/* 表格 */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; font-weight: 500; color: var(--muted); background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.dev-name { font-weight: 500; }
.dev-note { color: var(--muted); font-size: 12px; }
.dev-extra { color: var(--muted); font-size: 12px; }
.unnamed { color: var(--muted); font-style: italic; font-weight: 400; }
tr.is-disabled td { color: var(--muted); }
.empty { padding: 32px; text-align: center; color: var(--muted); margin: 0; }

.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 500; padding: 2px 8px; border-radius: 99px; white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.online { color: var(--ok); background: var(--ok-bg); }
.badge.offline { color: var(--muted); background: var(--surface-2); }
.badge.disabled { color: var(--warn); background: var(--warn-bg); }

.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: nowrap; }
.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 10; min-width: 140px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 4px;
}
.menu-list button {
  display: block; width: 100%; text-align: left; border: 0; background: none; color: var(--text);
  font: inherit; padding: 7px 10px; border-radius: 6px; cursor: pointer;
}
.menu-list button:hover { background: var(--surface-2); }
.menu-list button.danger-text { color: var(--danger); }

/* 设置 */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 8px 0 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

/* 对话框 */
dialog {
  width: min(480px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow: auto;
  border: 1px solid var(--border); border-radius: 14px; padding: 22px 22px 18px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.wide { width: min(760px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 14, 20, .45); }
dialog h2 { margin-bottom: 14px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.config-body { display: grid; grid-template-columns: 240px 1fr; gap: 16px; align-items: start; }
#config-qr { width: 240px; height: 240px; background: #fff; border-radius: 8px; border: 1px solid var(--border); }
#config-text {
  margin: 0; padding: 12px; background: var(--surface-2); border-radius: 8px; overflow: auto;
  font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre; max-height: 260px;
}
.warn-text { color: var(--warn); background: var(--warn-bg); padding: 8px 12px; border-radius: 8px; margin: 14px 0 6px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
}
.toast.error { background: var(--danger); color: #fff; }

/* 手机 */
@media (max-width: 760px) {
  .topbar { padding: 0 16px; gap: 10px; height: auto; flex-wrap: wrap; padding-top: 10px; }
  .tabs { order: 3; flex-basis: 100%; }
  .tab { height: 42px; }
  .user { margin-left: auto; }
  .page { padding: 16px 16px 40px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .toolbar input { width: 100%; flex: 1 1 100%; }
  .toolbar select { flex: 1; width: auto; }
  .row { grid-template-columns: 1fr; }
  .config-body { grid-template-columns: 1fr; justify-items: center; }
  #config-text { width: 100%; }

  table.devices thead { display: none; }
  table.devices, table.devices tbody, table.devices tr, table.devices td { display: block; width: 100%; }
  table.devices tr { padding: 12px 14px; border-bottom: 1px solid var(--border); }
  table.devices td { border: 0; padding: 2px 0; text-align: left; }
  table.devices td.num { text-align: left; }
  table.devices td[data-label]::before { content: attr(data-label) "："; color: var(--muted); font-size: 12px; }
  table.devices .actions { justify-content: flex-start; margin-top: 8px; }

  table.audit thead { display: none; }
  table.audit, table.audit tbody, table.audit tr, table.audit td { display: block; width: 100%; }
  table.audit tr { padding: 10px 14px; border-bottom: 1px solid var(--border); }
  table.audit td { border: 0; padding: 1px 0; }
}
