:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --border: #e3e7f0;
  --text: #1a2233;
  --muted: #6b7488;
  --brand: #3b5bdb;
  --brand-ink: #ffffff;
  --accent: #15aabf;
  --ok: #2f9e44;
  --warn: #e8590c;
  --danger: #e03131;
  --shadow: 0 1px 2px rgba(20, 30, 60, 0.06), 0 8px 24px rgba(20, 30, 60, 0.06);
  --radius: 12px;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

:root[data-theme="dark"] {
  --bg: #0e1422;
  --surface: #161d2e;
  --surface-2: #1d273b;
  --border: #283250;
  --text: #e6eaf3;
  --muted: #8b94ac;
  --brand: #5c7cfa;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
a { color: var(--brand); text-decoration: none; }
button { font-family: inherit; }

/* --- Buttons / inputs ----------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 600;
  cursor: pointer; font-size: 13px; transition: filter .15s, background .15s;
}
.btn:hover { filter: brightness(0.97); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.menu-toggle { display: none; font-size: 18px; line-height: 1; padding: 6px 10px; }
.nav-backdrop { display: none; }
.type-field { flex: 0 0 130px; }

input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--text);
  font: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 22%, transparent);
}
label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 0 5px; }
.field { margin-bottom: 14px; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }

/* --- App shell ------------------------------------------------------------ */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 18px 14px; display: flex; flex-direction: column; gap: 4px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 17px; padding: 6px 8px 18px; }
.brand .dot { width: 26px; height: 26px; border-radius: 8px; background: linear-gradient(135deg, var(--brand), var(--accent)); }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px;
  border-radius: 9px; color: var(--muted); font-weight: 600; cursor: pointer;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand); }
.nav-spacer { flex: 1; }

.main { padding: 0; overflow: auto; height: 100vh; }
.topbar {
  display: flex; align-items: center; gap: 14px; padding: 16px 28px;
  border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; }
.content { padding: 24px 28px 60px; }
.user-chip { display: flex; align-items: center; gap: 8px; color: var(--muted); font-weight: 600; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  background: var(--brand); color: #fff; font-size: 12px; font-weight: 700;
}

/* --- Cards / metrics ------------------------------------------------------ */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; margin-bottom: 24px; }
.metric { padding: 18px 20px; }
.metric.clickable { cursor: pointer; transition: box-shadow .15s, transform .1s; }
.metric.clickable:hover { box-shadow: 0 4px 20px rgba(59,91,219,.18); transform: translateY(-1px); }
.metric .k { color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.metric .v { font-size: 28px; font-weight: 800; margin-top: 6px; }
.metric-hint { font-size: 11px; color: var(--brand); margin-top: 8px; font-weight: 600; opacity: 0; transition: opacity .15s; }
.metric.clickable:hover .metric-hint { opacity: 1; }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; align-items: start; }
.panel { padding: 18px 20px; }
.panel h3 { margin: 0 0 14px; font-size: 14px; }

.stage-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 13px; }
.stage-bar.clickable { cursor: pointer; border-radius: 6px; padding: 3px 4px; margin: 0 -4px 10px; transition: background .12s; }
.stage-bar.clickable:hover { background: var(--surface-2); }
.stage-bar .name { width: 110px; color: var(--muted); font-weight: 600; }
.stage-bar .track { flex: 1; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.stage-bar .fill { height: 100%; background: var(--brand); border-radius: 99px; }
.stage-bar .n { width: 28px; text-align: right; font-weight: 700; }

/* --- Tables --------------------------------------------------------------- */
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--border); }
td { padding: 12px 14px; border-bottom: 1px solid var(--border); }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }

/* --- Pipeline board ------------------------------------------------------- */
.board { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 12px; }
.column { flex: 0 0 268px; background: var(--surface-2); border-radius: var(--radius); padding: 10px; }
.column.dragover { outline: 2px dashed var(--brand); outline-offset: -2px; }
.column h4 { margin: 4px 6px 10px; font-size: 13px; display: flex; justify-content: space-between; }
.column .count { color: var(--muted); }
.deal-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 12px; margin-bottom: 9px; cursor: grab; box-shadow: var(--shadow);
  transition: box-shadow .12s, transform .1s;
}
.deal-card:hover { box-shadow: 0 4px 16px rgba(20,30,60,.12); transform: translateY(-1px); }
.deal-card:active { cursor: grabbing; }
.deal-card .ref { font-size: 11px; color: var(--muted); font-weight: 700; display: flex; gap: 6px; align-items: center; }
.deal-card .who { font-weight: 700; margin: 4px 0 2px; }
.deal-card .meta { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 6px; }
.deal-card .price { color: var(--text); font-weight: 700; }
.card-owner { font-size: 11px; color: var(--muted); margin-top: 5px; }

/* --- Badges --------------------------------------------------------------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 99px; font-size: 11px; font-weight: 700; }
.badge.submitted { background: #e7f5ff; color: #1971c2; }
.badge.quoted { background: #fff3bf; color: #e67700; }
.badge.booked { background: #e6fcf5; color: #0ca678; }
.badge.in_transit { background: #edf2ff; color: #3b5bdb; }
.badge.delivered { background: #ebfbee; color: #2f9e44; }
.badge.cancelled { background: #ffe3e3; color: #e03131; }

/* --- Drawer / modal ------------------------------------------------------- */
.overlay { position: fixed; inset: 0; background: rgba(10, 15, 30, 0.45); z-index: 50; display: flex; justify-content: flex-end; }
.overlay.center { align-items: center; justify-content: center; }
.drawer { width: min(560px, 100%); height: 100%; background: var(--bg); overflow-y: auto; box-shadow: var(--shadow); }
.modal { width: min(540px, 92%); background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); max-height: 90vh; overflow-y: auto; }
.drawer-head, .modal-head { display: flex; align-items: center; gap: 10px; padding: 18px 22px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 2; }
.drawer-head h2, .modal-head h2 { margin: 0; font-size: 17px; flex: 1; }
.drawer-body, .modal-body { padding: 20px 22px; }
.section-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 10px; }

/* --- Timeline ------------------------------------------------------------- */
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { position: relative; padding: 0 0 16px 22px; border-left: 2px solid var(--border); }
.timeline li:last-child { border-left-color: transparent; }
.timeline .dot { position: absolute; left: -7px; top: 2px; width: 12px; height: 12px; border-radius: 50%; background: var(--brand); border: 2px solid var(--surface); }
.timeline .when { color: var(--muted); font-size: 12px; }

/* --- Filter bar ----------------------------------------------------------- */
.filter-bar {
  display: flex; align-items: center; gap: 10px; margin-bottom: 18px; flex-wrap: wrap;
}
.filter-group { display: flex; align-items: center; gap: 6px; }
.filter-input {
  width: 260px; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; font-size: 13px;
}
.filter-input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 22%, transparent); }
.filter-select {
  width: auto; min-width: 150px; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; font-size: 13px; cursor: pointer;
}
.filter-select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 22%, transparent); }
.filter-toggle {
  display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600;
  color: var(--muted); cursor: pointer; padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
}
.filter-toggle input[type="checkbox"] { width: auto; margin: 0; cursor: pointer; }
.filter-toggle:has(input:checked) { border-color: var(--brand); color: var(--brand); }

/* --- Task list ------------------------------------------------------------ */
.count-chip {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--brand); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 5px; margin-left: 6px;
}
.task-body { font-weight: 500; }
.task-owner { display: inline-block; margin-left: 6px; font-size: 11px; color: var(--muted); background: var(--surface-2); padding: 1px 7px; border-radius: 99px; }
.task-link { color: var(--brand); font-weight: 600; cursor: pointer; }
.task-link:hover { text-decoration: underline; }
.when.overdue { color: var(--danger); font-weight: 600; }
.dot-warn { background: var(--warn) !important; }
.clickable-li { cursor: default; }

/* --- Activity timeline clickable ----------------------------------------- */
.clickable-li { transition: background .1s; }
.clickable-li:hover { background: color-mix(in srgb, var(--brand) 6%, transparent); border-radius: 6px; }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 10px; z-index: 100; box-shadow: var(--shadow); font-weight: 600; }
.spinner { width: 26px; height: 26px; border: 3px solid var(--border); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; margin: 60px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Auth screen ---------------------------------------------------------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; padding: 30px; }
.auth-card .brand { justify-content: center; padding-bottom: 10px; }
.auth-card p.sub { color: var(--muted); text-align: center; margin: 0 0 22px; }
.error { color: var(--danger); font-size: 13px; margin: 8px 0 0; min-height: 18px; }

/* --- Portal --------------------------------------------------------------- */
.portal-wrap { max-width: 820px; margin: 0 auto; padding: 30px 20px 60px; }
.portal-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.portal-head h1 { flex: 1; font-size: 20px; margin: 0; }
.progress { display: flex; gap: 6px; margin: 16px 0; }
.progress .step { flex: 1; text-align: center; }
.progress .bar { height: 6px; border-radius: 99px; background: var(--surface-2); margin-bottom: 6px; }
.progress .step.done .bar { background: var(--ok); }
.progress .step.current .bar { background: var(--brand); }
.progress .step small { font-size: 11px; color: var(--muted); }
.progress .step.current small { color: var(--brand); font-weight: 700; }

/* Contact banner */
.contact-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 14px 18px; margin-bottom: 20px; flex-wrap: wrap;
}
.contact-banner-text {
  display: flex; align-items: center; gap: 8px; color: var(--muted); font-weight: 600;
}

/* Route row */
.route-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  flex-wrap: wrap; margin: 10px 0 4px;
}
.route-zips { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.zip-badge {
  display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 99px;
  font-size: 13px; font-weight: 700; letter-spacing: .03em;
}
.zip-badge.pickup { background: #ebfbee; color: #2f9e44; }
.zip-badge.dropoff { background: #ffe3e3; color: #e03131; }
:root[data-theme="dark"] .zip-badge.pickup { background: rgba(47,158,68,.18); color: #69db7c; }
:root[data-theme="dark"] .zip-badge.dropoff { background: rgba(224,49,49,.18); color: #ff8787; }
.route-arrow { color: var(--muted); flex-shrink: 0; }
.route-dist { font-size: 12px; font-weight: 700; color: var(--brand); }

/* Map */
.map-container {
  height: 220px; border-radius: 10px; overflow: hidden;
  border: 1px solid var(--border); margin: 14px 0; position: relative;
  background: var(--surface-2);
}
.map-loading {
  position: absolute; inset: 0; display: grid; place-items: center;
}
.map-error {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--muted); font-size: 13px;
}
.spinner-sm {
  width: 16px; height: 16px; border: 2px solid var(--border);
  border-top-color: var(--brand); border-radius: 50%;
  animation: spin .8s linear infinite; display: inline-block;
}

/* Leaflet custom pins */
.map-pin {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 50%;
  font-size: 11px; font-weight: 800; color: #fff;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
.pickup-pin { background: #2f9e44; }
.dropoff-pin { background: #e03131; }

/* --- Responsive / mobile -------------------------------------------------- */
@media (max-width: 768px) {
  .shell { grid-template-columns: 1fr; }

  /* Sidebar becomes an off-canvas drawer toggled from the topbar */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; height: 100%;
    width: 250px; max-width: 82vw; z-index: 60;
    transform: translateX(-100%);
    transition: transform .22s ease;
  }
  .shell.nav-open .sidebar { transform: translateX(0); box-shadow: var(--shadow); }

  .nav-backdrop {
    display: block; position: fixed; inset: 0; z-index: 55;
    background: rgba(10, 15, 30, 0.45);
    opacity: 0; pointer-events: none; transition: opacity .2s;
  }
  .shell.nav-open .nav-backdrop { opacity: 1; pointer-events: auto; }

  .menu-toggle { display: inline-flex; }
  .nav-item { padding: 12px 12px; }

  .topbar { padding: 12px 14px; gap: 10px; }
  .topbar h1 { font-size: 16px; min-width: 0; }
  .content { padding: 16px 14px 72px; }
  .user-chip .user-name { display: none; }

  .metrics { grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 18px; }
  .metric { padding: 14px 16px; }
  .metric .v { font-size: 24px; }
  .grid-2 { grid-template-columns: 1fr; }

  .filter-bar { gap: 8px; }
  .filter-group { flex: 1 1 100%; }
  .filter-input, .filter-select { width: 100%; min-width: 0; }

  /* Stack form rows so paired fields don't get cramped */
  .row { flex-direction: column; gap: 0; }
  .type-field { flex: 1 1 auto; }

  .drawer-head, .modal-head { padding: 14px 16px; }
  .drawer-body, .modal-body { padding: 16px; }

  /* Pipeline columns snap into view one at a time */
  .board { scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; }
  .column { flex-basis: min(82vw, 280px); scroll-snap-align: start; }

  /* Tables reflow into stacked cards with inline labels */
  table thead { display: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  tbody tr {
    border: 1px solid var(--border); border-radius: 10px;
    margin-bottom: 10px; background: var(--surface);
  }
  td {
    display: flex; align-items: center; justify-content: space-between; gap: 14px;
    padding: 9px 14px; text-align: right; border-bottom: 1px solid var(--border);
  }
  tr td:last-child { border-bottom: none; }
  td::before {
    content: attr(data-label); flex-shrink: 0; text-align: left;
    font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--muted); font-weight: 700;
  }
  td[colspan] { padding: 0; }
  td[colspan]::before { content: none; }
}
