:root {
  --bg: #0f1419;
  --panel: #1a222c;
  --line: #2a3542;
  --text: #e8eef4;
  --muted: #8b9aab;
  --accent: #3d8bfd;
  --ok: #2f6f4e;
  --bad: #8b3a3a;
  --warn: #8a6d1f;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: radial-gradient(1200px 600px at 10% -10%, #1b2838 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
  padding: 1.25rem 1.5rem 2rem;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.acoes { display: flex; gap: 0.5rem; flex-wrap: wrap; }

h1, h2 { margin: 0 0 0.25rem; letter-spacing: -0.02em; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.15rem; }
.sub { margin: 0; color: var(--muted); font-size: 0.9rem; }

.btn {
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: 0.65rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  text-decoration: none;
  display: inline-block;
}
.btn:disabled { opacity: 0.6; cursor: wait; }
.btn-secondary { background: #243041; border: 1px solid var(--line); }

.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.kpis article {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.75rem 0.9rem;
}
.kpis span { display: block; color: var(--muted); font-size: 0.8rem; }
.kpis strong { font-size: 1.35rem; }

.fila-box {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.75rem 0.9rem;
  margin-bottom: 1rem;
}
.fila-box strong { font-size: 0.95rem; }
.fila-chips { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.55rem; }
.chip {
  background: #243041;
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-size: 0.78rem;
}
.chip.agora { background: var(--warn); }

.erro {
  background: #3a2222;
  border: 1px solid var(--bad);
  color: #f3d0d0;
  padding: 0.75rem 0.9rem;
  border-radius: 8px;
  margin: 0 0 1rem;
}

.abas { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.tab {
  border: 1px solid var(--line);
  background: #243041;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 0.55rem 0.9rem;
  border-radius: 8px;
  cursor: pointer;
}
.tab.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.tab .n { opacity: 0.85; font-weight: 500; }

.tabela-wrap {
  overflow: visible;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}

table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { padding: 0.65rem 0.75rem; text-align: left; border-bottom: 1px solid var(--line); white-space: normal; }
td.ref { min-width: 0; }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; }
.th { display: flex; align-items: center; gap: 0.15rem; }
.th-nome, .th-filtro {
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  letter-spacing: inherit;
  text-transform: inherit;
  cursor: pointer;
  padding: 0;
}
.th-nome { flex: 1; text-align: left; }
.th-filtro { width: 1.1rem; color: var(--muted); }
.th-filtro.ativo, .th-nome.on { color: var(--accent); }
.filtro {
  position: fixed;
  z-index: 30;
  width: 16.5rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.filtro[hidden] { display: none; }
.filtro > button[data-ord], .filtro-tudo {
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  padding: 0.35rem 0.4rem;
  border-radius: 6px;
  cursor: pointer;
}
.filtro > button[data-ord]:hover, .filtro-lista label:hover { background: #223041; }
.filtro input[type="search"] {
  width: 100%;
  border: 1px solid var(--line);
  background: #121920;
  color: var(--text);
  font: inherit;
  border-radius: 6px;
  padding: 0.4rem 0.5rem;
}
.filtro-lista {
  max-height: 14rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.filtro-lista::-webkit-scrollbar { width: 8px; }
.filtro-lista::-webkit-scrollbar-thumb { background: #4c6278; border-radius: 999px; }
.filtro-lista label {
  display: flex;
  gap: 0.45rem;
  align-items: flex-start;
  padding: 0.28rem 0.4rem;
  cursor: pointer;
}
.filtro-acoes { display: flex; justify-content: flex-end; gap: 0.4rem; }
.filtro-acoes .btn { padding: 0.4rem 0.7rem; }

.config-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem;
  max-width: 40rem;
}
.config-card h2 { margin-bottom: 0.35rem; }
.config-form { display: flex; flex-direction: column; gap: 0.65rem; margin-top: 0.8rem; }
.config-form input {
  border: 1px solid var(--line);
  background: #121920;
  color: var(--text);
  font: inherit;
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
}
.config-nota { color: var(--muted); margin: 0.35rem 0 0.8rem; }
.config-busca { margin: 0.4rem 0 0.6rem; }
.config-lista {
  max-height: min(58vh, 34rem);
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.config-lista::-webkit-scrollbar { width: 8px; }
.config-lista::-webkit-scrollbar-thumb { background: #4c6278; border-radius: 999px; }
.origem {
  display: grid;
  grid-template-columns: auto 1fr auto 7.5rem;
  gap: 0.55rem;
  align-items: center;
  padding: 0.45rem 0.7rem;
  border-bottom: 1px solid var(--line);
}
.origem:last-child { border-bottom: 0; }
.origem select, .config-busca {
  border: 1px solid var(--line);
  background: #121920;
  color: var(--text);
  font: inherit;
  border-radius: 6px;
  padding: 0.35rem 0.45rem;
}
.config-busca { width: 100%; }
.origem .qtd { color: var(--muted); font-size: 0.8rem; }
.config-rodape { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.8rem; }
tbody tr.ok { cursor: pointer; }
tbody tr.ok:hover { background: #223041; }
tbody tr.espera { cursor: default; }
.vazio { cursor: default; color: var(--muted); text-align: center; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  font-size: 0.78rem;
  background: #243041;
}
.badge.carregar { background: var(--ok); }
.badge.nao { background: var(--bad); }
.badge.pronto { background: var(--ok); }
.badge.erro, .badge.sem { background: var(--bad); }
.badge.fila, .badge.consultando { background: var(--warn); }

body:has(dialog[open]) { overflow: hidden; }

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  width: min(1100px, calc(100vw - 2rem));
  max-height: calc(100vh - 1.5rem);
  overflow: hidden;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.62); }
.modal-card {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1rem;
  color: var(--text);
  max-height: calc(100vh - 1.5rem);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-top {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
  margin-bottom: 0.75rem;
  flex: 0 0 auto;
}
#mapa {
  height: min(280px, 32vh);
  flex: 0 0 auto;
  border-radius: 10px;
  border: 1px solid var(--line);
  margin-bottom: 0.75rem;
  background: #121820;
}
.modal-tabela {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: #4c6278 transparent;
}
.modal-tabela::-webkit-scrollbar { width: 8px; }
.modal-tabela::-webkit-scrollbar-track { background: transparent; }
.modal-tabela::-webkit-scrollbar-thumb {
  background: #4c6278;
  border-radius: 999px;
}
.modal-tabela table { table-layout: fixed; }
.modal-tabela th:nth-child(1) { width: 11.5rem; }
.modal-tabela th:nth-child(2) { width: 6.2rem; }
.modal-tabela th:nth-child(3) { width: 5.5rem; }
.modal-tabela thead th {
  position: sticky;
  top: 0;
  background: #1a222c;
  z-index: 1;
}
tr.perto-matriz td { background: rgba(224, 161, 0, 0.16); }
tr.perto-matriz td:first-child { box-shadow: inset 3px 0 0 #e0a100; }
a { color: var(--accent); }

.busca {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.9rem;
  margin-bottom: 1rem;
}
.busca label { display: flex; flex-direction: column; gap: 0.3rem; color: var(--muted); font-size: 0.8rem; }
.busca input {
  border: 1px solid var(--line);
  background: #121920;
  color: var(--text);
  font: inherit;
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
}
.busca input[type="text"] { width: 9rem; text-transform: uppercase; }
.percurso-grade {
  display: grid;
  grid-template-columns: minmax(16rem, 22rem) minmax(0, 1fr);
  gap: 0.75rem;
  align-items: start;
}
.dia-lista { display: flex; flex-direction: column; gap: 0.45rem; }
.dia {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  border-radius: 10px;
  padding: 0.7rem 0.8rem;
  cursor: pointer;
}
.dia.on { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.dia strong { display: block; font-size: 1.05rem; }
.dia span { display: block; color: var(--muted); font-size: 0.82rem; margin-top: 0.15rem; }
body.pagina-percurso #mapa {
  height: min(640px, 68vh);
  margin-bottom: 0.55rem;
}
.mapa-controles { display: flex; gap: 0.6rem; align-items: center; }
.mapa-controles .sub { flex: 1; }

@media (max-width: 800px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  body { padding: 1rem; }
  .percurso-grade { grid-template-columns: 1fr; }
}
