:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.16);
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
}

.is-hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 500; }

/* ---------------------------------------------------------------- layout */
.app {
  display: grid;
  grid-template-columns: 380px minmax(0, 1fr);
  grid-template-rows: 56px minmax(0, 1fr);
  grid-template-areas: "top top" "side map";
  height: 100vh;
  height: 100dvh;
}

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 1200;
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.15);
  flex: 0 0 auto;
}
.brand-name { font-size: 16px; font-weight: 700; letter-spacing: -0.02em; }
.brand-name span { color: var(--accent); }

.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; }

.live {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted); white-space: nowrap;
}
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; }
.live--on .live-dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.15); }
.live--on { color: var(--ok); }
.live--off .live-dot { background: var(--bad); }

.icon-btn {
  border: 0; background: transparent; color: var(--ink);
  font-size: 18px; line-height: 1; padding: 8px; border-radius: 8px;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(17, 24, 39, 0.06); }
#sidebar-toggle { display: none; }

.sidebar {
  grid-area: side;
  overflow-y: auto;
  padding: 12px;
  border-right: 1px solid var(--line);
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
}

.map-wrap { grid-area: map; position: relative; }
.map { position: absolute; inset: 0; background: #dfe6ec; }

.map-overlay {
  position: absolute; left: 12px; bottom: 22px; z-index: 500;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 12px; font-size: 12px; color: var(--muted);
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
  max-width: calc(100% - 24px);
}

/* ---------------------------------------------------------------- panels */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}

.panel-head { display: flex; align-items: center; gap: 8px; }
.panel-title {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.panel-head .panel-title { margin-bottom: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sub-title {
  margin: 16px 0 8px;
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--muted);
}

.empty { color: var(--muted); font-size: 13px; line-height: 1.5; margin: 8px 0 4px; }

/* ------------------------------------------------------------ device list */
.device-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }

.device-item {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  grid-template-areas: "dot name right" ". meta right";
  gap: 2px 10px;
  align-items: center;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  background: #fafafa;
  text-align: left;
  width: 100%;
  font: inherit;
  color: inherit;
}
.device-item:hover { background: #f1f5f9; }
.device-item.is-selected { border-color: var(--accent); background: #eff6ff; }
.device-item.is-offline .device-name { color: var(--muted); }

.device-dot { grid-area: dot; width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; }
.device-item.is-online .device-dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.14); }
.device-name { grid-area: name; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-meta { grid-area: meta; font-size: 12px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; }
.device-right { grid-area: right; text-align: right; font-size: 12px; color: var(--muted); line-height: 1.35; }
.device-battery { font-weight: 600; color: var(--ink); }
.device-battery.is-low { color: var(--bad); }
.device-charging { color: var(--ok); }

/* --------------------------------------------------------------- details */
.stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 12px 0;
}
.stat {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  min-width: 0;
}
.stat--wide { grid-column: 1 / -1; }
.stat-label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.stat-value { display: block; font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.form-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.form-grid--two { grid-template-columns: 1fr 1fr; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field input {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  padding: 8px 9px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  width: 100%;
  min-width: 0;
}
.field input:focus { outline: 2px solid rgba(37, 99, 235, 0.25); border-color: var(--accent); }

/* --------------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: 8px;
  padding: 9px 14px; font: inherit; font-weight: 600; font-size: 13px;
  cursor: pointer; white-space: nowrap; background: #eef2f7; color: var(--ink);
}
.btn:hover { filter: brightness(0.97); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn--sm { padding: 7px 11px; font-size: 12px; }
.btn--primary { background: var(--accent); color: #fff; }
.btn--primary:hover { background: var(--accent-dark); filter: none; }
.btn--ghost { background: #fff; border-color: #d1d5db; color: #374151; }
.btn--ghost:hover { background: #f8fafc; filter: none; }
.btn--danger { background: #fef2f2; border-color: #fecaca; color: var(--bad); }
.btn--danger:hover { background: #fee2e2; filter: none; }
.btn--block { width: 100%; }
.btn-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.btn-row--tight { gap: 6px; margin-top: 6px; }

.history-info {
  margin-top: 10px;
  padding: 9px 10px;
  background: #f0f9ff;
  border: 1px solid #bae6fd;
  border-radius: 8px;
  font-size: 12px;
  line-height: 1.5;
  color: #075985;
}
.history-info strong { color: #0c4a6e; }

.fine-print { margin: 10px 0 0; font-size: 11.5px; color: var(--muted); line-height: 1.5; }

/* ---------------------------------------------------------------- marker */
.device-marker { background: transparent; border: 0; }
.device-marker__pin {
  width: 16px; height: 16px; border-radius: 50%;
  background: #94a3b8; border: 2.5px solid #fff;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.5);
}
.device-marker__pin.is-online { background: var(--ok); }
.device-marker__pin.is-selected { width: 22px; height: 22px; background: var(--accent); box-shadow: 0 0 0 5px rgba(37, 99, 235, 0.2), 0 1px 5px rgba(15, 23, 42, 0.45); }
.device-marker__label {
  position: absolute; left: 20px; top: -6px;
  font-size: 11px; font-weight: 600; color: #0f172a;
  background: rgba(255, 255, 255, 0.9);
  padding: 1px 5px; border-radius: 5px;
  white-space: nowrap;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.25);
}

.route-point { stroke: #fff; stroke-width: 1; }
.stop-marker { background: rgba(217, 119, 6, 0.9); border: 2px solid #fff; border-radius: 50%; }

/* ----------------------------------------------------------------- login */
.overlay {
  position: fixed; inset: 0; z-index: 3000;
  display: grid; place-items: center;
  background: rgba(15, 23, 42, 0.55);
  padding: 16px;
  backdrop-filter: blur(2px);
}

.dialog {
  background: var(--panel);
  border-radius: 14px;
  width: min(94vw, 460px);
  max-height: 88vh;
  overflow-y: auto;
  padding: 20px;
  box-shadow: var(--shadow);
}
.dialog--small { width: min(94vw, 360px); }
.dialog-head { display: flex; align-items: center; gap: 9px; margin-bottom: 6px; }
.dialog-head .icon-btn { margin-left: auto; }
.dialog-title { margin: 0; font-size: 18px; letter-spacing: -0.01em; }
.dialog-sub { margin: 0 0 14px; color: var(--muted); font-size: 13px; }
.dialog-body { font-size: 13px; line-height: 1.6; }
.dialog-body .field { margin-bottom: 10px; }
.dialog-body p { margin: 0 0 10px; }
.dialog-body code {
  display: block; word-break: break-all; background: #f1f5f9;
  border: 1px solid var(--line); border-radius: 8px; padding: 10px;
  font-size: 12.5px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  margin: 8px 0;
  user-select: all;
}
.form-error { color: var(--bad); font-size: 12.5px; margin: 0 0 10px; }

.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px;
  border-radius: 999px; font-size: 13px; z-index: 4000;
  box-shadow: var(--shadow); max-width: 90vw; text-align: center;
}

/* --------------------------------------------------------------- mobile */
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "map"; }
  #sidebar-toggle { display: inline-flex; }
  .brand-name { font-size: 15px; }
  .topbar { padding: 0 8px; gap: 4px; }
  .topbar-actions { gap: 6px; }
  #live-text { display: none; }
  .sidebar {
    position: fixed;
    top: 56px; left: 0; bottom: 0;
    width: min(94vw, 380px);
    z-index: 1100;
    border-right: 1px solid var(--line);
    box-shadow: 0 10px 40px rgba(15, 23, 42, 0.25);
    transform: translateX(-104%);
    transition: transform 0.22s ease;
    padding-bottom: 28px;
  }
  .sidebar.is-open { transform: translateX(0); }
  .btn { padding: 10px 14px; }
  .btn--sm { padding: 9px 11px; }
  .form-grid--two { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
