/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue:    #1a56db;
  --blue-dk: #1340a8;
  --blue-lt: #e8f0fe;
  --teal:    #0d9488;
  --amber:   #d97706;
  --red:     #dc2626;
  --green:   #16a34a;
  --gray-50: #f9fafb;
  --gray-100:#f3f4f6;
  --gray-200:#e5e7eb;
  --gray-400:#9ca3af;
  --gray-600:#4b5563;
  --gray-800:#1f2937;
  --white:   #ffffff;
  --radius:  10px;
  --shadow:  0 2px 8px rgba(0,0,0,.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  line-height: 1.5;
}

/* ── Nav ── */
nav {
  background: var(--blue-dk);
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 32px;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 6px rgba(0,0,0,.2);
}
nav .brand {
  color: #fff;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: .3px;
  text-decoration: none;
}
nav .brand span { color: #93c5fd; }
nav a {
  color: #bfdbfe;
  text-decoration: none;
  font-size: .9rem;
  padding: 4px 0;
  border-bottom: 2px solid transparent;
  transition: color .15s, border-color .15s;
}
nav a:hover, nav a.active { color: #fff; border-color: #fff; }

/* ── Layout ── */
main {
  display: grid;
  grid-template-columns: 25fr 40fr 35fr;
  gap: 20px;
  padding: 20px 24px;
  align-items: start;
  min-height: calc(100vh - 56px);
}

.col-left  { min-width: 0; }
.col-mid   { min-width: 0; }
.col-right { min-width: 0; }

/* Stops queue scrolls internally so left column stays compact */
.queue-card { display: flex; flex-direction: column; }
.stops-list {
  flex: 1;
  max-height: calc(100vh - 260px);
  overflow-y: auto;
  margin-bottom: 14px;
}

/* Results column scrolls internally */
.col-right .card { overflow: hidden; }

@media (max-width: 1100px) {
  main { grid-template-columns: 1fr 1fr; }
  .col-right { grid-column: 1 / -1; }
}
@media (max-width: 700px) {
  main { grid-template-columns: 1fr; }
}

/* ── Cards ── */
.card {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}
.card h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--gray-800);
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ── Form ── */
.form-group {
  margin-bottom: 14px;
}
.form-group label {
  display: block;
  font-size: .82rem;
  font-weight: 500;
  color: var(--gray-600);
  margin-bottom: 5px;
}
.form-group input,
.form-group textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--gray-200);
  border-radius: 7px;
  font-size: .9rem;
  font-family: inherit;
  color: var(--gray-800);
  background: var(--white);
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.form-group input:focus,
.form-group textarea:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(26,86,219,.12);
}
.form-group textarea { resize: vertical; min-height: 68px; }

/* Autocomplete / combobox */
.autocomplete-wrap { position: relative; }

/* Right-side icon (dropdown arrow or checkmark) */
.combo-icon {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-size: .9rem;
  color: var(--gray-400);
  pointer-events: none;
  line-height: 1;
  transition: color .15s;
}
.autocomplete-wrap.has-retailer .combo-icon {
  color: var(--green);
  font-weight: 700;
}
/* Nudge input text so it doesn't overlap the icon */
.autocomplete-wrap input { padding-right: 28px; }

.autocomplete-list {
  position: absolute;
  top: calc(100% + 2px);
  left: 0; right: 0;
  background: var(--white);
  border: 1px solid var(--gray-200);
  border-radius: 7px;
  box-shadow: 0 6px 16px rgba(0,0,0,.12);
  z-index: 50;
  max-height: 250px;
  overflow-y: auto;
}
.autocomplete-list li {
  padding: 9px 12px;
  cursor: pointer;
  font-size: .87rem;
  list-style: none;
  border-bottom: 1px solid var(--gray-100);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.autocomplete-list li:last-child { border-bottom: none; }
.autocomplete-list li:hover { background: var(--blue-lt); }
.autocomplete-list li .r-name { flex: 1; min-width: 0; }
.autocomplete-list li .r-tag {
  font-size: .72rem;
  font-weight: 500;
  color: var(--gray-600);
  background: var(--gray-100);
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
  flex-shrink: 0;
}
.autocomplete-list li:hover .r-tag { background: rgba(255,255,255,.6); }
.autocomplete-list li.no-results {
  color: var(--gray-400);
  font-style: italic;
  cursor: default;
  justify-content: center;
}
.autocomplete-list li.no-results:hover { background: none; }

/* Search match highlight */
mark {
  background: #fef08a;
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

/* ── Retailer remarks warning box ── */
.remarks-box {
  background: #fefce8;
  border: 1px solid #fde047;
  border-radius: 7px;
  padding: 9px 13px;
  font-size: .82rem;
  color: #713f12;
  font-weight: 500;
  margin-bottom: 12px;
}

/* ── Stops list ── */
.stops-list { margin-bottom: 16px; }
.stop-item {
  background: var(--gray-50);
  border: 1px solid var(--gray-200);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  animation: fadeIn .2s ease;
}
.stop-item-info { flex: 1; min-width: 0; }
.stop-item-info strong { font-size: .9rem; display: block; }
.stop-item-info span { font-size: .8rem; color: var(--gray-600); word-break: break-word; }
.stop-item-info .notes {
  font-size: .78rem;
  color: var(--amber);
  margin-top: 3px;
  font-style: italic;
}
.remove-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray-400);
  font-size: 1.1rem;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
  flex-shrink: 0;
  transition: color .15s;
}
.remove-btn:hover { color: var(--red); }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 7px;
  font-size: .9rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s, transform .1s;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dk); }
.btn-secondary { background: var(--gray-100); color: var(--gray-800); border: 1px solid var(--gray-200); }
.btn-secondary:hover { background: var(--gray-200); }
.btn-teal { background: var(--teal); color: #fff; }
.btn-teal:hover { background: #0f766e; }
.btn-full { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Stop items — compact for narrow left column */
.stop-item {
  padding: 9px 12px;
}
.stop-item-info strong { font-size: .85rem; }
.stop-item-info span  { font-size: .77rem; }

/* ── Results panel ── */
#results-panel { display: none; }
#results-panel.visible { display: block; }

.recommendation-box {
  background: var(--blue-lt);
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  padding: 14px 16px;
  font-size: .88rem;
  color: var(--blue-dk);
  margin-bottom: 20px;
  font-weight: 500;
}
.recommendation-box.warn {
  background: #fffbeb;
  border-color: #fde68a;
  color: #92400e;
}

.driver-block {
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 20px;
}
.driver-header {
  background: var(--blue);
  color: #fff;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.driver-header h3 { font-size: .95rem; font-weight: 600; }
.driver-header .total-time { font-size: .82rem; opacity: .85; }

.driver-stops { padding: 6px 0; }
.route-stop {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--gray-100);
  font-size: .88rem;
}
.route-stop:last-child { border-bottom: none; }
.stop-num {
  background: var(--blue-lt);
  color: var(--blue-dk);
  font-weight: 700;
  font-size: .78rem;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.stop-details { flex: 1; min-width: 0; }
.stop-details .name { font-weight: 600; }
.stop-details .address { color: var(--gray-600); font-size: .82rem; word-break: break-word; }
.stop-details .time { color: var(--teal); font-size: .82rem; font-weight: 500; margin-top: 2px; }
.stop-details .warning {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 5px;
  padding: 4px 8px;
  color: var(--red);
  font-size: .78rem;
  margin-top: 5px;
  display: inline-block;
}

/* WhatsApp box */
.whatsapp-box {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 8px;
  padding: 14px 16px;
  margin: 0 12px 14px;
}
.whatsapp-box pre {
  font-family: 'Menlo', 'Consolas', monospace;
  font-size: .78rem;
  white-space: pre-wrap;
  word-break: break-word;
  color: #166534;
  margin-bottom: 10px;
}
.copy-btn {
  background: #16a34a;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  font-size: .82rem;
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
  transition: background .15s;
}
.copy-btn:hover { background: #15803d; }
.copy-btn.copied { background: #0f766e; }

/* ── Empty / loading states ── */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--gray-400);
}
.empty-state svg { width: 48px; height: 48px; margin-bottom: 12px; opacity: .4; }
.empty-state p { font-size: .9rem; }

.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* ── History page ── */
.history-main {
  max-width: 860px;
  margin: 32px auto;
  padding: 0 20px;
}
.history-entry {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow: hidden;
}
.history-entry-header {
  background: var(--gray-100);
  padding: 14px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--gray-200);
}
.history-entry-header .date { font-weight: 600; font-size: .95rem; }
.history-entry-header .meta { font-size: .82rem; color: var(--gray-600); }
.history-entry-body { padding: 16px 20px; }
.history-driver { margin-bottom: 14px; }
.history-driver h4 { font-size: .88rem; font-weight: 600; color: var(--blue-dk); margin-bottom: 6px; }
.history-stop-row {
  font-size: .82rem;
  padding: 4px 0;
  border-bottom: 1px dotted var(--gray-200);
  display: flex;
  gap: 8px;
}
.history-stop-row:last-child { border-bottom: none; }
.history-stop-row .num { color: var(--gray-400); min-width: 20px; }

/* ── Driver panel ── */
.driver-config { margin-top: -4px; }

.driver-config-item {
  padding: 13px 0;
  border-bottom: 1px solid var(--gray-100);
}
.driver-config-item:last-child { border-bottom: none; padding-bottom: 0; }

.driver-config-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 9px;
}

.driver-label {
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .6px;
  text-transform: uppercase;
  color: var(--blue-dk);
  background: var(--blue-lt);
  padding: 3px 8px;
  border-radius: 4px;
  white-space: nowrap;
  flex-shrink: 0;
  min-width: 56px;
  text-align: center;
}
.driver-label.standby {
  color: var(--gray-600);
  background: var(--gray-100);
}

.driver-name-input {
  flex: 1;
  padding: 7px 10px;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  font-size: .92rem;
  font-family: inherit;
  font-weight: 600;
  color: var(--gray-800);
  outline: none;
  min-width: 0;
}
.driver-name-input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(26,86,219,.1);
}

.driver-timing-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding-left: 66px; /* visually indent under the label */
}
.driver-timing-pair .form-group { margin-bottom: 0; }

input[type="time"] {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  font-size: .88rem;
  font-family: inherit;
  outline: none;
  color: var(--gray-800);
}
input[type="time"]:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(26,86,219,.1);
}

/* Stop count badge */
.count-badge {
  background: var(--blue);
  color: #fff;
  font-size: .72rem;
  padding: 2px 8px;
  border-radius: 999px;
  margin-left: 6px;
  font-weight: 600;
}

/* ── B2B / B2C toggle ── */
.stop-type-toggle {
  display: flex;
  border: 1px solid var(--gray-200);
  border-radius: 7px;
  overflow: hidden;
  margin-bottom: 18px;
}
.toggle-opt {
  flex: 1;
  text-align: center;
  padding: 9px 12px;
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  background: var(--white);
  color: var(--gray-600);
  transition: background .15s, color .15s;
  user-select: none;
}
.toggle-opt input[type="radio"] { display: none; }
.toggle-opt.active {
  background: var(--blue);
  color: #fff;
}

/* ── Derived detail chips (B2B read-only preview) ── */
.derived-details {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: -4px 0 12px;
}
.detail-chip {
  padding: 3px 9px;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 500;
  background: var(--blue-lt);
  color: var(--blue-dk);
}
.detail-chip.muted  { background: var(--gray-100); color: var(--gray-600); }
.detail-chip.timing { background: #fffbeb; color: #92400e; }
.detail-chip.remark { background: #fdf4ff; color: #7e22ce; }

/* ── Stop queue: type + area badges ── */
.stop-badges { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 4px; }
.type-badge {
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .4px;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
}
.type-badge.b2b { background: #dbeafe; color: #1e40af; }
.type-badge.b2c { background: #d1fae5; color: #065f46; }
.area-badge {
  font-size: .68rem;
  font-weight: 500;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--gray-100);
  color: var(--gray-600);
}
.invoice-tag {
  font-size: .78rem;
  font-weight: 400;
  color: var(--gray-600);
  margin-left: 4px;
}
.timing-note {
  font-size: .78rem;
  color: var(--amber);
  font-style: italic;
  margin-top: 2px;
}

/* ── Work-status banner (three states) ── */
.work-status {
  border-radius: 7px;
  padding: 10px 14px;
  font-size: .83rem;
  font-weight: 600;
  margin: 10px 12px 4px;
}
.work-status.overrun {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--red);
}
.work-status.good {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: var(--green);
}
.work-status.light {
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: var(--amber);
}

/* ── Time breakdown panel ── */
.time-breakdown {
  margin: 0 12px 12px;
  border: 1px solid var(--gray-200);
  border-radius: 8px;
  overflow: hidden;
  font-size: .82rem;
}
.breakdown-row {
  display: flex;
  justify-content: space-between;
  padding: 7px 12px;
  border-bottom: 1px solid var(--gray-100);
  color: var(--gray-600);
}
.breakdown-row:last-child { border-bottom: none; }
.breakdown-row.total {
  font-weight: 600;
  color: var(--gray-800);
  background: var(--gray-50);
}

/* ── Estimated arrival + on-site inline ── */
.est-arrival {
  color: var(--teal);
  font-size: .8rem;
  font-weight: 500;
}

/* ── Standby status ── */
.standby-status {
  border-radius: 7px;
  padding: 10px 14px;
  font-size: .85rem;
  font-weight: 600;
  margin-bottom: 10px;
}
.standby-status.idle {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: var(--green);
}
.standby-status.active {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  color: #c2410c;
}

/* ── Route quality indicator ── */
.route-quality {
  border-radius: 7px;
  padding: 9px 14px;
  font-size: .83rem;
  font-weight: 600;
  margin-bottom: 14px;
}
.route-quality .quality-detail { font-weight: 400; }
.route-quality.good     { background: #f0fdf4; border: 1px solid #bbf7d0; color: var(--green); }
.route-quality.moderate { background: #fffbeb; border: 1px solid #fde68a; color: var(--amber); }
.route-quality.poor     { background: #fef2f2; border: 1px solid #fecaca; color: var(--red); }

/* ── Form row pair (side-by-side inputs) ── */
.form-row-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.form-row-pair .form-group { margin-bottom: 0; }

/* ── Party package select ── */
.party-select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--gray-200);
  border-radius: 7px;
  font-size: .9rem;
  font-family: inherit;
  color: var(--gray-800);
  background: var(--white);
  outline: none;
}
.party-select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(26,86,219,.12); }

/* ── Stop type badge: party ── */
.type-badge.party { background: #fdf4ff; color: #7e22ce; }

/* ── Party stop in queue ── */
.stop-item.party-stop { border-left: 3px solid #c084fc; }

/* ── Party route stop ── */
.route-stop.party-route-stop { background: #fdf4ff; }

/* ── Time window label on route stop ── */
.stop-time-window {
  font-size: .78rem;
  color: var(--teal);
  font-weight: 500;
  margin-top: 3px;
}

.stop-wait-notice {
  font-size: .78rem;
  color: #b45309;
  font-weight: 500;
  margin-top: 3px;
}

/* ── Ops Attention panel ── */
.ops-attention {
  background: #fff8f0;
  border: 1.5px solid #f59e0b;
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 14px;
}
.ops-attention-title {
  font-weight: 700;
  font-size: .92rem;
  color: #92400e;
  margin-bottom: 10px;
}
.ops-attention-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}
.ops-flag {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: #fff;
  border-radius: 7px;
  padding: 9px 12px;
  border-left: 3px solid transparent;
}
.ops-flag.flag-red   { border-left-color: var(--red); }
.ops-flag.flag-amber { border-left-color: #f59e0b; }
.flag-emoji { font-size: 1rem; line-height: 1.5; flex-shrink: 0; }
.flag-content { flex: 1; min-width: 0; }
.flag-stop  { font-weight: 700; font-size: .85rem; color: var(--gray-800); margin-bottom: 2px; }
.flag-msg   { font-size: .82rem; color: var(--gray-700); line-height: 1.4; }
.flag-sug   { font-size: .78rem; color: #6b7280; margin-top: 4px; font-style: italic; }
.ops-note {
  font-size: .78rem;
  color: #78716c;
  background: #f9f6f2;
  border-radius: 6px;
  padding: 7px 10px;
  line-height: 1.5;
}

/* Info notices (party split, etc.) */
.ops-info-box {
  background: #f0f9ff;
  border: 1px solid #bae6fd;
  border-radius: 7px;
  padding: 9px 13px;
  margin-bottom: 12px;
  font-size: .82rem;
  color: #0c4a6e;
  line-height: 1.55;
}
.ops-info-item + .ops-info-item { margin-top: 5px; }

/* Per-stop inline flag */
.stop-tw-flag {
  font-size: .77rem;
  font-weight: 500;
  margin-top: 4px;
  line-height: 1.4;
  padding: 3px 7px;
  border-radius: 5px;
  display: inline-block;
}
.stop-tw-flag.flag-red   { background: #fef2f2; color: var(--red); }
.stop-tw-flag.flag-amber { background: #fffbeb; color: #92400e; }

/* Legacy violations box (old history entries) */
.violations-box {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 7px;
  padding: 10px 14px;
  margin: 10px 12px 4px;
  font-size: .82rem;
  color: var(--red);
  font-weight: 500;
  line-height: 1.55;
}
.violations-box div + div { margin-top: 4px; }
.plan-violations { margin: 0 0 14px; }

/* ── Split info + balance info banners ── */
.split-info, .balance-info {
  border-radius: 7px;
  padding: 9px 14px;
  font-size: .82rem;
  font-weight: 500;
  margin-bottom: 10px;
}
.split-info {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e40af;
}
.balance-info {
  background: #f5f3ff;
  border: 1px solid #ddd6fe;
  color: #5b21b6;
}

/* ── Peak-hour warning ── */
.peak-warning {
  background: #fef3c7;
  border: 1px solid #fde68a;
  border-radius: 8px;
  padding: 12px 14px;
  font-size: .85rem;
  color: #92400e;
  font-weight: 500;
  margin-bottom: 16px;
}

/* ── Route stop: invoice + area ── */
.stop-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 3px;
}
.stop-meta .area  { font-size: .78rem; color: var(--gray-600); }
.stop-meta .inv   { font-size: .78rem; color: var(--teal); font-weight: 500; }

/* ── Toasts ── */
#toast {
  position: fixed;
  bottom: 24px; right: 24px;
  background: var(--gray-800);
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: .88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s;
  z-index: 999;
}
#toast.show { opacity: 1; }
#toast.success { background: var(--green); }
#toast.error { background: var(--red); }
