/* =============================================================
   ESP32 设备管理平台 — 样式系统
   纯原生 CSS，无构建步骤，无外部 CDN 依赖
   ============================================================= */

:root {
  --bg: #080d18;
  --bg-elev: #0e1526;
  --surface: #121b2e;
  --surface-2: #17223a;
  --border: #223148;
  --border-strong: #2e405c;

  --text: #e8eef8;
  --text-dim: #9aabc4;
  --text-faint: #64748b;

  --accent: #4d8dff;
  --accent-2: #7c6bff;
  --accent-soft: rgba(77, 141, 255, 0.14);

  --ok: #23c55e;
  --ok-soft: rgba(35, 197, 94, 0.14);
  --warn: #f5a623;
  --warn-soft: rgba(245, 166, 35, 0.14);
  --danger: #f05252;
  --danger-soft: rgba(240, 82, 82, 0.14);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;

  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px -12px rgba(0, 0, 0, 0.6);
  --shadow-lg: 0 24px 60px -20px rgba(0, 0, 0, 0.75);

  --font: "Inter", "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", "Cascadia Code", "SF Mono", Consolas, monospace;

  --header-h: 60px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  background:
    radial-gradient(1200px 600px at 15% -10%, rgba(77, 141, 255, 0.10), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(124, 107, 255, 0.10), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 24px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }

p { margin: 0 0 12px; }
small { font-size: 12px; }

.mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }
.hidden { display: none !important; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- 按钮 ---------- */
.btn {
  --btn-bg: var(--surface-2);
  --btn-fg: var(--text);
  --btn-bd: var(--border-strong);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--btn-bd);
  border-radius: var(--radius-sm);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font: inherit;
  font-size: 13.5px;
  font-weight: 550;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.06s, opacity 0.15s;
  white-space: nowrap;
  user-select: none;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-primary {
  --btn-bg: linear-gradient(135deg, var(--accent), var(--accent-2));
  --btn-fg: #fff;
  --btn-bd: transparent;
  box-shadow: 0 8px 20px -10px rgba(77, 141, 255, 0.9);
}
.btn-primary:hover { filter: brightness(1.08); border-color: transparent; }

.btn-ghost { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--text-dim); }
.btn-ghost:hover { --btn-bg: var(--surface-2); --btn-fg: var(--text); border-color: transparent; }

.btn-danger { --btn-bg: var(--danger-soft); --btn-fg: #ffb4b4; --btn-bd: rgba(240, 82, 82, 0.35); }
.btn-danger:hover { --btn-bg: rgba(240, 82, 82, 0.24); border-color: var(--danger); }

.btn-sm { height: 30px; padding: 0 10px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn-icon { width: 36px; padding: 0; }

/* ---------- 表单 ---------- */
.field { margin-bottom: 14px; }
.field > label {
  display: block;
  margin-bottom: 6px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--text-dim);
}
.input, .select, .textarea {
  width: 100%;
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  color: var(--text);
  font: inherit;
  font-size: 13.5px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.textarea { height: auto; min-height: 80px; padding: 10px 12px; resize: vertical; line-height: 1.55; }
.input::placeholder, .textarea::placeholder { color: var(--text-faint); }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
                    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 17px) 16px, calc(100% - 12px) 16px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}
.hint { margin-top: 5px; font-size: 12px; color: var(--text-faint); }
.err-text { margin-top: 6px; font-size: 12.5px; color: #ff9b9b; }

/* ---------- 卡片 ---------- */
.card {
  background: linear-gradient(180deg, rgba(23, 34, 58, 0.75), rgba(18, 27, 46, 0.92));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-pad { padding: 18px; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

/* ---------- 徽标 ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 22px;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.01em;
  background: var(--surface-2);
  color: var(--text-dim);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-ok { background: var(--ok-soft); color: #6ee7a0; border-color: rgba(35, 197, 94, 0.3); }
.badge-off { background: rgba(100, 116, 139, 0.14); color: #a9b6c9; border-color: rgba(100, 116, 139, 0.3); }
.badge-warn { background: var(--warn-soft); color: #f7c56b; border-color: rgba(245, 166, 35, 0.3); }
.badge-danger { background: var(--danger-soft); color: #ff9b9b; border-color: rgba(240, 82, 82, 0.3); }
.badge-accent { background: var(--accent-soft); color: #9cc2ff; border-color: rgba(77, 141, 255, 0.3); }

.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.dot-live { box-shadow: 0 0 0 0 currentColor; animation: pulse 2.2s infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(35, 197, 94, 0.5); }
  70% { box-shadow: 0 0 0 7px rgba(35, 197, 94, 0); }
  100% { box-shadow: 0 0 0 0 rgba(35, 197, 94, 0); }
}

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: rgba(8, 13, 24, 0.82);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -0.02em; }
.brand-mark {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-size: 14px; font-weight: 800;
  box-shadow: 0 6px 16px -8px rgba(77, 141, 255, 0.9);
}
.nav {
  display: flex;
  align-items: center;
  gap: 4px;
  /* 管理员登录后导航项较多，窄屏横向滚动而不是撑破顶栏 */
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.nav::-webkit-scrollbar { display: none; }
.nav a {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-size: 13.5px;
  font-weight: 550;
  text-decoration: none;
  white-space: nowrap;
  flex: none;
  transition: background 0.15s, color 0.15s;
}
.nav a:hover { background: var(--surface); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: #a8c8ff; }
/* 管理端与用户端导航之间的分隔线 */
.nav-sep {
  width: 1px;
  height: 18px;
  margin: 0 7px;
  flex: none;
  background: var(--border-strong);
}

.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #2b3f63, #1d2b45);
  border: 1px solid var(--border-strong);
  font-size: 12px; font-weight: 700; color: #b9cdea;
  text-transform: uppercase;
  flex: none;
}

/* ---------- 布局 ---------- */
.page { max-width: 1180px; margin: 0 auto; padding: 24px 20px 60px; }
.page-narrow { max-width: 880px; }
.page-head { margin-bottom: 20px; }
.page-head h1 { margin-bottom: 4px; }
.page-head .sub { color: var(--text-dim); font-size: 13.5px; }

.grid { display: grid; gap: 14px; }
.grid-cards { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.grid-stats { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }

/* ---------- 统计卡 ---------- */
.stat { padding: 16px 18px; }
.stat-label { font-size: 12px; color: var(--text-dim); font-weight: 550; letter-spacing: 0.02em; }
.stat-value { font-size: 26px; font-weight: 700; letter-spacing: -0.03em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat-foot { font-size: 12px; color: var(--text-faint); margin-top: 2px; }

/* ---------- 设备卡 ---------- */
.device-card {
  position: relative;
  padding: 16px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
  overflow: hidden;
}
.device-card:hover {
  border-color: var(--border-strong);
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}
.device-card::after {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 2px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  opacity: 0;
  transition: opacity 0.15s;
}
.device-card:hover::after { opacity: 1; }
.device-card.is-online { border-color: rgba(35, 197, 94, 0.28); }
.device-name { font-size: 15px; font-weight: 650; margin-bottom: 2px; }
.device-meta { font-size: 12px; color: var(--text-faint); font-family: var(--mono); }
.device-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}
.metric-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 7px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  font-size: 12px;
  white-space: nowrap;
  flex: none;
}
.metric-chip b { font-weight: 650; font-variant-numeric: tabular-nums; }
.metric-chip span { color: var(--text-faint); font-size: 11px; }

/* ---------- 表格 ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.tbl th {
  text-align: left;
  padding: 10px 14px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: rgba(18, 27, 46, 0.6);
}
table.tbl td { padding: 11px 14px; border-bottom: 1px solid rgba(34, 49, 72, 0.55); vertical-align: middle; }
table.tbl tbody tr:hover { background: rgba(23, 34, 58, 0.5); }
table.tbl tbody tr:last-child td { border-bottom: none; }
.tbl-empty { padding: 40px 16px; text-align: center; color: var(--text-faint); }

/* ---------- 空状态 ---------- */
.empty {
  padding: 56px 24px;
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  background: rgba(14, 21, 38, 0.5);
}
.empty-icon { font-size: 34px; opacity: 0.55; margin-bottom: 10px; }
.empty h3 { margin-bottom: 6px; }
.empty p { color: var(--text-dim); font-size: 13px; max-width: 420px; margin: 0 auto 16px; }

/* ---------- 骨架屏 ---------- */
.skeleton {
  background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--radius-sm);
  color: transparent !important;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- 登录页 ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  border-radius: var(--radius-lg);
  background: linear-gradient(180deg, rgba(23, 34, 58, 0.9), rgba(14, 21, 38, 0.97));
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
}
.auth-brand { display: flex; align-items: center; gap: 11px; margin-bottom: 22px; }
.auth-brand .brand-mark { width: 36px; height: 36px; border-radius: 10px; font-size: 16px; }
.auth-title { font-size: 19px; font-weight: 700; letter-spacing: -0.02em; }
.auth-sub { font-size: 12.5px; color: var(--text-faint); }

.tabs { display: flex; gap: 4px; padding: 4px; background: var(--bg-elev); border-radius: 10px; border: 1px solid var(--border); margin-bottom: 20px; }
.tabs button {
  flex: 1;
  height: 32px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tabs button.active { background: var(--surface-2); color: var(--text); box-shadow: var(--shadow); }

/* ---------- 弹窗 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(4, 8, 16, 0.72);
  backdrop-filter: blur(3px);
  animation: fade 0.15s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: 100%;
  max-width: 480px;
  max-height: calc(100vh - 40px);
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  animation: pop 0.16s ease-out;
}
.modal-wide { max-width: 720px; }
@keyframes pop { from { transform: scale(0.97) translateY(6px); opacity: 0; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }

/* ---------- 提示条 / Toast ---------- */
.callout {
  display: flex;
  gap: 10px;
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  line-height: 1.55;
  border: 1px solid;
}
.callout-warn { background: var(--warn-soft); border-color: rgba(245, 166, 35, 0.3); color: #f2c879; }
.callout-danger { background: var(--danger-soft); border-color: rgba(240, 82, 82, 0.3); color: #ffb1b1; }
.callout-info { background: var(--accent-soft); border-color: rgba(77, 141, 255, 0.3); color: #a8c8ff; }
.callout-ok { background: var(--ok-soft); border-color: rgba(35, 197, 94, 0.3); color: #86efac; }

#toasts {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  min-width: 220px;
  max-width: 360px;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-lg);
  font-size: 13px;
  animation: slide-in 0.2s ease-out;
  pointer-events: auto;
}
.toast-ok { border-color: rgba(35, 197, 94, 0.45); }
.toast-error { border-color: rgba(240, 82, 82, 0.5); }
@keyframes slide-in { from { transform: translateX(16px); opacity: 0; } }

/* ---------- 提示块（密钥展示） ---------- */
.secret-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  border: 1px dashed rgba(245, 166, 35, 0.45);
  font-family: var(--mono);
  font-size: 12.5px;
  word-break: break-all;
  color: #f7d08a;
}

/* ---------- 对话 ---------- */
.chat-shell {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--header-h) - 48px);
  min-height: 420px;
}
.chat-log { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; gap: 10px; max-width: 86%; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; }
.msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-assistant .msg-bubble { background: var(--surface-2); border: 1px solid var(--border); border-top-left-radius: 4px; }
.msg-user .msg-bubble { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; border-top-right-radius: 4px; }
.msg-tools { margin-top: 8px; display: flex; flex-direction: column; gap: 5px; }
.tool-trace {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text-dim);
  padding: 5px 9px;
  border-radius: 6px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
}
.tool-trace.tool-fail { color: #ffb1b1; border-color: rgba(240, 82, 82, 0.35); }
.chat-input-bar { border-top: 1px solid var(--border); padding: 12px; display: flex; gap: 8px; align-items: flex-end; }
.chat-input-bar .textarea { min-height: 44px; max-height: 140px; }
.typing { display: inline-flex; gap: 3px; align-items: center; }
.typing i { width: 5px; height: 5px; border-radius: 50%; background: var(--text-dim); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* ---------- 详情页 ---------- */
.detail-hero { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kv-item { padding: 11px 13px; border-radius: var(--radius-sm); background: var(--bg-elev); border: 1px solid var(--border); }
.kv-key { font-size: 11.5px; color: var(--text-faint); margin-bottom: 3px; }
.kv-val { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }

.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-faint); font-weight: 600; margin: 22px 0 10px; }

/* ---------- 分页 ---------- */
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--border); font-size: 12.5px; color: var(--text-dim); }

/* ---------- 响应式 ---------- */
@media (max-width: 720px) {
  .topbar { padding: 0 12px; gap: 10px; }
  .brand span.brand-text { display: none; }
  .nav a { padding: 6px 9px; font-size: 13px; }
  .page { padding: 16px 12px 48px; }
  .msg { max-width: 94%; }
  .modal-foot { flex-direction: column-reverse; }
  .modal-foot .btn { width: 100%; }
  .grid-cards { grid-template-columns: 1fr; }
}

/* ---------- 设备类型标签（小语 / 自研） ---------- */
.tag{display:inline-block;font-size:11px;font-weight:700;padding:2px 7px;border-radius:5px;
     margin-left:7px;vertical-align:2px;letter-spacing:.3px;white-space:nowrap}
.tag-xiaoyu{background:rgba(124,92,214,.18);color:#a58bff;border:1px solid rgba(124,92,214,.45)}
.tag-native{background:rgba(61,127,209,.15);color:#6fa8f5;border:1px solid rgba(61,127,209,.4)}
