:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f3f4f6;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #d6336c;
  --ok: #2f9e44;
  --warn: #e8590c;
  --err: #c92a2a;
  --shadow: 0 6px 24px rgba(0, 0, 0, .12);
  --c1: #d6336c; --c2: #1971c2; --c9: #7048e8; --c3: #e8590c;
  --c4: #2b8a3e; --c6: #5c940d; --c7: #8f6b1f; --c8: #7a5230;
  --c5: #868e96; --c0: #343a40; --cq: #495057;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0d12; --surface: #151922; --surface-2: #1e2430; --text: #e5e7eb; --muted: #9ca3af;
    --border: #2a3140; --shadow: 0 6px 24px rgba(0, 0, 0, .5);
    --c0: #adb5bd; --cq: #ced4da; --c5: #a5adb5;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0d12; --surface: #151922; --surface-2: #1e2430; --text: #e5e7eb; --muted: #9ca3af;
  --border: #2a3140; --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  --c0: #adb5bd; --cq: #ced4da; --c5: #a5adb5;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
#map { position: fixed; inset: 0; background: var(--surface-2); }

.panel {
  position: fixed; top: 12px; left: 12px; bottom: 12px; width: 340px; z-index: 1000;
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); overflow: hidden;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 6px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; color: var(--accent); }
.brand span { color: var(--text); letter-spacing: -.01em; }
.icon-btn { border: 0; background: transparent; color: var(--muted); padding: 6px; border-radius: 8px; cursor: pointer; display: none; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.status { display: flex; align-items: center; gap: 8px; margin: 0 14px 8px; color: var(--muted); font-size: 12.5px; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status.ok .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.status.warn .dot { background: var(--warn); }
.status.err .dot { background: var(--err); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 0 14px 10px; }
.stats div { background: var(--surface-2); border-radius: 10px; padding: 8px 6px; text-align: center; }
.stats b { display: block; font-size: 16px; font-variant-numeric: tabular-nums; }
.stats span { font-size: 11px; color: var(--muted); }

.controls { padding: 0 14px 10px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border); }
#search {
  width: 100%; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; outline: none;
}
#search:focus { border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: 12px;
  padding: 3px 8px; border-radius: 999px; cursor: pointer; display: inline-flex; align-items: center; gap: 5px;
}
.chip i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }
.chip[aria-pressed="false"] { opacity: .45; }
.check { display: flex; gap: 6px; align-items: center; font-size: 12.5px; color: var(--muted); cursor: pointer; }

.list { list-style: none; margin: 0; padding: 4px 6px; overflow-y: auto; flex: 1; }
.list li {
  display: grid; grid-template-columns: auto 1fr auto; gap: 2px 10px; align-items: center;
  padding: 8px; border-radius: 10px; cursor: pointer;
}
.list li:hover, .list li.sel { background: var(--surface-2); }
.list .hc { grid-row: span 2; }
.list .where { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .age { grid-row: span 2; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.list .empty { display: block; color: var(--muted); text-align: center; padding: 24px 12px; cursor: default; }
.list .empty:hover { background: none; }

.hc {
  display: inline-block; font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #fff; background: var(--cls, var(--cq)); padding: 4px 6px; border-radius: 6px; letter-spacing: .03em;
}
.hc.approx { background: transparent; color: var(--cls, var(--cq)); box-shadow: inset 0 0 0 2px var(--cls, var(--cq)); }

.foot { padding: 8px 14px 10px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); }

/* Map markers */
.train-icon { background: none; border: 0; }
.train-marker { position: relative; transform: translate(-50%, -50%); width: max-content; }
.train-marker .hc { box-shadow: 0 1px 4px rgba(0,0,0,.35); }
.train-marker .hc.approx { background: var(--surface); box-shadow: inset 0 0 0 2px var(--cls), 0 1px 4px rgba(0,0,0,.25); }
.train-marker .arrow {
  position: absolute; left: 50%; top: 50%; width: 0; height: 0; pointer-events: none;
  transform-origin: 0 0;
}
.train-marker .arrow::after {
  content: ""; position: absolute; left: -5px; top: -24px;
  border: 5px solid transparent; border-bottom: 7px solid var(--cls); border-top: 0;
}
.train-marker.sel .hc { outline: 3px solid var(--accent); outline-offset: 2px; }
.dot-marker { width: 9px; height: 9px; border-radius: 50%; background: var(--cls); border: 1.5px solid var(--surface); transform: translate(-50%, -50%); }
.dot-marker.approx { background: var(--surface); border-color: var(--cls); }

/* Detail card */
.detail {
  position: fixed; top: 12px; right: 12px; width: 300px; z-index: 1000; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 14px;
}
.detail h2 { margin: 0 0 2px; padding-right: 28px; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.detail .close { position: absolute; top: 8px; right: 8px; }
.detail .close { display: block; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 12px 0 0; font-size: 13px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; font-variant-numeric: tabular-nums; }
.detail .note { margin-top: 10px; font-size: 12px; color: var(--muted); }
.detail a { color: var(--accent); }
.detail { max-height: calc(100vh - 24px); overflow-y: auto; }
.punc { font-weight: 600; }
.punc.ontime { color: var(--ok); }
.punc.slight { color: var(--warn); }
.punc.late { color: var(--err); }

.leaflet-container { font: inherit; background: var(--surface-2); }
.leaflet-control-attribution { font-size: 10px; }

@media (max-width: 720px) {
  .panel { top: auto; left: 0; right: 0; bottom: 0; width: auto; height: 55vh; border-radius: 16px 16px 0 0; border-bottom: 0; }
  .panel.collapsed { height: 112px; }
  .panel { height: 60vh; }
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
  .chip { flex: none; }
  .stats div { padding: 6px 4px; }
  .panel.collapsed .controls, .panel.collapsed .list, .panel.collapsed .foot { display: none; }
  .icon-btn { display: block; }
  .detail { top: 12px; left: 16px; right: 16px; width: auto; max-height: 40vh; }
}
