:root {
  /* One Stop Auto Glass brand (oneglass.co.za): navy structure + red
     accent, sampled from the vector logo. Red stays the DANGER colour here
     (credits, reject, errors) - the site's red CTA is not copied onto
     primary buttons or Authorise/Reject would read alike. */
  --brand-primary: #322783;
  --brand-primary-dark: #241c66;
  --brand-primary-tint: #eceaf6;
  --brand-red: #e40613;
  --brand-red-dark: #b8050f;
  --mobile-color: #2f6fed;
  --inhouse-color: #d9772f;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #dde1e6;
  --text: #1c2126;
  --muted: #6b7280;
}

* { box-sizing: border-box; }

.hidden { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 3px solid var(--brand-primary);
  flex-wrap: wrap;
}

.brand-logo {
  display: block;
  height: 46px;
  border-radius: 8px;
}

.topbar h1 {
  font-size: 1.2rem;
  margin: 0;
  margin-right: auto;
}

.date-nav, .actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

button {
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
}

button:hover { background: #eef0f3; }

button.primary {
  background: var(--brand-primary);
  color: white;
  border-color: var(--brand-primary);
  font-weight: 700;
}
button.primary:hover { background: var(--brand-primary-dark); }

button.danger {
  background: var(--brand-red);
  color: white;
  border-color: var(--brand-red);
}
button.danger:hover { background: var(--brand-red-dark); }

input[type="date"], input[type="time"] {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
}

/* Booking search */
.search-wrap { position: relative; }
#bookingSearch {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.88rem;
  width: 190px;
}
#searchResults {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 40;
  width: 340px;
  max-width: 92vw;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0 0 10px 10px;
  box-shadow: 0 10px 24px rgba(0,0,0,.18);
  max-height: 320px;
  overflow-y: auto;
}
#searchResults.hidden { display: none; }
.search-hit { padding: 9px 12px; cursor: pointer; font-size: 0.84rem; border-bottom: 1px solid var(--border); }
.search-hit:hover { background: var(--bg); }
.search-hit .hit-meta { color: var(--muted); font-size: 0.76rem; margin-top: 2px; }
.search-empty { padding: 10px 12px; color: var(--muted); font-size: 0.82rem; }
.search-hint { padding: 7px 12px; color: var(--muted); font-size: 0.72rem; background: var(--bg); border-top: 1px solid var(--border); }

/* Outstanding-count badge on the Client Queries button */
.badge {
  display: inline-block; min-width: 17px; margin-left: 6px; padding: 0 5px;
  background: var(--brand-red); color: #fff; border-radius: 999px;
  font-size: 0.72rem; font-weight: 700; line-height: 17px; text-align: center;
}
.badge.hidden { display: none; }

/* The job an issue is being reported against, shown in the report form */
.issue-job-card {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 12px; margin-bottom: 12px; font-size: 0.85rem; color: var(--text);
}

/* A found booking flashes so the eye lands on it */
@keyframes searchPulse {
  0%, 100% { box-shadow: 0 1px 2px rgba(0,0,0,.15); }
  50% { box-shadow: 0 0 0 4px rgba(37, 99, 235, .55); }
}
.booking-block.search-flash { animation: searchPulse 1s ease-in-out 3; }

#branchPicker {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  background: var(--panel);
}

main {
  padding: 16px 20px;
}

.grid {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  padding-bottom: 8px;
  position: relative; /* anchors the current-time line */
}

/* Current time, top to bottom across today's calendar */
.now-line {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #16a34a;
  z-index: 3;
  pointer-events: none; /* clicks pass through to slots and bookings */
}
.now-line::before {
  content: '';
  position: absolute;
  top: 0;
  left: -3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #16a34a;
}

.grid-header {
  display: flex;
  border-bottom: 1px solid var(--border);
  min-width: 900px;
}

.grid-header .team-label-col {
  width: 180px;
  flex-shrink: 0;
}

.grid-header .hours {
  position: relative;
  flex: 1;
  height: 28px;
}

.grid-header .hour-tick {
  position: absolute;
  top: 0;
  font-size: 0.72rem;
  color: var(--muted);
  transform: translateX(-50%);
}

.team-section-title {
  padding: 8px 12px;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: #fafbfc;
  border-bottom: 1px solid var(--border);
}

.team-row {
  display: flex;
  border-bottom: 1px solid var(--border);
  min-width: 900px;
}

.team-row:last-child { border-bottom: none; }

.team-label-col {
  width: 180px;
  flex-shrink: 0;
  padding: 10px 12px;
  font-size: 0.85rem;
  font-weight: 500;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 2px;
}
.team-label-line { display: flex; align-items: center; }
/* Live drive time from the branch yard to this team's van right now */
.shop-eta {
  font-size: 0.68rem;
  font-weight: 400;
  color: var(--muted);
  margin-left: 14px;
}
.shop-eta:empty { display: none; }

.team-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 8px;
}

.team-track {
  position: relative;
  flex: 1;
  min-height: 52px;
  background-image: repeating-linear-gradient(
    to right,
    var(--border) 0,
    var(--border) 1px,
    transparent 1px,
    transparent calc(100% / var(--day-hours, 9))
  );
  cursor: pointer;
}

.booking-block {
  position: absolute;
  top: 6px;
  bottom: 6px;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 0.75rem;
  color: white;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
  z-index: 2;
}

.booking-block.mobile { background: var(--mobile-color); }
.booking-block.inhouse { background: var(--inhouse-color); }
/* Stock collections stand out in orange; status rules further down still win */
.booking-block.stock-collection { background: #ea580c; }

/* Drive-time window: click-triggered, so it works on touch as well as mouse */
#etaPopover {
  position: fixed;
  z-index: 70;
  background: #23262b;
  color: #fff;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.8rem;
  max-width: 280px;
  white-space: pre-line; /* the van leg and the after-fitment leg stack */
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
#etaPopover a {
  display: block;
  margin-top: 8px;
  padding: 7px 10px;
  background: rgba(255,255,255,.12);
  color: #fff;
  text-decoration: none;
  border-radius: 6px;
  font-size: 0.78rem;
  text-align: center;
}
#etaPopover a:hover { background: rgba(255,255,255,.22); }

.travel-info {
  font-size: 0.8rem;
  color: #1d4ed8;
  background: #eff4ff;
  border: 1px solid #cfdcf8;
  border-radius: 6px;
  padding: 7px 10px;
  margin: 0;
}
.travel-info.hidden { display: none; }

/* Address autocomplete */
.address-wrap { position: relative; display: block; }
#addressSuggestions {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0 0 8px 8px;
  box-shadow: 0 8px 20px rgba(0,0,0,.15);
  max-height: 200px;
  overflow-y: auto;
}
#addressSuggestions.hidden { display: none; }
.addr-option {
  padding: 9px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.addr-option:hover { background: var(--bg); }

/* Click-to-call */
/* Fitment jobs: the block base is the travel leg (blue); the fitment window
   sits on top in green, taking the crew-status colour as the job progresses */
.booking-block.fitment-job { background: #2563eb; }
.fitment-seg {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 0;
  pointer-events: none;
}
/* Invisible hover region over the travel band; events bubble to the block */
.travel-hover {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 1;
}
/* Teal, deliberately far from the completed-job forest green so "booked
   fitment window" and "finished job" can't be confused at a glance */
.fitment-seg.st-scheduled { background: #0d9488; }
.fitment-seg.st-on-route  { background: #7c5cd6; }
.fitment-seg.st-on-site   { background: #c77f18; }
/* Blocks are two fixed lines tall; long text truncates with an ellipsis
   rather than wrapping into a third line the block can't fit */
.booking-block .b-customer,
.booking-block .b-time {
  position: relative;
  z-index: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Click menu on a booking block: Call / WhatsApp / drive time / edit */
#blockMenu {
  position: fixed;
  z-index: 60;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.18);
  padding: 6px;
  min-width: 160px;
}
#blockMenu .phone-number {
  padding: 6px 10px;
  font-size: 0.78rem;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
#blockMenu a {
  display: block;
  padding: 9px 10px;
  border-radius: 6px;
  text-decoration: none;
  color: inherit;
  font-size: 0.9rem;
}
#blockMenu a:hover { background: var(--bg); }

/* Tracking panel */
.track-team { color: var(--muted); font-weight: 400; font-size: 0.8rem; }
.track-state { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.track-mute-btn { font-size: 0.78rem; padding: 4px 10px; }

/* Team labels linked to a van open its live position */
.team-label-col.has-vehicle { cursor: pointer; }
.team-label-col.has-vehicle:hover {
  background: rgba(37, 99, 235, 0.07);
  text-decoration: underline;
}
.vehicle-live {
  margin-left: 6px;
  font-size: 0.85rem;
  line-height: 1;
  filter: grayscale(1) opacity(.45);
}
.vehicle-live.moving { filter: none; }
.vehicle-live.stale { filter: grayscale(1) opacity(.25); }

/* Crew progress, readable at a glance across the day */
.booking-block.st-on-route { background: #7c5cd6; }
.booking-block.st-on-site  { background: #c77f18; }
.booking-block.st-complete { background: #2e7d46; }
.booking-block.flagged {
  outline: 2px solid #c33d3d;
  outline-offset: -2px;
}
/* Crew running 10+ min late: a thicker, brighter red than the problem-flag
   outline so the two aren't confused, plus an explicit LATE badge */
.booking-block.late-warning {
  outline: 3px solid #dc2626;
  outline-offset: -3px;
}
.booking-block .b-late {
  display: inline-block;
  background: #dc2626;
  color: #fff;
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: .04em;
  padding: 1px 5px;
  border-radius: 3px;
}

.status-key {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  align-items: center;
  padding: 8px 16px;
  font-size: 0.72rem;
  color: var(--muted);
}
.status-key span { display: inline-flex; align-items: center; gap: 5px; }
.status-key i {
  width: 10px; height: 10px; border-radius: 3px; display: inline-block;
}

.booking-block.draggable { cursor: grab; }
.booking-block.dragging {
  cursor: grabbing;
  opacity: 0.85;
  z-index: 10;
  box-shadow: 0 6px 16px rgba(0,0,0,0.3);
  /* so elementFromPoint sees the track underneath, not this block */
  pointer-events: none;
  transition: none;
}
.booking-block.drop-blocked { outline: 2px solid #c33d3d; }
.team-track.drop-target { background-color: rgba(37, 99, 235, 0.06); }
.team-track.drop-target-invalid { background-color: rgba(195, 61, 61, 0.08); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: #23262b;
  color: #fff;
  padding: 11px 18px;
  border-radius: 8px;
  font-size: 0.85rem;
  max-width: 90vw;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  z-index: 100;
}
#toast.error { background: #b3332f; }

.booking-block .b-customer {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.booking-block .b-time {
  opacity: 0.85;
  white-space: nowrap;
}

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

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modal.hidden { display: none; }

.modal-content {
  background: var(--panel);
  border-radius: 10px;
  padding: 24px;
  width: 420px;
  max-width: 92vw;
  max-height: 88vh;
  overflow-y: auto;
}

.modal-content h2 {
  margin-top: 0;
  font-size: 1.1rem;
}

#bookingForm label, .modal-content label {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 12px;
}

#bookingForm input, #bookingForm select, #bookingForm textarea,
#passwordForm input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
  font-family: inherit;
}

#fEnd[readonly] {
  background: #f3f4f6;
  cursor: default;
}

.row {
  display: flex;
  gap: 12px;
}
.row label, .row > input, .row > select { flex: 1; }

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.spacer { flex: 1; }

.lock-note {
  color: #8a6d1a;
  font-size: 0.85rem;
  background: #fdf6e3;
  border: 1px solid #ecd9a0;
  padding: 8px 10px;
  border-radius: 6px;
}
.lock-note.hidden { display: none; }

.error {
  color: #c33d3d;
  font-size: 0.85rem;
  background: #fdecec;
  border: 1px solid #f3b9b9;
  padding: 8px 10px;
  border-radius: 6px;
}
.error.hidden { display: none; }

.teams-list {
  list-style: none;
  padding: 0;
  margin: 0 0 16px;
  max-height: 240px;
  overflow-y: auto;
}
/* Teams management needs the width for name + branch + type + vehicle */
#teamsModal .modal-content {
  width: 860px;
  max-width: 94vw;
}

.teams-list li {
  display: flex;
  align-items: center;
  flex-wrap: wrap; /* an edit row keeps every control visible */
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}
.teams-list li .t-name { flex: 1; }
.teams-list li button {
  padding: 4px 10px;
  font-size: 0.78rem;
}
.teams-list li input, .teams-list li select {
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
}
.teams-list li input.t-edit-name { flex: 1; min-width: 0; }

#teamForm { margin-top: 4px; }
#teamForm input, #teamForm select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

/* ---- Help widget: floating button, bottom-right ---- */
#helpFab {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 55;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 11px 16px;
  border-radius: 999px;
  border: none;
  background: var(--brand-primary);
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  box-shadow: 0 4px 14px rgba(0,0,0,.22);
}
#helpFab:hover { background: var(--brand-primary-dark); }
#helpPanel {
  position: fixed;
  right: 20px;
  bottom: 74px;
  z-index: 56;
  width: 360px;
  max-width: calc(100vw - 40px);
  max-height: 76vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0,0,0,.24);
  padding: 16px;
}
#helpPanel h3 { margin: 0 0 4px; font-size: 1.05rem; }
#helpPanel .help-sub { color: var(--muted); font-size: 0.82rem; margin: 0 0 12px; }
#helpPanel .help-sec { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 12px; }
#helpPanel .help-sec:first-of-type { border-top: none; margin-top: 0; padding-top: 0; }
#helpPanel label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; }
#helpPanel input[type="search"],
#helpPanel select,
#helpPanel textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-family: inherit;
}
#helpPanel textarea { resize: vertical; min-height: 76px; }

/* Reply badge sits in the corner of the floating Help button */
#helpReplyBadge {
  position: absolute; top: -5px; right: -5px; margin: 0;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
}

/* Admin: reply picker + Mark done sit together on the help notification */
.help-ack-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-left: auto; }
.help-ack-row .help-reply-pick {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--panel); font-size: 0.82rem; max-width: 230px;
}

/* Sender: an admin's reply shown in the Help panel */
.help-reply {
  background: var(--bg); border: 1px solid var(--border); border-left: 3px solid var(--brand-primary);
  border-radius: 6px; padding: 8px 10px; margin-bottom: 8px;
}
.help-reply.unseen { border-left-color: var(--brand-red); background: #fff6f5; }
.help-reply .hr-text { font-size: 0.86rem; font-weight: 600; }
.help-reply .hr-meta { font-size: 0.72rem; color: var(--muted); margin-top: 3px; }
#helpFaqResults { margin-top: 8px; }
.faq-hit { padding: 8px 0; border-bottom: 1px solid var(--border); }
.faq-hit:last-child { border-bottom: none; }
.faq-hit .faq-q { font-weight: 600; font-size: 0.86rem; }
.faq-hit .faq-a { font-size: 0.84rem; color: var(--text); margin-top: 3px; line-height: 1.45; }
.help-empty { color: var(--muted); font-size: 0.84rem; padding: 6px 0; }
.help-manual-link {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 12px; border-radius: 8px; text-decoration: none;
  background: var(--bg); border: 1px solid var(--border);
  color: inherit; font-size: 0.88rem; font-weight: 600;
}
.help-manual-link:hover { background: #eef0f3; }
#helpPanel .help-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
#helpSendBtn { flex-shrink: 0; }
