/* SmartGoTrip IoT — admin UI. Không build, không framework. */
:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --border: #e3e7eb;
  --text: #16202a;
  --muted: #5d6b78;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #e6f4f2;
  --danger: #c2372b;
  --danger-soft: #fdecea;
  --success: #1f8a4c;
  --success-soft: #e7f5ec;
  --warn: #b7791f;
  --moving: #16a34a;
  --stopped: #2563eb;
  --lost: #8a949e;
  --alarm: #dc2626;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.08);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #171d24;
    --surface-2: #1d252d;
    --border: #2a333d;
    --text: #e6ebf0;
    --muted: #93a1ae;
    --primary: #2cb5a8;
    --primary-ink: #06201d;
    --primary-soft: #133330;
    --danger: #f06b5e;
    --danger-soft: #3a1c19;
    --success: #4cc27f;
    --success-soft: #16301f;
    --lost: #6b7682;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 20px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: var(--mono); }

/* ── Buttons & inputs ─────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 8px; border: 1px solid transparent;
  font-weight: 600; white-space: nowrap; transition: background 0.15s, border-color 0.15s;
}
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.05); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-block { width: 100%; }
.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after {
  content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.5); border-top-color: #fff; animation: spin 0.8s linear infinite;
}
.btn-ghost.is-busy::after { border-color: var(--border); border-top-color: var(--primary); }
@keyframes spin { to { transform: rotate(360deg); } }
.icon-btn { background: none; border: 0; width: 32px; height: 32px; border-radius: 8px; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); }

.input, .field input {
  width: 100%; height: 38px; padding: 0 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit;
}
.input:focus, .field input:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.field { display: block; margin: 0 0 14px; }
.field > span { display: block; margin-bottom: 6px; font-weight: 600; font-size: 13px; }
.req { color: var(--danger); font-weight: 700; }
.field-error, .form-error { color: var(--danger); font-size: 12.5px; margin: 6px 0 0; min-height: 0; }
.field-error:empty, .form-error:empty { display: none; }
.hint { font-size: 12.5px; margin: 6px 0 0; color: var(--muted); }
.hint:empty { display: none; }
.hint.ok { color: var(--success); }
.hint.warn { color: var(--warn); }
.row { display: flex; gap: 8px; }

/* ── Login ────────────────────────────────────────────────── */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px 24px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-lg { margin-bottom: 22px; }
.brand-name { font-weight: 700; font-size: 16px; }

/* ── App shell ────────────────────────────────────────────── */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); z-index: 1000;
}
.stats { display: flex; gap: 14px; flex: 1; justify-content: center; color: var(--muted); font-size: 13px; }
.stats b { color: var(--text); }
.stat-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 1px; }
.topbar-actions { display: flex; gap: 8px; }
.sync-info { font-size: 12px; color: var(--muted); }
.sync-info.is-error { color: var(--danger); }
.banner { background: var(--danger-soft); color: var(--danger); text-align: center; padding: 6px; font-weight: 600; font-size: 13px; }
.workspace { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 330px; flex-shrink: 0; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-right: 1px solid var(--border);
}
.sidebar-head { padding: 12px; border-bottom: 1px solid var(--border); display: grid; gap: 10px; }
.sheet-handle { display: none; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 4px 10px; font-size: 12.5px; color: var(--muted);
}
.chip.is-active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }
/* position tường minh: Leaflet chỉ tự gán `relative` lúc tạo map — qua breakpoint mobile↔desktop
   mà để `static` là tile + nút zoom văng ra ngoài khung. */
.map { position: relative; flex: 1; min-width: 0; background: var(--surface-2); }

/* ── Vehicle list ─────────────────────────────────────────── */
.vehicle-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.vehicle-item {
  display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: center;
  padding: 9px 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent;
}
.vehicle-item:hover { background: var(--surface-2); }
.vehicle-item.is-selected { background: var(--primary-soft); border-color: var(--primary); }
.vehicle-item .plate { font-weight: 700; letter-spacing: 0.2px; }
.vehicle-item .sub { color: var(--muted); font-size: 12.5px; }
.vi-icon { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; }
.vi-icon svg { width: 24px; height: 24px; }
.badge {
  font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge-moving { color: var(--moving); border-color: currentColor; background: transparent; }
.badge-stopped { color: var(--stopped); border-color: currentColor; background: transparent; }
.badge-lost { color: var(--lost); }
.badge-nofix { color: var(--warn); border-color: currentColor; background: transparent; }
.vi-badges { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.power-tag { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; white-space: nowrap; }
.power-tag.is-on { color: var(--success); background: var(--success-soft); }
.power-tag.is-off { color: var(--danger); background: var(--danger-soft); }
.empty { color: var(--muted); text-align: center; padding: 24px 16px; margin: 0; }

/* ── Marker ───────────────────────────────────────────────── */
.car-marker { background: none; border: 0; }
.car-marker .cm-body {
  position: relative; width: 38px; height: 38px; border-radius: 50%;
  background: var(--c, var(--lost)); border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  display: grid; place-items: center;
}
.car-marker .cm-car { width: 27px; height: 27px; transition: transform 0.4s ease; }
.car-marker.is-alarm .cm-body { animation: alarm 1.1s ease-in-out infinite; }
@keyframes alarm { 0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.7); } 50% { box-shadow: 0 0 0 9px rgba(220, 38, 38, 0); } }
.car-marker .cm-offline {
  position: absolute; right: -3px; bottom: -3px; width: 13px; height: 13px; border-radius: 50%;
  background: #6b7682; border: 2px solid #fff;
}
.car-marker .cm-label {
  position: absolute; top: 42px; left: 50%; transform: translateX(-50%);
  background: rgba(22, 32, 42, 0.86); color: #fff; font-size: 11px; font-weight: 700;
  padding: 1px 6px; border-radius: 4px; white-space: nowrap; pointer-events: none;
}
.map.hide-labels .cm-label { display: none; }
.car-marker.is-selected .cm-body { outline: 3px solid var(--primary); outline-offset: 2px; }

/* ── Popup ────────────────────────────────────────────────── */
.leaflet-popup-content-wrapper { border-radius: 12px; background: var(--surface); color: var(--text); }
.leaflet-popup-tip { background: var(--surface); }
.leaflet-popup-content { margin: 14px 16px; font: 13.5px/1.45 var(--font); }
.pp-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.pp-plate { font-size: 17px; font-weight: 800; }
.pp-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.pp-grid { display: grid; grid-template-columns: auto 1fr; gap: 5px 14px; margin: 12px 0; padding: 10px 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.pp-grid dt { color: var(--muted); }
.pp-grid dd { margin: 0; font-weight: 600; }
.pp-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pp-note { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.pp-warn { color: var(--warn); font-size: 12px; margin: 8px 0 0; }

/* ── Dialog ───────────────────────────────────────────────── */
.dialog {
  width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 20px;
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text);
  box-shadow: var(--shadow);
}
.dialog-sm { width: min(400px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(10, 16, 22, 0.45); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-title { font-size: 17px; margin: 0 0 6px; }
.dialog-body { color: var(--muted); margin: 6px 0 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.pick-list {
  margin-top: 8px; max-height: 220px; overflow-y: auto; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface-2);
}
.pick-item {
  display: flex; justify-content: space-between; gap: 10px; width: 100%; text-align: left;
  padding: 9px 12px; background: none; border: 0; border-bottom: 1px solid var(--border);
}
.pick-item:last-child { border-bottom: 0; }
.pick-item:hover:not(:disabled) { background: var(--primary-soft); }
.pick-item.is-selected { background: var(--primary-soft); box-shadow: inset 3px 0 0 var(--primary); }
.pick-item .plate { font-weight: 700; }
.pick-item .sub { color: var(--muted); font-size: 12.5px; }
.pick-item:disabled { opacity: 0.5; }
.pick-empty { padding: 16px; color: var(--muted); text-align: center; }
.picked {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 8px; background: var(--primary-soft); margin: 0 0 14px;
}
.picked b { font-size: 15px; }

/* ── Toast ────────────────────────────────────────────────── */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 5000; max-width: min(420px, calc(100vw - 32px)); }
.toast {
  padding: 11px 14px; border-radius: 10px; box-shadow: var(--shadow); font-weight: 600;
  background: var(--surface); border-left: 4px solid var(--primary); animation: toast-in 0.2s ease-out;
}
.toast-success { border-left-color: var(--success); }
.toast-error { border-left-color: var(--danger); }
.toast small { display: block; font-weight: 400; color: var(--muted); margin-top: 2px; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* ── Mobile: sidebar thành ngăn kéo dưới ──────────────────── */
@media (max-width: 767px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 8px 16px; }
  .stats { order: 3; flex-basis: 100%; justify-content: flex-start; gap: 10px; font-size: 12px; }
  .topbar-actions .btn { height: 32px; padding: 0 10px; }
  .workspace { position: relative; }
  .map { position: absolute; inset: 0; }
  .sidebar {
    position: absolute; left: 0; right: 0; bottom: 0; width: auto; height: 42%;
    border-right: 0; border-top: 1px solid var(--border); border-radius: 14px 14px 0 0;
    box-shadow: var(--shadow); z-index: 900; transition: height 0.2s ease;
  }
  .sidebar.is-collapsed { height: 64px; }
  .sheet-handle {
    display: block; width: 100%; height: 18px; border: 0; background: none; position: relative; flex-shrink: 0;
  }
  .sheet-handle::before {
    content: ""; position: absolute; left: 50%; top: 7px; width: 40px; height: 4px; margin-left: -20px;
    border-radius: 2px; background: var(--border);
  }
  .sidebar-head { padding-top: 0; }
  .leaflet-popup-content { margin: 12px; }
  .pp-grid { gap: 4px 10px; }
  html, body { overflow: hidden; }
}

/* ── Nhật ký thiết bị ─────────────────────────────────────── */
.dialog-wide { width: min(980px, calc(100vw - 32px)); }
.log-config { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.log-config .text-bad { color: var(--danger, #c62828); }
.log-cmd { display: flex; gap: 8px; margin-top: 8px; }
.log-cmd .input { flex: 1; min-width: 0; }
.log-cmd-reply { margin-top: 6px; padding: 6px 8px; background: var(--surface-2); border-radius: 6px; white-space: pre-wrap; word-break: break-word; }
.log-hints { margin: 10px 0; padding: 10px 12px 10px 28px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; }
.log-hints li { margin: 3px 0; }
.log-table-wrap { max-height: 60vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.log-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.log-table th { position: sticky; top: 0; background: var(--surface-2); text-align: left; padding: 6px 8px; font-size: 11.5px; color: var(--muted); }
.log-table td { padding: 6px 8px; border-top: 1px solid var(--border); vertical-align: top; }
.log-table tr.is-bad td { background: var(--danger-soft); }
.log-table .mono { font-size: 11px; color: var(--muted); word-break: break-all; }
.log-table td:first-child { white-space: nowrap; color: var(--muted); }
.pp-link { background: none; border: 0; padding: 0; margin-top: 8px; color: var(--primary); font-weight: 600; font-size: 12.5px; }
