:root {
  --bg: #f5f5f2;
  --panel: #ffffff;
  --line: #e4e2dc;
  --line-soft: #efeee9;
  --text: #24231f;
  --text-2: #6b6a64;
  --text-3: #9c9a93;
  --accent: #1d5fb0;
  --accent-soft: #e8f0fb;
  --ok: #1a7f4b;
  --warn: #b0700f;
  --danger: #b13a34;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

button, input, select, textarea { font-family: inherit; }
button { cursor: pointer; font-size: 13px; -webkit-tap-highlight-color: transparent; }

/* ---------------------------------- 布局 ---------------------------------- */

#app {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-height: 0;
}

.sidebar-mask { display: none; }

.icon-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text-2);
  padding: 0;
  flex: 0 0 auto;
}
.icon-btn:active { background: #f2f1ec; }

.sidebar-head {
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--line-soft);
}

.brand h1 {
  margin: 0 0 10px;
  font-size: 14.5px;
  font-weight: 500;
  letter-spacing: .01em;
  display: flex;
  align-items: center;
}

.brand .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--ok);
  display: inline-block;
  margin-right: 7px;
}
.brand .dot.off { background: var(--danger); }

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.user-name {
  font-size: 12px;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.user-actions { display: flex; gap: 2px; flex: 0 0 auto; }

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* ---------------------------------- 顶栏 ---------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
  flex-wrap: wrap;
}
.spacer { flex: 1; }

.seg {
  display: flex;
  background: #f4f2ed;
  border-radius: 9px;
  padding: 3px;
  flex: 0 0 auto;
}
.seg button {
  border: 0;
  background: transparent;
  padding: 5px 14px;
  color: var(--text-2);
  font-size: 12.5px;
  border-radius: 7px;
  transition: .15s;
}
.seg button.active {
  background: var(--panel);
  color: var(--text);
  font-weight: 500;
  box-shadow: 0 1px 2px rgba(0,0,0,.07);
}

.range-bar {
  display: none;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
}
.range-bar.show { display: flex; }

.range-bar input[type=datetime-local] {
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 12px;
  color: var(--text);
  background: var(--panel);
  outline: none;
}
.range-bar input[type=datetime-local]:focus { border-color: var(--accent); }
.range-sep { color: var(--text-3); font-size: 12px; }
.quick { display: flex; gap: 2px; margin-left: 2px; }

/* ---------------------------------- 地图 ---------------------------------- */

.map-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}
#map { flex: 1; min-height: 0; background: #ecebe6; }

.detail {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 262px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 2px 12px rgba(0,0,0,.08);
  padding: 14px;
  display: none;
  z-index: 5;
}
.detail.show { display: block; }
.detail h2 { margin: 0 0 2px; font-size: 14px; font-weight: 500; }
.detail .addr { font-size: 12px; color: var(--text-2); line-height: 1.6; margin: 8px 0 10px; }
.kv { display: flex; justify-content: space-between; padding: 6px 0; border-top: 1px solid var(--line-soft); font-size: 12px; }
.kv span:first-child { color: var(--text-3); }
.kv span:last-child { font-variant-numeric: tabular-nums; text-align: right; }

.timeline {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  background: rgba(255,255,255,.97);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: 0 2px 10px rgba(0,0,0,.07);
  display: none;
  z-index: 5;
}
.timeline.show { display: block; }
.timeline-row { display: flex; align-items: center; gap: 12px; }
.timeline input[type=range] { flex: 1; accent-color: var(--accent); }
.tl-time { font-size: 12px; color: var(--text-2); font-variant-numeric: tabular-nums; min-width: 140px; }
.tl-count { font-size: 11.5px; color: var(--text-3); }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 8px;
  color: var(--text-3);
  font-size: 13px;
  pointer-events: none;
}
.empty strong { color: var(--text-2); font-weight: 500; font-size: 14px; }

.map-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px 22px;
  max-width: 420px;
  line-height: 1.7;
  font-size: 12.5px;
  color: var(--text-2);
  box-shadow: 0 4px 20px rgba(0,0,0,.08);
  z-index: 6;
}
.map-hint b { color: var(--text); font-weight: 500; }
.map-hint code { background: #f4f2ed; padding: 1px 5px; border-radius: 4px; font-size: 11.5px; font-family: ui-monospace, Menlo, Consolas, monospace; }

/* ---------------------------------- 设备列表 ---------------------------------- */

.device-list { flex: 1; overflow-y: auto; padding: 8px 8px 24px; }

.group-title {
  font-size: 11px;
  color: var(--text-3);
  padding: 12px 8px 6px;
  letter-spacing: .04em;
}

.dev-item {
  display: flex;
  gap: 10px;
  padding: 9px;
  border-radius: var(--radius);
  cursor: pointer;
  align-items: center;
  transition: background .12s;
}
.dev-item:hover { background: #f7f6f2; }
.dev-item.active { background: var(--accent-soft); }

.dev-avatar {
  width: 34px; height: 34px;
  border-radius: 8px;
  background: #f1efe9;
  flex: 0 0 34px;
  object-fit: contain;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--text-3);
  overflow: hidden;
}
.dev-avatar img { width: 100%; height: 100%; object-fit: contain; padding: 2px; }

.dev-main { min-width: 0; flex: 1; }
.dev-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}
.dev-sub {
  font-size: 11.5px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 3px;
}

.pill { font-size: 10px; padding: 1px 6px; border-radius: 20px; font-weight: 400; flex: 0 0 auto; }
.pill.on { background: #e7f5ed; color: var(--ok); }
.pill.off { background: #f3f1ec; color: var(--text-3); }

/* ---------------------------------- 按钮 ---------------------------------- */

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 8px;
  transition: .15s;
}
.btn:hover { background: #faf9f6; border-color: #d6d3cb; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #19539b; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: #f2f1ec; color: var(--text); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.sm { padding: 3px 9px; font-size: 12px; }

/* ---------------------------------- 模态 ---------------------------------- */

.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(30,29,26,.32);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-mask.show { display: flex; }

.modal {
  background: var(--panel);
  border-radius: 14px;
  width: 348px;
  padding: 20px;
  box-shadow: 0 12px 40px rgba(0,0,0,.16);
}
.modal h3 { margin: 0 0 4px; font-size: 15px; font-weight: 500; }
.modal p.hint { margin: 0 0 16px; font-size: 12px; color: var(--text-3); line-height: 1.6; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 5px; }
.field input, .field textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13px;
  outline: none;
}
.field textarea { font-size: 12px; font-family: ui-monospace, Menlo, Consolas, monospace; line-height: 1.5; resize: vertical; }
.field input:focus, .field textarea:focus { border-color: var(--accent); }

.tabs { display: flex; gap: 4px; margin-bottom: 16px; background: #f4f2ed; padding: 3px; border-radius: 9px; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 6px; border-radius: 7px; color: var(--text-2); font-size: 12.5px; }
.tabs button.active { background: var(--panel); color: var(--text); font-weight: 500; box-shadow: 0 1px 2px rgba(0,0,0,.06); }

.qr-box { text-align: center; padding: 8px 0 4px; }
.qr-box img { width: 200px; height: 200px; border-radius: 8px; border: 1px solid var(--line); }
.qr-status { font-size: 12px; color: var(--text-2); margin-top: 10px; line-height: 1.6; }

.msg { font-size: 12px; padding: 8px 10px; border-radius: 8px; margin-bottom: 12px; display: none; line-height: 1.5; }
.msg.show { display: block; }
.msg.err { background: #fceceb; color: var(--danger); }
.msg.info { background: var(--accent-soft); color: var(--accent); }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #24231f;
  color: #fff;
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 12.5px;
  opacity: 0;
  transition: .2s;
  pointer-events: none;
  z-index: 99;
}
.toast.show { opacity: 1; }

/* ---------------------------------- 系统登录 ---------------------------------- */

.login-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.login-screen.hide { display: none; }

.login-card {
  width: 320px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px 24px 22px;
  box-shadow: 0 8px 30px rgba(0,0,0,.06);
}
.login-card h1 { margin: 0 0 4px; font-size: 17px; font-weight: 500; }
.login-card .sub { margin: 0 0 18px; font-size: 12px; color: var(--text-3); }

/* ---------------------------------- 管理后台 ---------------------------------- */

.admin-modal { width: 620px; max-height: 86vh; overflow-y: auto; padding: 22px; }
.admin-section { margin-bottom: 18px; }
.admin-title { font-size: 12px; color: var(--text-3); margin-bottom: 8px; overflow: hidden; }

.row-form { display: flex; gap: 8px; }
.row-form input {
  flex: 1; min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12.5px;
  outline: none;
}
.row-form select {
  padding: 7px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12.5px;
  background: var(--panel);
  outline: none;
}

.user-table .u-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  margin-bottom: 6px;
  font-size: 12.5px;
}
.u-row .u-name { font-weight: 500; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-row .u-tag { font-size: 10.5px; padding: 1px 7px; border-radius: 20px; background: #f1efe9; color: var(--text-2); flex: 0 0 auto; }
.u-row .u-tag.admin { background: var(--accent-soft); color: var(--accent); }
.u-row .u-tag.warn { background: #fdf1e3; color: var(--warn); }
.u-row .u-meta { color: var(--text-3); font-size: 11.5px; margin-left: auto; flex: 0 0 auto; }
.u-row.active { border-color: var(--accent); background: #f7fafe; }

.perm-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
}
.perm-list label { display: flex; align-items: center; gap: 6px; font-size: 12px; padding: 4px 6px; border-radius: 6px; cursor: pointer; }
.perm-list label:hover { background: #f7f6f2; }

.alias-list .a-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.alias-list .a-row span {
  flex: 0 0 130px;
  font-size: 12px;
  color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.alias-list .a-row input {
  flex: 1; min-width: 0;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 12.5px;
  outline: none;
}
.alias-list .a-row input:focus { border-color: var(--accent); }

.email-form .row-form { margin-bottom: 6px; }
.email-form .chk {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--text-2);
  margin-bottom: 8px;
  cursor: pointer;
}
.mail-tip { font-size: 12px; color: var(--text-3); align-self: center; }
.mail-tip.err { color: var(--danger); }
.mail-tip.ok { color: var(--ok); }

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: #ddd9d1; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------------------------------- 移动端 ---------------------------------- */

@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 84vw;
    max-width: 320px;
    z-index: 60;
    transform: translateX(-102%);
    transition: transform .22s ease;
    box-shadow: 0 0 28px rgba(0, 0, 0, .18);
    border-right: 0;
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-mask {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(30, 29, 26, .36);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
    z-index: 55;
  }
  .sidebar-mask.show { opacity: 1; pointer-events: auto; }

  .icon-btn.menu-btn { display: inline-flex; }

  .topbar { padding: 8px 10px; gap: 8px; }
  .seg button { padding: 6px 12px; }
  .btn { min-height: 32px; }

  .range-bar {
    order: 10;
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: none;
  }
  .range-bar::-webkit-scrollbar { display: none; }
  .range-bar input[type=datetime-local] { flex: 0 0 auto; font-size: 12px; }
  .quick { flex: 0 0 auto; }
  .range-sep { flex: 0 0 auto; }

  .detail {
    top: auto;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    left: 8px;
    right: 8px;
    width: auto;
    max-height: 44vh;
    overflow-y: auto;
    padding: 12px;
  }
  .detail h2 { font-size: 13.5px; }
  .detail .addr { margin: 6px 0 8px; }
  .kv { padding: 5px 0; }

  .map-wrap { flex-direction: column; }

  .timeline {
    position: static;
    order: -1;
    margin: 8px 10px 0;
    box-shadow: none;
  }
  .timeline-row { gap: 8px; }
  .tl-time { min-width: 96px; font-size: 11px; }
  .tl-count { display: none; }

  .modal, .login-card { width: calc(100vw - 32px); }
  .admin-modal { width: calc(100vw - 24px); padding: 16px; max-height: 88vh; }
  .row-form { flex-wrap: wrap; }
  .row-form input, .row-form select { flex: 1 1 130px; }
  .perm-list { grid-template-columns: 1fr; max-height: 180px; }
  .u-row { flex-wrap: wrap; row-gap: 6px; }
  .u-row .u-meta { margin-left: 0; }
  .alias-list .a-row span { flex: 0 0 96px; }

  .dev-item { padding: 11px 9px; }
  .device-list { padding: 6px 8px calc(20px + env(safe-area-inset-bottom, 0px)); }
}

@media (max-width: 420px) {
  .brand h1 { font-size: 14px; }
  .seg button { padding: 6px 10px; font-size: 12px; }
  .detail { max-height: 50vh; }
  .login-card { padding: 24px 18px 18px; }
}
