:root {
  --azul: #1E6FDE;
  --azul-h: #1559b0;
  --azul-d: #0C447C;
  --azul-l: #E6F1FB;
  --tinta: #1f2a37;
  --muted: #6b7280;
  --linha: #e6e8ee;
  --fundo: #f4f6fb;
  --branco: #fff;
  --nav-h: 60px;
  --raio: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Inter", system-ui, sans-serif;
  color: var(--tinta);
  background: var(--fundo);
  font-size: 14px;
  /* app-shell: coluna que ocupa toda a altura da tela */
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ===== Barra superior (full-width, fixa no topo) ===== */
.nav {
  background: var(--azul);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 clamp(14px, 2vw, 26px);
  height: var(--nav-h);
  position: sticky;
  top: 0;
  z-index: 50;
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-words { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-words strong { font-family: "Space Grotesk", sans-serif; font-size: 19px; font-weight: 700; }
.brand-words span { font-size: 12px; opacity: .85; white-space: nowrap; }
.nav-acoes { display: flex; align-items: center; gap: 10px; }
.nav-user { color: #fff; font-size: 13px; opacity: .9; white-space: nowrap; }

/* ===== Área principal: full-width, ocupa a altura restante ===== */
main {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;                       /* permite os filhos com overflow encolherem */
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: clamp(12px, 1.6vw, 24px);
}

.filtros {
  background: var(--branco);
  border: 1px solid var(--linha);
  border-radius: var(--raio);
  padding: 14px 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  flex: 0 0 auto;
}
.campo { display: flex; flex-direction: column; gap: 4px; }
.campo.grow { flex: 1; min-width: 150px; }
.campo.grow2 { flex: 1.4; min-width: 180px; }
.campo label { font-size: 12px; font-weight: 500; color: var(--muted); }
.campo-acoes { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }

select, input[type="text"] {
  height: 38px;
  border: 1px solid var(--linha);
  border-radius: 8px;
  padding: 0 10px;
  font: inherit;
  background: var(--branco);
  color: var(--tinta);
  min-width: 120px;
}
select:focus, input[type="text"]:focus {
  outline: none;
  border-color: var(--azul);
  box-shadow: 0 0 0 3px var(--azul-l);
}

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 38px; padding: 0 15px;
  border-radius: 8px; border: 1px solid transparent;
  font: inherit; font-weight: 500; cursor: pointer;
  white-space: nowrap;
  transition: background-color .18s ease, border-color .18s ease;
}
.btn .ic { font-size: 17px; line-height: 1; }
.btn-primary { background: var(--azul); color: #fff; }
.btn-primary:hover { background: var(--azul-h); }
.btn-outline { background: var(--branco); border-color: var(--linha); color: var(--tinta); }
.btn-outline:hover { background: var(--azul-l); border-color: var(--azul); }
.btn-light { background: rgba(255,255,255,.16); color: #fff; border-color: rgba(255,255,255,.45); }
.btn-light:hover { background: rgba(255,255,255,.28); }
.btn-mini { height: 28px; padding: 0 10px; background: var(--branco); border: 1px solid var(--linha); color: var(--tinta); border-radius: 7px; }
.btn-mini:hover { background: var(--azul-l); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-perigo { background: #e24b4a; color: #fff; border-color: #e24b4a; }
.btn-perigo:hover { background: #c0392b; border-color: #c0392b; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; flex: 0 0 auto; }
.stat { background: var(--branco); border: 1px solid var(--linha); border-radius: var(--raio); padding: 14px 16px; }
.stat .rotulo { font-size: 13px; color: var(--muted); }
.stat .valor { font-family: "Space Grotesk", sans-serif; font-size: 26px; font-weight: 600; color: var(--azul-d); margin-top: 2px; }

/* ===== Abas (so aparecem no celular) ===== */
.tabs-mobile { display: none; gap: 8px; flex: 0 0 auto; }
.tab-btn {
  flex: 1; height: 42px; border: 1px solid var(--linha); background: var(--branco);
  color: var(--muted); border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
}
.tab-btn.ativo { background: var(--azul); color: #fff; border-color: var(--azul); }

/* ===== Painel: mapa + tabela ocupando a altura restante ===== */
.painel {
  display: grid;
  grid-template-columns: 1fr 1.15fr;
  gap: 16px;
  flex: 1 1 auto;
  min-height: 0;
}
.card { background: var(--branco); border: 1px solid var(--linha); border-radius: 14px; overflow: hidden; }
.mapa-card { display: flex; min-height: 0; }
#mapa { flex: 1; width: 100%; min-height: 320px; }

.tabela-card { display: flex; flex-direction: column; min-height: 0; }
.tabela-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--linha); flex: 0 0 auto; }
.paginacao { display: flex; align-items: center; gap: 8px; }
.tabela-scroll { flex: 1; overflow: auto; min-height: 0; }
.muted { color: var(--muted); font-size: 13px; }

table { width: 100%; border-collapse: collapse; }
thead th {
  position: sticky; top: 0; z-index: 1;
  text-align: left; padding: 10px 12px;
  background: #f7f9fc; color: var(--muted);
  font-size: 12px; font-weight: 600;
  border-bottom: 1px solid var(--linha); white-space: nowrap;
}
tbody td { padding: 9px 12px; border-bottom: 1px solid #f0f2f6; font-size: 13px; vertical-align: middle; }
.cel-email { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tbody tr:hover td { background: var(--azul-l); }
.nome { font-weight: 500; }
.tag { display: inline-block; background: var(--azul-l); color: var(--azul-d); font-size: 12px; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
td a { color: var(--azul); text-decoration: none; }
td a:hover { text-decoration: underline; }
.maps-link { font-size: 12px; color: var(--azul); white-space: nowrap; }

/* ===== Modal ===== */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 16px; }
.modal-overlay[hidden] { display: none; }
.modal { background: var(--branco); border-radius: 14px; width: 460px; max-width: 100%; max-height: calc(100dvh - 32px); overflow: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--linha); }
.modal-head h2 { margin: 0; font-family: "Space Grotesk", sans-serif; font-size: 17px; font-weight: 600; }
.modal-x { border: none; background: none; font-size: 16px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 18px; border-top: 1px solid var(--linha); }
.tipos-check { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; }
.tipos-check label { display: flex; align-items: center; gap: 7px; font-size: 13px; font-weight: 400; color: var(--tinta); }
.check-linha { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--tinta); cursor: pointer; }
.check-linha input { width: auto; min-width: 0; height: auto; }

/* ===== Progresso da coleta CNES ===== */
.progresso-coleta .barra { height: 8px; border-radius: 6px; background: var(--linha); overflow: hidden; }
.barra-fill { height: 100%; width: 0; background: var(--azul); border-radius: 6px; transition: width .3s ease; }
.barra-fill.completa { width: 100%; background: #16a34a; }
.barra-fill.indeterminada { width: 40%; animation: indet 1.2s ease-in-out infinite; }
@keyframes indet { 0% { margin-left: -40%; } 100% { margin-left: 100%; } }
#coletaStatus.sucesso { color: #15803d; font-weight: 600; }
#coletaStatus.erro { color: #b91c1c; font-weight: 600; }

/* chip flutuante quando a coleta esta minimizada */
.coleta-chip { position: fixed; right: 18px; bottom: 18px; z-index: 200; display: inline-flex; align-items: center;
  gap: 8px; background: var(--azul); color: #fff; border: none; border-radius: 999px; padding: 10px 16px;
  font: inherit; font-weight: 600; cursor: pointer; box-shadow: 0 8px 24px rgba(30,111,222,.4); }
.coleta-chip[hidden] { display: none; }
.spinner { width: 14px; height: 14px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff;
  border-radius: 50%; animation: girar .8s linear infinite; }
@keyframes girar { to { transform: rotate(360deg); } }

.export-wrap { position: relative; }
.export-menu { position: absolute; right: 0; top: calc(100% + 6px); background: var(--branco); border: 1px solid var(--linha); border-radius: 10px; box-shadow: 0 8px 26px rgba(15,23,42,.14); padding: 6px; min-width: 190px; z-index: 50; display: flex; flex-direction: column; gap: 2px; }
.export-menu[hidden] { display: none; }
.export-menu button { width: 100%; text-align: left; background: none; border: none; padding: 9px 12px; border-radius: 7px; font: inherit; color: var(--tinta); cursor: pointer; }
.export-menu button:hover { background: var(--azul-l); }

/* ===== Login (continua centralizado, faz sentido) ===== */
.login-body { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center; background: var(--fundo); padding: 20px; }
.login-card { background: var(--branco); border: 1px solid var(--linha); border-radius: 16px; padding: 28px 26px; width: 360px; max-width: 100%; display: flex; flex-direction: column; gap: 14px; }
.login-logo { display: flex; align-items: center; gap: 12px; }
.login-mark { width: 40px; height: 40px; border-radius: 50%; background: var(--azul); display: flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.login-logo strong { font-family: "Space Grotesk", sans-serif; font-size: 18px; display: block; color: var(--tinta); }
.login-logo span { font-size: 12px; color: var(--muted); }
.login-card h1 { font-family: "Space Grotesk", sans-serif; font-size: 20px; font-weight: 600; margin: 4px 0 0; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.login-card .btn-primary { margin-top: 6px; justify-content: center; }
.login-erro { background: #fdecec; color: #a32d2d; border-radius: 8px; padding: 8px 12px; font-size: 13px; margin: 0; }

/* ===== Admin (agora full-width) ===== */
.admin { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; align-items: start; }
.admin .card { padding: 18px 20px; }
.admin h2 { font-family: "Space Grotesk", sans-serif; font-size: 16px; font-weight: 600; margin: 0 0 14px; }
.admin-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.admin-form input { flex: 1; min-width: 160px; }
.admin-form select { min-width: 150px; }
.admin table { width: 100%; border-collapse: collapse; }
#adminMsg { margin: 12px 0 0; }

/* =========================================================
   RESPONSIVO
   ========================================================= */

/* Tablet: mapa e tabela empilhados (ambos visiveis), pagina rola */
@media (max-width: 1024px) {
  .painel { grid-template-columns: 1fr; flex: 0 0 auto; }
  .mapa-card { height: 60vh; min-height: 320px; }
  #mapa { height: 100%; }
  .tabela-card { max-height: 80vh; }
}

/* Celular: abas Mapa/Lista, filtros empilhados, tabela vira cartoes */
@media (max-width: 640px) {
  body { font-size: 15px; }
  .brand-words { display: none; }            /* no celular fica so o icone da marca */
  .nav-user { display: none; }
  .nav { padding: 0 12px; gap: 8px; }
  .nav-acoes { gap: 6px; }
  .nav-acoes .btn { height: 38px; padding: 0 11px; font-size: 13px; }
  .nav-acoes .btn .ic { font-size: 15px; }

  .filtros { padding: 12px; gap: 10px; }
  .campo, .campo.grow, .campo.grow2 { flex: 1 1 100%; min-width: 0; }
  .campo-acoes { width: 100%; }
  .campo-acoes .btn, .export-wrap { flex: 1; }
  .export-wrap .btn { width: 100%; justify-content: center; }
  /* 16px evita o zoom automatico do iOS; 44px = alvo de toque confortavel */
  select, input[type="text"] { font-size: 16px; height: 44px; }
  .btn { height: 44px; }

  /* abas controlam o que aparece */
  .tabs-mobile { display: flex; }
  .painel { grid-template-columns: 1fr; }
  .mapa-card, .tabela-card { height: auto; max-height: none; }
  .painel.vista-mapa { flex: 1 1 auto; }     /* mapa preenche a tela */
  .painel.vista-lista { flex: 0 0 auto; }    /* lista flui e rola junto com a pagina */
  .painel.vista-mapa .tabela-card { display: none; }
  .painel.vista-lista .mapa-card { display: none; }
  .painel.vista-mapa .mapa-card { display: flex; flex: 1 1 auto; }
  .painel.vista-lista .tabela-card { display: flex; }
  #mapa { min-height: 55vh; }
  .tabela-scroll { overflow: visible; }      /* sem scroll aninhado no celular */

  /* tabela -> cartoes */
  thead { display: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  tbody tr { border: 1px solid var(--linha); border-radius: 12px; margin: 10px; padding: 6px 14px; background: var(--branco); }
  tbody tr:hover td { background: transparent; }
  tbody td { display: flex; justify-content: space-between; align-items: center; gap: 14px; padding: 7px 0; border-bottom: 1px solid #f2f4f8; font-size: 14px; text-align: right; }
  tbody td:last-child { border-bottom: none; }
  tbody td::before { content: attr(data-label); font-weight: 600; color: var(--muted); font-size: 12px; text-align: left; flex: 0 0 auto; }
  tbody td.nome { font-size: 15px; font-weight: 600; }
  tbody td .tag { white-space: normal; }
  tbody td[data-label=""]::before { display: none; }
  tbody td.cel-email { white-space: normal; overflow: visible; max-width: none; word-break: break-all; }
}

/* Telas largas: um respiro extra nas laterais para nao "esticar" demais o conteudo */
@media (min-width: 1800px) {
  main { padding-left: clamp(24px, 4vw, 90px); padding-right: clamp(24px, 4vw, 90px); }
}

/* Respeita quem prefere menos animacao */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
