:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --line: #e2e6ec;
  --text: #1e2430;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.5 "Microsoft YaHei", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }
.err { color: var(--bad); font-size: 13px; min-height: 18px; margin: 6px 0 0; }

/* 登录 */
.login-wrap {
  height: 100%; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, #eef2f9, #f7f9fc);
}
.login-card {
  width: 320px; padding: 28px; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: 0 6px 24px rgba(15, 23, 42, .06);
}
.login-card h1 { font-size: 17px; margin: 0 0 4px; }
.login-card p { margin: 0 0 18px; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.login-card input { width: 100%; margin-top: 6px; }

input, select, button {
  font: inherit; color: var(--text);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 9px; background: #fff;
}
input:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
button { cursor: pointer; }
button:disabled, select:disabled { opacity: 0.55; cursor: wait; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.06); }
button.ghost { background: #fff; color: var(--text); }
button.ghost:hover { background: #f1f3f7; }
button.link { border: none; background: none; color: var(--accent); padding: 0 4px; }

/* 顶栏 */
.topbar {
  height: 54px; display: flex; align-items: center; gap: 20px;
  padding: 0 18px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; letter-spacing: .2px; white-space: nowrap; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: none; background: none; padding: 8px 12px; border-radius: 6px; color: var(--muted);
}
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.topright { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.chip {
  background: #f1f3f7; border-radius: 999px; padding: 3px 10px; font-size: 12px; color: var(--muted);
}

/* 视图 */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar { flex: none; }
.view { display: none; flex: 1; min-height: 0; }
.view.active { display: flex; }
#view-map { flex-direction: row; }
#view-map .sidebar {
  width: 320px; flex: none; background: var(--card); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.search-row { display: flex; gap: 6px; padding: 12px; border-bottom: 1px solid var(--line); }
.search-row input { flex: 1; }
.legend {
  display: flex; gap: 14px; padding: 8px 12px; font-size: 12px; color: var(--muted);
  border-bottom: 1px solid var(--line);
}
.legend .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }
.legend .dot.on { background: var(--accent); }
.legend .dot.stale { background: #94a3b8; }
.legend .dot.comm { background: #f08a1c; }
.traffic-toggle {
  display: flex; align-items: center; gap: 6px; padding: 6px 12px 8px;
  font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); cursor: pointer;
}
.traffic-toggle input { margin: 0; }
.traffic-toggle em { font-style: normal; margin-left: auto; font-size: 11px; color: #9aa3b0; }
.traffic-legend {
  background: #fff; border: 1px solid #e2e6ec; border-radius: 4px; padding: 6px 8px;
  font-size: 11px; line-height: 1.7; color: #374151; box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.traffic-legend b { display: block; margin-bottom: 2px; font-weight: 600; }
.traffic-legend i {
  display: inline-block; width: 14px; height: 3px; border-radius: 2px;
  margin: 0 4px 0 10px; vertical-align: middle;
}
.traffic-legend i:first-of-type { margin-left: 0; }
.office-list { flex: 1; overflow: auto; padding: 6px 0; }
.office { border-bottom: 1px solid #f0f2f6; }
.office-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer;
  font-size: 13px;
}
.office-head:hover { background: #f7f9fc; }
.office-head .count { margin-left: auto; color: var(--muted); font-size: 12px; }
.office-body { display: none; max-height: 260px; overflow: auto; }
.office.open .office-body { display: block; }
.veh-row {
  display: flex; gap: 8px; align-items: center; padding: 5px 12px 5px 30px;
  font-size: 12px; cursor: pointer; color: #374151;
}
.veh-row:hover { background: var(--accent-soft); }
.veh-row .no { font-family: ui-monospace, Consolas, monospace; }
.veh-row .st { margin-left: auto; width: 7px; height: 7px; border-radius: 50%; background: #cbd5e1; }
.veh-row .st.on { background: var(--ok); }
.veh-row .st.comm { background: #f08a1c; }
.veh-row .st.stale { background: #94a3b8; }
.side-foot { padding: 8px 12px; border-top: 1px solid var(--line); }
#map { flex: 1; }

/* 车辆标记 */
.veh-icon { background: none; border: none; }
.veh-icon svg { transform-origin: 50% 50%; filter: drop-shadow(0 1px 2px rgba(0,0,0,.25)); }

/* 工具条 */
.toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 12px 16px; background: var(--card); border-bottom: 1px solid var(--line);
  width: 100%;
}
.toolbar label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.veh-filter { width: 200px; }
#view-track, #view-clips, #view-machines, #view-devices, #view-stats { flex-direction: column; }
.split { display: flex; flex: 1; min-height: 0; }
#trackMap { flex: 1; min-width: 0; }
.table-wrap { flex: 1; overflow: auto; background: var(--card); }
.split .table-wrap { flex: 0 0 44%; border-left: 1px solid var(--line); }

table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid #eef1f5; white-space: nowrap; }
th { position: sticky; top: 0; background: #fafbfd; font-weight: 600; color: var(--muted); z-index: 1; }
tbody tr:hover { background: #f8fafd; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
#officeTable { width: auto; min-width: 620px; }
#officeTable th:first-child, #officeTable td:first-child { width: 320px; }

/* 列表分页条：粘在表格可视区底部，翻到哪一页都不用先滚到表尾 */
.pager {
  position: sticky; bottom: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 7px 12px; background: var(--card); border-top: 1px solid var(--line); font-size: 12px;
}
.pager:empty { display: none; }
.pager-ctl { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.pager-ctl label { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.pager select { width: auto; padding: 2px 6px; font-size: 12px; }
.pager-btn { padding: 3px 10px; font-size: 12px; line-height: 1.2; }
.pager-btn[disabled] { opacity: .45; cursor: not-allowed; }
.pager-num { min-width: 54px; text-align: center; font-variant-numeric: tabular-nums; }

/* 统计卡片 */
.cards { display: flex; gap: 12px; padding: 16px; flex-wrap: wrap; }
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 18px; min-width: 150px;
}
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 22px; font-weight: 600; margin-top: 4px; }
.stat .v small { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 4px; }

/* 弹窗 */
.modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .35);
  display: flex; align-items: center; justify-content: center; z-index: 5000;
}
.modal-card {
  background: var(--card); border-radius: 10px; width: min(760px, 92vw); max-height: 86vh;
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 18px 48px rgba(15,23,42,.24);
}
.modal-head {
  display: flex; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line);
  font-weight: 600;
}
.modal-head button { margin-left: auto; border: none; font-size: 18px; line-height: 1; }
#modalBody { padding: 16px; overflow: auto; }
#modalBody video { width: 100%; border-radius: 6px; background: #000; }
#modalBody .form-row { display: flex; gap: 10px; margin-bottom: 10px; }
#modalBody .form-row label { flex: 1; font-size: 12px; color: var(--muted); }
#modalBody .form-row input, #modalBody .form-row select { width: 100%; margin-top: 4px; }
.popup-actions { display: flex; gap: 6px; margin-top: 8px; }
.popup-actions button { font-size: 12px; padding: 3px 8px; }
.kv { display: grid; grid-template-columns: 90px 1fr; gap: 4px 10px; font-size: 13px; }
.kv .k { color: var(--muted); }

/* 查询反馈与窄屏布局 */
button:focus-visible, a:focus-visible, [role="button"]:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 3px;
}
body[data-role="viewer"] [data-admin] { display: none !important; }
.search-row input { min-width: 0; }
.toolbar select { max-width: min(440px, 65vw); }
.time-zone { flex-basis: 100%; }
.empty-state { padding: 30px 16px; color: var(--muted); text-align: center; white-space: normal; }
.veh-row.selected { background: var(--accent-soft); color: var(--accent); }
#map { min-width: 0; }
.kv { overflow-wrap: anywhere; }
.login-card { width: min(360px, calc(100vw - 32px)); }

@media (max-width: 1100px) {
  .topbar { height: auto; min-height: 54px; flex-wrap: wrap; gap: 8px 16px; padding: 10px 14px; }
  .tabs { order: 3; width: 100%; overflow-x: auto; flex-shrink: 0; }
  .tabs button { white-space: nowrap; }
  .topright #clock { display: none; }
}
@media (max-width: 680px) {
  .brand { font-size: 13px; }
  .topright { gap: 6px; }
  .chip { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #view-map { flex-direction: column; }
  #view-map .sidebar { width: 100%; height: 36%; min-height: 190px; border-right: 0; border-bottom: 1px solid var(--line); }
  #map { min-height: 200px; }
  .toolbar { gap: 8px; padding: 10px; }
  .toolbar label { flex-wrap: wrap; }
  .toolbar select { max-width: calc(100vw - 72px); }
  .split { flex-direction: column; }
  #trackMap { flex: 0 0 45%; min-height: 190px; }
  .split .table-wrap { flex: 1; border-left: 0; border-top: 1px solid var(--line); }
  #modalBody .form-row { flex-direction: column; }
  th, td { padding: 8px; }
  .cards { gap: 8px; padding: 10px; }
  .stat { min-width: 0; flex: 1 1 42%; padding: 12px; }
}
