:root {
  --bg: #f4f6f9; --panel: #fff; --ink: #1f2933; --muted: #66788a;
  --line: #e1e6ed; --brand: #14507a; --brand-dark: #0e3a59;
  --ok: #1c7c4a; --warn: #b4690e; --danger: #b3261e;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14px; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.4rem; margin: .2rem 0 1rem; }
h2 { font-size: 1.05rem; margin: 1.2rem 0 .6rem; }
h3 { font-size: .95rem; margin: .6rem 0 .3rem; }

.topbar { display: flex; align-items: center; gap: 1rem; background: var(--brand);
  color: #fff; padding: .55rem 1rem; }
.topbar .brand a { color: #fff; font-weight: 700; font-size: 1.05rem; }
.topbar .brand .unit-name { font-weight: 500; font-size: .85rem; opacity: .85; margin-left: .4rem; }
.topbar .context { margin-left: auto; display: flex; gap: .4rem; align-items: center; }
.topbar .context .muted-light { color: #cfe3f2; font-size: .72rem; display: flex; flex-direction: column; gap: .15rem; }
.topbar select, .topbar input { padding: .25rem .4rem; border-radius: 5px; border: none; }
.userbox { display: flex; align-items: center; gap: .6rem; }
.userbox a { color: #cfe3f2; }
.role-badge { background: var(--brand-dark); padding: .1rem .5rem; border-radius: 10px;
  font-size: .72rem; text-transform: uppercase; letter-spacing: .03em; }

.mainnav { display: flex; gap: .2rem; background: var(--panel); padding: 0 1rem;
  border-bottom: 1px solid var(--line); }
.mainnav a { padding: .6rem .8rem; color: var(--ink); font-weight: 500; }
.mainnav a:hover { background: var(--bg); text-decoration: none; }

/* Cabecalho + menu fixos no topo ao rolar */
.appbar { position: sticky; top: 0; z-index: 100; box-shadow: 0 2px 6px rgba(16,24,40,.06); }

.content { max-width: 1200px; margin: 1.2rem auto; padding: 0 1rem; }

/* Listas com altura limitada e cabecalho fixo dentro do scroll */
.scroll-list { max-height: 360px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.scroll-list table.data { border: none; border-radius: 0; }
.scroll-list thead th { position: sticky; top: 0; z-index: 1; }

/* Scroll horizontal para grades largas (ex.: tabela de frete) */
.scroll-x { overflow-x: auto; }

/* Detalhe da viagem: usa 95% da largura da tela (page head, alertas e grid). */
.trip-wide { width: 95vw; position: relative; left: 50%; transform: translateX(-50%); }
.trip-wide .page-head { margin-bottom: .6rem; }

/* 3 colunas (dados | valores+historico | etapas), cada uma com altura limitada
   e rolagem interna - o conteudo cabe na tela sem rolagem vertical da pagina. */
.trip-grid { display: grid; grid-template-columns: 1.15fr 1fr 1fr; gap: 1rem;
  align-items: start; }
.trip-grid > .card { max-height: calc(100vh - 200px); overflow-y: auto; overflow-x: hidden; margin: 0; }

/* Dados operacionais sem rolagem lateral: colunas encolhem e campos ocupam 100%. */
.trip-grid .form-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.trip-grid fieldset.form-grid { min-width: 0; }
.trip-grid .form-grid input,
.trip-grid .form-grid select,
.trip-grid .form-grid textarea { width: 100%; min-width: 0; box-sizing: border-box; }

@media (max-width: 1000px) {
  .trip-wide { width: 100%; left: auto; transform: none; }
  .trip-grid { grid-template-columns: 1fr; }
  .trip-grid > .card { max-height: none; }
}

.etapas-col { display: flex; flex-direction: column; gap: .6rem; }

/* Dropdown de checkboxes (ex.: selecionar placas no cadastro de terceiros) */
.check-dd { position: relative; display: inline-block; min-width: 200px; }
.check-toggle { width: 100%; min-height: 34px; padding: .4rem 1.5rem .4rem .5rem; text-align: left;
  border: 1px solid var(--line); border-radius: 5px; background: var(--panel); color: var(--ink); cursor: pointer; }
.check-toggle::after { content: "v"; position: absolute; right: .5rem; color: var(--muted); }
.check-options { display: none; position: absolute; z-index: 30; top: calc(100% + 4px); left: 0;
  min-width: 220px; max-height: 260px; overflow: auto; background: var(--panel);
  border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 8px 24px rgba(16,24,40,.16); padding: .3rem; }
.check-dd.open .check-options { display: block; }
.check-opt { display: flex; align-items: center; gap: .4rem; padding: .3rem .35rem; border-radius: 4px; font-size: .85rem; }
.check-opt:hover { background: var(--bg); }
.check-opt input { width: auto; }

/* Disponibilidade */
.disp-wide { width: 95vw; position: relative; left: 50%; transform: translateX(-50%); }
.disp-bar { display: flex; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.disp-grid { display: grid; grid-template-columns: 280px 1fr; gap: 1rem; margin-top: .8rem; align-items: start; }
@media (max-width: 1000px) { .disp-wide { width:100%; left:auto; transform:none; } .disp-grid { grid-template-columns: 1fr; } }
.field-destaque { display: flex; flex-direction: column; gap: .25rem; font-weight: 700;
  background: #fff7e6; border: 2px solid var(--warn); border-radius: 8px; padding: .5rem .7rem; }
.field-destaque input { padding: .4rem .5rem; border: 1px solid var(--line); border-radius: 5px; }
.btn-salvar { font-size: 1rem; padding: .55rem 1.1rem; }
.veh-list { display: flex; flex-direction: column; gap: .4rem; max-height: 70vh; overflow: auto; }
.veh-chip { border: 1px solid var(--line); border-left: 4px solid var(--brand); border-radius: 6px;
  padding: .4rem .6rem; background: var(--panel); cursor: grab; font-size: .85rem; }
.veh-chip:hover { background: var(--bg); }
#disp-table select, #disp-table input { padding: .25rem .35rem; border: 1px solid var(--line); border-radius: 5px; }
#disp-table td { vertical-align: top; }
.helper-menu { position: relative; min-width: 150px; max-width: 220px; }
.helper-toggle { width: 100%; min-height: 30px; padding: .25rem 1.5rem .25rem .35rem;
  border: 1px solid var(--line); border-radius: 5px; background: var(--panel);
  color: var(--ink); text-align: left; cursor: pointer; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.helper-toggle::after { content: "v"; position: absolute; right: .45rem; top: .35rem;
  color: var(--muted); pointer-events: none; }
.helper-options { display: none; position: fixed; z-index: 1000; max-height: 220px; overflow: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
  box-shadow: 0 8px 24px rgba(16,24,40,.16); padding: .25rem; }
.helper-menu.open .helper-options, body > .helper-options { display: block; }
.helper-option { display: flex; align-items: center; gap: .35rem; padding: .3rem .35rem;
  border-radius: 4px; font-size: .84rem; color: var(--ink); cursor: pointer; }
.helper-option:hover { background: var(--bg); }
.helper-option input { width: auto; margin: 0; }
.helper-empty { padding: .35rem; color: var(--muted); font-size: .82rem; }
.etapa-card { border: 1px solid var(--line); border-left: 4px solid var(--line);
  border-radius: 8px; padding: .55rem .7rem; background: var(--panel); }
.etapa-card.etapa-entregue { border-left-color: var(--ok); }
.etapa-card.etapa-devolvido { border-left-color: var(--danger); }
.etapa-card.etapa-pendente { border-left-color: var(--warn); }
.etapa-head { display: flex; align-items: center; justify-content: space-between; }
.etapa-num { font-weight: 700; color: var(--muted); font-size: .8rem; }
.etapa-dest { font-weight: 600; font-size: .9rem; margin: .15rem 0; }
.etapa-status.pendente { background: #fbe9cf; color: var(--warn); }
.etapa-status.entregue { background: #d9efe2; color: var(--ok); }
.etapa-status.devolvido { background: #fde2e0; color: var(--danger); }
.etapa-vals { display: flex; justify-content: space-between; gap: .5rem; margin-top: .3rem; color: var(--ink); }
.etapa-form { margin-top: .4rem; }
.etapa-form select { width: 100%; padding: .3rem .4rem; border: 1px solid var(--line); border-radius: 5px; }

/* Popup de detalhes do historico (HTML <dialog>) */
.audit-dialog { border: 1px solid var(--line); border-radius: 10px; padding: 1rem 1.2rem;
  max-width: 520px; width: 90%; box-shadow: 0 10px 40px rgba(16,24,40,.25); }
.audit-dialog h3 { margin: 0 0 .6rem; font-size: .95rem; }
.audit-dialog::backdrop { background: rgba(16,24,40,.45); }

.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.page-head > div { display: flex; gap: .4rem; align-items: center; }

.cards { display: flex; flex-wrap: wrap; gap: .8rem; margin: 1rem 0; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: .7rem 1rem; min-width: 150px; display: flex; flex-direction: column; }
.kpi-label { color: var(--muted); font-size: .78rem; }
.kpi-value { font-size: 1.3rem; font-weight: 700; }
.kpi.warn .kpi-value { color: var(--warn); }

.shortcuts { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1rem 0; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1.2rem; }
@media (max-width: 880px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 1rem; margin: .6rem 0; }

table.data { width: 100%; border-collapse: collapse; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
table.data th, table.data td { text-align: left; padding: .45rem .6rem;
  border-bottom: 1px solid var(--line); }
table.data th { background: #eef2f7; font-size: .76rem; text-transform: uppercase;
  letter-spacing: .03em; color: var(--muted); }
table.data.small th, table.data.small td { padding: .3rem .5rem; font-size: .82rem; }
.row-warn { background: #fff7ec; }

table.kv td { padding: .3rem .5rem; border-bottom: 1px solid var(--line); }
table.kv td:first-child { color: var(--muted); width: 45%; }

.status { font-size: .72rem; padding: .12rem .5rem; border-radius: 10px; text-transform: uppercase;
  letter-spacing: .02em; background: #e4e9f0; color: #3a4754; }
.status.divergent, .status.unmatched { background: #fde2e0; color: var(--danger); }
.status.pending, .status.nao_encontrado, .status.duplicado { background: #fbe9cf; color: var(--warn); }
.status.checked, .status.matched, .status.retornado { background: #d9efe2; color: var(--ok); }
.status.approved { background: #d4e6f6; color: var(--brand); }
.status.closed { background: #d7dbe0; color: #43505c; }
.status.canceled { background: var(--danger); color: #fff; }

.btn { display: inline-block; padding: .4rem .8rem; border-radius: 6px; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); cursor: pointer; font-size: .85rem; }
.btn:hover { background: var(--bg); text-decoration: none; }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }

.filters { display: flex; flex-wrap: wrap; gap: .4rem; margin: .8rem 0; align-items: center; }
.filters input, .filters select { padding: .35rem .5rem; border: 1px solid var(--line); border-radius: 5px; }
.filters .check { display: flex; align-items: center; gap: .3rem; color: var(--muted); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .6rem 1rem; }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr; } }
.form-grid label { display: flex; flex-direction: column; gap: .2rem; font-size: .8rem; color: var(--muted); }
.form-grid input, .form-grid select, .form-grid textarea { padding: .4rem .5rem;
  border: 1px solid var(--line); border-radius: 5px; font-size: .9rem; color: var(--ink); }
.form-grid .full { grid-column: 1 / -1; }
.no-border { border: none; padding: 0; margin: 0; }

.inline-form { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.inline-form input, .inline-form select { padding: .4rem .5rem; border: 1px solid var(--line); border-radius: 5px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 1rem; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 1rem; }
.tile:hover { border-color: var(--brand); text-decoration: none; }
.tile h3 { margin: 0 0 .3rem; color: var(--brand); }

.muted { color: var(--muted); }
.small { font-size: .82rem; }
.alert { padding: .6rem .9rem; border-radius: 6px; margin: .8rem 0; background: #eef2f7; }
.alert.warn { background: #fbe9cf; color: var(--warn); }
.alert.error { background: #fde2e0; color: var(--danger); }
.alert.ok { background: #d9efe2; color: var(--ok); }
.log { background: #20262e; color: #e6edf3; padding: .7rem; border-radius: 6px; overflow:auto; font-size: .78rem; }
code { background: #eef2f7; padding: .1rem .35rem; border-radius: 4px; }

.login-wrap { max-width: 420px; margin: 4rem auto; }
.login-wrap label { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .8rem; }
.login-wrap input { padding: .5rem; border: 1px solid var(--line); border-radius: 6px; }
.login-wrap ul { margin: .4rem 0 0; padding-left: 1.1rem; }

/* Sub-abas (ex.: Terceiros: Fretes / Fechamento) */
.subtabs { display: flex; gap: .4rem; margin: .2rem 0 .8rem; border-bottom: 1px solid var(--line); }
.subtab { padding: .4rem .8rem; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; }
.subtab:hover { color: var(--text); }
.subtab.active { color: var(--text); border-bottom-color: var(--accent, #2f6fed); font-weight: 600; }
.linklike { background: none; border: none; color: var(--accent, #2f6fed); cursor: pointer; padding: 0; font: inherit; text-decoration: underline; }
table.data.compact th, table.data.compact td { padding: .25rem .4rem; font-size: .8rem; }
table.data tfoot th { background: #eef2f7; }
