:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --hover: #eef2ff;
  --danger-bg: #fef2f2;
  --danger: #b91c1c;
  --success-bg: #ecfdf5;
  --success: #047857;
  --notice-bg: #fffbeb;
  --notice: #92400e;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 2px 8px rgb(0 0 0 / 0.04);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2f3a;
    --primary: #3b82f6;
    --hover: #1e2533;
    --danger-bg: #3a1414;
    --danger: #fca5a5;
    --success-bg: #0f2e22;
    --success: #6ee7b7;
    --notice-bg: #33270d;
    --notice: #fcd34d;
  }
}

* { box-sizing: border-box; }
/* The hidden attribute must win over component display rules (e.g. label { display: grid }). */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
a { color: var(--primary); }
h1, h2 { margin: 0 0 .5rem; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.hint { color: var(--muted); margin: 0 0 .75rem; font-size: .9em; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .6rem 1.25rem; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.05rem; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: .25rem; }
.topbar nav a { padding: .4rem .75rem; border-radius: 8px; color: var(--muted); text-decoration: none; }
.topbar nav a:hover { background: var(--hover); color: var(--text); }
.topbar nav a.active { background: var(--hover); color: var(--primary); font-weight: 600; }
.logout { margin-left: auto; display: flex; align-items: center; gap: .75rem; }

/* Layout */
.container { width: 100%; max-width: 1280px; margin: 0 auto; padding: 1.25rem; display: grid; gap: 1rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.25rem; }

/* Forms */
label { display: grid; gap: .25rem; font-weight: 500; font-size: .9em; color: var(--muted); }
input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: .45rem .6rem; min-height: 36px;
}
input:focus, select:focus, .btn:focus-visible, tr.clickable:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.btn {
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
  font: inherit; cursor: pointer; border-radius: 8px; padding: .45rem .9rem; min-height: 36px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--primary); filter: brightness(1.08); }
.btn-ghost { background: transparent; border-color: transparent; }
.alert { background: var(--danger-bg); color: var(--danger); padding: .6rem .8rem; border-radius: 8px; margin: .75rem 0 0; }

.filters { display: flex; flex-wrap: wrap; gap: .75rem 1rem; align-items: flex-end; }
.filter-actions { display: flex; flex-wrap: wrap; gap: .4rem; }

/* Table */
.table-head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
.table-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.data th, table.data td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.data th { font-size: .8em; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
table.data .num { text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--hover); }
td.empty { text-align: center; color: var(--muted); padding: 2rem; }
.pill { display: inline-block; padding: .05rem .5rem; border: 1px solid var(--border); border-radius: 999px; font-size: .8em; text-decoration: none; }
.pill:hover { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: .75rem; }

/* Login */
.login-wrap { flex: 1; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: .9rem; padding: 2rem; }
.login-card p { margin: 0; }

/* Map */
.map-layout { flex: 1; display: grid; grid-template-columns: 300px 1fr; min-height: 0; }
.map-panel { border-radius: 0; border-width: 0 1px 0 0; overflow-y: auto; box-shadow: none; }
.map { min-height: 400px; height: 100%; }
.stack { display: grid; gap: .75rem; }
.quick { display: flex; gap: .4rem; }
.quick .btn { flex: 1; }
.stats { display: grid; grid-template-columns: auto 1fr; gap: .3rem 1rem; margin: 1rem 0 0; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.legend { list-style: none; padding: 0; margin: 1.25rem 0 0; display: grid; gap: .35rem; color: var(--muted); font-size: .9em; }
.legend li { display: flex; align-items: center; gap: .6rem; }
.lg { display: inline-block; width: 26px; flex: none; text-align: center; }
.lg-line { height: 4px; background: #2563eb; border-radius: 2px; }
.lg-dash { height: 0; border-top: 3px dashed #f59e0b; }
.lg-dot { width: 10px; height: 10px; margin: 0 8px; border-radius: 50%; background: #2563eb; }
.lg-ghost { width: 8px; height: 8px; margin: 0 9px; border-radius: 50%; border: 1.5px solid #f59e0b; background: #fff; }
.lg-start { width: 14px; height: 14px; margin: 0 6px; border-radius: 50%; background: #16a34a; border: 2px solid #fff; }
.lg-star { color: #facc15; font-size: 20px; line-height: 1; text-shadow: 0 0 1px #854d0e; }
.star-marker { background: none; border: none; filter: drop-shadow(0 1px 2px rgb(0 0 0 / .45)); }
.popup { display: grid; gap: .15rem; }
.popup-links { margin-top: .25rem; }
.leaflet-container { font: inherit; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: .5rem 1rem; }
  .map-layout { grid-template-columns: 1fr; grid-template-rows: auto minmax(60vh, 1fr); }
  .map-panel { border-width: 0 0 1px 0; }
}

/* Admin interface */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); }
.tabs a { padding: .5rem 1rem; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.success { background: var(--success-bg); color: var(--success); padding: .6rem .8rem; border-radius: 8px; margin: 0; }
.notice { background: var(--notice-bg); color: var(--notice); padding: .6rem .8rem; border-radius: 8px; margin: .5rem 0; }
.card > .alert { margin: 0 0 .75rem; }
.container > .alert { margin: 0; }
.btn-small { min-height: 30px; padding: .25rem .65rem; font-size: .85em; }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover:not(:disabled) { background: var(--danger-bg); }
td.actions { display: flex; flex-wrap: wrap; gap: .35rem; align-items: flex-start; white-space: normal; }
form.inline { display: inline; margin: 0; }
details { display: inline-block; }
details > summary { list-style: none; display: inline-flex; align-items: center; }
details > summary::-webkit-details-marker { display: none; }
details[open] > summary { background: var(--hover); }
.inline-form { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .35rem; align-items: center; }
.inline-form input { min-height: 30px; padding: .25rem .5rem; }
label.check { display: inline-flex; align-items: center; gap: .35rem; font-weight: 400; color: var(--text); }
label.check input { min-height: 0; }
.role { display: inline-block; padding: .05rem .55rem; border-radius: 999px; font-size: .8em; font-weight: 600; }
.role-admin { background: var(--hover); color: var(--primary); }
.role-viewer, .role-active { background: var(--success-bg); color: var(--success); }
.role-revoked { background: var(--danger-bg); color: var(--danger); }
.key-row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: .75rem 0 1.25rem; }
.key { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 1rem; padding: .6rem .8rem; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; word-break: break-all; user-select: all; }
.snippet { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: .75rem 1rem; overflow-x: auto; font-size: .85em; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: .25rem 0; width: 100%; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }

/* Battery */
.battery-low { color: var(--danger); font-weight: 600; }
