/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  background: #f1f5f9;
  color: #1e293b;
  overflow: hidden;
}

/* ── Layout ── */
body {
  display: flex;
  height: 100dvh;
}

#map {
  flex: 1;
  height: 100%;
  z-index: 0;
}

/* ── Sidebar (Plan Mode) ── */
#sidebar {
  width: 300px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #fff;
  border-right: 1px solid #e2e8f0;
  z-index: 10;
  overflow: hidden;
}

.sidebar-header {
  padding: 14px 16px 10px;
  border-bottom: 1px solid #e2e8f0;
  background: #1e40af;
  color: #fff;
}

.header-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sidebar-header h1 {
  font-size: 17px;
  font-weight: 700;
}

.btn-logout {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  font-size: 14px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  flex-shrink: 0;
}
.btn-logout:hover { background: rgba(255,255,255,0.3); }

.header-mode-btns {
  display: flex;
  gap: 6px;
}
.header-mode-btns .btn {
  flex: 1;
  padding: 6px 8px;
  font-size: 12px;
}

.sidebar-actions {
  padding: 12px 16px;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#route-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid #e2e8f0;
  background: #f8fafc;
}

#route-picker {
  flex: 1;
  font-size: 12px;
  padding: 5px 8px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: #fff;
  color: #1e293b;
  cursor: pointer;
  min-width: 0;
}

.btn-icon {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  background: #fff;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.btn-icon:hover { background: #f1f5f9; }
.btn-icon-danger:hover { background: #fee2e2; border-color: #fca5a5; }

#stat-route-name {
  margin-left: auto;
  font-size: 11px;
  font-weight: 700;
  color: #7c3aed;
  background: #ede9fe;
  padding: 2px 7px;
  border-radius: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100px;
}

#stop-list-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

#stop-list-header {
  padding: 8px 16px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  font-size: 12px;
  color: #64748b;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

#stat-distance { margin-left: auto; font-weight: 600; color: #1e40af; }

#split-stats {
  display: flex;
  gap: 8px;
  padding: 6px 16px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  font-size: 12px;
  font-weight: 600;
}
.split-stat { flex: 1; text-align: center; padding: 3px 6px; border-radius: 5px; }
.p1-stat { background: #dbeafe; color: #1d4ed8; }
.p2-stat { background: #ffedd5; color: #c2410c; }

#stop-list {
  list-style: none;
  padding: 4px 0;
}

.stop-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  border-bottom: 1px solid #f1f5f9;
  cursor: pointer;
  transition: background 0.1s;
}
.stop-item:hover { background: #f8fafc; }
.stop-item.disabled { opacity: 0.45; }
.stop-item.no-delivery { opacity: 0.6; }

.stop-badge {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #2563eb;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.stop-badge.p2  { background: #f97316; }
.stop-badge.nd  { background: #94a3b8; border-radius: 3px; }
.stop-badge.dis { background: #cbd5e1; }

.stop-name {
  flex: 1;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stop-dir {
  font-size: 14px;
  width: 18px;
  text-align: center;
  flex-shrink: 0;
  color: #64748b;
}
.stop-dir-cross {
  color: #dc2626;
  font-weight: 700;
}

.stop-item input[type="checkbox"] { flex-shrink: 0; cursor: pointer; }

.stop-del {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 15px;
  padding: 2px 4px;
  border-radius: 4px;
  line-height: 1;
}
.stop-del:hover { color: #ef4444; background: #fee2e2; }

.stop-nd-toggle {
  background: none;
  border: 1px solid #e2e8f0;
  color: #64748b;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 5px;
  line-height: 1.4;
  flex-shrink: 0;
}
.stop-nd-toggle:hover     { background: #fee2e2; color: #dc2626; border-color: #fca5a5; }
.stop-nd-toggle.is-nd     { background: #fee2e2; color: #dc2626; border-color: #fca5a5; }
.stop-nd-toggle.is-nd:hover { background: #dcfce7; color: #166534; border-color: #86efac; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid #e2e8f0;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  width: 100%;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary  { background: #2563eb; color: #fff; }
.btn-primary:hover:not(:disabled)  { background: #1d4ed8; }
.btn-secondary { background: #e2e8f0; color: #334155; }
.btn-secondary:hover:not(:disabled) { background: #cbd5e1; }
.btn-danger   { background: #fee2e2; color: #ef4444; }
.btn-danger:hover:not(:disabled) { background: #fecaca; }
.btn-warning  { background: #fef9c3; color: #854d0e; }
.btn-warning:hover:not(:disabled) { background: #fef08a; }

#bulk-toolbar {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
  padding: 6px 10px;
  background: #fefce8;
  border: 1px solid #fde68a;
  border-radius: 8px;
  font-size: 12px;
}
#bulk-count {
  flex: 1;
  min-width: 80px;
  color: #92400e;
  font-weight: 600;
}
.btn-sm { padding: 4px 8px; font-size: 11px; width: auto; }
.btn-sm-p1   { padding: 4px 8px; font-size: 11px; width: auto; background: #dbeafe; color: #1d4ed8; border: 1px solid #93c5fd; border-radius: 6px; font-weight: 600; cursor: pointer; }
.btn-sm-p1:hover:not(:disabled)   { background: #bfdbfe; }
.btn-sm-p2   { padding: 4px 8px; font-size: 11px; width: auto; background: #ffedd5; color: #c2410c; border: 1px solid #fdba74; border-radius: 6px; font-weight: 600; cursor: pointer; }
.btn-sm-p2:hover:not(:disabled)   { background: #fed7aa; }
.btn-sm-auto { padding: 4px 8px; font-size: 11px; width: auto; background: #f1f5f9; color: #475569; border: 1px solid #cbd5e1; border-radius: 6px; font-weight: 600; cursor: pointer; }
.btn-sm-auto:hover:not(:disabled) { background: #e2e8f0; }
.btn-sm-p1:disabled, .btn-sm-p2:disabled, .btn-sm-auto:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Toggle row ── */
.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  padding: 2px 0;
}
.toggle-row input { cursor: pointer; }

/* ── Progress bar ── */
.progress-bar {
  position: relative;
  height: 24px;
  background: #e2e8f0;
  border-radius: 6px;
  overflow: hidden;
}
#optimise-bar {
  height: 100%;
  width: 0%;
  background: #2563eb;
  transition: width 0.2s;
}
#optimise-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: #1e40af;
}

/* ── Marker icons ── */
.stop-marker {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid #1e40af;
  background: #2563eb;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
.stop-marker.p2       { background: #f97316; border-color: #c2410c; }
.stop-marker.nd       { background: #94a3b8; border-color: #475569; border-radius: 4px; }
.stop-marker.dis      { background: #cbd5e1; border-color: #94a3b8; color: #64748b; }
.stop-marker.depot    { width: 34px; height: 34px; background: #7c3aed; border-color: #4c1d95; font-size: 14px; }
.stop-marker.selected { outline: 3px solid #facc15; outline-offset: 2px; }
.stop-marker.pinned   { outline: 2px solid #f97316; outline-offset: 2px; }

/* ── Sequences panel ─────────────────────────────────────────────────────── */
#sequences-panel { border-bottom: 1px solid #e2e8f0; }
.sequences-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 6px 12px;
  background: #f8fafc; border-bottom: 1px solid #e2e8f0;
}
.sequences-title { font-size: 11px; font-weight: 700; color: #64748b; text-transform: uppercase; letter-spacing: 0.5px; }
#sequences-list { list-style: none; padding: 2px 0; }
.sequence-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-bottom: 1px solid #f1f5f9; font-size: 13px;
}
.sequence-name { flex: 1; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sequence-count { font-size: 11px; color: #94a3b8; flex-shrink: 0; }
.sequence-btn { background: none; border: none; cursor: pointer; padding: 2px 5px; border-radius: 3px; font-size: 13px; }
.sequence-btn-load { color: #2563eb; }
.sequence-btn-load:hover { background: #dbeafe; }
.sequence-btn-del { color: #94a3b8; }
.sequence-btn-del:hover { color: #ef4444; background: #fee2e2; }
.sequences-empty { padding: 8px 12px; font-size: 12px; color: #94a3b8; font-style: italic; }

/* GPS dot (pulsing) */
.gps-pulse {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #3b82f6;
  border: 3px solid #fff;
  box-shadow: 0 0 0 0 rgba(59,130,246,0.6);
  animation: pulse 1.8s infinite;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(59,130,246,0.6); }
  70%  { box-shadow: 0 0 0 14px rgba(59,130,246,0); }
  100% { box-shadow: 0 0 0 0 rgba(59,130,246,0); }
}

/* ── Mode visibility ── */
body.mode-plan   #sidebar       { display: flex; }
body.mode-plan   #record-panel  { display: none !important; }
body.mode-plan   #walk-panel    { display: none !important; }
body.mode-record #sidebar       { display: none; }
body.mode-record #record-panel  { display: flex !important; }
body.mode-record #walk-panel    { display: none !important; }
body.mode-walk   #sidebar       { display: flex; }
body.mode-walk   #walk-panel    { display: flex !important; }
body.mode-walk   #record-panel  { display: none !important; }
/* hide plan-only controls while walking */
body.mode-walk   #btn-optimise  { display: none; }
body.mode-walk   #btn-lock      { display: none; }
body.mode-walk   #btn-edit-paths { display: none; }
body.mode-walk   #btn-clear     { display: none; }
body.mode-walk   #btn-switch-walk   { display: none; }
body.mode-walk   #btn-switch-record { display: none; }

#record-panel, #walk-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  background: #fff;
  border-top: 1px solid #e2e8f0;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
  flex-direction: column;
  padding: 12px 16px 20px;
  gap: 12px;
}

.record-header {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  font-weight: 600;
}
.record-title { color: #dc2626; }
#rec-stop-count { color: #64748b; margin-left: auto; }

.gps-dot {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
}
.gps-dot::before { content: ''; width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.gps-waiting { background: #fef9c3; color: #854d0e; }
.gps-waiting::before { background: #eab308; }
.gps-good    { background: #dcfce7; color: #166534; }
.gps-good::before { background: #22c55e; }
.gps-poor    { background: #fee2e2; color: #991b1b; }
.gps-poor::before { background: #ef4444; }
.gps-none    { background: #f1f5f9; color: #64748b; }
.gps-none::before { background: #94a3b8; }

.record-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.rec-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 18px 12px;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  font-weight: 700;
  transition: transform 0.1s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.rec-btn:active:not(:disabled) { transform: scale(0.96); }
.rec-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.rec-btn-icon  { font-size: 32px; line-height: 1; }
.rec-btn-label { font-size: 14px; }

.rec-btn-add  { background: #dcfce7; color: #166534; }
.rec-btn-add:hover:not(:disabled)  { background: #bbf7d0; }
.rec-btn-skip { background: #fee2e2; color: #991b1b; }
.rec-btn-skip:hover:not(:disabled) { background: #fecaca; }

.record-footer {
  display: flex;
  gap: 8px;
}
.record-footer .btn { flex: 1; }

#btn-recentre {
  width: auto;
  flex: none;
  padding: 8px 12px;
}

/* ── Routing status ── */
#routing-status {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 12px;
  color: #64748b;
  z-index: 50;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.routing-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid #cbd5e1;
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Edit Paths UI ── */
#edit-paths-hint {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: #7c3aed;
  color: #fff;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 12px;
  font-weight: 600;
  z-index: 50;
  pointer-events: none;
  white-space: nowrap;
}

#via-edit-panel {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 12px 16px;
  z-index: 55;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  min-width: 260px;
  max-width: 340px;
}

.via-edit-header {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 8px;
}

.via-edit-title {
  font-weight: 700;
  font-size: 13px;
  color: #7c3aed;
}

.via-edit-hint {
  font-size: 11px;
  color: #64748b;
}

.via-edit-buttons {
  display: flex;
  gap: 6px;
}

.via-edit-buttons .btn {
  flex: 1;
  padding: 6px 10px;
  font-size: 12px;
}

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1e293b;
  color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  z-index: 100;
  white-space: nowrap;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Record mode — map takes full screen, crosshair cursor for click-to-add ── */
body.mode-record #map {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

/* ── Popup styles ── */
.popup-count {
  width: 60px;
  padding: 2px 4px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 13px;
}
.popup-order {
  font-size: 11px;
  color: #6b7280;
  margin-bottom: 6px;
}
.popup-name {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 13px;
  margin-bottom: 8px;
}
.popup-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.popup-row label { font-size: 12px; display: flex; align-items: center; gap: 5px; cursor: pointer; }
.popup-row select {
  font-size: 12px;
  padding: 2px 6px;
  border: 1px solid #cbd5e1;
  border-radius: 5px;
}
.popup-del {
  margin-top: 4px;
  width: 100%;
  padding: 5px;
  background: #fee2e2;
  color: #dc2626;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}
.popup-del:hover { background: #fecaca; }
.popup-depot-btn {
  width: 100%;
  padding: 5px;
  background: #ede9fe;
  color: #7c3aed;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  margin-top: 4px;
}
.popup-depot-btn:hover { background: #ddd6fe; }

.popup-person-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 4px 0;
}
.popup-person-label {
  font-size: 11px;
  color: #64748b;
  margin-right: 2px;
  white-space: nowrap;
}
.popup-person-btn {
  flex: 1;
  padding: 4px 2px;
  border: 1px solid #e2e8f0;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  background: #f8fafc;
  color: #64748b;
}
.popup-person-btn.active-p1  { background: #dbeafe; color: #1d4ed8; border-color: #93c5fd; }
.popup-person-btn.active-p2  { background: #ffedd5; color: #c2410c; border-color: #fdba74; }
.popup-person-btn.active-auto { background: #f1f5f9; color: #334155; border-color: #cbd5e1; }

/* ── Responsive ── */
@media (max-width: 600px) {
  body.mode-plan #sidebar { width: 100%; position: fixed; bottom: 0; left: 0; right: 0; height: 45dvh; border-right: none; border-top: 1px solid #e2e8f0; z-index: 20; }
  body.mode-plan #map { height: 55dvh; }
}
