:root {
  --bg: #08100d;
  --bg-soft: #0a1410;
  --panel: #0d1714;
  --panel-2: #111f1a;
  --line: #1d2d26;
  --line-soft: #16241e;
  --text: #e9f4ee;
  --muted: #8ba198;
  --muted-2: #647a70;
  --mint: #7df3b2;
  --mint-2: #38d68b;
  --mint-dim: rgba(125, 243, 178, 0.12);
  --amber: #f5b83d;
  --cyan: #37d6e0;
  --red: #ff7b72;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --topbar-h: 64px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }
a { color: inherit; }
.hidden { display: none !important; }

/* ---------- Top bar ---------- */

.topbar {
  position: relative;
  z-index: 1200;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 18px;
  background:
    radial-gradient(120% 180% at 0% 0%, rgba(56, 214, 139, 0.14), transparent 45%),
    linear-gradient(180deg, #0b1511 0%, #09110e 100%);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  text-decoration: none;
  min-width: 0;
}

.brand-mark {
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  display: grid;
  place-items: center;
  border-radius: 11px;
  color: #06120c;
  background: linear-gradient(135deg, var(--mint) 0%, var(--mint-2) 100%);
  box-shadow: 0 0 24px rgba(56, 214, 139, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.brand-mark svg { width: 23px; height: 23px; }

.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.15; }
.brand-text strong {
  font-size: 15px;
  letter-spacing: 0.16em;
  font-weight: 700;
  color: var(--text);
}
.brand-text span {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
}

.topbar-right { display: flex; align-items: center; gap: 14px; }

.clock { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.1; }
.clock-time {
  font-variant-numeric: tabular-nums;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.04em;
}
.clock small { font-size: 9px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted-2); }

.conn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.conn i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 0 0 rgba(245, 184, 61, 0.5);
  animation: pulse 1.8s infinite;
}
.conn.is-live { color: var(--mint); border-color: rgba(125, 243, 178, 0.28); background: var(--mint-dim); }
.conn.is-live i { background: var(--mint); box-shadow: 0 0 10px var(--mint); animation: pulse-live 1.8s infinite; }
.conn.is-down { color: var(--red); border-color: rgba(255, 123, 114, 0.35); }
.conn.is-down i { background: var(--red); box-shadow: none; animation: none; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}
@keyframes pulse-live {
  0% { box-shadow: 0 0 0 0 rgba(125, 243, 178, 0.55); }
  70% { box-shadow: 0 0 0 7px rgba(125, 243, 178, 0); }
  100% { box-shadow: 0 0 0 0 rgba(125, 243, 178, 0); }
}

.icon-btn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.icon-btn:hover { color: var(--mint); border-color: rgba(125, 243, 178, 0.35); background: var(--mint-dim); }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn.small { width: 28px; height: 28px; border: 0; background: transparent; font-size: 20px; line-height: 1; }
.icon-btn.small:hover { background: rgba(255, 255, 255, 0.05); }

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: 344px minmax(0, 1fr);
  height: calc(100dvh - var(--topbar-h));
  min-height: 0;
}

.panel {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 16px 22px;
  border-right: 1px solid var(--line);
  background:
    radial-gradient(120% 80% at 0% 0%, rgba(56, 214, 139, 0.05), transparent 40%),
    var(--panel);
  scrollbar-width: thin;
  scrollbar-color: #243a30 transparent;
}
.panel::-webkit-scrollbar { width: 8px; }
.panel::-webkit-scrollbar-thumb { background: #22362c; border-radius: 8px; }

/* ---------- Stats ---------- */

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 11px 12px;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.025), rgba(255, 255, 255, 0));
}
.stat-value {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  color: var(--mint);
  letter-spacing: -0.01em;
}
.stat-value small { font-size: 12px; color: var(--muted); font-weight: 500; }
.stat-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.09em; color: var(--muted-2); }

/* ---------- Search ---------- */

.search-block { margin-top: 16px; position: relative; }
.field-label {
  display: block;
  margin-bottom: 7px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted-2);
  font-weight: 600;
}
.search-input {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--bg-soft);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search-input:focus-within {
  border-color: rgba(125, 243, 178, 0.5);
  box-shadow: 0 0 0 3px rgba(125, 243, 178, 0.09);
}
.search-input svg { width: 17px; height: 17px; color: var(--muted-2); flex: none; }
.search-input input {
  flex: 1;
  min-width: 0;
  padding: 11px 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 13.5px;
}
.search-input input::placeholder { color: var(--muted-2); }
.search-input input::-webkit-search-cancel-button { filter: invert(0.7); }

.results {
  display: none;
  position: absolute;
  z-index: 30;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  max-height: 320px;
  overflow-y: auto;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #0b1511;
  box-shadow: var(--shadow);
}
.results.open { display: block; }
.result {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 8px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  text-align: left;
}
.result:hover, .result:focus-visible { background: var(--mint-dim); outline: none; }
.result-route { flex: 1; min-width: 0; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result-speed { font-size: 11px; color: var(--muted-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.result-empty { padding: 9px 10px; font-size: 12px; color: var(--muted-2); }

.line-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 52px;
  padding: 5px 9px;
  border-radius: 7px;
  background: linear-gradient(135deg, rgba(125, 243, 178, 0.2), rgba(56, 214, 139, 0.08));
  border: 1px solid rgba(125, 243, 178, 0.28);
  color: var(--mint);
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.03em;
  font-variant-numeric: tabular-nums;
}
.line-pill.small { min-width: 44px; padding: 3px 7px; font-size: 11px; }

/* ---------- Filters ---------- */

.filters {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 16px;
  padding-top: 15px;
  border-top: 1px solid var(--line-soft);
}
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track {
  position: relative;
  width: 34px;
  height: 19px;
  flex: none;
  border-radius: 999px;
  background: #1b2b24;
  border: 1px solid #273b31;
  transition: background 0.18s, border-color 0.18s;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #82988e;
  transition: transform 0.18s, background 0.18s;
}
.switch input:checked + .track { background: rgba(56, 214, 139, 0.22); border-color: rgba(125, 243, 178, 0.5); }
.switch input:checked + .track::after { transform: translateX(15px); background: var(--mint); box-shadow: 0 0 8px rgba(125, 243, 178, 0.6); }
.switch input:disabled + .track { opacity: 0.4; }
.switch input:disabled ~ .switch-label { opacity: 0.4; }
.switch input:focus-visible + .track { outline: 2px solid rgba(125, 243, 178, 0.5); outline-offset: 2px; }
.switch-label { font-size: 12.5px; color: var(--muted); }

/* ---------- Selected card ---------- */

.card {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    radial-gradient(120% 100% at 100% 0%, rgba(56, 214, 139, 0.09), transparent 55%),
    var(--panel-2);
  padding: 13px;
  animation: slide-in 0.2s ease-out;
}
@keyframes slide-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: none; }
}

.selected-head { display: flex; align-items: center; gap: 8px; }
.sel-state {
  margin-left: auto;
  margin-right: 4px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(255, 123, 114, 0.12);
  border: 1px solid rgba(255, 123, 114, 0.3);
  color: var(--red);
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.selected-head .icon-btn { margin-left: auto; }
.selected-head .sel-state + .icon-btn { margin-left: 0; }

.route {
  margin-top: 9px;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.01em;
  word-break: break-word;
}

.selected-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 7px;
  margin-top: 12px;
}
.selected-grid > div {
  padding: 8px 9px;
  border-radius: 9px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid var(--line-soft);
}
.selected-grid b {
  display: block;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  line-height: 1.1;
}
.selected-grid small { font-size: 9.5px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted-2); }

.coords { margin-top: 9px; font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.trip { margin-top: 3px; font-size: 10.5px; color: var(--muted-2); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; word-break: break-all; }

.selected-actions { display: flex; align-items: center; gap: 9px; margin-top: 12px; }
.primary-btn {
  padding: 8px 13px;
  border: 0;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--mint), var(--mint-2));
  color: #05120c;
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
  transition: filter 0.15s, transform 0.12s;
}
.primary-btn:hover { filter: brightness(1.06); }
.primary-btn:active { transform: translateY(1px); }
.ghost-link { font-size: 12px; color: var(--muted); text-decoration: none; }
.ghost-link:hover { color: var(--mint); }

/* ---------- Legend & footer ---------- */

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line-soft);
}
.legend-title { width: 100%; font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted-2); font-weight: 600; }
.legend-row { display: inline-flex; align-items: center; gap: 5px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.legend-row i { width: 8px; height: 8px; border-radius: 50%; background: var(--c); box-shadow: 0 0 7px color-mix(in srgb, var(--c) 55%, transparent); }

.panel-foot { margin-top: 18px; padding-top: 13px; border-top: 1px solid var(--line-soft); color: var(--muted-2); }
.panel-foot p { margin: 0 0 6px; font-size: 10.5px; line-height: 1.55; }
.panel-foot .feed-line { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11.5px; }
.dot-sep { opacity: 0.5; }

/* ---------- Map ---------- */

.map-wrap { position: relative; min-width: 0; min-height: 0; background: #0a1210; }
#map { position: absolute; inset: 0; outline: none; background: #0a1210; }

body:not(.light) .leaflet-tile-pane {
  filter: invert(1) hue-rotate(180deg) brightness(0.86) contrast(0.92) saturate(0.62);
}
body.light .leaflet-tile-pane { filter: none; }

.leaflet-container {
  background: #0a1210;
  font-family: inherit;
}
body.light .leaflet-container { background: #e8efe9; }

.map-hud {
  position: absolute;
  z-index: 500;
  left: 14px;
  top: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: calc(100% - 90px);
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(9, 17, 14, 0.82);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.3);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hud-dot {
  width: 7px;
  height: 7px;
  flex: none;
  border-radius: 50%;
  background: var(--mint);
  box-shadow: 0 0 8px var(--mint);
  animation: pulse 1.8s infinite;
}

.panel-toggle {
  display: none;
  position: absolute;
  z-index: 1100;
  right: 14px;
  top: 14px;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(9, 17, 14, 0.9);
  color: var(--text);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.panel-toggle svg { width: 19px; height: 19px; }

/* Leaflet theming */
.leaflet-bar { border: 1px solid var(--line) !important; box-shadow: 0 8px 24px rgba(0,0,0,.35) !important; }
.leaflet-bar a {
  background: #0d1714 !important;
  color: var(--text) !important;
  border-bottom-color: var(--line) !important;
}
.leaflet-bar a:hover { background: #16241e !important; color: var(--mint) !important; }
.leaflet-control-scale-line {
  background: rgba(9, 17, 14, 0.75) !important;
  border-color: rgba(255, 255, 255, 0.5) !important;
  color: var(--text) !important;
  text-shadow: none !important;
}
.leaflet-control-attribution {
  background: rgba(9, 17, 14, 0.78) !important;
  color: #9fb3a9 !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: #b9d9c9 !important; }
body.light .leaflet-control-attribution { background: rgba(255,255,255,.8) !important; color: #33443c !important; }
body.light .leaflet-control-attribution a { color: #0f5c3b !important; }

.leaflet-tooltip.bus-tooltip {
  background: #0b1511;
  border: 1px solid #263a30;
  color: var(--text);
  border-radius: 9px;
  padding: 7px 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  font-size: 12px;
  line-height: 1.35;
}
.leaflet-tooltip.bus-tooltip::before { border-top-color: #263a30; }
.tooltip-line { font-weight: 700; color: var(--mint); }
.tooltip-route { color: var(--muted); }
.tooltip-meta { margin-top: 3px; font-size: 10.5px; color: var(--muted-2); font-variant-numeric: tabular-nums; }

/* ---------- Light theme ---------- */

body.light {
  --bg: #eef4f0;
  --bg-soft: #ffffff;
  --panel: #f7faf8;
  --panel-2: #ffffff;
  --line: #d5e1da;
  --line-soft: #e2ebe6;
  --text: #10241b;
  --muted: #4d6659;
  --muted-2: #7a9287;
  --mint: #0a8f57;
  --mint-2: #16b06b;
  --mint-dim: rgba(10, 143, 87, 0.09);
  --shadow: 0 18px 50px rgba(20, 50, 35, 0.16);
}
body.light .topbar { background: linear-gradient(180deg, #ffffff, #f4f8f5); }
body.light .brand-text strong { color: #0c2016; }
body.light .brand-mark { color: #03130b; }
body.light .conn.is-live { color: #067a49; }
body.light .results { background: #ffffff; }
body.light .map-hud,
body.light .panel-toggle { background: rgba(255, 255, 255, 0.9); color: var(--text); }
body.light .leaflet-bar a { background: #ffffff !important; color: #16291f !important; }
body.light .leaflet-bar a:hover { background: #eef6f1 !important; color: #067a49 !important; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  z-index: 2000;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 14px);
  max-width: min(92vw, 520px);
  padding: 10px 15px;
  border-radius: 11px;
  border: 1px solid rgba(255, 123, 114, 0.35);
  background: rgba(24, 12, 11, 0.95);
  color: #ffd9d5;
  font-size: 12.5px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .layout { grid-template-columns: 300px minmax(0, 1fr); }
}

@media (max-width: 820px) {
  :root { --topbar-h: 58px; }
  .topbar { padding: 0 12px; gap: 8px; }
  .brand-mark { width: 34px; height: 34px; flex-basis: 34px; border-radius: 10px; }
  .brand-mark svg { width: 20px; height: 20px; }
  .brand-text strong { font-size: 13px; }
  .brand-text span { font-size: 9.5px; }
  .clock { display: none; }
  .conn { padding: 6px 9px; font-size: 11px; }
  .layout { display: block; position: relative; }
  .panel {
    position: absolute;
    z-index: 1050;
    inset: 0 auto 0 0;
    width: min(88vw, 340px);
    padding: 14px 14px 20px;
    transform: translateX(-102%);
    transition: transform 0.24s ease;
    box-shadow: 24px 0 60px rgba(0, 0, 0, 0.45);
  }
  .panel.open { transform: none; }
  .map-wrap { position: absolute; inset: 0; }
  .panel-toggle { display: grid; }
  .map-hud { left: 12px; top: 12px; max-width: calc(100% - 72px); font-size: 11px; padding: 7px 10px; }
  .toast { bottom: 16px; }
}

@media (max-width: 380px) {
  .brand-text span { display: none; }
  .conn span { display: none; }
  .conn { padding: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
