* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:      #1a1a1a;
  --bg2:     #242424;
  --bg3:     #2e2e2e;
  --border:  #3a3a3a;
  --text:    #f0f0f0;
  --text2:   #aaa;
  --accent:  #f5a623;
  --accent2: #e09510;
  --danger:  #e74c3c;
  --success: #27ae60;
  --radius:  10px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ── Screens ─────────────────────────────────────────────────────────────── */
.screen { display: none; flex-direction: column; }
.screen.active { display: flex; }

.screen-home {
  min-height: 100dvh;
  justify-content: center;
  align-items: center;
  gap: 18px;
  padding: 40px 24px;
  text-align: center;
}

.screen-fixed {
  height: 100dvh;
  overflow: hidden;
}

/* ── Common ──────────────────────────────────────────────────────────────── */
h1 { font-size: 1.8rem; font-weight: 700; color: var(--accent); }
h2 { font-size: 1.15rem; font-weight: 600; flex: 1; }

.badge {
  background: var(--accent);
  color: #111;
  font-size: .75rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 20px;
  white-space: nowrap;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: opacity 0.15s, transform 0.1s;
  -webkit-user-select: none;
  user-select: none;
}
.btn:active { transform: scale(0.97); opacity: 0.9; }
.btn:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }

.btn-primary { background: var(--accent); color: #111; }
.btn-primary:hover { background: var(--accent2); }

.btn-secondary { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }

.btn-danger-soft { background: var(--bg3); color: #e88; border: 1px solid #633; }

.btn-sm {
  min-height: 36px;
  padding: 6px 14px;
  font-size: .85rem;
  width: auto;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text2);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 8px;
  line-height: 1;
  transition: color 0.15s;
  -webkit-user-select: none;
  user-select: none;
}
.btn-icon:hover { color: var(--text); }

/* ── Screen header / footer ──────────────────────────────────────────────── */
.screen-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.screen-footer {
  padding: 12px 16px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

/* ── Home ────────────────────────────────────────────────────────────────── */
.home-logo { font-size: 3.5rem; line-height: 1; }
.home-subtitle { color: var(--text2); font-size: .95rem; }
.home-info {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 18px;
  font-size: .9rem;
  color: var(--text2);
  width: 100%;
  max-width: 380px;
}
.home-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 380px;
}

/* ── Address search ──────────────────────────────────────────────────────── */
.search-wrap {
  padding: 10px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.addr-input {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: .95rem;
  padding: 11px 14px;
  outline: none;
  transition: border-color 0.15s;
}
.addr-input:focus { border-color: var(--accent); }
.addr-input::placeholder { color: var(--text2); }

/* Global fixed dropdown */
.addr-dropdown-global {
  position: fixed;
  z-index: 9999;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  overflow-y: auto;
  max-height: 260px;
  display: none;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
.addr-dropdown-global.open { display: block; }

.addr-result {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.addr-result:last-child { border-bottom: none; }
.addr-result:hover, .addr-result.focused { background: var(--bg3); }

.addr-result-top    { display: flex; align-items: baseline; gap: 6px; }
.addr-result-road   { font-size: .92rem; font-weight: 600; color: var(--text); line-height: 1.3; flex: 1; min-width: 0; }
.addr-result-loc    { font-size: .82rem; color: var(--accent); margin-top: 3px; font-weight: 500; }
.addr-result-county { font-size: .75rem; color: var(--text2); margin-top: 1px; }

.result-type-badge {
  font-size: .68rem;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 8px;
  white-space: nowrap;
  flex-shrink: 0;
}
.result-type-badge.building { background: #1a3a2a; color: #5ddb9a; }
.result-type-badge.street   { background: #2a2a2a; color: #888; }

/* ── Packages list ───────────────────────────────────────────────────────── */
.packages-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.empty-msg {
  color: var(--text2);
  font-size: .9rem;
  text-align: center;
  padding: 24px 0;
}

.pkg-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.pkg-item.editing {
  flex-wrap: wrap;
  gap: 8px;
}

.pkg-num {
  font-size: .85rem;
  font-weight: 700;
  color: var(--text2);
  width: 22px;
  text-align: center;
  flex-shrink: 0;
}

.pkg-addr-wrap {
  flex: 1;
  min-width: 0;
}
.pkg-addr-main {
  font-size: .9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pkg-edit-wrap {
  flex: 1;
  min-width: 0;
}

.pkg-coord-status {
  font-size: .72rem;
  margin-top: 3px;
}
.pkg-coord-status.precise { color: #5ddb9a; }
.pkg-coord-status.approx  { color: #888; }

.btn-pin { opacity: 0.45; transition: opacity 0.15s; }
.btn-pin.pin-active { opacity: 1; }

/* ── Route sections ──────────────────────────────────────────────────────── */
.route-stale-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: #3a2800;
  border-bottom: 1px solid #6a4800;
  padding: 10px 14px;
  font-size: .85rem;
  color: var(--accent);
  flex-shrink: 0;
}

.route-sections {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.route-section { margin-bottom: 4px; }

.route-section-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 10px 14px;
  font-size: .9rem;
  font-weight: 700;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.loading-header { color: #7ec8e3; }
.delivery-header { color: #f5a623; }
.section-sub {
  font-size: .73rem;
  font-weight: 400;
  color: var(--text2);
}

/* ── Route stop rows ─────────────────────────────────────────────────────── */
.route-stop {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}

.loading-stop { background: var(--bg); }
.loading-stop .stop-num { color: #7ec8e3; }

.stop-num {
  font-size: .85rem;
  font-weight: 700;
  color: var(--accent);
  width: 22px;
  text-align: center;
  flex-shrink: 0;
  padding-top: 2px;
}

.stop-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stop-top {
  display: flex;
  align-items: flex-start;
  gap: 6px;
}

.stop-addr {
  flex: 1;
  font-size: .9rem;
  line-height: 1.3;
}
.stop-delivery-ref {
  font-size: .75rem;
  color: var(--text2);
  margin-top: 2px;
}

.stop-badge {
  font-size: .75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 12px;
  width: fit-content;
}
.stop-badge.navigating { background: #1a4a6a; color: #7ec8e3; }
.stop-badge.done       { background: #1a3a2a; color: #5ddb9a; }
.stop-badge.skipped    { background: #2a2a2a; color: #888; }

.stop-actions {
  display: flex;
  gap: 6px;
}

.btn-status {
  flex: 1;
  border: none;
  border-radius: 8px;
  padding: 7px 6px;
  font-size: .78rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--bg3);
  color: var(--text2);
  transition: background 0.15s, color 0.15s;
  -webkit-user-select: none;
  user-select: none;
}
.btn-status:active { transform: scale(0.96); }
.btn-status.done-btn.active  { background: #1a3a2a; color: #5ddb9a; }
.btn-status.skip-btn.active  { background: #2a2a2a; color: #888; }

.btn-nav {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg3);
  color: var(--text2);
  font-size: .8rem;
  font-weight: 600;
  padding: 8px 10px;
  cursor: pointer;
  flex-shrink: 0;
  align-self: flex-start;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  -webkit-user-select: none;
  user-select: none;
}
.btn-nav:active { transform: scale(0.96); }
.btn-nav.active { background: #1a3050; color: #7ec8e3; border-color: #2a5080; }

/* Stop status tinting */
.stop-done     { opacity: 0.55; }
.stop-done .stop-addr { text-decoration: line-through; }
.stop-skipped  { opacity: 0.45; }
.stop-navigating { border-left: 3px solid #3498db; }
.stop-editing  { background: #1e1e2e; }

/* Inline edit in route stop */
.stop-edit-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.stop-edit-cancel {
  font-size: .75rem;
  color: var(--text2);
  flex-shrink: 0;
}

/* ── Map screen ──────────────────────────────────────────────────────────── */
.screen-map #leaflet-map {
  flex: 1;
  background: #2a2a2a;
}

#leaflet-map .leaflet-popup-content-wrapper,
#leaflet-map .leaflet-popup-tip {
  background: var(--bg2);
  color: var(--text);
  border: 1px solid var(--border);
}
#leaflet-map .leaflet-popup-content-wrapper {
  box-shadow: 0 4px 16px rgba(0,0,0,.5);
}

/* Map markers */
.map-marker {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid #111;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .8rem;
  font-weight: 700;
  color: #111;
}

/* Cluster icons */
.cluster-icon {
  width: 40px;
  height: 40px;
  background: var(--accent);
  border: 2px solid #111;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .85rem;
  font-weight: 700;
  color: #111;
  box-shadow: 0 2px 8px rgba(0,0,0,.4);
}
.cluster-icon-lg {
  width: 48px;
  height: 48px;
  font-size: 1rem;
}

/* Override default Leaflet cluster styles for dark theme */
.marker-cluster-small  { background: rgba(245,166,35,.25); }
.marker-cluster-medium { background: rgba(245,166,35,.35); }
.marker-cluster-large  { background: rgba(245,166,35,.45); }
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background: rgba(245,166,35,.8);
  color: #111;
  font-weight: 700;
}

/* ── Toast ───────────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #333;
  color: var(--text);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: .88rem;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 8000;
  border: 1px solid var(--border);
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Loading overlay ─────────────────────────────────────────────────────── */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.75);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 9000;
  color: var(--text2);
  font-size: .9rem;
}
#loading-overlay.visible { display: flex; }

.spin-ring {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── SweetAlert2 overrides ───────────────────────────────────────────────── */
.swal2-popup { background: #242424 !important; color: #f0f0f0 !important; }
.swal2-title, .swal2-html-container { color: #f0f0f0 !important; }
