:root {
  --rail: #0f2a3d;
  --rail-2: #143a54;
  --accent: #14b8a6;
  --accent-2: #0ea5e9;
  --bg: #eef2f6;
  --card: #ffffff;
  --ink: #1e293b;
  --muted: #64748b;
  --moving: #16a34a;
  --idling: #f59e0b;
  --parked: #dc2626;
  --nosignal: #6b7280;
  --stale: #a855f7;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --line: #e2e8f0;
  --line-soft: #f1f5f9;
  --hover: #f0fdfa;
  --link-strong: #0f4c75;
  --shadow: 0 1px 3px rgba(15,42,61,.08);
  /* port terminal */
  --term-bg: #fbfcfd;
  --term-ink: #1e293b;
  --term-dim: #64748b;
  --term-rx: #0369a1;
  --term-tx: #7c3aed;
  --term-ok: #15803d;
  --term-warn: #b45309;
  --term-err: #b91c1c;
  --term-sel: #e0f2fe;
}
[data-bs-theme="dark"] {
  --bg: #0b1220;
  --card: #111a2b;
  --ink: #e2e8f0;
  --muted: #94a3b8;
  --panel: #111a2b;
  --panel-2: #0f1726;
  --line: #22304a;
  --line-soft: #1a2538;
  --hover: #12302f;
  --link-strong: #7dd3fc;
  --shadow: 0 1px 3px rgba(0,0,0,.5);
  --rail: #07101d;
  --rail-2: #0b1a2c;
  --term-bg: #05090f;
  --term-ink: #d1d9e6;
  --term-dim: #6b7a90;
  --term-rx: #38bdf8;
  --term-tx: #c4b5fd;
  --term-ok: #4ade80;
  --term-warn: #fbbf24;
  --term-err: #f87171;
  --term-sel: #10243a;
  color-scheme: dark;
}
[data-bs-theme="dark"] .card { background: var(--card); }
[data-bs-theme="dark"] .leaflet-tile-pane { filter: brightness(.72) invert(1) contrast(.85) hue-rotate(200deg) saturate(.35) brightness(.85); }
[data-bs-theme="dark"] .leaflet-container { background: #0b1220; }
[data-bs-theme="dark"] .leaflet-popup-content-wrapper, [data-bs-theme="dark"] .leaflet-popup-tip { background: var(--card); color: var(--ink); }
[data-bs-theme="dark"] .leaflet-bar a, [data-bs-theme="dark"] .leaflet-control-layers { background: var(--card); color: var(--ink); border-color: var(--line); }
[data-bs-theme="dark"] .table-light { --bs-table-bg: #16223a; --bs-table-color: var(--ink); --bs-table-border-color: var(--line); }
.theme-btn { width: 34px; padding-left: 0; padding-right: 0; }
html, body { height: 100%; }
body { background: var(--bg); color: var(--ink); font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; margin: 0; }

.rail { position: fixed; inset: 0 auto 0 0; width: 84px; background: linear-gradient(180deg, var(--rail), var(--rail-2)); z-index: 1040; display: flex; flex-direction: column; align-items: center; padding-top: 10px; }
.rail-brand { color: var(--accent); font-size: 28px; margin-bottom: 14px; }
.rail nav { display: flex; flex-direction: column; width: 100%; }
.rail nav a { color: #cbd5e1; text-decoration: none; text-align: center; padding: 11px 4px; font-size: 11px; border-left: 3px solid transparent; }
.rail nav a i { display: block; font-size: 21px; margin-bottom: 2px; }
.rail nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.rail nav a.active { color: #fff; background: rgba(20,184,166,.18); border-left-color: var(--accent); }

.shell { margin-left: 84px; min-height: 100%; display: flex; flex-direction: column; }
.topbar { height: 54px; background: var(--rail); color: #fff; display: flex; align-items: center; justify-content: space-between; padding: 0 18px; position: sticky; top: 0; z-index: 1030; }
.brand-text { letter-spacing: .3px; }
.env-badge { background: var(--accent); }
.clock { font-size: 13px; background: rgba(255,255,255,.08); padding: 3px 10px; border-radius: 6px; }
.rx-pill { font-size: 12px; padding: 3px 10px; border-radius: 20px; background: rgba(255,255,255,.08); }
.rx-pill.up i { color: #22c55e; }
.rx-pill.down i { color: #ef4444; }

.content { padding: 18px; }
.content-flush { padding: 0; flex: 1; display: flex; flex-direction: column; }

.card { border: 0; border-radius: 10px; box-shadow: var(--shadow); }
.card-header { background: transparent; font-weight: 600; border-bottom: 1px solid var(--line); }
.kpi { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.kpi .value { font-size: 26px; font-weight: 700; }
.kpi .sub { font-size: 12px; color: var(--muted); }
.kpi.moving { border-top: 3px solid var(--moving); }
.kpi.idling { border-top: 3px solid var(--idling); }
.kpi.parked { border-top: 3px solid var(--parked); }
.kpi.nosignal { border-top: 3px solid var(--nosignal); }
.kpi.all { border-top: 3px solid var(--accent-2); }

.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.dot.moving { background: var(--moving); } .dot.idling { background: var(--idling); }
.dot.parked { background: var(--parked); } .dot.nosignal { background: var(--nosignal); }
.link-online { color: var(--moving); } .link-stale { color: var(--stale); } .link-offline, .link-never, .link-disabled { color: var(--nosignal); }
.badge-state { font-size: 11px; font-weight: 600; }
.bg-moving { background: var(--moving) !important; } .bg-idling { background: var(--idling) !important; }
.bg-parked { background: var(--parked) !important; } .bg-nosignal { background: var(--nosignal) !important; }

/* Live map layout */
.live-wrap { display: flex; flex: 1; min-height: calc(100vh - 54px); }
.vpanel { width: 380px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.vtabs { display: grid; grid-template-columns: repeat(5, 1fr); border-bottom: 1px solid var(--line); }
.vtabs button { border: 0; background: var(--panel-2); color: var(--ink); padding: 8px 2px; font-size: 11px; border-bottom: 3px solid transparent; }
.vtabs button b { display: block; font-size: 16px; }
.vtabs button.active { background: var(--panel); border-bottom-color: var(--accent); }
.vtabs button[data-f=moving] b { color: var(--moving); } .vtabs button[data-f=idling] b { color: var(--idling); }
.vtabs button[data-f=parked] b { color: var(--parked); } .vtabs button[data-f=nosignal] b { color: var(--nosignal); }
.vsearch { padding: 8px; border-bottom: 1px solid var(--line); }
.vlist { overflow-y: auto; flex: 1; }
.vgroup { background: #334155; color: #fff; font-size: 12px; font-weight: 600; padding: 6px 10px; text-transform: uppercase; }
.vitem { padding: 9px 12px; border-bottom: 1px solid var(--line-soft); cursor: pointer; }
.vitem:hover, .vitem.sel { background: var(--hover); }
.vitem .nm { font-weight: 600; color: var(--link-strong); }
.vitem .meta { font-size: 12px; color: var(--muted); }
#map { flex: 1; min-height: 400px; }
.map-tools { position: absolute; top: 10px; right: 10px; z-index: 500; }
.leaflet-popup-content { font-size: 13px; min-width: 220px; }
.pop-title { font-weight: 700; margin-bottom: 4px; }
.pop-grid { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; }
.pop-grid span:nth-child(odd) { color: var(--muted); }
.veh-icon { border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; color: #fff; font-size: 14px; }
.veh-icon i { display: inline-block; }

.hex { font-family: ui-monospace, Consolas, monospace; font-size: 12px; word-break: break-all; }
.hex-part { display: inline-block; padding: 2px 4px; margin: 1px; border-radius: 4px; }
.table-sm td, .table-sm th { font-size: 13px; vertical-align: middle; }
.step-ok { color: var(--moving); } .step-fail { color: var(--parked); }
code.cmd { display: block; background: #0f172a; border: 1px solid var(--line); color: #e2e8f0; padding: 8px 10px; border-radius: 6px; font-size: 12px; white-space: pre-wrap; }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, var(--rail), #0b5563); }
body.login .rail, body.login .topbar { display: none; }
body.login .shell { margin-left: 0; }
@media (max-width: 900px) { .vpanel { width: 100%; max-height: 45vh; } .live-wrap { flex-direction: column; } }

/* ---------------- Port terminal ---------------- */
.term-page { display: flex; flex-direction: column; height: calc(100vh - 54px); }
.term-head { padding: 12px 18px 0; }
.term-status { display: flex; flex-wrap: wrap; gap: 8px 18px; align-items: center; font-size: 13px; }
.term-status .k { color: var(--muted); }
.term-status code { color: inherit; }
.term-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--panel); }
.term-bar .form-control, .term-bar .form-select { width: auto; }
.term-bar .kinds .btn { font-size: 12px; }
.term-bar .spacer { flex: 1; }
.term { flex: 1; overflow: auto; background: var(--term-bg); color: var(--term-ink); font: 12.5px/1.55 ui-monospace, "Cascadia Mono", Consolas, "SFMono-Regular", Menlo, monospace; padding: 6px 0 24px; }
.term .ln { display: grid; grid-template-columns: 96px 150px 128px 54px minmax(0, 1fr); gap: 0 10px; padding: 1px 18px; cursor: pointer; border-left: 3px solid transparent; }
.term .ln:hover { background: var(--term-sel); }
.term .ln.open { background: var(--term-sel); border-left-color: var(--accent); }
.term .t, .term .r, .term .im { color: var(--term-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.term .k { font-weight: 700; }
.term .body { white-space: pre-wrap; word-break: break-all; }
.term .k-rx { color: var(--term-rx); } .term .k-tx { color: var(--term-tx); }
.term .k-connect, .term .k-close { color: var(--term-dim); }
.term .lv-ok .k, .term .lv-ok .msg { color: var(--term-ok); }
.term .lv-warn .k, .term .lv-warn .msg { color: var(--term-warn); }
.term .lv-error .k, .term .lv-error .msg { color: var(--term-err); }
.term .hexb { color: var(--term-ink); }
.term .asc { color: var(--term-dim); }
.term .dump { grid-column: 1 / -1; margin: 4px 0 6px 0; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--ink); white-space: pre; overflow-x: auto; cursor: auto; }
.term .dump a { color: var(--accent-2); }
.term .sep { color: var(--term-dim); padding: 6px 18px; font-style: italic; }
.term-empty { padding: 30px 18px; color: var(--term-dim); white-space: normal; font-family: var(--bs-body-font-family); font-size: 14px; }
.term-foot { display: flex; justify-content: space-between; gap: 10px; padding: 6px 18px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); background: var(--panel); }
.live-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--nosignal); margin-right: 4px; }
.live-dot.on { background: #22c55e; box-shadow: 0 0 0 3px rgba(34,197,94,.25); animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(34,197,94,0); } }
@media (prefers-reduced-motion: reduce) { .live-dot.on { animation: none; } }
@media (max-width: 900px) {
  .term .ln { grid-template-columns: 78px 44px minmax(0, 1fr); }
  .term .r, .term .im { display: none; }
  .term-page { height: auto; min-height: calc(100vh - 54px); }
  .term { min-height: 60vh; }
}
.login-theme { position: fixed; top: 14px; right: 14px; z-index: 10; }
@media (max-width: 700px) {
  .topbar .clock, .topbar .env-badge, .topbar .rx-pill { display: none; }
  .topbar { padding: 0 10px; }
}
