:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-2: #64748b;
  --sidebar: #0f172a;
  --sidebar-text: #cbd5e1;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --danger-dark: #b91c1c;
  --ok: #16a34a;
  --radius: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

#app { height: 100%; }
.boot-loading { display: flex; align-items: center; justify-content: center; height: 100vh; color: var(--text-2); }

a { color: var(--primary); text-decoration: none; }

/* ---------- 布局 ---------- */
.layout { display: flex; height: 100vh; }

.sidebar {
  width: 220px;
  flex: none;
  background: var(--sidebar);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
}
.sidebar .brand {
  padding: 20px 16px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar .brand small { display: block; font-size: 12px; font-weight: 400; color: var(--sidebar-text); margin-top: 4px; }
.sidebar nav { flex: 1; padding: 12px 8px; }
.sidebar nav a {
  display: block;
  padding: 10px 12px;
  margin: 2px 0;
  border-radius: 6px;
  color: var(--sidebar-text);
}
.sidebar nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar nav a.active { background: var(--primary); color: #fff; }
.sidebar .foot { padding: 12px 16px; font-size: 12px; color: #64748b; border-top: 1px solid rgba(255,255,255,.08); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: 56px;
  flex: none;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}
.topbar .title { font-size: 16px; font-weight: 600; }
.userbox { display: flex; align-items: center; gap: 10px; }

/* 汉堡按钮：仅移动端显示 */
.menu-btn {
  display: none;
  flex: none;
  width: 36px;
  height: 36px;
  margin-right: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  cursor: pointer;
  padding: 0;
  align-items: center;
  justify-content: center;
}
.menu-btn svg { display: block; }
.userbox .name { font-weight: 500; }

.content { flex: 1; overflow: auto; padding: 24px; }

/* 登录页页脚：版权与 ICP 备案，仅登录页显示（贴视口底部，适配深色渐变背景） */
.page-foot {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  font-size: 12px;
}
.login-foot {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  color: rgba(255, 255, 255, 0.72);
}
.login-foot a { color: rgba(255, 255, 255, 0.72); }
.login-foot a:hover { color: #ffffff; }

/* ---------- 卡片 / 表格 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card h3 { margin: 0 0 4px; font-size: 14px; }
.card .hint { margin: 0 0 12px; color: var(--text-2); font-size: 12px; }

.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }

.table-wrap { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
table { width: 100%; border-collapse: collapse; white-space: nowrap; }
th, td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: #f8fafc; font-weight: 600; color: var(--text-2); font-size: 13px; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }
td.wrap { white-space: normal; min-width: 160px; }

/* 设备列表：压缩列宽，桌面端避免出现横向滚动条 */
.table-compact th, .table-compact td { padding: 8px 10px; }
.table-compact .nowrap { white-space: nowrap; }
.table-compact td.wrap { min-width: 120px; }
.table-compact .badge { padding: 1px 6px; font-size: 11px; }
.table-compact .btn-link { padding: 2px 4px; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; }
.empty { padding: 40px; text-align: center; color: var(--text-2); }

/* ---------- 按钮 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}
.btn:hover { background: #f8fafc; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-dark); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-link { border: none; background: none; color: var(--primary); cursor: pointer; padding: 2px 6px; font-size: 13px; }
.btn-link.danger { color: var(--danger); }
.btn-link:hover { text-decoration: underline; }

/* ---------- 表单 ---------- */
input[type=text], input[type=password], input[type=number], input[type=file], input[type=tel], select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  outline: none;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(37,99,235,.15); }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--text-2); }
.field .err { color: var(--danger); font-size: 12px; min-height: 14px; }
.form-row { display: flex; gap: 12px; }
.form-row .field { flex: 1; }

/* ---------- 徽标 ---------- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
}
.badge-admin { background: #dbeafe; color: #1d4ed8; }
.badge-viewer { background: #e2e8f0; color: #475569; }
.badge-ok { background: #dcfce7; color: #15803d; }
.badge-mute { background: #f1f5f9; color: #94a3b8; }
.badge-fail { background: #fee2e2; color: #b91c1c; }

.auth-recent { color: var(--ok); }
.auth-old { color: var(--text-2); }
.auth-never { color: #94a3b8; }

/* ---------- 登录页 ---------- */
.login-wrap {
  position: relative;
  height: 100vh;
  display: flex;
  /* 不垂直居中，卡片固定落在视口顶部约 10% 处 */
  align-items: flex-start;
  justify-content: center;
  padding-top: 15vh;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
}
/* 登录页页脚：贴视口底部，适配深色渐变背景 */
.login-foot {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: transparent;
  border-top: none;
  color: rgba(255, 255, 255, 0.72);
}
.login-foot a { color: rgba(255, 255, 255, 0.72); }
.login-foot a:hover { color: #ffffff; }
.login-card {
  /* 桌面固定 360px，窄窗口按视口收缩（两侧至少留 16px），避免溢出或横向滚动 */
  width: min(360px, calc(100vw - 32px));
  background: var(--card);
  border-radius: 12px;
  padding: 32px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card .sub { margin: 0 0 24px; color: var(--text-2); font-size: 13px; }
.login-card .err { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 8px; }
.login-card .btn-primary { width: 100%; justify-content: center; padding: 10px; font-size: 14px; }

/* ---------- 模态框 ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal {
  width: 480px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 64px);
  overflow: auto;
  background: var(--card);
  border-radius: 12px;
  padding: 20px 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
.modal h3 { margin: 0 0 16px; font-size: 16px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------- 提示条 ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
  z-index: 200;
}
#toast.show { opacity: .95; }
#toast.error { background: var(--danger); }

/* ===== 设备数据查看 ===== */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
  align-items: start;
}
.cards-grid .card-wide { grid-column: 1 / -1; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px dashed #edf0f3; font-size: 13px; }
.kv:last-of-type { border-bottom: none; }
.kv span { color: var(--text-2); }
.big-num { font-size: 34px; font-weight: 700; margin: 6px 0 10px; }
.big-num small { font-size: 16px; color: var(--text-2); font-weight: 400; }
.title-inline { font-size: 15px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 6px 0 10px; font-size: 12px; color: var(--text-2); }
.chart-legend-item { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.chart-legend-item .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
#imu-chart { display: block; max-width: 100%; }

/* ---------- 移动端适配 ---------- */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.5);
  z-index: 90;
  display: none;
}

@media (max-width: 768px) {
  .layout { display: block; height: auto; min-height: 100vh; }
  .main { height: 100vh; }

  /* 数据视图工具栏：标题换行显示，按钮平分一行 */
  .toolbar .title-inline { flex-basis: 100%; order: 3; font-size: 13px; }
  .toolbar .title-inline .mono { word-break: break-all; }
  .toolbar label[for], .toolbar .chart-legend-item { white-space: nowrap; }

  /* 列表表格在小屏下转为卡片：每行一张卡，td 前显示字段名 */
  .table-wrap.cards { overflow: visible; background: none; border: none; border-radius: 0; }
  table.resp { display: block; }
  table.resp thead { display: none; }
  table.resp tbody { display: block; }
  table.resp tr {
    display: block;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 14px;
    margin-bottom: 10px;
  }
  table.resp td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
    border-bottom: 1px dashed #edf0f3;
    padding: 8px 0;
    white-space: normal;
  }
  table.resp tr td:last-child { border-bottom: none; }
  table.resp td::before {
    content: attr(data-label);
    color: var(--text-2);
    font-size: 12px;
    flex: none;
  }
  table.resp td.wrap { min-width: 0; }
  /* 下载 URL 等长串按字符断行，而不是只在完整词边界换行 */
  table.resp td.mono.wrap { word-break: break-all; }
  table.resp td .row-actions { display: flex; gap: 12px; flex-wrap: wrap; }
  table.resp tr:hover { background: var(--card); }

  /* 用户列表工具栏提示文字 */
  .toolbar .hint-inline { flex-basis: 100%; order: 3; }

  .card { padding: 12px; }
  .big-num { font-size: 28px; }
  .kv { gap: 8px; }
  .kv b { text-align: right; word-break: break-all; }
  .card .btn-primary { display: flex; justify-content: center; }
  #imu-chart { width: 100% !important; }

  /* 侧边栏改为抽屉 */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    width: 240px;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: none;
  }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .scrim.show { display: block; }

  .menu-btn { display: inline-flex; }
  .topbar { padding: 0 12px; }
  .topbar .title-wrap { display: flex; align-items: center; min-width: 0; }
  .topbar .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .userbox { gap: 6px; }
  .userbox .name { display: none; }

  .content { padding: 12px; }

  .toolbar input[type=text] { width: 100% !important; }
  .toolbar .btn { flex: 1 1 auto; justify-content: center; white-space: nowrap; }
  .toolbar .btn-sm { flex: 0 0 auto; }

  /* 固件上传表单：文件选择独占一行，版本号与上传按钮成组不拆开 */
  #fw-form { flex-wrap: wrap; }
  #fw-form input[type=file] { flex: 1 1 100%; }

  /* 表格横向滚动（.table-wrap 已有 overflow:auto） */
  .table-wrap { -webkit-overflow-scrolling: touch; }

  .cards-grid { grid-template-columns: 1fr; }
  .form-row { flex-direction: column; }

  .modal { width: auto; margin: 12px; padding: 16px; }
  /* 宽度由基础规则 min(360px, 100vw-32px) 统一控制，此处仅缩小内边距 */
  .login-card { padding: 16px 24px 24px; }

  #toast { left: 12px; right: 12px; bottom: 16px; transform: none; width: auto; text-align: center; }
}

@media (max-width: 420px) {
  .userbox .badge { display: none; }
  .userbox .btn-sm { padding: 4px 8px; }
}

@media (hover: none) {
  .sidebar nav a:hover { background: none; color: var(--sidebar-text); }
  .sidebar nav a.active:hover { background: var(--primary); color: #fff; }
}
