:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #263244;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

/* Login */
.login-overlay {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #172554 0%, var(--bg) 60%);
}

.login-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.45);
}

.login-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #60a5fa;
  background: rgba(59, 130, 246, 0.14);
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 12px;
}

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

.login-card p {
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 20px;
  line-height: 1.45;
}

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}

input[type="password"],
.select-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
}

input[type="password"]:focus,
.select-input:focus {
  border-color: var(--primary);
}

.error-text {
  color: #f87171;
  font-size: 13px;
  margin-top: 10px;
}

/* App layout */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

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

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
}

.brand-dot.connected {
  background: var(--accent);
  box-shadow: 0 0 10px rgba(16, 185, 129, 0.7);
}

.brand-title {
  font-size: 15px;
  font-weight: 700;
}

.brand-sub {
  font-size: 12px;
  color: var(--muted);
}

.status-pills {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
}

.pill-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.pill-dot.online {
  background: var(--accent);
}

.pill-dot.offline {
  background: var(--danger);
}

.pill.gps-on {
  border-color: rgba(16, 185, 129, 0.5);
  background: rgba(16, 185, 129, 0.12);
  color: #6ee7b7;
}

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

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

.sidebar {
  width: 360px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}

.panel-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.section-header h2 {
  font-size: 14px;
  font-weight: 700;
}

.field-group {
  margin-bottom: 10px;
}

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}

.btn-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-full {
  width: 100%;
  margin-top: 14px;
}

.btn-sm {
  padding: 6px 11px;
  font-size: 12px;
}

.btn-xs {
  padding: 4px 8px;
  font-size: 11px;
}

.btn-primary {
  background: var(--primary);
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-accent {
  background: var(--accent);
  color: #052e16;
}

.btn-accent:hover {
  background: #059669;
  color: #ecfdf5;
}

.btn-secondary {
  background: var(--surface-2);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: #283548;
}

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

.btn-outline:hover {
  background: var(--surface-2);
}

.btn-warning {
  background: var(--warning);
  color: #1c1917;
}

.btn-warning:hover {
  background: #d97706;
  color: #fff;
}

.btn-danger {
  background: var(--danger);
}

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

.btn-danger:disabled {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--muted);
}

.gps-banner {
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 13px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 12px;
  border: 1px solid var(--border);
}

.gps-banner.idle {
  background: var(--surface-2);
  color: var(--muted);
}

.gps-banner.active {
  background: rgba(16, 185, 129, 0.14);
  border-color: rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  background: var(--surface-2);
}

.countdown-badge {
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(16, 185, 129, 0.16);
  color: #6ee7b7;
}

.status-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

.status-tag.active {
  background: rgba(245, 158, 11, 0.2);
  color: #fcd34d;
}

.helper-text {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.history-card {
  display: flex;
  flex-direction: column;
  min-height: 180px;
  flex: 1;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  max-height: 260px;
}

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  transition: border-color 0.15s ease;
}

.history-item:hover {
  border-color: var(--primary);
}

.history-top {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 3px;
}

.history-meta {
  font-size: 11px;
  color: var(--muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.history-links {
  display: flex;
  gap: 10px;
  margin-top: 5px;
}

.history-link {
  font-size: 11px;
  color: #60a5fa;
  text-decoration: none;
}

.history-link:hover {
  text-decoration: underline;
}

.popup-links {
  display: flex;
  gap: 10px;
  margin-top: 6px;
}

.popup-link {
  color: #2563eb;
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
}

.popup-link:hover {
  text-decoration: underline;
}

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

/* Map */
.map-container {
  position: relative;
  flex: 1;
  min-height: 360px;
}

#map {
  width: 100%;
  height: 100%;
  background: #0f172a;
}

.toast {
  position: absolute;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
  background: rgba(17, 24, 39, 0.95);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.45);
}

.device-switcher {
  display: flex;
  align-items: center;
  gap: 6px;
}

.device-select {
  min-width: 175px;
  padding: 6px 10px;
  font-size: 13px;
  background: var(--surface-2);
}

.text-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 13px;
  outline: none;
}

.text-input:focus {
  border-color: var(--primary);
}

.sentinel-box {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.sentinel-armed {
  background: rgba(16, 185, 129, 0.16) !important;
  color: #6ee7b7 !important;
}

.geofence-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.geofence-list,
.alert-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
  max-height: 160px;
  overflow-y: auto;
}

.geofence-item,
.alert-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 12px;
}

.geofence-item {
  cursor: pointer;
}

.geofence-item:hover {
  border-color: var(--accent);
}

.alert-item {
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
}

.alert-item.danger {
  border-color: rgba(239, 68, 68, 0.5);
  background: rgba(239, 68, 68, 0.1);
}

.alert-item.info {
  border-color: rgba(16, 185, 129, 0.45);
  background: rgba(16, 185, 129, 0.08);
}

.alert-time {
  font-size: 10px;
  color: var(--muted);
}

.empty-state-sm {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  padding: 8px 4px;
}

.pick-banner {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  background: rgba(37, 99, 235, 0.95);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.4);
}

.device-pin {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #3b82f6;
  border: 3px solid #ffffff;
  box-shadow: 0 0 0 6px rgba(59, 130, 246, 0.3);
}

.btn-view-active {
  background: rgba(59, 130, 246, 0.2) !important;
  border-color: var(--primary) !important;
  color: #93c5fd !important;
}

.multi-pin {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 3px solid #ffffff;
  box-shadow: 0 0 0 5px rgba(255, 255, 255, 0.22);
}

.device-map-label {
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.device-map-label::before {
  display: none;
}

.popup-select-btn {
  margin-top: 8px;
  width: 100%;
  padding: 5px 8px;
  border: none;
  border-radius: 6px;
  background: #2563eb;
  color: #ffffff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.popup-select-btn:hover {
  background: #1d4ed8;
}

@media (max-width: 900px) {
  .app-shell {
    height: auto;
    min-height: 100vh;
  }

  .main-layout {
    flex-direction: column-reverse;
  }

  .sidebar {
    width: 100%;
    border-right: none;
    border-top: 1px solid var(--border);
  }

  .map-container {
    height: 55vh;
  }
}
