:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --border: #dde3e8;
  --text: #1f2937;
  --muted: #6b7280;
  --brand: #1d4ed8;
  --brand-dark: #1e3a8a;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber-bg: #fef3c7;
}

* { box-sizing: border-box; }
body { margin: 0; font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif; background: var(--bg); color: var(--text); font-size: 16px; }
a { color: var(--brand); }
.wrap { max-width: 900px; margin: 0 auto; padding: 0 20px; }
.main { padding: 40px 20px; }
.login-main { display: flex; align-items: center; justify-content: center; min-height: 100vh; flex-direction: column; }
.login-box { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 32px; width: 100%; max-width: 380px; }
.login-box h1 { margin-top: 0; font-size: 1.4rem; text-align: center; }
.powered-by { color: var(--muted); font-size: 0.8rem; margin-top: 16px; }

.stacked-form { display: flex; flex-direction: column; gap: 14px; max-width: 480px; }
.stacked-form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 0.9rem; }
.stacked-form label[hidden] { display: none; } /* a same-specificity author rule setting display:flex above would otherwise beat the browser's own [hidden]{display:none} */
.stacked-form input, .stacked-form select { font-size: 1rem; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-weight: 400; }
.inline-form { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.inline-form input, .inline-form select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; }

.btn { display: inline-block; padding: 10px 18px; border-radius: 8px; border: 1px solid transparent; font-size: 0.95rem; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-outline { background: #fff; border-color: var(--border); color: var(--text); }
.btn-outline.is-active { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-danger { color: var(--red); border-color: var(--red); }
.btn-sm { padding: 6px 12px; font-size: 0.85rem; }
.link-btn { background: none; border: none; color: var(--muted); cursor: pointer; padding: 0; font-size: 0.85rem; text-decoration: underline; }

.notice { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; }
.notice-warn { background: var(--amber-bg); }
.notice-danger { background: var(--red-bg); color: #7f1d1d; font-weight: 600; }
.notice-ok { background: var(--green-bg); color: #14532d; font-weight: 600; }
.row-danger { background: var(--red-bg); }
.flash { padding: 12px 16px; margin: 0 0 16px; }
.flash-success { background: var(--green-bg); color: #14532d; }
.flash-error { background: var(--red-bg); color: #7f1d1d; }

.admin-shell { display: flex; min-height: 100vh; }
.admin-sidebar { width: 220px; background: #111827; color: #e5e7eb; display: flex; flex-direction: column; padding: 20px 0; flex-shrink: 0; }
.admin-brand { font-weight: 700; padding: 0 20px 16px; font-size: 1.1rem; }
.admin-nav { display: flex; flex-direction: column; flex: 1; }
.admin-nav a { color: #cbd5e1; text-decoration: none; padding: 12px 20px; font-size: 0.95rem; }
.admin-nav a.active, .admin-nav a:hover { background: #1f2937; color: #fff; }
.admin-sidebar-footer { padding: 16px 20px; border-top: 1px solid #1f2937; }
.admin-whoami { font-size: 0.8rem; color: #9ca3af; margin-bottom: 8px; }
.admin-main { flex: 1; min-width: 0; }
.admin-content { padding: 32px; max-width: 1100px; }

.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 1.5rem; }

.stat-row { display: flex; gap: 16px; margin-bottom: 24px; flex-wrap: wrap; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 18px 22px; min-width: 160px; }
.stat-num { font-size: 2rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.85rem; }

.admin-table { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.admin-table th, .admin-table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.admin-table th { background: #f9fafb; font-size: 0.8rem; text-transform: uppercase; color: var(--muted); }
.admin-table img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.roster-photo { width: 48px; }
.photo-placeholder { width: 40px; height: 40px; border-radius: 6px; background: #e5e7eb; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; background: #e5e7eb; font-size: 0.78rem; font-weight: 600; }
.badge-board { background: var(--green-bg); color: #14532d; }
.badge-defect { background: var(--red-bg); color: #7f1d1d; }

/* ---- Scan station ---- */
.scan-status { padding: 20px; border-radius: 10px; font-size: 1.4rem; font-weight: 700; text-align: center; margin-bottom: 16px; }
.scan-status-idle { background: #e5e7eb; color: var(--text); }
.scan-status-busy { background: #dbeafe; color: #1e3a8a; }
.scan-status-board { background: var(--green-bg); color: #14532d; }
.scan-status-leave { background: var(--amber-bg); color: #78350f; }
.scan-status-error { background: var(--red-bg); color: #7f1d1d; }
.scan-area { position: relative; max-width: 480px; margin: 0 auto 16px; }
#scan-video { width: 100%; border-radius: 12px; background: #000; aspect-ratio: 4/3; object-fit: cover; }
.scan-off-placeholder {
  display: flex; align-items: center; justify-content: center; width: 100%; aspect-ratio: 4/3;
  border-radius: 12px; background: #111827; color: #6b7280; font-weight: 600; font-size: 1.05rem;
}
.scan-off-placeholder[hidden] { display: none; }
.scan-hidden-input { position: absolute; opacity: 0.02; width: 1px; height: 1px; }
.scan-result { display: flex; align-items: center; gap: 16px; max-width: 480px; margin: 0 auto; }
.scan-result img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; }

/* The scan on/off control: big, fixed to the bottom, and always in the
   same spot -- this gets tapped every time a kid boards or leaves, so it
   needs to be unmissable and not require scrolling to reach. */
.scan-toggle-spacer { height: 100px; }
.scan-toggle-btn {
  position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 150;
  min-height: 76px; border-radius: 16px; border: none;
  background: var(--green); color: #fff; font-size: 1.4rem; font-weight: 800;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25); cursor: pointer;
}
.scan-toggle-btn.is-active { background: var(--red); }
.scan-toggle-btn:disabled { background: #9ca3af; cursor: default; }

/* ---- Pre-trip inspection ---- */
.pretrip-progress { position: sticky; top: 0; background: var(--bg); padding: 10px 0; font-weight: 700; z-index: 5; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.pretrip-section { border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; margin-bottom: 16px; background: var(--card); }
.pretrip-section legend { font-weight: 700; padding: 0 6px; display: flex; align-items: center; justify-content: space-between; width: 100%; gap: 12px; }
.pretrip-section-ok { font-weight: 600; }
.pretrip-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.pretrip-item:last-child { border-bottom: none; }
.pretrip-item-label { flex: 1; min-width: 160px; font-size: 0.95rem; }
.pretrip-item-buttons { display: flex; gap: 8px; }
.pretrip-btn { min-width: 84px; min-height: 44px; padding: 10px 16px; border-radius: 8px; border: 2px solid var(--border); background: #fff; font-weight: 700; cursor: pointer; font-size: 0.9rem; }
.pretrip-btn-ok.is-active { background: var(--green); border-color: var(--green); color: #fff; }
.pretrip-btn-defect.is-active { background: var(--red); border-color: var(--red); color: #fff; }
.pretrip-btn-extra.is-active { background: var(--muted); border-color: var(--muted); color: #fff; }
.pretrip-note { width: 100%; margin-top: 8px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 0.9rem; }

.signature-block { margin: 20px 0; }
.signature-pad { display: block; width: 100%; max-width: 600px; height: 180px; border: 2px solid var(--border); border-radius: 10px; background: #fff; touch-action: none; cursor: crosshair; }
.signature-actions { display: flex; align-items: center; gap: 12px; margin-top: 8px; }

/* ---- Time clock ---- */
.clock-current { padding: 16px; border-radius: 10px; background: var(--card); border: 1px solid var(--border); font-size: 1.1rem; margin-bottom: 16px; }
.clock-current.duty-driving { background: var(--green-bg); }
.clock-current.duty-onDuty { background: var(--amber-bg); }
.clock-current.duty-offDuty { background: #e5e7eb; }
.clock-buttons { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 24px; }
.clock-btn { min-width: 140px; min-height: 64px; padding: 14px 20px; border-radius: 10px; border: 2px solid var(--border); background: #fff; font-weight: 700; font-size: 1rem; cursor: pointer; }
.clock-btn.duty-driving { border-color: var(--green); }
.clock-btn.duty-onDuty { border-color: #d97706; }
.clock-btn.duty-offDuty { border-color: var(--muted); }
.clock-btn.active { color: #fff; }
.clock-btn.active.duty-driving { background: var(--green); }
.clock-btn.active.duty-onDuty { background: #d97706; }
.clock-btn.active.duty-offDuty { background: var(--muted); }

.hos-summary { max-width: 600px; }
.hos-summary h3 { margin: 20px 0 8px; font-size: 1rem; }
.limit-row { margin-bottom: 10px; }
.limit-label { font-size: 0.9rem; margin-bottom: 4px; }
.limit-track { height: 10px; border-radius: 999px; background: #e5e7eb; overflow: hidden; }
.limit-fill { height: 100%; }
.limit-fill.limit-ok { background: var(--green); }
.limit-fill.limit-warn { background: #d97706; }
.limit-fill.limit-over { background: var(--red); }

/* ---- Drive lock (public/app.js) ---- */
#drive-lock-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  color: #fff;
  z-index: 999999;
  display: flex;
  flex-direction: column;
}
#drive-lock-overlay[hidden] { display: none; }
.drive-lock-content {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  padding: 24px;
  box-sizing: border-box;
  text-align: center;
}
.drive-lock-clock { font-size: 6rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1; }
.drive-lock-date { font-size: 1.4rem; color: #9ca3af; margin-top: 12px; }
.drive-lock-manifest { width: 100%; max-width: 480px; }
.drive-lock-manifest h2 { font-size: 1.3rem; margin-bottom: 16px; }
.drive-lock-manifest ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.drive-lock-manifest li { display: flex; align-items: center; gap: 14px; background: #1f2937; border-radius: 10px; padding: 12px 16px; font-size: 1.15rem; text-align: left; }
.drive-lock-manifest img { width: 48px; height: 48px; object-fit: cover; border-radius: 50%; }
.drive-lock-map { width: 100%; max-width: 700px; height: 60vh; border-radius: 12px; overflow: hidden; }
.drive-lock-switcher { display: flex; gap: 20px; padding: 24px; justify-content: center; flex-wrap: wrap; }
.drive-lock-switcher button {
  min-width: 220px;
  min-height: 100px;
  padding: 20px 32px;
  border-radius: 16px;
  border: 3px solid #4b5563;
  background: #1f2937;
  color: #fff;
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
}
.drive-lock-switcher button.active { background: var(--brand); border-color: var(--brand); }
.drive-lock-footer { text-align: center; color: #6b7280; font-size: 0.8rem; padding-bottom: 14px; }

/* ---- Live map ---- */
.live-map { height: 520px; border-radius: 10px; overflow: hidden; margin-bottom: 12px; border: 1px solid var(--border); }
.stops-map { height: 480px; border-radius: 10px; overflow: hidden; margin-bottom: 16px; border: 1px solid var(--border); }
.route-arrow div { font-size: 1.2rem; color: #1d4ed8; text-align: center; line-height: 24px; text-shadow: 0 0 3px #fff, 0 0 3px #fff; }
.stop-search-results { margin-bottom: 12px; }
.stop-search-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.stop-search-list button { text-align: left; }
.route-directions-list { padding-left: 20px; }
.route-directions-list li { margin-bottom: 8px; font-size: 0.95rem; }
.stop-actions { display: flex; gap: 6px; align-items: center; }
.stop-actions .inline-form { margin-bottom: 0; }

/* ---- Printable badge ---- */
.badge-layout { display: flex; gap: 24px; align-items: flex-start; margin-bottom: 24px; flex-wrap: wrap; }
.badge-card { width: 280px; border: 2px solid var(--border); border-radius: 14px; padding: 20px; text-align: center; background: #fff; }
.badge-photo { width: 100px; height: 100px; object-fit: cover; border-radius: 50%; margin-bottom: 10px; }
.badge-name { font-weight: 700; font-size: 1.1rem; }
.badge-meta { color: var(--muted); font-size: 0.85rem; margin-bottom: 10px; }
.badge-qr svg { width: 160px; height: 160px; }
.badge-layout form { display: inline-block; margin: 4px 8px 4px 0; }

@media print {
  body * { visibility: hidden; }
  #printable-badge, #printable-badge * { visibility: visible; }
  #printable-badge { position: absolute; top: 0; left: 0; }
}

@media (max-width: 720px) {
  .admin-shell { flex-direction: column; }
  .admin-sidebar { width: 100%; }
  .admin-nav { flex-direction: row; flex-wrap: wrap; }
  .admin-content { padding: 16px; }
}

/* ---- Driver tablet shell (lib/layout.js tabletLayout) ---- */
body.tablet { font-size: 19px; padding-top: 76px; }
.tablet-header {
  position: fixed; top: 0; left: 0; right: 0; height: 56px; z-index: 300;
  background: #111827; color: #fff; display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; gap: 12px;
}
.tablet-clock { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.1rem; min-width: 100px; }
.tablet-header-mid { flex: 1; text-align: center; font-weight: 600; opacity: 0.85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tablet-late-toggle {
  border: 2px solid #4b5563; background: #1f2937; color: #fff; border-radius: 10px;
  padding: 8px 14px; font-weight: 700; font-size: 0.95rem; cursor: pointer; white-space: nowrap;
}
.tablet-late-toggle.is-late { background: var(--red); border-color: var(--red); }
.tablet-late-toggle:disabled { opacity: 0.5; cursor: default; }

.tablet-late-wrap { position: relative; }
.tablet-late-picker {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 320;
  background: #1f2937; border: 1px solid #374151; border-radius: 12px; padding: 10px;
  display: flex; flex-direction: column; gap: 8px; min-width: 180px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
}
.tablet-late-option {
  border: 2px solid #4b5563; background: #111827; color: #fff; border-radius: 8px;
  padding: 12px 14px; font-weight: 700; font-size: 1rem; cursor: pointer; text-align: left;
}
.tablet-late-option.tablet-late-clear { border-color: var(--green); color: var(--green); }
.tablet-late-picker[hidden] { display: none; }

.tablet-drawer-handle {
  position: fixed; top: 56px; left: 50%; transform: translateX(-50%); z-index: 301;
  width: 90px; height: 22px; background: #1f2937; border-radius: 0 0 12px 12px;
  display: flex; align-items: center; justify-content: center; cursor: grab; touch-action: none;
}
.tablet-drawer-grip { width: 40px; height: 5px; border-radius: 999px; background: #6b7280; }
.tablet-drawer-handle .tablet-unread-dot { position: absolute; top: 2px; right: 12px; width: 10px; height: 10px; padding: 0; border-radius: 50%; background: var(--red); }

/* Closed sits fully tucked behind the header (translated up by its own
   height PLUS the header's height) so nothing can peek out above the
   header -- translateY(-100%) alone only pushes it flush with the
   header's bottom edge, not fully behind it. Header/handle also carry a
   higher z-index than the drawer as a second line of defense. */
.tablet-drawer {
  /* A fixed px cap, not vh -- viewport-height units are unreliable on
     mobile browsers (dynamic address-bar/toolbar chrome resizes them),
     and tablets in portrait are comfortably tall enough for this. */
  position: fixed; top: 56px; left: 0; right: 0; z-index: 250; max-height: 600px; overflow-y: auto;
  background: #111827; color: #fff; padding: 30px 20px 20px; display: flex; flex-direction: column; gap: 4px;
  transform: translateY(calc(-100% - 56px)); transition: transform 0.25s ease;
}
.tablet-drawer.dragging { transition: none; }
.tablet-drawer.open { transform: translateY(0); }
.tablet-drawer a { color: #e5e7eb; text-decoration: none; padding: 14px 10px; font-size: 1.1rem; border-bottom: 1px solid #1f2937; display: flex; align-items: center; gap: 10px; }
.tablet-drawer a.active { color: #93c5fd; font-weight: 700; }
.tablet-drawer-footer { margin-top: 12px; padding-top: 12px; border-top: 1px solid #1f2937; display: flex; flex-direction: column; gap: 10px; }
.tablet-drawer-footer a, .tablet-drawer-footer .link-btn { color: #cbd5e1; }

.tablet-drawer-backdrop {
  position: fixed; inset: 0; top: 56px; background: rgba(0, 0, 0, 0.4); z-index: 240;
  opacity: 0; pointer-events: none; transition: opacity 0.25s ease;
}
.tablet-drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.tablet-content { padding: 20px 16px 40px; max-width: 700px; margin: 0 auto; }

.tablet-unread-badge {
  display: inline-flex; align-items: center; justify-content: center; min-width: 22px; height: 22px;
  padding: 0 6px; border-radius: 999px; background: var(--red); color: #fff; font-size: 0.8rem; font-weight: 700;
}
.tablet-unread-badge[hidden] { display: none; }

/* ---- Manifest list (scan station / tablet home) ---- */
.manifest-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.manifest-list li { display: flex; align-items: center; gap: 14px; background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; font-size: 1rem; }
.manifest-list img { width: 44px; height: 44px; object-fit: cover; border-radius: 50%; }

/* ---- Messages ---- */
.message-list { display: flex; flex-direction: column; gap: 14px; }
.message-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
.message-card.message-unread { border-left: 4px solid var(--brand); }
.message-meta { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.message-body { margin: 0 0 10px; white-space: pre-wrap; }
.message-replies { margin-bottom: 10px; display: flex; flex-direction: column; gap: 6px; }
.message-reply { background: var(--bg); border-radius: 8px; padding: 8px 12px; font-size: 0.92rem; }
.message-reply-form input[type="text"] { flex: 1; min-width: 160px; }
