:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #e3e7ee;
    --text: #1d2433;
    --muted: #667085;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --accent-soft: #eaf1fe;
    --button: #2563eb;
    --button-hover: #1d4ed8;
    --on-button: #ffffff;
    --danger: #d92d20;
    --danger-soft: #fdecea;
    --star: #e5a000;
    --ok: #18794e;
    --ok-soft: #e7f6ec;
    --warn: #b54708;
    --warn-soft: #fff4e5;
    --row-hover: #fafbfc;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.06);
    color-scheme: light;
}

/* Dark theme: follows the browser/OS setting unless the user picked one with the toggle in
   the app bar (js/theme.js sets data-theme="light"/"dark" on <html>; no attribute = automatic).
   The same values twice, because one selector can't express "system dark or chosen dark". */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0e131d;
        --surface: #161d2a;
        --border: #2a3345;
        --text: #e4e8ef;
        --muted: #97a1b3;
        --accent: #6c9bff;
        --accent-hover: #8db2ff;
        --accent-soft: #1b2947;
        --button: #2f63e0;
        --button-hover: #3d72f0;
        --on-button: #ffffff;
        --danger: #f97066;
        --danger-soft: #3b1f1f;
        --star: #f5b82e;
        --ok: #47cd89;
        --ok-soft: #12311f;
        --warn: #fdb022;
        --warn-soft: #3a2b10;
        --row-hover: #1c2434;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
        color-scheme: dark;
    }
}
:root[data-theme="dark"] {
    --bg: #0e131d;
    --surface: #161d2a;
    --border: #2a3345;
    --text: #e4e8ef;
    --muted: #97a1b3;
    --accent: #6c9bff;
    --accent-hover: #8db2ff;
    --accent-soft: #1b2947;
    --button: #2f63e0;
    --button-hover: #3d72f0;
    --on-button: #ffffff;
    --danger: #f97066;
    --danger-soft: #3b1f1f;
    --star: #f5b82e;
    --ok: #47cd89;
    --ok-soft: #12311f;
    --warn: #fdb022;
    --warn-soft: #3a2b10;
    --row-hover: #1c2434;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
}

* { box-sizing: border-box; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 12px; }

.icon { width: 18px; height: 18px; flex-shrink: 0; vertical-align: -3px; }

/* ---------------------------------------------------------------- app bar */

.appbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    /* above Leaflet's map controls (z-index 1000) when scrolling past a map */
    z-index: 1100;
}
.appbar-inner {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 16px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand .icon { width: 22px; height: 22px; color: var(--accent); }
.appbar nav { display: flex; gap: 4px; }
.appbar-right { display: flex; align-items: center; gap: 4px; }
.theme-toggle.floating { position: fixed; top: 12px; right: 12px; }
.appbar nav a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--muted);
    font-weight: 500;
}
.appbar nav a:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.appbar nav a.active { background: var(--accent-soft); color: var(--accent); }
@media (max-width: 520px) {
    /* icons only in the bar on phones */
    .appbar nav a span { display: none; }
    .appbar nav a { padding: 8px; }
}

/* ---------------------------------------------------------------- layout */

.content {
    max-width: 640px;
    margin: 0 auto;
    padding: 24px 16px 48px;
}
/* pages with wide tables, e.g. the hit list on zone-edit.html */
.content.wide { max-width: 1000px; }

.page-title { font-size: 1.5rem; margin: 0 0 4px; }
.subtitle { color: var(--muted); margin: 0 0 20px; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
}
@media (max-width: 520px) {
    .content { padding: 16px 12px 40px; }
    .card { padding: 16px; }
}

/* heading row of a card: title left, actions right */
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.topbar h2 { margin: 0; }

/* login / register: centered narrow card */
.auth { max-width: 400px; margin: 0 auto; padding: 48px 16px; }
.auth .brand { justify-content: center; width: 100%; font-size: 1.5rem; margin-bottom: 8px; }
.auth .brand .icon { width: 28px; height: 28px; }
.auth .subtitle { text-align: center; }

/* ---------------------------------------------------------------- forms */

label { display: block; font-weight: 600; font-size: 0.9rem; margin-top: 14px; margin-bottom: 6px; }
input[type=text], input[type=email], input[type=password], input[type=number], select {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
input[readonly] { background: var(--bg); }
select.compact { width: auto; padding: 6px 10px; font-size: 0.9rem; }

button, .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 16px;
    padding: 9px 16px;
    background: var(--button);
    color: var(--on-button);
    border: 1px solid transparent;
    border-radius: 8px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}
button:hover, .btn:hover { background: var(--button-hover); text-decoration: none; }
button.secondary, .btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
button.secondary:hover, .btn.secondary:hover { background: var(--bg); }
button.danger, .btn.danger { background: var(--surface); color: var(--danger); border-color: var(--border); }
button.danger:hover, .btn.danger:hover { background: var(--danger-soft); }
.topbar .btn, .topbar button { margin-top: 0; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

/* round icon-only button for row actions; the label is in title/aria-label */
.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    margin: 0;
    padding: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    border: 1px solid transparent;
}
/* "button.icon-btn" etc. so these win over the generic button/.danger rules above */
a.icon-btn, button.icon-btn, button.icon-btn.danger { background: transparent; border-color: transparent; color: var(--muted); }
a.icon-btn:hover, button.icon-btn:hover { background: var(--bg); color: var(--text); text-decoration: none; }
button.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
a.icon-btn.starred { color: var(--star); }
.icon-btn.starred .icon { fill: var(--star); }
.row-actions { display: inline-flex; gap: 2px; align-items: center; }

.error { color: var(--danger); margin-top: 12px; }
.error-text { color: var(--danger); }
.notice { color: var(--accent); margin-top: 12px; }
.success-banner {
    display: flex; align-items: center; gap: 8px;
    margin: 0 0 16px; padding: 10px 12px; border-radius: 8px;
    color: var(--ok);
    background: var(--ok-soft);
    font-weight: 600;
}
small.hint, .hint { color: var(--muted); display: block; margin-top: 6px; font-size: 0.875rem; }
.muted { color: var(--muted); }

/* ---------------------------------------------------------------- tables */

table { width: 100%; border-collapse: collapse; }
th {
    text-align: left;
    padding: 8px 10px;
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
    border-bottom: 1px solid var(--border);
}
td { text-align: left; padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--row-hover); }
td.actions { text-align: right; white-space: nowrap; }
.table-scroll { overflow-x: auto; }
/* admin request statistics */
.stats-table th.num, .stats-table td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.stats-table tr.today td { background: var(--accent-soft); }
.stats-table tfoot td { border-top: 2px solid var(--border); border-bottom: none; }
.error-count { color: var(--danger); font-size: 0.85em; }

/* on phones, table.responsive rows become stacked cards: one "label  value" line per cell */
@media (max-width: 700px) {
    table.responsive thead { display: none; }
    table.responsive tr { display: block; padding: 12px 0; border-bottom: 1px solid var(--border); }
    table.responsive tbody tr:last-child { border-bottom: none; }
    table.responsive tbody tr:hover td { background: none; }
    table.responsive td { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border: none; text-align: right; }
    table.responsive td::before { content: attr(data-label); font-weight: 600; color: var(--muted); text-align: left; }
    table.responsive td.actions { display: flex; justify-content: flex-start; gap: 8px; padding-top: 8px; }
    table.responsive td.actions::before { content: none; }
    table.responsive td[colspan] { display: block; text-align: left; }
    table.responsive td[colspan]::before { content: none; }
    table.responsive td.actions select { flex: 1; }
}

/* ---------------------------------------------------------------- maps */

#mapid { width: 100%; height: 380px; border-radius: 10px; margin-top: 12px; border: 1px solid var(--border); }
@media (max-width: 520px) {
    #mapid { height: 320px; }
}

/* dark theme: darken the map tiles only (inverted, hue turned back), so the flight path,
   zone circle and markers keep their colors; plus Leaflet's own white controls */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9); }
    :root:not([data-theme="light"]) .leaflet-container { background: #1a1f29; }
}
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9); }
:root[data-theme="dark"] .leaflet-container { background: #1a1f29; }
/* ".leaflet-container" prefix: as specific as Leaflet's own rules, which set white backgrounds */
.leaflet-container .leaflet-bar a, .leaflet-container .leaflet-control-attribution,
.leaflet-container .leaflet-popup-content-wrapper, .leaflet-container .leaflet-popup-tip {
    background: var(--surface); color: var(--text);
}
.leaflet-container .leaflet-bar a { border-bottom-color: var(--border); }
.leaflet-container .leaflet-bar a:hover { background: var(--bg); }
.leaflet-container .leaflet-control-attribution a { color: var(--accent); }

/* helicopter marker on the flight path map (Leaflet divIcon); no default white box */
.heli-icon { background: none; border: none; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4)); }

/* altitude color legend below the flight path map */
.alt-legend { font-size: 0.8rem; color: var(--muted); margin-top: 10px; display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; }
.alt-legend span { display: inline-flex; align-items: center; gap: 4px; }
.alt-legend i { display: inline-block; width: 18px; height: 6px; border-radius: 3px; }

/* ---------------------------------------------------------------- misc */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    background: var(--bg);
    color: var(--muted);
}
.badge .icon { width: 14px; height: 14px; vertical-align: -2px; }
.icon.filled { fill: currentColor; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

/* key/value lines, e.g. the identifiers on machine.html */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 4px 0 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; }
