:root {
  --green: #16a34a;
  --amber: #f59e0b;
  --red: #ef4444;
  --warn: #f5b301;
  --route: #4f46e5;
  --ink: #1f2937;
  --dim: #8a94a3;
  --label: #9aa3b0;
  --bg: #f4f6f8;
  --card: #ffffff;
  --line: rgba(30, 41, 59, 0.10);
  --line-soft: rgba(30, 41, 59, 0.06);
  --shadow: 0 8px 30px rgba(30, 45, 70, 0.12);
  --chip: #f1f3f6;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
body { font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); -webkit-font-smoothing: antialiased; font-size: 14px; display: flex; flex-direction: column; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }
[hidden] { display: none !important; }

/* ---------- appbar + flikar ---------- */
.appbar { display: flex; align-items: center; gap: clamp(12px, 2vw, 28px); padding: 10px clamp(12px, 1.4vw, 20px); background: var(--card); border-bottom: 1px solid var(--line); z-index: 20; }
.brand { display: flex; flex-direction: row; align-items: center; gap: 12px; }
.brand .logo { height: 62px; width: 62px; flex: none; display: block; }
.brand-text { display: flex; flex-direction: column; line-height: 1.04; }
.wordmark { font-family: "Quicksand", Inter, system-ui, sans-serif; font-size: clamp(20px, 1.9vw, 27px); font-weight: 700; letter-spacing: -0.4px; color: #1b2950; }
.tagline { font-family: "Quicksand", Inter, system-ui, sans-serif; font-size: clamp(12px, 1.1vw, 15px); font-weight: 600; color: #6a7a96; margin-top: 1px; }
.tabs { display: flex; gap: 10px; }
.tab { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; background: #fff; border: 1px solid var(--line); padding: 9px 16px; border-radius: 12px; font-size: 14px; font-weight: 600; font-family: inherit; color: var(--ink); box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04); transition: background .15s, color .15s, border-color .15s, box-shadow .15s; }
.tab svg { width: 18px; height: 18px; flex: none; }
.tab:hover { background: #f6f7fb; border-color: #d4d9e3; }
.tab.active { background: rgba(79, 70, 229, 0.10); border-color: rgba(79, 70, 229, 0.30); color: var(--route); box-shadow: none; }
.appstatus { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.mode { font-size: 11.5px; font-weight: 700; letter-spacing: 0.6px; padding: 3px 10px; border-radius: 999px; }
.mode.live { color: #0f7a36; background: rgba(22, 163, 74, 0.14); animation: pulse 2s infinite; }
.mode.playback { color: #9a6b00; background: rgba(245, 158, 11, 0.18); }
.clock { font-family: ui-monospace, monospace; font-size: 13px; color: var(--ink); }
.rps { font-size: 12px; color: var(--dim); } .rps b { color: var(--ink); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

/* ---------- vy-container ---------- */
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ---------- horisontell filterrad ---------- */
.filterbar { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; padding: 12px clamp(12px, 1.4vw, 20px); background: var(--card); border-bottom: 1px solid var(--line); z-index: 15; }
.fb-field { display: flex; flex-direction: column; gap: 4px; }
.fb-field label { font-size: 10px; font-weight: 600; letter-spacing: 0.4px; text-transform: uppercase; color: var(--dim); }
.fb-field input, .fb-field select { height: 34px; min-width: 150px; background: #fff; color: var(--ink); border: 1px solid var(--line); border-radius: 9px; padding: 0 9px; font-size: 13px; font-family: inherit; }
.fb-field select[multiple] { height: 34px; padding: 4px 6px; overflow: auto; }
.fb-field input:focus, .fb-field select:focus { outline: none; border-color: var(--route); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12); }
.fb-actions { display: flex; gap: 8px; }
.fb-meta { margin-left: auto; display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
button { cursor: pointer; background: #fff; color: var(--ink); border: 1px solid var(--line); border-radius: 9px; padding: 0 14px; height: 34px; font-size: 13px; font-weight: 600; font-family: inherit; transition: background .15s, border-color .15s, box-shadow .15s; }
button:hover { background: #f6f8fb; }
button.primary { background: var(--route); border-color: var(--route); color: #fff; }
button.primary:hover { background: #4338ca; }
.dim { color: var(--dim); font-size: 11px; }

/* ---------- karta + HUD ---------- */
.mapwrap { position: relative; flex: 1; min-height: 0; overflow: hidden; }
#map { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; }
.hud { position: absolute; z-index: 5; }
.detail, .legend, .transport, .zoomctl, .down-banner, .statusbar { background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); }

/* system-filter (Livevy) */
.sysfilter { top: 16px; left: 16px; pointer-events: auto; display: flex; flex-direction: column; align-items: stretch; width: max-content; }
.sysfilter-btn { background: var(--card); border: 1px solid var(--line); border-radius: 11px; box-shadow: var(--shadow); height: 38px; padding: 0 14px; font-weight: 600; font-size: 13px; display: inline-flex; align-items: center; gap: 7px; }
.sysfilter-btn .caret { color: var(--dim); font-size: 11px; }
.sysfilter-panel { margin-top: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 13px; box-shadow: var(--shadow); padding: 10px; width: 220px; }
.sysfilter-actions { display: flex; gap: 8px; margin-bottom: 8px; }
.sysfilter-actions button { flex: 1; height: 30px; padding: 0; font-size: 12px; }
.sysfilter-row { display: flex; align-items: center; gap: 9px; padding: 6px 6px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.sysfilter-row:hover { background: #f5f7fa; }
.sysfilter-row input { width: 15px; height: 15px; accent-color: var(--route); flex: none; }
.sysfilter-row .sf-dot { width: 9px; height: 9px; border-radius: 3px; flex: none; }
.sysfilter-row .sf-name { flex: 1; }
.sysfilter-row .sf-type { font-size: 10px; color: var(--dim); text-transform: uppercase; letter-spacing: 0.4px; }
/* "System & plattformar"-rullgardin i Spåra-filterbaren — samma utseende som på startsidan, men flytande panel */
.fb-sysfilter { position: relative; }
.fb-sysfilter .sysfilter-panel { position: absolute; top: calc(100% + 6px); left: 0; margin-top: 0; z-index: 30; }
/* kryssrutorna ligger i .fb-field och får annars .fb-field input-stilar (min-width m.m.) som spräcker raden */
.fb-sysfilter .sysfilter-row input { min-width: 0; width: 15px; height: 15px; border: none; padding: 0; box-shadow: none; }
.fb-sysfilter .sysfilter-row input:focus { box-shadow: none; }
.tube-toggle { display: inline-flex; align-items: center; gap: 9px; margin-top: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 11px; box-shadow: var(--shadow); height: 38px; padding: 0 14px 0 8px; font-weight: 600; font-size: 13px; color: var(--ink); cursor: pointer; width: 100%; }
.tube-toggle .tube-knob { position: relative; width: 34px; height: 20px; border-radius: 999px; background: #d4d9e3; flex: none; transition: background .18s; }
.tube-toggle .tube-knob::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(16,24,40,0.25); transition: transform .18s; }
.tube-toggle[aria-checked="true"] { border-color: rgba(0,25,168,0.35); color: #0019A8; }
.tube-toggle[aria-checked="true"] .tube-knob { background: #0019A8; }
.tube-toggle[aria-checked="true"] .tube-knob::after { transform: translateX(14px); }

/* down-banner */
.down-banner { display: none; top: 16px; left: 50%; transform: translateX(-50%); align-items: center; gap: 10px; padding: 10px 18px; border-color: rgba(239, 68, 68, 0.4); color: #b91c1c; font-weight: 600; animation: pulse 1.4s infinite; }
.down-banner .tri { color: var(--warn); font-size: 18px; }

/* detaljpanel */
.detail { display: none; top: 16px; right: 16px; width: clamp(280px, 25vw, 332px); padding: 16px; pointer-events: auto; max-height: calc(100% - 32px); overflow: auto; }
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.detail-title { display: flex; align-items: center; gap: 8px; font-size: 17px; font-weight: 700; }
.vda { font-size: 9.5px; letter-spacing: 0.8px; color: var(--dim); background: var(--chip); padding: 2px 6px; border-radius: 5px; font-weight: 700; }
.detail-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag { font-size: 11px; font-weight: 600; color: #4b5563; background: var(--chip); padding: 3px 9px; border-radius: 999px; display: inline-flex; align-items: center; gap: 5px; }
.tag.status .dotg, .tag.status .dotr { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.dotg { background: var(--green); } .dotr { background: var(--red); }
.iconbtn { width: 30px; height: 30px; padding: 0; border-radius: 8px; color: var(--dim); font-size: 14px; }
.detail-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 16px 0; }
.detail-actions button { height: auto; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 4px; font-size: 11px; }
.detail-actions .ic { width: 36px; height: 36px; border-radius: 50%; background: var(--chip); display: flex; align-items: center; justify-content: center; font-size: 16px; color: var(--ink); }
.detail-actions button:hover .ic { background: #e7eaf0; }
.detail-stats { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--line-soft); border-radius: 12px; overflow: hidden; }
.detail-stats > div { padding: 12px 8px; text-align: center; border-right: 1px solid var(--line-soft); }
.detail-stats > div:last-child { border-right: none; }
.detail-stats b { display: block; font-size: 18px; font-weight: 700; }
.detail-stats span { font-size: 10.5px; color: var(--dim); text-transform: uppercase; letter-spacing: 0.4px; }
.detail-kv { margin-top: 14px; }
.detail-kv > div { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line-soft); font-size: 13px; }
.detail-kv > div:last-child { border-bottom: none; }
.detail-kv span { color: var(--dim); } .detail-kv b { font-weight: 600; }
.detail-section { margin: 16px 0 8px; font-size: 10.5px; font-weight: 700; letter-spacing: 1px; color: var(--label); }
.detail-related { display: flex; flex-direction: column; gap: 8px; }
.rel { display: flex; align-items: center; gap: 10px; padding: 9px 11px; border: 1px solid var(--line-soft); border-radius: 11px; font-size: 12.5px; }
.rel .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.rel .rel-name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rel .rel-dir { color: var(--dim); font-size: 11px; }
.rel .rel-num { font-family: ui-monospace, monospace; }

/* legend */
.legend { left: 16px; bottom: 56px; padding: 11px 14px; font-size: 12px; pointer-events: none; line-height: 1.5; }
.tube-roundel { display: none; left: 16px; bottom: 56px; background: none; border: none; box-shadow: none; padding: 0; pointer-events: none; }
.tube-roundel svg { display: block; height: 100px; width: auto; }
body.tube .tube-roundel { display: block; left: 210px; }
.legend hr { border: none; border-top: 1px solid var(--line-soft); margin: 7px 0; }
.legend-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--dim); margin-bottom: 5px; }
.legend-row { display: flex; align-items: center; gap: 9px; }
.sw { width: 20px; height: 5px; border-radius: 3px; display: inline-block; }
.sw.green { background: var(--green); } .sw.amber { background: var(--amber); } .sw.red { background: var(--red); }
.sw.white { background: #fff; border: 1px solid var(--line); }
.tri { color: var(--warn); }
.road { display: inline-block; width: 20px; border-radius: 2px; background: #c4ccd4; }
.road.motorvag { height: 8px; background: var(--green); } .road.landsvag { height: 5px; background: var(--green); }
.road.kommunal { height: 4px; background: #aeb6c2; } .road.skogsvag { height: 0; border-top: 2px dashed #aeb6c2; }

/* statusrad */
.statusbar { left: 16px; bottom: 14px; padding: 7px 14px; font-size: 11.5px; color: var(--dim); display: flex; align-items: center; gap: 8px; pointer-events: none; }
.statusbar b { color: var(--ink); } .statusbar .sep { color: var(--line); }
#sb-view { color: var(--route); font-weight: 700; }
#tab-admin { display: none; }
.sb-link { pointer-events: auto; cursor: inherit; }

/* transport */
.transport { display: none; left: 50%; bottom: 16px; transform: translateX(-50%); align-items: center; gap: 12px; padding: 11px 16px; width: min(700px, 80%); pointer-events: auto; }
body.playback .transport { display: flex; }
.transport button { min-width: 80px; } #t-speed { min-width: 52px; }
#t-time { font-family: ui-monospace, monospace; font-size: 14px; min-width: 64px; }
#t-scrub { flex: 1; accent-color: var(--route); }

/* zoom */
.zoomctl { right: 16px; bottom: 14px; display: flex; gap: 8px; padding: 9px; pointer-events: auto; }
.zoomctl button { min-width: 38px; font-size: 16px; } .zoomctl #zoom-reset { min-width: auto; font-size: 13px; }

.hint { right: 16px; top: 16px; font-size: 11.5px; pointer-events: none; background: none; border: none; box-shadow: none; color: var(--dim); }

/* ---------- Administration ---------- */
.admin { flex: 1; min-height: 0; overflow: auto; display: grid; grid-template-columns: minmax(320px, 460px) 1fr; gap: 18px; padding: 20px clamp(14px, 2vw, 32px); align-content: start; }
.admin-card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 18px 20px; }
.admin-card h2 { margin: 0 0 4px; font-size: 16px; }
.admin-card label { display: block; margin: 14px 0 5px; font-size: 11px; font-weight: 600; letter-spacing: 0.4px; text-transform: uppercase; color: var(--dim); }
.admin-card input { width: 100%; height: 36px; border: 1px solid var(--line); border-radius: 9px; padding: 0 10px; font-size: 14px; font-family: inherit; color: var(--ink); }
.admin-card input:focus { outline: none; border-color: var(--route); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12); }
.svc-row { display: flex; gap: 8px; margin-bottom: 8px; }
.svc-row input { flex: 1; }
.svc-row button { width: 36px; min-width: 36px; padding: 0; color: var(--dim); }
button.ghost { background: transparent; border: 1px dashed var(--line); color: var(--route); width: 100%; }
button.ghost:hover { background: rgba(79, 70, 229, 0.06); }
.admin-actions { display: flex; align-items: center; gap: 12px; margin-top: 16px; }
.admin-systems { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.sys-row { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border: 1px solid var(--line-soft); border-radius: 12px; }
.sys-dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.sys-info { flex: 1; min-width: 0; }
.sys-name { font-weight: 600; }
.sys-svc { font-size: 12px; color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sys-badge { font-size: 10px; font-weight: 700; letter-spacing: 0.5px; text-transform: uppercase; color: var(--dim); background: var(--chip); padding: 3px 8px; border-radius: 6px; }
.sys-row button.del { color: var(--red); border-color: rgba(239, 68, 68, 0.3); }
.sys-row button { white-space: nowrap; }
.admin-card select { width: 100%; height: 36px; border: 1px solid var(--line); border-radius: 9px; padding: 0 10px; font-size: 14px; font-family: inherit; color: var(--ink); background: #fff; }
.admin-card select:focus { outline: none; border-color: var(--route); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12); }
.sys-row.editing { flex-direction: column; align-items: stretch; }
.sys-edit { display: flex; flex-direction: column; gap: 6px; width: 100%; }
.sys-edit > label { margin: 2px 0; }
.sys-edit input, .sys-edit select { width: 100%; height: 34px; border: 1px solid var(--line); border-radius: 9px; padding: 0 10px; font-size: 13px; font-family: inherit; color: var(--ink); background: #fff; }
.sys-edit-comps { display: flex; flex-direction: column; gap: 6px; }
.sys-edit-comps .comp-row { display: flex; gap: 8px; }
.sys-edit-comps .comp-row input { flex: 1; }
.sys-edit-comps .comp-row button.del { width: 34px; min-width: 34px; flex: none; padding: 0; color: var(--red); border-color: rgba(239, 68, 68, 0.3); }
.comp-add { align-self: flex-start; height: 32px; padding: 0 12px; font-size: 12px; font-weight: 600; color: var(--route); border: 1px dashed rgba(79, 70, 229, 0.4); border-radius: 9px; background: rgba(79, 70, 229, 0.05); cursor: pointer; }
.comp-add:hover { background: rgba(79, 70, 229, 0.1); }
.sys-edit-actions { display: flex; gap: 8px; margin-top: 6px; }

/* ---------- Händelser ---------- */
.events { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(320px, 400px) 1fr; gap: 18px; padding: 18px clamp(14px, 2vw, 28px); }
.events-list, .events-detail { background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); overflow: auto; }
.events-head { padding: 14px 16px; font-weight: 700; font-size: 14px; border-bottom: 1px solid var(--line-soft); position: sticky; top: 0; background: var(--card); }
.ev { display: flex; align-items: center; gap: 11px; padding: 12px 16px; border-bottom: 1px solid var(--line-soft); cursor: pointer; }
.ev:hover { background: #f7f9fc; }
.ev.active { background: rgba(79, 70, 229, 0.07); }
.ev-sev { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.ev-main { flex: 1; min-width: 0; }
.ev-title { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ev-sub { font-size: 11.5px; color: var(--dim); }
.ev-time { font-family: ui-monospace, monospace; font-size: 11.5px; color: var(--dim); flex: none; }
.ev-empty { padding: 40px; color: var(--dim); text-align: center; }
.evd { padding: 20px 22px; }
.evd-head { display: flex; align-items: center; gap: 10px; }
.evd-title { font-size: 18px; font-weight: 700; }
.evd-tags { display: flex; gap: 6px; margin: 10px 0 18px; flex-wrap: wrap; }
.evd-kv > div { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line-soft); font-size: 13px; }
.evd-kv span { color: var(--dim); } .evd-kv b { font-weight: 600; font-family: ui-monospace, monospace; }
.evd-section { margin: 18px 0 10px; font-size: 10.5px; font-weight: 700; letter-spacing: 1px; color: var(--label); }
.flow { display: flex; flex-direction: column; gap: 0; }
.flow-step { display: flex; align-items: center; gap: 12px; }
.flow-node { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border: 1px solid var(--line-soft); border-radius: 11px; flex: 1; }
.flow-node.fail { border-color: rgba(239, 68, 68, 0.5); background: rgba(239, 68, 68, 0.05); }
.flow-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); flex: none; }
.flow-node.fail .flow-dot { background: var(--red); }
.flow-name { font-weight: 600; font-size: 13px; } .flow-ns { font-size: 11px; color: var(--dim); margin-left: auto; }
.flow-conn { height: 18px; width: 2px; background: var(--line); margin-left: 17px; }

@media (max-width: 1100px) { .hint { display: none; } .fb-field input, .fb-field select { min-width: 120px; } }
@media (max-width: 820px) { .legend { display: none; } .detail { width: 250px; } }
