:root {
  --bg: #0d1117;
  --panel: #161d27;
  --panel-border: #2b3648;
  --text: #e6edf3;
  --muted: #8b98a9;
  --accent: #5aa9e9;
  --chip-bg: #0d1420;
  --overlay-bg: rgba(13, 20, 32, 0.4);
  --hover-bg: rgba(255, 255, 255, 0.05);
  --button-bg: #1c2530;
  --button-bg-hover: #26313e;
  --button-border: #33414f;
  --button-text: #d7e1ea;
  --divider: #1c2530;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

[data-theme='light'] {
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-border: #d5dbe3;
  --text: #1f2933;
  --muted: #5b6570;
  --accent: #1b6fb8;
  --chip-bg: #e7ecf2;
  --overlay-bg: rgba(255, 255, 255, 0.6);
  --hover-bg: rgba(31, 41, 51, 0.06);
  --button-bg: #e7ecf2;
  --button-bg-hover: #d9e0e9;
  --button-border: #c3ccd6;
  --button-text: #1f2933;
  --divider: #e2e7ed;
  --shadow: 0 12px 40px rgba(31, 41, 51, 0.18);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

#panel {
  position: absolute;
  top: 16px;
  left: 16px;
  width: 320px;
  max-height: calc(100% - 32px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 10;
}

#panel-header {
  padding: 16px 16px 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-title {
  flex: 1;
  min-width: 0;
}

#panel-toggle {
  display: none;
  background: var(--button-bg);
  color: var(--button-text);
  border: 1px solid var(--button-border);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
}

#panel-header h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.02em;
}

#status-line {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
}

#filters {
  padding: 0 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#line-search {
  background: var(--chip-bg);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

#line-search:focus {
  outline: none;
  border-color: var(--accent);
}

.toggle {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

#line-list {
  margin: 0;
  padding: 0 8px 12px;
  overflow-y: auto;
  flex: 1;
}

.line-network,
.line-mode {
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  margin: 6px 0;
  background: var(--overlay-bg);
}

.line-network > summary,
.line-mode > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  cursor: pointer;
  font-weight: 700;
  font-size: 13px;
  color: var(--text);
  border-radius: 10px;
}

.line-network > summary:hover,
.line-mode > summary:hover {
  background: var(--hover-bg);
}

.network-logo {
  height: 20px;
  max-width: 60px;
  object-fit: contain;
  flex-shrink: 0;
}

.network-title,
.mode-title {
  flex: 1;
}

.line-mode {
  border: none;
  border-top: 1px solid var(--panel-border);
  border-radius: 0;
  margin: 0;
  background: none;
}

.line-group {
  padding: 0 4px 6px;
}

.line-group h4 {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 8px 6px 4px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.line-group-lines {
  list-style: none;
  margin: 0;
  padding: 0;
}

.line-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.line-item:hover { background: var(--hover-bg); }
.line-item.selected { background: rgba(90, 169, 233, 0.15); }

.line-badge {
  min-width: 42px;
  height: 24px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: #fff;
  flex-shrink: 0;
}

.line-badge.lb-circle {
  border-radius: 50%;
  padding: 0 4px;
}

.line-badge.lb-square {
  border-radius: 22.5%;
  padding: 0 4px;
}

.line-badge.lb-text {
  background: transparent;
  font-size: 14px;
  letter-spacing: -0.02em;
}

.brand-logo {
  height: 16px;
  max-width: 34px;
  object-fit: contain;
  flex-shrink: 0;
}

.brand-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  flex-shrink: 0;
}

.line-item,
.board-row {
  gap: 6px;
}

.line-logo {
  height: 22px;
  min-width: 42px;
  max-width: 60px;
  object-fit: contain;
  flex-shrink: 0;
}

h2 .line-logo {
  height: 26px;
  vertical-align: middle;
}

.board-row .line-logo {
  height: 20px;
}

.line-meta {
  flex: 1;
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.line-count {
  background: var(--chip-bg);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  font-size: 11px;
  padding: 2px 8px;
  color: var(--text);
}

#vehicle-detail {
  position: absolute;
  right: 16px;
  top: 16px;
  width: 320px;
  max-height: calc(100% - 32px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
  z-index: 10;
  font-size: 13px;
}

#vehicle-detail h2 {
  margin: 0 0 6px;
  font-size: 16px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding-right: 20px;
  overflow-wrap: anywhere;
}

#vehicle-detail h2 .line-badge,
#vehicle-detail h2 .line-logo,
#vehicle-detail h2 .brand-logo {
  flex-shrink: 0;
}

#vehicle-detail h2 .line-badge {
  min-width: 0;
  padding: 0 8px;
}

#vehicle-detail .close {
  position: absolute;
  top: 8px;
  right: 12px;
  cursor: pointer;
  color: var(--muted);
  font-size: 16px;
  background: none;
  border: none;
}

.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 11px;
  font-weight: 600;
}

.badge.realtime { background: rgba(111, 207, 151, 0.2); color: #6fcf97; }
.badge.scheduled { background: rgba(242, 201, 76, 0.15); color: #f2c94c; }
.badge.on-time { background: rgba(111, 207, 151, 0.2); color: #6fcf97; }
.badge.slight-delay { background: rgba(242, 153, 74, 0.2); color: #f2994a; }
.badge.delayed { background: rgba(235, 87, 87, 0.2); color: #eb5757; }

.detail-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--panel-border);
}

.detail-row:last-child { border-bottom: none; }

.detail-row .label { color: var(--muted); }
.detail-row .value {
  text-align: right;
  overflow-wrap: anywhere;
  min-width: 0;
}

.maplibregl-popup-content {
  background: var(--panel) !important;
  color: var(--text) !important;
  border-radius: 12px !important;
  border: 1px solid var(--panel-border);
  font-size: 13px;
}

.maplibregl-popup-tip {
  border-top-color: var(--panel) !important;
}

@media (max-width: 700px) {
  #panel {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: 78vh;
    max-height: 78dvh;
    border-radius: 16px 16px 0 0;
    border-width: 1px 0 0 0;
  }

  #panel-toggle {
    display: block;
  }

  #panel-toggle::before {
    content: '☰';
  }

  body:not(.panel-collapsed) #panel-toggle::before {
    content: '▾';
  }

  body.panel-collapsed #filters,
  body.panel-collapsed #line-list {
    display: none;
  }

  #panel-header {
    padding: 10px 12px;
    min-height: 48px;
  }

  #filters {
    padding: 0 12px 12px;
    gap: 6px;
  }

  #vehicle-detail,
  #stop-board {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: 70vh;
    max-height: 70dvh;
    border-radius: 16px 16px 0 0;
    border-width: 1px 0 0 0;
    z-index: 12;
  }

  .maplibregl-ctrl-top-right {
    top: 10px;
  }
}

#stop-board {
  position: absolute;
  right: 16px;
  top: 16px;
  width: 340px;
  max-height: calc(100% - 32px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
  z-index: 11;
  font-size: 13px;
}

#stop-board h2 {
  margin: 0 0 2px;
  font-size: 16px;
}

.board-toggle {
  display: flex;
  gap: 4px;
  margin: 4px 0 8px;
}

.board-toggle button {
  flex: 1;
  border: 1px solid var(--panel-border);
  background: var(--chip-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}

.board-toggle button.active {
  background: rgba(90, 169, 233, 0.25);
  border-color: rgba(90, 169, 233, 0.6);
  font-weight: 700;
}

#stop-board .stop-sub {
  color: var(--muted);
  font-size: 11px;
  margin-bottom: 10px;
}

.board-alert {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  margin: 4px 0;
  border-radius: 8px;
  font-size: 12px;
}

.board-alert .alert-chip {
  flex-shrink: 0;
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.board-alert .alert-text {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.alert-disrupted {
  background: rgba(235, 87, 87, 0.15);
  border: 1px solid rgba(235, 87, 87, 0.4);
}

.alert-disrupted .alert-chip {
  background: #eb5757;
  color: #fff;
}

.alert-works {
  background: rgba(242, 153, 74, 0.15);
  border: 1px solid rgba(242, 153, 74, 0.4);
}

.alert-works .alert-chip {
  background: #f2994a;
  color: #fff;
}

.alert-info {
  background: rgba(90, 169, 233, 0.15);
  border: 1px solid rgba(90, 169, 233, 0.4);
}

.alert-info .alert-chip {
  background: #5aa9e9;
  color: #fff;
}

.alert-neutral {
  background: var(--chip-bg);
  border: 1px solid var(--panel-border);
}

.alert-neutral .alert-chip {
  background: var(--muted);
  color: #fff;
}

.board-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 0;
  border-bottom: 1px solid var(--panel-border);
}

.board-row:last-child { border-bottom: none; }

.board-time {
  font-variant-numeric: tabular-nums;
  text-align: right;
  min-width: 40px;
}

.board-time .scheduled-late {
  text-decoration: line-through;
  color: var(--muted);
  font-size: 11px;
  display: block;
}

.board-eta {
  color: var(--muted);
  font-size: 11px;
  min-width: 46px;
  text-align: right;
}

.board-dest {
  flex: 1;
  overflow-wrap: anywhere;
  min-width: 0;
}

#stop-search {
  margin-top: 6px;
}

#stop-search-results {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 4px;
  max-height: 220px;
  overflow-y: auto;
}

.stop-search-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: var(--overlay-bg);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 6px 10px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-size: 12px;
}

.stop-search-item:hover {
  background: rgba(90, 169, 233, 0.15);
}

.stop-search-meta {
  color: var(--muted);
  font-size: 10px;
  flex-shrink: 0;
}

.panel-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.panel-actions button {
  flex: 1;
}

#view-toggle,
#theme-toggle {
  margin-top: 8px;
  padding: 6px 10px;
  background: var(--button-bg);
  color: var(--button-text);
  border: 1px solid var(--button-border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}

.panel-actions button:hover {
  background: var(--button-bg-hover);
}

#mode-settings-details {
  margin-top: 8px;
  font-size: 13px;
}

#mode-settings-details summary {
  cursor: pointer;
  color: #8b98a9;
}

.mode-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
  border-bottom: 1px solid var(--divider);
}

.mode-check {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 140px;
}

.mode-zoom-label {
  color: #8b98a9;
  font-size: 12px;
}

.mode-zoom {
  width: 100%;
}

.speed-src {
  cursor: help;
  font-weight: bold;
  color: #8b98a9;
  border-bottom: 1px dotted #8b98a9;
}

.board-time.bt-scheduled {
  color: #f2994a;
}

.board-time.bt-ontime {
  color: #6fcf97;
}

.board-time.bt-late {
  color: #eb5757;
}

.board-time.bt-early {
  color: #56ccf2;
}

.board-time .time-icon {
  cursor: help;
  font-size: 11px;
  border-bottom: 1px dotted currentColor;
}

.board-row {
  overflow: hidden;
}

.board-row.expiring {
  animation: board-row-out 0.45s ease forwards;
  pointer-events: none;
}

@keyframes board-row-out {
  0% {
    opacity: 1;
    max-height: 60px;
    padding-top: 7px;
    padding-bottom: 7px;
  }
  100% {
    opacity: 0;
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    border-bottom-width: 0;
  }
}
