/* Fleet monitoring shell — dark theme, map-first layout */
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap");

:root {
  --bg: #0b0f14;
  --bg-elevated: #121922;
  --surface: #161d27;
  --surface-hover: #1c2633;
  --border: #2a3545;
  --text: #e8eef5;
  --muted: #8b9bb4;
  --accent: #4d9fff;
  --accent-dim: #2d5a8a;
  --ok: #3ecf8e;
  --warn: #f0b429;
  --danger: #f25c54;
  --sidebar-w: 300px;
  --topbar-h: 52px;
  --bottom-collapsed: 0px;
  --bottom-expanded: 220px;
  --drawer-w: 340px;
  --radius: 10px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --font: "Inter", "Segoe UI", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* --- Shell: topbar + main row + bottom --- */
.app-shell {
  display: grid;
  grid-template-rows: auto 1fr var(--bottom-collapsed);
  grid-template-columns: 1fr;
  height: 100vh;
  width: 100vw;
}

.app-shell.bottom-open {
  grid-template-rows: auto 1fr var(--bottom-expanded);
}

/* --- Top bar --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  min-height: var(--topbar-h);
  background: var(--bg-elevated);
  border-bottom: none;
  gap: 0.75rem;
  flex-wrap: nowrap;
  z-index: 50;
  min-width: 0;
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.topbar__logo {
  width: 8px;
  height: 28px;
  border-radius: 4px;
  background: linear-gradient(180deg, var(--accent), #1a4d7a);
  flex-shrink: 0;
}

.topbar__title {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__status {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--muted);
  flex-shrink: 1;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: 0.1rem;
  scrollbar-width: thin;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-weight: 500;
}

.status-pill--ok {
  border-color: #2a5c42;
  color: var(--ok);
}

.status-pill--off {
  border-color: #5c3d2a;
  color: var(--warn);
}

.ws-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.ws-dot--live {
  background: var(--ok);
  box-shadow: 0 0 8px var(--ok);
}

.topbar__actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  padding-bottom: 0.1rem;
  scrollbar-width: thin;
}

.btn {
  font: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover {
  background: var(--surface-hover);
  border-color: var(--accent-dim);
}

.btn:active {
  transform: scale(0.98);
}

.btn--primary {
  border-color: var(--accent-dim);
  background: #1a2a3d;
  color: #cfe6ff;
}

.btn--danger {
  border-color: #6b2a2a;
  color: #ffb4af;
}

.btn--ghost {
  background: transparent;
}

/* --- Body: sidebar + map --- */
.body-row {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 0;
  min-width: 0;
}

.app-shell--sidebar-collapsed .body-row {
  grid-template-columns: 0 1fr;
}

.app-shell--sidebar-collapsed .sidebar {
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-rows: auto 1fr var(--bottom-collapsed);
  }

  .app-shell.bottom-open {
    grid-template-rows: auto 1fr var(--bottom-expanded);
  }

  .body-row {
    grid-template-columns: 1fr;
    position: relative;
    overflow: hidden;
  }

  .sidebar {
    display: flex;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    height: min(52vh, 420px);
    width: 100%;
    max-width: 100%;
    z-index: 420;
    border-right: none;
    border-top: 1px solid var(--border);
    box-shadow: 0 -10px 28px rgba(0, 0, 0, 0.45);
    transform: translateY(calc(100% - 42px));
    transition: transform 0.22s ease;
    overflow: hidden;
  }

  .sidebar.sidebar--mobile-open {
    transform: translateY(0);
  }

  .sidebar::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 46px;
    height: 4px;
    border-radius: 999px;
    background: rgba(139, 155, 180, 0.55);
    z-index: 2;
  }

  .sidebar__head {
    padding-top: 1.1rem;
  }

  .map-host {
    min-height: 42vh;
  }
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  min-height: 0;
}

.sidebar__head {
  padding: 0.75rem 0.85rem;
  border-bottom: 1px solid var(--border);
}

.sidebar__search {
  width: 100%;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
}

.sidebar__search::placeholder {
  color: var(--muted);
}

.sidebar__filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.5rem;
}

.chip {
  font-size: 0.72rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.chip--active {
  background: #1a2a3d;
  border-color: var(--accent-dim);
  color: var(--text);
}

.sidebar__list {
  flex: 1;
  overflow-y: auto;
  padding: 0.35rem;
}

.device-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.6rem 0.55rem;
  margin-bottom: 2px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.12s, border-color 0.12s;
}

.device-row:hover {
  background: var(--surface);
}

.device-row--selected {
  background: #1a2838;
  border-color: var(--accent-dim);
}

.device-row__dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-top: 4px;
  flex-shrink: 0;
}

.device-row__dot--on {
  background: var(--ok);
  box-shadow: 0 0 6px rgba(62, 207, 142, 0.45);
}

.device-row__dot--off {
  background: #555;
}

.device-row__dot--moving {
  background: #22c55e;
  box-shadow: 0 0 8px rgba(34, 197, 94, 0.45);
}

.device-row__dot--idle {
  background: #eab308;
  box-shadow: 0 0 8px rgba(234, 179, 8, 0.35);
}

.device-row__dot--standing {
  background: #f59e0b;
  box-shadow: 0 0 8px rgba(245, 158, 11, 0.35);
}

.device-row__dot--parked {
  background: #eab308;
  box-shadow: 0 0 8px rgba(234, 179, 8, 0.35);
}

.device-row__dot--offline {
  background: #ef4444;
  box-shadow: 0 0 8px rgba(239, 68, 68, 0.35);
}

.device-row__main {
  flex: 1;
  min-width: 0;
}

.device-row__imei {
  font-size: 0.82rem;
  font-weight: 600;
  word-break: break-all;
}

.device-row__meta {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.map-host {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: #0a0d11;
}

.trace-history-panel {
  position: absolute;
  left: 12px;
  top: 12px;
  width: min(420px, 40vw);
  max-height: calc(100% - 24px);
  z-index: 610;
  background: rgba(15, 20, 28, 0.96);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  display: none;
  flex-direction: column;
}

.trace-history-panel--open {
  display: flex;
}

.trace-history-panel__head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.55rem 0.65rem;
  border-bottom: 1px solid var(--border);
}

.trace-history-panel__title {
  margin: 0;
  font-size: 0.86rem;
}

.trace-history-panel__close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
}

.trace-history-panel__body {
  padding: 0.65rem;
  overflow: auto;
  font-size: 0.8rem;
}

.th-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.th-form label {
  display: grid;
  gap: 0.22rem;
  font-size: 0.72rem;
  color: var(--muted);
}

.th-form select,
.th-form input {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem 0.42rem;
  font: inherit;
}

.th-controls {
  display: flex;
  gap: 0.35rem;
  margin-top: 0.55rem;
  flex-wrap: wrap;
}

.th-range {
  width: 100%;
  margin-top: 0.55rem;
}

.th-meta {
  margin-top: 0.45rem;
  color: var(--muted);
  font-size: 0.72rem;
}

#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.drawer-toggle {
  position: absolute;
  top: 12px;
  right: 52px;
  z-index: 500;
  padding: 0.45rem 0.7rem;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(18, 25, 34, 0.92);
  color: var(--text);
  cursor: pointer;
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow);
}

.drawer-toggle:hover {
  border-color: var(--accent);
}

.drawer {
  position: absolute;
  top: 0;
  right: 0;
  width: var(--drawer-w);
  max-width: 96vw;
  height: 100%;
  background: rgba(15, 20, 28, 0.96);
  border-left: 1px solid var(--border);
  z-index: 400;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(12px);
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.drawer--open {
  transform: translateX(0);
  pointer-events: auto;
}

.drawer__head {
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.5rem;
}

.drawer__title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.drawer__close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.15rem;
}

.drawer__close:hover {
  color: var(--text);
}

.drawer__body {
  padding: 1rem;
  overflow-y: auto;
  overflow-x: hidden;
  font-size: 0.85rem;
}

.kv {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 0.35rem 0.75rem;
  margin-bottom: 0.5rem;
}

.kv dt {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kv dd {
  margin: 0;
  word-break: break-all;
}

.drawer-admin {
  margin-top: 1rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--border);
}

.drawer-admin__label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.drawer-admin__input {
  width: 100%;
  padding: 0.45rem 0.55rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  margin-bottom: 0.65rem;
}

.drawer-admin__input:focus {
  outline: none;
  border-color: var(--accent-dim);
}

.drawer-admin__btns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.drawer-tabs {
  display: flex;
  gap: 0.35rem;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  margin-bottom: 0.6rem;
  padding-bottom: 0.15rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.drawer-tab {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: 8px;
  padding: 0.35rem 0.4rem;
  cursor: pointer;
  font-size: 0.72rem;
}

.drawer-tab--active {
  background: #1a2a3d;
  border-color: var(--accent-dim);
  color: var(--text);
}

.drawer-pane {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(22, 29, 39, 0.6);
  padding: 0.6rem;
}

.drawer-raw {
  margin: 0;
  white-space: pre-wrap;
  font-size: 0.72rem;
  color: var(--muted);
}

.trace-card {
  margin-top: 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem;
  background: rgba(22, 29, 39, 0.55);
}

.trace-card__top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.45rem;
}

.status-badge {
  border-radius: 999px;
  font-size: 0.68rem;
  border: 1px solid var(--border);
  padding: 0.2rem 0.45rem;
}

.status-badge--moving {
  color: #22c55e;
  border-color: rgba(34, 197, 94, 0.5);
}

.status-badge--idle {
  color: #eab308;
  border-color: rgba(234, 179, 8, 0.5);
}

.status-badge--standing {
  color: #f59e0b;
  border-color: rgba(245, 158, 11, 0.5);
}

.status-badge--parked {
  color: #eab308;
  border-color: rgba(234, 179, 8, 0.5);
}

.status-badge--offline {
  color: #ef4444;
  border-color: rgba(239, 68, 68, 0.5);
}

.trace-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.trace-range {
  display: flex;
  gap: 0.35rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

.trace-custom {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 0.35rem;
  margin-top: 0.45rem;
}

.trace-dt {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  font: inherit;
  padding: 0.26rem 0.32rem;
  font-size: 0.74rem;
}

.trace-speed {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  font: inherit;
  padding: 0.22rem 0.3rem;
}

.trace-slider {
  width: 100%;
  margin-top: 0.45rem;
}

.trace-range-info {
  margin-top: 0.35rem;
  font-size: 0.72rem;
  color: var(--muted);
}

/* Bottom panel */
.bottom-panel {
  background: var(--bg-elevated);
  border-top: none;
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 30;
}

.bottom-panel__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.75rem;
  height: var(--bottom-collapsed);
  flex-shrink: 0;
  cursor: pointer;
  user-select: none;
  display: none;
}

.bottom-panel__bar:hover {
  background: var(--surface);
}

.bottom-panel__title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.bottom-panel__chev {
  color: var(--muted);
  transition: transform 0.2s;
}

.app-shell.bottom-open .bottom-panel__chev {
  transform: rotate(180deg);
}

.bottom-panel__content {
  display: none;
  flex: 1;
  min-height: 0;
  padding: 0 0.75rem 0.5rem;
}

.app-shell.bottom-open .bottom-panel__content {
  display: flex;
  flex-direction: column;
}

.app-shell.bottom-open .bottom-panel__bar {
  display: flex;
}

.data-table-wrap {
  flex: 1;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.data-table th,
.data-table td {
  padding: 0.45rem 0.5rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.65rem;
  letter-spacing: 0.05em;
}

.data-table tr:hover td {
  background: rgba(77, 159, 255, 0.06);
}

.btn-xs {
  font-size: 0.72rem;
  padding: 0.2rem 0.45rem;
  border-radius: 6px;
  border: 1px solid var(--danger);
  background: transparent;
  color: #ffb4af;
  cursor: pointer;
}

.btn-xs:hover {
  background: rgba(242, 92, 84, 0.12);
}

.bulk-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  font-size: 0.78rem;
}

.link-doc {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.78rem;
}

.link-doc:hover {
  text-decoration: underline;
}

/* Leaflet dark tweaks */
.leaflet-popup-content-wrapper {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 8px !important;
}

.leaflet-popup-tip {
  background: var(--surface) !important;
}

.leaflet-bar a {
  background: var(--surface) !important;
  color: var(--text) !important;
}

.leaflet-control-layers {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 8px !important;
}

.leaflet-control-attribution {
  font-size: 10px !important;
  max-width: 45% !important;
  background: rgba(11, 15, 20, 0.85) !important;
  color: var(--muted) !important;
}

.marker-cluster-small {
  background: rgba(77, 159, 255, 0.35) !important;
}

.marker-cluster-small div {
  background: rgba(77, 159, 255, 0.85) !important;
  color: #fff !important;
}

.tracker-marker-wrap {
  background: transparent !important;
  border: none !important;
}

.veh-marker {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(10, 13, 17, 0.9);
  border: 1px solid rgba(148, 163, 184, 0.3);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  position: relative;
  display: grid;
  place-items: center;
}

.veh-marker--selected {
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.9), 0 4px 14px rgba(0, 0, 0, 0.45);
}

.veh-marker__arrow {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 9px solid var(--veh-color, #22c55e);
  transform: rotate(var(--veh-rot, 0deg)) translateY(-1px);
  transform-origin: 50% 70%;
}

.veh-marker__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--veh-color, #22c55e);
  position: absolute;
  bottom: 3px;
  left: 50%;
  transform: translateX(-50%);
}

.leaflet-container {
  touch-action: none;
}

@media (max-width: 900px) {
  .topbar {
    display: grid;
    grid-template-columns: 1fr;
    grid-auto-rows: auto;
    align-content: start;
    padding: 0.45rem 0.6rem;
    gap: 0.45rem;
  }

  .topbar__brand {
    order: 1;
  }

  .topbar__status {
    order: 2;
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 0.45rem;
    padding-bottom: 0.1rem;
  }

  .topbar__actions {
    order: 3;
    margin-left: 0;
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    width: 100%;
    max-width: 100%;
    gap: 0.35rem;
  }

  #btnSidebarMobile {
    position: sticky;
    left: 0;
    z-index: 2;
    flex-shrink: 0;
  }

  .drawer-toggle {
    display: none;
  }

  .drawer-toggle {
    right: 10px;
    top: 10px;
    z-index: 560;
  }

  .drawer {
    width: 100vw;
    max-width: 100vw;
  }

  .drawer__body {
    padding: 0.75rem;
  }

  .drawer-tabs {
    position: sticky;
    top: 0;
    z-index: 2;
    background: rgba(15, 20, 28, 0.96);
    margin: -0.75rem -0.75rem 0.6rem;
    padding: 0.6rem 0.75rem 0.35rem;
  }

  .trace-history-panel {
    left: 8px;
    right: 8px;
    width: auto;
    max-height: 56vh;
    top: 8px;
  }
}
