:root {
  --bg: #ffffff;
  --fg: #1f2328;
  --muted: #59636e;
  --panel-bg: rgba(255, 255, 255, 0.94);
  --border: #d1d9e0;
  --gps: #16a34a;
  --confirmed: #2563eb;
  --forecast: #7c3aed;
  --schedule: #9ca3af;
  --late: #dc2626;
  --route: #06b6d4;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --fg: #e6edf3;
    --muted: #9198a1;
    --panel-bg: rgba(13, 17, 23, 0.92);
    --border: #3d444d;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

#map { position: absolute; inset: 0; }

.panel {
  position: absolute;
  z-index: 1000;
  top: calc(12px + env(safe-area-inset-top));
  left: calc(12px + env(safe-area-inset-left));
  max-width: calc(100% - 24px);
  width: 290px;
  padding: 10px 14px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.panel h1 { margin: 0 0 4px; font-size: 16px; }

#status { margin: 0 0 8px; color: var(--muted); }

.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; font-size: 12px; }

.dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  margin-right: 6px;
  border-radius: 50%;
  vertical-align: -1px;
  border: 2px solid #fff;
}

.dot.gps { background: var(--gps); }
.dot.confirmed { background: var(--confirmed); }
.dot.forecast { background: var(--forecast); }
.dot.schedule { background: var(--schedule); }
.dot.ring { background: transparent; border-color: var(--late); }

.leaflet-container { background: var(--bg); font: inherit; }

.base-tiles { filter: grayscale(0.6) contrast(0.95); }

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--panel-bg);
  color: var(--fg);
}

.leaflet-popup-content { margin: 10px 14px; font-size: 13px; min-width: 220px; }

.map-button {
  display: grid;
  place-items: center;
  padding: 0;
  width: 34px;
  height: 34px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  color: #1f2328;
  background: #ffffff;
  border: 2px solid rgba(0, 0, 0, 0.2);
  border-radius: 4px;
}

.train-label {
  padding: 0 4px;
  font-size: 11px;
  background: rgba(255, 255, 255, 0.85);
  border: none;
  box-shadow: none;
}

.train-label::before { display: none; }

.popup h2 { margin: 0 0 2px; font-size: 14px; }
.popup p { margin: 2px 0; }
.popup .muted { color: var(--muted); }
.popup .late { color: var(--late); font-weight: 600; }

@media (max-width: 480px) {
  .panel { width: auto; right: calc(60px + env(safe-area-inset-right)); }
  .legend { grid-template-columns: 1fr 1fr; }
}

.train-icon { background: none; border: none; }

.train { overflow: visible; cursor: pointer; }

.train .body,
.train .arrow {
  stroke: #ffffff;
  stroke-width: 1.5;
  stroke-linejoin: round;
}

.train.late .body { stroke: var(--late); stroke-width: 3; }

.train.basis-gps > * { fill: var(--gps); }
.train.basis-confirmed > * { fill: var(--confirmed); }
.train.basis-forecast > * { fill: var(--forecast); }
.train.basis-schedule > * { fill: var(--schedule); }

.search { display: flex; gap: 6px; margin: 6px 0 8px; }

.search input {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.search button {
  padding: 6px 10px;
  font: inherit;
  color: #ffffff;
  background: var(--confirmed);
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.search-results {
  list-style: none;
  margin: 0 0 8px;
  padding: 0;
  max-height: 50vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.search-results li + li { border-top: 1px solid var(--border); }

.result {
  display: grid;
  gap: 1px;
  width: 100%;
  padding: 6px 8px;
  text-align: left;
  font: inherit;
  font-size: 12px;
  color: var(--fg);
  background: none;
  border: none;
  cursor: pointer;
}

.result:hover, .result:focus-visible { background: rgba(37, 99, 235, 0.1); outline: none; }

.result-title { font-weight: 600; font-size: 13px; }
.result-course, .result-position { color: var(--muted); }
.result.state-running .result-position { color: var(--fg); }
.result.state-cancelled .result-title { text-decoration: line-through; }

.result .late { color: var(--late); font-weight: 600; }
.result .muted { color: var(--muted); font-weight: 400; }

.result-empty { padding: 8px; font-size: 12px; color: var(--muted); }

.heading { white-space: nowrap; color: var(--muted); }
.heading-arrow { display: inline-block; font-weight: 700; color: var(--confirmed); }

.share-button {
  margin-top: 8px;
  padding: 5px 10px;
  font: inherit;
  color: #ffffff;
  background: var(--confirmed);
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.result-section {
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: rgba(127, 127, 127, 0.08);
}

.popup-actions { display: flex; gap: 6px; margin-top: 8px; }
.popup-actions .share-button { margin-top: 0; }

.route-button {
  padding: 5px 10px;
  font: inherit;
  color: var(--confirmed);
  background: transparent;
  border: 1px solid var(--confirmed);
  border-radius: 6px;
  cursor: pointer;
}

.app-dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: min(85vh, 900px);
  padding: 0;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.app-dialog[open] { display: flex; flex-direction: column; }

.app-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px 6px;
}

.dialog-header h2 { margin: 0; font-size: 17px; }
.dialog-header p { margin: 2px 0 0; }
.muted { color: var(--muted); }

.dialog-close {
  width: 32px;
  height: 32px;
  flex: none;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.dialog-close:hover { background: rgba(127, 127, 127, 0.15); }

.route-legend { display: flex; gap: 8px; margin: 0; padding: 0 16px 8px; font-size: 12px; }

.route-stops {
  list-style: none;
  margin: 0;
  padding: 4px 16px 16px;
  overflow-y: auto;
  border-top: 1px solid var(--border);
}

.route-stop {
  --dot-color: var(--confirmed);
  position: relative;
  display: grid;
  grid-template-columns: 22px 1fr auto;
  align-items: center;
  column-gap: 8px;
  min-height: 42px;
  padding: 4px 0;
}

.route-stop::before {
  content: "";
  position: absolute;
  left: 10px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--dot-color);
}

.route-stop:first-child::before { top: 50%; }
.route-stop:last-child::before { bottom: 50%; }

.route-dot {
  position: relative;
  z-index: 1;
  width: 12px;
  height: 12px;
  margin-left: 5px;
  border-radius: 50%;
  background: var(--bg);
  border: 2px solid var(--dot-color);
}

.route-stop.passed { --dot-color: var(--schedule); color: var(--muted); }
.route-stop.passed .route-dot { background: var(--schedule); }

.route-stop.current .route-dot,
.route-stop.next .route-dot {
  width: 16px;
  height: 16px;
  margin-left: 3px;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.25);
}

.route-stop.current .route-dot { background: var(--confirmed); }
.route-stop.current .route-stop-name,
.route-stop.next .route-stop-name { font-weight: 600; }

.route-stop-label {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--confirmed);
}

.route-stop-times {
  display: grid;
  justify-items: end;
  gap: 2px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.route-time s { color: var(--muted); }
.route-time-label { color: var(--muted); font-size: 11px; }

.delay-badge {
  display: inline-block;
  padding: 0 5px;
  font-size: 11px;
  border-radius: 4px;
  border: 1px solid currentColor;
  white-space: nowrap;
}

.delay-badge.on-time,
.delay-badge.early { color: var(--gps); }
.delay-badge.slight { color: #b45309; }
.delay-badge.late { color: var(--late); font-weight: 600; }
.delay-badge.forecast { border-style: dashed; opacity: 0.85; }

@media (prefers-color-scheme: dark) {
  .delay-badge.slight { color: #f59e0b; }
}

@media (max-width: 480px) {
  .route-stop { grid-template-columns: 22px 1fr; }
  .route-stop-times { grid-column: 2; justify-items: start; text-align: left; }
}

.leaflet-top.leaflet-right { margin-top: env(safe-area-inset-top); margin-right: env(safe-area-inset-right); }
.leaflet-bottom { margin-bottom: env(safe-area-inset-bottom); }

.panel-header { display: flex; align-items: center; gap: 8px; }
.panel-header h1 { margin: 0; }
.panel-body { margin-top: 4px; }

.panel-toggle {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: none;
  padding: 0;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}

.panel-toggle:hover { background: rgba(127, 127, 127, 0.12); }

.panel.collapsed { width: auto; right: auto; padding: 6px; }
.panel.collapsed h1,
.panel.collapsed .panel-body { display: none; }

.map-button[hidden] { display: none; }

.install-steps {
  display: grid;
  gap: 14px;
  margin: 0;
  padding: 12px 16px 18px;
  list-style: none;
  border-top: 1px solid var(--border);
  counter-reset: step;
}

.install-steps li {
  display: grid;
  grid-template-columns: 22px 36px 1fr;
  align-items: center;
  gap: 10px;
  counter-increment: step;
}

.install-steps li::before {
  content: counter(step) ".";
  font-weight: 600;
  color: var(--muted);
}

.step-icon {
  width: 36px;
  height: 36px;
  padding: 6px;
  fill: none;
  stroke: var(--confirmed);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  border: 1px solid var(--border);
  border-radius: 8px;
}

img.step-icon { padding: 0; border: none; }

@media (max-width: 600px) {
  .leaflet-bottom.leaflet-left { bottom: var(--attribution-height, 0px); }
}
