:root {
  --bg: #f1f5f9;
  --bg-subtle: #e8eef5;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: rgba(37, 99, 235, 0.09);
  --accent-text: #ffffff;
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, 0.08);
  --ok: #16a34a;
  --warn: #d97706;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 10px 25px -5px rgba(15, 23, 42, 0.1), 0 8px 10px -6px rgba(15, 23, 42, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1017;
    --bg-subtle: #111824;
    --card: #151c27;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --border: #243041;
    --accent: #3b82f6;
    --accent-hover: #60a5fa;
    --accent-soft: rgba(59, 130, 246, 0.14);
    --danger-soft: rgba(220, 38, 38, 0.14);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden, [hidden] { display: none !important; }

h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 4px; }
h2 {
  font-size: 12px;
  font-weight: 700;
  margin: 0 0 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.muted { color: var(--muted); }
.small { font-size: 12px; line-height: 1.4; }
.error { color: var(--danger); min-height: 1.2em; font-size: 13px; font-weight: 500; }

input, select {
  width: 100%;
  padding: 9px 11px;
  margin-top: 5px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0 8px 0 0;
  accent-color: var(--accent);
  cursor: pointer;
}
label { display: block; margin-bottom: 11px; font-size: 13px; font-weight: 500; }
label.inline { display: inline-flex; align-items: center; margin: 0; cursor: pointer; font-weight: 500; }

button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  transition: all 150ms ease;
}
button:hover:not(:disabled), .button:hover {
  border-color: var(--accent);
  background: var(--bg);
}
button:active:not(:disabled) { transform: translateY(0.5px); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
}
.primary:hover:not(:disabled), .button.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.danger {
  color: var(--danger);
  border-color: rgba(220, 38, 38, 0.35);
  background: transparent;
  font-weight: 600;
}
.danger:hover:not(:disabled) {
  background: var(--danger-soft);
  border-color: var(--danger);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

/* Login */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at top center, var(--accent-soft), transparent 60%), var(--bg);
}
.login-card {
  width: 100%;
  max-width: 380px;
  padding: 28px 24px;
  box-shadow: var(--shadow-lg);
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}
.login-brand .brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 18px;
}
.login-card button[type="submit"] { width: 100%; margin-top: 6px; padding: 11px; }

/* App Shell */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 18px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 10;
}
.brand-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.topbar-right { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.topbar-right button, .topbar-right .button { padding: 7px 12px; font-size: 13px; }
.refresh-pill {
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 13px;
}

.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 400px;
  overflow-y: auto;
  padding: 14px;
  border-right: 1px solid var(--border);
  background: var(--bg);
}
.sidebar::-webkit-scrollbar, .points::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb, .points::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}
.map-wrap { flex: 1; min-width: 0; position: relative; }
#map { height: 100%; width: 100%; }

.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.row > * { flex: 1; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid-2 label { margin-bottom: 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chips button {
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--bg);
}
.chips button.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

/* Device Cards */
.device-list { display: flex; flex-direction: column; gap: 8px; }
.device {
  text-align: left;
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  padding: 11px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: border-color 150ms ease, background 150ms ease;
}
.device:hover { border-color: var(--accent); }
.device.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}
.device-info { flex: 1; min-width: 0; }
.device-name { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.device-right { display: flex; align-items: center; gap: 8px; flex: none; }
.pin-badge {
  font-size: 10.5px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(217, 119, 6, 0.16);
  color: #b45309;
  border: 1px solid rgba(217, 119, 6, 0.35);
}
@media (prefers-color-scheme: dark) {
  .pin-badge { color: #fbbf24; }
}
.pin-btn {
  padding: 4px 7px;
  font-size: 13px;
  border-radius: 7px;
  background: transparent;
  border: 1px solid transparent;
  opacity: 0.45;
  transition: all 150ms ease;
}
.pin-btn:hover { opacity: 1; background: var(--card); border-color: var(--border); }
.pin-btn.pinned {
  opacity: 1;
  background: rgba(217, 119, 6, 0.15);
  border-color: rgba(217, 119, 6, 0.45);
}
.device .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.06);
}
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.18); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 3px rgba(217, 119, 6, 0.18); }
.dot.bad { background: var(--danger); box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.18); }

/* Key-Value Summary Grid */
.kv {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 14px;
  font-size: 13px;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-top: 4px;
}
.kv span:nth-child(odd) { color: var(--muted); font-weight: 500; }
.kv span:nth-child(even) { font-weight: 600; text-align: right; }

/* History Points */
.points {
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-top: 10px;
  background: var(--bg);
}
.point {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  padding: 8px 10px;
  font-size: 12px;
  background: transparent;
  justify-content: flex-start;
}
.point:last-child { border-bottom: 0; }
.point:hover { background: var(--card); border-color: var(--border); }
.acc-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex: none;
  border: 1.5px solid #fff;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.25);
}

.danger-zone { border-color: rgba(220, 38, 38, 0.25); }
.danger-zone button { width: 100%; margin-top: 6px; }
.divider { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
#remove-device-msg { min-height: 1em; margin: 6px 0 0; }
#settings-msg { min-height: 1em; margin: 8px 0 0; font-weight: 500; }

/* Map Overlays */
.leaflet-container { background: var(--bg); font: inherit; }
.route-start-label, .route-end-label {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  white-space: nowrap;
  color: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  border: 1.5px solid #fff;
}
.route-start-label { background: #16a34a; }
.route-end-label { background: #dc2626; }

.map-legend {
  position: absolute;
  bottom: 24px;
  left: 12px;
  z-index: 500;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  padding: 7px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  pointer-events: none;
}
.leg-item { display: inline-flex; align-items: center; gap: 5px; }
.leg-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1.5px solid #fff;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.35);
}
.leg-dot.start { background: #16a34a; }
.leg-dot.acc-high { background: #1e3a8a; }
.leg-dot.acc-low { background: #93c5fd; }
.leg-dot.end { background: #dc2626; }

.pin { position: relative; cursor: pointer; }
.pin::before {
  content: '';
  position: absolute;
  inset: 10%;
  background: var(--pin-color, #2563eb);
  border: 2px solid #fff;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  transform-origin: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}
.pin::after {
  content: '';
  position: absolute;
  left: 50%; top: 32%;
  width: 28%; height: 28%;
  background: #fff;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

@media (max-width: 860px) {
  .layout { flex-direction: column-reverse; }
  .sidebar { width: 100%; border-right: 0; padding: 12px; overflow: visible; }
  .map-wrap { height: 52vh; flex: none; }
  .app { height: auto; }
  .map-legend { bottom: 10px; left: 8px; right: 8px; border-radius: 12px; justify-content: center; }
}

.locate-btn { width: 100%; padding: 11px; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.locate-btn:disabled { opacity: 0.6; cursor: progress; }
#locate-msg { min-height: 1em; margin: 4px 0 8px; font-weight: 500; }

/* Recording & Screen Check Section */
.rec-btn-row { display: flex; gap: 8px; margin-top: 6px; }
#recording-start {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
  flex: 1;
}
#recording-start:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
#recording-start.recording-active {
  background: #dc2626;
  border-color: #dc2626;
  animation: pulse 1.2s infinite;
}
#recording-start.recording-active:hover:not(:disabled) {
  background: #b91c1c;
  border-color: #b91c1c;
}
#recording-start[disabled] {
  background: var(--border);
  border-color: var(--border);
  color: var(--muted);
  cursor: not-allowed;
  animation: none;
}
#screen-check-btn { font-weight: 600; white-space: nowrap; flex: none; }

.screen-pill {
  margin-top: 10px;
  padding: 8px 11px;
  border-radius: 9px;
  font-size: 12.5px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--bg);
}
.screen-pill.on { background: rgba(22, 163, 74, 0.12); border-color: rgba(22, 163, 74, 0.4); color: #15803d; }
.screen-pill.lock { background: rgba(217, 119, 6, 0.12); border-color: rgba(217, 119, 6, 0.4); color: #b45309; }
.screen-pill.off { background: rgba(100, 116, 139, 0.12); border-color: rgba(100, 116, 139, 0.3); color: var(--muted); }
.screen-pill.pending { background: var(--accent-soft); border-color: rgba(37, 99, 235, 0.35); color: var(--accent); }
@media (prefers-color-scheme: dark) {
  .screen-pill.on { color: #4ade80; }
  .screen-pill.lock { color: #fbbf24; }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.78; }
}
#recording-status { min-height: 1.4em; margin: 8px 0 0; font-weight: 500; }

.audio-box {
  margin-top: 10px;
  padding: 10px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
}
#recording-list {
  margin-top: 10px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 44px;
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.rec-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 12px;
}
.rec-info { flex: 1; min-width: 0; }
.rec-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec-meta { color: var(--muted); font-size: 11px; margin-top: 2px; }
.rec-actions { display: flex; gap: 4px; flex-shrink: 0; }
.rec-btn { padding: 5px 9px; font-size: 12px; border-radius: 7px; text-decoration: none; cursor: pointer; }
.rec-btn.danger { background: #dc2626; border-color: #dc2626; color: #fff; font-weight: 600; }
.rec-btn.danger:hover:not(:disabled) { background: #b91c1c; border-color: #b91c1c; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  padding: 16px;
}
.modal-card {
  width: 100%;
  max-width: 380px;
  padding: 20px;
  box-shadow: var(--shadow-lg);
}
.rec-wake-option {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(59, 130, 246, 0.08);
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
}
.rec-wake-option input { width: 16px; height: 16px; margin: 0; flex-shrink: 0; accent-color: var(--accent); }
