:root {
  --bg: #0f1419;
  --panel: #1a2129;
  --panel-2: #222c37;
  --line: #2c3845;
  --fg: #e6edf3;
  --muted: #8b9aa8;
  --accent: #4aa3ff;
  --ok: #3fb950;
  --warn: #d8a629;
  --err: #f0506e;
  --chip: #2c3845;
  --radius: 8px;
  --tap: 42px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; white-space: nowrap; }
.status { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot-on { background: var(--ok); box-shadow: 0 0 6px var(--ok); }
.dot-off { background: var(--err); }
.iconbtn { background: none; border: none; font-size: 22px; cursor: pointer; padding: 4px 8px; }

.btn {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--fg);
  border-radius: var(--radius); padding: 8px 12px; cursor: pointer; min-height: var(--tap);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #07131f; font-weight: 600; }
.btn.danger { border-color: var(--err); color: var(--err); }
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.badge { font-size: 11px; padding: 2px 7px; border-radius: 99px; background: var(--chip); white-space: nowrap; }
.badge.ok { background: rgba(63,185,80,.18); color: var(--ok); }
.badge.warn { background: rgba(216,166,41,.18); color: var(--warn); }
.badge.err { background: rgba(240,80,110,.18); color: var(--err); }
.badge.accent { background: rgba(74,163,255,.18); color: var(--accent); }

/* ---------- layout ---------- */
main { padding: 12px; max-width: 1300px; margin: 0 auto; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

.toolbar {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px;
}
.toolbar input[type=search] {
  flex: 1; min-width: 160px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 9px 12px; min-height: var(--tap);
}
.chip {
  background: var(--chip); border: 1px solid var(--line); color: var(--muted);
  padding: 7px 11px; border-radius: 99px; cursor: pointer; font-size: 13px;
}
.chip.active { background: rgba(74,163,255,.16); color: var(--accent); border-color: var(--accent); }

/* ---------- fleet table ---------- */
.tablewrap {
  overflow: auto;                      /* own scroll box (both axes) so the sticky header works */
  max-height: calc(100vh - 150px);     /* leaves room for topbar + toolbar */
  border: 1px solid var(--line); border-radius: var(--radius);
}
table.fleet { width: 100%; border-collapse: collapse; }
table.fleet th, table.fleet td { padding: 9px 10px; text-align: left; white-space: nowrap; }
table.fleet thead th {
  position: sticky; top: 0;            /* stick to the top of .tablewrap, not the viewport */
  background: var(--panel-2); border-bottom: 1px solid var(--line);
  cursor: pointer; user-select: none; font-size: 12px; color: var(--muted); z-index: 1;
}
table.fleet thead th .arrow { font-size: 10px; }
table.fleet tbody tr { border-bottom: 1px solid var(--line); cursor: pointer; }
table.fleet tbody tr:hover { background: var(--panel); }
.dev-label { font-weight: 600; }
.dev-imei { font-size: 11px; color: var(--muted); font-family: ui-monospace, monospace; }

.bar { display: inline-block; height: 6px; border-radius: 3px; background: var(--panel-2); width: 46px; vertical-align: middle; overflow: hidden; }
.bar > span { display: block; height: 100%; }

.statusdot { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- responsive: table -> cards ---------- */
@media (max-width: 720px) {
  table.fleet thead { display: none; }
  table.fleet, table.fleet tbody, table.fleet tr, table.fleet td { display: block; width: 100%; }
  table.fleet tr {
    border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; padding: 6px 4px;
    background: var(--panel);
  }
  table.fleet td {
    display: flex; justify-content: space-between; gap: 12px; white-space: normal;
    border-bottom: 1px dashed var(--line);
  }
  table.fleet td:last-child { border-bottom: none; }
  table.fleet td::before {
    content: attr(data-label); color: var(--muted); font-size: 12px; flex: 0 0 42%;
  }
  .tablewrap { border: none; overflow: visible; max-height: none; }
}

/* ---------- detail ---------- */
.detail-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 12px; }
.detail-head h2 { margin: 0; font-size: 18px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.card h3 { margin: 0 0 10px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.kv { display: grid; grid-template-columns: minmax(110px, 40%) 1fr; gap: 4px 10px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.kv dd.mono { font-family: ui-monospace, monospace; font-size: 12px; }

.field { margin-bottom: 10px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.field .row { display: flex; align-items: center; gap: 8px; }
.field input[type=number], .field input[type=text] {
  width: 100%; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 6px; padding: 8px 10px; min-height: var(--tap);
}
.field .unit { color: var(--muted); font-size: 12px; min-width: 34px; }
.field .hint { font-size: 11px; color: var(--muted); margin-top: 2px; }
.field .hint.clamp { color: var(--warn); }
.field input.changed { border-color: var(--accent); }

.cmdgrid { display: flex; flex-wrap: wrap; gap: 8px; }
.bizrow { display: flex; gap: 6px; margin-bottom: 6px; }
.bizrow input { flex: 1; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; }

.log { max-height: 360px; overflow-y: auto; font-size: 12px; }
.log .item { padding: 6px 0; border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: baseline; }
.log .item .t { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; white-space: nowrap; }
.log .item .d { flex: 1; }
.raw { white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, monospace; font-size: 11px; color: var(--muted); max-height: 260px; overflow: auto; }

/* ---------- toast + modal ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 16px; z-index: 50; max-width: 90vw; box-shadow: 0 6px 24px rgba(0,0,0,.4);
}
.toast.ok { border-color: var(--ok); }
.toast.err { border-color: var(--err); }

.modal-wrap {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 18px; width: 100%; max-width: 440px; }
.modal h3 { margin-top: 0; }
.modal .field { margin-bottom: 14px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
