* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #1a1a2e;
  color: #e0e0e0;
  height: 100vh;
  height: -webkit-fill-available; /* iOS Safari full height */
  display: flex;
  overflow: hidden;
}

#map { flex: 1; height: 100vh; }

#panel {
  width: 280px;
  min-width: 280px;
  background: #16213e;
  border-left: 2px solid #EC0016;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* iPhone notch + home-indicator safe areas */
  padding-top:    env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* ── Header ─────────────────────────────────────────────────────── */
#panel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: #0f0a0a;
  border-bottom: 1px solid #2a0a0a;
  flex-shrink: 0;
}
.db-logo {
  background: #EC0016;
  color: white;
  font-weight: 900;
  font-size: 16px;
  width: 38px;
  height: 38px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.route-label { font-size: 14px; font-weight: 700; color: #fff; }
#status { font-size: 11px; color: #666; margin-top: 1px; }
#status.ok  { color: #4caf50; }
#status.err { color: #ef5350; }
#train-count-box { margin-left: auto; text-align: center; flex-shrink: 0; }
#train-count { display: block; font-size: 24px; font-weight: 800; color: #EC0016; line-height: 1; }
#train-count-box small { font-size: 10px; color: #555; }

/* ── Search mode tabs ────────────────────────────────────────────── */
#search-tabs {
  display: flex;
  gap: 6px;
  padding: 10px 12px 0;
  flex-shrink: 0;
}
.stab {
  flex: 1;
  padding: 7px 0;
  background: #0f1a2e;
  border: 1.5px solid #2a3a5a;
  border-radius: 7px;
  color: #556;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
}
.stab.active        { background: #EC0016; border-color: #EC0016; color: #fff; }
.stab:hover:not(.active) { border-color: #EC0016; color: #ccc; }

/* ── Train number search ─────────────────────────────────────────── */
#trainnr-search {
  padding: 12px;
  border-bottom: 1px solid #1a1a3a;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#trainnr-input {
  width: 100%;
  background: #0f1a2e;
  color: #e0e0e0;
  border: 1.5px solid #2a3a5a;
  border-radius: 7px;
  padding: 10px 12px;
  font-size: 13px;
  outline: none;
  box-sizing: border-box;
  transition: border-color 0.15s;
}
#trainnr-input:focus       { border-color: #EC0016; }
#trainnr-input::placeholder { color: #445; }
#trainnr-btn {
  background: #EC0016; color: white; border: none;
  border-radius: 8px; padding: 10px;
  font-size: 14px; font-weight: 700; cursor: pointer;
  transition: background 0.15s;
}
#trainnr-btn:hover    { background: #c4000e; }
#trainnr-btn:disabled { background: #555; cursor: default; }

/* ── Stop list (train number mode) ──────────────────────────────── */
.stop-list { padding: 4px 10px 10px; }
.stop-row {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 0;
  border-bottom: 1px solid #12203a;
}
.stop-row:last-child { border-bottom: none; }
.stop-row.stop-done  { opacity: 0.35; }
.stop-pin  { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.stop-time { font-size: 11px; color: #889; min-width: 38px; flex-shrink: 0; }
.stop-name { font-size: 12px; color: #ccc; flex: 1; min-width: 0;
             white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stop-delay-ok   { font-size: 10px; color: #4caf50; flex-shrink: 0; }
.stop-delay-late { font-size: 10px; color: #f9a825; flex-shrink: 0; }

/* ── Route search ────────────────────────────────────────────────── */
#route-search {
  padding: 12px;
  border-bottom: 1px solid #1a1a3a;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;   /* establishes stacking context */
  z-index: 10;          /* sits above #route-info so the dropdown is never buried */
}

.station-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.station-label {
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  color: #555;
  width: 34px;
  flex-shrink: 0;
  letter-spacing: 0.05em;
}

.autocomplete-wrap {
  position: relative;
  flex: 1;
}

.autocomplete-wrap input {
  width: 100%;
  background: #0f1a2e;
  color: #e0e0e0;
  border: 1.5px solid #2a3a5a;
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}
.autocomplete-wrap input:focus { border-color: #EC0016; }
.autocomplete-wrap input::placeholder { color: #445; }

.ac-dropdown {
  /* position/top/left/width set by JS (position:fixed) — immune to parent overflow/z-index */
  background: #0f1a2e;
  border: 1.5px solid #2a3a5a;
  border-radius: 7px;
  z-index: 99999;
  max-height: 200px;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}

.ac-item {
  padding: 9px 12px;
  cursor: pointer;
  font-size: 13px;
  color: #ccc;
  border-bottom: 1px solid #1a2a4a;
}
.ac-item:last-child { border-bottom: none; }
.ac-item:hover { background: #1a2a4a; color: #fff; }
.ac-item small { display: block; font-size: 10px; color: #556; margin-top: 1px; }

.swap-row {
  display: flex;
  justify-content: center;
}
#swap-btn {
  background: #1a1a3a;
  border: 1px solid #2a2a5a;
  color: #667;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
}
#swap-btn:hover { background: #2a2a5a; color: #ccc; }

#find-btn {
  background: #EC0016;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
  margin-top: 2px;
}
#find-btn:hover { background: #c4000e; }
#find-btn:disabled { background: #555; cursor: default; }

/* ── Route info / train list ─────────────────────────────────────── */
#route-info {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* ── Journey cards (connection picker) ───────────────────────────── */
.journey-card {
  background: #0f1a2e;
  border: 1px solid #1a2a4a;
  border-radius: 8px;
  padding: 9px 11px;
  cursor: pointer;
  transition: border-color 0.15s;
  display: flex;
  align-items: center;
  gap: 10px;
}
.journey-card:hover  { border-color: #EC0016; }
.journey-card.selected { border-color: #EC0016; background: #1a0a0a; }
.journey-past { opacity: 0.38; }

.jc-times { display:flex; flex-direction:column; align-items:center; gap:1px; min-width:40px; flex-shrink:0; }
.jc-dep   { font-size:15px; font-weight:800; color:#fff; line-height:1.1; }
.jc-arr   { font-size:11px; color:#556; }

.jc-info  { flex:1; min-width:0; }
.jc-trains { display:block; font-size:12px; font-weight:700; color:#ddd; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.jc-meta   { display:block; font-size:10px; color:#556; margin-top:2px; }

.jc-now { width:8px; height:8px; border-radius:50%; background:#EC0016; flex-shrink:0; animation: blink 1.4s ease-in-out infinite; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.25} }

/* ── Journey leg breakdown ───────────────────────────────────────── */
#leg-details { padding: 4px 10px 10px; }
.leg-row { display:flex; align-items:center; gap:7px; padding:5px 0; border-bottom:1px solid #12203a; }
.leg-row:last-child { border-bottom:none; }
.leg-badge { font-size:10px; font-weight:800; padding:2px 7px; border-radius:3px; color:#fff; flex-shrink:0; }
.leg-stops { flex:1; min-width:0; }
.leg-name  { font-size:11px; color:#bbb; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; display:block; }
.leg-time  { font-size:10px; color:#445; display:block; margin-top:1px; }
.leg-dot   { width:7px; height:7px; border-radius:50%; background:#EC0016; flex-shrink:0; }

#route-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #555;
  padding: 10px 12px 6px;
  flex-shrink: 0;
}

#train-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 10px 10px;
}

.train-card {
  background: #0f1a2e;
  border: 1px solid #1a2a4a;
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.train-card:hover { border-color: #EC0016; }
.train-card.selected { border-color: #EC0016; background: #1a0a0a; }

.train-card-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
}

.tc-badge {
  font-size: 11px;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 4px;
  color: white;
  flex-shrink: 0;
}

.tc-dir { font-size: 12px; color: #aaa; flex: 1; }

.tc-delay-ok   { font-size: 11px; color: #4caf50; font-weight: 600; }
.tc-delay-late { font-size: 11px; color: #f9a825; font-weight: 600; }

.tc-near {
  font-size: 11px;
  color: #666;
  display: flex;
  align-items: center;
  gap: 4px;
}
.tc-near .dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }

.tc-time { font-size: 10px; color: #445; margin-top: 3px; }

.tc-loading { font-size: 12px; color: #555; font-style: italic; }

.muted { font-size: 12px; color: #445; padding: 8px 0; }

/* ── Selected train detail ───────────────────────────────────────── */
#selected-train {
  margin: 8px 10px 0;
  background: #0f1a2e;
  border-radius: 8px;
  border: 1px solid #2a3a5a;
  flex-shrink: 0;
  max-height: 54vh;       /* never swallows the whole panel */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#train-detail {
  flex: 1;
  overflow-y: auto;
  padding: 12px 12px 4px;
}
#train-detail .tname  { font-size: 18px; font-weight: 800; color: #fff; }
#train-detail .tdir   { font-size: 12px; color: #aaa; margin-top: 3px; }
#train-detail .tdelay { display: inline-block; margin-top: 6px; padding: 3px 9px; border-radius: 4px; font-size: 12px; font-weight: 700; }
.tdelay.ok   { background: #1b5e20; color: #a5d6a7; }
.tdelay.late { background: #7f1d1d; color: #fca5a5; }
#train-detail .tnear  { margin-top: 6px; font-size: 11px; color: #888; }
#train-detail .detail-stops { margin-top: 10px; border-top: 1px solid #1a2a4a; padding-top: 2px; }
#close-detail {
  flex-shrink: 0;
  margin: 6px 10px 8px;
  background: transparent; border: 1px solid #2a3a5a;
  color: #556; border-radius: 4px; padding: 5px;
  font-size: 11px; cursor: pointer; width: calc(100% - 20px);
}
#close-detail:hover { color: #aaa; }

.hidden { display: none !important; }

/* ── Footer ──────────────────────────────────────────────────────── */
#panel-footer {
  font-size: 10px; color: #333;
  padding: 8px 14px;
  border-top: 1px solid #1a1a3a;
  flex-shrink: 0;
}

/* Mobile bar hidden on desktop */
#mobile-bar { display: none; }

/* ── Mobile layout ───────────────────────────────────────────────── */
@media (max-width: 768px) {

  /* Map always fullscreen */
  #map {
    position: fixed;
    top: 0; left: 0;
    width: 100vw; height: 100vh;
    z-index: 1;
  }

  /* Panel: fixed height, hidden below screen by default */
  #panel {
    position: fixed;
    bottom: 58px; left: 0; right: 0;
    width: 100%; min-width: unset;
    height: 48vh;
    border-left: none;
    border-top: 2px solid #EC0016;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.5);
    z-index: 500;
    display: flex; flex-direction: column;
    overflow-y: auto;
    transform: translateY(calc(100% + 58px));
    transition: transform 0.28s ease;
  }

  #panel.mobile-open {
    transform: translateY(0);
  }

  #panel-header { display: none; }
  #panel-footer { display: none; }
  #selected-train { max-height: 24vh; }

  /* No iOS zoom on inputs */
  .autocomplete-wrap input,
  #trainnr-input { font-size: 16px; }

  /* Mobile bar — always visible, always on top */
  #mobile-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 58px;
    background: #16213e;
    border-top: 2px solid #EC0016;
    padding: 0 18px;
    z-index: 9999;
    cursor: pointer;
    -webkit-tap-highlight-color: rgba(236,0,22,0.15);
    user-select: none;
  }

  #mobile-bar-left {
    display: flex; align-items: center; gap: 12px;
  }

  #mobile-bar-label {
    font-size: 15px; font-weight: 700; color: #fff;
  }

  #mobile-bar-right {
    display: flex; align-items: center; gap: 6px;
    font-size: 14px; font-weight: 800; color: #EC0016;
  }
  #mobile-bar-right small { color: #556; font-weight: 400; }
  #mobile-bar-chevron { font-size: 13px; margin-left: 4px; }
}

/* ── Train markers ───────────────────────────────────────────────── */
.train-icon { cursor: pointer; filter: drop-shadow(0 2px 5px rgba(0,0,0,0.55)); transition: filter 0.2s; }
.train-icon:hover { filter: drop-shadow(0 2px 10px rgba(236,0,22,0.8)); }
.train-icon.highlighted { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse {
  0%,100% { filter: drop-shadow(0 0 5px white); }
  50%      { filter: drop-shadow(0 0 12px white) drop-shadow(0 0 22px rgba(236,0,22,0.6)); }
}

/* ── Leaflet ─────────────────────────────────────────────────────── */
.leaflet-popup-content-wrapper {
  background: #0f1a2e; color: #e0e0e0;
  border: 1px solid #EC0016; border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.6);
}
.leaflet-popup-tip { background: #0f1a2e; }
.leaflet-popup-content { margin: 10px 12px; min-width: 130px; }
.popup-name  { font-size: 16px; font-weight: 800; color: #fff; }
.popup-dir   { font-size: 12px; color: #aaa; margin-top: 2px; }
.popup-near  { font-size: 11px; color: #666; margin-top: 2px; }
.popup-delay { margin-top: 5px; font-size: 12px; font-weight: 600; }
.popup-delay.ok   { color: #4caf50; }
.popup-delay.late { color: #f9a825; }
