:root {
  --bg: #f3f5f8;
  --line: rgba(16, 24, 40, 0.12);
  --accent: #1e8e5a;
  --accent-strong: #135d3d;
  --text: #17212d;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  font-family: Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

button {
  font: inherit;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.2rem;
  padding-top: calc(0.85rem + env(safe-area-inset-top, 0px));
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.08);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-mark {
  display: block;
  width: 3.6rem;
  height: auto;
  object-fit: contain;
  border-radius: 0.4rem;
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
}

.eyebrow {
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #5c6b7a;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.1rem, 2vw, 1.6rem);
}

.top-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: flex-end;
}

.top-actions button {
  border: 1px solid var(--line);
  background: white;
  color: var(--text);
  border-radius: 999px;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  transition: 0.2s ease;
  white-space: nowrap;
}

.top-actions button:hover,
.top-actions button.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.top-actions .is-current-stage {
  background: rgba(30, 142, 90, 0.08);
  border-color: rgba(30, 142, 90, 0.35);
  color: var(--accent-strong);
  font-weight: 700;
}

.stage-quick-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  max-width: min(38rem, 60vw);
}

.stage-quick-label {
  flex: 0 0 auto;
  color: #536170;
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}

.stage-quick-list button {
  min-width: 2.6rem;
  padding: 0.42rem 0.4rem;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  text-align: center;
  background: rgba(15, 23, 42, 0.04);
  border-color: rgba(15, 23, 42, 0.08);
}

.map-area {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.map-canvas {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#map {
  width: 100%;
  height: 100%;
  min-height: 0;
  background: #dfe9f2;
}

.leaflet-container {
  width: 100%;
  height: 100%;
  background: #dfe9f2;
}

.tracker-marker-wrapper {
  background: transparent;
  border: none;
}

.tracker-cluster-marker {
  border: none;
  background: transparent;
}

.tracker-cluster-marker span {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 2px solid white;
  border-radius: 50%;
  background: #1e8e5a;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.4);
  color: white;
  font-size: 0.82rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.tracker-pin {
  position: relative;
  display: block;
  width: 34px;
  height: 34px;
  border: 2px solid white;
  border-radius: 50%;
  background: #65727e;
  background-position: center;
  background-size: cover;
  box-shadow: 0 2px 7px rgba(15, 23, 42, 0.35);
}

.tracker-pin.online {
  outline: 2px solid #2ecc71;
}

.tracker-pin.offline {
  outline: 2px solid #f39c12;
}

.tracker-status-dot {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 12px;
  height: 12px;
  border: 2px solid white;
  border-radius: 50%;
  background: #65727e;
}

.tracker-pin.online .tracker-status-dot {
  background: #2ecc71;
}

.tracker-pin.offline .tracker-status-dot {
  background: #f39c12;
}

.tracker-follow-action {
  display: block;
  margin-top: 0.5rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  background: white;
  color: var(--accent-strong);
  cursor: pointer;
  font: inherit;
}

.tracker-follow-action[aria-pressed="true"] {
  background: var(--accent);
  color: white;
}

.stage-route {
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.12));
}

.route-marker-wrapper {
  background: transparent;
  border: none;
}

.route-marker {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.9);
  font-size: 0.68rem;
  font-weight: 700;
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.25);
}

.route-marker.route-start {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  width: 88px;
  height: 28px;
  padding: 3px;
  border: 1px solid rgba(19, 93, 61, 0.35);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.96);
  color: #135d3d;
  font-size: 0.68rem;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 3px 9px rgba(15, 23, 42, 0.28);
}

.route-marker.route-start.is-reserved,
.route-marker.route-handoff.is-reserved {
  border-color: rgba(180, 83, 9, 0.55);
  background: #fff7ed;
  color: #8a3a00;
}

.route-start-symbol {
  display: grid;
  flex: 0 0 20px;
  height: 20px;
  place-items: center;
  border-radius: 50%;
  background: #1e8e5a;
  color: white;
  font-size: 0.62rem;
  font-weight: 700;
}

.route-marker.route-start.is-reserved .route-start-symbol {
  background: #b45309;
}

.route-marker.route-start time {
  font-weight: 700;
}

.route-marker.route-finish {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  width: 96px;
  height: 28px;
  padding: 3px 0.45rem;
  border: 1px solid rgba(185, 28, 28, 0.42);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.97);
  color: #991b1b;
  font-size: 0.68rem;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 3px 9px rgba(15, 23, 42, 0.28);
}

.route-marker.route-handoff {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  width: 92px;
  height: 28px;
  padding: 3px 0.45rem;
  border: 1px solid rgba(13, 110, 253, 0.35);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.97);
  color: #0b4da1;
  font-size: 0.68rem;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  box-shadow: 0 3px 9px rgba(15, 23, 42, 0.28);
}

.partner-bar {
  position: absolute;
  right: 1rem;
  bottom: 1rem;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.65rem;
  padding: 0.45rem 0.7rem;
  border-radius: 0.9rem;
  background: rgba(255, 255, 255, 0.18);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 10px 22px rgba(15, 23, 42, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.18);
  z-index: 999;
}

.partner-logo-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.partner-logo {
  display: block;
  max-height: 2.15rem;
  max-width: 4.2rem;
  width: auto;
  object-fit: contain;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.06));
  background: transparent;
}

.partner-logo-primary {
  max-height: 2.7rem;
  max-width: 5.2rem;
}

.elevation-profile {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 550;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  height: clamp(150px, 28%, 200px);
  padding: 0.5rem 1rem 0.35rem;
  background: rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(1px);
  -webkit-backdrop-filter: blur(1px);
  border-top: 1px solid var(--line);
  pointer-events: none;
}

.elevation-profile[hidden] {
  display: none;
}

.elevation-profile-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
}

.elevation-profile-header h2,
.elevation-profile-stats {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.2;
}

.elevation-profile-stats {
  color: #586574;
  text-align: right;
}

.elevation-profile-trackers {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-left: auto;
}

.elevation-tracker-key {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.68rem;
  color: #586574;
  white-space: nowrap;
}

.elevation-tracker-key::before {
  width: 0.5rem;
  height: 0.5rem;
  border: 1px solid white;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(23, 33, 45, 0.16);
  content: "";
}

.elevation-tracker-key.online::before,
.profile-tracker-marker.online {
  fill: #2ecc71;
  background: #2ecc71;
}

.elevation-tracker-key.offline::before,
.profile-tracker-marker.offline {
  fill: #f39c12;
  background: #f39c12;
}

.elevation-chart {
  display: block;
  width: 100%;
  min-height: 0;
  flex: 1;
  overflow: visible;
}

.elevation-area {
  fill: var(--profile-color, var(--accent));
  fill-opacity: 0.14;
}

.elevation-line {
  fill: none;
  stroke: var(--profile-color, var(--accent));
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.5;
  vector-effect: non-scaling-stroke;
}

.elevation-baseline {
  stroke: rgba(23, 33, 45, 0.18);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.profile-tracker-guide {
  stroke: currentColor;
  stroke-dasharray: 2 3;
  stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
}

.profile-tracker-guide.online {
  color: #2ecc71;
}

.profile-tracker-guide.offline {
  color: #f39c12;
}

.profile-tracker-marker {
  stroke: white;
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}

@media (max-width: 720px) {
  .topbar {
    flex-direction: column;
    align-items: flex-start;
    padding: 0.8rem 1rem;
    padding-top: calc(0.8rem + env(safe-area-inset-top, 0px));
  }

  .top-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .top-actions > button:not(.is-current-stage):not(.profile-toggle),
  .top-actions .profile-toggle {
    order: 0;
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
  }

  .top-actions .is-current-stage {
    order: 1;
    flex: 1 1 100%;
    min-width: 0;
    white-space: normal;
  }

  .stage-quick-list {
    order: 2;
    flex: 1 1 100%;
    width: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
  }

  .stage-quick-list button {
    flex: 0 0 auto;
    min-width: 2.6rem;
  }

  .elevation-profile {
    padding: 0.4rem 0.7rem calc(0.25rem + env(safe-area-inset-bottom, 0px));
  }

  .elevation-profile-header {
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 0.2rem 0.75rem;
  }

  .elevation-profile-trackers {
    margin-left: 0;
  }

  .elevation-profile-stats {
    text-align: left;
  }
}

@media (max-height: 500px) and (min-width: 721px) {
  .topbar {
    gap: 0.6rem;
    padding: 0.35rem 0.8rem;
  }

  .brand {
    gap: 0.5rem;
  }

  .brand-mark {
    width: 2.6rem;
  }

  .brand h1 {
    font-size: 1.1rem;
  }

  .eyebrow {
    font-size: 0.58rem;
  }

  .top-actions {
    gap: 0.3rem;
    max-height: 82px;
    overflow-y: auto;
  }

  .top-actions button {
    padding: 0.35rem 0.55rem;
    font-size: 0.76rem;
  }

  .stage-quick-list {
    max-width: min(32rem, 48vw);
    max-height: 68px;
    overflow-y: auto;
  }

  .stage-quick-list button {
    padding: 0.3rem 0.55rem;
  }

  .elevation-profile {
    height: clamp(100px, 32%, 132px);
    padding: 0.3rem 0.7rem calc(0.3rem + env(safe-area-inset-bottom, 0px));
  }

  .elevation-profile-header h2,
  .elevation-profile-stats {
    font-size: 0.68rem;
  }
}

.admin-body {
  min-height: 100%;
  height: auto;
  background: #f3f5f8;
}

.admin-shell {
  width: min(1120px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 2rem 0 3rem;
}

.admin-header,
.admin-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.admin-header {
  margin-bottom: 1.5rem;
}

.admin-header h1 {
  margin: 0;
  font-size: 1.6rem;
}

.admin-header a {
  color: var(--accent-strong);
}

.admin-toolbar {
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-bottom: 1rem;
}

.admin-action {
  border: 1px solid var(--line);
  border-radius: 0.35rem;
  padding: 0.55rem 0.75rem;
  background: white;
  color: var(--text);
  cursor: pointer;
}

.admin-action-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: white;
}

.admin-status {
  min-height: 1.4rem;
  margin: 0.75rem 0;
  color: #465462;
}

.admin-status.is-warning {
  color: #9a4b00;
  font-weight: 700;
}

.admin-table-wrap {
  overflow-x: auto;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.72);
}

.admin-table {
  width: 100%;
  min-width: 850px;
  border-collapse: collapse;
}

.admin-table th,
.admin-table td {
  padding: 0.65rem 0.5rem;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

.admin-table th {
  color: #536170;
  font-size: 0.75rem;
  font-weight: 700;
}

.admin-table input,
.admin-table select {
  width: 100%;
  min-height: 2.25rem;
  border: 1px solid rgba(16, 24, 40, 0.2);
  border-radius: 0.3rem;
  padding: 0.35rem 0.45rem;
  background: white;
  color: var(--text);
  font: inherit;
}

.admin-table .device-id-input {
  width: 6rem;
  font-variant-numeric: tabular-nums;
}

.admin-remove {
  border: 0;
  padding: 0.4rem;
  background: transparent;
  color: #a12c2c;
  cursor: pointer;
}

.admin-help {
  max-width: 72ch;
  margin-top: 1.25rem;
  color: #536170;
  font-size: 0.9rem;
  line-height: 1.5;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 720px) {
  .admin-shell {
    width: calc(100% - 1.25rem);
    padding-top: 1rem;
  }

  .admin-header {
    align-items: flex-start;
  }

  .admin-header h1 {
    font-size: 1.3rem;
  }
}
