*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
}

h1, h2, h3 {
  margin: 0;
  font-weight: 600;
  letter-spacing: -0.01em;
}

h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }

p { margin: 0 0 .75rem; }

button {
  font-family: inherit;
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Botones ---------- */
.btn-primary,
.btn-secondary,
.btn-icon,
.btn-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font-size: 0.9rem;
  font-weight: 600;
  transition: transform var(--transition), background var(--transition), box-shadow var(--transition);
}

/* Cualquier icono SVG dentro de un boton del sistema queda siempre a
   este tamaño fijo — sin esta regla, un <svg> sin width/height propio
   se renderiza de forma inconsistente segun el navegador (a veces
   enorme, a veces casi invisible). */
.btn-primary svg,
.btn-secondary svg,
.btn-icon svg,
.btn-danger svg,
.btn-pdf svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  box-shadow: 0 6px 16px rgba(17, 17, 20, 0.22);
}

.btn-primary:hover { transform: translateY(-1px); }

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
}

.btn-secondary:hover { background: var(--border); }

.btn-icon {
  background: transparent;
  color: var(--text-muted);
  padding: 6px 10px;
  font-size: 1rem;
  border-radius: var(--radius-sm);
}

.btn-icon:hover {
  background: var(--surface-2);
  color: var(--text);
}

.btn-icon:disabled,
.btn-icon[disabled] {
  opacity: 0.35;
  cursor: not-allowed;
  background: transparent;
}

.btn-icon:disabled:hover { background: transparent; }

.btn-danger {
  background: var(--danger);
  color: #fff;
  box-shadow: 0 6px 16px rgba(215, 38, 61, 0.28);
}

.btn-danger:hover { transform: translateY(-1px); }

/* ---------- Formularios ---------- */
form label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  margin: 14px 0 6px;
}

/* Combinador directo (>), no descendiente: la intencion es quitarle el
   margen superior solo al label que es el primer campo de un formulario
   simple (modales). Con "form label:first-of-type" a secas, CUALQUIER
   label que sea el primero DENTRO DE SU PROPIO padre (ej. el primero de
   cada <div> de un .form-grid, o el que sigue suelto despues de un
   .form-grid dentro de un .form-section) tambien matcheaba y perdia su
   separacion, aunque no fuera visualmente el primer campo del formulario. */
form > label:first-of-type { margin-top: 0; }

input[type="text"],
input[type="password"],
input[type="email"],
input[type="date"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 0.92rem;
  font-family: inherit;
  transition: border-color var(--transition), box-shadow var(--transition);
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}

.form-hint {
  display: block;
  margin-top: 4px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ---------- Alertas ---------- */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  margin-bottom: 18px;
}

.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-ok    { background: var(--success-soft); color: var(--success); }
.alert-warning { background: var(--warning-soft); color: var(--warning); }

/* ---------- Tarjetas ---------- */
.card {
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-card);
  padding: 24px;
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 12px;
  flex-wrap: wrap;
}

/* ---------- Tablas ---------- */
/* overflow-x:auto permite deslizar la tabla cuando es mas ancha que
   la pantalla (moviles/tablets); el border-radius se conserva porque
   solo recortamos en el eje vertical. */
.table-card {
  padding: 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  min-width: 640px;
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  text-align: left;
  padding: 14px 20px;
  font-size: 0.88rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table th {
  color: var(--text);
  font-weight: 600;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--surface-2);
}

.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-2); }

/* Variante compacta: para tablas densas o jerarquicas (ej. Plan de Cuentas).
   Solo reduce el padding de las filas de datos — el encabezado mantiene
   el mismo alto en todas las tablas (compactas o no), tomando como
   referencia el de la tabla de Personas. */
.data-table--compact td {
  padding: 5px 16px;
}

/* width:1% + nowrap es el truco estandar para que esta columna se
   encoja a su contenido minimo y el resto del ancho sobrante de la
   tabla se lo quede la primera columna — asi las acciones siempre
   quedan pegadas al borde derecho, sin importar cuanto crezca o
   encoja el contenido de las demas columnas (ej. el arbol del Plan
   de Cuentas al expandir/colapsar niveles). */
.table-actions {
  width: 1%;
  white-space: nowrap;
  text-align: right;
}

/* En vez de "display:flex" (que en una <td> desalinea el borde de la
   fila cuando otra celda es mas alta, ej. Razon Social con 2 lineas),
   separamos los botones con margen entre hermanos: la celda se queda
   como una celda de tabla normal y la fila mide bien su altura. */
.table-actions > * + * {
  margin-left: 4px;
}

/* ---------- Badges ---------- */
.badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 600;
}

.badge-ok { background: var(--success-soft); color: var(--success); }
.badge-muted { background: var(--surface-2); color: var(--text-muted); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

/* Estado en linea: icono + texto sin fondo, para cuando el badge con
   pastilla de color se ve demasiado pesado (ej. listado de Personas). */
.estado-inline {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.76rem;
  font-weight: 600;
}

.estado-inline svg { width: 13px; height: 13px; }

.estado-inline-ok { color: var(--success); }
.estado-inline-off { color: var(--danger); }

.persona-nombre { font-weight: 600; }
.persona-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 3px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* ---------- Breadcrumb ---------- */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.breadcrumb a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
}

.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb .breadcrumb-icon { width: 16px; height: 16px; }
.breadcrumb .breadcrumb-icon svg { width: 100%; height: 100%; }

/* ---------- Tarjetas grandes de navegacion a sub-modulos ---------- */
.section-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 18px;
}

.section-card {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 26px 24px;
  box-shadow: var(--shadow-card);
  text-decoration: none;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 16px;
  border: 1px solid transparent;
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
  min-height: 150px;
}

.section-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-soft);
  border-color: var(--border);
}

.section-card-icon {
  width: 46px;
  height: 46px;
  border-radius: var(--radius-md);
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}

.section-card-icon svg { width: 24px; height: 24px; }

.section-card h3 { font-size: 1rem; margin-bottom: 2px; }
.section-card p { font-size: 0.83rem; color: var(--text-muted); margin: 0; }

.section-card.disabled {
  opacity: 0.45;
  pointer-events: none;
}

.section-card-tag {
  align-self: flex-start;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  background: var(--surface-2);
  padding: 4px 10px;
  border-radius: 999px;
}

/* ---------- Formularios: secciones y switches ---------- */
.form-section {
  padding-bottom: 26px;
  margin-bottom: 26px;
  border-bottom: 1px solid var(--border);
}

.form-section:last-of-type { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }

.form-section h2 { margin-bottom: 4px; }
.form-section .form-section-hint { color: var(--text-muted); font-size: 0.83rem; margin-bottom: 18px; }

/* Subtitulo liviano para agrupar campos relacionados dentro de un
   modal (que ya tiene su propio <h2> de titulo, por eso no reutiliza
   .form-section) — mismo estilo de las cabeceras de tabla: mayusculas,
   tenue, con una linea arriba para separar del grupo anterior. */
.modal-section-title {
  margin: 22px 0 4px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}

/* first-of-type (no first-child): los forms suelen empezar con inputs
   hidden (csrf_token, accion, id) antes del primer titulo de seccion. */
.modal-section-title:first-of-type {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

/* row-gap explicito: cada campo va envuelto en su propio <div>, asi
   que su label es "el primero" dentro de ESE div y la regla general
   "form label:first-of-type { margin-top:0 }" termina aplicando a
   todas las etiquetas de la grilla (no solo a la primera del
   formulario) — sin este row-gap, las filas quedan pegadas entre si. */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 22px 20px;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.switch-row:last-child { border-bottom: none; }

.switch-row-text { display: flex; flex-direction: column; gap: 2px; }
.switch-row-label { font-size: 0.9rem; font-weight: 600; }
.switch-row-hint { font-size: 0.78rem; color: var(--text-muted); }

.switch {
  position: relative;
  display: inline-block;
  width: 42px;
  height: 24px;
  flex-shrink: 0;
}

.switch input { opacity: 0; width: 0; height: 0; }

.switch-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background var(--transition);
}

.switch-slider::before {
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform var(--transition);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.switch input:checked + .switch-slider { background: var(--accent); }
.switch input:checked + .switch-slider::before { transform: translateX(18px); }

.conditional-field { padding-top: 14px; }

.logo-preview {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 6px;
}

.logo-preview img {
  max-width: 140px;
  max-height: 70px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  padding: 8px;
  object-fit: contain;
}

/* Input de archivo: se oculta el <input type="file"> nativo (feo y no
   personalizable entre navegadores) y se usa un <label for="..."> con
   el mismo estilo de .btn-secondary para abrir el selector, mas un
   texto aparte con el nombre del archivo elegido. */
.file-input {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.file-input-native {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  opacity: 0;
}

.file-input label {
  margin: 0;
  cursor: pointer;
}

.file-input-name {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.form-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 26px;
}

/* ---------- Pestañas (tabs) ---------- */
.tabs {
  display: flex;
  gap: 22px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  background: transparent;
  border: none;
  padding: 10px 2px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  position: relative;
  top: 1px;
}

.tab-btn:hover { color: var(--text); }

.tab-btn.active {
  color: var(--text);
  border-bottom-color: var(--active-bg);
}

.tab-panel[hidden] { display: none; }

/* ---------- Arbol jerarquico (Plan de Cuentas y similares) ---------- */
.tree-cell {
  display: flex;
  align-items: center;
  gap: 6px;
}

tr.fila-madre > td:first-child {
  font-weight: 700;
}

.tree-toggle {
  background: transparent;
  border: none;
  padding: 0;
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
}

.tree-toggle svg {
  width: 14px;
  height: 14px;
  transform: rotate(90deg);
  transition: transform var(--transition);
}

.tree-toggle.collapsed svg { transform: rotate(0deg); }

.tree-spacer {
  width: 18px;
  display: inline-block;
  flex-shrink: 0;
}

/* ---------- Boton de descarga PDF (rojo, reutilizable) ---------- */
.btn-pdf {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  border: none;
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-size: 0.82rem;
  font-weight: 600;
  background: var(--danger-soft);
  color: var(--danger);
  transition: background var(--transition), color var(--transition);
}

.btn-pdf:hover {
  background: var(--danger);
  color: #fff;
}

/* ---------- Autocompletado de cuenta (filtros y formularios) ---------- */
.autocomplete-wrap { position: relative; }

.autocomplete-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-soft);
  max-height: 240px;
  overflow-y: auto;
  z-index: 60;
  display: none;
}

.autocomplete-list.open { display: block; }

.autocomplete-item {
  padding: 9px 14px;
  font-size: 0.85rem;
  cursor: pointer;
}

.autocomplete-item:hover,
.autocomplete-item.active {
  background: var(--surface-2);
}

.autocomplete-empty {
  padding: 9px 14px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* ---------- Formulario de producto ---------- */
/* Dos columnas de campos que en pantallas angostas pasan a una sola. */
.form-cols-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 32px;
}

.form-subseccion {
  margin-top: 26px;
  padding-top: 22px;
  border-top: 1px solid var(--border);
}

/* Recuadro vacio en lugar de la vista previa cuando no hay imagen. */
.foto-vacia {
  width: 140px;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.78rem;
}

.foto-vacia[hidden],
.logo-preview img[hidden] { display: none; }

/* Dos campos en la misma linea (ej. Tipo + Subtipo). */
.campo-doble {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.form-grid--3 {
  grid-template-columns: repeat(3, 1fr);
  gap: 0 12px;
}

/* Sin overflow:hidden (a diferencia de .table-card) para que la lista
   del buscador de productos no quede recortada dentro de la tabla. */
.formula-table { min-width: 0; }
.formula-table td:first-child { width: 100%; white-space: normal; }

@media (max-width: 760px) {
  .form-cols-2 { grid-template-columns: 1fr; }
  .form-grid--3 { grid-template-columns: 1fr; }
  .formula-table th:nth-child(2),
  .formula-table td:nth-child(2) { width: 110px !important; }
  .formula-table th:nth-child(3),
  .formula-table td:nth-child(3) { display: none; }
}

/* ---------- Aviso flotante (toast) ---------- */
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 400;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 40px);
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  border-left: 4px solid var(--success);
  font-size: 0.9rem;
  font-weight: 600;
  animation: modal-in 0.25s ease;
  transition: opacity 0.4s ease, transform 0.4s ease;
}

.toast svg { width: 18px; height: 18px; flex-shrink: 0; }
.toast-ok svg { color: var(--success); }
.toast-hide { opacity: 0; transform: translateY(-8px); }

/* ---------- Grupos colapsables (Productos por categoria) ---------- */
.grupo-card { margin-bottom: 14px; }

.grupo-head {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 16px 20px;
  background: transparent;
  border: none;
  text-align: left;
  font: inherit;
  color: var(--text);
  cursor: pointer;
}

.grupo-head:hover { background: var(--surface-2); }

.grupo-chevron {
  display: inline-flex;
  color: var(--text-muted);
}

.grupo-chevron svg {
  width: 16px;
  height: 16px;
  transform: rotate(90deg);
  transition: transform var(--transition);
}

.grupo-card.collapsed .grupo-chevron svg { transform: rotate(0deg); }

.grupo-titulo {
  flex: 1;
  font-weight: 700;
  font-size: 0.95rem;
}

.grupo-body { border-top: 1px solid var(--border); }
.grupo-card.collapsed .grupo-body { display: none; }

/* ---------- Libro diario (Asientos) ---------- */
.asiento-block { margin-bottom: 40px; }

.asiento-toolbar {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-bottom: 8px;
}

.fila-encabezado-asiento td {
  text-align: center;
  color: var(--text);
  font-size: 0.82rem;
  background: var(--surface-2);
  line-height: 1.5;
}

/* Especificidad reforzada a proposito: sin esto, la regla de padding
   de .data-table--compact (mismo tipo de selector: clase + td) le
   gana a esta y la sangria del Haber desaparece. */
.data-table td.linea-haber { padding-left: 26px; }

.fila-glosa td {
  border-top: 2px solid var(--border);
  font-size: 0.85rem;
}

.fila-glosa .glosa-wrap {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.fila-glosa .glosa-total-label {
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}

.fila-totales td {
  background: var(--surface-2);
  font-weight: 700;
}

.btn-primary:disabled,
.btn-primary[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

.diferencia-ok { color: var(--success); font-weight: 600; }
.diferencia-mal { color: var(--danger); font-weight: 600; }

/* ---------- Modal ---------- */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(17, 17, 20, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  z-index: 300;
  padding: 20px;
}

.modal-backdrop.open { display: flex; }

.modal {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 28px;
  width: 100%;
  max-width: 420px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  box-shadow: var(--shadow-soft);
  animation: modal-in 0.2s ease;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 22px;
}

/* ---------- Paginacion de listados ---------- */
.paginacion {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 16px;
}

.paginacion-info,
.paginacion-actual {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.paginacion-botones {
  display: flex;
  align-items: center;
  gap: 10px;
}
