:root {
  --slate-900: #0F172A;
  --slate-700: #334155;
  --slate-500: #64748B;
  --slate-300: #CBD5E1;
  --slate-100: #F1F5F9;
  --slate-50: #F8FAFC;
  --sky-500: #0EA5E9;
  --sky-100: rgba(14, 165, 233, 0.12);
  --emerald-500: #10B981;
  --emerald-100: rgba(16, 185, 129, 0.12);
  --amber-500: #F59E0B;
  --amber-700: #B45309;
  --purple-500: #8B5CF6;
  --red-500: #EF4444;
  --white: #FFFFFF;

  --radius-lg: 20px;
  --radius-md: 16px;
  --radius-sm: 12px;
  --radius-xs: 8px;

  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.10);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--slate-50);
  color: var(--slate-900);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button, input, textarea, select {
  font-family: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* ---------- App bar ---------- */
.app-bar {
  padding-top: var(--safe-top);
  background: var(--slate-50);
  border-bottom: 1px solid var(--slate-100);
  position: sticky;
  top: 0;
  z-index: 20;
}
.app-bar-inner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
}
.app-bar-icon {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--sky-100);
  border-radius: var(--radius-xs);
  font-size: 17px;
  flex-shrink: 0;
}
.app-bar-titles h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.3px;
}
.app-bar-titles p {
  margin: 1px 0 0;
  font-size: 12.5px;
  color: var(--slate-500);
}

/* ---------- View root ---------- */
.view-root {
  flex: 1;
  overflow-y: auto;
  padding-bottom: calc(78px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--white);
  border-top: 1px solid var(--slate-100);
  box-shadow: 0 -2px 10px rgba(15, 23, 42, 0.06);
  padding-bottom: var(--safe-bottom);
  z-index: 30;
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: none;
  background: none;
  padding: 10px 4px 8px;
  color: var(--slate-500);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
}
.nav-icon { font-size: 20px; filter: grayscale(1) opacity(0.55); transition: filter .15s; }
.nav-item.active { color: var(--slate-900); }
.nav-item.active .nav-icon { filter: none; }

/* ---------- Generic sections ---------- */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
}
.section-header h2 {
  font-size: 15.5px;
  font-weight: 700;
  margin: 0;
}
.link-btn {
  border: none;
  background: none;
  color: var(--sky-500);
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  padding: 6px 4px;
}

/* ---------- Home banner ---------- */
.home-banner {
  margin: 16px;
  padding: 20px;
  border-radius: 24px;
  background: linear-gradient(135deg, var(--slate-900), var(--emerald-500));
  box-shadow: var(--shadow-md);
  color: var(--white);
}
.home-banner-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(255,255,255,0.2);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 700;
}
.icon-btn {
  border: none;
  background: none;
  color: var(--white);
  font-size: 18px;
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { background: rgba(255,255,255,0.15); }
.home-banner h2 {
  margin: 14px 0 4px;
  font-size: 21px;
  font-weight: 800;
}
.home-banner p {
  margin: 0 0 14px;
  font-size: 13.5px;
  color: rgba(255,255,255,0.9);
}
.btn-white {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--white);
  color: var(--slate-900);
  border: none;
  border-radius: 14px;
  padding: 11px 16px;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
}

/* ---------- Cards ---------- */
.card {
  background: var(--white);
  border-radius: var(--radius-md);
  margin: 6px 16px;
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.empty-state {
  text-align: center;
  padding: 32px 20px;
  color: var(--slate-500);
}
.empty-state .emoji { font-size: 44px; display: block; margin-bottom: 12px; }
.empty-state h3 { margin: 0 0 6px; color: var(--slate-900); font-size: 16px; }
.empty-state p { margin: 0; font-size: 13.5px; line-height: 1.5; }

/* ---------- Task card ---------- */
.task-card {
  background: var(--white);
  border-radius: var(--radius-md);
  margin: 6px 16px;
  padding: 14px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--slate-100);
}
.task-card.completed { border-color: var(--emerald-100); }
.task-row { display: flex; align-items: flex-start; gap: 12px; }
.check-circle {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.2px solid var(--sky-500);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--white);
  font-size: 14px;
  margin-top: 2px;
}
.check-circle.done { background: var(--emerald-500); border-color: var(--emerald-500); }
.stop-badge {
  background: var(--slate-900);
  color: var(--white);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 8px;
  flex-shrink: 0;
  margin-top: 1px;
}
.task-main { flex: 1; min-width: 0; }
.task-title { font-weight: 700; font-size: 15px; margin: 0; }
.task-title.done { text-decoration: line-through; color: var(--slate-500); }
.task-desc { font-size: 12.5px; color: var(--slate-500); margin: 3px 0 0; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.menu-btn { border: none; background: none; font-size: 18px; color: var(--slate-500); cursor: pointer; padding: 4px; }
.docs-alert {
  margin-top: 10px;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.3);
  border-radius: var(--radius-xs);
  padding: 7px 10px;
  font-size: 11px;
  font-weight: 700;
  color: var(--amber-700);
  display: flex;
  gap: 6px;
  align-items: center;
}
.task-badges { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center; }
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 700;
  padding: 3px 8px; border-radius: 6px;
  background: var(--sky-100); color: var(--sky-500);
}
.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-plain { font-size: 11px; color: var(--slate-500); display: flex; align-items: center; gap: 3px; }
.location-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--emerald-100); color: var(--emerald-500);
  font-size: 11px; font-weight: 700;
  padding: 3px 8px; border-radius: 999px;
  cursor: pointer; border: none;
}

/* ---------- Document card ---------- */
.document-card {
  background: var(--white);
  border-radius: var(--radius-md);
  margin: 6px 16px;
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.doc-row { display: flex; align-items: flex-start; gap: 14px; }
.doc-icon {
  width: 42px; height: 42px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; flex-shrink: 0;
}
.doc-main { flex: 1; min-width: 0; }
.doc-title { font-weight: 700; font-size: 15px; margin: 0 0 4px; }
.doc-number-row { display: flex; align-items: center; gap: 6px; }
.doc-number { font-family: "SFMono-Regular", Consolas, monospace; font-weight: 700; font-size: 13.5px; }
.copy-btn { border: none; background: none; color: var(--slate-500); font-size: 15px; cursor: pointer; padding: 2px; }
.star-btn { border: none; background: none; font-size: 20px; cursor: pointer; padding: 4px; flex-shrink: 0; }
.link-tag {
  margin-top: 10px;
  background: var(--sky-100);
  border: 1px solid rgba(14,165,233,0.2);
  border-radius: var(--radius-xs);
  padding: 6px 10px;
  font-size: 12px; font-weight: 600; color: var(--sky-500);
  display: flex; align-items: center; gap: 6px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.doc-notes { margin-top: 8px; font-size: 12.5px; color: var(--slate-500); }

/* ---------- Search + filter chips ---------- */
.search-wrap { padding: 8px 16px; }
.search-input-wrap { position: relative; }
.search-input {
  width: 100%;
  background: var(--slate-100);
  border: none;
  border-radius: 14px;
  padding: 12px 14px 12px 40px;
  font-size: 14.5px;
  color: var(--slate-900);
}
.search-input:focus { outline: 2px solid var(--sky-500); outline-offset: -1px; }
.search-icon { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--slate-500); font-size: 15px; }
.search-clear { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); border: none; background: none; font-size: 15px; color: var(--slate-500); cursor: pointer; padding: 6px; }

.chip-row {
  display: flex; gap: 8px; overflow-x: auto;
  padding: 4px 16px 8px;
  scrollbar-width: none;
}
.chip-row::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  border: 1px solid var(--slate-300);
  background: var(--white);
  color: var(--slate-700);
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 13px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.chip.active {
  background: var(--sky-100);
  border-color: transparent;
  color: var(--slate-900);
  font-weight: 700;
}

/* ---------- Tabs ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--slate-100); padding: 0 8px; }
.tab-btn {
  flex: 1;
  border: none; background: none;
  padding: 12px 8px;
  font-size: 13.5px; font-weight: 700;
  color: var(--slate-500);
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.tab-btn.active { color: var(--slate-900); border-color: var(--sky-500); }

/* ---------- FAB ---------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(90px + var(--safe-bottom));
  background: var(--slate-900);
  color: var(--white);
  border: none;
  border-radius: 999px;
  box-shadow: var(--shadow-md);
  cursor: pointer;
  z-index: 25;
}
.fab.round { width: 56px; height: 56px; font-size: 24px; }
.fab.extended { display: flex; align-items: center; gap: 8px; padding: 14px 20px; font-size: 14px; font-weight: 700; }

/* ---------- Forms ---------- */
.form-page { padding: 16px; padding-bottom: 48px; }
.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--slate-700);
  margin-bottom: 6px;
}
.field input[type="text"],
.field input[type="number"],
.field input[type="datetime-local"],
.field textarea,
.field select {
  width: 100%;
  background: var(--slate-100);
  border: none;
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 14.5px;
  color: var(--slate-900);
}
.field textarea { resize: vertical; min-height: 64px; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--sky-500); outline-offset: -1px;
}
.field-error { color: var(--red-500); font-size: 11.5px; margin-top: 4px; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 2px;
}
.switch-row .switch-text strong { display: block; font-size: 14px; }
.switch-row .switch-text span { font-size: 12px; color: var(--slate-500); }
.switch {
  position: relative; width: 46px; height: 27px; flex-shrink: 0;
  background: var(--slate-300); border-radius: 999px; border: none; cursor: pointer;
  transition: background .15s;
}
.switch.on { background: var(--emerald-500); }
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 21px; height: 21px; border-radius: 50%; background: var(--white);
  transition: left .15s; box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.switch.on::after { left: 22px; }

.btn-primary {
  width: 100%;
  background: var(--slate-900);
  color: var(--white);
  border: none;
  border-radius: 14px;
  padding: 14px;
  font-size: 15px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer;
}
.btn-outline {
  background: none;
  border: 1.5px solid var(--emerald-500);
  color: var(--emerald-500);
  border-radius: 12px;
  padding: 9px 14px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn-dark {
  background: var(--slate-900);
  color: var(--white);
  border: none;
  border-radius: 12px;
  padding: 9px 14px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}

/* ---------- Form page header ---------- */
.form-header {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  padding-top: calc(14px + var(--safe-top));
  border-bottom: 1px solid var(--slate-100);
  position: sticky; top: 0; background: var(--white); z-index: 20;
}
.form-header h2 { flex: 1; margin: 0; font-size: 16.5px; font-weight: 800; }
.back-btn, .save-btn {
  border: none; background: none; font-size: 20px; cursor: pointer; padding: 4px 6px;
  color: var(--slate-900);
}

/* ---------- Map ---------- */
.map-wrap { position: relative; height: calc(100dvh - 70px - 78px - var(--safe-top) - var(--safe-bottom)); }
#map-canvas { width: 100%; height: 100%; z-index: 1; }
.offline-banner {
  position: absolute; top: 10px; left: 16px; right: 16px;
  background: var(--slate-900); color: var(--white);
  border-radius: 12px; padding: 10px 12px;
  display: flex; align-items: center; gap: 10px;
  font-size: 12px; z-index: 10; box-shadow: var(--shadow-md);
}
.offline-banner button { border: none; background: none; color: rgba(255,255,255,0.7); font-size: 15px; cursor: pointer; margin-left: auto; }
.recenter-btn {
  position: absolute; right: 16px; top: 16px; z-index: 10;
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--white); border: none; box-shadow: var(--shadow-sm);
  font-size: 17px; cursor: pointer;
}
.marker-pin {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--slate-900); border: 2.5px solid var(--white);
  color: var(--white); font-weight: 700; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 8px rgba(0,0,0,0.35);
}
.marker-pin.selected { background: var(--red-500); }
.marker-user {
  width: 42px; height: 42px; border-radius: 50%;
  background: rgba(14,165,233,0.25); border: 2.5px solid var(--sky-500);
  display: flex; align-items: center; justify-content: center; font-size: 18px;
}

.route-summary-card {
  position: absolute; left: 12px; right: 12px; bottom: 12px; z-index: 10;
  background: var(--white); border-radius: 20px; padding: 16px;
  box-shadow: var(--shadow-md); border: 1.5px solid rgba(14,165,233,0.3);
}
.route-summary-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.route-summary-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.route-summary-title .rs-icon {
  width: 38px; height: 38px; border-radius: 50%; background: var(--sky-100);
  display: flex; align-items: center; justify-content: center; font-size: 18px; flex-shrink: 0;
}
.route-summary-title h4 { margin: 0; font-size: 13.5px; font-weight: 700; }
.route-summary-title span { font-size: 11.5px; color: var(--slate-500); }
.route-summary-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.route-stats { display: flex; justify-content: space-around; margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--slate-100); }
.route-stat { text-align: center; }
.route-stat .val { font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 4px; justify-content: center; }
.route-stat .lbl { font-size: 10.5px; color: var(--slate-500); margin-top: 2px; }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,0.4);
  z-index: 40; opacity: 0; pointer-events: none; transition: opacity .18s;
}
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  background: var(--white); border-radius: 24px 24px 0 0;
  padding: 22px 20px calc(22px + var(--safe-bottom));
  max-height: 85dvh; overflow-y: auto;
  transform: translateY(100%); transition: transform .2s ease-out;
  box-shadow: 0 -8px 30px rgba(0,0,0,0.2);
}
.sheet.open { transform: translateY(0); }
.sheet-handle { width: 36px; height: 4px; background: var(--slate-300); border-radius: 999px; margin: 0 auto 16px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 16px; right: 16px;
  bottom: calc(90px + var(--safe-bottom));
  background: var(--slate-900); color: var(--white);
  padding: 12px 16px; border-radius: 12px; font-size: 13.5px; font-weight: 600;
  text-align: center; z-index: 60;
  transform: translateY(20px); opacity: 0; pointer-events: none;
  transition: all .2s ease-out;
  box-shadow: var(--shadow-md);
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.success { background: var(--emerald-500); }

/* ---------- Utility ---------- */
.row-between { display: flex; align-items: center; justify-content: space-between; }
.mt-8 { margin-top: 8px; }
.loading-spin {
  display: block; width: 28px; height: 28px; margin: 40px auto;
  border: 3px solid var(--slate-100); border-top-color: var(--sky-500);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-color-scheme: dark) {
  html, body { background: #0B1220; color: #E2E8F0; }
  .app-bar { background: #0B1220; border-color: #1E293B; }
  .task-card, .document-card, .card, .form-header, .route-summary-card, .sheet, .bottom-nav { background: #111C34; border-color: #1E293B; }
  .search-input, .field input, .field select, .field textarea { background: #1E293B; color: #E2E8F0; }
  .chip { background: #111C34; border-color: #334155; color: #CBD5E1; }
  .app-bar-titles p, .task-desc, .doc-notes, .badge-plain, .switch-row .switch-text span, .empty-state, .route-stat .lbl, .route-summary-title span { color: #94A3B8; }
}
