:root {
  --navy: #17135f; --navy-light: #2e2a86; --navy-dark: #0b0936;
  --coffee: #2d2925; --coffee-light: #4a4138;
  --cream: #fffaf1; --cream-dark: #f3ead7; --border: #e5d7bd;
  --text: #1f2937; --text-muted: #6b7280;
  --pos: #16a34a; --neg: #dc2626; --warn: #f59e0b; --gold: #f6a800; --red: #b31e2d;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #f5f2ec; color: var(--text); font-size: 14px; line-height: 1.4; }
.hidden { display: none !important; }

/* ---------- Loading ---------- */
#loading { position:fixed; inset:0; z-index:9999; display:flex; align-items:center; justify-content:center; flex-direction:column; background:#214e3b; color:#fff; opacity:1; transition:opacity .22s ease; }
#loading.hidden { opacity: 0; pointer-events: none; display:flex; }
#loading .loading-logo { width: 96px; height: 96px; object-fit: contain; border-radius: 50%; background: white; padding: 6px; box-shadow: 0 8px 28px rgba(0,0,0,0.28); margin-bottom: 24px; }
#loading .msg { font-size: 20px; font-weight: 600; margin-bottom: 12px; }
#loading .detail { font-size: 13px; color: #a5b8d0; margin-bottom: 24px; min-height: 20px; }
#loading .bar { width: 320px; height: 8px; background: rgba(255,255,255,0.15); border-radius: 4px; overflow: hidden; }
#loading .bar-fill { height: 100%; background: linear-gradient(90deg, var(--gold), var(--coffee-light)); width: 30%; transition: width 0.3s; animation: pulse-bar 1.4s ease-in-out infinite; }
@keyframes pulse-bar { 0%,100% { width: 20%; } 50% { width: 80%; } }
#loading .err { max-width: 500px; margin-top: 20px; padding: 14px 20px; background: rgba(220, 38, 38, 0.2); border: 1px solid rgba(220, 38, 38, 0.5); border-radius: 6px; font-size: 13px; line-height: 1.5; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #214e3b; padding: 20px; }
.login-card { background: white; border-radius: 12px; padding: 40px 36px; max-width: 380px; width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.login-card .brand-logo { width: 72px; height: 72px; object-fit: contain; border-radius: 50%; display: block; margin: 0 auto 16px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); }
.login-card h1 { font-size: 20px; color: #214e3b; text-align: center; margin-bottom: 4px; }
.login-card .sub { font-size: 13px; color: var(--text-muted); text-align: center; margin-bottom: 28px; }
.login-card .err { background: #fee2e2; color: var(--neg); border: 1px solid #fca5a5; border-radius: 6px; padding: 10px 14px; font-size: 13px; margin-bottom: 16px; }
.password-wrap { position: relative; }
.password-wrap input { padding-right: 42px; }
.password-toggle { position: absolute; top: 50%; right: 6px; transform: translateY(-50%); background: transparent; border: none; padding: 4px 8px; cursor: pointer; font-size: 16px; line-height: 1; color: var(--text-muted); }
.password-toggle:hover { color: var(--navy); }
.login-forgot { display: block; text-align: center; margin-top: 14px; font-size: 12px; color: var(--text-muted); text-decoration: none; }
.login-forgot:hover { color: #214e3b; text-decoration: underline; }
.login-card .btn-primary { background: #214e3b; }
.login-card .btn-primary:hover { background: #183b2d; }

/* ---------- Forms ---------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 12px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.4px; margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; font-family: inherit; color: var(--text); background: white;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid rgba(23,19,95,0.25); border-color: var(--navy); }
.field textarea { resize: vertical; min-height: 80px; }
.field .hint { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.checklist { border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; max-height: 160px; overflow-y: auto; background: white; display: flex; flex-direction: column; gap: 6px; }
/* .checklist .checklist-item (2 clases) para ganarle a la especificidad de ".field label" */
.checklist .checklist-item { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 400; text-transform: none; letter-spacing: normal; color: var(--text); cursor: pointer; margin-bottom: 0; }
.checklist .checklist-item input { width: auto; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 4px 20px; }
.form-grid .field.full { grid-column: 1 / -1; }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 6px; font-family: inherit; font-size: 13px; font-weight: 700; cursor: pointer; border: 1px solid transparent; }
.btn-primary { background: var(--navy); color: white; }
.btn-primary:hover { background: var(--navy-light); }
.btn-secondary { background: white; color: var(--navy); border-color: var(--border); }
.btn-secondary:hover { background: var(--cream); }
.btn-danger { background: white; color: var(--neg); border-color: #fca5a5; }
.btn-danger:hover { background: #fee2e2; }
.btn-gold { background: var(--gold); color: var(--navy-dark); }
.btn-gold:hover { filter: brightness(1.05); }
.btn-call { background: #25d366; color: white; border-color: #1ebe57; }
.btn-call:hover { background: #1ebe57; }
.btn:disabled { opacity: 0.5; cursor: wait; }
.btn-icon { padding: 6px 9px; }
.btn-block { width: 100%; justify-content: center; }

/* ---------- Header / Nav ---------- */
header { background: #214e3b; color: white; padding: 16px 32px; display: flex; align-items: center; justify-content: space-between; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
header h1 { font-size: 20px; font-weight: 700; letter-spacing: 0.5px; display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 44px; height: 44px; object-fit: contain; border-radius: 50%; background: white; padding: 3px; box-shadow: 0 2px 8px rgba(0,0,0,0.22); }
.subtitle { color: #a5b8d0; font-size: 12px; margin-top: 2px; }
.header-actions { display: flex; gap: 10px; align-items: center; }
.header-user-tools { display: flex; align-items: center; gap: 8px; }
.header-user { font-size: 12px; color: #d7e0ee; text-align: right; }
.header-user b { color: white; display: block; font-size: 13px; }
.header-settings { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; border: 1px solid rgba(255,255,255,0.28); background: rgba(255,255,255,0.1); color: white; cursor: pointer; transition: background .15s ease, transform .15s ease; }
.header-settings:hover { background: rgba(255,255,255,0.22); transform: rotate(12deg); }
.header-settings:focus-visible { outline: 3px solid rgba(246,168,0,.75); outline-offset: 2px; }
.header-settings svg { width: 18px; height: 18px; fill: currentColor; }
.header-btn { background: rgba(255,255,255,0.1); text-decoration:none; display:inline-flex; align-items:center; color: white; border: 1px solid rgba(255,255,255,0.25); padding: 8px 14px; border-radius: 6px; font-family: inherit; font-size: 12px; font-weight: 700; cursor: pointer; }
.header-btn:hover { background: rgba(255,255,255,0.2); }

nav.tabs { background: white; border-bottom: 2px solid var(--border); padding: 0 32px; display: flex; gap: 2px; position: sticky; top: 0; z-index: 120; box-shadow: 0 1px 3px rgba(0,0,0,0.05); overflow-x: auto; white-space: nowrap; }
.tab-btn { padding: 14px 20px; cursor: pointer; background: transparent; border: none; border-bottom: 3px solid transparent; font-size: 14px; font-weight: 600; color: var(--text-muted); font-family: inherit; flex: 0 0 auto; }
.tab-btn:hover { color: var(--navy); background: var(--cream); }
.tab-btn.active { color: var(--navy); border-bottom-color: var(--gold); background: var(--cream); }

.filter-bar { background: rgba(255,255,255,0.97); border-bottom: 1px solid var(--border); padding: 10px 32px; display: flex; gap: 20px; align-items: center; flex-wrap: wrap; row-gap: 8px; position: sticky; top: 49px; z-index: 115; box-shadow: 0 2px 8px rgba(0,0,0,0.05); }
.filter-group { display: flex; align-items: center; gap: 8px; }
.filter-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 700; }
.filter-select { padding: 6px 28px 6px 12px; border: 1px solid var(--border); border-radius: 6px; background: white; font-size: 12px; font-family: inherit; color: var(--navy); font-weight: 700; cursor: pointer; }
.filter-select:focus { outline: 2px solid rgba(23,19,95,0.25); border-color: var(--navy); }

main { padding: 24px 32px 60px; max-width: 1680px; margin: 0 auto; }
.section { display: none; }
.section.active { display: block; }

h2.sec { font-size: 18px; color: var(--navy); margin-bottom: 16px; padding-bottom: 8px; border-bottom: 2px solid var(--gold); display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }

/* ---------- Sub-tabs (módulos con más de una entidad, ej. Personal) ---------- */
.subtabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.subtab-btn { padding: 8px 16px; background: transparent; border: none; border-bottom: 2px solid transparent; font-size: 13px; color: var(--text-muted); cursor: pointer; font-family: inherit; font-weight: 600; }
.subtab-btn:hover { color: var(--navy); }
.subtab-btn.active { color: var(--navy); border-bottom-color: var(--coffee); }
.subtab { display: none; }
.subtab.active { display: block; }
.entidad-toolbar { display: flex; justify-content: flex-end; margin-bottom: 12px; }
.link-archivo { color: var(--navy); font-weight: 600; }

/* ---------- Cards / tables ---------- */
.card { background: white; border-radius: 8px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); overflow-x: auto; }
table.data { width: 100%; font-size: 13px; border-collapse: collapse; }
table.data th, table.data td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #f0eee8; vertical-align: top; white-space: nowrap; }
table.data th { color: var(--text-muted); font-weight: 600; text-transform: uppercase; font-size: 10.5px; letter-spacing: 0.5px; background: var(--cream); }
/* Tablas desplazables con encabezados siempre visibles. */
.card:has(> table.data) {
  overflow: auto;
  max-height: calc(100vh - 190px);
  scrollbar-gutter: stable;
  overscroll-behavior: contain;
}
/* Las tablas operativas empiezan al borde: ninguna fila puede asomarse detrás del encabezado. */
.card:has(> table.data:first-child) { padding: 0; }
table.data thead th {
  position: sticky;
  top: 0;
  z-index: 4;
  box-shadow: 0 1px 0 var(--border);
}
table.data tr:hover td { background: #fafaf7; }
table.data td.actions { white-space: nowrap; text-align: right; width: 1%; }
table.data td.actions button { margin-left: 6px; }
table.data td.col-wrap { white-space: normal; word-break: break-word; min-width: 220px; max-width: 420px; }
.empty-state { padding: 40px 20px; text-align: center; color: var(--text-muted); }
.badge { display: inline-block; padding: 3px 10px; border-radius: 10px; font-size: 11px; font-weight: 700; background: var(--cream-dark); color: var(--coffee); white-space: nowrap; }
.badge-pos { background: #dcfce7; color: #166534; }
.badge-warn { background: #fef3c7; color: #92400e; }
.badge-neg { background: #fee2e2; color: #991b1b; }
.badge-info { background: #dbeafe; color: #1e40af; }
.badge-media { background: #ffedd5; color: #9a3412; }
.badge-baja { background: #fef9c3; color: #854d0e; }
.resumen-click { transition: transform .15s ease, box-shadow .15s ease, background .15s ease, outline-color .15s ease; }
.resumen-click:hover { transform: translateY(-1px); box-shadow: 0 4px 14px rgba(0,0,0,.08); }
.resumen-click.resumen-seleccionado {
  background: #e5e7eb;
  outline: 2px solid #9ca3af;
  outline-offset: -2px;
  box-shadow: 0 3px 10px rgba(0,0,0,.08);
}
.resumen-click.resumen-seleccionado .resumen-label { color: #374151; font-weight: 700; }
.resumen-click.resumen-seleccionado .resumen-label::after { content: " (Seleccionado)"; font-size: 11px; }
.badge-neutral { background: var(--cream-dark); color: var(--coffee); }
.texto-propio { color: var(--pos); font-weight: 700; }
tr.fila-completada td:not(.actions) { opacity: 0.55; }

/* Aging: fecha arriba, "hace X días" abajo en fuente chica para no ensanchar la columna. */
.aging-cell { display: flex; flex-direction: column; line-height: 1.15; }
.aging-cell .aging-days { font-size: 10.5px; font-weight: 600; margin-top: 2px; text-transform: none; letter-spacing: 0; }
.aging-pos { color: #16a34a; }
.aging-warn { color: #b45309; }
.aging-neg { color: #b91c1c; }

/* ---------- Modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 500; display: none; align-items: center; justify-content: center; padding: 20px; }
.modal-bg.active { display: flex; }
.modal { background: white; border-radius: 12px; max-width: 640px; width: 100%; max-height: 88vh; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.modal-head { background: var(--navy); color: white; padding: 16px 24px; display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { font-size: 16px; font-weight: 700; }
.modal-close { background: rgba(255,255,255,0.15); color: white; border: none; width: 32px; height: 32px; border-radius: 4px; cursor: pointer; font-size: 18px; font-family: inherit; }
.modal-close:hover { background: rgba(255,255,255,0.3); }
.modal-body { padding: 20px 24px; overflow-y: auto; }
.modal-foot { padding: 14px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; background: var(--cream); }

/* ---------- Toasts ---------- */
#toast-container { position: fixed; bottom: 20px; right: 20px; z-index: 9000; display: flex; flex-direction: column; gap: 8px; }
.toast { min-width: 260px; max-width: 380px; padding: 12px 16px; border-radius: 8px; font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,0.2); color: white; white-space: pre-line; }
.toast.ok { background: var(--pos); }
.toast.error { background: var(--neg); }

@media (max-width: 640px) {
  header { padding: 12px 16px; flex-direction: column; align-items: flex-start; gap: 10px; }
  .header-actions { width: 100%; flex-wrap: wrap; }
  .header-user-tools { margin-right: auto; }
  nav.tabs { padding: 0 8px; }
  main { padding: 14px 12px 40px; }
  .modal { max-width: 100%; }
}

/* Edición rápida: doble clic en campos simples */
td.celda-editable {
  cursor: text;
  position: relative;
  transition: background .15s ease, box-shadow .15s ease;
}
td.celda-editable:hover {
  background: #fffaf0;
  box-shadow: inset 0 0 0 1px rgba(196, 145, 34, .28);
}
td.celda-editable:hover::after {
  content: '✎';
  position: absolute;
  top: 4px;
  right: 5px;
  color: var(--gold);
  font-size: 11px;
  opacity: .8;
}
td.celda-editable.guardando {
  opacity: .65;
  pointer-events: none;
}
.inline-edit-control {
  width: 100%;
  min-width: 90px;
  border: 1px solid var(--gold);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
  padding: 7px 8px;
  outline: none;
  box-sizing: border-box;
}
.inline-edit-control:focus {
  box-shadow: 0 0 0 3px rgba(196, 145, 34, .14);
}

.checkbox-control { display:inline-flex !important; align-items:center; gap:9px; width:max-content; min-height:42px; padding:8px 12px; border:1px solid var(--border); border-radius:7px; background:#fff; color:var(--text) !important; font-size:14px !important; font-weight:600 !important; text-transform:none !important; letter-spacing:0 !important; cursor:pointer; }
.checkbox-control input { width:18px; height:18px; margin:0; accent-color:#214e3b; }

/* Administración: límites visuales claros y orden de hojas fácil de cambiar. */
.admin-permissions-wrap { overflow:auto; max-height:calc(100vh - 265px); border:1px solid #cbd5e1; border-radius:8px; }
table.admin-permissions-table { min-width:980px; border-collapse:separate; border-spacing:0; }
table.admin-permissions-table th,
table.admin-permissions-table td { border-right:1px solid #cbd5e1; border-bottom:1px solid #cbd5e1; }
table.admin-permissions-table th:last-child,
table.admin-permissions-table td:last-child { border-right:0; }
table.admin-permissions-table tbody tr:last-child td { border-bottom:0; }
table.admin-permissions-table thead th { background:#eef2ef; color:#214e3b; text-align:center; padding:12px 10px; }
table.admin-permissions-table thead th:first-child,
table.admin-permissions-table tbody td:first-child { position:sticky; left:0; z-index:5; background:#fffdf7; min-width:150px; }
table.admin-permissions-table thead th:first-child { z-index:7; background:#e7eee9; }
.admin-permission-cell { display:grid; grid-template-columns:repeat(2,minmax(72px,1fr)); gap:5px 9px; min-width:170px; padding:3px; font-size:11px; color:var(--text); }
.admin-permission-cell label { display:flex; align-items:center; gap:5px; margin:0; font-weight:500; text-transform:none; letter-spacing:0; white-space:nowrap; cursor:pointer; }
.admin-permission-cell input { width:15px; height:15px; margin:0; accent-color:#214e3b; }
.admin-card-heading { display:flex; align-items:center; justify-content:space-between; gap:16px; margin-bottom:14px; }
.admin-card-heading h3 { color:var(--navy); font-size:15px; margin-bottom:3px; }
.admin-module-order { list-style:none; display:flex; flex-direction:column; gap:7px; }
.admin-module-order li { display:flex; align-items:center; gap:10px; min-height:50px; padding:8px 10px; border:1px solid #d8dee3; border-radius:8px; background:#fbfcfb; }
.admin-order-handle { color:#94a3b8; font-size:18px; letter-spacing:-4px; }
.admin-order-label { flex:1; font-weight:700; color:#214e3b; }
.admin-order-actions { display:flex; gap:6px; }

/* Editor de contactos múltiples */
.contactos-editor { display:flex; flex-direction:column; gap:12px; }
.contacto-card { border:1px solid var(--border); border-left:4px solid var(--navy); border-radius:10px; padding:14px; background:#fff; }
.contacto-card-head { display:flex; align-items:center; justify-content:space-between; margin-bottom:10px; }
.contacto-card label, .contacto-subtitulo { display:block; font-size:12px; font-weight:700; color:var(--coffee); margin:10px 0 5px; }
.contacto-item { display:grid; grid-template-columns:minmax(0,1fr) auto; gap:6px; margin-bottom:6px; }
.contacto-add { padding:6px 10px; font-size:12px; margin-bottom:4px; }

/* Búsqueda instantánea en encabezados de módulos */
h2.sec .busqueda-inline { display:flex; align-items:center; gap:7px; margin-right:auto; color:#64748b; font-size:14px; font-weight:400; }
h2.sec .busqueda-input { width:min(280px,55vw); padding:8px 11px; border:1px solid var(--border); border-radius:7px; background:#fff; color:var(--text); font:inherit; outline:none; }
h2.sec .busqueda-input:focus { border-color:#214e3b; box-shadow:0 0 0 3px rgba(33,78,59,.12); }
@media (max-width:640px) { h2.sec .busqueda-inline { order:3; width:100%; } h2.sec .busqueda-input { width:100%; } }

/* Progreso de subida de documentos */
.upload-progress { margin-top:4px; padding:13px; border:1px solid #cbd5e1; border-radius:8px; background:#f8fafc; }
.upload-progress-head { display:flex; align-items:center; justify-content:space-between; gap:12px; margin-bottom:8px; color:#214e3b; }
.upload-progress-track { height:10px; overflow:hidden; border-radius:999px; background:#dbe4df; }
.upload-progress-fill { width:0; height:100%; border-radius:inherit; background:#214e3b; transition:width .35s ease; }
.upload-progress .hint { margin-top:7px; }
.upload-progress.error { border-color:#fca5a5; background:#fff7f7; }
.upload-progress.error .upload-progress-head { color:#b91c1c; }
.upload-progress.error .upload-progress-fill { background:#dc2626; }

/* Clientes y Proveedores: tablas compactas con espacio legible para contacto. */
#tabla-Clientes tbody td,
#tabla-Proveedores tbody td {
  font-size: 12px;
  line-height: 1.3;
  padding-top: 8px;
  padding-bottom: 8px;
}
#tabla-Clientes thead th,
#tabla-Proveedores thead th {
  font-size: 11px;
}
#tabla-Clientes .badge,
#tabla-Proveedores .badge {
  font-size: 10px;
  padding: 3px 7px;
}

table.data td.col-telefono-compacto {
  width: 150px;
  min-width: 130px;
  max-width: 210px;
  white-space: normal;
}
.telefonos-en-linea {
  display: inline;
  white-space: normal;
  line-height: 1.35;
}

#tabla-Clientes td.col-correo-compacto,
#tabla-Proveedores td.col-correo-compacto {
  width: 190px;
  min-width: 190px;
  max-width: 220px;
  overflow-wrap: anywhere;
  word-break: break-word;
}
