:root {
  --green: #1f6f4a;
  --green-dark: #155539;
  --amber: #d98a00;
  --red: #c1351d;
  --bg: #f6f7f5;
  --card: #ffffff;
  --ink: #1a201d;
  --muted: #6b756f;
  --line: #e2e6e2;
  --shadow: 0 4px 24px rgba(0,0,0,.14);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  overscroll-behavior: none;
}

#app { position: fixed; inset: 0; }
#map { position: absolute; inset: 0; z-index: 1; background: #dfe5df; }

/* Top bar */
#topbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 1000;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-top)) 12px 8px;
  background: linear-gradient(180deg, rgba(255,255,255,.96), rgba(255,255,255,.0));
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 6px; font-weight: 700; margin-right: auto;
  background: var(--card); padding: 6px 12px; border-radius: 999px; box-shadow: var(--shadow); }
.brand-mark { color: var(--green); transform: translateY(-1px); }
.brand-name { font-size: 15px; letter-spacing: .2px; }
.icon-btn {
  width: 42px; height: 42px; border: none; border-radius: 50%;
  background: var(--card); box-shadow: var(--shadow); font-size: 18px; cursor: pointer;
  display: grid; place-items: center;
}
.icon-btn:active { transform: scale(.94); }

/* Bottom sheet */
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 1000;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -6px 28px rgba(0,0,0,.16);
  padding: 8px 16px calc(16px + var(--safe-bottom));
  max-height: 78vh; overflow-y: auto;
  transition: transform .28s ease;
}
.sheet.collapsed { transform: translateY(calc(100% - 132px)); }
/* Generous, full-width grab zone so the drag is easy to hit on a phone.
   touch-action:none lets us own the vertical gesture instead of the page. */
#sheet-handle {
  display: block; width: 100%; padding: 8px 0 10px; margin: 0 0 6px;
  cursor: grab; touch-action: none; -webkit-user-select: none; user-select: none;
}
#sheet-handle:active { cursor: grabbing; }
#sheet-handle::before {
  content: ""; display: block; width: 44px; height: 5px; border-radius: 999px;
  background: var(--line); margin: 0 auto;
}
.sheet.dragging { transition: none !important; }

.field { margin-bottom: 10px; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.input-row { display: flex; gap: 8px; }
.input-row input, .field input[type="text"] {
  flex: 1; width: 100%; font-size: 16px; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 12px; background: #fbfcfb;
}
input:focus, select:focus { outline: 2px solid var(--green); outline-offset: 0; border-color: var(--green); }
.mini-btn {
  flex: 0 0 46px; border: 1px solid var(--line); border-radius: 12px; background: #fbfcfb;
  font-size: 18px; cursor: pointer;
}
.mini-btn:active { background: #eef2ee; }

.results {
  list-style: none; margin: 4px 0 0; padding: 4px; border: 1px solid var(--line);
  border-radius: 12px; background: var(--card); max-height: 180px; overflow-y: auto;
}
.results li { padding: 10px 12px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.results li:active, .results li.active { background: #eef4f0; }

.profile-chip {
  font-size: 13px; color: var(--green-dark); background: #eaf3ee;
  border: 1px solid #cfe3d8; padding: 8px 12px; border-radius: 999px;
  text-align: center; margin: 4px 0 12px; cursor: pointer; font-weight: 600;
}

.rvmode-toggle {
  display: flex; align-items: center; gap: 9px; cursor: pointer;
  font-size: 13px; color: var(--ink); padding: 2px 4px; margin: 0 0 10px;
  user-select: none;
}
.rvmode-toggle input { width: 18px; height: 18px; accent-color: var(--green); flex: none; }
.rvmode-text { line-height: 1.25; }

.primary-btn {
  width: 100%; border: none; border-radius: 14px; padding: 15px;
  background: var(--green); color: #fff; font-size: 16px; font-weight: 700; cursor: pointer;
}
.primary-btn:active { background: var(--green-dark); }
.primary-btn:disabled { opacity: .55; cursor: default; }
.ghost-btn {
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 14px; padding: 15px; font-size: 16px; font-weight: 600; cursor: pointer;
}

.status {
  margin-top: 12px; padding: 10px 14px; border-radius: 12px; font-size: 14px;
  background: #eef2ee; color: var(--muted);
}
.status.error { background: #fbeae7; color: var(--red); }
.status.ok { background: #eaf3ee; color: var(--green-dark); }

.route-summary {
  margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: #f1f5f2;
  font-size: 14px; display: flex; gap: 18px; font-weight: 600;
}
.route-summary .num { font-size: 20px; color: var(--green-dark); }

/* Warnings */
.warnings { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.warn {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 11px 13px; border-radius: 12px; font-size: 13.5px; border: 1px solid;
}
.warn .ico { font-size: 18px; line-height: 1; }
.warn.danger { background: #fbeae7; border-color: #f2c4ba; color: #8c2415; }
.warn.caution { background: #fff5e0; border-color: #f3dca0; color: #6b4a00; }
.warn.clear { background: #eaf3ee; border-color: #cfe3d8; color: var(--green-dark); }
.warn b { display: block; }
.verify-tag {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 999px;
  font-size: 11px; font-weight: 700; letter-spacing: .03em; text-transform: uppercase;
  background: #8c2415; color: #fff; vertical-align: middle;
}

/* Modals */
.modal {
  position: absolute; inset: 0; z-index: 2000; display: grid; place-items: end center;
  background: rgba(20,28,24,.45); padding: 0;
}
/* The [hidden] attribute must win over the display:grid above, or modals never close. */
.modal[hidden] { display: none; }
.modal-card {
  width: 100%; max-width: 520px; background: var(--card);
  border-radius: 18px 18px 0 0; padding: 20px 18px calc(20px + var(--safe-bottom));
  max-height: 92vh; overflow-y: auto; box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 4px; font-size: 19px; }
.muted { color: var(--muted); }
.modal-card .muted { font-size: 13px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
/* Grid/flex children default to min-width:auto, which refuses to shrink below
   their content and pushes the unit selectors off the right edge of the card
   (the clipping seen on desktop). Letting them shrink keeps everything inside. */
.grid2 > * { min-width: 0; }
.modal-card label { display: block; font-size: 13px; font-weight: 600; }
.modal-card label.full { display: block; margin-top: 14px; }
.unit-input { display: flex; gap: 6px; margin-top: 6px; }
.unit-input input { flex: 1; min-width: 0; font-size: 16px; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; }
.unit-input select, .unit-static {
  flex: 0 0 64px; font-size: 15px; padding: 11px 8px; border: 1px solid var(--line);
  border-radius: 10px; background: #fbfcfb; text-align: center;
}
.unit-static { display: grid; place-items: center; color: var(--muted); }

/* Feet + inches input: two compact number fields, each with a trailing unit. */
.ftin-input { display: flex; gap: 6px; margin-top: 6px; }
.ftin-field { flex: 1 1 0; min-width: 0; display: flex; align-items: center; gap: 4px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; padding-right: 8px; }
.ftin-field input { flex: 1 1 0; min-width: 0; width: 100%; font-size: 16px;
  padding: 11px 4px 11px 12px; border: none; border-radius: 10px; background: transparent;
  -moz-appearance: textfield; }
.ftin-field input:focus { outline: none; }
.ftin-field:focus-within { border-color: var(--green-dark); box-shadow: 0 0 0 2px rgba(31,111,74,.15); }
.ftin-unit { flex: 0 0 auto; font-size: 13px; font-weight: 600; color: var(--muted); }
.modal-card small { display: block; margin-top: 6px; font-weight: 400; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions > * { flex: 1; }
.modal-card input[type="text"] { width: 100%; font-size: 16px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; margin-top: 6px; }
.modal-card hr { border: none; border-top: 1px solid var(--line); margin: 18px 0 12px; }
.disclaimer { font-size: 12px; color: var(--muted); line-height: 1.5; }
.disclaimer code { background: #eef2ee; padding: 1px 5px; border-radius: 5px; }
.link-like { color: var(--green); text-decoration: underline; cursor: pointer; }

/* Map markers — pill sized to fit a one-decimal label like "13-2". */
.clearance-pin {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 26px; border-radius: 13px; color: #fff;
  font-size: 11px; font-weight: 800; letter-spacing: .2px; white-space: nowrap;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4);
}
.clearance-pin.danger { background: var(--red); }
.clearance-pin.caution { background: var(--amber); }

/* Numbered waypoint markers (start / stops / destination). */
.wp-pin {
  display: grid; place-items: center; width: 28px; height: 28px;
  border-radius: 50% 50% 50% 2px; transform: rotate(0deg);
  color: #fff; font-size: 13px; font-weight: 800; border: 2px solid #fff;
  box-shadow: 0 1px 5px rgba(0,0,0,.45); background: #2f6fed;
}
.wp-pin.start { background: var(--green); }
.wp-pin.end { background: var(--green-dark); }

/* Manual route-steering points — distinct purple badge so they read as
   guidance, not stops. */
.steer-pin {
  display: grid; place-items: center; width: 22px; height: 22px;
  color: #fff; font-size: 12px; font-weight: 800; line-height: 1;
  background: #8338ec; border: 2px solid #fff; border-radius: 50%;
  box-shadow: 0 1px 5px rgba(0,0,0,.45); cursor: grab;
}

/* Steering choice modal — stacked full-width option buttons. */
.choice-options { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.choice-options .choice-btn { width: 100%; }

/* Remove button inside a steering-point popup. */
.popup-del {
  margin-top: 8px; padding: 6px 12px; font-size: 13px; font-weight: 700;
  color: #fff; background: var(--red); border: none; border-radius: 8px; cursor: pointer;
}

/* Keep the Leaflet zoom control clear of the top-right header buttons. */
.leaflet-top.leaflet-right { top: calc(var(--safe-top) + 62px); }

/* Hazard toggle on/off state. */
#btn-hazards { font-size: 17px; opacity: .5; }
#btn-hazards.on { opacity: 1; box-shadow: 0 0 0 2px var(--red) inset, var(--shadow); }

/* ---- Waypoint list ---- */
.waypoints { display: flex; flex-direction: column; gap: 8px; }
.wp-row { display: flex; align-items: center; gap: 8px; }
.wp-row.drag-over { outline: 2px dashed var(--green); outline-offset: 2px; border-radius: 12px; }
.wp-row.dragging-row { opacity: .45; }
.wp-grip {
  flex: 0 0 26px; height: 44px; display: grid; place-items: center;
  color: var(--muted); font-size: 17px; cursor: grab; touch-action: none;
  -webkit-user-select: none; user-select: none;
}
.wp-grip:active { cursor: grabbing; }
.wp-badge {
  flex: 0 0 26px; height: 26px; display: grid; place-items: center;
  border-radius: 50%; font-size: 12px; font-weight: 800; color: #fff; background: #2f6fed;
}
.wp-badge.start { background: var(--green); }
.wp-badge.end { background: var(--green-dark); }
.wp-main { flex: 1; min-width: 0; position: relative; }
.wp-main input[type="text"] {
  width: 100%; font-size: 16px; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 12px; background: #fbfcfb;
}
.wp-mini {
  flex: 0 0 40px; height: 44px; border: 1px solid var(--line); border-radius: 12px;
  background: #fbfcfb; font-size: 17px; cursor: pointer; color: var(--muted);
}
.wp-mini:active { background: #eef2ee; }
.wp-actions { display: flex; gap: 8px; margin: 10px 0 12px; }
.ghost-btn.small { padding: 10px 12px; font-size: 14px; border-radius: 12px; }
.ghost-btn.small.full-w { width: 100%; }
.wp-actions .ghost-btn { flex: 1; }

/* ---- Per-leg breakdown ---- */
.legs { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.leg {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  padding: 9px 12px; border-radius: 10px; background: #f6f8f6; font-size: 13px;
}
.leg .leg-name { color: var(--ink); font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leg .leg-num { color: var(--green-dark); font-weight: 700; white-space: nowrap; }

/* ---- Trips list ---- */
.trips-list { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.trip-item {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 12px; background: #fbfcfb;
}
.trip-item .trip-info { flex: 1; min-width: 0; }
.trip-item .trip-name { font-weight: 700; font-size: 15px; }
.trip-item .trip-meta { font-size: 12px; color: var(--muted); }
.trip-item button { border: none; background: none; cursor: pointer; font-size: 14px; font-weight: 700; padding: 8px 10px; border-radius: 10px; }
.trip-item .trip-load { color: var(--green-dark); background: #eaf3ee; }
.trip-item .trip-del { color: var(--red); }
.trip-item .trip-del.armed { background: #fbeae7; color: #8c2415; }

@media (min-width: 720px) {
  .sheet { left: auto; right: 16px; bottom: 16px; width: 380px; border-radius: 18px; max-height: 84vh; }
  .sheet.collapsed { transform: none; }
  .modal { place-items: center; }
  .modal-card { border-radius: 18px; }
}

/* ---- Search results: name + category tag ---------------------------------- */
.results li { display: flex; align-items: baseline; gap: 8px; justify-content: space-between; }
.result-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.result-kind {
  flex: none; font-size: 11px; font-weight: 700; text-transform: capitalize;
  color: var(--green-dark); background: #eaf3ee; border: 1px solid #cfe3d8;
  padding: 2px 8px; border-radius: 999px; white-space: nowrap;
}

/* ---- Category POI quick-search ------------------------------------------- */
.poi-cats {
  display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 10px;
}
.poi-chip {
  flex: 1 1 calc(50% - 4px); min-width: 0;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 999px; padding: 9px 12px; font-size: 13px; font-weight: 600;
  cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.poi-chip:active { background: #eef4f0; }
.poi-chip.active {
  background: var(--green); border-color: var(--green); color: #fff;
}

.poi-results {
  list-style: none; margin: 0 0 12px; padding: 4px; border: 1px solid var(--line);
  border-radius: 12px; background: var(--card); max-height: 200px; overflow-y: auto;
}
.poi-results li {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  padding: 10px 12px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.poi-results li:active { background: #eef4f0; }
.poi-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.poi-meta { flex: none; font-size: 12px; color: var(--muted); white-space: nowrap; }
.poi-results li.poi-msg {
  cursor: default; color: var(--muted); font-size: 13px; justify-content: flex-start;
}
.poi-results li.poi-msg:active { background: transparent; }
