:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #d8dee8;
  --person: #2563eb;
  --animal: #138a5b;
  --vehicle: #c55f18;
  --unknown: #68758a;
  --fresh: #0f8a4b;
  --stale: #b54708;
  --shadow: 0 18px 45px rgba(23, 32, 51, 0.18);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

body.login-page {
  overflow: auto;
}

button {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  height: 100%;
  min-height: 100%;
}

.app-shell.is-sidebar-collapsed {
  grid-template-columns: minmax(0, 1fr) 0;
}

#map {
  position: relative;
  min-width: 0;
  height: 100%;
  overflow: hidden;
  background: #dde5ef;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

#map.is-dragging {
  cursor: grabbing;
}

.tile-pane,
.marker-pane {
  position: absolute;
  inset: 0;
}

.tile-pane {
  z-index: 1;
}

.marker-pane {
  z-index: 5;
  pointer-events: none;
}

.map-tile {
  position: absolute;
  top: 0;
  left: 0;
  width: 256px;
  max-width: none;
  height: 256px;
  background: #dce5ee;
  pointer-events: none;
  user-select: none;
}

.map-tile.is-missing {
  opacity: 0;
}

.map-controls {
  position: absolute;
  z-index: 20;
  top: 12px;
  left: 12px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid rgba(23, 32, 51, 0.22);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 8px 24px rgba(23, 32, 51, 0.18);
}

.map-control-button {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-bottom: 1px solid var(--line);
  color: var(--text);
  background: #ffffff;
  cursor: pointer;
  font-size: 18px;
  font-weight: 800;
  line-height: 1;
}

.map-control-button:last-child {
  border-bottom: 0;
  font-size: 13px;
}

.map-control-button:hover,
.map-control-button:focus-visible {
  background: #eef2f7;
  outline: none;
}

.map-attribution {
  position: absolute;
  right: 8px;
  bottom: 6px;
  z-index: 12;
  max-width: calc(100% - 16px);
  padding: 3px 6px;
  background: rgba(255, 255, 255, 0.86);
  color: #344054;
  font-size: 11px;
  line-height: 1.3;
}

.map-attribution a {
  color: #1d4ed8;
}

.map-popup {
  position: absolute;
  z-index: 18;
  min-width: 190px;
  max-width: min(270px, calc(100% - 24px));
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 10px 30px rgba(23, 32, 51, 0.22);
  transform: translate(-50%, -100%);
}

.map-popup::after {
  position: absolute;
  bottom: -7px;
  left: 50%;
  width: 12px;
  height: 12px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #ffffff;
  content: "";
  transform: translateX(-50%) rotate(45deg);
}

.tracker-panel {
  z-index: 500;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  padding: 18px;
  border-left: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.app-shell.is-sidebar-collapsed .tracker-panel {
  overflow: hidden;
  width: 0;
  padding: 0;
  border-left: 0;
  box-shadow: none;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.panel-actions {
  display: flex;
  align-items: flex-start;
  gap: 7px;
}

.icon-button,
.sidebar-fab {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: #ffffff;
  cursor: pointer;
  font-size: 18px;
  font-weight: 900;
  line-height: 1;
}

.icon-button:hover,
.icon-button:focus-visible,
.sidebar-fab:hover,
.sidebar-fab:focus-visible {
  background: #edf2f7;
  outline: none;
}

.sidebar-fab {
  position: absolute;
  z-index: 650;
  top: 12px;
  right: 12px;
  box-shadow: 0 8px 24px rgba(23, 32, 51, 0.18);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.15;
}

.status-pill {
  flex: 0 0 auto;
  min-width: 78px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-align: center;
}

.status-pill.is-online {
  border-color: rgba(15, 138, 75, 0.25);
  color: var(--fresh);
  background: rgba(15, 138, 75, 0.08);
}

.status-pill.is-offline {
  border-color: rgba(181, 71, 8, 0.25);
  color: var(--stale);
  background: rgba(181, 71, 8, 0.08);
}

.filter-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin-top: 18px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f2f4f7;
}

.filter-button {
  min-width: 0;
  min-height: 36px;
  padding: 7px 8px;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
}

.filter-button.is-active {
  color: var(--text);
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(23, 32, 51, 0.12);
}

.panel-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.panel-stats div {
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbfc;
}

.panel-stats span {
  display: block;
  overflow: hidden;
  font-size: 20px;
  font-weight: 800;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.panel-stats small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.tool-panel {
  margin-top: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbfc;
}

.tool-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.tool-header h2,
.tool-header h3 {
  margin: 0;
  font-size: 14px;
  line-height: 1.2;
}

.coordinate-grid,
.offline-grid,
.cleanup-grid {
  display: grid;
  gap: 6px;
}

.coordinate-grid {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 68px;
}

.offline-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.cleanup-grid {
  grid-template-columns: minmax(0, 1fr) 92px;
}

.device-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.coordinate-grid label,
.offline-grid label,
.cleanup-grid label,
.device-form-grid label,
.login-form label {
  min-width: 0;
}

.coordinate-grid span,
.offline-grid span,
.cleanup-grid span,
.device-form-grid span,
.login-form span {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.coordinate-grid input,
.offline-grid input,
.cleanup-grid input,
.cleanup-grid select,
.device-form-grid input,
.device-form-grid select,
.login-form input {
  width: 100%;
  min-width: 0;
  height: 34px;
  padding: 6px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: #ffffff;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
}

.cleanup-grid select {
  text-overflow: ellipsis;
}

.device-form-grid label:first-child,
.device-form-grid label:nth-child(6) {
  grid-column: 1 / -1;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  accent-color: #2563eb;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.primary-button,
.secondary-button,
.danger-button,
.compact-button {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
}

.primary-button {
  flex: 1 1 150px;
  padding: 7px 10px;
  color: #ffffff;
  border-color: #1d4ed8;
  background: #2563eb;
}

.secondary-button,
.compact-button {
  padding: 7px 9px;
  color: #263449;
  background: #ffffff;
}

.danger-button {
  padding: 7px 9px;
  color: #9f1239;
  border-color: rgba(159, 18, 57, 0.28);
  background: #fff5f7;
}

.primary-button:hover,
.primary-button:focus-visible {
  background: #1d4ed8;
  outline: none;
}

.secondary-button:hover,
.secondary-button:focus-visible,
.danger-button:hover,
.danger-button:focus-visible,
.compact-button:hover,
.compact-button:focus-visible {
  background: #edf2f7;
  outline: none;
}

.primary-button:disabled,
.secondary-button:disabled,
.danger-button:disabled {
  cursor: wait;
  opacity: 0.62;
}

.tool-note {
  min-height: 18px;
  margin-top: 7px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mode-badge {
  flex: 0 0 auto;
  padding: 4px 8px;
  border: 1px solid rgba(15, 138, 75, 0.24);
  border-radius: 999px;
  color: var(--fresh);
  background: rgba(15, 138, 75, 0.08);
  font-size: 11px;
  font-weight: 900;
}

.mode-badge.is-offline {
  border-color: rgba(37, 99, 235, 0.24);
  color: #1d4ed8;
  background: rgba(37, 99, 235, 0.08);
}

.progress-shell {
  height: 6px;
  margin-top: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8edf4;
}

.progress-bar {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: #2563eb;
  transition: width 0.2s ease;
}

.setup-overlay {
  position: fixed;
  z-index: 900;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(23, 32, 51, 0.42);
}

.setup-overlay[hidden] {
  display: none;
}

.setup-dialog {
  display: flex;
  flex-direction: column;
  width: min(720px, 100%);
  max-height: min(760px, calc(100vh - 36px));
  overflow: hidden;
  border: 1px solid rgba(216, 222, 232, 0.9);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(23, 32, 51, 0.32);
}

.setup-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.setup-header h2 {
  margin: 0;
  font-size: 22px;
  line-height: 1.15;
}

.setup-content {
  display: grid;
  gap: 12px;
  overflow-y: auto;
  padding: 14px;
}

.device-list {
  display: grid;
  gap: 7px;
  margin-top: 10px;
}

.device-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: #ffffff;
  cursor: pointer;
  text-align: left;
}

.device-item:hover,
.device-item:focus-visible {
  border-color: #9aa8bd;
  outline: none;
}

.device-item strong,
.device-item span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-item span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.login-shell {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 18px;
  background: #eef2f7;
}

.login-panel {
  width: min(380px, 100%);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.login-panel h1 {
  margin-bottom: 16px;
}

.login-form {
  display: grid;
  gap: 10px;
}

.tracker-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  margin-top: 14px;
  overflow-y: auto;
  padding-right: 2px;
}

.tracker-item {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  cursor: pointer;
}

.tracker-item:hover,
.tracker-item:focus-visible {
  border-color: #9aa8bd;
  outline: none;
}

.tracker-icon,
.map-marker {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 2px solid #ffffff;
  border-radius: 50%;
  color: #ffffff;
  font-size: 13px;
  font-weight: 900;
  box-shadow: 0 3px 10px rgba(23, 32, 51, 0.28);
}

.marker-pane .map-marker {
  position: absolute;
  top: 0;
  left: 0;
  padding: 0;
  pointer-events: auto;
  cursor: pointer;
}

.tracker-icon.person,
.map-marker.person {
  background: var(--person);
}

.tracker-icon.animal,
.map-marker.animal {
  background: var(--animal);
}

.tracker-icon.vehicle,
.map-marker.vehicle {
  background: var(--vehicle);
}

.tracker-icon.unknown,
.map-marker.unknown {
  background: var(--unknown);
}

.tracker-main {
  min-width: 0;
}

.tracker-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.tracker-name {
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tracker-age {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.tracker-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 7px;
}

.tracker-meta span {
  max-width: 100%;
  overflow: hidden;
  padding: 4px 7px;
  border-radius: 999px;
  background: #edf0f5;
  color: #344054;
  font-size: 12px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 140px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.popup-title {
  margin-bottom: 6px;
  font-weight: 800;
}

.popup-line {
  margin: 2px 0;
  color: #344054;
}

@media (max-width: 820px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(42%, 1fr) minmax(240px, 40%);
  }

  .app-shell.is-sidebar-collapsed {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) 0;
  }

  .tracker-panel {
    height: 100%;
    min-height: 0;
    padding: 14px;
    border-top: 1px solid var(--line);
    border-left: 0;
    box-shadow: 0 -14px 36px rgba(23, 32, 51, 0.18);
  }

  .app-shell.is-sidebar-collapsed .tracker-panel {
    height: 0;
    padding: 0;
    border-top: 0;
  }

  .sidebar-fab {
    top: 12px;
    right: 12px;
  }

  h1 {
    font-size: 19px;
  }

  .filter-row {
    overflow-x: auto;
    grid-template-columns: repeat(4, minmax(92px, 1fr));
  }

  .panel-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 430px) {
  .panel-header {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
  }

  .panel-actions {
    align-items: center;
  }

  .status-pill {
    align-self: flex-start;
  }

  .panel-stats span {
    font-size: 16px;
  }

  .tracker-item {
    grid-template-columns: 32px minmax(0, 1fr);
  }

  .filter-row {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
  }

  .filter-button {
    min-height: 34px;
    padding: 6px 4px;
    font-size: 12px;
  }

  .panel-stats {
    gap: 6px;
  }

  .panel-stats div {
    padding: 8px;
  }

  .panel-stats small {
    font-size: 11px;
  }

  .coordinate-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .coordinate-grid label:last-child {
    grid-column: 1 / -1;
  }

  .tool-note {
    white-space: normal;
  }

  .cleanup-grid {
    grid-template-columns: 1fr;
  }

  .device-form-grid {
    grid-template-columns: 1fr;
  }

  .setup-dialog {
    max-height: calc(100vh - 20px);
  }

  .setup-overlay {
    padding: 10px;
  }
}

/* Jo-Tracker additions: history and device colors */
.app-shell:not(.is-sidebar-collapsed) .sidebar-fab {
  display: none;
}

.history-pane {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.history-panel {
  margin-top: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbfc;
}

.history-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 116px;
  gap: 6px;
}

.history-grid label {
  min-width: 0;
}

.history-grid span {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.history-grid select {
  width: 100%;
  min-width: 0;
  height: 34px;
  padding: 6px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: #ffffff;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
}

.tracker-icon,
.map-marker {
  font-size: 11px;
  text-transform: uppercase;
}

@media (max-width: 430px) {
  .history-grid {
    grid-template-columns: 1fr;
  }
}