[hidden] { display: none !important; }
:root {
  --bg: #f3f5f8; --card: #fff; --ink: #2b3038; --muted: #5f6b7a; --line: #e2e6eb;
  --brand: #0b5cad; --brand-ink: #fff; --navy: #0b2a5b; --green: #2f9e44; --lime: #8dc63f;
  --ok: #2b8a3e; --warn: #e67700; --bad: #c92a2a;
  --ok-bg: #ebfbee; --warn-bg: #fff4e6; --bad-bg: #fff5f5;
  --chart-line: #0a7fc9; --chart-line2: #2f9e44; --chart-line3: #e8590c; --chart-line4: #7048e8; --chart-line5: #c2255c; --chart-line6: #0c8599; --chart-base: #868e96;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --card: #1a1f25; --ink: #e9ecef; --muted: #9aa5b1; --line: #2c333b;
    --ok-bg: #12301a; --warn-bg: #3a2a10; --bad-bg: #3b1616;
    --chart-line: #4dabf7; --chart-line2: #69db7c; --chart-line3: #ffa94d; --chart-line4: #b197fc; --chart-line5: #f783ac; --chart-line6: #66d9e8; --brand: #4d9de0; --chart-base: #adb5bd;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
h1, h2, h3 { margin: 0 0 .25rem; line-height: 1.2; }
h2 { font-size: 1.15rem; } h3 { font-size: 1rem; margin-top: 1.25rem; }
a { color: var(--brand); }
.muted { color: var(--muted); } .small { font-size: .85rem; }

.top { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .6rem 1rem; background: #fff; color: #2b3038; border-bottom: 3px solid; border-image: linear-gradient(90deg, var(--navy), #0a8fd6, var(--green), var(--lime)) 1;
  position: sticky; top: 0; z-index: 5; }
.top .muted { color: #5f6b7a; }
.top .btn { background: #fff; color: #2b3038; border-color: #d5dae1; }
.top .btn.primary { background: #0b5cad; border-color: #0b5cad; color: #fff; }
.logo { height: 42px; width: auto; display: block; }
.product { font-size: .85rem; color: #5f6b7a; padding-left: .75rem; border-left: 1px solid #d5dae1; }
@media (max-width: 560px) { .logo { height: 34px; } .product, #user { display: none; } }
.landing #signInBtn { display: none !important; }
.brand { display: flex; align-items: center; gap: .6rem; }
.actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }

main { max-width: 1100px; margin: 0 auto; padding: 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 1rem; margin-top: 1rem; }
.section-head { display: flex; justify-content: space-between; align-items: flex-start; gap: .75rem; flex-wrap: wrap; margin-top: .5rem; }
.row { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.empty { text-align: center; padding: 4rem 1rem; }

.btn { font: inherit; padding: .45rem .85rem; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--ink); cursor: pointer; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .6; cursor: wait; }
select, input { font: inherit; padding: .45rem .6rem; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--ink); width: 100%; }
select { width: auto; }
.badge { font-size: .75rem; padding: .15rem .5rem; border-radius: 99px; font-weight: 600; }
.badge.demo { background: var(--warn-bg); color: var(--warn); }

.form .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem; }
.form label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); }

.routes { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: .75rem; margin-top: .75rem; }
.route { text-align: left; font: inherit; color: inherit; background: var(--card); border: 1px solid var(--line); border-left: 5px solid var(--muted);
  border-radius: 12px; padding: .85rem 1rem; cursor: pointer; }
.route.sel { outline: 2px solid var(--chart-line); }
.route.ok { border-left-color: var(--ok); } .route.warn { border-left-color: var(--warn); } .route.bad { border-left-color: var(--bad); }
.route .name { font-weight: 600; }
.route .big { font-size: 1.6rem; font-weight: 700; margin: .25rem 0 0; }
.route .delay { font-size: .85rem; font-weight: 600; padding: .1rem .45rem; border-radius: 6px; display: inline-block; }
.route .err { color: var(--bad); font-size: .8rem; margin-top: .35rem; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .75rem; margin: 1rem 0; }
.kpi { border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem; }
.kpi span { display: block; color: var(--muted); font-size: .8rem; }
.kpi .kv { font-size: 1.15rem; font-weight: 700; }
.kpi .kv > div { display: flex; gap: .4rem; align-items: baseline; }
.kdir { font-size: .75rem; font-weight: 600; color: var(--muted); min-width: 2.5rem; }
.chart-wrap { position: relative; height: 300px; }

.table-wrap { overflow: auto; max-height: 420px; border: 1px solid var(--line); border-radius: 8px; }
thead th { position: sticky; top: 0; background: var(--card); }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
td.alert { color: var(--bad); font-weight: 600; }

.toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: .6rem 1rem; border-radius: 8px; font-size: .9rem; max-width: calc(100% - 2rem); }

/* Both-direction cards */
.route { display: flex; flex-direction: column; justify-content: flex-start; }
.dir-row { display: grid; grid-template-columns: minmax(2.6rem, max-content) 1fr auto auto; align-items: baseline; gap: .5rem; margin: .35rem 0; }
.dir-row .dir { font-weight: 600; font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 6.5rem; }
.dir-row .t { font-size: 1.25rem; font-weight: 700; white-space: nowrap; }
.dir-row .delay { white-space: nowrap; font-size: .8rem; font-weight: 600; padding: .1rem .45rem; border-radius: 6px; background: var(--line); }
.dir-row.ok .delay { background: var(--ok-bg); color: var(--ok); }
.dir-row.warn .delay { background: var(--warn-bg); color: var(--warn); }
.dir-row.bad .delay { background: var(--bad-bg); color: var(--bad); }
.err-dot { background: var(--bad); color: #fff; border-radius: 99px; width: 1.1rem; height: 1.1rem; display: grid; place-items: center; font-size: .7rem; font-weight: 700; }
.err { color: var(--bad); }
.form .check { flex-direction: row; align-items: center; gap: .5rem; margin-top: .75rem; color: var(--ink); font-size: .9rem; }
.form .check input { width: auto; }
.reverse-box { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; margin-top: .75rem; padding: .6rem .8rem;
  border: 1px dashed var(--line); border-radius: 10px; }
.reverse-box label { display: flex; align-items: center; gap: .35rem; color: var(--muted); }
.reverse-box input { width: 7rem; }

/* Landing hero with background image (falls back to a brand gradient) */
.hero-landing { min-height: calc(100vh - 64px); display: grid; place-items: center; margin: -1rem; padding: 2rem 1rem;
  background: linear-gradient(180deg, rgba(11,42,91,.35), rgba(11,42,91,.65)), url("img/bg.jpg") center 40% / cover no-repeat,
              linear-gradient(135deg, #0b2a5b 0%, #0a6fb8 55%, #2f9e44 100%); }
.landing main { max-width: none; }
.hero-card { background: #fff; color: #2b3038; border-radius: 16px; padding: 2rem 1.75rem; max-width: 460px; width: 100%;
  text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.hero-card .muted { color: #5f6b7a; }
.hero-logo { width: 100%; max-width: 320px; height: auto; margin: 0 auto 1rem; display: block; }
.hero-card h1 { font-size: 1.35rem; margin-bottom: .5rem; }
.btn.big { padding: .7rem 1.4rem; font-size: 1rem; margin: .75rem 0; background: #0b5cad; border-color: #0b5cad; color: #fff; }

/* Banner above the dashboard */
.banner { margin-top: .25rem; position: relative; border-radius: 14px; overflow: hidden; color: #fff; padding: 1.5rem 1.25rem; margin-bottom: 1rem; min-height: 130px;
  display: flex; align-items: flex-end;
  background: linear-gradient(90deg, rgba(11,42,91,.9) 0%, rgba(11,42,91,.55) 55%, rgba(11,42,91,.15) 100%), url("img/bg.jpg") center 55% / cover no-repeat,
              linear-gradient(135deg, #0b2a5b 0%, #0a6fb8 60%, #2f9e44 100%); }
.banner h1 { font-size: 1.4rem; margin: .1rem 0 .25rem; }
.eyebrow { margin: 0; text-transform: uppercase; letter-spacing: .12em; font-size: .72rem; font-weight: 700; color: var(--lime); }
.banner-sub { margin: 0; font-size: .9rem; opacity: .9; max-width: 36rem; }
@media (max-width: 560px) { .banner h1 { font-size: 1.15rem; } }

.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 1.5rem 1rem 2rem; }
.landing .foot { display: none; }

#coordBox input { width: 11rem; }

.check-inline { display: flex; align-items: center; gap: .4rem; color: var(--ink); }
.check-inline input { width: auto; }
#pubUrl { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .2rem .4rem; word-break: break-all; }

/* Route map */
.map-card { margin-top: 1rem; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.map { height: 300px; background: var(--bg); }
.map-legend { display: flex; flex-wrap: wrap; gap: .4rem 1rem; padding: .5rem .75rem; color: var(--muted); }
.map-legend .sw { display: inline-block; width: 18px; height: 4px; border-radius: 2px; vertical-align: middle; margin-right: .35rem; }
.map-empty { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: .9rem; padding: 1rem; text-align: center; }
.pin { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 13px;
  border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); }
.leaflet-container { font: inherit; }

/* Day-on-day comparison */
.cmp-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 1rem; }
.cmp th, .cmp td { white-space: nowrap; }
.cmp tbody tr:nth-child(3n) td, .cmp tbody tr:nth-child(3n) th { border-bottom: 2px solid var(--line); }
.cmp-dir { vertical-align: top; font-weight: 700; color: var(--ink); }
.delta.good { color: var(--ok); font-weight: 600; }
.delta.bad { color: var(--bad); font-weight: 600; }

.pub-links { display: grid; gap: .35rem; width: 100%; }
#shareUrl { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .2rem .4rem; word-break: break-all; }

/* Public read-only view: hide all staff controls */
.public-view #importToggle, .public-view #addRouteToggle, .public-view #routeForm, .public-view #importForm,
.public-view #pubBox, .public-view #coordBox, .public-view #reverseBox, .public-view #pollBtn,
.public-view #signInBtn, .public-view #signOutBtn, .public-view #heroSignIn, .public-view .hero-card .muted.small { display: none !important; }
.public-view .section-head h2 { visibility: visible; }

/* Public view: no raw readings table */
.public-view .readings-section { display: none !important; }

/* Public view: no raw data download */
.public-view #csvBtn, .public-view .kpi-readings { display: none !important; }

/* Edit panel */
.edit-box { margin-top: .75rem; border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem; background: var(--card); }
.edit-box summary { cursor: pointer; font-weight: 600; font-size: .9rem; }
.edit-box .form { margin-top: .75rem; }
.edit-box .form .grid + .grid { margin-top: .75rem; }
.paused { color: var(--warn); font-weight: 600; }
.public-view #editBox { display: none !important; }

/* Footer link */
.foot a { color: inherit; font-weight: 600; }

/* Photo backdrop behind the whole page, softened so cards and text stay readable */
body { background: linear-gradient(rgba(243,245,248,.86), rgba(243,245,248,.92)), url("img/bg.jpg") center 30% / cover fixed no-repeat, var(--bg); }
@media (prefers-color-scheme: dark) {
  body { background: linear-gradient(rgba(17,20,24,.86), rgba(17,20,24,.92)), url("img/bg.jpg") center 30% / cover fixed no-repeat, var(--bg); }
}
@media (max-width: 760px) { body { background-attachment: scroll; } }
.landing .foot { display: block; color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,.6); margin-top: -3.5rem; position: relative; }

/* Contractor logos */
.title-wrap { display: flex; align-items: center; gap: .9rem; }
.site-logo { max-height: 56px; max-width: 180px; width: auto; height: auto; object-fit: contain; background: #fff; border-radius: 6px; padding: 4px; border: 1px solid var(--line); }
.partner-logo { max-height: 40px; max-width: 160px; width: auto; height: auto; object-fit: contain; }
.logo-edit { margin-top: .75rem; display: grid; gap: .35rem; }
.logo-edit input[type=file] { width: auto; }
@media (max-width: 560px) { .partner-logo { max-height: 30px; max-width: 110px; } .site-logo { max-height: 44px; max-width: 120px; } }

/* ---------- Install bar ---------- */
.install-bar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(12px + env(safe-area-inset-bottom)); z-index: 20;
  display: flex; align-items: center; gap: .7rem; width: min(560px, calc(100% - 24px)); padding: .65rem .75rem;
  background: #fff; color: #2b3038; border-radius: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.25); border: 1px solid #e2e6eb; }
.install-bar img { border-radius: 10px; flex: none; }
.install-text { display: flex; flex-direction: column; font-size: .85rem; line-height: 1.3; flex: 1; min-width: 0; }
.install-text span { color: #5f6b7a; }
.install-bar .btn { flex: none; }
.install-bar .btn.ghost { border: 0; padding: .4rem .5rem; color: #5f6b7a; background: transparent; }
#alertBtn.on { border-color: var(--ok); color: var(--ok); font-weight: 600; }

/* ---------- Installed app: respect phone notches ---------- */
@media (display-mode: standalone) {
  .top { padding-top: calc(.6rem + env(safe-area-inset-top)); }
}

/* ---------- Phone layout ---------- */
@media (max-width: 640px) {
  main { padding: .75rem; }
  .top { padding: .5rem .75rem; gap: .5rem; }
  .actions { gap: .35rem; }
  .actions .btn { padding: .5rem .7rem; font-size: .85rem; }
  .btn { min-height: 40px; }
  select, input:not([type=checkbox]):not([type=file]) { min-height: 40px; font-size: 16px; } /* 16px stops iPhone zooming into fields */
  input[type=checkbox] { width: 20px; height: 20px; flex: none; }
  .banner { padding: 1rem; min-height: 96px; border-radius: 12px; }
  .banner-sub { font-size: .8rem; }
  .card { padding: .8rem; border-radius: 12px; }
  .section-head { flex-direction: column; align-items: stretch; }
  .section-head .row { width: 100%; }
  .section-head .row > * { flex: 1 1 auto; }
  .title-wrap { align-items: flex-start; }
  .routes { grid-template-columns: 1fr; }
  .map { height: 230px; }
  .chart-wrap { height: 240px; }
  .kpis { grid-template-columns: repeat(2, 1fr); gap: .5rem; }
  .kpi { padding: .5rem .6rem; }
  .kpi .kv { font-size: 1rem; }
  .cmp { font-size: .8rem; }
  .cmp th, .cmp td { padding: .4rem .45rem; }
  table { font-size: .82rem; }
  .table-wrap { max-height: 340px; }
  .reverse-box input:not([type=checkbox]), #coordBox input { width: 100%; }
  .reverse-box label { flex: 1 1 100%; }
  .install-text { font-size: .8rem; }
  .foot { padding-bottom: calc(5.5rem + env(safe-area-inset-bottom)); } /* room for the install bar */
}

/* Direction rows: label on its own line so place names fit */
.dir-row { grid-template-columns: 1fr auto auto; row-gap: .1rem; }
.dir-row .dir { grid-column: 1 / -1; max-width: none; white-space: normal; }
.kpi .kv > div { flex-wrap: wrap; }
.kdir { min-width: 0; }

/* Public share page: fixed 2-hour window, no range picker */
.public-view #period { display: none !important; }
.public-view #user { display: none; }

/* Keep header, install bar and messages above the map (Leaflet uses z-index up to 1000) */
.top { z-index: 1100; }
.install-bar { z-index: 1200; }
.toast { z-index: 1300; }
.map-card { position: relative; z-index: 0; }

.public-view #alertBtn { display: none !important; }

/* ---------- Roles ---------- */
/* Client users: same dashboard, no editing or admin tools */
.client-view #importToggle, .client-view #addRouteToggle, .client-view #routeForm, .client-view #importForm,
.client-view #pollBtn, .client-view #pubBox, .client-view #coordBox, .client-view #reverseBox, .client-view #editBox,
.client-view #adminArea { display: none !important; }
body:not(.admin) #adminArea { display: none !important; }

/* ---------- Super Admin sections ---------- */
.admin-area { display: grid; gap: 1rem; margin-top: 1rem; }
.admin-card > summary { cursor: pointer; display: flex; align-items: baseline; gap: .75rem; flex-wrap: wrap; list-style: none; }
.admin-card > summary::-webkit-details-marker { display: none; }
.admin-card > summary h2 { margin: 0; }
.admin-card > summary::before { content: "▸"; color: var(--muted); }
.admin-card[open] > summary::before { content: "▾"; }
.admin-tools { margin: .9rem 0 .4rem; }
.admin-tools input[type=month] { width: auto; }
.admin-card .form { margin: .9rem 0; }
.site-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: .35rem .9rem; margin-bottom: .75rem; }
tr.total td { border-top: 2px solid var(--line); }
.row-actions { white-space: nowrap; }
.row-actions .btn { padding: .25rem .55rem; font-size: .8rem; }

.site-checks label { flex-direction: row !important; align-items: center; gap: .45rem; color: var(--ink) !important; }
.site-checks input { width: auto; }

/* ---------- Route choice ---------- */
.fast-chip { display: inline-block; font-size: .68rem; font-weight: 700; color: #fff; background: var(--ok); border-radius: 99px; padding: .05rem .45rem; vertical-align: middle; }
.choice-box { margin-top: .75rem; border: 1px solid var(--ok); background: var(--ok-bg); border-radius: 10px; padding: .6rem .8rem; display: grid; gap: .35rem; }
.choice-row { display: flex; flex-wrap: wrap; gap: .25rem .9rem; align-items: baseline; font-size: .9rem; }
.choice-dir { font-weight: 700; }
.choice-best { color: var(--ok); font-weight: 700; }
.choice-alt { color: var(--muted); }
.route-edit { border-top: 1px dashed var(--line); padding-top: .6rem; margin-top: .6rem; }
.small-btn { padding: .25rem .6rem; font-size: .8rem; margin-top: .4rem; }
#altBox textarea { font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: .5rem .6rem; width: 100%; }
#altBox label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); margin-top: .6rem; }
#altBox label.check { flex-direction: row; color: var(--ink); }
.client-view #altBox, .public-view #altBox { display: none !important; }

/* ---------- Email sign-in ---------- */
.email-signin { text-align: left; margin: 1rem 0 .25rem; }
.email-signin label { display: block; margin-bottom: .35rem; color: #2b3038; font-weight: 600; }
.email-signin .row { flex-wrap: nowrap; }
.email-signin input { flex: 1; min-width: 0; color: #2b3038; background: #fff; border-color: #d5dae1; }
.email-signin p { margin: .5rem 0 0; color: #2b3038; }
.email-signin p.err { color: var(--bad); }
.or-line { display: flex; align-items: center; gap: .6rem; margin: .9rem 0 .2rem; }
.or-line::before, .or-line::after { content: ""; flex: 1; height: 1px; background: #e2e6eb; }
.btn.ghost-strong { background: #fff; color: #2b3038; border: 1px solid #c9d0d8; padding: .65rem 1.2rem; margin: .5rem 0 .75rem; }
@media (max-width: 480px) { .email-signin .row { flex-wrap: wrap; } .email-signin .btn { width: 100%; } }
/* Queue length */
.q-chip { display: inline-block; font-size: .68rem; font-weight: 700; color: #fff; background: #e8590c; border-radius: 99px; padding: .05rem .45rem; white-space: nowrap; }
#queueSection .chart-wrap { height: 220px; }
.dir-row .busy-chip, .dir-row .q-chip { grid-column: 1 / -1; justify-self: start; }

/* VMS sign preview */
.vms-box { margin-top: .75rem; display: flex; flex-wrap: wrap; gap: .75rem; }
.vms-sign { background: #111; border: 3px solid #333; border-radius: 8px; padding: .5rem .9rem; min-width: 210px; }
.vms-sign .vms-name { color: #adb5bd; font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.vms-sign .vms-msg { color: #ffb000; font-family: "Courier New", monospace; font-weight: 700; font-size: 1.35rem; letter-spacing: .06em; min-height: 1.7rem; white-space: pre; line-height: 1.25; text-align: center; min-height: 5.2rem; display: flex; align-items: center; justify-content: center; }
.vms-sign .vms-msg.blank { color: #555; font-size: .9rem; }
.vms-edit { margin-top: .4rem; }
.vms-url { word-break: break-all; font-size: .75rem; }
.busy-chip { display: inline-block; font-size: .68rem; font-weight: 700; color: #fff; background: #7048e8; border-radius: 99px; padding: .05rem .45rem; }
.edit-sub { margin: 1rem 0 .25rem; font-size: .95rem; }
.vms-sign { min-width: 230px; }
.vms-page { color: #ffb000; }
.site-pick { display: flex; align-items: center; gap: .5rem; margin: -.25rem 0 .75rem; padding-bottom: .75rem; border-bottom: 1px solid var(--line); }
.site-pick label { font-weight: 600; font-size: .85rem; color: var(--muted); }
.site-pick select { flex: 1; min-width: 0; font-size: 1rem; padding: .45rem .5rem; border: 1px solid var(--line); border-radius: 8px; background: var(--card, #fff); color: inherit; }
@media (min-width: 761px) { #allSites { display: none; } }
#emailAlertBtn.on, #alertBtn.on { border-color: var(--ok); color: var(--ok); }
