:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --text: #161a20;
  --muted: #6b7280;
  --border: #e2e5ea;
  --hover: #f3f5f8;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --accent-weak: #eff4ff;
  --danger: #dc2626;
  --danger-weak: #fef2f2;
  --ok: #16a34a;
  --warn: #d97706;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --panel: #151a21;
    --panel-2: #11161c;
    --text: #e7eaf0;
    --muted: #9aa3b2;
    --border: #252c37;
    --hover: #1b222c;
    --accent: #6b93ff;
    --accent-strong: #8aa9ff;
    --accent-weak: #18223a;
    --danger: #f87171;
    --danger-weak: #2a1517;
    --ok: #4ade80;
    --warn: #fbbf24;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.25;
}

.muted {
  color: var(--muted);
  margin: 0;
  font-size: 13px;
}

.error {
  color: var(--danger);
  background: var(--danger-weak);
  border-radius: 8px;
  padding: 8px 10px;
  margin: 4px 0 0;
  font-size: 13px;
}

/* ---------- 登录 ---------- */

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-card h1 {
  font-size: 22px;
}

.login-card .muted {
  margin-bottom: 4px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 20px;
  letter-spacing: -0.02em;
}

.brand-mark.small {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  font-size: 16px;
}

/* ---------- 表单控件 ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field > span {
  font-size: 12px;
  color: var(--muted);
}

input[type="password"],
input[type="search"],
input[type="datetime-local"],
select,
input[type="text"] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 11px;
  outline: none;
  min-width: 0;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-weak);
}

button,
.link {
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 8px 14px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
}

button:hover,
.link:hover {
  background: var(--hover);
}

button:disabled {
  opacity: 0.55;
  cursor: default;
}

button:focus-visible,
.link:focus-visible,
input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.primary:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

@media (prefers-color-scheme: dark) {
  .primary {
    color: #0b1020;
  }
}

.ghost {
  background: transparent;
}

.danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}

.danger:hover {
  background: var(--danger-weak);
}

.wide {
  width: 100%;
}

.icon {
  border: none;
  background: transparent;
  font-size: 20px;
  line-height: 1;
  padding: 4px 8px;
  color: var(--muted);
}

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

.app {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

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

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.brand-text strong {
  font-size: 15px;
}

.brand-text small {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.08em;
}

.device-field select {
  width: 100%;
  font-size: 13px;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav button {
  justify-content: flex-start;
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 9px 12px;
  font-weight: 500;
}

.nav button:hover {
  background: var(--hover);
  color: var(--text);
}

.nav button.active {
  background: var(--accent-weak);
  color: var(--accent);
}

.sidebar-foot {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-foot .link,
.sidebar-foot button {
  font-size: 12px;
  padding: 7px 10px;
  color: var(--muted);
}

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

.view {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 22px 24px 18px;
  gap: 14px;
}

.view-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.view-head h2 {
  font-size: 20px;
  margin-bottom: 2px;
}

.head-actions {
  display: flex;
  gap: 8px;
}

/* ---------- 筛选 ---------- */

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.filters input[type="search"] {
  flex: 1 1 220px;
  max-width: 340px;
}

.date-field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.date-field input {
  font-size: 13px;
  padding: 7px 9px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}

.presets {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 9px;
  overflow: hidden;
}

.presets button {
  border: none;
  border-radius: 0;
  background: transparent;
  font-size: 13px;
  padding: 7px 11px;
  color: var(--muted);
}

.presets button + button {
  border-left: 1px solid var(--border);
}

.presets button:hover {
  background: var(--hover);
  color: var(--text);
}

/* ---------- 短信列表 ---------- */

.list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.sms-list {
  display: flex;
  flex-direction: column;
}

.sms-item {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  outline: none;
}

.sms-item:last-child {
  border-bottom: none;
}

.sms-item:hover,
.sms-item:focus-visible {
  background: var(--hover);
}

.sms-who {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.sms-who strong {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sms-who small {
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sms-preview {
  color: var(--text);
  opacity: 0.86;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}

.sms-item time {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.list-foot {
  display: flex;
  justify-content: center;
}

.empty {
  padding: 48px 20px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

/* ---------- 轨迹 ---------- */

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--muted);
}

.stat strong {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.map {
  flex: 1;
  min-height: 320px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel-2);
  z-index: 0;
}

.leaflet-container {
  font-family: inherit;
  background: var(--panel-2);
}

/* ---------- 设备 ---------- */

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
  align-content: start;
  overflow-y: auto;
  min-height: 0;
  padding-bottom: 4px;
}

.device-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.device-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.device-card-head strong {
  font-size: 15px;
}

.device-id {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  word-break: break-all;
}

.device-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.device-actions {
  display: flex;
  gap: 6px;
  margin-top: 2px;
}

.device-actions button {
  font-size: 12px;
  padding: 6px 10px;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  background: var(--accent-weak);
  color: var(--accent);
  font-size: 11px;
  padding: 2px 8px;
  white-space: nowrap;
}

.badge.warn {
  background: rgba(217, 119, 6, 0.12);
  color: var(--warn);
}

/* ---------- 弹窗 ---------- */

.dialog {
  width: min(620px, calc(100vw - 32px));
  max-height: min(80vh, 760px);
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  box-shadow: var(--shadow);
}

.dialog::backdrop {
  background: rgba(10, 14, 20, 0.5);
  backdrop-filter: blur(2px);
}

.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.dialog-title {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.dialog-title h3 {
  font-size: 18px;
  word-break: break-all;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  padding: 12px 20px;
  color: var(--muted);
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}

.sms-body {
  margin: 0;
  padding: 16px 20px;
  max-height: 46vh;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.65;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 20px 18px;
  border-top: 1px solid var(--border);
}

.dialog-actions button {
  font-size: 13px;
  padding: 7px 12px;
}

/* ---------- 提示 ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: rgba(20, 24, 31, 0.92);
  color: #fff;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 1000;
  max-width: calc(100vw - 32px);
}

/* ---------- 窄屏 ---------- */

@media (max-width: 820px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    height: 100dvh;
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 12px;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
  }

  .brand-text {
    display: none;
  }

  .device-field {
    flex: 1 1 160px;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }

  .device-field > span {
    display: none;
  }

  .device-field select {
    width: 100%;
  }

  .nav {
    flex-direction: row;
    order: 3;
    width: 100%;
    gap: 2px;
  }

  .nav button {
    flex: 1;
    justify-content: center;
    padding: 7px 8px;
  }

  .sidebar-foot {
    margin: 0;
    flex-direction: row;
    gap: 2px;
  }

  .sidebar-foot .link {
    display: none;
  }

  .sidebar-foot button {
    padding: 6px 8px;
  }

  .view {
    padding: 14px 12px 12px;
    gap: 10px;
  }

  .view-head h2 {
    font-size: 18px;
  }

  .filters input[type="search"] {
    max-width: none;
    flex-basis: 100%;
  }

  .date-field {
    flex: 1 1 150px;
  }

  .date-field input {
    width: 100%;
  }

  .sms-item {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 4px 12px;
    padding: 12px 14px;
  }

  .sms-who {
    grid-column: 1;
  }

  .sms-item time {
    grid-column: 2;
    grid-row: 1;
  }

  .sms-preview {
    grid-column: 1 / -1;
    grid-row: 2;
    font-size: 13px;
  }

  .map {
    min-height: 52vh;
  }

  .stats {
    gap: 5px;
  }

  .stat {
    padding: 4px 9px;
    font-size: 11px;
  }

  .presets button {
    padding: 7px 9px;
    font-size: 12px;
  }
}
