:root {
  --bg: #0f1419;
  --panel: #171d24;
  --text: #e8eef5;
  --muted: #93a1b3;
  --accent: #4da3ff;
  --available: #22c55e;
  --queue: #f59e0b;
  --unavailable: #ef4444;
  --border: #2a3441;
  --sheet-peek: 3.5rem;
  --mobile-header: 3.25rem;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  grid-template-areas: "sidebar map";
  height: 100vh;
  height: 100dvh;
  min-height: 0;
}

.mobile-header,
.sheet-handle,
.sheet-backdrop {
  display: none;
}

.sidebar {
  grid-area: sidebar;
  overflow: auto;
  border-right: 1px solid var(--border);
  background: linear-gradient(180deg, #121820, #0f1419);
  padding: 18px;
  min-height: 0;
}

.desktop-header h1 { margin: 0; font-size: 26px; }
.subtitle { color: var(--muted); margin-top: 4px; font-size: 13px; }

.panel {
  margin-top: 14px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

button {
  width: 100%;
  margin-top: 8px;
  border: 0;
  border-radius: 10px;
  padding: 11px 14px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
  font-size: 15px;
}

button:hover:not(:disabled) { filter: brightness(1.08); }
button:disabled { opacity: 0.6; cursor: wait; }

.filter-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

.field-label {
  display: block;
  margin-top: 0;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: #1b2430;
  color: var(--text);
  font-size: 16px;
}

.count-breakdown .yes { color: var(--available); }
.count-breakdown .no { color: var(--unavailable); }

.filter-btn {
  margin-top: 0;
  background: #243041;
  color: var(--text);
  font-size: 13px;
  padding: 10px 6px;
  min-height: 44px;
}

.filter-btn.active {
  background: var(--accent);
  color: white;
}

.source-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.chip input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.chip span {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #1b2430;
  font-size: 14px;
  transition: background 0.15s, border-color 0.15s;
}

.chip input:checked + span {
  background: rgba(77, 163, 255, 0.18);
  border-color: var(--accent);
  color: white;
}

.map-count {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.warning { color: #f59e0b; }
.success { color: #22c55e; }
.error { color: #ef4444; }

.meta {
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.list-panel {
  max-height: 280px;
  display: flex;
  flex-direction: column;
}

#stationList {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: auto;
  flex: 1;
}

#stationList li {
  margin-bottom: 8px;
  line-height: 1.35;
  cursor: pointer;
  border-radius: 10px;
  padding: 10px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  border: 1px solid transparent;
}

.list-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-top: 4px;
  flex-shrink: 0;
}

#stationList li.empty {
  cursor: default;
  color: var(--muted);
  border: 0;
  padding-left: 0;
}

#stationList li:hover,
#stationList li.active {
  background: rgba(77, 163, 255, 0.12);
  border-color: rgba(77, 163, 255, 0.25);
}

#stationList strong {
  color: white;
  font-size: 15px;
}

#stationList .tags { color: var(--muted); font-size: 12px; }

.legend-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
}

.legend-grid div {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.dot.available { background: var(--available); }
.dot.queue { background: var(--queue); }
.dot.unavailable { background: var(--unavailable); }
.dot.unknown { background: #64748b; }
.dot.tbank { background: #ffdd2d; }

#map {
  grid-area: map;
  width: 100%;
  height: 100%;
  min-height: 0;
  z-index: 1;
}

.leaflet-control-zoom {
  border: 0 !important;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35) !important;
}

.leaflet-control-zoom a {
  width: 40px !important;
  height: 40px !important;
  line-height: 40px !important;
  font-size: 20px !important;
}

@media (max-width: 768px) {
  .layout {
    display: block;
    position: relative;
    overflow: hidden;
  }

  .mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 500;
    height: calc(var(--mobile-header) + env(safe-area-inset-top, 0px));
    padding: env(safe-area-inset-top, 0px) 12px 0 16px;
    background: rgba(15, 20, 25, 0.92);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
  }

  .mobile-header__title {
    margin: 0;
    font-size: 17px;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
    margin: 0;
    padding: 0;
    border-radius: 12px;
    background: #243041;
    font-size: 22px;
    line-height: 1;
    flex-shrink: 0;
  }

  .icon-btn:disabled {
    opacity: 0.55;
  }

  #map {
    position: fixed;
    inset: 0;
    height: 100dvh;
    padding-top: calc(var(--mobile-header) + env(safe-area-inset-top, 0px));
    padding-bottom: calc(var(--sheet-peek) + env(safe-area-inset-bottom, 0px));
  }

  .sheet-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 600;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  .sheet-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 700;
    display: flex;
    flex-direction: column;
    max-height: calc(88dvh - env(safe-area-inset-top, 0px));
    padding: 0;
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: 18px 18px 0 0;
    background: #121820;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
    transform: translateY(calc(100% - var(--sheet-peek) - env(safe-area-inset-bottom, 0px)));
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  }

  .sidebar.open {
    transform: translateY(0);
  }

  .sidebar.open-full {
    max-height: calc(94dvh - env(safe-area-inset-top, 0px));
  }

  .sheet-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    margin: 0;
    padding: 10px 16px 12px;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--text);
    flex-shrink: 0;
  }

  .sheet-handle__bar {
    width: 42px;
    height: 5px;
    border-radius: 999px;
    background: #4b5563;
    margin-bottom: 8px;
  }

  .sheet-peek {
    font-size: 14px;
    font-weight: 600;
    color: var(--muted);
    text-align: center;
    width: 100%;
  }

  .sidebar__body {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 14px calc(16px + env(safe-area-inset-bottom, 0px));
    overscroll-behavior: contain;
  }

  .desktop-header {
    display: none;
  }

  .panel {
    margin-top: 10px;
    padding: 12px;
  }

  .list-panel {
    max-height: none;
    min-height: 180px;
  }

  #stationList li {
    padding: 12px;
  }

  #syncBtn {
    min-height: 48px;
    font-size: 16px;
  }

  .field-grid {
    grid-template-columns: 1fr;
  }

  .select {
    min-height: 48px;
  }

  .leaflet-top.leaflet-left {
    top: auto;
    bottom: calc(12px + var(--sheet-peek) + env(safe-area-inset-bottom, 0px));
    left: auto;
    right: 12px;
  }
}

@media (min-width: 769px) {
  .sidebar__body {
    display: block;
  }

  .sheet-handle,
  .sheet-peek {
    display: none !important;
  }
}