:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #111827;
  --ink-2: #4b5563;
  --ink-3: #9ca3af;
  --line: #e8ebf0;
  --primary: #2563eb;
  --primary-dark: #1e40af;
  --primary-soft: #eff4ff;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --sidebar: #0f172a;
  --sidebar-2: #1e293b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 24px rgba(16, 24, 40, 0.06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", system-ui, sans-serif;
  font-size: 14px;
}

button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--ink-3); }
.mono { font-family: "Cascadia Code", Consolas, "SF Mono", monospace; }

/* IRK 在表格里显示为可点击复制：32 位 hex 直接铺开会把列撑得很宽 */
.irk-cell {
  font-size: 11px;
  letter-spacing: 0;
  cursor: copy;
  word-break: break-all;
  border-bottom: 1px dashed transparent;
}
.irk-cell:hover {
  color: var(--primary);
  border-bottom-color: currentColor;
}

/* ------------------------------------------------------------- 登录页 --- */
.login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 15% 10%, #dbeafe 0%, transparent 60%),
    radial-gradient(700px 500px at 90% 90%, #e0e7ff 0%, transparent 55%),
    var(--bg);
}

.login-card {
  width: min(420px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 32px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.12);
}

.brand { display: flex; gap: 12px; align-items: center; }
.brand h1 { font-size: 17px; margin: 0; }
.brand p { margin: 2px 0 0; font-size: 12px; color: var(--ink-3); }
.brand-lg h1 { font-size: 22px; }
.brand-lg { margin-bottom: 24px; }

.brand-mark {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: linear-gradient(145deg, #2563eb, #4f46e5);
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  flex: none;
}

#login-form { display: grid; gap: 14px; }
#login-form label { display: grid; gap: 6px; font-size: 13px; color: var(--ink-2); }
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f9fafb;
  outline: none;
  transition: border-color .15s, background .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); background: #fff; }
input:disabled { color: var(--ink-3); }

.form-error { color: var(--danger); min-height: 18px; margin: 0; font-size: 12px; }

.login-hint {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px dashed var(--line);
  display: grid;
  gap: 4px;
  font-size: 12px;
  color: var(--ink-3);
}
.login-hint strong { color: var(--ink-2); }

/* ---------------------------------------------------------------- 布局 --- */
.app-view { display: flex; min-height: 100%; }

.sidebar {
  width: 232px;
  flex: none;
  background: linear-gradient(180deg, var(--sidebar), #111c33);
  color: #cbd5e1;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar .brand h1 { color: #f8fafc; }
.sidebar .brand p { color: #64748b; }
.sidebar .brand-mark { font-size: 16px; }

.nav { display: grid; gap: 4px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: #cbd5e1;
  cursor: pointer;
  text-align: left;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: rgba(148, 163, 184, .12); color: #f8fafc; }
.nav-item.active { background: var(--primary); color: #fff; box-shadow: 0 8px 20px rgba(37, 99, 235, .35); }
.nav-item span { width: 20px; text-align: center; }

.sidebar-foot { margin-top: auto; display: grid; gap: 10px; }
.who { font-size: 12px; color: #94a3b8; line-height: 1.6; }
.who strong { display: block; color: #e2e8f0; font-size: 13px; }
.version { font-size: 11px; color: #475569; margin: 0; }

.main { flex: 1; min-width: 0; padding: 22px 28px 40px; }
.topbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 18px; }
.topbar h2 { margin: 0; font-size: 22px; }
.topbar p { margin: 4px 0 0; font-size: 13px; }
.topbar-actions { display: flex; gap: 10px; align-items: center; }

.content { display: grid; gap: 18px; }

/* ---------------------------------------------------------------- 组件 --- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.card-head h3 { margin: 0; font-size: 15px; }
.card-head p { margin: 3px 0 0; font-size: 12px; color: var(--ink-3); }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; }
.stat {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.stat::after {
  content: "";
  position: absolute;
  right: -24px;
  top: -24px;
  width: 86px;
  height: 86px;
  border-radius: 50%;
  background: var(--tint, var(--primary-soft));
  opacity: .8;
}
.stat b { display: block; font-size: 28px; letter-spacing: -.5px; margin: 6px 0 2px; }
.stat small { color: var(--ink-3); }
.stat .stat-label { font-size: 12px; color: var(--ink-2); }

.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  background: #eef1f6;
  color: var(--ink-2);
  transition: filter .15s, background .15s, color .15s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink-2); }
.sidebar .btn-ghost { color: #cbd5e1; border-color: rgba(148, 163, 184, .25); }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 8px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.icon-btn {
  border: 0;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  color: var(--ink-3);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 8px;
}
.icon-btn:hover { background: #f1f5f9; color: var(--ink); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  background: #f1f5f9;
  color: var(--ink-2);
}
.pill-ok { background: var(--success-soft); color: #15803d; }
.pill-warn { background: var(--warning-soft); color: #b45309; }
.pill-bad { background: var(--danger-soft); color: #b91c1c; }
.pill-muted { background: #f1f5f9; color: var(--ink-3); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-weight: 600; color: var(--ink-3); font-size: 12px; background: #fafbfc; }
tbody tr:hover { background: #fafcff; }
td.wrap { white-space: normal; min-width: 180px; }
.empty { text-align: center; color: var(--ink-3); padding: 32px 0; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.toolbar label { display: grid; gap: 5px; font-size: 12px; color: var(--ink-2); }
.toolbar input, .toolbar select { min-width: 130px; }
.toolbar .grow { flex: 1; }

.avatar {
  width: 30px; height: 30px; border-radius: 50%;
  display: inline-grid; place-items: center;
  background: var(--primary-soft); color: var(--primary-dark);
  font-size: 12px; font-weight: 600; margin-right: 8px;
}
.person { display: flex; align-items: center; }

/* 周趋势 */
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: 12px; align-items: end; height: 170px; padding-top: 10px; }
.bar-col { display: grid; grid-template-rows: 1fr auto; gap: 8px; height: 100%; text-align: center; }
.bar-track { display: flex; align-items: flex-end; justify-content: center; gap: 4px; height: 100%; }
.bar { width: 12px; border-radius: 6px 6px 2px 2px; background: var(--primary); min-height: 3px; transition: height .3s; }
.bar.late { background: var(--warning); }
.bar-col small { color: var(--ink-3); font-size: 11px; }

.device-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; }
.device-card { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; background: #fff; }
.device-card header { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.device-card h4 { margin: 0; font-size: 15px; }
.device-card .meta { color: var(--ink-3); font-size: 12px; margin-top: 4px; line-height: 1.7; }
.device-card .meta strong { color: var(--ink-2); font-weight: 500; }
.device-card .actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

.field-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.field-grid label { display: grid; gap: 5px; font-size: 12px; color: var(--ink-2); }
.switch-row { display: flex; align-items: center; justify-content: space-between; padding: 9px 0; border-bottom: 1px dashed var(--line); }
.switch-row:last-child { border-bottom: 0; }
.switch-row span { font-size: 13px; color: var(--ink-2); }
.switch { position: relative; width: 42px; height: 24px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch i { position: absolute; inset: 0; background: #d1d5db; border-radius: 999px; transition: background .15s; }
.switch i::after { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked + i { background: var(--primary); }
.switch input:checked + i::after { transform: translateX(18px); }

/* 协议调试 */
.protocol-grid { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.protocol-actions { display: grid; gap: 8px; }
.protocol-actions button { justify-content: flex-start; text-align: left; display: flex; gap: 8px; align-items: center; }
pre.log {
  margin: 0;
  background: #0b1220;
  color: #a5b4fc;
  border-radius: 12px;
  padding: 16px;
  min-height: 340px;
  max-height: 540px;
  overflow: auto;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 12px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-all;
}
pre.log .ok { color: #34d399; }
pre.log .err { color: #f87171; }

/* 弹窗 */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .45);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}
.modal-card {
  width: min(640px, 100%);
  max-height: 88vh;
  overflow: auto;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 30px 80px rgba(15, 23, 42, .3);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: #fff; }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px 22px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--line); position: sticky; bottom: 0; background: #fff; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 11px 20px;
  border-radius: 12px;
  z-index: 80;
  box-shadow: 0 16px 40px rgba(15, 23, 42, .35);
  font-size: 13px;
}
.toast.bad { background: #b91c1c; }

@media (max-width: 900px) {
  .app-view { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 12px; padding: 14px; }
  .nav { display: flex; overflow-x: auto; width: 100%; order: 3; }
  .sidebar-foot { margin: 0 0 0 auto; display: flex; align-items: center; gap: 10px; }
  .sidebar-foot .version { display: none; }
  .main { padding: 16px; }
  .protocol-grid { grid-template-columns: 1fr; }
}

/* 菜单图标：统一 18px 线性图标（替换原 emoji，跨系统渲染一致） */
.nav .ico { width: 18px; height: 18px; flex: 0 0 18px; opacity: .72; }
.nav-item.active .ico { opacity: 1; }
.brand-logo img { width: 100%; height: 100%; border-radius: 8px; display: block; }
