/* ── Reset & tokens ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #0b0b0b;
  --surface:    #141414;
  --surface2:   #1c1c1c;
  --surface3:   #242424;
  --border:     #2c2c2c;
  --text:       #efefef;
  --muted:      #666;
  --db-red:     #EC0016;
  --db-red-dim: rgba(236,0,22,.15);

  /* Train type */
  --ice:   #EC0016;
  --ic:    #e07000;
  --re:    #2172c4;
  --sbahn: #007a3d;
  --other: #555;

  --panel: 320px;
}

html, body { height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; font-size: 14px; }

/* ── Layout ───────────────────────────────────────────────────────────────── */
body { display: flex; }

#left-panel {
  width: var(--panel); flex-shrink: 0;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden; z-index: 10;
}

#map-wrap { flex: 1; position: relative; overflow: hidden; }
#map      { width: 100%; height: 100%; }

/* Tiles are already dark (CARTO dark_all) — no filter needed */

/* ── Brand ────────────────────────────────────────────────────────────────── */
#brand {
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#logo { display: flex; align-items: baseline; gap: 8px; }
.logo-db {
  background: var(--db-red); color: #fff; font-weight: 900;
  font-size: 20px; padding: 2px 7px; border-radius: 4px; letter-spacing: 0.5px;
  line-height: 1.3;
}
.logo-title { font-size: 17px; font-weight: 600; }

/* ── Search ───────────────────────────────────────────────────────────────── */
#search-wrap {
  padding: 12px; border-bottom: 1px solid var(--border);
  position: relative; flex-shrink: 0;
}
#station-search {
  width: 100%; background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 9px 12px;
  font-size: 13px; outline: none; transition: border-color 0.15s;
}
#station-search::placeholder { color: var(--muted); }
#station-search:focus { border-color: var(--db-red); }

#search-results {
  position: absolute; left: 12px; right: 12px; top: calc(100% - 12px);
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden; z-index: 100;
  box-shadow: 0 8px 24px rgba(0,0,0,0.7);
}
#search-results.hidden { display: none; }
.search-result-item {
  padding: 10px 14px; cursor: pointer; font-size: 13px;
  border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 8px;
  transition: background 0.1s;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--surface3); }
.sri-icon { font-size: 16px; opacity: 0.6; }
.sri-name { font-weight: 500; }
.sri-products { font-size: 11px; color: var(--muted); }

/* ── Departures ───────────────────────────────────────────────────────────── */
#departures-header {
  padding: 10px 14px 6px;
  display: flex; justify-content: space-between; align-items: baseline;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#departures-station-name { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }
#departures-count { font-size: 11px; color: var(--muted); }

#departures-list {
  flex: 1; overflow-y: auto; padding: 4px 0;
}
.placeholder-msg { color: var(--muted); font-size: 13px; padding: 24px 16px; text-align: center; line-height: 1.6; }

/* ── Departure group headers ──────────────────────────────────────────────── */
.dep-group-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px 5px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 2;
  background: var(--surface);
}
.dep-group-label {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.8px; color: var(--muted);
}
.dep-group-count {
  font-size: 10px; color: var(--muted);
  background: var(--surface3); border-radius: 10px;
  padding: 1px 6px;
}

.dep-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-bottom: 1px solid var(--border);
  cursor: pointer; transition: background 0.1s; position: relative;
}
.dep-item:hover   { background: var(--surface2); }
.dep-item.active  { background: var(--db-red-dim); border-left: 3px solid var(--db-red); }
.dep-item.cancelled { opacity: 0.45; }
.dep-item.dep-ongoing  { opacity: 0.75; }
.dep-item.dep-finished { opacity: 0.45; pointer-events: none; }

.dep-badge {
  flex-shrink: 0; border-radius: 5px; padding: 2px 7px;
  font-size: 11px; font-weight: 800; color: #fff;
  min-width: 52px; text-align: center; white-space: nowrap;
}
.dep-badge.ice   { background: var(--ice); }
.dep-badge.ic    { background: var(--ic); }
.dep-badge.re    { background: var(--re); }
.dep-badge.sbahn { background: var(--sbahn); }
.dep-badge.other { background: var(--other); }

.dep-main { flex: 1; min-width: 0; }
.dep-direction { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dep-platform  { font-size: 11px; color: var(--muted); margin-top: 1px; }

.dep-time { flex-shrink: 0; text-align: right; }
.dep-sched { font-size: 14px; font-weight: 700; }
.dep-delay { font-size: 11px; margin-top: 1px; }
.on-time    { color: #4ade80; }
.late       { color: #fb923c; }
.very-late  { color: #f87171; }
.muted-label{ color: var(--muted); }

.dep-loading { padding: 20px; text-align: center; }
.dep-loading .spinner { margin: 0 auto; }

/* ── Trip overlay ─────────────────────────────────────────────────────────── */
#trip-overlay {
  position: absolute; top: 12px; right: 12px; width: 300px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; z-index: 500; overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.7);
  max-height: calc(100vh - 120px); display: flex; flex-direction: column;
}
#trip-overlay.hidden { display: none; }

#trip-overlay-close {
  position: absolute; top: 10px; right: 12px; background: none; border: none;
  color: var(--muted); font-size: 18px; cursor: pointer; line-height: 1;
}
#trip-overlay-close:hover { color: var(--text); }

#trip-overlay-content { padding: 14px 14px 16px; overflow-y: auto; }

.ov-badge {
  display: inline-block; border-radius: 5px; padding: 2px 8px;
  font-size: 11px; font-weight: 800; color: #fff; margin-bottom: 8px;
}
.ov-name     { font-size: 22px; font-weight: 800; line-height: 1.1; }
.ov-subtitle { font-size: 12px; color: var(--muted); margin: 3px 0 12px; }

.ov-section-title {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.8px; color: var(--muted); margin: 12px 0 6px;
}

/* Stopovers */
.sv-list { list-style: none; }
.sv-item {
  display: flex; gap: 10px; padding: 5px 0; align-items: flex-start; position: relative;
}
.sv-line {
  display: flex; flex-direction: column; align-items: center; flex-shrink: 0;
  width: 14px; padding-top: 4px;
}
.sv-dot {
  width: 8px; height: 8px; border-radius: 50%; border: 2px solid var(--border);
  background: var(--surface); z-index: 1;
}
.sv-dot.passed  { background: var(--muted); border-color: var(--muted); }
.sv-dot.current { background: var(--db-red); border-color: var(--db-red); width: 11px; height: 11px; box-shadow: 0 0 0 3px var(--db-red-dim); }
.sv-dot.future  { background: var(--surface2); border-color: var(--border); }
.sv-connector   { width: 2px; flex: 1; min-height: 14px; background: var(--border); }

.sv-info { flex: 1; min-width: 0; padding-bottom: 6px; }
.sv-name { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sv-name.passed  { color: var(--muted); }
.sv-name.current { font-weight: 700; color: #fff; }
.sv-times { font-size: 10px; color: var(--muted); margin-top: 1px; }
.sv-delay { color: #fb923c; }

/* Dwell time badges */
.dwell-badge {
  display: inline-block; font-size: 9px; font-weight: 700;
  padding: 1px 5px; border-radius: 3px; margin-left: 4px;
  background: var(--surface3); color: var(--muted); vertical-align: middle;
}
.dwell-badge.long {
  background: rgba(251,191,36,0.15); color: #fbbf24;
  border: 1px solid rgba(251,191,36,0.3);
}
.sv-item.long-dwell { background: rgba(251,191,36,0.04); border-radius: 4px; margin: 0 -4px; padding: 5px 4px; }
.sv-dot.dwell-flag { background: #fbbf24 !important; border-color: #fbbf24 !important; }

/* Action buttons row */
.replay-launch-btn, .analyze-btn {
  width: 100%; margin-top: 8px; padding: 10px;
  border: none; border-radius: 8px;
  font-size: 13px; font-weight: 700; cursor: pointer; transition: filter 0.15s;
}
.replay-launch-btn { background: var(--db-red); color: #fff; margin-top: 12px; font-size: 14px; }
.replay-launch-btn:hover { filter: brightness(1.15); }
.analyze-btn { background: var(--surface3); color: var(--text); border: 1px solid var(--border); }
.analyze-btn:hover { border-color: var(--muted); }
.analyze-btn:disabled { opacity: 0.5; cursor: default; }
.analyze-btn.station { margin: 0; border-radius: 6px; padding: 8px; font-size: 12px; }

/* Analysis output box */
.analysis-box {
  margin-top: 8px; padding: 10px 12px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; font-size: 12px; line-height: 1.6;
  color: #ccc; white-space: pre-wrap; max-height: 200px; overflow-y: auto;
}
.analysis-box.hidden { display: none; }
.analysis-box.err { color: #f87171; border-color: rgba(248,113,113,0.3); }
.analysis-loading {
  display: flex; align-items: center; gap: 10px; color: var(--muted);
}
.analysis-loading .spinner { width: 18px; height: 18px; border-width: 2px; flex-shrink: 0; }

/* Station analysis in departure list */
.dep-analyze-wrap { padding: 8px 12px; border-bottom: 1px solid var(--border); }

/* ── Replay bar ───────────────────────────────────────────────────────────── */
#replay-bar {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: rgba(14,14,14,0.97); border-top: 1px solid var(--border);
  padding: 12px 20px 14px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 500; backdrop-filter: blur(6px);
}
#replay-bar.hidden { display: none; }

#replay-meta { display: flex; align-items: baseline; gap: 10px; }
#replay-train-name  { font-size: 16px; font-weight: 800; }
#replay-train-route { font-size: 12px; color: var(--muted); }

#replay-timeline { display: flex; align-items: center; gap: 10px; }
#replay-time-current,
#replay-time-end { font-size: 11px; color: var(--muted); flex-shrink: 0; width: 45px; }
#replay-time-end { text-align: right; }

#replay-slider-wrap { flex: 1; position: relative; }
#replay-slider {
  width: 100%; height: 4px; accent-color: var(--db-red);
  cursor: pointer; position: relative; z-index: 1;
}
#replay-stops-track { position: absolute; top: 50%; transform: translateY(-50%); left: 0; right: 0; height: 4px; pointer-events: none; }
.stop-tick { position: absolute; width: 3px; height: 10px; background: var(--border); border-radius: 1px; top: 50%; transform: translate(-50%, -50%); }
.stop-tick.passed { background: var(--db-red); }

#replay-btns { display: flex; align-items: center; gap: 8px; }
.rbtn {
  background: var(--surface2); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 6px 14px; font-size: 13px; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.rbtn:hover   { background: var(--surface3); }
.rbtn.primary { background: var(--db-red); border-color: var(--db-red); font-size: 15px; padding: 6px 18px; }
.rbtn.primary:hover { filter: brightness(1.15); }
.rbtn.exit    { margin-left: auto; font-size: 12px; color: var(--muted); }
.rbtn.exit:hover { border-color: var(--db-red); color: var(--db-red); }

.speed-group { display: flex; gap: 4px; margin-left: 4px; }
.sbtn {
  background: var(--surface2); border: 1px solid var(--border); color: var(--muted);
  border-radius: 4px; padding: 4px 8px; font-size: 11px; font-weight: 700; cursor: pointer;
  transition: all 0.12s;
}
.sbtn:hover, .sbtn.active { background: var(--db-red); border-color: var(--db-red); color: #fff; }

/* Train map marker */
.train-marker {
  width: 40px; height: 46px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; border-radius: 8px;
  border: 2.5px solid rgba(255,255,255,0.9);
  box-shadow: 0 0 0 3px rgba(236,0,22,.35), 0 4px 14px rgba(0,0,0,.8);
  animation: rPulse 1.2s ease-in-out infinite;
}
.train-marker.ice   { background: var(--ice); }
.train-marker.ic    { background: var(--ic); }
.train-marker.re    { background: var(--re); }
.train-marker.sbahn { background: var(--sbahn); }
.train-marker.other { background: var(--other); }
.train-marker .tm-label {
  font-size: 7px; font-weight: 800; color: #fff; white-space: nowrap; line-height: 1;
}
@keyframes rPulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(236,0,22,.35), 0 4px 14px rgba(0,0,0,.8); }
  50%       { box-shadow: 0 0 0 8px rgba(236,0,22,.1),  0 4px 14px rgba(0,0,0,.8); }
}

/* Station tooltip (hover label) */
.station-tip.leaflet-tooltip {
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); font-size: 12px; font-weight: 500;
  padding: 4px 8px; border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  white-space: nowrap; pointer-events: none;
}
.station-tip.leaflet-tooltip::before { display: none; }

/* ── Toast ────────────────────────────────────────────────────────────────── */
#toasts {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 6px; z-index: 9000; pointer-events: none;
  align-items: center;
}
.toast {
  background: var(--surface2); border: 1px solid var(--border);
  padding: 8px 16px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 4px 14px rgba(0,0,0,.6); white-space: nowrap;
  animation: tIn .25s ease forwards;
}
.toast.err { border-color: #f87171; color: #fca5a5; }
@keyframes tIn  { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@keyframes tOut { from { opacity: 1; } to { opacity: 0; } }

/* ── Welcome / city picker ────────────────────────────────────────────────── */
#welcome {
  position: fixed; inset: 0; background: rgba(6,6,6,0.92);
  display: flex; align-items: center; justify-content: center;
  z-index: 9000; backdrop-filter: blur(8px);
  transition: opacity .35s;
}
#welcome.hidden { opacity: 0; pointer-events: none; }

.welcome-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px 36px; width: 520px; max-width: 94vw;
  box-shadow: 0 16px 48px rgba(0,0,0,0.6);
}

.welcome-brand {
  display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px;
}
.welcome-title { font-size: 22px; font-weight: 700; }

.welcome-sub { color: var(--muted); font-size: 13px; margin-bottom: 18px; }

.welcome-search-wrap { position: relative; margin-bottom: 12px; }
.welcome-search-wrap input {
  width: 100%; background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); border-radius: 10px; padding: 11px 14px;
  font-size: 14px; outline: none; transition: border-color 0.15s;
}
.welcome-search-wrap input::placeholder { color: var(--muted); }
.welcome-search-wrap input:focus { border-color: var(--db-red); }

#welcome-search-results {
  position: absolute; left: 0; right: 0; top: 100%; margin-top: 4px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden; z-index: 10;
  box-shadow: 0 8px 24px rgba(0,0,0,0.6);
}
#welcome-search-results.hidden { display: none; }
#welcome-search-results .search-result-item { padding: 10px 14px; }

.welcome-divider {
  display: flex; align-items: center; gap: 12px; margin: 16px 0;
  color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px;
}
.welcome-divider::before, .welcome-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}

#welcome-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
}

.city-card {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 8px 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  transition: all 0.15s; text-align: center;
}
.city-card:hover {
  border-color: var(--db-red); background: var(--db-red-dim);
  transform: translateY(-2px);
}
.city-name { font-size: 14px; font-weight: 700; color: var(--text); }
.city-station { font-size: 10px; color: var(--muted); }
.city-card.loading { border-color: var(--db-red); background: var(--db-red-dim); }
.city-card:disabled { opacity: 0.4; cursor: default; pointer-events: none; }

/* .station-label removed — station names now shown via hover tooltips */

/* ── Prediction card ─────────────────────────────────────────────────────── */
.prediction-card {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; margin: 8px 0;
}
.prediction-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 8px;
}
.prediction-body { margin-bottom: 10px; }
.prediction-stat {
  display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px;
}
.prediction-value { font-size: 22px; font-weight: 800; }
.prediction-label { font-size: 11px; color: var(--muted); }
.prediction-detail { font-size: 12px; color: var(--dim); }
.prediction-trend {
  font-size: 12px; font-weight: 700; padding: 3px 8px;
  border-radius: 6px; text-transform: capitalize;
}
.prediction-trend.on-time { background: rgba(34,197,94,0.15); color: #22c55e; }
.prediction-trend.late { background: rgba(251,191,36,0.15); color: #fbbf24; }
.prediction-trend.very-late { background: rgba(239,68,68,0.15); color: #ef4444; }
.prediction-stale {
  font-size: 10px; color: var(--muted); margin-top: 6px;
  padding: 4px 6px; background: var(--surface3); border-radius: 4px;
}

/* ── Alternatives card ───────────────────────────────────────────────────── */
.alt-card {
  background: rgba(251,146,60,0.08); border: 1px solid rgba(251,146,60,0.25);
  border-radius: 10px; padding: 12px; margin: 8px 0;
}
.alt-header { margin-bottom: 6px; }
.alt-desc { font-size: 12px; color: var(--dim); margin: 0 0 10px; line-height: 1.5; }
.alt-btn {
  background: rgba(251,146,60,0.15) !important; border-color: rgba(251,146,60,0.3) !important;
  color: #fb923c !important;
}
.alt-btn:hover { background: rgba(251,146,60,0.25) !important; }

/* ── Small analyze button variant ────────────────────────────────────────── */
.analyze-btn.sm {
  font-size: 12px; padding: 6px 12px; margin-top: 6px;
}

/* ── Spinner ──────────────────────────────────────────────────────────────── */
.spinner {
  width: 28px; height: 28px; border: 3px solid var(--border);
  border-top-color: var(--db-red); border-radius: 50%;
  animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Scrollbar ────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Trip overlay handle (mobile only) ──────────────────────────────────── */
#trip-overlay-handle { display: none; }

/* ── Mobile panel toggle ─────────────────────────────────────────────────── */
#panel-toggle {
  display: none;
  position: fixed; top: 12px; left: 12px; z-index: 600;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 11px;
  font-size: 18px; line-height: 1; cursor: pointer; color: var(--text);
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}

#panel-backdrop {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.55); z-index: 540;
}
#panel-backdrop.visible { display: block; }

/* ── Responsive — tablet / large phone (≤ 768px) ─────────────────────────── */
@media (max-width: 768px) {
  #panel-toggle { display: block; }

  #left-panel {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: min(320px, 88vw);
    transform: translateX(-110%);
    transition: transform 0.28s ease;
    z-index: 550;
    box-shadow: 4px 0 24px rgba(0,0,0,0.7);
  }
  #left-panel.open { transform: translateX(0); }

  #map-wrap { width: 100%; }

  /* Trip overlay → two-state bottom sheet */
  #trip-overlay {
    position: fixed;
    top: auto; bottom: 0; left: 0; right: 0;
    width: 100%; max-width: 100%;
    border-radius: 16px 16px 0 0;
    max-height: 44vh;
    transition: max-height 0.3s ease;
  }
  #trip-overlay.expanded { max-height: 78vh; }

  #trip-overlay-handle {
    display: flex; justify-content: center;
    padding: 10px 0 4px; cursor: pointer; flex-shrink: 0;
  }
  #trip-overlay-pill {
    width: 36px; height: 4px;
    background: var(--border); border-radius: 2px;
  }

  /* Replay bar — tighter */
  #replay-bar { padding: 10px 12px 14px; }
  #replay-train-name { font-size: 14px; }
  #replay-btns { flex-wrap: wrap; gap: 6px; }
  .rbtn { padding: 6px 10px; font-size: 12px; }
  .rbtn.primary { padding: 6px 14px; font-size: 14px; }
  .speed-group { margin-left: 0; }

  /* Welcome card */
  .welcome-card { padding: 24px 20px; }
  #welcome-grid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .city-card { padding: 10px 6px 8px; }
  .city-name { font-size: 13px; }
  .city-station { font-size: 9px; }

  /* Toast above replay bar */
  #toasts { bottom: 72px; }
}

/* ── Responsive — small phone (≤ 480px) ──────────────────────────────────── */
@media (max-width: 480px) {
  #welcome-grid { grid-template-columns: repeat(2, 1fr); }
  .welcome-card { padding: 20px 16px; }
  #trip-overlay { max-height: 78vh; }
  #replay-time-current,
  #replay-time-end { width: 36px; font-size: 10px; }
  #replay-train-route { font-size: 11px; }
}
