:root {
  --panel: #ffffff;
  --ink: #17202a;
  --muted: #687789;
  --line: #dbe3ea;
  --soft: #f5f8fa;
  --blue: #1769e0;
  --cyan: #00a6b4;
  --green: #178957;
  --amber: #b56b00;
  --red: #c83f49;
  --selected: #e33f5f;
  --device: #1f7a6d;
  --shadow: 0 16px 42px rgba(18, 30, 44, 0.18);
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  font-family: Arial, "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: #e8eef2;
}

button,
input {
  font: inherit;
}

button,
.icon-link {
  border: 0;
  height: 36px;
  border-radius: 6px;
  background: var(--blue);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 12px;
  cursor: pointer;
  text-decoration: none;
}

button:hover,
.icon-link:hover { filter: brightness(0.96); }

svg { width: 18px; height: 18px; }

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #edf1f4;
}

.login-panel {
  width: min(380px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-panel h1 {
  margin: 0 0 22px;
  font-size: 26px;
}

.login-panel label {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
}

.login-panel input,
.search-box input,
.history-box input {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
}

.login-panel button { width: 100%; }
.form-error { color: var(--red); margin: -4px 0 14px; }

.map-app {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.map-canvas {
  position: absolute;
  inset: 0;
  background: #dfe8ee;
}

.map-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 6px;
  text-align: center;
  color: var(--muted);
}

.map-topbar {
  position: absolute;
  top: 14px;
  left: 14px;
  right: 430px;
  z-index: 20;
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid rgba(219, 227, 234, 0.92);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.6);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

.brand-block {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.brand-block strong {
  font-size: 18px;
}

.brand-block span {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.icon-button,
.icon-link {
  width: 36px;
  min-width: 36px;
  padding: 0;
  color: var(--ink);
  background: #f4f7f9;
  border: 1px solid var(--line);
}

.drawer {
  position: absolute;
  z-index: 30;
  top: 14px;
  right: 14px;
  bottom: 14px;
  width: min(400px, calc(100vw - 28px));
  display: grid;
  grid-template-rows: auto auto 1fr;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid rgba(219, 227, 234, 0.95);
  border-radius: 8px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  transform: translateX(calc(100% + 18px));
  transition: transform 180ms ease;
  overflow: hidden;
}

.drawer.open { transform: translateX(0); }

.drawer-head {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.drawer-head h1 {
  margin: 0;
  font-size: 20px;
}

.drawer-head p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 10px;
  border-bottom: 1px solid var(--line);
}

.tab {
  color: var(--ink);
  background: var(--soft);
  border: 1px solid var(--line);
}

.tab.active {
  color: #fff;
  background: var(--ink);
  border-color: var(--ink);
}

.tab-panel {
  min-height: 0;
  overflow: auto;
  display: none;
  padding: 12px;
}

.tab-panel.active {
  display: block;
}

.search-box {
  position: relative;
  display: block;
}

.search-box svg {
  position: absolute;
  left: 10px;
  top: 10px;
  color: var(--muted);
}

.search-box input {
  padding-left: 36px;
}

.device-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 10px 0;
}

.history-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.device-list {
  display: grid;
  gap: 8px;
}

.device-item {
  height: auto;
  min-height: 58px;
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  text-align: left;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.device-item.active {
  border-color: var(--selected);
  box-shadow: 0 0 0 2px rgba(227, 63, 95, 0.14);
}

.device-name {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-meta {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.state-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--muted);
  margin-top: 4px;
}

.state-dot.online { background: var(--green); }
.state-dot.warn { background: var(--amber); }

.status-section {
  display: grid;
  gap: 12px;
}

.hidden { display: none; }

.section-title {
  display: grid;
  gap: 4px;
  padding: 12px;
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.section-title h2 {
  margin: 0;
  font-size: 18px;
}

.section-title span {
  color: var(--muted);
  font-size: 13px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.metric {
  min-height: 72px;
  padding: 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.metric strong {
  font-size: 24px;
  overflow-wrap: anywhere;
}

.field-table {
  display: grid;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.field-title {
  padding: 10px 12px;
  font-weight: 700;
  background: var(--soft);
  border-bottom: 1px solid var(--line);
}

.field-row {
  display: grid;
  grid-template-columns: minmax(96px, 34%) minmax(0, 1fr);
  border-bottom: 1px solid var(--line);
}

.field-row:last-child { border-bottom: 0; }

.field-key,
.field-value {
  min-width: 0;
  padding: 9px 10px;
  overflow-wrap: anywhere;
}

.field-key {
  color: var(--muted);
  background: #fbfcfd;
  border-right: 1px solid var(--line);
}

.setting-row,
.history-box {
  display: grid;
  gap: 10px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.setting-row {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.setting-row span,
.history-box span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

.check-row input {
  width: 22px;
  height: 22px;
}

.history-box {
  grid-template-columns: 1fr;
}

.device-marker {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--device);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.96);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.05;
  text-align: center;
  padding: 2px;
  overflow: hidden;
}

.device-marker.selected {
  width: 48px;
  height: 48px;
  background: var(--selected);
  box-shadow: 0 0 0 5px rgba(227, 63, 95, 0.22), 0 10px 26px rgba(0, 0, 0, 0.32);
}

.device-marker.rtk-fixed {
  background: #ffc107;
  border-color: #0066cc;
  border-width: 3px;
  color: #c83f49;
}

.device-marker.rtk-fixed.selected {
  background: #ffc107;
  border-color: #0066cc;
  border-width: 3px;
  color: #c83f49;
  box-shadow: 0 0 0 5px rgba(255, 193, 7, 0.35), 0 10px 26px rgba(0, 0, 0, 0.32);
}

.device-marker.rtk-fixed-5 {
  background: #d4a017;
  border-color: #c83f49;
  border-width: 3px;
}

.device-marker.rtk-fixed-5.selected {
  background: #ffc107;
  border-color: #c83f49;
  border-width: 3px;
  box-shadow: 0 0 0 5px rgba(255, 193, 7, 0.35), 0 10px 26px rgba(0, 0, 0, 0.32);
}

.device-marker.fix-6 {
  background: #0066cc;
  color: #ffffff;
}

.device-marker.fix-6.selected {
  background: #0066cc;
  color: #ffffff;
  box-shadow: 0 0 0 5px rgba(0, 102, 204, 0.35), 0 10px 26px rgba(0, 0, 0, 0.32);
}

.device-marker.fix-9 {
  width: 360px;
  height: 360px;
  opacity: 0.1;
}

.device-marker.fix-9.selected {
  width: 480px;
  height: 480px;
  opacity: 0.1;
}

.track-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--cyan);
  border: 2px solid #fff;
}

.location-settings-box {
  display: grid;
  gap: 10px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.location-settings-box > strong {
  font-size: 14px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.check-row label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 13px;
}

.check-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
}

.keep-days-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.keep-days-row label {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.keep-days-row span {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

.keep-days-row input[type="number"] {
  flex: 1;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  font-size: 13px;
}

.logout-box {
  padding: 12px;
  margin-bottom: 10px;
}

.logout-link {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  border: 0;
  border-radius: 8px;
  background: #ff4d4f;
  color: #fff;
  text-decoration: none;
  cursor: pointer;
}

.logout-link:hover {
  background: #ff7875;
}

.command-box {
  display: grid;
  gap: 10px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.command-box > strong {
  font-size: 14px;
}

.voice-msg-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.voice-msg-row input {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  font-size: 13px;
}

.command-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.command-btn {
  height: 34px;
  font-size: 12px;
  padding: 0 6px;
  gap: 4px;
  color: var(--ink);
  background: var(--soft);
  border: 1px solid var(--line);
}

.command-btn.danger {
  color: var(--red);
  border-color: var(--red);
}

.rename-row {
  display: grid;
  gap: 8px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.rename-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.rename-input-row input {
  flex: 1;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  font-size: 13px;
}

#settingsMapType,
#saveDeviceName {
  color: #fff !important;
}

.geofence-box {
  display: grid;
  gap: 10px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.geofence-box > strong {
  font-size: 14px;
}

.geofence-draw-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.geofence-settings-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.geofence-settings-row input {
  flex: 1;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  font-size: 13px;
}

.fence-strategy-select {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  font-size: 13px;
  min-width: 120px;
}

.fence-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
  max-height: 200px;
  overflow-y: auto;
}

.fence-list-empty {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 16px;
}

.fence-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 10px;
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  align-items: center;
}

.fence-name {
  font-weight: 700;
  font-size: 13px;
}

.fence-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}

.fence-delete-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--red);
  cursor: pointer;
}

.fence-delete-btn:hover {
  background: rgba(200, 63, 73, 0.1);
}

.device-marker.out-of-bounds-gray {
  filter: grayscale(100%);
}

.device-marker.out-of-bounds-warning {
  background: #b56b00 !important;
  color: #fff !important;
  position: relative;
}

.device-marker.out-of-bounds-warning::after {
  content: "!";
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #b56b00;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: grid;
  place-items: center;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.device-marker.out-of-bounds-gray.selected,
.device-marker.out-of-bounds-warning.selected {
  box-shadow: 0 0 0 5px rgba(227, 63, 95, 0.22), 0 10px 26px rgba(0, 0, 0, 0.32);
}

@media (max-width: 900px) {
  .map-topbar {
    right: 14px;
    align-items: flex-start;
  }

  .brand-block strong {
    font-size: 16px;
  }

  .drawer {
    top: auto;
    height: min(72vh, 620px);
    width: calc(100vw - 20px);
    right: 10px;
    bottom: 10px;
  }
}
