:root {
  --red: #b91c1c;
  --red-dark: #7f1d1d;
  --green: #16a34a;
  --grey: #6b7280;
  --line: #e5e7eb;
  --bg: #fff;
  --text: #111827;
  --muted: #6b7280;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
  --top: calc(52px + env(safe-area-inset-top));
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  font: 15px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

h1, h2, h3, h4 {
  margin: 0 0 0.4em;
  line-height: 1.2;
}
h3 {
  font-size: 1.05rem;
  margin-top: 1.2em;
}
p {
  margin: 0.3em 0 0.6em;
}
.small {
  font-size: 0.85rem;
}
.muted {
  color: var(--muted);
}
.error {
  color: var(--red);
  font-weight: 600;
}
.notice {
  background: #fef3c7;
  padding: 8px 10px;
  border-radius: 8px;
}

/* ---------- buttons & forms ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f9fafb;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:active {
  transform: translateY(1px);
}
.btn:disabled {
  opacity: 0.5;
  cursor: default;
}
.btn.small {
  min-height: 34px;
  padding: 0 10px;
  font-size: 0.85rem;
}
.btn.primary {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}
.btn.done {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  flex: 1.4;
}
.btn.on {
  background: #dbeafe;
  border-color: #93c5fd;
}
.btn.pulse {
  animation: pulse 1.2s infinite;
}
@keyframes pulse {
  50% {
    box-shadow: 0 0 0 6px rgba(22, 163, 74, 0.3);
  }
}
.link {
  border: 0;
  background: none;
  color: var(--red);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
  padding: 8px 0;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 0;
  background: none;
  color: inherit;
  font-size: 1.2rem;
  cursor: pointer;
  border-radius: 50%;
  flex: none;
}
input,
select,
textarea {
  font: inherit;
  padding: 9px 10px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  min-height: 42px;
  max-width: 100%;
}
textarea {
  width: 100%;
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
}
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-weight: 600;
  font-size: 0.9rem;
}
label.check {
  flex-direction: row;
  align-items: center;
  font-weight: normal;
}
label.check input {
  min-height: 0;
  width: 20px;
  height: 20px;
}
.form-grid {
  display: grid;
  gap: 10px;
  margin: 10px 0;
}
.form-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 8px 0;
}
.form-row input,
.form-row select {
  flex: 1;
  min-width: 120px;
}
.form-row label.check input {
  flex: none;
  min-width: 0;
}
.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 10px 0 4px;
}
.actions .btn {
  flex: 1;
}
.actions.secondary .btn {
  flex: none;
}
.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #e5e7eb;
  white-space: nowrap;
}
.pill.ok {
  background: #dcfce7;
  color: #166534;
}
.pill.warn {
  background: #fef3c7;
  color: #92400e;
}
.pill.bad {
  background: #fee2e2;
  color: #991b1b;
}
.dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  margin-right: 6px;
  background: #dc2626;
  flex: none;
}
.dot.done {
  background: var(--green);
}
.dot.skipped {
  background: var(--grey);
}
.dot.empty {
  background: #d1d5db;
}
.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 4px;
}
.meter {
  height: 8px;
  background: #e5e7eb;
  border-radius: 4px;
  overflow: hidden;
  margin: 4px 0;
}
.meter > div {
  height: 100%;
  background: var(--green);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
th,
td {
  text-align: left;
  padding: 6px 4px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
td input,
td select {
  min-height: 34px;
  padding: 4px 6px;
  width: 100%;
}
tr.on {
  background: #eff6ff;
}
tr[data-focus] {
  cursor: pointer;
}
tr.inactive {
  opacity: 0.5;
}
ul.plain {
  list-style: none;
  padding: 0;
}
ul.plain li {
  padding: 6px 0;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ---------- login ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: linear-gradient(160deg, #fef2f2, #fff 60%);
  overflow: auto;
}
.login-box {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-box label {
  text-align: left;
}
.login-box img {
  align-self: center;
}

/* ---------- app layout ---------- */
#app {
  position: fixed;
  inset: 0;
}
#topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: var(--top);
  padding: env(safe-area-inset-top) 8px 0 4px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--red);
  color: #fff;
  z-index: 1100;
}
#topbar .title {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
#topbar .title strong,
#topbar .title span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#progress-text {
  font-size: 0.8rem;
  opacity: 0.9;
}
#progress-bar {
  position: absolute;
  top: var(--top);
  left: 0;
  right: 0;
  height: 4px;
  background: var(--red-dark);
  z-index: 1100;
}
#progress-bar > div {
  height: 100%;
  width: 0;
  background: #4ade80;
  transition: width 0.4s;
}
#map {
  position: absolute;
  top: calc(var(--top) + 4px);
  left: 0;
  right: 0;
  bottom: 0;
  background: #e5e7eb;
}
#bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  padding-bottom: env(safe-area-inset-bottom);
}
#bottom > * {
  pointer-events: auto;
}
#fabs {
  align-self: flex-end;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin: 0 12px 8px;
}
.fab {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 0;
  background: #fff;
  color: var(--red);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.7rem;
  cursor: pointer;
}
.fab.small {
  width: 44px;
  height: 44px;
  color: #374151;
}
.fab.next {
  background: #9333ea;
  color: #fff;
}
.fab.on {
  background: #1d4ed8;
  color: #fff;
}
.card {
  background: #fff;
  box-shadow: var(--shadow);
  border-radius: var(--radius);
  margin: 0 8px;
  padding: 12px 14px;
  max-height: 60vh;
  overflow: auto;
}
#sheet {
  margin-bottom: 8px;
}
#next-card {
  padding: 8px 12px 10px;
}
.next-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.next-row .icon-btn {
  width: 32px;
  height: 32px;
}
.next-title {
  margin: 0 0 4px;
}
#next-card .actions {
  margin-top: 4px;
}
.sheet-head {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.sheet-head > div {
  flex: 1;
  min-width: 0;
}
.sheet-head h3 {
  margin: 4px 0 0;
  font-size: 1.15rem;
}
.sheet-head p {
  margin: 0;
}
.side-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px 4px;
  margin-top: 8px;
}
.count-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}
.count-row label {
  flex: 1;
  font-weight: normal;
  font-size: 0.9rem;
}
.count-row input {
  width: 80px;
  text-align: center;
}
.sel-list {
  list-style: none;
  padding: 0;
  margin: 6px 0;
  max-height: 22vh;
  overflow: auto;
  font-size: 0.9rem;
}
.sel-list li {
  display: flex;
  align-items: center;
  padding: 3px 0;
}
.gps-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}
.gps-head .switch {
  flex: 1;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  font-weight: normal;
  gap: 6px;
}
.gps-head .switch input {
  min-height: 0;
  width: 20px;
  height: 20px;
}
.switch.share {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  font-weight: normal;
  font-size: 0.85rem;
  margin: 2px 0 4px;
}
.switch.share input {
  min-height: 0;
  width: 20px;
  height: 20px;
}
.live {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #1d4ed8;
  animation: pulse-blue 1.5s infinite;
}
@keyframes pulse-blue {
  50% {
    box-shadow: 0 0 0 5px rgba(29, 78, 216, 0.25);
  }
}
#legend {
  position: absolute;
  right: 70px;
  bottom: 90px;
  z-index: 1001;
  max-width: 280px;
  font-size: 0.85rem;
}
#legend ul {
  list-style: none;
  padding: 0;
  margin: 0 0 6px;
}
#legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
}

#preview-bar,
#version-bar {
  position: absolute;
  top: calc(var(--top) + 12px);
  left: 10px;
  right: 60px;
  z-index: 1050;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 0;
  padding: 8px 10px;
  font-size: 0.85rem;
  font-weight: 600;
}
#preview-bar span,
#version-bar span {
  flex: 1 1 100%;
}
#version-bar {
  z-index: 2100;
}

/* ---------- drawer & panel ---------- */
#drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 1990;
}
#drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: 280px;
  max-width: 85vw;
  background: #fff;
  z-index: 2000;
  transform: translateX(-105%);
  transition: transform 0.2s;
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 10px) 0 10px;
  box-shadow: var(--shadow);
}
#drawer.open {
  transform: none;
}
.drawer-head {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px 16px 14px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 6px;
}
.drawer-head div {
  display: flex;
  flex-direction: column;
}
#drawer button {
  border: 0;
  background: none;
  text-align: left;
  font: inherit;
  font-size: 1rem;
  padding: 13px 18px;
  cursor: pointer;
}
#drawer button:hover {
  background: #f3f4f6;
}
#drawer hr {
  border: 0;
  border-top: 1px solid var(--line);
  width: 100%;
}
.admin-only {
  display: none;
}
body.is-admin .admin-only {
  display: block;
}
#panel {
  position: fixed;
  inset: 0;
  z-index: 1500;
  background: #fff;
  display: flex;
  flex-direction: column;
}
#panel header {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: env(safe-area-inset-top) 8px 0 4px;
  min-height: var(--top);
  border-bottom: 1px solid var(--line);
}
#panel header h2 {
  margin: 0;
  font-size: 1.15rem;
}
.panel-body {
  flex: 1;
  overflow: auto;
  padding: 12px 16px calc(24px + env(safe-area-inset-bottom));
}
.panel-body section {
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}
.panel-body details {
  margin: 8px 0;
}
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 8px;
  margin: 8px 0;
}
.stat-row > div {
  background: #f9fafb;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  display: flex;
  flex-direction: column;
}
.stat-row b {
  font-size: 1.2rem;
}
.stat-row span {
  font-size: 0.75rem;
  color: var(--muted);
}
.route-list,
.street-list {
  padding: 0;
  list-style: none;
}
.route-list li,
.street-list li {
  padding: 8px 4px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.route-list li {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.route-list li.is-done,
.route-list li.is-skipped {
  opacity: 0.55;
}
.route-list .num {
  width: 28px;
  color: var(--muted);
  font-size: 0.8rem;
}
.street-list .meter {
  margin: 4px 0 2px;
}
.segmented {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.segmented button {
  flex: 1;
  border: 0;
  background: #fff;
  padding: 10px 6px;
  font: inherit;
  cursor: pointer;
}
.segmented button.on {
  background: #1d4ed8;
  color: #fff;
}
input[type='search'] {
  width: 100%;
}

/* ---------- map markers ---------- */
.route-start span,
.route-num span {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
}
.route-start span {
  color: #fff;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}
.route-num span {
  background: #fff;
  border: 2px solid;
  color: #111;
}

.pkg-label span {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  border-radius: 10px;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}
.leaflet-tooltip.peer-label {
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 700;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
ul.plain.pkg-list li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 4px 10px;
  align-items: center;
}
.pkg-list .pkg-num {
  min-width: 34px;
  text-align: center;
  color: #fff;
  font-weight: 700;
  font-size: 0.8rem;
  border-radius: 10px;
  padding: 2px 6px;
}
.pkg-list .meter {
  grid-column: 2 / 4;
  margin: 0;
}
.tag {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0 6px;
  border-radius: 6px;
  background: #fef3c7;
  color: #92400e;
  margin-left: 4px;
}

#toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  /* Top, so it never covers the buttons of the cards at the bottom. */
  top: calc(var(--top) + 14px);
  z-index: 3000;
  background: #111827;
  color: #fff;
  padding: 10px 14px;
  border-radius: 10px;
  display: flex;
  gap: 12px;
  align-items: center;
  max-width: calc(100vw - 24px);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
}
#toast button {
  border: 0;
  background: none;
  color: #fde047;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

.leaflet-container {
  font: inherit;
}

/* ---------- larger screens ---------- */
@media (min-width: 800px) {
  #bottom {
    right: auto;
    width: 440px;
    left: 12px;
    bottom: 12px;
  }
  #fabs {
    position: fixed;
    right: 12px;
    bottom: 24px;
  }
  #legend {
    right: 90px;
    bottom: 24px;
  }
  #preview-bar,
  #version-bar {
    right: auto;
    max-width: 440px;
  }
  #preview-bar span,
  #version-bar span {
    flex: 1;
  }
  #panel {
    left: auto;
    width: 460px;
    top: calc(var(--top) + 4px);
    box-shadow: var(--shadow);
  }
}

/* ---------- team start pins (route planning) ---------- */
#pin-card {
  padding: 8px 12px 10px;
}
#pin-card p {
  margin: 4px 0;
}
.pin-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 6px 0;
}
.pin-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 3px 10px 3px 3px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.pin-chip.on {
  border-color: #111827;
  box-shadow: 0 0 0 1px #111827;
}
.pin-chip .dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  font-size: 12px;
}
.pin-edit {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.pin-edit select {
  width: auto;
  padding: 6px 8px;
}
.stepper {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.stepper .btn {
  min-width: 34px;
}
.team-pin-icon {
  background: none;
  border: 0;
}
.team-pin {
  position: absolute;
  left: 3px;
  top: 0;
  width: 28px;
  height: 28px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 3px solid #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
}
.team-pin span {
  transform: rotate(45deg);
  color: #fff;
  font-weight: 800;
  font-size: 14px;
}
.team-pin.on {
  outline: 3px solid #111827;
  outline-offset: 1px;
}

/* GPS button: green while on; a small dot while the position is shared */
#gps-btn.gps-on {
  background: #16a34a;
  color: #fff;
}
#gps-btn {
  position: relative;
  -webkit-touch-callout: none;
  user-select: none;
}
#gps-btn.sharing::after {
  content: '';
  position: absolute;
  top: 4px;
  right: 4px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #22d3ee;
  border: 2px solid #fff;
}
.gps-head > span:nth-child(2) {
  flex: 1;
}
