* { box-sizing: border-box; }

:root {
  --sidebar-w: min(22rem, 92vw);
  --bg: #111;
  --surface: #1a1a1a;
  --surface-2: #141414;
  --border: #333;
  --text: #e8e8e8;
  --muted: #888;
  --accent: #3d5afe;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

#map {
  position: fixed;
  inset: 0;
  z-index: 0;
}

.map-sidebar-toggle {
  position: fixed;
  top: max(0.75rem, var(--safe-top));
  right: max(0.75rem, var(--safe-right));
  z-index: 1000;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

body.sidebar-desktop .map-sidebar-toggle,
body:not(.sidebar-mobile-closed) .map-sidebar-toggle {
  display: none;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.icon-btn:active { background: #252525; }

.btn-text {
  margin: 0;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.875rem;
  cursor: pointer;
  white-space: nowrap;
}

.btn-text:active { background: #252525; }

.sidebar {
  position: fixed;
  top: 0;
  right: 0;
  left: auto;
  bottom: 0;
  z-index: 1050;
  width: var(--sidebar-w);
  max-width: 100%;
  display: flex;
  flex-direction: column;
  background: rgba(26, 26, 26, 0.97);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-left: 1px solid var(--border);
  transform: translateX(0);
  transition: transform 0.25s ease;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.sidebar-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}

.sidebar-close {
  display: none;
}

.sidebar-scroll {
  flex: 1;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 0.75rem 1rem 1rem;
  -webkit-overflow-scrolling: touch;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1040;
  background: rgba(0, 0, 0, 0.45);
}

body.sidebar-mobile-closed .sidebar {
  transform: translateX(100%);
}

body.sidebar-desktop .sidebar-backdrop {
  display: none !important;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(0.75rem, var(--safe-top)) max(0.75rem, var(--safe-right)) max(0.75rem, var(--safe-bottom)) max(0.75rem, var(--safe-left));
}

.overlay[hidden] { display: none; }

.overlay-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.overlay-panel {
  position: relative;
  z-index: 1;
  width: min(42rem, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.overlay-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.overlay-body {
  overflow: auto;
  overscroll-behavior: contain;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  -webkit-overflow-scrolling: touch;
}

.panel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
}

.panel h3 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
}

.section-title {
  margin: 1rem 0 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

label { display: block; margin: 0.5rem 0; font-size: 0.9rem; }

input, textarea, button { font: inherit; }

input, textarea {
  width: 100%;
  background: #0f0f0f;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.65rem;
  font-size: 16px;
}

textarea { resize: vertical; min-height: 4rem; }

button {
  margin-top: 0.5rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0.55rem 1rem;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled { opacity: 0.55; cursor: not-allowed; }

.hint { color: var(--muted); font-size: 0.85rem; line-height: 1.45; }

.status { display: block; margin-top: 0.35rem; font-size: 0.85rem; color: #81c784; }

#location-list, #device-list, #device-map-list { list-style: none; padding: 0; margin: 0; }

#location-list li, #device-list li {
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  margin-right: 0.25rem;
  font-weight: 600;
}

.badge.google { background: #1b5e20; }
.badge.apple { background: #4a148c; }
.badge.semantic { background: #e65100; }

.muted { color: var(--muted); font-size: 0.85rem; }

.poll-banner { margin: 0 0 0.75rem; }

.poll-banner-track {
  position: relative;
  height: 8px;
  border-radius: 999px;
  overflow: hidden;
  background: #2a2a2a;
  border: 1px solid var(--border);
}

.poll-banner-fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #1565c0, #42a5f5);
  transition: width 0.8s linear;
}

.poll-banner-wave {
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.05) 15%,
    rgba(255, 255, 255, 0.45) 50%,
    rgba(255, 255, 255, 0.05) 85%,
    transparent 100%
  );
  background-size: 200% 100%;
}

.poll-banner-label {
  margin: 0.35rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.35;
}

.poll-banner.is-live .poll-banner-wave {
  opacity: 1;
  animation: poll-wave 1.8s linear infinite;
}

.poll-banner.is-busy .poll-banner-fill {
  width: 100% !important;
  background: linear-gradient(90deg, #1565c0, #64b5f6, #1565c0);
  background-size: 200% 100%;
  animation: poll-busy 1.2s linear infinite;
}

.poll-banner.is-fresh .poll-banner-fill {
  background: linear-gradient(90deg, #2e7d32, #66bb6a);
}

.poll-banner.is-error .poll-banner-track { border-color: #c62828; }

.poll-banner.is-error .poll-banner-fill {
  background: #c62828;
  width: 100%;
}

@keyframes poll-wave {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

@keyframes poll-busy {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

.selected-point {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.selected-point-summary {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
  color: #ccc;
}

.maps-link {
  display: inline-block;
  font-size: 0.85rem;
  color: #64b5f6;
  text-decoration: none;
}

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

#location-list li.selectable { cursor: pointer; }
#location-list li.selectable:active { background: #222; }

#location-list li.selected-point-row {
  background: #263238;
  border-left: 3px solid #64b5f6;
  padding-left: 0.35rem;
}

.device-row {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 0.9rem;
}

.device-row:active { background: #222; }

.device-row.selected {
  background: #263238;
  border-left: 3px solid var(--accent);
  padding-left: 0.5rem;
  cursor: default;
}

.device-detail {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin-bottom: 0.5rem;
}

.device-detail h3 { margin: 0 0 0.5rem; font-size: 1rem; }

.range-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0.5rem 0;
}

.range-presets button {
  margin: 0;
  padding: 0.35rem 0.6rem;
  background: #2a2a2a;
  font-size: 0.85rem;
}

.range-field { display: block; margin: 0.35rem 0; font-size: 0.85rem; }

.network-filters {
  display: flex;
  gap: 1rem;
  margin: 0.5rem 0;
  font-size: 0.85rem;
}

.network-filters label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0;
}

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin: 0.5rem 0;
  font-size: 0.9rem;
}

.checkbox-row input { width: auto; margin-top: 0.2rem; }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.btn-row button { margin-top: 0.35rem; flex: 1; min-width: 7rem; }

.export-row { margin: 0.5rem 0 0.75rem; }

.report-time { color: var(--muted); font-size: 0.78rem; }
.stale-hint { color: #ffb74d; font-size: 0.78rem; }
.poll-hint { color: #78909c; font-size: 0.78rem; }

button.secondary { background: #444; }
button.danger { background: #b71c1c; width: 100%; margin-left: 0; }

#device-list button {
  margin-top: 0.35rem;
  margin-right: 0.25rem;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
}

#device-list button.danger { width: auto; }

.share-history-fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  margin: 0.75rem 0;
  padding: 0.5rem 0.75rem;
}

.share-history-fieldset legend {
  font-size: 0.85rem;
  color: var(--muted);
  padding: 0 0.25rem;
}

#share-links-list { list-style: none; padding: 0; margin: 0; }

#share-links-list li {
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

#share-links-list button {
  margin-top: 0.35rem;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
}

#share-created-url { font-size: 0.85rem; }

.share-error {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: var(--bg);
  text-align: center;
}

.share-error[hidden] { display: none; }

.share-expiry { margin: 0 0 0.75rem; font-size: 0.8rem; }

.share-alerts-panel {
  margin: 0;
  padding-top: 0;
  border-top: none;
}

.share-alerts-panel .hint { font-size: 0.82rem; margin: 0.35rem 0 0.75rem; }

.share-alerts-panel input[type="number"],
.share-alerts-panel input[type="text"] {
  width: 100%;
  margin-top: 0.25rem;
}

#share-geofence-list {
  list-style: none;
  padding: 0;
  margin: 0.75rem 0;
}

#share-geofence-list li {
  margin-bottom: 0.75rem;
  font-size: 0.88rem;
}

.geofence-row {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
}

.geofence-row strong { display: block; margin-bottom: 0.25rem; }

.geofence-meta {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 0.35rem;
}

.geofence-alerts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin: 0.35rem 0;
  font-size: 0.82rem;
}

.geofence-alerts label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0;
}

.fence-shape-tabs {
  display: flex;
  gap: 0.35rem;
  margin: 0.5rem 0;
}

.fence-shape-tab {
  flex: 1;
  margin: 0;
  padding: 0.4rem 0.5rem;
  background: #2a2a2a;
  font-size: 0.82rem;
}

.fence-shape-tab.active {
  background: var(--accent);
}

.fence-alert-options {
  margin: 0.5rem 0;
}

#share-geofence-list button {
  margin-left: 0.35rem;
  font-size: 0.78rem;
  padding: 0.15rem 0.45rem;
}

.health-dl {
  margin: 0;
  display: grid;
  grid-template-columns: minmax(8rem, 40%) 1fr;
  gap: 0.35rem 0.75rem;
  font-size: 0.88rem;
}

.health-dl dt { margin: 0; color: var(--muted); }
.health-dl dd { margin: 0; word-break: break-word; }

.health-badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 600;
}

.health-badge.ok { background: #1b5e20; color: #c8e6c9; }
.health-badge.warn { background: #e65100; color: #ffe0b2; }
.health-badge.bad { background: #b71c1c; color: #ffcdd2; }

.health-issues {
  margin: 0 0 1rem;
  padding: 0.65rem 0.75rem;
  border-radius: 6px;
  background: #3e2723;
  color: #ffccbc;
  font-size: 0.88rem;
}

.health-issues ul { margin: 0.35rem 0 0; padding-left: 1.2rem; }

.health-list {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 0.88rem;
}

.health-list li {
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
}

.health-list li:last-child { border-bottom: none; }

.share-view #share-latest { margin-bottom: 0.75rem; font-size: 0.9rem; }

.share-device-btn { width: 100%; margin-top: 0.5rem; }

.sidebar-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-tab {
  flex: 1;
  margin: 0;
  padding: 0.55rem 0.35rem;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.sidebar-tab:hover:not(:disabled) { color: var(--text); background: #222; }

.sidebar-tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
  background: rgba(61, 90, 254, 0.08);
}

.sidebar-tab:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.sidebar-tab-panel[hidden] { display: none; }

.sticky-range {
  position: sticky;
  top: 0;
  z-index: 2;
  background: rgba(26, 26, 26, 0.97);
  padding: 0.25rem 0;
  margin-bottom: 0.25rem;
}

.back-link {
  margin: 0 0 0.5rem;
  padding: 0;
  border: none;
  background: none;
  color: #64b5f6;
  font-size: 0.85rem;
  cursor: pointer;
}

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

.device-last-seen { margin: 0 0 0.5rem; font-size: 0.82rem; }

.network-legend { margin: 0 0 0.75rem; font-size: 0.78rem; }

.manage-panel {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.manage-panel .panel { margin-top: 0.5rem; }

.settings-layout {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0 !important;
  overflow: hidden;
}

.settings-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  padding: 0 0.5rem;
}

.settings-nav-btn {
  margin: 0;
  padding: 0.65rem 0.75rem;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.settings-nav-btn.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.settings-content {
  overflow: auto;
  padding: 1rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.settings-section[hidden] { display: none; }

.settings-btn {
  position: relative;
}

.health-status-dot {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 1px solid var(--surface);
}

.health-status-dot.ok { background: #66bb6a; }
.health-status-dot.warn { background: #ffb74d; }
.health-status-dot.bad { background: #ef5350; }

.map-chips {
  position: fixed;
  bottom: max(0.75rem, var(--safe-bottom));
  left: max(0.75rem, var(--safe-left));
  z-index: 1000;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.map-chip {
  margin: 0;
  padding: 0.4rem 0.65rem;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: 999px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

.map-chip.secondary { background: rgba(68, 68, 68, 0.95); }

.map-status-chip {
  position: fixed;
  top: max(0.75rem, var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  background: rgba(26, 26, 26, 0.92);
  border: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--muted);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  max-width: calc(100% - 2rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.map-mode-banner {
  position: fixed;
  top: max(3rem, calc(var(--safe-top) + 2rem));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  background: rgba(255, 152, 0, 0.95);
  color: #111;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

.now-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
}

.now-updated { margin: 0 0 0.35rem; font-size: 0.82rem; }

.sheet-handle {
  display: none;
  width: 100%;
  margin: 0;
  padding: 0.5rem 0 0.35rem;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
}

.sheet-handle-bar {
  display: block;
  width: 2.5rem;
  height: 4px;
  margin: 0 auto 0.35rem;
  border-radius: 999px;
  background: #555;
}

.sheet-handle-label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.leaflet-control-zoom a {
  width: 2rem !important;
  height: 2rem !important;
  line-height: 2rem !important;
}

@media (min-width: 900px) {
  body {
    --sidebar-w: 22rem;
  }

  body.sidebar-desktop .sidebar {
    transform: translateX(0);
  }
}

@media (max-width: 899px) {
  body.sidebar-mobile-closed .sidebar-close {
    display: none;
  }

  body:not(.sidebar-mobile-closed) .sidebar-close {
    display: inline-flex;
  }

  body.share-view .sheet-handle {
    display: block;
  }

  body.share-view .sidebar-bottom-sheet {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    height: auto;
    max-height: 38vh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 12px 12px 0 0;
    transform: translateY(calc(100% - 2.75rem));
    transition: transform 0.28s ease, max-height 0.28s ease;
  }

  body.share-view.sidebar-expanded .sidebar-bottom-sheet {
    max-height: 70vh;
    transform: translateY(0);
  }

  body.share-view.sidebar-expanded .map-sidebar-toggle {
    display: none;
  }

  body.share-view.sidebar-peek:not(.sidebar-expanded) .sidebar-close {
    display: none;
  }

  body.share-view .map-chips {
    bottom: max(3.5rem, calc(var(--safe-bottom) + 2.5rem));
  }

  .overlay-panel {
    width: 100%;
    max-height: 92vh;
    border-radius: 12px 12px 0 0;
    align-self: flex-end;
  }

  .overlay {
    align-items: flex-end;
    padding: 0;
  }

  .overlay-body {
    max-height: calc(92vh - 3.5rem);
  }
}

@media (max-width: 480px) {
  .btn-text { padding: 0.4rem 0.55rem; font-size: 0.8rem; }
  .sidebar-title { font-size: 0.9rem; }
}
