/* Mobile first: bottom tab bar on phones, sidebar from 900px wide. */
:root {
  --bg: #f3f4f6; --card: #ffffff; --text: #111827; --muted: #6b7280; --line: #e5e7eb;
  --accent: #2563eb; --accent-text: #ffffff; --danger: #dc2626; --ok: #16a34a; --warn: #d97706;
  --nav: #1f2937; --nav-text: #d1d5db; --nav-active: #ffffff;
  --tabbar: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a; --card: #1e293b; --text: #e5e7eb; --muted: #94a3b8; --line: #334155;
    --accent: #3b82f6; --nav: #020617; --nav-text: #94a3b8;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
}
[hidden] { display: none !important; }
h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.25rem; margin: 0; }
h3 { font-size: 1.05rem; margin: 1.25rem 0 .5rem; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: .5rem 0 0; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85em; word-break: break-all; }

/* controls: large touch targets */
button, select, input {
  font: inherit; color: inherit; min-height: 44px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); padding: 0 14px;
}
input, select { width: 100%; }
button { cursor: pointer; white-space: nowrap; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.small { min-height: 36px; padding: 0 10px; font-size: .9em; }
label { display: block; font-size: .85em; color: var(--muted); }
label input, label select { margin-top: 4px; color: var(--text); font-size: 15px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }
.row select { width: auto; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 360px; display: grid; gap: 12px; }

/* layout */
.app { height: 100%; }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000;
  height: calc(var(--tabbar) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  display: flex; background: var(--nav);
}
.tabs .brand { display: none; }
.tabs button {
  flex: 1; border: 0; border-radius: 0; background: none; color: var(--nav-text);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 1.15rem; padding: 0;
}
.tabs button span { font-size: .7rem; }
.tabs button.active { color: var(--nav-active); box-shadow: inset 0 3px 0 var(--accent); }
main { height: 100%; padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom)); }
.tab { display: none; height: 100%; }
.tab.active { display: block; }
.page { overflow-y: auto; padding: 16px 16px 32px; }
.page > h2 { margin-bottom: 12px; }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }

/* map */
#tab-map { position: relative; }
#map { height: 100%; }
.map-overlay {
  position: absolute; top: 10px; right: 10px; z-index: 500; max-width: 60%;
  background: var(--card); border-radius: 10px; padding: 6px 10px; font-size: .85em;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .3);
}
.map-overlay:empty { display: none; }
.map-overlay div { cursor: pointer; padding: 3px 0; }
.dev-label {
  background: var(--card); color: var(--text); border: 0; border-radius: 6px; padding: 1px 6px;
  font-size: 12px; font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .4); white-space: nowrap;
}
.leaflet-popup-content { font-size: 13px; }
#data-map { height: 45vh; min-height: 240px; border-radius: 12px; margin: 8px 0 12px; }

/* cards */
.cards { display: grid; gap: 12px; }
.card { background: var(--card); border-radius: 14px; padding: 14px; box-shadow: 0 1px 2px rgba(0, 0, 0, .08); }
.card .title { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.card .title strong { font-size: 1.05rem; }
.card .meta { color: var(--muted); font-size: .9em; margin: 6px 0 10px; }
.card .actions { display: flex; flex-wrap: wrap; gap: 6px; }
.stat .value { font-size: 1.5rem; font-weight: 600; }
.stat .label { color: var(--muted); font-size: .85em; }
.badge { font-size: .75em; padding: 2px 8px; border-radius: 999px; background: var(--line); }
.badge.ok { background: var(--ok); color: #fff; }
.badge.off { background: var(--danger); color: #fff; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.dot.live { background: var(--ok); }
.dot.recent { background: var(--warn); }
.filters { display: grid; gap: 10px; }

/* tables */
.table-wrap { overflow-x: auto; background: var(--card); border-radius: 12px; }
table { border-collapse: collapse; width: 100%; font-size: .85em; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--card); }
table:empty { display: none; }

/* dialog */
dialog {
  border: 0; border-radius: 16px; padding: 18px; width: min(520px, calc(100% - 24px));
  background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog h3 { margin-top: 0; }
#dialog-body { display: grid; gap: 12px; margin-bottom: 16px; }
.copy-box { background: var(--bg); border-radius: 8px; padding: 10px; }

/* desktop */
@media (min-width: 900px) {
  .tabs {
    top: 0; bottom: 0; right: auto; width: 200px; height: auto; padding: 16px 0;
    flex-direction: column; gap: 4px;
  }
  .tabs .brand { display: block; color: #fff; font-weight: 700; padding: 0 18px 16px; }
  .tabs button {
    flex: 0 0 auto; flex-direction: row; justify-content: flex-start; gap: 10px;
    min-height: 44px; padding: 0 18px; font-size: 1rem;
  }
  .tabs button span { font-size: .95rem; }
  .tabs button.active { box-shadow: inset 3px 0 0 var(--accent); background: rgba(255, 255, 255, .06); }
  .tabs .logout { margin-top: auto; }
  main { padding-bottom: 0; margin-left: 200px; }
  .page { padding: 24px 32px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
  #status-cards { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  .filters { grid-template-columns: 1fr 1fr 1fr; align-items: end; }
  .filters .row { grid-column: 1 / -1; }
  #data-map { height: 55vh; }
}
