:root {
  color-scheme: dark;
  --bg: #07111f;
  --panel: #0c1929;
  --panel-strong: #102238;
  --panel-soft: #132941;
  --line: #213a55;
  --text: #f4f8fc;
  --muted: #8fa6bc;
  --cyan: #29d7c7;
  --cyan-dark: #0e9088;
  --blue: #54a8ff;
  --amber: #ffb84d;
  --red: #ff6575;
  --green: #46d390;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

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

body {
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-header {
  position: relative;
  z-index: 50;
  height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(7, 17, 31, 0.96);
  backdrop-filter: blur(12px);
}

.eyebrow,
.section-kicker {
  margin: 0 0 4px;
  color: var(--cyan);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 1.55rem;
  letter-spacing: -0.03em;
}

h2 {
  margin-bottom: 0;
  font-size: 1.08rem;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 0.82rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.database-link {
  padding: 0.55rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 0.35rem;
  color: var(--text);
  background: var(--panel);
  font-size: 0.78rem;
  font-weight: 750;
  text-decoration: none;
  white-space: nowrap;
}

.database-link:hover,
.database-link:focus-visible {
  border-color: var(--cyan);
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 0 5px rgba(255, 184, 77, 0.12);
}

.status-dot.connected {
  background: var(--green);
  box-shadow: 0 0 0 5px rgba(70, 211, 144, 0.12);
}

.status-dot.error {
  background: var(--red);
  box-shadow: 0 0 0 5px rgba(255, 101, 117, 0.12);
}

.status-divider {
  height: 18px;
  border-left: 1px solid var(--line);
}

.app-shell {
  position: relative;
  height: calc(100vh - 78px);
  min-height: 0;
  display: grid;
  grid-template-columns: 390px 1fr;
  overflow: hidden;
}

.sidebar {
  position: relative;
  z-index: 30;
  display: flex;
  min-height: 0;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.search-panel {
  padding: 20px;
  border-bottom: 1px solid var(--line);
}

.section-heading,
.results-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.icon-button,
.map-toolbar button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--text);
  padding: 7px 10px;
  font-size: 0.78rem;
}

.icon-button:hover,
.map-toolbar button:hover {
  border-color: var(--cyan-dark);
}

form {
  display: grid;
  gap: 13px;
}

label {
  display: grid;
  gap: 6px;
}

label > span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
}

input,
select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 9px;
  outline: none;
  background: #081522;
  color: var(--text);
  padding: 10px 11px;
}

input::placeholder {
  color: #536b82;
}

input:focus,
select:focus {
  border-color: var(--cyan);
  box-shadow: 0 0 0 3px rgba(41, 215, 199, 0.1);
}

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

.form-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #607890;
  font-size: 0.67rem;
  text-transform: uppercase;
}

.form-divider::before,
.form-divider::after {
  height: 1px;
  flex: 1;
  background: var(--line);
  content: "";
}

.primary-button {
  border: 0;
  border-radius: 9px;
  background: var(--cyan);
  color: #041412;
  padding: 11px 14px;
  font-weight: 900;
}

.primary-button:hover {
  background: #54e4d7;
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.form-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.secondary-button {
  border: 1px solid var(--cyan-dark);
  border-radius: 9px;
  background: transparent;
  color: var(--cyan);
  padding: 10px 12px;
  font-weight: 800;
}

.secondary-button:hover {
  background: rgba(41, 215, 199, 0.08);
  border-color: var(--cyan);
}

.form-note {
  margin: 11px 0 0;
  color: #7189a0;
  font-size: 0.7rem;
  line-height: 1.5;
}

.results-panel {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  overflow: hidden;
  padding: 18px 14px 10px;
}

.results-heading {
  padding: 0 6px;
  margin-bottom: 12px;
}

.count-badge {
  min-width: 28px;
  border-radius: 999px;
  background: var(--panel-soft);
  color: var(--cyan);
  padding: 5px 8px;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 800;
}

.results-message {
  margin: 4px 6px 12px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.5;
}

.train-results {
  flex: 1 1 0;
  height: 0;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 4px;
  scrollbar-color: var(--line) transparent;
  scrollbar-width: thin;
}

.train-results::-webkit-scrollbar,
.details-drawer::-webkit-scrollbar,
.board-services::-webkit-scrollbar {
  width: 9px;
}

.train-results::-webkit-scrollbar-thumb,
.details-drawer::-webkit-scrollbar-thumb,
.board-services::-webkit-scrollbar-thumb {
  border: 2px solid transparent;
  border-radius: 999px;
  background: var(--line);
  background-clip: padding-box;
}

.train-card {
  width: 100%;
  display: grid;
  gap: 8px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #0a1726;
  color: var(--text);
  padding: 12px;
  text-align: left;
}

.train-card:hover,
.train-card.active {
  border-color: var(--cyan-dark);
  background: var(--panel-strong);
}

.train-card-top,
.train-card-route,
.train-card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.headcode {
  color: var(--cyan);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.operator {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.7rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.train-card-route {
  align-items: flex-start;
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 700;
}

.route-arrow {
  color: var(--cyan);
}

.train-card-meta {
  color: var(--muted);
  font-size: 0.68rem;
}

.delay {
  color: var(--green);
  font-weight: 800;
}

.delay.late {
  color: var(--amber);
}

.map-panel,
#map {
  width: 100%;
  height: 100%;
}

.map-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 1;
  background: #0a1726;
  overflow: hidden;
  isolation: isolate;
}

.leaflet-container,
.leaflet-map-pane,
.leaflet-tile-pane {
  max-width: none;
}

.leaflet-container img.leaflet-tile {
  max-width: none !important;
  max-height: none !important;
}

.map-toolbar,
.map-legend {
  position: absolute;
  z-index: 500;
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(7, 17, 31, 0.92);
  box-shadow: var(--shadow);
  color: var(--muted);
  backdrop-filter: blur(10px);
}

.map-toolbar {
  top: 14px;
  right: 14px;
  padding: 7px;
}

.switch-label {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  gap: 7px;
  padding-right: 5px;
}

.switch-label input {
  width: auto;
  accent-color: var(--cyan);
}

.switch-label span {
  color: var(--muted);
  font-size: 0.72rem;
}

.map-legend {
  right: 14px;
  bottom: 22px;
  padding: 9px 11px;
  font-size: 0.7rem;
}

.map-legend span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.legend-dot.on-time {
  background: var(--cyan);
}

.legend-dot.delayed {
  background: var(--amber);
}

.legend-dot.selected {
  background: var(--red);
}

.train-marker {
  width: 15px;
  height: 15px;
  border: 2px solid white;
  border-radius: 50% 50% 50% 0;
  background: var(--cyan);
  box-shadow: 0 2px 9px rgba(0, 0, 0, 0.5);
  transform: rotate(-45deg);
}

.train-marker.delayed {
  background: var(--amber);
}

.train-marker.selected {
  width: 20px;
  height: 20px;
  background: var(--red);
  box-shadow: 0 0 0 6px rgba(255, 101, 117, 0.2);
}

.details-drawer {
  position: absolute;
  z-index: 600;
  top: 0;
  right: 0;
  width: min(430px, 100%);
  height: 100%;
  overflow-y: auto;
  border-left: 1px solid var(--line);
  background: rgba(8, 21, 34, 0.98);
  box-shadow: -20px 0 60px rgba(0, 0, 0, 0.35);
  padding: 24px;
  transform: translateX(105%);
  transition: transform 220ms ease;
  backdrop-filter: blur(18px);
}

.details-drawer.open {
  transform: translateX(0);
}

.drawer-close {
  position: absolute;
  top: 16px;
  right: 17px;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel-soft);
  color: var(--text);
  font-size: 1.4rem;
  line-height: 1;
}

.detail-hero {
  padding-right: 35px;
}

.detail-headcode {
  margin-bottom: 7px;
  color: var(--cyan);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.12em;
}

.detail-hero h2 {
  margin-bottom: 8px;
  font-size: 1.55rem;
  line-height: 1.22;
}

.detail-subtitle {
  color: var(--muted);
  font-size: 0.8rem;
}

.detail-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 20px 0;
}

.detail-stat {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  padding: 11px;
}

.detail-stat span {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.63rem;
  text-transform: uppercase;
}

.detail-stat strong {
  font-size: 0.88rem;
}

.calling-title {
  margin: 22px 0 14px;
  font-size: 0.9rem;
}

.calling-point {
  position: relative;
  display: grid;
  grid-template-columns: 20px 1fr auto;
  gap: 10px;
  min-height: 54px;
}

.calling-point-button {
  width: 100%;
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto;
  gap: 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: inherit;
  padding: 4px 5px 4px 0;
  text-align: left;
}

.calling-point-button:hover,
.calling-point-button:focus-visible {
  outline: none;
  background: rgba(41, 215, 199, 0.08);
}

.calling-line {
  position: relative;
  display: flex;
  justify-content: center;
}

.calling-line::before {
  position: absolute;
  top: 6px;
  bottom: -6px;
  width: 2px;
  background: var(--line);
  content: "";
}

.calling-point-button:last-child .calling-line::before {
  display: none;
}

.calling-line::after {
  position: relative;
  z-index: 1;
  width: 9px;
  height: 9px;
  margin-top: 5px;
  border: 2px solid var(--cyan);
  border-radius: 50%;
  background: var(--panel);
  content: "";
}

.calling-point-button.cancelled .calling-line::after {
  border-color: var(--red);
}

.calling-name {
  font-size: 0.8rem;
  font-weight: 750;
}

.calling-point-button:hover .calling-name {
  color: var(--cyan);
}

.calling-meta,
.calling-time small {
  color: var(--muted);
  font-size: 0.67rem;
}

.calling-time {
  text-align: right;
  font-size: 0.75rem;
  font-weight: 800;
}

.calling-time small {
  display: block;
  font-weight: 500;
}

.cancelled-label {
  color: var(--red);
}

.drawer-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 16px;
  border: 0;
  background: transparent;
  color: var(--cyan);
  padding: 0;
  font-size: 0.75rem;
  font-weight: 800;
}

.board-updated {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 0.7rem;
}

.board-services {
  display: grid;
  gap: 8px;
  margin-top: 18px;
  overflow-y: auto;
}

.board-service {
  width: 100%;
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto;
  gap: 11px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--panel);
  color: var(--text);
  padding: 12px;
  text-align: left;
}

.board-service:hover {
  border-color: var(--cyan-dark);
  background: var(--panel-strong);
}

.board-time {
  color: var(--cyan);
  font-size: 1rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.board-route {
  min-width: 0;
}

.board-destination {
  overflow: hidden;
  margin-bottom: 4px;
  font-size: 0.84rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board-origin,
.board-meta {
  color: var(--muted);
  font-size: 0.67rem;
  line-height: 1.45;
}

.board-platform {
  min-width: 42px;
  border-radius: 7px;
  background: var(--panel-soft);
  padding: 6px;
  text-align: center;
}

.board-platform span {
  display: block;
  color: var(--muted);
  font-size: 0.56rem;
  text-transform: uppercase;
}

.board-platform strong {
  font-size: 0.82rem;
}

.board-status {
  grid-column: 2 / 4;
  font-size: 0.68rem;
  font-weight: 800;
}

.board-status.cancelled {
  color: var(--red);
}

.board-status.late {
  color: var(--amber);
}

.board-status.on-time {
  color: var(--green);
}

.board-empty {
  border: 1px dashed var(--line);
  border-radius: 11px;
  color: var(--muted);
  padding: 18px;
  text-align: center;
  font-size: 0.78rem;
  line-height: 1.5;
}

.station-pin {
  width: 22px;
  height: 22px;
  border: 3px solid white;
  border-radius: 50%;
  background: var(--blue);
  box-shadow:
    0 0 0 7px rgba(84, 168, 255, 0.2),
    0 4px 14px rgba(0, 0, 0, 0.5);
}

.toast {
  position: fixed;
  z-index: 1000;
  right: 18px;
  bottom: 18px;
  max-width: 390px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-strong);
  box-shadow: var(--shadow);
  color: var(--text);
  padding: 12px 15px;
  font-size: 0.78rem;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: 180ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.leaflet-control-zoom a {
  border-color: var(--line) !important;
  background: var(--panel) !important;
  color: var(--text) !important;
}

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

.leaflet-popup-content {
  margin: 10px 12px;
  font-family: inherit;
  font-size: 0.75rem;
}

.leaflet-popup-content button {
  border: 0;
  background: transparent;
  color: var(--cyan);
  padding: 0;
  font-weight: 800;
}

@media (max-width: 840px) {
  body {
    overflow: auto;
  }

  .app-header {
    height: auto;
    min-height: 74px;
    gap: 12px;
    padding: 14px 16px;
  }

  .header-status {
    align-items: flex-end;
    flex-direction: column;
    gap: 2px;
  }

  .header-actions {
    align-items: flex-end;
  }

  .header-status .status-dot,
  .status-divider {
    display: none;
  }

  .app-shell {
    height: auto;
    min-height: calc(100vh - 74px);
    grid-template-columns: 1fr;
  }

  .sidebar {
    max-height: none;
    border-right: 0;
  }

  .results-panel {
    max-height: 360px;
    overflow: hidden;
  }

  .map-panel {
    height: 62vh;
    min-height: 430px;
  }

  .map-toolbar {
    left: 10px;
    right: auto;
  }
}

@media (max-width: 500px) {
  h1 {
    font-size: 1.25rem;
  }

  .app-header {
    align-items: flex-start;
  }

  .header-actions {
    width: 100%;
    align-items: stretch;
  }

  .database-link {
    display: grid;
    place-items: center;
  }

  .search-panel {
    padding: 17px 14px;
  }

  .field-row,
  .detail-stats,
  .form-actions {
    grid-template-columns: 1fr;
  }

  .map-legend {
    display: none;
  }
}
