:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --online: #16a34a;
  --offline: #94a3b8;
  --sos: #dc2626;
  --warning: #d97706;
  --radius: 8px;
  --sidebar-width: 360px;
  font-family:
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
  color: var(--text);
  background: var(--bg);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-size: 14px;
  line-height: 1.4;
}

[hidden] {
  display: none !important;
}

h1,
h2,
h3,
h4 {
  margin: 0;
  line-height: 1.2;
}

code {
  font-size: 0.9em;
  background: var(--bg);
  padding: 0 4px;
  border-radius: 4px;
}

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

/* Botões e campos */

.button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 14px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.button:hover {
  background: var(--bg);
}

.button:disabled {
  opacity: 0.6;
  cursor: progress;
}

.button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.button.primary:hover {
  background: var(--primary-hover);
}

.button.ghost {
  background: transparent;
  border-color: transparent;
}

.button.ghost:hover {
  background: rgba(15, 23, 42, 0.06);
}

.button.light {
  background: #fff;
  color: var(--sos);
  border-color: #fff;
}

.button.small {
  padding: 4px 10px;
  font-size: 13px;
}

.button.block {
  width: 100%;
}

input,
textarea {
  font: inherit;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  background: var(--surface);
  width: 100%;
  min-width: 0;
}

input:focus,
textarea:focus,
.button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

textarea {
  resize: vertical;
}

.form-error {
  color: var(--sos);
  margin: 4px 0 0;
  min-height: 0;
  font-size: 13px;
}

.form-error:empty {
  display: none;
}

/* Login */

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 16px;
  background: linear-gradient(160deg, #0f172a, #1e3a8a);
}

.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border-radius: 12px;
  padding: 28px 24px;
  display: grid;
  gap: 14px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.login-card .muted {
  margin: -8px 0 4px;
}

.field {
  display: grid;
  gap: 4px;
  font-weight: 600;
}

/* Estrutura */

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: #0f172a;
  color: #fff;
  flex-wrap: wrap;
}

.topbar .button.ghost {
  color: #fff;
  margin-left: auto;
}

.topbar .button.ghost:hover {
  background: rgba(255, 255, 255, 0.12);
}

.brand {
  font-size: 16px;
}

.summary {
  color: #cbd5e1;
}

.live {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #cbd5e1;
  font-size: 13px;
}

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

.live[data-state="live"] .live-dot {
  background: #22c55e;
}

.live[data-state="offline"] .live-dot {
  background: var(--sos);
}

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-areas: "sidebar map";
}

.map {
  grid-area: map;
  min-height: 0;
}

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

.list-view,
.detail-view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

/* SOS */

.sos-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--sos);
  color: #fff;
  animation: sos-flash 1s ease-in-out infinite alternate;
}

.sos-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  flex: 1;
}

.sos-device {
  border: 1px solid rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 999px;
  padding: 3px 10px;
  font: inherit;
  cursor: pointer;
}

.sos-hint {
  font-size: 13px;
  opacity: 0.9;
}

@keyframes sos-flash {
  from {
    background: #dc2626;
  }
  to {
    background: #991b1b;
  }
}

.marker-sos {
  animation: marker-pulse 1.2s ease-out infinite;
}

@keyframes marker-pulse {
  0% {
    stroke-width: 2px;
    stroke-opacity: 1;
  }
  100% {
    stroke-width: 22px;
    stroke-opacity: 0;
  }
}

/* Lista */

.sidebar-header {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

.device-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.device-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.device-item:hover {
  background: var(--bg);
}

.device-item.selected {
  background: #eff6ff;
  box-shadow: inset 3px 0 0 var(--primary);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
  background: var(--offline);
}

.status-online .status-dot {
  background: var(--online);
}

.status-sos .status-dot {
  background: var(--sos);
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.25);
}

.device-main {
  flex: 1;
  min-width: 0;
  display: grid;
}

.device-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-sub {
  color: var(--muted);
  font-size: 12px;
}

.device-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

.battery {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  font-weight: 600;
}

.battery-low {
  color: var(--sos);
}

.signal {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
  vertical-align: middle;
}

.signal .bar {
  width: 3px;
  background: var(--border);
  border-radius: 1px;
}

.signal .bar:nth-child(1) {
  height: 25%;
}
.signal .bar:nth-child(2) {
  height: 50%;
}
.signal .bar:nth-child(3) {
  height: 75%;
}
.signal .bar:nth-child(4) {
  height: 100%;
}

.signal .bar.on {
  background: var(--text);
}

.empty {
  color: var(--muted);
  padding: 16px;
  margin: 0;
}

/* Detalhe */

.detail-view {
  padding: 12px 16px 24px;
}

.detail-view h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 20px 0 8px;
}

.back {
  margin-left: -10px;
}

.detail-title {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 8px 0 10px;
}

.detail-title h2 {
  font-size: 18px;
  word-break: break-all;
}

.pill {
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  color: #fff;
  background: var(--offline);
}

.pill-online {
  background: var(--online);
}

.pill-sos {
  background: var(--sos);
}

.rename {
  display: flex;
  gap: 6px;
}

.facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  margin: 14px 0 0;
}

.fact {
  display: contents;
}

.fact dt {
  color: var(--muted);
}

.fact dd {
  margin: 0;
  word-break: break-word;
}

.action {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}

.action h4 {
  font-size: 14px;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
}

.action-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.action-row textarea,
.action-row input[type="tel"] {
  flex: 1;
}

.action:not(:has(input, textarea)) .action-row {
  justify-content: flex-start;
}

.inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.inline input {
  width: 72px;
}

.counter {
  font-weight: 400;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.counter-full {
  color: var(--sos);
}

.events,
.commands {
  list-style: none;
  margin: 0;
  padding: 0;
}

.event,
.command {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.event time,
.command-status {
  color: var(--muted);
  white-space: nowrap;
  font-size: 12px;
}

.event-alert .event-label {
  color: var(--warning);
  font-weight: 600;
}

.event-sos .event-label {
  color: var(--sos);
}

.command-ok .command-status {
  color: var(--online);
}

.command-error .command-status {
  color: var(--sos);
}

/* Avisos */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 8px;
  z-index: 1000;
  max-width: min(360px, calc(100vw - 32px));
}

.toast {
  background: #0f172a;
  color: #fff;
  padding: 10px 14px;
  border-radius: var(--radius);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.toast-success {
  background: #166534;
}

.toast-danger {
  background: #b91c1c;
}

.toast-warning {
  background: #b45309;
}

.leaflet-container {
  font: inherit;
}

/* Celular: mapa em cima, painel embaixo */

@media (max-width: 767px) {
  .topbar {
    gap: 10px;
    padding: 8px 12px;
  }

  .summary {
    display: none;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: 45% 55%;
    grid-template-areas:
      "map"
      "sidebar";
  }

  .sidebar {
    border-right: 0;
    border-top: 1px solid var(--border);
  }

  .toasts {
    left: 16px;
    right: 16px;
    max-width: none;
  }
}
