:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0ea5e9;
  --primary-dark: #0284c7;
  --accent: #1e293b;
  --font: 'Inter', 'Segoe UI', system-ui, sans-serif;

  --c-blue: #2563eb;   --c-blue-bg: #eff6ff;
  --c-green: #16a34a;  --c-green-bg: #f0fdf4;
  --c-red: #dc2626;    --c-red-bg: #fef2f2;
  --c-amber: #d97706;  --c-amber-bg: #fffbeb;
  --c-slate: #475569;  --c-slate-bg: #f8fafc;
  --c-violet: #7c3aed; --c-violet-bg: #f5f3ff;

  --shadow-sm: 0 1px 2px rgba(15,23,42,.04), 0 1px 3px rgba(15,23,42,.06);
  --shadow-md: 0 4px 6px rgba(15,23,42,.04), 0 8px 24px rgba(15,23,42,.06);
  --shadow-lg: 0 20px 50px rgba(15,23,42,.12);
}
* { box-sizing: border-box; }
body {
  margin: 0; font-family: var(--font);
  background: var(--bg); color: var(--text);
}

/* ── Login ── */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
  background: var(--card); padding: 36px 32px; border-radius: 18px;
  box-shadow: var(--shadow-lg); width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.login-logo {
  width: 60px; height: 60px; border-radius: 16px; margin: 0 auto;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff; font-weight: 800; font-size: 24px;
  display: flex; align-items: center; justify-content: center;
}
.login-logo.sm { width: 30px; height: 30px; font-size: 13px; border-radius: 8px; display: inline-flex; }
.login-card h1 { margin: 6px 0 0; font-size: 22px; }
.login-card label { text-align: left; font-size: 13px; font-weight: 600; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
input, select {
  font-family: var(--font); font-size: 14px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--card); color: var(--text);
}
select option { color: var(--text); background: var(--card); }
button {
  font-family: var(--font); font-weight: 700; font-size: 14px; cursor: pointer;
  background: var(--primary); color: #fff; border: none; border-radius: 10px; padding: 11px 16px;
  transition: background .15s, transform .1s;
}
button:hover { background: var(--primary-dark); }
button:active { transform: scale(.98); }
button:disabled { opacity: .6; cursor: default; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: rgba(0,0,0,.05); color: var(--text); }
.erro { color: var(--c-red); font-size: 13px; min-height: 16px; }
.muted { color: var(--muted); }

/* ── App shell ── */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 32px; background: var(--accent); color: #fff;
  box-shadow: 0 2px 12px rgba(0,0,0,.15); position: sticky; top: 0; z-index: 50;
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 10px; }
.brand .muted, .brand small { color: #94a3b8; }
.container { max-width: 1600px; margin: 0 auto; padding: 28px 36px 48px; display: flex; flex-direction: column; gap: 24px; }

/* ── KPI cards ── */
.kpis { display: grid; grid-template-columns: repeat(5, 1fr); gap: 16px; }
.kpi {
  background: var(--card); border-radius: 16px; padding: 20px 22px;
  box-shadow: var(--shadow-sm); border: 1px solid var(--border);
  display: flex; align-items: center; gap: 14px;
  transition: box-shadow .15s, transform .15s;
}
.kpi:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); }
.kpi-icon {
  width: 44px; height: 44px; border-radius: 12px; flex: none;
  display: flex; align-items: center; justify-content: center; font-size: 20px;
}
.kpi-body { display: flex; flex-direction: column; min-width: 0; }
.kpi .v { font-size: 26px; font-weight: 800; line-height: 1.1; }
.kpi .l { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-top: 3px; white-space: nowrap; }

.kpi.c-blue   .kpi-icon { background: var(--c-blue-bg);   color: var(--c-blue); }
.kpi.c-green  .kpi-icon { background: var(--c-green-bg);  color: var(--c-green); }
.kpi.c-red    .kpi-icon { background: var(--c-red-bg);    color: var(--c-red); }
.kpi.c-amber  .kpi-icon { background: var(--c-amber-bg);  color: var(--c-amber); }
.kpi.c-slate  .kpi-icon { background: var(--c-slate-bg);  color: var(--c-slate); }
.kpi.c-violet .kpi-icon { background: var(--c-violet-bg); color: var(--c-violet); }

/* ── Grids gerais ── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; align-items: start; }
.card {
  background: var(--card); border-radius: 18px; padding: 24px;
  box-shadow: var(--shadow-sm); border: 1px solid var(--border);
}
.card h2 {
  margin: 0 0 16px; font-size: 14.5px; font-weight: 700; color: var(--text);
  display: flex; align-items: center; gap: 8px;
}
.card h2 .badge-count {
  background: var(--bg); color: var(--muted); font-size: 11px; font-weight: 700;
  padding: 2px 9px; border-radius: 999px; margin-left: auto;
}

.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 160px 1fr 50px; align-items: center; gap: 10px; font-size: 13px; }
.bar-row .lbl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: var(--bg); border-radius: 6px; height: 10px; overflow: hidden; }
.bar-fill { background: var(--primary); height: 100%; border-radius: 6px; transition: width .3s; }
.bar-row .num { text-align: right; font-weight: 700; }

.filtros { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 18px; }
.filtros input[type=search] { flex: 1; min-width: 200px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 12px 14px; white-space: nowrap; }
thead th {
  color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  border-bottom: 2px solid var(--border); font-weight: 700;
}
tbody tr { border-bottom: 1px solid var(--border); transition: background .1s; }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--bg); }
.tag { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; letter-spacing: .02em; }
.tag.MOBILE { background: var(--c-blue-bg); color: var(--c-blue); }
.tag.DESKTOP { background: var(--c-green-bg); color: var(--c-green); }
.tag.TABLET { background: var(--c-amber-bg); color: var(--c-amber); }

.pager { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 18px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
  .container { padding: 20px; }
}

/* ── Licenças ── */
.subnav { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }
.subnav-btn {
  background: var(--card); color: var(--muted); border: 1px solid var(--border);
  padding: 9px 18px; border-radius: 11px; font-size: 13px; font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow-sm); transition: all .15s;
}
.subnav-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); box-shadow: 0 4px 14px rgba(14,165,233,.35); }
.subnav-btn:hover:not(.active) { background: var(--bg); transform: translateY(-1px); }

.bars-list { display: flex; flex-direction: column; gap: 4px; }
.bars-row {
  display: flex; align-items: center; gap: 14px;
  font-size: 13.5px; padding: 12px 8px; border-radius: 10px;
  transition: background .12s;
}
.bars-row:hover { background: var(--bg); }
.row-avatar {
  width: 36px; height: 36px; border-radius: 10px; flex: none;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 13px; color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
}
.row-info { display: flex; flex-direction: column; min-width: 0; gap: 2px; }
.row-info strong { font-size: 13.5px; }
.row-info code { font-size: 11px; align-self: flex-start; }
.row-trail { margin-left: auto; flex: none; }

.btn-acao {
  font-size: 12px; padding: 6px 12px; border-radius: 8px;
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  margin-right: 6px; cursor: pointer; font-weight: 600; transition: all .15s;
}
.btn-acao:hover { background: var(--primary); color: #fff; border-color: var(--primary); }
code { background: var(--bg); padding: 2px 7px; border-radius: 6px; font-size: 12px; color: var(--slate); }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.55); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal {
  background: var(--card); border-radius: 18px; padding: 28px; width: 100%; max-width: 660px;
  max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow-lg);
}
.modal h2 { margin: 0 0 18px; font-size: 18px; font-weight: 800; }
.modal-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.modal-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; font-weight: 600; color: var(--muted); }
.modal-foot { display: flex; gap: 10px; margin-top: 20px; }
.modal-secao {
  margin: 22px 0 12px; padding-top: 14px; border-top: 1px solid var(--border);
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted);
}
.modal-secao small { text-transform: none; letter-spacing: 0; font-weight: 500; }
.modal-grid input:disabled { opacity: .6; cursor: not-allowed; }
.checkbox-list {
  display: flex; flex-direction: column; gap: 6px; margin-top: 8px;
  max-height: 160px; overflow-y: auto; border: 1px solid var(--border);
  border-radius: 10px; padding: 10px;
}
.checkbox-list label {
  display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 500;
  color: var(--text); cursor: pointer;
}
.checkbox-list input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }

@media (max-width: 600px) {
  .modal-grid { grid-template-columns: 1fr; }
}

/* ── Navegação compartilhada ── */
.topbar-nav { display: flex; gap: 14px; align-items: center; }
.topbar-nav a {
  color: #cbd5e1; text-decoration: none; font-size: 13px; font-weight: 500;
  padding: 6px 10px; border-radius: 8px; transition: background .15s;
}
.topbar-nav a:hover { background: rgba(255,255,255,.08); }
.topbar-nav a.active { color: #fff; font-weight: 700; background: rgba(255,255,255,.12); }
.btn-menu {
  display: none; background: transparent; border: none; color: #fff;
  font-size: 22px; padding: 6px 10px; cursor: pointer; border-radius: 8px;
}
.btn-menu:hover { background: rgba(255,255,255,.1); }

.nav-drawer {
  position: fixed; inset: 0; z-index: 200; pointer-events: none; opacity: 0;
  transition: opacity .2s;
}
.nav-drawer.open { pointer-events: auto; opacity: 1; }
.nav-drawer-backdrop {
  position: absolute; inset: 0; background: rgba(15,23,42,.5);
}
.nav-drawer-panel {
  position: absolute; top: 0; right: 0; width: min(280px, 85vw); height: 100%;
  background: var(--accent); color: #fff; padding: 20px;
  display: flex; flex-direction: column; gap: 6px;
  transform: translateX(100%); transition: transform .25s ease;
  box-shadow: -8px 0 30px rgba(0,0,0,.2);
}
.nav-drawer.open .nav-drawer-panel { transform: translateX(0); }
.nav-drawer-panel a {
  color: #e2e8f0; text-decoration: none; font-size: 15px; font-weight: 600;
  padding: 14px 16px; border-radius: 12px;
}
.nav-drawer-panel a:hover, .nav-drawer-panel a.active {
  background: rgba(255,255,255,.12); color: #fff;
}
.nav-drawer-panel .drawer-sair { margin-top: auto; color: #94a3b8; }

.bottom-nav {
  display: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 60;
  background: var(--card); border-top: 1px solid var(--border);
  box-shadow: 0 -4px 20px rgba(15,23,42,.08);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px));
}
.bottom-nav-inner {
  display: flex; justify-content: space-around; align-items: stretch; max-width: 600px; margin: 0 auto;
}
.bottom-nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
  text-decoration: none; color: var(--muted); font-size: 10px; font-weight: 600;
  padding: 8px 4px; border-radius: 10px; min-height: 48px; justify-content: center;
  transition: color .15s, background .15s;
}
.bottom-nav a .bn-icon { font-size: 20px; line-height: 1; }
.bottom-nav a.active { color: var(--primary); background: var(--c-blue-bg); }
.bottom-nav a.danger { color: var(--c-red); }

.has-bottom-nav { padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px)); }

/* ── Cards mobile (substituem tabelas) ── */
.data-cards { display: none; flex-direction: column; gap: 10px; }
.data-card {
  background: var(--bg); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px 16px; display: flex; flex-direction: column; gap: 8px;
}
.data-card-row {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
  font-size: 13px;
}
.data-card-row .dc-label {
  color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .04em; flex: none;
}
.data-card-row .dc-val { text-align: right; word-break: break-word; }
.data-card-head {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding-bottom: 8px; border-bottom: 1px solid var(--border);
}
.data-card-head strong { font-size: 14px; }
.data-card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

/* ── Gráfico temporal (CSS puro) ── */
.chart-bars {
  display: flex; align-items: flex-end; gap: 4px; height: 140px;
  padding: 8px 0 24px; overflow-x: auto;
}
.chart-bar-col {
  flex: 1; min-width: 18px; max-width: 36px; display: flex; flex-direction: column;
  align-items: center; gap: 4px; height: 100%; justify-content: flex-end;
}
.chart-bar-fill {
  width: 100%; background: linear-gradient(180deg, var(--primary), var(--primary-dark));
  border-radius: 4px 4px 0 0; min-height: 2px; transition: height .3s;
}
.chart-bar-lbl {
  font-size: 9px; color: var(--muted); writing-mode: vertical-rl;
  transform: rotate(180deg); max-height: 40px; overflow: hidden;
}

/* ── Subnav scroll horizontal ── */
.subnav-scroll {
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  scrollbar-width: none; margin: 0 -4px; padding: 0 4px 4px;
}
.subnav-scroll::-webkit-scrollbar { display: none; }
.subnav-scroll .subnav { flex-wrap: nowrap; width: max-content; min-width: 100%; }

/* ── Toolbar de ações ── */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; min-width: 8px; }
.btn-secondary {
  background: var(--card); color: var(--text); border: 1px solid var(--border);
  font-weight: 600;
}
.btn-secondary:hover { background: var(--bg); color: var(--text); }

/* ── Detalhe / histórico ── */
.hist-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.hist-item {
  font-size: 13px; padding: 10px 12px; background: var(--bg);
  border-radius: 10px; border-left: 3px solid var(--primary);
}
.hist-item .hist-acao { font-weight: 700; text-transform: capitalize; }
.hist-item .hist-data { font-size: 11px; color: var(--muted); margin-top: 2px; }
.detail-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; font-size: 13px; margin-bottom: 12px;
}
.detail-grid div { display: flex; flex-direction: column; gap: 2px; }
.detail-grid span:first-child { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; }

/* ── PWA / mobile meta ── */
.page-title { margin: 0 0 4px; font-size: 20px; font-weight: 800; }
.page-subtitle { margin: 0 0 16px; font-size: 13px; color: var(--muted); }

/* ── Responsivo mobile ── */
@media (max-width: 768px) {
  .topbar { padding: 12px 16px; }
  .topbar-nav, .topbar .btn-ghost:not(.drawer-only) { display: none; }
  .btn-menu { display: block; }
  .brand { font-size: 16px; }
  .brand small { display: none; }
  .container { padding: 16px 14px 24px; gap: 18px; }
  .has-bottom-nav .container { padding-bottom: 8px; }

  .kpis { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .kpi { padding: 14px 16px; gap: 10px; }
  .kpi .v { font-size: 22px; }
  .kpi .l { font-size: 10px; white-space: normal; }
  .kpi-icon { width: 38px; height: 38px; font-size: 17px; }

  .bottom-nav { display: block; }
  body { padding-bottom: 0; }

  .filtros, .toolbar { flex-direction: column; }
  .filtros input[type=search],
  .filtros input[type=date],
  .filtros select,
  .filtros button,
  .toolbar input[type=search],
  .toolbar input[type=date],
  .toolbar select,
  .toolbar button { width: 100%; min-width: unset; }
  .toolbar .spacer { display: none; }
  .toolbar button { flex: 1; min-width: calc(50% - 5px); }

  .table-wrap.hide-mobile { display: none; }
  .data-cards.show-mobile { display: flex; }

  .bar-row { grid-template-columns: 90px 1fr 40px; font-size: 12px; gap: 6px; }
  .bars-row { padding: 10px 4px; }
  .row-trail { font-size: 11px; }

  .subnav-btn { padding: 10px 14px; font-size: 12px; white-space: nowrap; min-height: 44px; }
  .btn-acao { padding: 8px 14px; min-height: 40px; font-size: 13px; margin-right: 0; }
  button { min-height: 44px; }
  .pager button { min-height: 44px; flex: 1; }

  .modal { padding: 20px 18px; border-radius: 16px 16px 0 0; max-height: 92vh; align-self: flex-end; }
  .modal-overlay { align-items: flex-end; padding: 0; }
  .detail-grid { grid-template-columns: 1fr; }

  .login-card { margin: 16px; padding: 28px 22px; }
}

@media (max-width: 400px) {
  .kpis { grid-template-columns: 1fr; }
}

/* ── Mapa Leaflet ── */
.mapa-wrap {
  width: 100%; height: 420px; border-radius: 14px; overflow: hidden;
  border: 1px solid var(--border); margin-top: 12px; z-index: 1;
}
@media (max-width: 768px) {
  .mapa-wrap { height: 55vh; min-height: 280px; }
}
.leaflet-popup-content { font-family: var(--font); font-size: 13px; margin: 10px 14px; line-height: 1.5; }
.leaflet-popup-content strong { color: var(--text); }

/* ── Dark mode ── */
[data-theme="dark"] {
  --bg: #0f172a;
  --card: #1e293b;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --border: #334155;
  --accent: #020617;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,.3);
}
[data-theme="dark"] .topbar { background: #020617; }
[data-theme="dark"] input, [data-theme="dark"] select {
  background: #0f172a; color: var(--text); border-color: var(--border);
}

/* ── Busca global ── */
.topbar-actions { display: flex; gap: 6px; align-items: center; margin-left: auto; }
.btn-busca {
  background: rgba(255,255,255,.1); border: none; color: #fff;
  width: 36px; height: 36px; border-radius: 10px; cursor: pointer; font-size: 16px;
}
.busca-modal {
  position: fixed; inset: 0; z-index: 300; background: rgba(15,23,42,.6);
  display: flex; align-items: flex-start; justify-content: center; padding: 80px 16px 16px;
}
.busca-modal-inner {
  background: var(--card); border-radius: 16px; width: 100%; max-width: 520px;
  box-shadow: var(--shadow-lg); overflow: hidden;
}
.busca-modal-inner input {
  width: 100%; border: none; border-bottom: 1px solid var(--border);
  padding: 16px 18px; font-size: 16px; border-radius: 0;
}
.busca-item {
  display: flex; flex-direction: column; gap: 2px; padding: 12px 18px;
  text-decoration: none; color: var(--text); border-bottom: 1px solid var(--border);
}
.busca-item:hover { background: var(--bg); }
.busca-item small { color: var(--muted); font-size: 12px; }

.pull-hint {
  position: fixed; top: -40px; left: 50%; transform: translateX(-50%);
  background: var(--primary); color: #fff; padding: 8px 16px; border-radius: 999px;
  font-size: 12px; font-weight: 700; z-index: 40; transition: top .2s;
}
.pull-hint.visible { top: 60px; }

.impersonate-banner {
  background: var(--c-amber); color: #78350f; padding: 10px 16px; text-align: center;
  font-size: 13px; display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap;
}
.impersonate-banner button {
  background: #fff; color: #78350f; padding: 6px 14px; min-height: auto; font-size: 12px;
}

.health-ok { color: var(--c-green); font-weight: 700; }
.health-fail { color: var(--c-red); font-weight: 700; }
.print-only { display: none; }
@media print {
  .topbar, .bottom-nav, .busca-global, .subnav, .toolbar button { display: none !important; }
  .print-only { display: block; }
  body { background: #fff; }
}
