:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1d2733;
  --muted: #6b7786;
  --border: #dde3ea;
  --brand: #1e5fa8;
  --brand-ink: #ffffff;
  --top: #16324f;
  --top-ink: #eaf1f8;
  --fresh: #22a06b;
  --stale: #e0a100;
  --old: #d9534f;
  --none: #8a94a3;
  --sel-bg: #e8f1fc;
  --radius: 10px;
  --shadow: 0 6px 22px rgba(16, 32, 51, .14);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1720;
    --panel: #17212c;
    --text: #e6edf5;
    --muted: #93a1b3;
    --border: #2a3847;
    --brand: #4d9de0;
    --brand-ink: #0b1420;
    --top: #0b1520;
    --top-ink: #e6edf5;
    --sel-bg: #1d3046;
    --shadow: 0 6px 22px rgba(0, 0, 0, .45);
  }
}

:root[data-theme="dark"] {
  --bg: #0f1720;
  --panel: #17212c;
  --text: #e6edf5;
  --muted: #93a1b3;
  --border: #2a3847;
  --brand: #4d9de0;
  --brand-ink: #0b1420;
  --top: #0b1520;
  --top-ink: #e6edf5;
  --sel-bg: #1d3046;
  --shadow: 0 6px 22px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

button, input, select { font: inherit; color: inherit; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }

/* ---------- topo ---------- */

.topbar {
  height: 52px;
  background: var(--top);
  color: var(--top-ink);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand strong { font-size: 16px; white-space: nowrap; }
.logo {
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center; flex: none;
}
.badge {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: #f0b429; color: #3a2a00; white-space: nowrap;
}
.top-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 8px; border: 0;
  background: transparent; color: inherit; cursor: pointer;
  display: grid; place-items: center;
}
.icon-btn:hover { background: rgba(255, 255, 255, .12); }
.text-btn {
  background: transparent; border: 1px solid rgba(255, 255, 255, .3);
  color: inherit; border-radius: 8px; padding: 6px 10px; cursor: pointer;
}

/* ---------- layout ---------- */

.layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  flex: 1;
  min-height: 0;
}

/* Contêiner da aplicação ocupa a janela inteira (abaixo do cabeçalho). */
#appShell {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---------- lateral ---------- */

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side-head { padding: 14px; border-bottom: 1px solid var(--border); display: grid; gap: 10px; }
.side-title { display: flex; align-items: center; justify-content: space-between; font-weight: 600; }
.count {
  background: var(--sel-bg); color: var(--text);
  border-radius: 999px; padding: 0 9px; font-size: 12px;
}
input[type="search"], input[type="text"], input[type="datetime-local"], select, .login input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row label { flex: 1 1 140px; display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.actions { justify-content: space-between; }
.selected-count { font-size: 12px; color: var(--muted); margin-left: auto; }

.chip-btn, .primary-btn {
  border-radius: 8px; padding: 7px 11px; cursor: pointer; border: 1px solid var(--border);
  background: var(--panel); font-size: 13px;
}
.chip-btn:hover { border-color: var(--brand); }
.primary-btn { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }

.tag-list {
  list-style: none; margin: 0; padding: 0;
  overflow-y: auto; flex: 1; min-height: 0;
}
.tag-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.tag-item:hover { background: var(--sel-bg); }
.tag-item.active { background: var(--sel-bg); box-shadow: inset 3px 0 0 var(--brand); }
.tag-item input { width: 16px; height: 16px; accent-color: var(--brand); margin: 0; }
.tag-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot {
  width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 6px; vertical-align: middle;
}
.st-fresh { background: var(--fresh); }
.st-stale { background: var(--stale); }
.st-old { background: var(--old); }
.st-none { background: var(--none); }
.tag-time { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.empty { padding: 24px 16px; color: var(--muted); text-align: center; }
.side-foot {
  padding: 8px 14px; border-top: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}

/* ---------- mapa ---------- */

.map-wrap { position: relative; min-width: 0; min-height: 0; }
#map { position: absolute; inset: 0; }

.marker {
  position: relative;
  width: 26px; height: 26px; border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg); border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
}
.marker::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 8px; height: 8px; margin: -4px 0 0 -4px; border-radius: 50%; background: #fff;
}
.marker.st-fresh { background: var(--fresh); }
.marker.st-stale { background: var(--stale); }
.marker.st-old { background: var(--old); }
.marker.st-none { background: var(--none); }
.marker-label {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 1px 6px; font-size: 12px; font-weight: 600; white-space: nowrap;
  box-shadow: var(--shadow);
}

.leaflet-container { font: inherit; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel); color: var(--text); }
.popup h4 { margin: 0 0 4px; }
.popup p { margin: 2px 0; font-size: 12px; color: var(--muted); }

.details {
  position: absolute; top: 12px; right: 12px; width: 340px; max-height: calc(100% - 24px);
  overflow-y: auto; z-index: 500;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px;
}
.details-head { display: flex; justify-content: space-between; gap: 8px; }
.details h2 { margin: 0; font-size: 17px; }
.details h3 { margin: 14px 0 8px; font-size: 14px; }
.details-head p { margin: 2px 0 0; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 10px; margin: 12px 0 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }
.history { border-top: 1px solid var(--border); margin-top: 14px; }
.history .row { margin-bottom: 8px; }
.history .actions { justify-content: flex-start; }
.history .primary-btn, .history .chip-btn { flex: 0 0 auto; }

.toast {
  position: absolute; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--top); color: var(--top-ink); padding: 10px 16px; border-radius: 8px;
  box-shadow: var(--shadow); z-index: 1000; max-width: calc(100% - 32px);
}

/* ---------- login ---------- */

.login {
  position: fixed; inset: 0; background: rgba(8, 15, 24, .55);
  display: grid; place-items: center; z-index: 2000; padding: 16px;
}
.login-card {
  background: var(--panel); color: var(--text); border-radius: 12px; box-shadow: var(--shadow);
  padding: 22px; width: min(360px, 100%); display: grid; gap: 12px;
}
.login-card h2 { margin: 0; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card .primary-btn { width: 100%; padding: 10px; }
.error { color: var(--old); margin: 0; font-size: 13px; }

/* ---------- celular ---------- */

@media (max-width: 860px) {
  body { overflow: auto; }
  #appShell { height: auto; min-height: 100%; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; flex: none; min-height: calc(100vh - 52px); }
  .sidebar { max-height: 42vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .map-wrap { height: 58vh; min-height: 380px; }
  .details {
    position: static; width: auto; max-height: none; margin: 12px 16px 0; box-shadow: none;
  }
  .map-wrap:has(.details:not([hidden])) { height: auto; min-height: 0; }
  .map-wrap:has(.details:not([hidden])) #map { position: relative; height: 50vh; min-height: 320px; }
  .toast { bottom: 12px; }
}

@media (max-width: 420px) {
  .brand strong { font-size: 14px; }
  .kv { grid-template-columns: 105px 1fr; }
}
