:root {
  color-scheme: light;
  --ink: #172033;
  --muted: #64748b;
  --line: #d7dee8;
  --panel: #ffffff;
  --panel-soft: #f6f8fb;
  --map-dark: #101820;
  --accent: #2563eb;
  --teal: #14b8a6;
  --red: #e11d48;
  --yellow: #f4b942;
  --shadow: 0 18px 55px rgba(15, 23, 42, 0.18);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* Minimal Leaflet base styles, kept local so the map still works if CDN CSS is blocked. */
.leaflet-container {
  position: relative;
  overflow: hidden;
  background: #d9e6ed;
  outline-offset: 1px;
  touch-action: pan-x pan-y;
}

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
  position: absolute;
  left: 0;
  top: 0;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
  user-select: none;
  -webkit-user-drag: none;
}

.leaflet-tile {
  visibility: hidden;
  border: 0;
  filter: inherit;
}

.leaflet-tile-loaded {
  visibility: inherit;
}

.leaflet-pane {
  z-index: 400;
}

.leaflet-tile-pane {
  z-index: 200;
}

.leaflet-overlay-pane {
  z-index: 400;
}

.leaflet-shadow-pane {
  z-index: 500;
}

.leaflet-marker-pane {
  z-index: 600;
}

.leaflet-tooltip-pane {
  z-index: 650;
}

.leaflet-popup-pane {
  z-index: 700;
}

.leaflet-map-pane canvas {
  z-index: 100;
}

.leaflet-map-pane svg {
  z-index: 200;
}

.leaflet-control {
  position: relative;
  z-index: 800;
  pointer-events: auto;
}

.leaflet-top,
.leaflet-bottom {
  position: absolute;
  z-index: 1000;
  pointer-events: none;
}

.leaflet-top {
  top: 0;
}

.leaflet-right {
  right: 0;
}

.leaflet-bottom {
  bottom: 0;
}

.leaflet-left {
  left: 0;
}

.leaflet-control {
  float: left;
  clear: both;
}

.leaflet-right .leaflet-control {
  float: right;
}

.leaflet-top .leaflet-control {
  margin-top: 10px;
}

.leaflet-bottom .leaflet-control {
  margin-bottom: 10px;
}

.leaflet-left .leaflet-control {
  margin-left: 10px;
}

.leaflet-right .leaflet-control {
  margin-right: 10px;
}

.leaflet-control-zoom {
  overflow: hidden;
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.14);
}

.leaflet-control-zoom a {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  color: #172033;
  text-decoration: none;
  font-size: 1.2rem;
  font-weight: 800;
  line-height: 1;
}

.leaflet-control-zoom a + a {
  border-top: 1px solid #e2e8f0;
}

.leaflet-control-attribution {
  margin: 0;
  border-radius: 6px 0 0 0;
  background: rgba(255, 255, 255, 0.82);
  color: #475569;
  padding: 3px 6px;
  font-size: 0.68rem;
}

.leaflet-popup {
  position: absolute;
  text-align: center;
  margin-bottom: 20px;
}

.leaflet-popup-content-wrapper {
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 28px rgba(15, 23, 42, 0.2);
}

.leaflet-popup-content {
  line-height: 1.35;
}

.leaflet-popup-tip-container {
  position: absolute;
  left: 50%;
  width: 24px;
  height: 12px;
  margin-left: -12px;
  overflow: hidden;
  pointer-events: none;
}

.leaflet-popup-tip {
  width: 14px;
  height: 14px;
  margin: -7px auto 0;
  transform: rotate(45deg);
  background: #fff;
  box-shadow: 0 8px 28px rgba(15, 23, 42, 0.16);
}

.leaflet-popup-close-button {
  position: absolute;
  top: 4px;
  right: 6px;
  border: 0;
  background: transparent;
  color: #64748b;
  text-decoration: none;
  font-size: 1.1rem;
}

.leaflet-interactive {
  cursor: pointer;
}

* {
  box-sizing: border-box;
}

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

body {
  color: var(--ink);
  background: #e8eef4;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 392px) 1fr;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  z-index: 500;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 18px 16px;
  border-bottom: 1px solid var(--line);
  background: #fbfcfe;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--map-dark);
  color: var(--yellow);
  font-weight: 800;
  letter-spacing: 0;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  line-height: 1.15;
}

.brand p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.control-section {
  display: grid;
  gap: 9px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.control-section label,
.list-heading {
  color: #334155;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.input-row {
  display: grid;
  grid-template-columns: 1fr 42px;
  gap: 8px;
  align-items: center;
}

input,
select {
  width: 100%;
  min-width: 0;
  height: 42px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.16);
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  color: #1e293b;
  transition:
    background 140ms ease,
    border-color 140ms ease,
    transform 140ms ease;
}

.icon-button:hover {
  background: #f8fafc;
  border-color: #94a3b8;
}

.icon-button:active {
  transform: translateY(1px);
}

.icon-button svg {
  width: 19px;
  height: 19px;
}

.feed-section {
  gap: 10px;
}

.check-row {
  display: grid;
  grid-template-columns: 18px 10px 1fr;
  align-items: center;
  gap: 9px;
  color: #1f2937;
  font-size: 0.94rem;
  font-weight: 650;
  text-transform: none;
}

.check-row input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
}

.feed-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
}

.feed-dot.metro {
  background: var(--teal);
}

.feed-dot.regional {
  background: var(--red);
}

.stats-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid var(--line);
  background: var(--panel-soft);
}

.stats-strip div {
  min-width: 0;
  padding: 13px 12px;
  border-right: 1px solid var(--line);
}

.stats-strip div:last-child {
  border-right: 0;
}

.stats-strip strong {
  display: block;
  font-size: 1.06rem;
  line-height: 1.2;
}

.stats-strip span {
  color: var(--muted);
  font-size: 0.76rem;
}

.list-section {
  display: flex;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

.list-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
}

.train-list {
  display: grid;
  align-content: start;
  gap: 8px;
  min-height: 0;
  overflow: auto;
  padding: 12px;
}

.train-row {
  display: grid;
  grid-template-columns: 8px 1fr auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 72px;
  border: 1px solid #d9e1ec;
  border-radius: 8px;
  background: #fff;
  padding: 10px;
  color: var(--ink);
  text-align: left;
}

.train-row:hover,
.train-row.selected {
  border-color: #94a3b8;
  background: #fbfdff;
}

.train-color {
  width: 8px;
  height: 48px;
  border-radius: 999px;
  background: var(--train-color, var(--accent));
}

.train-main {
  min-width: 0;
}

.train-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.96rem;
  font-weight: 800;
}

.train-meta,
.train-submeta {
  overflow: hidden;
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.78rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-pill {
  max-width: 112px;
  overflow: hidden;
  border: 1px solid #cbd5e1;
  border-radius: 999px;
  padding: 5px 8px;
  color: #334155;
  font-size: 0.72rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-state {
  padding: 22px 14px;
  color: var(--muted);
  text-align: center;
  font-size: 0.92rem;
}

.map-area {
  position: relative;
  min-width: 0;
  min-height: 0;
}

#map {
  width: 100%;
  height: 100%;
  background: #d9e6ed;
}

.map-toolbar {
  position: absolute;
  top: 14px;
  right: 14px;
  display: flex;
  gap: 8px;
  z-index: 600;
}

.map-action {
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.14);
}

.status-badge {
  position: absolute;
  left: 16px;
  bottom: 18px;
  z-index: 600;
  max-width: min(460px, calc(100% - 32px));
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 8px 26px rgba(15, 23, 42, 0.16);
  color: #1e293b;
  padding: 10px 12px;
  font-size: 0.86rem;
  font-weight: 750;
}

.status-badge.live {
  border-color: rgba(20, 184, 166, 0.35);
}

.status-badge.error {
  border-color: rgba(225, 29, 72, 0.35);
}

.detail-panel {
  position: absolute;
  right: 14px;
  bottom: 18px;
  z-index: 650;
  width: min(430px, calc(100% - 28px));
  max-height: min(680px, calc(100% - 94px));
  overflow: auto;
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: var(--shadow);
}

.detail-header {
  display: grid;
  grid-template-columns: 1fr 42px;
  gap: 10px;
  align-items: start;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.detail-title {
  margin: 0;
  font-size: 1.08rem;
}

.detail-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.detail-body {
  display: grid;
  gap: 14px;
  padding: 14px 16px 16px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.detail-field {
  min-width: 0;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 10px;
  background: #fbfdff;
}

.detail-field span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.detail-field strong {
  display: block;
  overflow: hidden;
  margin-top: 4px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.carriage-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 8px;
}

.carriage-item {
  min-width: 0;
  border: 1px solid #d9e1ec;
  border-radius: 8px;
  padding: 9px;
  background: #fff;
}

.carriage-item strong,
.carriage-item span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.carriage-item strong {
  font-size: 0.84rem;
}

.carriage-item span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.72rem;
}

.train-marker {
  background: transparent;
  border: 0;
}

.marker-shell {
  position: relative;
  width: 36px;
  height: 36px;
  transform: rotate(var(--angle, 0deg));
  transform-origin: center;
}

.marker-core {
  position: absolute;
  left: 9px;
  top: 9px;
  z-index: 2;
  width: 18px;
  height: 18px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--marker-color, var(--accent));
  box-shadow: 0 5px 12px rgba(15, 23, 42, 0.36);
}

.marker-arrow {
  position: absolute;
  left: 10px;
  top: -2px;
  z-index: 1;
  width: 16px;
  height: 25px;
  background: var(--marker-color, var(--accent));
  clip-path: polygon(50% 0, 100% 58%, 65% 58%, 65% 100%, 35% 100%, 35% 58%, 0 58%);
  filter: drop-shadow(0 2px 5px rgba(15, 23, 42, 0.28));
}

.marker-no-bearing {
  transform: none;
}

.marker-no-bearing .marker-arrow {
  display: none;
}

.marker-inferred-bearing .marker-arrow {
  opacity: 0.86;
}

.marker-stale .marker-core,
.marker-stale .marker-arrow {
  opacity: 0.52;
}

.marker-selected .marker-core {
  left: 7px;
  top: 7px;
  width: 22px;
  height: 22px;
  border-color: var(--yellow);
}

.marker-selected .marker-arrow {
  filter:
    drop-shadow(0 0 3px rgba(244, 185, 66, 0.95))
    drop-shadow(0 3px 7px rgba(15, 23, 42, 0.28));
}

.leaflet-popup-content {
  margin: 10px 12px;
  min-width: 180px;
}

.popup-title {
  font-weight: 850;
}

.popup-meta {
  margin-top: 4px;
  color: #64748b;
  font-size: 0.82rem;
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(290px, 42vh) 1fr;
  }

  .sidebar {
    order: 2;
    border-top: 1px solid var(--line);
    border-right: 0;
  }

  .map-area {
    order: 1;
  }

  .brand,
  .control-section {
    padding-left: 14px;
    padding-right: 14px;
  }

  .feed-section {
    grid-template-columns: 1fr;
  }

  .stats-strip {
    grid-template-columns: repeat(3, 1fr);
  }

  .detail-panel {
    left: 10px;
    right: 10px;
    bottom: 10px;
    width: auto;
    max-height: calc(100% - 72px);
  }

  .status-badge {
    left: 10px;
    bottom: 10px;
  }
}
