:root {
    color-scheme: light dark;

    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #dde2e8;
    --text: #18202b;
    --muted: #5d6877;
    --accent: #2563eb;
    --accent-text: #ffffff;
    --good: #16a34a;
    --warn: #c2410c;
    --danger: #dc2626;
    --tx: #dc2626;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1318;
        --surface: #171c23;
        --surface-2: #1f262f;
        --border: #2b333e;
        --text: #e6eaef;
        --muted: #95a0ae;
        --accent: #4f8cff;
        --accent-text: #0b1220;
        --good: #34d399;
        --warn: #fb923c;
        --danger: #f87171;
        --tx: #f87171;
        --shadow: none;
    }
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.hidden {
    display: none !important;
}

.muted {
    color: var(--muted);
}

code {
    font-family: ui-monospace, Consolas, monospace;
    font-size: 0.92em;
    background: var(--surface-2);
    padding: 1px 5px;
    border-radius: 4px;
}

button,
input,
select {
    font: inherit;
    color: inherit;
}

button {
    cursor: pointer;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    padding: 6px 12px;
}

button:hover {
    background: var(--surface-2);
}

button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
    font-weight: 600;
}

button.primary:hover {
    filter: brightness(1.08);
}

button.ghost {
    background: transparent;
}

button.danger {
    color: var(--danger);
}

button:disabled {
    opacity: 0.6;
    cursor: default;
}

input,
select {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 6px;
    padding: 6px 10px;
    min-width: 0;
}

input:focus,
select:focus,
button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 16px;
}

/* ---------- Login ---------- */

.login {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 16px;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 28px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.login-card h1 {
    margin: 12px 0 0;
    font-size: 20px;
}

.login-card p {
    margin: 0 0 6px;
}

.login-card label {
    font-weight: 600;
    font-size: 13px;
}

.login-card input {
    padding: 9px 12px;
}

.login-card button {
    margin-top: 6px;
    padding: 9px 12px;
}

.error {
    color: var(--danger);
    margin: 0;
}

/* ---------- Layout ---------- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.topbar .host {
    font-weight: 400;
    font-size: 13px;
    color: var(--muted);
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.live {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
}

.live i {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--good);
}

.live.stale i {
    background: var(--warn);
}

.tabs {
    display: flex;
    gap: 4px;
    padding: 0 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
}

.tabs button {
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    background: transparent;
    padding: 10px 12px;
    color: var(--muted);
    white-space: nowrap;
}

.tabs button.active {
    color: var(--text);
    border-bottom-color: var(--accent);
    font-weight: 600;
}

main {
    max-width: 1280px;
    margin: 0 auto;
    padding: 20px 24px 40px;
}

.banner {
    margin: 0 0 16px;
    padding: 10px 14px;
    border-radius: 8px;
    border: 1px solid var(--danger);
    color: var(--danger);
    background: var(--surface);
}

/* ---------- Stats ---------- */

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    box-shadow: var(--shadow);
}

.stat-label {
    font-size: 13px;
    color: var(--muted);
}

.stat-sub {
    font-size: 12px;
    color: var(--muted);
}

.stat-value {
    font-size: 26px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.stat.alert {
    border-color: var(--danger);
}

.stat.alert .stat-value {
    color: var(--danger);
}

/* ---------- Panels and tables ---------- */

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}

.panel h2 {
    margin: 0 0 12px;
    font-size: 15px;
}

.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 12px;
}

.panel-head h2 {
    margin: 0;
}

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    text-align: left;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

th {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

tbody tr:last-child td {
    border-bottom: none;
}

td.num {
    font-variant-numeric: tabular-nums;
}

td.wrap {
    white-space: normal;
}

.empty {
    color: var(--muted);
    padding: 8px 2px;
    margin: 0;
}

/* ---------- Badges, bars, meters ---------- */

.badge {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    background: var(--surface-2);
    color: var(--muted);
    margin-right: 4px;
}

.badge.good {
    color: var(--good);
}

.badge.tx,
.badge.danger {
    color: #fff;
    background: var(--tx);
}

.badge.warn {
    color: var(--warn);
}

.bars {
    display: inline-flex;
    align-items: flex-end;
    gap: 2px;
    height: 14px;
    vertical-align: middle;
}

.bars i {
    width: 4px;
    background: var(--border);
    border-radius: 1px;
}

.bars i:nth-child(1) { height: 5px; }
.bars i:nth-child(2) { height: 8px; }
.bars i:nth-child(3) { height: 11px; }
.bars i:nth-child(4) { height: 14px; }

.bars i.on {
    background: var(--good);
}

.meter {
    display: inline-block;
    width: 120px;
    height: 8px;
    border-radius: 999px;
    background: var(--surface-2);
    overflow: hidden;
    vertical-align: middle;
    margin-right: 8px;
}

.meter > span {
    display: block;
    height: 100%;
    background: var(--accent);
}

.meter.full > span {
    background: var(--danger);
}

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.actions {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 6px;
}

.actions button {
    padding: 4px 10px;
    font-size: 13px;
}

.zone {
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}

.zone:last-child {
    border-bottom: none;
}

.zone-name {
    font-weight: 600;
    margin-bottom: 6px;
}

@media (max-width: 640px) {
    .topbar,
    .tabs {
        padding-left: 16px;
        padding-right: 16px;
    }

    main {
        padding: 16px;
    }

    .topbar .host {
        display: none;
    }
}

/* ---------- Map ---------- */

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 12px;
    color: var(--muted);
}

.legend span {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.legend i {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.legend .lg-site { background: color-mix(in srgb, var(--accent) 25%, transparent); border: 1.5px solid var(--accent); }
.legend .lg-fringe { border: 1.5px dashed var(--accent); }
.legend .lg-unit { background: var(--text); }
.legend .lg-tx { background: var(--tx); }
.legend .lg-emer { background: var(--danger); box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 35%, transparent); }

.map {
    position: relative;
    height: 480px;
    cursor: grab;
    touch-action: none;
    user-select: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
    overflow: hidden;
}

.map svg {
    display: block;
    width: 100%;
    height: 100%;
}

.map .grid {
    stroke: var(--border);
    stroke-width: 1;
    vector-effect: non-scaling-stroke;
}

.map .site .coverage {
    fill: color-mix(in srgb, var(--accent) 10%, transparent);
    stroke: var(--accent);
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

.map .site .fringe {
    fill: none;
    stroke: var(--accent);
    stroke-width: 1;
    stroke-dasharray: 5 5;
    opacity: 0.6;
    vector-effect: non-scaling-stroke;
}

.map .site .tower {
    fill: var(--accent);
}

.map .site.siteTrunking .coverage,
.map .site.siteTrunking .fringe {
    stroke: var(--warn);
    fill: color-mix(in srgb, var(--warn) 10%, transparent);
}

.map .site.siteTrunking .fringe {
    fill: none;
}

.map .site.siteTrunking .tower {
    fill: var(--warn);
}

.map .site.offline .coverage,
.map .site.offline .fringe {
    stroke: var(--muted);
    fill: none;
    opacity: 0.5;
}

.map .site.offline .tower {
    fill: var(--danger);
}

.map .site-label {
    fill: var(--muted);
    font-weight: 600;
    paint-order: stroke;
    stroke: var(--surface-2);
    stroke-width: 3px;
    vector-effect: non-scaling-stroke;
}

.map.has-image .site-label {
    fill: #fff;
    stroke: rgba(0, 0, 0, 0.75);
}

.map.has-image .unit-label {
    fill: #fff;
    stroke: rgba(0, 0, 0, 0.75);
}

.map .unit .dot {
    fill: var(--text);
    stroke: var(--surface);
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

.map .unit.direct .dot {
    fill: var(--surface);
    stroke: var(--text);
    stroke-width: 2;
}

.map .unit.nosvc .dot {
    fill: var(--muted);
}

.map .unit.tx .dot {
    fill: var(--tx);
}

.map .unit.emer .dot {
    fill: var(--danger);
}

.map .unit .pulse {
    fill: var(--danger);
    opacity: 0.35;
    animation: map-pulse 1s ease-in-out infinite;
}

@keyframes map-pulse {
    50% { opacity: 0.05; }
}

.map .unit-label {
    fill: var(--text);
    paint-order: stroke;
    stroke: var(--surface-2);
    stroke-width: 3px;
    vector-effect: non-scaling-stroke;
}

.badge.info {
    color: var(--accent);
}

@media (max-width: 640px) {
    .map {
        height: 340px;
    }
}

.map.has-image {
    height: 680px;
}

.map.dragging {
    cursor: grabbing;
}

.map .backdrop {
    opacity: 0.9;
}

.map-coords,
.map-help {
    position: absolute;
    padding: 2px 8px;
    border-radius: 6px;
    font-size: 12px;
    color: var(--muted);
    background: color-mix(in srgb, var(--surface) 85%, transparent);
    pointer-events: none;
}

.map-coords {
    left: 8px;
    bottom: 8px;
    font-variant-numeric: tabular-nums;
}

.map-coords:empty {
    display: none;
}

.map-help {
    right: 8px;
    bottom: 8px;
}

@media (max-width: 640px) {
    .map.has-image {
        height: 420px;
    }

    .map-help {
        display: none;
    }
}

/* ---------- Site editor on the map ---------- */

.map-wrap {
    position: relative;
}

.map.placing {
    cursor: crosshair;
}

.map-hint {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
    padding: 6px 12px;
    border-radius: 6px;
    background: var(--accent);
    color: var(--accent-text);
    font-weight: 600;
    font-size: 13px;
    pointer-events: none;
}

.map .site.draft .coverage {
    fill: color-mix(in srgb, var(--good) 15%, transparent);
    stroke: var(--good);
    stroke-width: 2;
    stroke-dasharray: 8 4;
}

.map .site.draft .fringe {
    stroke: var(--good);
}

.map .site.draft .tower {
    fill: var(--good);
}

.map .site[data-site-id] {
    cursor: pointer;
}

.map .site[data-site-id]:hover .coverage {
    stroke-width: 2.5;
}

.site-editor {
    position: absolute;
    top: 10px;
    right: 10px;
    bottom: 10px;
    width: 320px;
    max-width: calc(100% - 20px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.site-editor h3 {
    margin: 0;
    font-size: 15px;
}

.site-editor p {
    margin: 0;
}

.site-editor .small {
    font-size: 12px;
}

.site-editor label {
    display: flex;
    flex-direction: column;
    gap: 3px;
    font-size: 12px;
    color: var(--muted);
}

.site-editor label input {
    color: var(--text);
    font-size: 14px;
}

.site-editor label.check {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--text);
}

.site-editor input[readonly] {
    background: var(--surface-2);
    color: var(--muted);
}

.site-editor input[type="range"] {
    padding: 0;
    border: none;
    background: transparent;
    accent-color: var(--accent);
}

.se-row {
    display: flex;
    gap: 8px;
}

.se-row label {
    flex: 1 1 0;
    min-width: 0;
}

.se-row label.grow {
    flex: 2 1 0;
}

.se-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-top: auto;
    padding-top: 8px;
}

.se-actions .spacer {
    flex: 1;
}

@media (max-width: 640px) {
    .site-editor {
        position: static;
        width: auto;
        max-width: none;
        margin-top: 10px;
        box-shadow: none;
    }
}

/* ---------- Dead spots ---------- */

.map-tools {
    display: flex;
    gap: 8px;
}

.map .dead-spot path {
    fill: color-mix(in srgb, var(--danger) 28%, transparent);
    stroke: var(--danger);
    stroke-width: 2;
    stroke-dasharray: 6 4;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
}

.map .dead-spot[data-spot-id] {
    cursor: pointer;
}

.map .dead-spot[data-spot-id]:hover path {
    fill: color-mix(in srgb, var(--danger) 40%, transparent);
}

.map .dead-spot.off path {
    fill: color-mix(in srgb, var(--muted) 15%, transparent);
    stroke: var(--muted);
}

.map .dead-spot.draft path {
    stroke-width: 2.5;
    stroke-dasharray: none;
}

.map .spot-label {
    fill: #fff;
    font-weight: 700;
    text-anchor: middle;
    paint-order: stroke;
    stroke: rgba(0, 0, 0, 0.75);
    stroke-width: 3px;
    vector-effect: non-scaling-stroke;
    pointer-events: none;
}

.map .drawing path {
    fill: none;
    stroke: var(--warn);
    stroke-width: 2.5;
    vector-effect: non-scaling-stroke;
}

.map .drawing circle {
    fill: var(--warn);
    stroke: #fff;
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

.map .drawing circle.first {
    fill: #fff;
    stroke: var(--warn);
    stroke-width: 3;
}

.spot-editor {
    bottom: auto;
}

/* ---------- Relays ---------- */

.map .relay .link {
    stroke: var(--good);
    stroke-width: 1.5;
    stroke-dasharray: 2 5;
    opacity: 0.8;
    vector-effect: non-scaling-stroke;
}

.map .relay .coverage {
    fill: color-mix(in srgb, var(--good) 14%, transparent);
    stroke: var(--good);
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

.map .relay .fringe {
    fill: none;
    stroke: var(--good);
    stroke-width: 1;
    stroke-dasharray: 4 4;
    opacity: 0.6;
    vector-effect: non-scaling-stroke;
}

.map .relay .marker {
    fill: var(--good);
    stroke: #fff;
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

.map .relay[data-relay-id] {
    cursor: pointer;
}

.map .relay.down .coverage,
.map .relay.down .fringe,
.map .relay.down .link {
    stroke: var(--muted);
    fill: none;
}

.map .relay.down .marker {
    fill: var(--muted);
}

.map .relay.draft .coverage {
    stroke-width: 2.5;
    stroke-dasharray: 8 4;
}

.map .relay-label {
    fill: #fff;
    font-weight: 600;
    text-anchor: middle;
    paint-order: stroke;
    stroke: rgba(0, 0, 0, 0.75);
    stroke-width: 3px;
    vector-effect: non-scaling-stroke;
    pointer-events: none;
}

.site-editor select {
    color: var(--text);
    font-size: 14px;
}

.legend .lg-relay { background: var(--good); border-radius: 1px; transform: rotate(45deg) scale(0.8); }
.legend .lg-dead { background: color-mix(in srgb, var(--danger) 30%, transparent); border: 1.5px dashed var(--danger); border-radius: 2px; }
