:root {
  --bg: #0d1117;
  --bg-panel: #151b23;
  --bg-elevated: #1c2530;
  --border: #2a3542;
  --text: #e6edf3;
  --text-dim: #8b98a5;
  --accent: #3fa9f5;
  --accent-dim: #1f6fa8;
  --green: #3fb950;
  --red: #f85149;
  --amber: #d29922;
  --gray-light: #b0b8c1;
  --radius: 10px;
}

* { box-sizing: border-box; }

* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background-color: var(--border); border-radius: 999px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background-color: var(--accent-dim); background-clip: padding-box; }
::-webkit-scrollbar-corner { background: transparent; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

.app { max-width: 1200px; margin: 0 auto; padding: 24px 20px 60px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 16px;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand-icon { font-size: 32px; transform: rotate(45deg); display: inline-block; color: var(--accent); }
.brand h1 { font-size: 20px; margin: 0; }
.brand p { font-size: 13px; color: var(--text-dim); margin: 2px 0 0; }

.btn {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 9px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: #232d3a; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #06121c; font-weight: 600; }
.btn-primary:hover { background: #58b8f7; }
.btn-ghost { background: transparent; }
.btn-close {
  background: none; border: none; color: var(--text-dim); font-size: 22px; cursor: pointer; line-height: 1;
}
.btn-close:hover { color: var(--text); }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.stat-card .value { font-size: 26px; font-weight: 700; color: var(--accent); }
.stat-card .label { font-size: 12.5px; color: var(--text-dim); margin-top: 4px; }

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 14px;
}
.panel-header h2 { font-size: 16px; margin: 0; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input, .filters select, select, input:not([type="checkbox"]):not([type="radio"]), textarea {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13.5px;
}
.filters input { min-width: 200px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left;
  color: var(--text-dim);
  font-weight: 600;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  cursor: default;
}
thead th[data-sort] { cursor: pointer; }
thead th[data-sort]:hover { color: var(--text); }
tbody td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
thead th:last-child, tbody td:last-child { text-align: right; }
.action-cell { display: flex; align-items: center; justify-content: flex-end; gap: 6px; flex-wrap: nowrap; white-space: nowrap; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--bg-elevated); }
.route-badge { font-family: 'Consolas', monospace; color: var(--accent); }
.route-badge-lg { font-size: 15px; }
.daynight-icon { display: inline-flex; vertical-align: middle; color: var(--text); opacity: .75; cursor: help; }
.daynight-icon svg { display: block; }

.redo-icon { display: inline-flex; vertical-align: middle; color: var(--amber); cursor: help; }
.redo-icon svg { display: block; }

.longhaul-icon { display: inline-flex; vertical-align: middle; color: #fff; cursor: help; }
.longhaul-icon svg { display: block; }
.accelerated-icon { display: inline-flex; vertical-align: middle; color: var(--accent); cursor: help; }
.accelerated-icon svg { display: block; }

.dur-under { color: var(--green); }
.dur-over { color: var(--red); }
.dur-fast { color: var(--accent); }

.btn-warn { background: var(--amber); border-color: var(--amber); color: #2b1c02; font-weight: 600; }
.btn-warn:hover { background: #e0a92e; }

.checkbox-inline {
  display: flex; align-items: center; gap: 8px;
  height: 36px;
  font-size: 13.5px;
  cursor: pointer;
}
.checkbox-inline input { width: 16px; height: 16px; flex-shrink: 0; cursor: pointer; accent-color: var(--accent); margin: 0; }

.status-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 7px;
  flex-shrink: 0;
}
.status-dot.status-concluido { background: var(--green); }
.status-dot.status-interrompido { background: var(--amber); }
.status-dot.status-nao_concluido { background: var(--red); }

.leg-status-concluido td:not(.route-badge) { color: var(--green) !important; }
.leg-status-interrompido td:not(.route-badge) { color: var(--amber) !important; }
.leg-status-nao_concluido td:not(.route-badge) { color: var(--red) !important; }
.muted { color: var(--text-dim); }

.pagination { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 14px; font-size: 13px; color: var(--text-dim); }

.modal-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 20px 22px;
}
.modal-wide { max-width: 1100px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-header-actions { display: flex; align-items: center; gap: 10px; }
.modal-header h3 { margin: 0; font-size: 17px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field-wide { grid-column: 1 / -1; }
.field-row { grid-column: 1 / -1; display: flex; gap: 14px; }
.field-row .field { flex: 1; min-width: 0; }
.datetime-pair { display: flex; gap: 6px; }
.datetime-pair input { flex: 1; min-width: 0; }
.field label { font-size: 12.5px; color: var(--text-dim); }
.field input:not([type="checkbox"]):not([type="radio"]), .field textarea { width: 100%; }
.hint { color: var(--text-dim); min-height: 14px; }

.autocomplete-field { position: relative; }
.autocomplete-list {
  position: absolute;
  top: 100%; left: 0; right: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 4px;
  max-height: 220px;
  overflow-y: auto;
  z-index: 20;
  display: none;
}
.autocomplete-list.open { display: block; }
.ac-item { padding: 8px 10px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border); }
.ac-item:last-child { border-bottom: none; }
.ac-item:hover { background: #26313f; }
.ac-item .code { color: var(--accent); font-weight: 700; margin-right: 6px; }
.ac-item .desc { color: var(--text-dim); }

.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 18px; }
.spacer { flex: 1; }

.detail-body { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-start; }
.detail-map { flex: 1 1 460px; flex-shrink: 0; height: 460px; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
/* min-width:0 evita que o conteudo interno (linhas de perna, textos longos)
   force o item flex a ficar mais largo que seu flex-basis, o que espremia o mapa. */
.detail-info { flex: 1 1 300px; min-width: 0; font-size: 14px; max-height: 460px; overflow-y: auto; padding-right: 4px; }
.detail-info h4 { margin: 14px 0 6px; font-size: 12.5px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; }
.detail-info h4:first-child { margin-top: 0; }
.detail-info .row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px dashed var(--border); }
.detail-info .row span:last-child { color: var(--text-dim); }

/* ---------- Roteiros ---------- */
.topbar-actions { display: flex; align-items: center; gap: 10px; }

#itineraries-tbody tr.itinerary-row { cursor: pointer; }
#itineraries-tbody tr.itinerary-row:hover { background: var(--bg-elevated); }
.itinerary-name-cell strong { font-size: 13.5px; }
.itinerary-name-cell .muted { display: block; margin-top: 2px; font-family: 'Consolas', monospace; font-size: 12px; }

.progress-cell { display: flex; align-items: center; gap: 10px; }
.progress-bar { width: 120px; height: 7px; border-radius: 999px; background: #0d141c; overflow: hidden; border: 1px solid var(--border); flex-shrink: 0; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--accent-dim), var(--accent)); border-radius: 999px; transition: width .2s; }
.progress-label { font-size: 12px; color: var(--text-dim); white-space: nowrap; }

/* Essa td (colspan=5, unica filha da linha) tambem bate com a regra global
   "tbody td:last-child" (mais especifica), que herdaria text-align:right para
   dentro da tabela aninhada. Forcamos de volta para left aqui. */
tbody tr.itinerary-expand-row > td.itinerary-expand-cell {
  padding: 0; background: #0d141c; border-bottom: 1px solid var(--border); text-align: left !important;
}
/* table-layout:fixed + colgroup: garante que cabecalho e linhas fiquem sempre
   na mesma grade de colunas, independente do conteudo de cada linha. */
.leg-mini-table { margin: 0; table-layout: fixed; }
.leg-mini-table thead th { font-size: 11px; padding: 8px 14px; }
.leg-mini-table tbody td {
  padding: 8px 14px; font-size: 13px; border-bottom: 1px dashed var(--border);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.leg-mini-table tbody tr:last-child td { border-bottom: none; }
.leg-mini-table tbody td:last-child { overflow: visible; white-space: normal; }
.leg-mini-table tbody td:last-child .btn { margin: 2px 0 2px 6px; padding: 6px 10px; font-size: 12px; }
.leg-pending-row td { color: var(--text-dim); }

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 28px; height: 28px;
  cursor: pointer;
  font-size: 13px;
  flex-shrink: 0;
  line-height: 1;
}
.btn-icon:hover:not(:disabled) { color: var(--text); background: #232d3a; }
.btn-icon:disabled { opacity: .3; cursor: default; }

/* Lista de pernas dentro do modal de detalhe do roteiro */
.leg-list { display: flex; flex-direction: column; }
.leg-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 12px;
  padding: 9px 2px;
  font-size: 13.5px;
  border-bottom: 1px dashed var(--border);
}
.leg-row:last-child { border-bottom: none; }
.leg-order {
  width: 22px; height: 22px; flex-shrink: 0;
  border-radius: 50%;
  background: #0d141c;
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 11.5px; color: var(--text-dim);
}
.leg-route { min-width: 110px; }
.leg-distance { color: var(--text-dim); min-width: 90px; line-height: 1.4; }
.leg-distance .muted { font-size: 11px; }
.leg-status { flex: 1; color: var(--text-dim); font-size: 12.5px; }
.leg-row.leg-status-concluido .leg-status { color: var(--green); }
.leg-row.leg-status-interrompido .leg-status { color: var(--amber); }
.leg-row.leg-status-nao_concluido .leg-status { color: var(--red); }
.leg-row .btn { padding: 6px 12px; font-size: 12.5px; }

.it-stops { display: flex; flex-direction: column; gap: 8px; }
.it-stop-row {
  display: flex;
  align-items: center;
  gap: 6px;
  position: relative;
}
.it-stop-row .stop-index {
  width: 22px; height: 22px; flex-shrink: 0;
  border-radius: 50%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 11.5px; color: var(--text-dim);
}
.it-stop-row input { flex: 1; }
.it-stop-row .autocomplete-list { left: 28px; }
.stop-reorder { display: flex; flex-direction: column; gap: 2px; flex-shrink: 0; }
.stop-reorder .btn-icon { width: 20px; height: 15px; font-size: 10px; }
.btn-remove-stop {
  background: transparent; border: 1px solid var(--border); color: var(--text-dim);
  width: 26px; height: 26px; border-radius: 7px; cursor: pointer; flex-shrink: 0;
}
.btn-remove-stop:hover { color: var(--red); border-color: var(--red); }

.quick-add-row { display: flex; gap: 8px; }
.quick-add-row input { flex: 1; }

.stop-warning {
  display: none;
  flex-shrink: 0;
  color: var(--amber);
  font-size: 15px;
  cursor: help;
}
.it-stop-row.invalid input { border-color: var(--amber); color: var(--amber); }
.it-stop-row.invalid .stop-warning { display: inline-block; }

.map-marker-badge {
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: #06121c;
  font-weight: 700;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #06121c;
  box-shadow: 0 0 0 1px var(--accent);
}
.map-marker-badge.next { background: var(--green); box-shadow: 0 0 0 1px var(--green); }
.map-marker-badge.pending { background: var(--gray-light); box-shadow: 0 0 0 1px var(--gray-light); }

/* Bloco "ICAO - Cidade/UF" + nome do aeroporto embaixo, usado no detalhe de voo e de roteiro */
.airport-block { display: flex; flex-direction: column; padding: 4px 0; border-bottom: 1px dashed var(--border); }
.airport-block .airport-line1 { display: flex; justify-content: space-between; gap: 8px; }
.airport-block .airport-line2 { color: var(--text-dim); font-size: 12.5px; margin-top: 2px; }

@media (max-width: 640px) {
  .form-grid { grid-template-columns: 1fr; }
  .field-row { flex-direction: column; gap: 12px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .progress-cell { flex-direction: column; align-items: flex-start; gap: 4px; }
}
