:root {
  --bg: #0d131a;
  --bg2: #111a23;
  --panel: #161f2a;
  --panel2: #1b2633;
  --line: #263444;
  --text: #e8eef6;
  --muted: #8ea0b5;
  --accent: #2f80ed;
  --accent2: #56a0ff;
  --green: #2fbf71;
  --amber: #f2b84b;
  --red: #eb5757;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, sans-serif;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 20% 0%, rgba(47,128,237,.18), transparent 60%),
    radial-gradient(700px 500px at 100% 100%, rgba(47,191,113,.10), transparent 60%),
    var(--bg);
}
.login-card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px;
  box-shadow: 0 30px 80px rgba(0,0,0,.35);
}
.login-card h1 { margin: 14px 0 4px; font-size: 24px; }
.login-card label { display: block; margin: 16px 0 0; color: var(--muted); font-size: 13px; }
.login-card input {
  width: 100%;
  margin-top: 6px;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg2);
  color: var(--text);
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card .primary { width: 100%; margin-top: 20px; }

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 14px;
  background: linear-gradient(145deg, var(--accent), #1b5fbf);
  color: white;
  font-weight: 800;
  letter-spacing: 1px;
}
.brand-mark.small { width: 34px; height: 34px; border-radius: 9px; font-size: 12px; }

.primary, .ghost, .danger {
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 9px 13px;
  color: var(--text);
  background: var(--panel2);
  transition: .15s ease;
}
.primary { background: var(--accent); border-color: var(--accent); }
.primary:hover { background: var(--accent2); }
.ghost { border-color: var(--line); background: transparent; color: var(--muted); }
.ghost:hover { color: var(--text); border-color: #3a4d63; }
.danger { background: rgba(235,87,87,.12); border-color: rgba(235,87,87,.45); color: #ff9a9a; }
.danger:hover { background: rgba(235,87,87,.22); }
button:disabled { opacity: .45; cursor: not-allowed; }

.error { color: #ff9a9a; margin-top: 12px; font-size: 13px; }

.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  height: 58px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 18px;
  background: var(--bg2);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.stats { display: flex; gap: 8px; flex: 1; flex-wrap: wrap; }
.stat {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 999px;
  padding: 4px 10px;
  color: var(--muted);
  font-size: 12px;
}
.stat b { color: var(--text); font-weight: 600; }
.top-actions { display: flex; align-items: center; gap: 10px; }

.layout { flex: 1; min-height: 0; display: flex; }
.sidebar {
  width: 340px;
  flex: 0 0 340px;
  overflow: auto;
  padding: 16px;
  background: var(--bg2);
  border-right: 1px solid var(--line);
}
.sidebar section { margin-bottom: 22px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.section-head h2 { margin: 0; font-size: 14px; font-weight: 700; letter-spacing: .2px; }
.list { display: flex; flex-direction: column; gap: 8px; }

.row {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  padding: 10px 11px;
  cursor: pointer;
  transition: .15s ease;
}
.row:hover { border-color: #3a4d63; background: var(--panel2); }
.row.selected { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(47,128,237,.35) inset; }
.row-top { display: flex; align-items: center; gap: 8px; }
.row-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: flex; gap: 10px; color: var(--muted); font-size: 12px; margin-top: 4px; flex-wrap: wrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: 0 0 auto; }
.dot.online { background: var(--green); box-shadow: 0 0 0 3px rgba(47,191,113,.15); }
.dot.offline { background: #5b6b7e; }
.row-actions { display: flex; gap: 6px; margin-top: 8px; }
.row-actions button { padding: 5px 8px; font-size: 12px; }
.empty { color: var(--muted); font-size: 13px; padding: 8px 2px; }
.app-info a { color: var(--accent2); text-decoration: none; }
.app-info a:hover { text-decoration: underline; }

.map-wrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; background: #0b1016; }
.leaflet-container { background: #0b1016; }
.leaflet-control-attribution { background: rgba(13,19,26,.78) !important; color: #9fb0c4 !important; }
.leaflet-control-attribution a { color: #7fb3ff !important; }

.track-controls {
  position: absolute;
  z-index: 500;
  top: 12px;
  left: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(17,26,35,.94);
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 30px rgba(0,0,0,.3);
  flex-wrap: wrap;
  max-width: calc(100% - 24px);
}
.track-controls input[type="date"] {
  background: var(--bg2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
}

.device-panel {
  position: absolute;
  z-index: 520;
  top: 64px;
  right: 12px;
  width: 360px;
  max-width: calc(100% - 24px);
  max-height: calc(100% - 84px);
  overflow: auto;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(22,31,42,.97);
  backdrop-filter: blur(10px);
  box-shadow: 0 18px 50px rgba(0,0,0,.42);
}
.meta { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.meta b { color: var(--text); font-weight: 600; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.form-grid label { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; }
.form-grid label.checkbox { flex-direction: row; align-items: center; gap: 8px; grid-column: span 2; }
.form-grid input, .form-grid select {
  width: 100%;
  padding: 8px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg2);
  color: var(--text);
}
.form-grid input[type="checkbox"] { width: auto; }
.device-panel .primary { width: 100%; }
.actions { display: flex; gap: 7px; flex-wrap: wrap; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(4,8,12,.66);
  display: grid;
  place-items: center;
  padding: 20px;
}
.modal-card {
  width: min(460px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  box-shadow: 0 30px 90px rgba(0,0,0,.55);
}
.modal-card label { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; color: var(--muted); font-size: 13px; }
.modal-card input, .modal-card textarea {
  width: 100%;
  padding: 10px 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg2);
  color: var(--text);
}
.modal-card textarea { min-height: 74px; resize: vertical; }
.code-big {
  font: 700 30px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 6px;
  text-align: center;
  padding: 18px;
  margin: 12px 0;
  border: 1px dashed #3c5570;
  border-radius: 12px;
  background: var(--bg2);
  color: #cfe4ff;
}
.code-note { color: var(--muted); font-size: 12px; text-align: center; }

.toast {
  position: fixed;
  z-index: 1200;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(520px, calc(100% - 32px));
  padding: 11px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  box-shadow: 0 14px 40px rgba(0,0,0,.4);
}
.toast.error { border-color: rgba(235,87,87,.55); color: #ffb1b1; }

@media (max-width: 860px) {
  .topbar { height: auto; padding: 10px 12px; flex-wrap: wrap; }
  .stats { order: 3; flex-basis: 100%; }
  .layout { flex-direction: column; }
  .sidebar { width: 100%; flex: 0 0 auto; max-height: 42vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .map-wrap { min-height: 58vh; flex: 1 0 58vh; }
  .device-panel { top: 70px; right: 8px; width: calc(100% - 16px); max-height: 62vh; }
  .track-controls { left: 8px; right: 8px; top: 8px; }
}
