:root {
  --navy: #003f71;
  --blue: #0a4c97;
  --green: #bcd236;
  --ink: #1f2933;
  --muted: #5f6b76;
  --line: #d5dce3;
  --bg: #f3f6f9;
  --danger: #a61b1b;
  --radius: 6px;
}

* { box-sizing: border-box; }

html { font-size: 16px; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.45;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.wrap { width: 100%; max-width: 1400px; margin: 0 auto; padding: 0 1rem; }
main.wrap { flex: 1; padding-top: 1rem; padding-bottom: 2rem; }

.skip-link {
  position: absolute; left: -999px; top: 0; background: #fff; color: var(--navy); padding: .5rem 1rem; z-index: 10;
}
.skip-link:focus { left: 0; }

a { color: var(--blue); }
a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, .table-scroll:focus-visible {
  outline: 3px solid var(--green); outline-offset: 2px;
}

/* Cabecera */
.site-header { background: var(--navy); color: #fff; border-bottom: 4px solid var(--green); }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; padding-top: .5rem; padding-bottom: .5rem; }
.brand img { display: block; height: 57px; width: auto; }
.main-nav { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
.main-nav a, .nav-button { color: #fff; text-decoration: none; font-weight: 600; background: none; border: 0; padding: .35rem 0; cursor: pointer; font: inherit; font-weight: 600; }
.main-nav a:hover, .nav-button:hover { text-decoration: underline; }
.inline-form { display: inline; margin: 0; }
.lang-switch { display: inline-flex; border: 1px solid rgba(255,255,255,.5); border-radius: var(--radius); overflow: hidden; }
.lang-switch a { padding: .25rem .65rem; font-size: .85rem; }
.lang-switch a.active { background: var(--green); color: var(--navy); }
.lang-switch a:hover { text-decoration: none; background: rgba(255,255,255,.15); }
.lang-switch a.active:hover { background: var(--green); }

.welcome { display: flex; align-items: baseline; flex-wrap: wrap; gap: .1rem .5rem; margin: .35rem 0 1rem; padding: .15rem 0 .15rem .75rem; border-left: 4px solid var(--green); }
.welcome-label { font-size: 1.05rem; color: var(--muted); }
.welcome-name { font-size: 1.45rem; font-weight: 700; color: var(--navy); letter-spacing: -.01em; }

/* Tarjetas */
.card { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 1.25rem 1.5rem; margin-bottom: 1rem; box-shadow: 0 1px 2px rgba(16, 42, 67, .06), 0 4px 12px rgba(16, 42, 67, .04); }
.card-narrow { max-width: 460px; margin-left: auto; margin-right: auto; margin-top: 2rem; }
.card h1 { margin: 0 0 1rem; font-size: 1.5rem; color: var(--navy); }
.card h2 { margin: 0 0 .75rem; font-size: 1.2rem; color: var(--navy); }
.card-header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .75rem; }
.card-header h1 { margin: 0; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* Formularios */
.filter-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; align-items: start; }
.filter-form fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .75rem 1rem; margin: 0; }
.filter-form legend { font-weight: 600; color: var(--navy); padding: 0 .4rem; }
.text-fields { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 1rem; }
.field-group { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .6rem; }
.field-group:last-child { margin-bottom: 0; }
label { font-weight: 600; font-size: .92rem; }
.radio { display: block; font-weight: 400; margin-bottom: .25rem; }
input[type=text], input[type=password], input[type=date], select {
  font: inherit; padding: .5rem .6rem; border: 1px solid #aab4be; border-radius: var(--radius); background: #fff; width: 100%;
}
small { color: var(--muted); }
.form-actions { grid-column: 1 / -1; display: flex; gap: .5rem; flex-wrap: wrap; }

.btn {
  display: inline-block; font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  padding: .5rem 1rem; border-radius: var(--radius); border: 1px solid var(--navy); background: #fff; color: var(--navy);
  transition: background-color .15s, border-color .15s, box-shadow .15s;
}
.btn .icon { vertical-align: -3px; margin-right: .15rem; }
.btn:hover { background: #e9f0f7; }
.btn-primary { background: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--blue); }
.btn-small { padding: .2rem .6rem; font-size: .85rem; white-space: nowrap; }

.alert { padding: .75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.alert ul { margin: 0; padding-left: 1.2rem; }
.alert-error { background: #fdecea; border: 1px solid #e3a3a0; color: var(--danger); }

/* Tablas */
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.data-table { border-collapse: collapse; width: 100%; font-size: .9rem; }
.data-table th, .data-table td { padding: .5rem .65rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
.data-table thead th { background: var(--navy); color: #fff; position: sticky; top: 0; white-space: nowrap; }
.data-table tbody tr:nth-child(even) { background: #f7f9fb; }
.data-table tbody tr:hover { background: #eaf1f8; }
.data-table td { white-space: nowrap; }
.data-table td.note, .data-table td.wrap-text { white-space: pre-line; }
.row-actions { display: flex; gap: .3rem; }
.btn-disabled { opacity: .45; cursor: not-allowed; border-style: dashed; }
.btn-disabled:hover { background: #fff; }
.data-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table td.note { min-width: 12rem; }
.sort-link { color: #fff; text-decoration: none; }
.sort-link:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 1rem 0; }

.pager { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin: .5rem 0 .75rem; }
.pager-total { margin: 0; font-weight: 600; }
.page-size { display: flex; align-items: center; gap: .4rem; }
.page-size select { width: auto; padding: .25rem .4rem; }
.page-links { display: flex; align-items: center; gap: .25rem; flex-wrap: wrap; margin-left: auto; }
.page-links a, .page-links .current { padding: .25rem .6rem; border: 1px solid var(--line); border-radius: var(--radius); text-decoration: none; background: #fff; }
.page-links .current { background: var(--navy); border-color: var(--navy); color: #fff; font-weight: 600; }
.page-links .gap { padding: 0 .25rem; }
.back { margin: 1rem 0 0; }

/* Detalle y documentos */
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .75rem 1.5rem; margin: 0; }
.summary dt { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.summary dd { margin: 0; font-weight: 600; word-break: break-word; }
.doc-list { list-style: none; margin: 0; padding: 0; }
.doc-list li { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; padding: .6rem 0; border-bottom: 1px solid var(--line); }
.doc-name { word-break: break-all; }
.doc-actions { display: flex; gap: .5rem; }

/* Pie */
.site-footer { background: var(--navy); color: #dbe6f1; margin-top: auto; padding: 1.25rem 0; font-size: .88rem; }
.footer-inner { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; }
.footer-inner address { font-style: normal; }
.site-footer a { color: #fff; }
.legal-links { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: .25rem 1rem; }

@media (max-width: 640px) {
  .header-inner { justify-content: center; }
  .main-nav { justify-content: center; }
  .card { padding: 1rem; }
  .page-links { margin-left: 0; }
}

/* ---- Grid de Expedientes: compacta, con iconos SVG y acciones fijas a la derecha (no afecta a otras tablas) ---- */
.grid-expedientes { font-size: .85rem; border-collapse: separate; border-spacing: 0; }
.grid-expedientes th, .grid-expedientes td { padding: .35rem .4rem; }
.grid-expedientes thead th { white-space: normal; vertical-align: bottom; line-height: 1.2; min-width: 0; }
.grid-expedientes tbody td { vertical-align: middle; }

/* Acciones: última columna, fija al desplazar horizontalmente */
.grid-expedientes .col-actions {
  position: sticky; right: 0; z-index: 2; white-space: nowrap; background: #fff;
  box-shadow: -6px 0 6px -6px rgba(16, 42, 67, .35); border-left: 1px solid var(--line);
}
.grid-expedientes thead th.col-actions { background: var(--navy); z-index: 3; }
.grid-expedientes tbody tr:nth-child(even) td.col-actions { background: #f7f9fb; }
.grid-expedientes tbody tr:hover td.col-actions { background: #eaf1f8; }

.icon-group { display: inline-flex; gap: .1rem; }
.icon-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center; gap: .2rem;
  min-width: 34px; height: 34px; border-radius: 8px; border: 1px solid transparent; background: transparent;
  color: var(--blue); cursor: pointer; transition: background-color .15s, border-color .15s, color .15s;
}
.icon-btn:hover { background: #e3edf8; border-color: #c4d6ea; color: var(--navy); }
.icon-btn.is-disabled { color: #9aa7b4; cursor: not-allowed; }
.icon-btn.is-disabled:hover { background: transparent; border-color: transparent; color: #9aa7b4; }
.icon-btn::after, .filter-btn::after {
  content: attr(data-tip); position: absolute; right: calc(100% + 6px); top: 50%; transform: translateY(-50%);
  z-index: 6; width: max-content; max-width: 24ch; padding: .3rem .55rem; border-radius: 6px;
  background: var(--ink); color: #fff; font-size: .78rem; font-weight: 400; line-height: 1.25; white-space: normal; text-align: left;
  opacity: 0; visibility: hidden; pointer-events: none;
}
.icon-btn:hover::after, .icon-btn:focus-visible::after, .filter-btn:hover::after, .filter-btn:focus-visible::after { opacity: 1; visibility: visible; }
/* Los tooltips de las cabeceras salen hacia abajo para no quedar recortados por la tabla */
.filter-btn::after { right: auto; left: 50%; top: calc(100% + 6px); transform: translateX(-50%); }

/* Botón de filtro por columna */
.filter-btn {
  position: relative; flex: none; display: inline-flex; align-items: center; justify-content: center; width: 24px; height: 24px;
  padding: 0; border-radius: 6px; border: 1px solid transparent; background: transparent; color: #cfe0f1; cursor: pointer;
}
.filter-btn:hover, .filter-btn[aria-expanded="true"] { background: rgba(255, 255, 255, .18); color: #fff; }
.filter-btn.is-active { color: var(--green); }
.filter-btn.is-invalid { color: #ffb4ae; border-color: #ffb4ae; }
.filter-dot { position: absolute; top: 2px; right: 2px; width: 6px; height: 6px; border-radius: 50%; background: var(--green); }

/* Filtros activos */
.chip-bar { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; margin: 0 0 .6rem; }
.chip-title { font-weight: 600; color: var(--muted); font-size: .85rem; }
.chip { display: inline-flex; align-items: center; gap: .3rem; padding: .15rem .25rem .15rem .6rem; border-radius: 999px; background: #e3edf8; border: 1px solid #c4d6ea; font-size: .82rem; }
.chip.is-invalid { background: #fdecea; border-color: #e3a3a0; }
.chip-label { font-weight: 600; color: var(--navy); }
.chip-remove { display: inline-flex; align-items: center; justify-content: center; width: 22px; height: 22px; border-radius: 50%; color: var(--navy); }
.chip-remove:hover { background: rgba(0, 63, 113, .15); }

/* Paneles de filtro: sin JavaScript quedan visibles como formularios; con JavaScript son desplegables */
.filter-panels { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: .75rem; margin-top: .75rem; }
.filter-panel { display: flex; flex-direction: column; gap: .35rem; padding: .85rem; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.filter-panel h3 { margin: 0 0 .15rem; font-size: .95rem; color: var(--navy); }
.filter-panel label { font-size: .82rem; }
.panel-actions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .35rem; }
html.js .filter-panels { display: block; margin: 0; }
html.js .filter-panel {
  position: fixed; z-index: 50; width: 280px; max-width: calc(100vw - 16px); box-shadow: 0 10px 30px rgba(16, 42, 67, .25);
}
html.js .filter-panel[hidden] { display: none; }
@media (max-width: 640px) {
  html.js .filter-panel { left: 0; right: 0; bottom: 0; top: auto; width: auto; max-width: none; max-height: 80vh; overflow: auto; border-radius: 14px 14px 0 0; padding: 1rem; }
}

/* ---- Ancho completo en Expedientes (y documentos): márgenes laterales pequeños ---- */
body.wide .wrap { max-width: none; padding-left: .75rem; padding-right: .75rem; }
body.wide .card { padding: 1rem; }
.grid-expedientes { width: 100%; }
/* Distribución fija: los anchos no dependen del contenido de cada página.
   Columnas cortas: ancho fijo ajustado a cada idioma. Estado, Referencia, Transportista y Barco:
   porcentaje (Estado más ancho en escritorio para verse en una línea). Compañía absorbe el resto. */
.grid-expedientes {
  table-layout: fixed; min-width: 1365px;
  --w-flow: 63px; --w-adate: 92px; --w-atime: 84px; --w-ddate: 90px; --w-dtime: 84px;
  --w-trailer: 97px; --w-tractor: 87px; --w-insp: 103px; --w-actions: 146px;
  --w-state: 9.6%; --w-ref: 7.5%; --w-carrier: 8.9%; --w-ship: 5%;
}
html[lang="en"] .grid-expedientes {
  --w-atime: 78px; --w-ddate: 100px; --w-dtime: 100px; --w-trailer: 76px; --w-tractor: 81px; --w-insp: 101px;
}
@media (min-width: 1600px) {
  .grid-expedientes { --w-state: 13%; --w-ref: 8%; --w-carrier: 9.5%; --w-ship: 5.5%; }
}
.grid-expedientes thead th:nth-child(1) { width: var(--w-flow); }
.grid-expedientes thead th:nth-child(2) { width: var(--w-state); }
.grid-expedientes thead th:nth-child(3) { width: var(--w-adate); }
.grid-expedientes thead th:nth-child(4) { width: var(--w-atime); }
.grid-expedientes thead th:nth-child(5) { width: var(--w-ddate); }
.grid-expedientes thead th:nth-child(6) { width: var(--w-dtime); }
.grid-expedientes thead th:nth-child(7) { width: var(--w-ref); }
.grid-expedientes thead th:nth-child(8) { width: var(--w-trailer); }
.grid-expedientes thead th:nth-child(9) { width: var(--w-tractor); }
.grid-expedientes thead th:nth-child(10) { width: var(--w-carrier); }
.grid-expedientes thead th:nth-child(11) { width: var(--w-ship); }
.grid-expedientes thead th:nth-child(13) { width: var(--w-insp); }
.grid-expedientes thead th:nth-child(14) { width: var(--w-actions); }
/* Cabecera: título, indicador de orden y filtro forman un grupo compacto, centrado verticalmente */
.grid-expedientes thead th { vertical-align: middle; padding-left: .25rem; padding-right: .25rem; }
.th-inner { display: flex; align-items: center; justify-content: flex-start; gap: 3px; }
.th-inner > .sort-link, .th-inner > span { flex: 0 1 auto; min-width: 0; display: block; }
.th-inner > .filter-btn { flex: 0 0 20px; }
/* Estado: se envuelve solo entre palabras, nunca dentro de una palabra */
.grid-expedientes .cell-words { overflow-wrap: normal; word-break: normal; hyphens: manual; }
.grid-expedientes tbody td { white-space: normal; }
.grid-expedientes tbody td.nowrap { white-space: nowrap; }
.grid-expedientes thead th { white-space: normal; }
.grid-expedientes .cell-text { display: block; overflow-wrap: anywhere; min-width: 0; }
.grid-expedientes thead th, .grid-expedientes thead th .sort-link { overflow-wrap: normal; word-break: normal; }
.grid-expedientes th, .grid-expedientes td { padding: .35rem .3rem; }
.grid-expedientes thead th { font-size: .8rem; }
.grid-expedientes .filter-btn { width: 20px; height: 20px; }
.grid-expedientes .icon-btn { min-width: 32px; height: 32px; }
.grid-expedientes .icon-group { gap: 0; }
.icon-xlsx { padding: 0 .35rem; color: #1d6f42; }
.icon-xlsx:hover { background: #e4f3ea; border-color: #b7dcc5; color: #14532d; }
.xlsx-badge { font-size: .62rem; font-weight: 700; letter-spacing: .02em; line-height: 1; padding: .12rem .25rem; border-radius: 3px; background: #1d6f42; color: #fff; }

/* ---- Móvil/tableta estrecha: la grid pasa a fichas (datos, acciones y controles de orden/filtro conservados) ---- */
@media (max-width: 820px) {
  .table-scroll { border: 0; overflow: visible; }
  .grid-expedientes { table-layout: auto; min-width: 0; }
  .grid-expedientes thead th { width: auto !important; }
  .th-inner > .sort-link, .th-inner > span { min-height: 0; }
  .th-inner { flex-direction: row; align-items: center; gap: .3rem; }
  .grid-expedientes, .grid-expedientes thead, .grid-expedientes tbody, .grid-expedientes tr,
  .grid-expedientes td, .grid-expedientes th { display: block; }
  .grid-expedientes thead tr { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .75rem; }
  .grid-expedientes thead th { display: inline-flex; position: static; padding: .2rem .3rem .2rem .65rem; border-radius: 999px; font-size: .8rem; }
  .grid-expedientes thead th.col-actions { display: none; }
  .grid-expedientes tbody tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: .4rem .9rem; margin-bottom: .75rem; padding: .7rem .8rem;
    background: #fff; border: 1px solid var(--line); border-radius: 10px;
  }
  .grid-expedientes tbody tr:nth-child(even), .grid-expedientes tbody tr:hover { background: #fff; }
  .grid-expedientes tbody td { padding: 0; border: 0; white-space: normal; }
  .grid-expedientes tbody td::before {
    content: attr(data-label); display: block; font-size: .7rem; letter-spacing: .03em; text-transform: uppercase; color: var(--muted);
  }
  .grid-expedientes tbody td.col-actions {
    grid-column: 1 / -1; order: 99; position: static; width: auto; box-shadow: none; border: 0; border-top: 1px solid var(--line);
    padding-top: .5rem; background: transparent !important;
  }
  .grid-expedientes tbody td.col-actions::before { display: none; }
  .grid-expedientes tbody td.empty { grid-column: 1 / -1; }
  .grid-expedientes tbody td.empty::before { display: none; }
}

/* ---- Documentos: lista y vista previa integrada ---- */
.doc-layout { display: block; }
.doc-layout.has-viewer { display: grid; grid-template-columns: minmax(300px, 380px) minmax(0, 1fr); gap: 1rem; align-items: start; }
.doc-layout.has-viewer .doc-list li { flex-direction: column; align-items: stretch; gap: .35rem; }
.doc-layout.has-viewer .doc-list li[aria-current="true"] { background: #eef4fb; box-shadow: inset 3px 0 0 var(--blue); padding-left: .6rem; }
.doc-actions { flex-wrap: wrap; align-items: center; }
.doc-nopreview { font-size: .8rem; }
.doc-viewer { border: 1px solid var(--line); border-radius: 10px; background: #f7f9fb; padding: .75rem; position: sticky; top: .5rem; }
.viewer-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .6rem; }
.viewer-heading { display: flex; flex-direction: column; min-width: 0; }
.viewer-title { overflow-wrap: anywhere; }
.viewer-count { font-size: .8rem; }
.viewer-actions { display: flex; flex-wrap: wrap; gap: .35rem; }
.viewer-actions .btn[disabled] { opacity: .45; cursor: not-allowed; }
.viewer-stage { display: flex; align-items: center; justify-content: center; height: min(78vh, 900px); min-height: 320px; background: #fff; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.viewer-frame { width: 100%; height: 100%; border: 0; }
.viewer-image { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-message { padding: 1.5rem; text-align: center; color: var(--muted); }
.viewer-error { color: var(--danger); }
.viewer-message .btn { margin-top: .5rem; }
.viewer-hint { margin: .5rem 0 0; font-size: .8rem; }
@media (max-width: 820px) {
  .doc-layout.has-viewer { grid-template-columns: 1fr; }
  .doc-viewer { position: static; }
  .viewer-stage { height: 70vh; }
}

.btn-excel { display: inline-flex; align-items: center; gap: .45rem; }
.btn-excel .icon { margin: 0; color: #1d6f42; }
.btn-excel:hover .icon { color: #14532d; }

/* Selector de idioma con banderas */
.lang-switch a { display: inline-flex; align-items: center; gap: .35rem; }
.lang-switch .flag { display: block; border-radius: 2px; flex: none; }

/* Volver (zona superior de documentos) */
.back-top { margin: 0 0 .75rem; }
.btn-back { display: inline-flex; align-items: center; gap: .4rem; padding: .35rem .7rem .35rem .5rem; border-radius: var(--radius); font-weight: 600; text-decoration: none; color: var(--navy); background: #eef4fb; border: 1px solid #c4d6ea; transition: background-color .15s; }
.btn-back:hover { background: #dfeaf7; }
