/* Default theme: Slate (dark). Switched via data-theme on <html>. */
:root {
    --bg: #0f172a;
    --panel: #1e293b;
    --line: #334155;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --accent: #38bdf8;
    --btn-text: #04293f;
    --input-bg: #0b1220;
    --ok: #22c55e;
    --warn: #f59e0b;
    --bad: #ef4444;
}

:root[data-theme="light"] {
    --bg: #f1f5f9;
    --panel: #ffffff;
    --line: #e2e8f0;
    --text: #0f172a;
    --muted: #64748b;
    --accent: #0284c7;
    --btn-text: #ffffff;
    --input-bg: #ffffff;
    --ok: #16a34a;
    --warn: #d97706;
    --bad: #dc2626;
}

:root[data-theme="dracula"] {
    --bg: #282a36;
    --panel: #343746;
    --line: #44475a;
    --text: #f8f8f2;
    --muted: #6272a4;
    --accent: #bd93f9;
    --btn-text: #282a36;
    --input-bg: #21222c;
    --ok: #50fa7b;
    --warn: #f1fa8c;
    --bad: #ff5555;
}

:root[data-theme="nord"] {
    --bg: #2e3440;
    --panel: #3b4252;
    --line: #4c566a;
    --text: #eceff4;
    --muted: #8e99ab;
    --accent: #88c0d0;
    --btn-text: #2e3440;
    --input-bg: #272c36;
    --ok: #a3be8c;
    --warn: #ebcb8b;
    --bad: #bf616a;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

header {
    position: relative;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    padding: 14px 24px;
    display: flex;
    align-items: center;
    gap: 16px;
}

/* Current route pick, centered in the header (shared across map/analysis). */
.header-sel {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    max-width: 40vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
    font-size: 14px;
    font-weight: 600;
    pointer-events: none;
}
.header-sel strong { color: var(--text); }
@media (max-width: 760px) { .header-sel { display: none; } }

header h1 { font-size: 18px; margin: 0; }
.version { font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 4px; }
header a { color: var(--accent); text-decoration: none; }
/* Page tabs: aligned to the header's bottom edge, active tab underlined in accent. */
header nav.tabs { display: flex; gap: 4px; align-self: stretch; align-items: stretch; margin-bottom: -14px; }
header .header-right { margin-left: auto; display: flex; gap: 16px; align-items: center; }
header .header-right select { width: auto; padding: 5px 8px; font-size: 13px; }

.tab {
    display: flex;
    align-items: center;
    padding: 0 16px;
    color: var(--muted);
    font-weight: 600;
    font-size: 14px;
    border-bottom: 3px solid transparent;
}
.tab:hover { color: var(--text); border-bottom-color: var(--line); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }

h2 { border-bottom: 1px solid var(--line); padding-bottom: 6px; }

a { color: var(--accent); }

.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 18px;
    margin-bottom: 20px;
}

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; }
tr:hover td { background: rgba(56, 189, 248, 0.06); }

.btn {
    display: inline-block;
    background: var(--accent);
    color: var(--btn-text);
    border: none;
    border-radius: 7px;
    padding: 8px 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    font-size: 14px;
}
.btn.secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.btn.danger { background: var(--bad); color: #fff; }
.btn:disabled, .btn[disabled] { opacity: .45; cursor: not-allowed; }

form.inline { display: inline; }

input, textarea, select {
    width: 100%;
    background: var(--input-bg);
    border: 1px solid var(--line);
    border-radius: 7px;
    color: var(--text);
    padding: 9px 11px;
    font-size: 14px;
}
label { display: block; margin: 12px 0 4px; color: var(--muted); font-size: 13px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--input-bg); border: 1px solid var(--line); border-radius: 8px; padding: 12px; }
.stat .n { font-size: 22px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }

.badge { padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.SUCCESS { background: rgba(34,197,94,.15); color: var(--ok); }
.badge.FAILED { background: rgba(239,68,68,.15); color: var(--bad); }
.badge.RUNNING { background: rgba(245,158,11,.15); color: var(--warn); }

.flash-ok { border-left: 4px solid var(--ok); }
.flash-err { border-left: 4px solid var(--bad); }

.muted { color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

/* Vehicle-positions fetch control: button + rate dropdown + polling spinner. */
.vp-control { display: inline-flex; align-items: center; gap: 8px; }
.vp-control select { width: auto; padding: 6px 8px; font-size: 13px; }
.spinner {
    display: inline-block; width: 15px; height: 15px;
    border: 2px solid var(--line); border-top-color: var(--accent);
    border-radius: 50%; animation: spin .7s linear infinite;
}
.spinner[hidden] { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.pager { display: flex; gap: 12px; align-items: center; margin-top: 16px; }

/* ---- TSP additions ------------------------------------------------------- */

/* Engine pill in the header: the one always-visible answer to "is priority running?". */
.engine-pill {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--input-bg);
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}
.engine-pill .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.engine-pill.on .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(34, 197, 94, .2); }
.engine-pill.off .dot { background: var(--muted); }
.engine-pill.on { color: var(--ok); border-color: var(--ok); }
.engine-pill.off { color: var(--muted); }
@media (max-width: 900px) { .engine-pill { display: none; } }

/* Request lifecycle badges, reusing the .badge shape. */
/* Raised, not yet ruled on — ordinarily under a second, so it is deliberately quiet. */
.badge.REQUESTED { background: rgba(148,163,184,.18); color: var(--muted); }
.badge.PENDING { background: rgba(245,158,11,.15); color: var(--warn); }
.badge.GRANTED { background: rgba(34,197,94,.15); color: var(--ok); }
.badge.SERVED { background: rgba(56,189,248,.15); color: var(--accent); }
.badge.DENIED { background: rgba(239,68,68,.15); color: var(--bad); }
.badge.CANCELLED { background: rgba(148,163,184,.15); color: var(--muted); }
.badge.EXPIRED { background: rgba(148,163,184,.15); color: var(--muted); }
.badge.AUTO { background: rgba(148,163,184,.15); color: var(--muted); }
.badge.MANUAL { background: rgba(168,85,247,.18); color: #c084fc; }

/* Signal state key, shared by the map legend and the admin page. */
.state-key { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--muted); }
.state-key span { display: flex; align-items: center; gap: 6px; }
.state-key i { width: 11px; height: 11px; border-radius: 50%; display: inline-block; }

/* Admin: side-by-side control cards that collapse on narrow screens. */
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; }
.cols > .card { margin-bottom: 0; }

/* Compact two-column form grid for the policy editor. */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 16px; }
.form-grid label { margin-top: 10px; }
.check-row { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.check-row input { width: auto; }
.check-row label { margin: 0; color: var(--text); font-size: 14px; }

/* A run of key/value facts (feed status, policy summary). */
.facts { font-size: 13px; }
.facts div { display: flex; gap: 10px; padding: 3px 0; }
.facts div span:first-child { color: var(--muted); min-width: 150px; }

/* Collapsible card: a panel that opens from its own heading, no script involved.
   The toggle's label lives in CSS so the markup needs no state of its own. */
.panel > summary {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 12px;
    cursor: pointer;
    list-style: none;
}
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary::marker { content: ""; }
.panel > summary h3 { margin: 0; white-space: nowrap; }
.panel > summary:hover h3 { color: var(--accent); }
/* The counts drop to their own line, so a narrow card never wraps its heading. */
.panel-sub { flex-basis: 100%; order: 3; color: var(--muted); font-size: 13px; }
.panel-toggle { margin-left: auto; padding: 4px 12px; font-size: 12px; white-space: nowrap; }
.panel-toggle::after { content: "Expand"; }
.panel[open] .panel-toggle::after { content: "Collapse"; }

/* Cards in a row keep their own height, so a collapsed one is not stretched to
   match a tall neighbour. */
.cols { align-items: start; }
