/* ── Route Optimizer – Design Tokens ────────────────────────── */
:root {
  --ro-primary: #2563eb;
  --ro-primary-dark: #1d4ed8;
  --ro-primary-soft: #e0ecff;
  --ro-surface: #ffffff;
  --ro-surface-alt: #f8fafc;
  --ro-border: #e2e8f0;
  --ro-text: #0f172a;
  --ro-text-muted: #475569;
  --ro-success: #10b981;
  --ro-warning: #f59e0b;
  --ro-danger: #ef4444;
  --ro-shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.08);
  --ro-shadow-md: 0 6px 20px rgba(15, 23, 42, 0.12);
  --ro-shadow-lg: 0 20px 60px rgba(15, 23, 42, 0.18);
  --ro-radius: 16px;
  --ro-radius-sm: 10px;
  --ro-font: 'Inter', 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* ── Overlay ────────────────────────────────────────────────── */
#route-optimizer-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(120deg, rgba(37, 99, 235, 0.12), rgba(20, 184, 166, 0.12));
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 2000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 200ms ease;
  font-family: var(--ro-font);
  overflow: hidden;
  touch-action: manipulation;
}

#route-optimizer-overlay.ro-open {
  opacity: 1;
  pointer-events: auto;
}

body.ro-body-lock {
  overflow: hidden !important;
  width: 100%;
}

/* iOS Safari: position:fixed on body kills nested scroll containers.
   Only use it on desktop where it prevents background bounce. */
@media (min-width: 641px) {
  body.ro-body-lock {
    position: fixed;
    top: 0;
    left: 0;
  }
}

/* ── Shell (main container) ─────────────────────────────────── */
#route-optimizer-overlay .ro-shell {
  width: min(1480px, 98vw);
  height: min(92vh, 92dvh);
  background: var(--ro-surface);
  border: 1px solid var(--ro-border);
  border-radius: var(--ro-radius);
  box-shadow: var(--ro-shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

/* ── Fullscreen mode ────────────────────────────────────────── */
#route-optimizer-overlay.ro-fullscreen {
  padding: 0;
  align-items: stretch;
  justify-content: stretch;
}

#route-optimizer-overlay.ro-fullscreen .ro-shell {
  width: 100%;
  max-width: none;
  height: 100%;
  min-height: 0;
  max-height: none;
  border-radius: 0;
  border: none;
  box-shadow: none;
}

/* Explicit mobile class applied from JS so we can force a true full-screen layout
   when the optimizer is opened from the fleet dashboard on phones. */
#route-optimizer-overlay.ro-mobile {
  position: relative;
  inset: auto;
  padding: 0;
  display: block;
  overflow: visible;
}

#route-optimizer-overlay.ro-mobile .ro-shell {
  width: 100%;
  max-width: 100%;
  height: auto;
  min-height: 100vh;
  min-height: 100dvh;
  max-height: none;
  border-radius: 0;
  border: none;
  box-shadow: none;
  overflow: visible;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ── Header ─────────────────────────────────────────────────── */
.ro-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--ro-border);
  background: linear-gradient(120deg, #f8fafc, #eef2ff);
  flex-shrink: 0;
}

.ro-title-block {
  display: flex;
  gap: 12px;
  align-items: center;
}

.ro-title-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: #1d4ed8;
  color: #fff;
  box-shadow: var(--ro-shadow-sm);
  flex-shrink: 0;
}

.ro-title h2 {
  margin: 0;
  font-size: 18px;
  color: var(--ro-text);
}

.ro-title p {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--ro-text-muted);
}

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

.ro-expand-btn,
.ro-close-btn {
  border: 1px solid var(--ro-border);
  background: #fff;
  color: var(--ro-text);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, border-color 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.ro-expand-btn:hover,
.ro-close-btn:hover {
  transform: translateY(-1px);
  border-color: #cbd5e1;
  box-shadow: var(--ro-shadow-sm);
}

/* ── Toolbar ────────────────────────────────────────────────── */
.ro-toolbar {
  padding: 0;
  border-bottom: 1px solid var(--ro-border);
  background: var(--ro-surface);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 5;
}

/* Toolbar Row 1: controls + actions + filters */
.ro-toolbar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  flex-wrap: wrap;
}

.ro-toolbar-controls {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-shrink: 0;
}

.ro-toolbar-controls .ro-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.ro-toolbar-controls .ro-input,
.ro-toolbar-controls .ro-select {
  padding: 7px 10px;
  font-size: 13px;
}

.ro-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-left: auto;
}

.ro-toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--ro-border);
  flex-shrink: 0;
}

/* ── Buttons ────────────────────────────────────────────────── */
.ro-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ro-label {
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ro-text-muted);
  font-weight: 700;
}

.ro-input, .ro-select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--ro-border);
  border-radius: 8px;
  background: #fff;
  color: var(--ro-text);
  font-family: var(--ro-font);
  font-weight: 600;
  font-size: 13px;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.ro-input:focus, .ro-select:focus {
  outline: none;
  border-color: var(--ro-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.ro-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: var(--ro-primary);
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
  box-shadow: var(--ro-shadow-sm);
  white-space: nowrap;
}

.ro-btn.secondary {
  background: #fff;
  color: var(--ro-text);
  border-color: var(--ro-border);
}

.ro-btn.ghost {
  background: transparent;
  border-color: var(--ro-border);
  color: var(--ro-text-muted);
  box-shadow: none;
}

.ro-btn.ghost.active {
  background: var(--ro-primary-soft);
  border-color: var(--ro-primary);
  color: var(--ro-primary);
}

.ro-btn:hover {
  transform: translateY(-1px);
  box-shadow: var(--ro-shadow-md);
}

.ro-btn:active {
  transform: translateY(0);
}

.ro-settings-chevron {
  font-size: 10px;
  transition: transform 0.2s ease;
}

.ro-btn.ghost.active .ro-settings-chevron {
  transform: rotate(180deg);
}

/* ── Settings Panel ─────────────────────────────────────────── */
.ro-settings-panel {
  padding: 14px 20px 12px;
  border-top: 1px solid var(--ro-border);
  background: var(--ro-surface-alt);
  transition: max-height 0.3s ease, opacity 0.2s ease, padding 0.3s ease;
  max-height: 500px;
  overflow: hidden;
  opacity: 1;
}

.ro-settings-panel.ro-hidden {
  max-height: 0;
  opacity: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-top-color: transparent;
  pointer-events: none;
}

.ro-settings-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.ro-setting {
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--ro-surface);
  border: 1px solid var(--ro-border);
  border-radius: 10px;
  padding: 12px 14px;
}

.ro-slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.ro-slider {
  flex: 1;
  accent-color: var(--ro-primary);
  cursor: pointer;
  height: 6px;
  border-radius: 3px;
}

.ro-slider-val {
  font-weight: 700;
  min-width: 54px;
  font-size: 14px;
  text-align: right;
  color: var(--ro-primary);
}

.ro-setting-hint {
  font-size: 11px;
  color: var(--ro-text-muted);
  line-height: 1.4;
}

.ro-setting--full {
  grid-column: 1 / -1;
}

.ro-radio-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.ro-radio-opt {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 12px;
  border: 1px solid var(--ro-border);
  border-radius: 20px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  user-select: none;
}

.ro-radio-opt input[type="radio"] {
  display: none;
}

.ro-radio-opt:has(input:checked) {
  background: var(--ro-primary);
  border-color: var(--ro-primary);
  color: #fff;
}

.ro-settings-footer {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 0 0;
  margin-top: 10px;
  border-top: 1px solid var(--ro-border);
  font-size: 12px;
  color: var(--ro-text-muted);
}

.ro-settings-footer em {
  font-style: normal;
  opacity: 0.7;
}

/* ── Toggle group (Pickup / Dropoff filters) ────────────────── */
.ro-toggle-group {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.ro-toggle {
  border: 1px solid var(--ro-border);
  background: #fff;
  color: var(--ro-text);
  padding: 6px 10px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.ro-toggle.active {
  background: var(--ro-primary-soft);
  border-color: #cbd5e1;
  color: var(--ro-primary);
}

/* ── Content (sidebar + map) ────────────────────────────────── */
.ro-content {
  flex: 1;
  display: grid;
  grid-template-columns: var(--ro-sidebar-width, 380px) auto 1fr;
  gap: 0;
  min-height: 0;
  overflow: hidden;
}

.ro-sidebar {
  border-right: none;
  background: var(--ro-surface);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-y: auto;
}

/* ── Draggable resize handle ──────────────────────────────────── */
.ro-resize-handle {
  width: 8px;
  cursor: col-resize;
  background: var(--ro-border);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  position: relative;
  z-index: 5;
  flex-shrink: 0;
}

.ro-resize-handle:hover,
.ro-resize-handle.dragging {
  background: var(--ro-primary);
}

.ro-resize-grip {
  width: 4px;
  height: 32px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.7);
  pointer-events: none;
}

.ro-resize-handle:hover .ro-resize-grip,
.ro-resize-handle.dragging .ro-resize-grip {
  background: rgba(255, 255, 255, 0.9);
}

/* Prevent text selection and pointer events on map while dragging */
.ro-content.resizing {
  user-select: none;
  -webkit-user-select: none;
}

.ro-content.resizing .ro-main {
  pointer-events: none;
}

.ro-main {
  position: relative;
  background: var(--ro-surface-alt);
  min-height: 0;
  overflow: hidden;
}

/* ── Loading / Empty / Toast ────────────────────────────────── */
.ro-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(2px);
  z-index: 10;
  font-weight: 700;
  color: var(--ro-primary);
}

.ro-empty {
  padding: 20px;
  text-align: center;
  color: var(--ro-text-muted);
}

#ro-toast {
  background: var(--ro-primary);
  color: #fff;
  border-radius: 12px;
  padding: 12px 14px;
  font-weight: 700;
  box-shadow: var(--ro-shadow-md);
}
#ro-toast.show {
  animation: ro-toast 2.2s ease;
}

@keyframes ro-toast {
  0% { transform: translateY(8px); opacity: 0; }
  15% { transform: translateY(0); opacity: 1; }
  85% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(6px); opacity: 0; }
}


/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
   ═══════════════════════════════════════════════════════════════ */

/* ── Large tablets / small laptops (≤1200px) ────────────────── */
@media (max-width: 1200px) {
  .ro-settings-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ── Tablets (≤1024px): sidebar above map ───────────────────── */
@media (max-width: 1024px) {
  #route-optimizer-overlay {
    padding: 8px;
    align-items: stretch;
  }

  #route-optimizer-overlay .ro-shell {
    width: 100%;
    max-width: 100%;
    height: min(96vh, 96dvh);
    min-height: 0;
  }

  .ro-header {
    padding: 10px 14px;
  }

  .ro-title h2 { font-size: 16px; }
  .ro-title p { font-size: 12px; }

  .ro-toolbar-row {
    padding: 8px 14px;
  }

  .ro-settings-panel {
    padding: 12px 14px 10px;
  }

  .ro-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  .ro-resize-handle {
    display: none;
  }

  .ro-sidebar {
    flex: 0 0 auto;
    max-height: 38vh;
    min-height: 0;
    overflow-y: auto;
    border-right: none;
    border-bottom: 1px solid var(--ro-border);
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .ro-main {
    flex: 1;
    min-height: 220px;
    overflow: hidden;
  }
}

/* ── Mobile phones (≤640px): scrollable full-page layout ─────── */
@media (max-width: 640px) {
  #route-optimizer-overlay {
    position: relative;             /* break out of fixed so body scrolls */
    inset: auto;
    padding: 0;
    display: block;
    overflow: visible;
    -webkit-overflow-scrolling: touch;
  }

  #route-optimizer-overlay .ro-shell {
    width: 100%;
    max-width: 100%;
    min-height: 100vh;
    min-height: 100dvh;
    height: auto;                   /* grow with content instead of fixed viewport */
    max-height: none;
    border-radius: 0;
    border: none;
    overflow: visible;              /* let content flow naturally */
    display: flex;
    flex-direction: column;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  #route-optimizer-overlay.ro-fullscreen .ro-shell {
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  /* Sticky header: stays visible while scrolling */
  .ro-header {
    padding: 8px 12px;
    position: sticky;
    top: 0;
    z-index: 20;
    background: linear-gradient(120deg, #f8fafc, #eef2ff);
  }

  .ro-title-block { gap: 8px; }

  .ro-title-icon {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    font-size: 14px;
  }

  .ro-title h2 {
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .ro-title p { display: none; }

  .ro-header-actions { gap: 6px; }

  .ro-expand-btn,
  .ro-close-btn {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    flex-shrink: 0;
  }

  /* Toolbar: flows naturally so the page scrolls past it to the map */
  .ro-toolbar {
    position: relative;
    z-index: 19;
    background: var(--ro-surface);
  }

  /* Toolbar: stack vertically */
  .ro-toolbar-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 8px 12px;
  }

  .ro-toolbar-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
  }

  .ro-toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-left: 0;
    width: 100%;
  }

  .ro-toolbar-divider { display: none; }

  .ro-btn {
    min-height: 40px;
    padding: 8px 10px;
    font-size: 12px;
    flex: 1 1 auto;
    justify-content: center;
  }

  .ro-btn-label { display: none; }
  .ro-btn-icon { min-width: 40px; }

  .ro-toggle-group {
    width: 100%;
    justify-content: center;
  }

  .ro-toggle {
    min-height: 40px;
    padding: 8px 12px;
    font-size: 12px;
    flex: 1;
    justify-content: center;
  }

  /* Settings: single column, scrollable when open */
  .ro-settings-panel {
    padding: 10px 12px 8px;
    max-height: 50vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .ro-settings-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .ro-setting {
    padding: 10px 12px;
  }

  .ro-settings-footer {
    font-size: 11px;
  }

  /* Content: flows naturally, no overflow clipping */
  .ro-content {
    flex: none;                     /* don't flex — let it take natural height */
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: visible;              /* no clipping */
  }

  /* Sidebar: no max-height cap — expands to show all content */
  .ro-sidebar {
    flex: 0 0 auto;
    max-height: none;               /* remove viewport cap */
    min-height: 80px;
    overflow-y: visible;            /* no internal scroll — content flows in page */
    overflow-x: hidden;
  }

  /* Map: fixed height so it's always visible and usable */
  .ro-main {
    flex: none;
    height: 55vh;                   /* a good visible map area */
    min-height: 280px;
    position: relative;
    overflow: hidden;
  }

  .ro-map-legend { display: none; }

  .ro-map-container {
    position: absolute;
    inset: 0;
    min-height: 0;
  }

  #route-optimizer-map {
    min-height: 100%;
  }
}

/* ── Very small phones (≤380px) ─────────────────────────────── */
@media (max-width: 380px) {
  .ro-title h2 { font-size: 14px; }

  .ro-toolbar-controls {
    grid-template-columns: 1fr;
  }

  .ro-toolbar-actions {
    flex-direction: column;
  }

  .ro-btn {
    width: 100%;
    justify-content: center;
  }

  .ro-btn-label { display: inline; }
}

/* ── New Bookings / Pending Requests Panel ───────────────────── */
.ro-pending-panel {
  margin-top: 12px;
  border: 1px solid var(--ro-border);
  border-radius: var(--ro-radius-sm);
  overflow: hidden;
  background: var(--ro-surface);
}

.ro-pending-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: #F0FDF4;
  border-bottom: 1px solid #BBF7D0;
  font-size: 12px;
  font-weight: 600;
  color: #166534;
  gap: 8px;
}

.ro-pending-header span {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ro-pending-check {
  padding: 4px 10px;
  border: 1px solid #16A34A;
  border-radius: 6px;
  background: #16A34A;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 150ms;
}

.ro-pending-check:hover { background: #15803D; }
.ro-pending-check:disabled { opacity: 0.6; cursor: not-allowed; }

.ro-pending-body {
  max-height: 280px;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.ro-pending-hint {
  font-size: 11px;
  color: var(--ro-text-muted);
  line-height: 1.5;
  text-align: center;
  padding: 8px 4px;
}

.ro-pending-item {
  border: 1px solid var(--ro-border);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--ro-surface-alt);
  font-size: 12px;
  transition: opacity 200ms;
}

.ro-pending-item.inserted {
  opacity: 0.55;
  border-color: #86EFAC;
  background: #F0FDF4;
}

.ro-pending-name-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  color: var(--ro-text);
  margin-bottom: 4px;
  flex-wrap: wrap;
}

.ro-pending-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 99px;
  background: #EFF6FF;
  color: #1D4ED8;
  white-space: nowrap;
}

.ro-pending-addr {
  font-size: 11px;
  color: var(--ro-text-muted);
  margin-bottom: 2px;
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ro-pending-insert-btn {
  margin-top: 6px;
  width: 100%;
  padding: 5px 0;
  border: 1px solid #2563EB;
  border-radius: 6px;
  background: transparent;
  color: #2563EB;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: background 140ms, color 140ms;
}

.ro-pending-insert-btn:hover { background: #EFF6FF; }
.ro-pending-insert-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.ro-pending-insert-result {
  margin-top: 6px;
  width: 100%;
  padding: 5px 0;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  text-align: center;
}

.ro-pending-insert-ok {
  background: #DCFCE7;
  color: #166534;
  border: 1px solid #86EFAC;
}

.ro-pending-insert-fail {
  background: #FEF2F2;
  color: #991B1B;
  border: 1px solid #FECACA;
}
