:root {
  --bg: #eef3fb;
  --panel: #ffffff;
  --line: #dbe5f3;
  --line-strong: #c9d5e8;
  --text: #061937;
  --muted: #5d6d87;
  --blue: #2557e6;
  --blue-dark: #0d3c9d;
  --blue-soft: #e9efff;
  --green: #12805c;
  --red: #b42318;
  --orange: #b54708;
  --shadow: 0 12px 30px rgba(16, 35, 77, .08);
  --radius: 18px;
  --control-h: 46px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-width: 320px;
  font-family: Inter, Segoe UI, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select { font: inherit; }
button { cursor: pointer; }

.app { width: 100%; min-height: 100vh; }
.main {
  width: min(100%, 1640px);
  margin: 0 auto;
  padding: 16px;
}

.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.top h1 {
  margin: 0 0 6px;
  font-size: clamp(1.45rem, 2.1vw, 2rem);
  line-height: 1.05;
}
.top p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
  max-width: 980px;
}
.topActions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.panel,
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.ghost,
.secondary,
#btnConsultar,
#btnExportar,
.pager button,
.mini {
  min-height: 40px;
  border: 0;
  border-radius: 12px;
  padding: 9px 13px;
  font-weight: 800;
  line-height: 1.1;
  transition: transform .12s ease, box-shadow .12s ease, opacity .12s ease;
  white-space: nowrap;
}
.ghost,
.secondary,
.pager button,
.mini {
  background: var(--blue-soft);
  color: var(--blue-dark);
}
#btnConsultar,
#btnExportar {
  background: var(--blue);
  color: #fff;
}
.ghost:hover,
.secondary:hover,
#btnConsultar:hover,
#btnExportar:hover,
.pager button:hover,
.mini:hover { transform: translateY(-1px); }
.sm { min-height: 38px !important; padding: 8px 12px !important; font-size: .92rem !important; }

.hero {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 18px;
  padding: 18px 20px;
  margin-bottom: 14px;
  background: linear-gradient(120deg, #fff, #eff5ff);
}
.badge {
  display: inline-flex;
  max-width: 100%;
  background: #dfe9ff;
  color: #1450de;
  padding: 7px 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 900;
  line-height: 1.2;
}
.hero h2 {
  margin: 12px 0 8px;
  font-size: clamp(1.15rem, 1.7vw, 1.55rem);
  line-height: 1.2;
}
.hero p {
  margin: 0;
  max-width: 980px;
  color: #33415f;
  line-height: 1.5;
}
.heroStatus {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-width: 240px;
  padding: 14px 18px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  white-space: nowrap;
}
.idleDot,.loadingDot,.okDot,.warnDot,.errDot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: inline-block;
  flex: 0 0 auto;
}
.idleDot { background: #9aa8bd; }
.loadingDot { background: var(--blue); animation: pulse 1s infinite; }
.okDot { background: var(--green); }
.warnDot { background: #f59e0b; }
.errDot { background: var(--red); }
@keyframes pulse { 50% { opacity: .35; } }

.filters {
  display: grid;
  grid-template-columns: minmax(170px, 1.2fr) minmax(95px, .6fr) minmax(180px, 1fr) minmax(260px, 1.5fr) minmax(300px, 2.2fr) auto;
  gap: 12px;
  align-items: start;
  padding: 16px;
  margin-bottom: 14px;
}
.field { min-width: 0; }
.field label {
  display: block;
  margin-bottom: 7px;
  font-weight: 900;
  line-height: 1.2;
}
.field input,
.field select,
.tableTools input,
.tableTools select,
.exportPanel select {
  width: 100%;
  min-width: 0;
  min-height: var(--control-h);
  border: 1px solid var(--line-strong);
  border-radius: 13px;
  padding: 10px 13px;
  background: #fff;
  color: var(--text);
  outline: 0;
}
.field input:focus,
.field select:focus,
.tableTools input:focus,
.tableTools select:focus,
.exportPanel select:focus {
  border-color: #9db5ff;
  box-shadow: 0 0 0 3px rgba(37,87,230,.12);
}
.field small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: .8rem;
  line-height: 1.35;
}
.filtersActions {
  display: flex;
  gap: 8px;
  align-items: center;
  align-self: end;
  justify-content: flex-end;
  min-width: 220px;
}

.toolbarGrid {
  display: grid;
  grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
  gap: 14px;
  align-items: stretch;
  margin-bottom: 14px;
}
.exportPanel {
  padding: 16px;
  min-width: 0;
}
.toolbarHead h2,
.resultsPanel h2,
.grid2 h2 {
  margin: 0 0 5px;
  font-size: 1.08rem;
  line-height: 1.2;
}
.toolbarHead p,
.sectionHead p {
  margin: 0;
  color: var(--muted);
  line-height: 1.35;
}
.exportBar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
  margin-top: 12px;
}
.exportBar .field { margin: 0; }
.noMargin { margin: 0; }
.cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(145px, 1fr));
  gap: 14px;
  margin: 0;
  min-width: 0;
}
.card {
  padding: 16px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.card span {
  display: block;
  color: var(--muted);
  font-weight: 800;
  line-height: 1.2;
}
.card strong {
  display: block;
  margin: 10px 0 6px;
  font-size: clamp(1.25rem, 2vw, 1.65rem);
  line-height: 1.05;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card small { color: #74839a; line-height: 1.25; }

.resultsPanel {
  padding: 0;
  margin-bottom: 14px;
  overflow: hidden;
}
.sectionHead {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(420px, 760px);
  gap: 16px;
  align-items: end;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}
.resultsTitle { min-width: 0; }
.sectionHead p {
  max-width: 100%;
  overflow-wrap: anywhere;
}
.tableTools {
  display: grid;
  grid-template-columns: minmax(220px, 1.7fr) minmax(128px, .8fr) minmax(128px, .8fr) 88px;
  gap: 8px;
  align-items: center;
  min-width: 0;
}
.linkBtn {
  display: none;
  margin-top: 8px;
  border: 0;
  background: transparent;
  color: var(--blue);
  font-weight: 900;
  padding: 0;
  cursor: pointer;
}
.empty {
  margin: 18px 20px;
  border: 1px solid #ffd8a8;
  background: #fff6ed;
  color: #9a3412;
  border-radius: 14px;
  padding: 16px;
  text-align: center;
}
.tableWrap {
  overflow: auto;
  max-height: 620px;
}
.tableWrap table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.tableWrap th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #eef4ff;
}
.tableWrap th,
.tableWrap td {
  border-bottom: 1px solid #e3e9f4;
  padding: 10px;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}
.tableWrap th:nth-child(1),
.tableWrap td:nth-child(1) { min-width: 170px; }
.tableWrap th:nth-child(2),
.tableWrap td:nth-child(2) {
  min-width: 520px;
  white-space: normal;
  line-height: 1.35;
}
.tableWrap tr:hover { background: #f8fbff; }
.pager {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  padding: 14px;
  border-top: 1px solid var(--line);
}

.grid2 {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
}
.grid2 .panel { padding: 18px 20px; min-width: 0; }
.diagnostico { line-height: 1.65; color: #243653; overflow-wrap: anywhere; }
.ok { color: var(--green); font-weight: 800; }
.err { color: var(--red); font-weight: 800; }
.warn { color: var(--orange); font-weight: 800; }
.apiError {
  white-space: pre-wrap;
  font-family: ui-monospace, Consolas, monospace;
  background: #fff1f0;
  border: 1px solid #ffccc7;
  color: #8a1f11;
  padding: 12px;
  border-radius: 12px;
  margin-top: 10px;
}
.loading button { opacity: .75; }

.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(6,25,55,.45);
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal.show { display: flex; }
.modalBox {
  position: relative;
  width: min(760px, 96vw);
  max-height: 82vh;
  overflow: auto;
  background: #fff;
  border-radius: 20px;
  padding: 24px;
}
.x {
  position: absolute;
  right: 16px;
  top: 14px;
  width: 34px;
  height: 34px;
  border: 0;
  background: #eef4ff;
  border-radius: 50%;
  cursor: pointer;
}
.histItem {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 10px 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
}

@media (max-width: 1350px) {
  .filters {
    grid-template-columns: repeat(12, minmax(0, 1fr));
    align-items: start;
  }
  .fieldMunicipio { grid-column: span 3; }
  .fieldAno { grid-column: span 2; }
  .fieldMes { grid-column: span 3; }
  .fieldUnidade { grid-column: span 4; }
  .fieldModulo { grid-column: span 8; }
  .filtersActions {
    grid-column: span 4;
    justify-content: flex-end;
    align-self: end;
    min-width: 0;
  }
  .toolbarGrid { grid-template-columns: minmax(280px, 380px) minmax(0, 1fr); }
  .cards { grid-template-columns: repeat(4, minmax(125px, 1fr)); }
}

@media (max-width: 1100px) {
  .hero { grid-template-columns: 1fr; }
  .heroStatus { min-width: 0; width: fit-content; }
  .toolbarGrid { grid-template-columns: 1fr; }
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sectionHead { grid-template-columns: 1fr; align-items: stretch; }
  .tableTools { width: 100%; grid-template-columns: minmax(220px, 1.4fr) repeat(2, minmax(120px, .8fr)) 88px; }
  .grid2 { grid-template-columns: 1fr; }
}

@media (max-width: 820px) {
  .main { padding: 12px; }
  .top { flex-direction: column; align-items: stretch; }
  .topActions { justify-content: flex-start; }
  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 14px; }
  .fieldMunicipio,
  .fieldAno,
  .fieldMes,
  .fieldUnidade,
  .fieldModulo,
  .filtersActions { grid-column: span 1; }
  .fieldModulo { grid-column: 1 / -1; }
  .filtersActions { justify-content: stretch; align-items: stretch; }
  .filtersActions button { flex: 1; }
  .exportBar { grid-template-columns: 1fr auto; }
  .tableTools { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .main { padding: 10px; }
  .hero,
  .filters,
  .exportPanel,
  .sectionHead,
  .grid2 .panel { padding: 14px; }
  .heroStatus { width: 100%; }
  .filters { grid-template-columns: 1fr; }
  .fieldMunicipio,
  .fieldAno,
  .fieldMes,
  .fieldUnidade,
  .fieldModulo,
  .filtersActions { grid-column: 1 / -1; }
  .filtersActions { flex-direction: column; }
  .filtersActions button { width: 100%; }
  .exportBar { grid-template-columns: 1fr; }
  #btnExportar { width: 100%; }
  .cards { grid-template-columns: 1fr; }
  .tableTools { grid-template-columns: 1fr; }
  .pager { flex-direction: column; align-items: stretch; }
  .pager button { width: 100%; }
  .topActions button { flex: 1; }
  .modalBox { padding: 20px 16px; }
}
