:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --border: #e2e5ea;
  --accent: #1f5fd6;
  --accent-text: #ffffff;
  --chip: #eef1f5;
  --row-hover: #f2f5fa;
  --red: #c62828;
  --red-bg: #fdecea;
  --amber: #a15c00;
  --amber-bg: #fff4e0;
  --green: #2e7d32;
  --gray: #9aa1ad;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1a1d23;
    --text: #e8eaee;
    --muted: #98a0ad;
    --border: #2b3039;
    --accent: #5b8cff;
    --accent-text: #0b0d10;
    --chip: #252a33;
    --row-hover: #20242b;
    --red: #ff6b6b;
    --red-bg: #3a1d1f;
    --amber: #f0b153;
    --amber-bg: #3a2c16;
    --green: #5cc46a;
    --gray: #6c7480;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.top h1 { margin: 0; font-size: 18px; }

.user-box { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 32px 16px;
}
.center { text-align: center; }
.error { border-color: #d64545; margin-bottom: 16px; }
.muted { color: var(--muted); font-size: 13px; }

.btn {
  font: inherit;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); padding: 4px 10px; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.toolbar input[type="search"] {
  flex: 1 1 220px;
  font: inherit;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.check { display: flex; align-items: center; gap: 6px; font-size: 14px; }
#last-sync { margin-left: auto; }

.summary { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { background: var(--chip); border-radius: 999px; padding: 3px 10px; font-size: 13px; }
.chip-strong { background: var(--accent); color: var(--accent-text); }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--row-hover); }
th {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
th.sorted { color: var(--text); }
th.sorted::after { content: " ▲"; font-size: 10px; }
th.sorted[data-dir="desc"]::after { content: " ▼"; }

.num { text-align: right; font-variant-numeric: tabular-nums; }
.strong { font-weight: 600; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: var(--muted); }
.muted-cell { color: var(--muted); font-size: 13px; }
tr.quiet td { opacity: .55; }

@media (max-width: 640px) {
  .hide-sm { display: none; }
  .wrap-sm { white-space: normal; }
  .next-cell { min-width: 0; }
  .next-cell .next-when { display: block; margin-left: 16px; }
  #last-sync { margin-left: 0; width: 100%; }
  th, td { padding: 8px; }
}

/* ---- Maintenance status ---- */
.toolbar select {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.chip-overdue { background: var(--red-bg); color: var(--red); font-weight: 600; }
.chip-soon { background: var(--amber-bg); color: var(--amber); font-weight: 600; }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; flex: none; background: var(--gray); }
.dot-overdue { background: var(--red); }
.dot-soon { background: var(--amber); }
.dot-ok { background: var(--green); }

.when-overdue { color: var(--red); font-weight: 600; }
.when-soon { color: var(--amber); font-weight: 600; }

tr.clickable { cursor: pointer; }
tr.clickable:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.next-cell { white-space: normal; min-width: 200px; }
.next-cell .next-name { margin-right: 8px; }
.next-cell .next-when { font-size: 13px; color: var(--muted); }
.next-cell .when-overdue, .next-cell .when-soon { color: inherit; }
.next-cell .next-when.when-overdue { color: var(--red); }
.next-cell .next-when.when-soon { color: var(--amber); }

/* ---- Truck dialog ---- */
dialog#truck {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
}
dialog#truck::backdrop { background: rgba(0, 0, 0, .45); }
.dlg-head {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.dlg-head h2 { margin: 0 0 2px; font-size: 20px; }
.dlg-head p { margin: 0; }
#t-body { padding: 8px 16px 16px; }

.sched { margin-top: 12px; }
.sched-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 6px; }
.sched-head h3 { margin: 0; font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

.item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.item-overdue { border-left-color: var(--red); }
.item-soon { border-left-color: var(--amber); }
.item-ok { border-left-color: var(--green); }
.item-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.item-name { display: flex; align-items: center; font-weight: 600; }
.item-due { margin-top: 4px; font-size: 14px; }
.item-meta { margin-top: 2px; font-size: 13px; color: var(--muted); }
.item-tasks { margin-top: 6px; font-size: 13px; }
.item-tasks summary { cursor: pointer; color: var(--accent); }
.item-tasks ul { margin: 6px 0 0; padding-left: 20px; }
.btn-sm { padding: 4px 10px; font-size: 13px; white-space: nowrap; flex: none; }

.done-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 8px;
  align-items: end;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}
.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.field input {
  font: inherit;
  font-size: 14px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  min-width: 0;
}

/* ---- Tabs ---- */
.tabs { display: flex; gap: 4px; margin-bottom: 14px; border-bottom: 1px solid var(--border); }
.tabs a {
  padding: 8px 14px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }

/* ---- Schedules tab ---- */
#sched-intro { margin: 0 0 12px; }
.group-empty { opacity: .65; }
.truck-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip-btn { border: none; font: inherit; font-size: 13px; color: var(--text); cursor: pointer; }
.chip-btn:hover { background: var(--row-hover); outline: 1px solid var(--border); }
.sched-block { margin-top: 16px; }
.sched-table th { cursor: default; }
.sched-table td { white-space: normal; vertical-align: top; }
.sched-table .interval-cell { white-space: nowrap; }
.sched-table td:first-child { min-width: 180px; }
.details-cell { min-width: 220px; }
@media (max-width: 640px) {
  /* Stack each service as a small card instead of a wide table. */
  .sched-table thead { display: none; }
  .sched-table tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
  .sched-table tbody tr:last-child { border-bottom: none; }
  .sched-table td { padding: 0; border: none; min-width: 0 !important; }
  .sched-table .interval-cell { text-align: right; font-size: 13px; max-width: 42vw; white-space: normal; }
  .sched-table .details-cell { grid-column: 1 / -1; }
  .sched-table .details-cell:empty { display: none; }
}

/* ---- Service log tab ---- */
.log-filters { align-items: flex-end; }
.log-filters .field select, .log-filters .field input {
  font: inherit;
  font-size: 14px;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
#log-summary { margin: 0 0 8px; }
.log-table th { cursor: default; }
.wrap-cell { white-space: normal; }
.link-btn {
  font: inherit;
  font-weight: 600;
  color: var(--accent);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
.show-sm { display: none; }
@media (max-width: 640px) {
  .show-sm { display: block; }
  .log-filters .field { flex: 1 1 140px; }
}

/* ---- Fleet setup toggles ---- */
.setup {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 14px;
}
.setup h3 { margin: 0 0 2px; font-size: 16px; }
.setup > p { margin: 0 0 10px; font-size: 13px; }
.toggle {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
  cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch {
  flex: none;
  position: relative;
  width: 40px;
  height: 22px;
  margin-top: 1px;
  border-radius: 999px;
  background: var(--gray);
  transition: background .15s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s;
}
.toggle input:checked + .switch { background: var(--accent); }
.toggle input:checked + .switch::after { transform: translateX(18px); }
.toggle input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }
.toggle input:disabled + .switch { opacity: .6; }
.toggle-text { display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
.toggle-title { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 14px; font-weight: 600; color: var(--text); }
.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  background: var(--amber-bg);
  color: var(--amber);
  vertical-align: middle;
}

/* ---- Schedules list + popup ---- */
.group-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 12px 14px;
  margin-bottom: 8px;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.group-row:hover { background: var(--row-hover); }
.group-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.group-main { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.group-name { font-weight: 600; }
.group-main .muted { font-size: 13px; }
.chev { font-size: 22px; line-height: 1; color: var(--muted); }
dialog#schedule {
  width: min(860px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
}
dialog#schedule::backdrop { background: rgba(0, 0, 0, .45); }
#s-body { padding: 4px 16px 16px; }
.footnote { font-size: 12px; margin-top: 16px; }

/* Fleet setup collapses to one line */
details.setup { padding: 0; }
details.setup > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  padding: 12px 16px;
}
details.setup > summary::-webkit-details-marker { display: none; }
details.setup > summary::after { content: "▾"; margin-left: auto; color: var(--muted); }
details.setup[open] > summary::after { content: "▴"; }
.setup-title { font-weight: 700; }
#setup-summary { font-size: 13px; }
details.setup > p, details.setup > #setup-toggles { margin: 0; padding: 0 16px; }
details.setup > #setup-toggles { padding-bottom: 6px; }
details.setup > p { padding-bottom: 8px; font-size: 13px; }

.show-sm-inline { display: none; }

@media (max-width: 640px) {
  main { padding: 12px; }
  .show-sm-inline { display: inline; }
  .tabs { gap: 0; }
  .tabs a { flex: 1 1 auto; text-align: center; padding: 10px 6px; font-size: 13px; }
  .toolbar { gap: 8px; }
  .toolbar input[type="search"] { flex-basis: 100%; }
  .toolbar select { flex: 1; }

  /* Popups take the whole screen on phones. */
  dialog#truck, dialog#schedule {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: none;
    border-radius: 0;
  }
  .dlg-head { padding: 12px; }
  #t-body, #s-body { padding: 4px 12px 24px; }
}
