:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1b1e23;
  --muted: #5f6672;
  --border: #d9dde3;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --good: #1a7f37;
  --warn: #b35900;
  --bad: #c62828;
  --q4: #1a9850;
  --q3: #91cf60;
  --q2: #f0a020;
  --q1: #d73027;
  --shadow: 0 1px 2px rgb(0 0 0 / 8%), 0 2px 8px rgb(0 0 0 / 6%);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1d2026;
    --surface-2: #262a31;
    --text: #e8eaee;
    --muted: #a0a7b3;
    --border: #353a43;
    --accent: #4c8dff;
    --accent-text: #0b1020;
    --good: #4ac26b;
    --warn: #f0a44a;
    --bad: #ff6b6b;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
  .leaflet-tile-pane { filter: brightness(0.8) contrast(1.1) saturate(0.8); }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* --- header --- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; margin: 0; font-size: 17px; font-weight: 650; }
.brand-icon { width: 22px; height: 22px; fill: var(--accent); }
.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); min-width: 0; }
.status .text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.status.live .dot { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }
.status.stale .dot { background: var(--warn); }
.status.down .dot { background: var(--bad); }

/* --- layout: map on top (phone), map left + panel right (wide screens) --- */
.layout { display: block; }
/* isolation keeps Leaflet's high z-indexes inside the map, so they never cover the header */
.map-wrap { position: relative; isolation: isolate; height: 52vh; height: 52dvh; min-height: 260px; }
#map { position: absolute; inset: 0; background: var(--surface-2); }
.side { background: var(--bg); }
.tabs {
  position: sticky; top: var(--header-h, 49px); z-index: 900;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.tabs button {
  border: 0; background: none; padding: 11px 4px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; font-weight: 550;
}
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.panel { padding: 14px 16px 32px; padding-bottom: max(32px, env(safe-area-inset-bottom)); }

@media (min-width: 900px) {
  .layout { display: grid; grid-template-columns: 1fr 400px; height: calc(100vh - var(--header-h, 49px)); height: calc(100dvh - var(--header-h, 49px)); }
  .map-wrap { height: 100%; }
  .side { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--border); }
  .tabs { position: static; }
  .panel { overflow-y: auto; flex: 1; }
}

/* --- map overlays --- */
.map-buttons { position: absolute; right: 10px; bottom: 22px; z-index: 800; display: flex; flex-direction: column; gap: 8px; }
.map-btn {
  width: 42px; height: 42px; display: grid; place-items: center; cursor: pointer;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); box-shadow: var(--shadow);
}
.map-btn svg { width: 22px; height: 22px; fill: var(--text); }
.placing-hint {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%); z-index: 850;
  background: var(--accent); color: var(--accent-text); padding: 8px 14px; border-radius: 999px;
  box-shadow: var(--shadow); font-weight: 550; max-width: calc(100% - 32px); text-align: center;
}
.placing-hint .link { color: inherit; }
.leaflet-container { font: inherit; }

.bus-marker { display: grid; place-items: center; }
.bus-marker .pin {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); border: 3px solid #fff; box-shadow: 0 2px 6px rgb(0 0 0 / 35%);
}
.bus-marker.stale .pin { background: #8a929e; }
.bus-marker .pin svg { width: 18px; height: 18px; fill: #fff; transition: transform .4s; }
.bus-label {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 1px 6px; font-size: 12px; font-weight: 600; white-space: nowrap; box-shadow: var(--shadow);
}
.stop-label { font-size: 12px; font-weight: 600; }
.leaflet-tooltip.stop-label { background: var(--surface); color: var(--text); border-color: var(--border); }
.receiver-marker {
  width: 26px; height: 26px; border-radius: 6px; display: grid; place-items: center;
  background: #6b4fbb; border: 2px solid #fff; box-shadow: 0 1px 4px rgb(0 0 0 / 35%);
}
.receiver-marker svg { width: 16px; height: 16px; fill: #fff; }

/* --- cards and lists --- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 6px; font-size: 16px; }
.card .headline { font-size: 18px; font-weight: 650; margin: 2px 0 8px; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px 14px; margin: 0; }
.facts div { min-width: 0; }
.facts dt { font-size: 12px; color: var(--muted); }
.facts dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.muted { color: var(--muted); }
.hint { color: var(--muted); margin: 0 0 12px; }
.small { font-size: 13px; }
.error { color: var(--bad); margin: 8px 0 0; min-height: 1em; }
.warning-card { border-color: color-mix(in srgb, var(--warn) 55%, var(--border)); background: color-mix(in srgb, var(--warn) 9%, var(--surface)); }
.warning-card p { margin: 0; }
.ok { color: var(--good); }
.bad { color: var(--bad); }
.warnc { color: var(--warn); }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar input, .toolbar select, .stop-form input:not([type="range"]) {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); min-width: 0;
}
.toolbar select { flex: 1; min-width: 120px; }
.icon-btn { width: 38px; height: 38px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); cursor: pointer; font-size: 20px; line-height: 1; }
.check { display: flex; align-items: center; gap: 6px; }

button.primary, button.secondary {
  border-radius: 8px; padding: 9px 14px; cursor: pointer; font-weight: 600; border: 1px solid transparent;
}
button.primary { background: var(--accent); color: var(--accent-text); }
button.secondary { background: var(--surface-2); border-color: var(--border); }
button.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
button.link { background: none; border: 0; padding: 0; text-decoration: underline; cursor: pointer; font-weight: 600; }
#btn-add-stop, #btn-admin-login { width: 100%; margin-bottom: 12px; }

.events { list-style: none; margin: 0; padding: 0; }
.events li {
  display: grid; grid-template-columns: 74px 22px 1fr; gap: 8px; align-items: start;
  padding: 10px 2px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.events li:last-child { border-bottom: 0; }
.events .time { font-variant-numeric: tabular-nums; font-weight: 600; }
.events .kind { font-size: 16px; line-height: 1.3; text-align: center; }
.events .what small { display: block; color: var(--muted); }
.events .empty { display: block; cursor: default; color: var(--muted); border: 0; }
.day-divider { list-style: none; font-size: 12px; color: var(--muted); padding: 10px 2px 2px; }

.stops { list-style: none; margin: 0; padding: 0; }
.stops li {
  display: flex; align-items: center; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--border);
}
.stops li .grow { flex: 1; min-width: 0; }
.stops li .name { font-weight: 600; overflow-wrap: anywhere; }
.stops li button { padding: 6px 10px; }
.stop-form label { display: block; margin-bottom: 10px; font-weight: 600; }
.stop-form label input { display: block; width: 100%; margin-top: 4px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grow { flex: 1; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 12px; font-size: 13px; }
.legend span { display: flex; align-items: center; gap: 5px; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 50%; }
.sw.q4 { background: var(--q4); } .sw.q3 { background: var(--q3); } .sw.q2 { background: var(--q2); } .sw.q1 { background: var(--q1); }
.sw.gap { border-radius: 0; height: 0; width: 18px; border-top: 3px dashed var(--q1); }

/* --- login --- */
.overlay {
  position: fixed; inset: 0; z-index: 2000; display: grid; place-items: center; padding: 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(4px);
}
.login-card { width: min(360px, 100%); display: grid; gap: 10px; }
.login-card h2 { margin: 0; }
.login-card input { padding: 11px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.login-card .hint { margin: 0; }
.login-card code, .card code {
  font: 13px ui-monospace, SFMono-Regular, Consolas, monospace; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; white-space: nowrap;
}
