/* Layout and components. One rule per line; colours only through tokens. */

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: grid;
  grid-template-rows: var(--appbar) auto minmax(0, 1fr);
  background: var(--bg);
  color: var(--text);
  font: var(--fs-m)/1.4 var(--font);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
}

h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- app bar ---- */

.appbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: inherit;
  font-size: var(--fs-l);
  font-weight: 700;
  letter-spacing: -0.2px;
  text-decoration: none;
}

.brand b {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--accent);
  color: var(--surface);
  font-size: var(--fs-l);
}

select, input[type="search"], input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="url"] {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.button {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--fs-s);
  font-weight: 600;
  white-space: nowrap;
}

.button:hover { background: var(--surface-2); }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--surface); }
.button.icon { width: 32px; padding: 0; font-size: var(--fs-l); }
.button:disabled, input:disabled { opacity: 0.55; cursor: default; }
a.button { display: inline-flex; align-items: center; color: inherit; text-decoration: none; }

.status { position: relative; }

.status summary {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: var(--fs-s);
  font-weight: 600;
  list-style: none;
  cursor: pointer;
  white-space: nowrap;
}

.status summary::-webkit-details-marker { display: none; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-2); }
.status[data-tone="good"] summary { color: var(--good); border-color: var(--good); background: var(--good-soft); }
.status[data-tone="good"] .dot { background: var(--good); }
.status[data-tone="warn"] summary { color: var(--warn); border-color: var(--warn); background: var(--warn-soft); }
.status[data-tone="warn"] .dot { background: var(--warn); }
.status[data-tone="bad"] summary { color: var(--bad); border-color: var(--bad); background: var(--bad-soft); }
.status[data-tone="bad"] .dot { background: var(--bad); }

.popover {
  position: absolute;
  top: 40px;
  left: 0;
  z-index: 30;
  width: 340px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: var(--fs-s);
}

.popover dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; }
.popover dt { color: var(--text-2); }
.popover dd { margin: 0; text-align: right; }
.popover p { margin-top: 10px; color: var(--text-2); }

/* The user menu: the status popover, opened to the left from the bar's right end. */
.menu .popover { left: auto; right: 0; width: 240px; display: grid; gap: 2px; padding: 6px; }
.popover .menu-email { margin: 0; padding: 6px 8px; overflow-wrap: anywhere; }
.menu-item { padding: 6px 8px; border: 0; border-radius: 6px; background: none; color: var(--text); font-size: var(--fs-s); text-align: left; text-decoration: none; }
.menu-item:hover { background: var(--surface-2); }
.fleet-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 240px; }

.kpis { display: flex; gap: 8px; min-width: 0; }

.kpi {
  display: flex;
  align-items: baseline;
  gap: 5px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: var(--fs-xs);
  white-space: nowrap;
}

.kpi b { color: var(--text); font-size: var(--fs-m); }
.kpi.alert { border-color: var(--bad); color: var(--bad); background: var(--bad-soft); }
.kpi.alert b { color: var(--bad); }
button.kpi { cursor: pointer; }

.clock { margin-left: auto; color: var(--text-2); font-size: var(--fs-s); white-space: nowrap; }
.appbar-actions { display: flex; gap: 6px; }

/* ---- banners ---- */

.banners { display: grid; }

.banner {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 7px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  font-size: var(--fs-s);
}

.banner.good { background: var(--good-soft); color: var(--good); }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.banner strong { white-space: nowrap; }
.banner details { min-width: 0; }
.banner summary { cursor: pointer; }
.banner ul { margin: 6px 0 2px; padding-left: 18px; max-height: 140px; overflow: auto; color: var(--text); }

/* ---- workspace ---- */

.workspace {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 380px;
  min-height: 0;
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--surface);
}

.list-panel { border-right: 1px solid var(--border); }
.options-panel { border-left: 1px solid var(--border); }
.center { background: var(--bg); }

.tabs { display: flex; gap: 2px; padding: 0 10px; border-bottom: 1px solid var(--border); background: var(--surface); }

.tabs [role="tab"] {
  padding: 11px 10px 9px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-2);
  font-size: var(--fs-s);
  font-weight: 600;
}

.tabs [role="tab"][aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

.count {
  display: inline-block;
  min-width: 22px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: var(--fs-xs);
  font-weight: 600;
  text-align: center;
}

.filters { display: grid; gap: 8px; padding: 10px; border-bottom: 1px solid var(--border); }
.filters input[type="search"] { width: 100%; }
.filter-row { display: flex; gap: 8px; align-items: center; }
.filter-row select { flex: 1; min-width: 0; }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-size: var(--fs-s); white-space: nowrap; }

.list { flex: 1; min-height: 0; overflow-y: auto; padding: 6px; }

.panel-foot {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  color: var(--text-2);
  font-size: var(--fs-xs);
}

/* ---- rows: drivers and trips ---- */

.row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 2px 10px;
  align-items: center;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
}

.row + .row { margin-top: 2px; }
.row:hover { background: var(--surface-2); }
.row[aria-selected="true"] { border-color: var(--accent); background: var(--accent-soft); }
.row.muted { cursor: default; opacity: 0.85; }

.avatar {
  grid-row: span 2;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-2);
  font-size: var(--fs-xs);
  font-weight: 700;
}

.row-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { grid-column: 2 / -1; color: var(--text-2); font-size: var(--fs-xs); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-note { grid-column: 1 / -1; color: var(--text-2); font-size: var(--fs-xs); }
.row-note.warn { color: var(--warn); }
.row-note.bad { color: var(--bad); }
/* How much of the hours ahead a driver's route fills. */
.load-bar {
  display: inline-block;
  width: 56px;
  height: 5px;
  margin-right: 6px;
  border-radius: 3px;
  vertical-align: middle;
  background: linear-gradient(to right, var(--accent) var(--load, 0%), var(--surface-2) var(--load, 0%));
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: var(--fs-xs);
  font-weight: 700;
  white-space: nowrap;
}

.pill.good { background: var(--good-soft); color: var(--good); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }

.empty { padding: 28px 16px; color: var(--text-2); text-align: center; }
.empty strong { display: block; margin-bottom: 6px; color: var(--text); }
.empty ul { margin: 12px 0 0; padding: 0; list-style: none; text-align: left; font-size: var(--fs-s); }
.empty li { padding: 6px 0; border-top: 1px solid var(--border); }
.empty li b { color: var(--text); }

/* ---- centre: header, summary, map, dock ---- */

.detail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.detail-head > div:first-child { min-width: 0; }
.detail-head h1 { font-size: var(--fs-l); font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail-head p { color: var(--text-2); font-size: var(--fs-s); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail-actions { display: flex; gap: 10px; align-items: center; }

.summary {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.tile { padding: 8px 14px; border-right: 1px solid var(--border); min-width: 0; }
.tile:last-child { border-right: 0; }
.tile small { display: block; color: var(--text-2); font-size: var(--fs-xs); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile strong { display: block; font-size: var(--fs-l); white-space: nowrap; }
.tile span { display: block; color: var(--text-2); font-size: var(--fs-xs); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile strong.good { color: var(--good); }
.tile strong.warn { color: var(--warn); }
.tile strong.bad { color: var(--bad); }

.map-wrap { position: relative; flex: 1; min-height: 160px; }
#map { position: absolute; inset: 0; }
.map-fallback { display: grid; place-items: center; height: 100%; padding: 20px; color: var(--text-2); text-align: center; }

.map-badge, .map-error, .legend {
  position: absolute;
  z-index: 2;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--fs-xs);
}

.map-badge { top: 10px; left: 10px; color: var(--text-2); }
.map-error { top: 44px; left: 10px; color: var(--warn); background: var(--warn-soft); border-color: var(--warn); }
.legend { left: 10px; bottom: 10px; display: flex; gap: 12px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend .line { width: 18px; height: 0; border-top: 3px solid var(--route-assigned); }
.legend .line.new { border-color: var(--route-new); }
.legend .line.after { border-color: var(--route-after); }
.legend .line.deadhead { border-top: 3px dashed var(--route-empty); }

.route-marker {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--marker-color, var(--route-assigned));
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  box-shadow: 0 1px 4px rgb(0 0 0 / 35%);
}

.route-marker.driver { width: 30px; height: 30px; background: var(--marker-driver); color: var(--surface); }
.route-marker.driver.selected { outline: 3px solid var(--accent); }
.route-marker.dropoff { border-radius: 6px; }
.maplibregl-popup-content { padding: 8px 10px; background: var(--surface); color: var(--text); font: var(--fs-xs)/1.4 var(--font); border-radius: var(--radius); }
.maplibregl-popup-tip { display: none; }

.dock { display: flex; flex-direction: column; height: 236px; background: var(--surface); border-top: 1px solid var(--border); }
.dock-body { flex: 1; min-height: 0; overflow: auto; padding: 10px 14px; }

/* timeline */

.timeline { display: flex; gap: 8px; align-items: stretch; min-width: max-content; }

.stop {
  width: 172px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-top: 3px solid var(--stop-color, var(--route-assigned));
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--fs-xs);
}

.stop.new { background: var(--surface); box-shadow: inset 0 0 0 1px var(--route-new); }
.stop strong { display: block; font-size: var(--fs-m); }
.stop p { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stop small { display: block; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stop small.good { color: var(--good); }
.stop small.warn { color: var(--warn); }
.stop small.bad { color: var(--bad); font-weight: 700; }

/* before / after */

.compare-stats { display: flex; gap: 22px; margin-bottom: 10px; font-size: var(--fs-s); }
.compare-stats small { display: block; color: var(--text-2); font-size: var(--fs-xs); }
.compare-stats strong { font-size: var(--fs-l); }

table { width: 100%; border-collapse: collapse; font-size: var(--fs-s); }
th, td { padding: 5px 10px 5px 0; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { color: var(--text-2); font-size: var(--fs-xs); font-weight: 600; }
td.later { color: var(--warn); }
td.new { color: var(--route-new); font-weight: 700; }

.reasons { margin: 0; padding: 0; list-style: none; font-size: var(--fs-s); }
.reasons li { padding: 7px 0; border-bottom: 1px solid var(--border); }
.reasons b { margin-right: 6px; }
.reasons small { display: block; color: var(--text-2); font-size: var(--fs-xs); }
.muted-text { color: var(--text-2); font-size: var(--fs-s); }
.warn-text { color: var(--warn); }
.error { color: var(--bad); font-size: var(--fs-s); }
.error:empty { display: none; }

/* ---- options ---- */

.options-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.options-head h2 { font-size: var(--fs-l); font-weight: 700; }
.options-head p { color: var(--text-2); font-size: var(--fs-s); }
.notice { margin: 8px 10px 0; padding: 8px 10px; border-radius: var(--radius); background: var(--warn-soft); color: var(--warn); font-size: var(--fs-s); }

.option {
  display: grid;
  gap: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
}

.option + .option { margin-top: 6px; }
.option:hover { background: var(--surface-2); }
.option.best { border-left-color: var(--good); }
.option.alternative { border-left-color: var(--accent); }
.option.tight { border-left-color: var(--warn); }
.option[aria-selected="true"] { border-color: var(--accent); border-left-width: 4px; background: var(--accent-soft); }
.option.best[aria-selected="true"] { border-left-color: var(--good); }
.option.tight[aria-selected="true"] { border-left-color: var(--warn); }

.option-top { display: flex; align-items: center; gap: 6px; }
.option-top .rank { margin-left: auto; color: var(--text-2); font-size: var(--fs-xs); }
.option-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.option-sub { color: var(--text-2); font-size: var(--fs-xs); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.option-numbers { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.option-numbers small { display: block; color: var(--text-2); font-size: var(--fs-xs); }
.option-numbers b { display: block; white-space: nowrap; }
.option-numbers b.good { color: var(--good); }
.option-numbers b.warn { color: var(--warn); }
.option-numbers b.bad { color: var(--bad); }

.score { display: grid; gap: 4px; }
.score-line { display: flex; align-items: baseline; gap: 8px; font-size: var(--fs-xs); color: var(--text-2); flex-wrap: wrap; }
.score-line b { color: var(--text); font-size: var(--fs-m); }
.score-bar { display: flex; height: 6px; border-radius: 3px; overflow: hidden; background: var(--surface-2); }
.score-bar i { display: block; height: 100%; }
.score-bar .net { background: var(--good); }
.score-bar .drive { background: var(--route-empty); }
.score-bar .risk { background: var(--warn); }
.score-bar .ride { background: var(--route-after); }
.score-bar .balance { background: var(--text-2); }

.option-reason { color: var(--text-2); font-size: var(--fs-xs); }
.option-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--text-2); font-size: var(--fs-xs); }
.link { padding: 0; border: 0; background: none; color: var(--accent); font-size: var(--fs-xs); font-weight: 600; }

/* A result that is no longer current stays readable, but clearly not live. */
body[data-view-only] .options-panel .list, body[data-view-only] .summary { opacity: 0.6; }

/* ---- editor ---- */

dialog {
  width: min(860px, 92vw);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

dialog::backdrop { background: rgb(0 0 0 / 45%); }
dialog form { display: grid; gap: 12px; padding: 16px; }
dialog header { display: flex; justify-content: space-between; gap: 12px; }
dialog header h2 { font-size: var(--fs-l); }
dialog header p { color: var(--text-2); font-size: var(--fs-s); }
dialog textarea { height: min(56vh, 520px); padding: 10px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
dialog footer { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
dialog .error { margin-right: auto; color: var(--bad); font-size: var(--fs-s); }
dialog.modal { width: min(640px, 92vw); }

/* ---- forms, facts and secrets (login, dialogs, admin) ---- */

.form-stack { display: grid; gap: 12px; }
.form-field { display: grid; gap: 4px; color: var(--text-2); font-size: var(--fs-s); }
.form-field input, .form-field select, .form-field textarea { width: 100%; color: var(--text); font-size: var(--fs-m); }
.form-field small { font-size: var(--fs-xs); }
.form-field textarea { height: auto; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
.form-stack .check { white-space: normal; }
.inline-form { display: flex; align-items: end; gap: 8px; }
.inline-form .form-field { flex: 1; }

.facts { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 6px 16px; margin: 0; font-size: var(--fs-s); }
.facts dt { color: var(--text-2); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.snippet, .secret code { font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; overflow-wrap: anywhere; user-select: all; }
.snippet { display: block; margin: 6px 0; padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); color: var(--text); }
.secret { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); }
.secret code { flex: 1; font-size: var(--fs-l); }

/* ---- login and admin pages: a normal scrolling page under the bar ---- */

body.page { display: block; height: auto; overflow: auto; }
.page .appbar { height: var(--appbar); }
.page .menu { margin-left: auto; }

.card { display: grid; gap: 14px; width: min(380px, calc(100% - 32px)); margin: 12vh auto; padding: 24px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); }
.card h1 { font-size: var(--fs-xl); }
.card .button.primary { height: 36px; }

.admin { max-width: 1200px; margin: 0 auto; padding: 0 16px 32px; }
.admin .tabs { padding: 0; margin-bottom: 14px; background: none; }
.admin section { display: grid; gap: 10px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.section-head h1 { font-size: var(--fs-xl); }
.table-wrap { overflow-x: auto; }
.table-wrap td { vertical-align: middle; }
td.wrap { white-space: normal; overflow-wrap: anywhere; min-width: 240px; }
.actions { display: flex; gap: 6px; }
#audit-older { justify-self: start; }

/* ---- narrower screens ---- */

@media (max-width: 1240px) {
  .workspace { grid-template-columns: 280px minmax(0, 1fr) 340px; }
  .kpi.optional { display: none; }
}

@media (max-width: 1000px) {
  body { overflow: auto; grid-template-rows: auto auto auto; height: auto; }
  .appbar { flex-wrap: wrap; height: auto; padding: 8px 12px; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .list-panel, .options-panel { border: 0; border-bottom: 1px solid var(--border); }
  .list-panel .list { max-height: 42vh; }
  .options-panel .list { max-height: none; }
  .map-wrap { min-height: 46vh; }
  .summary { grid-auto-flow: row; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tile { border-bottom: 1px solid var(--border); }
}
