:root {
  --bg: #f4f6f9;          /* page background */
  --bg-2: #ffffff;        /* panels / cards / bars */
  --bg-3: #eceff3;        /* hover / secondary surfaces */
  --border: #dde2e9;
  --text: #1c2530;
  --text-dim: #6b7785;
  --accent: #f2b200;      /* GPSKosova brand */
  --accent-text: #3d2f00; /* readable text on the golden accent */
  --accent-2: #2563eb;
  --green: #16a34a;
  --orange: #f59e0b;
  --red: #dc2626;
  --map-bg: #e6ebf0;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.04);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  display: flex;
  flex-direction: column;
}

/* ---- Topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 16px;
  height: 52px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  flex: none;
  z-index: 5;
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: 0.2px; }
.brand span { color: var(--text-dim); font-weight: 500; }
.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent; color: var(--text-dim);
  border: 1px solid transparent; border-radius: 7px;
  padding: 7px 14px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.tab:hover { color: var(--text); background: var(--bg-3); }
.tab.active { color: var(--accent-text); background: var(--accent); }
.status { margin-left: auto; display: flex; align-items: center; gap: 8px; color: var(--text-dim); font-size: 13px; }
.status .sep { opacity: 0.4; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot-on { background: var(--green); box-shadow: 0 0 8px rgba(22, 163, 74, 0.5); }
.dot-off { background: var(--red); }

.user-box { display: flex; align-items: center; gap: 10px; }
.tabs + .user-box { margin-left: auto; } /* right-align when no .status precedes it (admin console) */
.user-name { font-weight: 600; color: var(--text); }
.user-name::before { content: "👤"; margin-right: 5px; }
.btn-logout {
  background: var(--bg-3); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 6px 12px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.btn-logout:hover { border-color: var(--text-dim); }

/* ---- Layout ---- */
.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 300px; flex: none; background: var(--bg-2);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; font-weight: 700; font-size: 12px; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--text-dim); border-bottom: 1px solid var(--border);
}
.mini {
  background: var(--bg-3); border: 1px solid var(--border); color: var(--text-dim);
  border-radius: 6px; width: 26px; height: 26px; cursor: pointer; font-size: 14px;
}
.mini:hover { color: var(--text); }
.device-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.device-item {
  padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer;
  display: grid; grid-template-columns: 14px 1fr auto; gap: 8px; align-items: center;
}
.device-item:hover { background: var(--bg-3); }
.device-item.selected { background: var(--bg-3); box-shadow: inset 3px 0 0 var(--accent); }
.device-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-item .sub { grid-column: 2 / 4; color: var(--text-dim); font-size: 12px; display: flex; gap: 10px; }
.device-item .speed { color: var(--text); font-variant-numeric: tabular-nums; }
.statedot { width: 10px; height: 10px; border-radius: 50%; }
.state-moving { background: var(--green); }
.state-stopped { background: var(--orange); }
.state-nofix { background: var(--red); }
.state-offline { background: #aab4c0; }

.legend {
  display: flex; gap: 14px; padding: 10px 14px; border-top: 1px solid var(--border);
  color: var(--text-dim); font-size: 12px;
}
.legend i { display: inline-block; vertical-align: middle; margin-right: 3px; }

/* ---- Content / panes ---- */
.content { flex: 1; min-width: 0; display: flex; }
.pane { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.hidden { display: none !important; }
#map { flex: 1; min-height: 0; background: var(--map-bg); }

.controls {
  display: flex; align-items: end; gap: 12px; flex-wrap: wrap;
  padding: 10px 14px; background: var(--bg-2); border-bottom: 1px solid var(--border);
}
.controls label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.controls input, .controls select {
  background: var(--bg-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 8px; font-size: 13px; font-family: inherit;
}
.controls input:focus, .controls select:focus { outline: none; border-color: var(--accent-2); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.controls button {
  background: var(--bg-3); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 7px 14px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.controls button:hover { border-color: var(--text-dim); }
.controls button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.summary { color: var(--text-dim); font-size: 12px; margin-left: auto; }

/* ---- Tables ---- */
.table-wrap { overflow: auto; background: var(--bg-2); }
#trip-table-wrap { max-height: 38%; border-top: 1px solid var(--border); flex: none; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  position: sticky; top: 0; background: var(--bg-2); text-align: left;
  padding: 8px 10px; color: var(--text-dim); font-weight: 600; border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table td { padding: 6px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table tbody tr:hover { background: var(--bg-3); cursor: pointer; }
.data-table tbody tr.selected { background: var(--bg-3); box-shadow: inset 3px 0 0 var(--accent); }
.mono { font-family: var(--mono); font-size: 12px; }
.frame-cell { font-family: var(--mono); font-size: 12px; color: var(--text-dim); max-width: 520px; overflow: hidden; text-overflow: ellipsis; }
.ok { color: var(--green); }
.bad { color: var(--red); }
.muted { color: var(--text-dim); }

/* ---- Raw inspector ---- */
.raw-split { flex: 1; display: flex; min-height: 0; }
.raw-split .table-wrap { flex: 1; }
.raw-detail { width: 340px; flex: none; border-left: 1px solid var(--border); background: var(--bg-2); overflow: auto; padding: 14px; }
.raw-detail-empty { color: var(--text-dim); }
.raw-detail h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); }
.raw-detail .kv { display: grid; grid-template-columns: 90px 1fr; gap: 4px 8px; font-size: 13px; }
.raw-detail .kv span:first-child { color: var(--text-dim); }
.raw-frame {
  margin-top: 12px; font-family: var(--mono); font-size: 12px; word-break: break-all;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 10px; color: var(--text);
}

/* ---- Vehicle markers ---- */
.veh-wrap { background: none; border: none; }
.tri, .veh {
  width: 0; height: 0;
  border-left: 7px solid transparent; border-right: 7px solid transparent;
  border-bottom: 14px solid var(--green);
}
.veh { filter: drop-shadow(0 1px 2px rgba(0,0,0,0.45)); }
.tri.moving, .veh.moving { border-bottom-color: var(--green); }
.tri.stopped, .veh.stopped { border-bottom-color: var(--orange); }
.tri.nofix, .veh.nofix { border-bottom-color: var(--red); }

/* Leaflet popup theming */
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--bg-2); color: var(--text); box-shadow: var(--shadow); }
.leaflet-popup-content { margin: 10px 12px; font-size: 13px; }
.popup-title { font-weight: 700; margin-bottom: 6px; }
.popup-row { color: var(--text-dim); }
.popup-row b { color: var(--text); font-weight: 600; }

/* ---- Admin management pane ---- */
#pane-admins { overflow: auto; padding: 22px; align-items: flex-start; }
.admin-wrap { width: 100%; max-width: 920px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }
.card {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 20px;
}
.card h2 { margin: 0 0 4px; font-size: 16px; }
.card .card-sub { margin: 0 0 16px; color: var(--text-dim); font-size: 13px; }
.admin-form { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; color: var(--text-dim); font-weight: 600; }
.field input, .field select {
  background: var(--bg-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; font-size: 14px; font-family: inherit; min-width: 200px;
}
.field select {
  appearance: none; -webkit-appearance: none; cursor: pointer;
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%236b7785' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent-2); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.btn {
  border: 1px solid var(--border); background: var(--bg-3); color: var(--text);
  border-radius: 8px; padding: 9px 16px; cursor: pointer; font-weight: 600; font-size: 14px;
}
.btn:hover { border-color: var(--text-dim); }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-primary:hover { filter: brightness(0.96); }
.btn-danger { background: #fff; color: var(--red); border-color: #f3c9c9; }
.btn-danger:hover { background: #fdecec; }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 6px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table th { text-align: left; padding: 9px 10px; color: var(--text-dim); font-weight: 600; font-size: 12px; border-bottom: 1px solid var(--border); }
.admin-table td { padding: 10px; border-bottom: 1px solid var(--border); }
.admin-table tr:last-child td { border-bottom: none; }
.admin-actions { display: flex; gap: 8px; justify-content: flex-end; }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-on { background: #e7f6ec; color: var(--green); }
.badge-off { background: #fdeede; color: #b45309; }
.tag-you { font-size: 11px; color: var(--text-dim); font-weight: 600; }

/* ---- Alerts / toasts ---- */
.alert { border-radius: 8px; padding: 10px 14px; font-size: 13px; margin-bottom: 12px; }
.alert-error { background: #fdecec; color: #b42318; border: 1px solid #f3c9c9; }
.alert-ok { background: #e7f6ec; color: #157347; border: 1px solid #bfe6cd; }
.alert-info { background: #eaf1fe; color: #1d4ed8; border: 1px solid #cdddfb; }

/* ---- Boot / connection banner ---- */
.boot-error {
  position: fixed; left: 50%; top: 16px; transform: translateX(-50%);
  z-index: 1000; background: #fdecec; color: #b42318;
  border: 1px solid #f3c9c9; border-radius: 8px;
  padding: 10px 18px; font-size: 13px; font-weight: 600;
  box-shadow: 0 4px 14px rgba(16, 24, 40, 0.12);
}

/* ---- Login page ---- */
.auth-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100%; padding: 24px;
  background:
    radial-gradient(1100px 520px at 50% -10%, #fff7e0 0%, rgba(255, 247, 224, 0) 60%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--bg-2);
  border: 1px solid var(--border); border-radius: 16px;
  box-shadow: 0 10px 30px rgba(16, 24, 40, 0.08); padding: 30px 28px;
}
.auth-brand { font-size: 22px; font-weight: 800; text-align: center; }
.auth-brand span { color: var(--accent); }
.auth-sub { text-align: center; color: var(--text-dim); margin: 6px 0 22px; font-size: 14px; }
.auth-step-title { font-size: 16px; font-weight: 700; margin: 0 0 4px; }
.auth-step-desc { color: var(--text-dim); font-size: 13px; margin: 0 0 18px; line-height: 1.5; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form .field input { width: 100%; min-width: 0; }
.auth-form .btn-primary { width: 100%; padding: 11px; font-size: 15px; }
.code-input {
  letter-spacing: 0.5em; text-align: center; font-size: 22px !important;
  font-family: var(--mono); font-weight: 600;
}
.auth-qr {
  display: flex; justify-content: center; padding: 16px;
  background: #fff; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 14px;
}
.auth-qr svg, .auth-qr canvas { width: 200px; height: 200px; }
.auth-secret {
  text-align: center; font-family: var(--mono); font-size: 14px; font-weight: 600;
  background: var(--bg-3); border: 1px dashed var(--border); border-radius: 8px;
  padding: 10px; word-break: break-all; margin-bottom: 16px; color: var(--text);
}
.auth-secret-label { text-align: center; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.link-btn { background: none; border: none; color: var(--accent-2); cursor: pointer; font-size: 13px; font-weight: 600; padding: 4px; }
.link-btn:hover { text-decoration: underline; }
.auth-footer { margin-top: 16px; text-align: center; }
