/* =============================================
   TABLAS COMO TARJETAS (teléfonos ≤600px)
   Cada fila se vuelve una tarjeta apilada y legible, sin scroll horizontal.
   ============================================= */
@media (max-width: 600px) {
  .table-wrapper {
    overflow-x: visible !important;
    border: none !important;
    background: transparent !important;
    box-shadow: none !important;
    padding: 0 !important;
  }
  .table-wrapper .table {
    min-width: 0 !important;
    width: 100% !important;
  }
  .table-wrapper .table thead {
    display: none; /* los encabezados se reemplazan por las etiquetas data-label */
  }
  .table-wrapper .table,
  .table-wrapper .table tbody,
  .table-wrapper .table tr,
  .table-wrapper .table td {
    display: block;
    width: 100%;
  }
  .table-wrapper .table tr {
    box-sizing: border-box;
    width: 100%;
    max-width: 100%;
    background: var(--bg-surface);
    border: none;
    border-left: 3px solid var(--border-strong); /* franja de acento; el color se define por tipo */
    border-radius: 4px var(--radius-md) var(--radius-md) 4px;
    box-shadow: none;
    margin-bottom: 0.7rem;
    padding: 0.3rem 1rem;
    overflow: hidden;
  }
  .table-wrapper .table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    text-align: right;
    padding: 0.6rem 0 !important;
    border: none !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05) !important; /* divisor sutil */
    background: transparent !important;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
    min-width: 0;
  }
  .table-wrapper .table td:last-child {
    border-bottom: none !important;
  }
  /* ----- Acento de color según el tipo de registro ----- */
  #tablaIngresos tr {
    border-left-color: var(--accent-green);
  }
  #tablaGastos tr {
    border-left-color: var(--accent-red);
  }
  #tablaGastosMensuales tr {
    border-left-color: var(--accent-amber);
  }
  #tablaResumenCerrado tr,
  #tablaResumenProximo tr,
  #tablaCuotasTerminadas tr {
    border-left-color: var(--accent-amber);
  }
  #tablaAhorros tr {
    border-left-color: var(--accent-violet);
  }
  .table-wrapper .table td[data-label]::before {
    content: attr(data-label);
    font-size: 0.62rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
    text-align: left;
    flex-shrink: 0;
  }
  .table-wrapper .table td.celda-acciones {
    justify-content: center;
    gap: 0.5rem;
    padding-top: 0.7rem !important;
  }
  .table-wrapper .table td[data-label="Próx. Pago"] {
    justify-content: stretch;
  }
  .table-wrapper .table td[data-label="Próx. Pago"] .btn {
    flex: 1;
  }
}

/* =============================================
   TARJETA COMPACTA-EXPANDIBLE (mobile ≤600px)
   Solo para Ingresos, Gastos y Ahorros.
   Resumen: Categoría + Monto (línea 1), Fecha + chevron (línea 2).
   Al tocar (.expandida): aparece Descripción + Editar/Borrar.
   ============================================= */
@media (max-width: 600px) {
  #tablaIngresos tr,
  #tablaGastos tr,
  #tablaAhorros tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    position: relative;
    cursor: pointer;
    padding: 0.85rem 1rem;
    min-height: 3.4rem;
    column-gap: 0.75rem;
    row-gap: 0.3rem;
  }
  /* Ocultar etiquetas (data-label::before) en el resumen compacto */
  #tablaIngresos tr td::before,
  #tablaGastos tr td::before,
  #tablaAhorros tr td::before {
    display: none;
  }
  /* ── Flex line-break: fuerza que Categoría y Descripción vayan a la línea 2 ── */
  #tablaIngresos tr::after,
  #tablaGastos tr::after,
  #tablaAhorros tr::after {
    content: "";
    order: 2;        /* después de Monto (order 2), antes de Categoría (order 3) */
    flex-basis: 100%;
    height: 0;
  }
  /* ── Línea 1: Fecha izq + Monto der (misma fila) ── */
  #tablaIngresos td[data-label="Fecha"],
  #tablaGastos td[data-label="Fecha"],
  #tablaAhorros td[data-label="Fecha"] {
    order: 1;
    flex: 1 1 auto;
    width: auto !important;
    justify-content: flex-start;
    border-bottom: none !important;
    padding: 0 !important;
    min-width: 0;
    font-size: 0.72rem;
    color: var(--text-muted);
  }
  #tablaIngresos td[data-label="Monto"],
  #tablaGastos td[data-label="Monto"],
  #tablaAhorros td[data-label="Monto"] {
    order: 2;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-end;
    border-bottom: none !important;
    padding: 0 !important;
    font-size: 1.05rem;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }
  /* ── Línea 2: Badge categoría + descripción truncada + chevron ── */
  #tablaIngresos td[data-label="Categoría"],
  #tablaGastos td[data-label="Categoría"],
  #tablaAhorros td[data-label="Categoría"] {
    order: 3;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-start;
    border-bottom: none !important;
    padding: 0 !important;
    min-width: 0;
  }
  #tablaIngresos td[data-label="Descripción"],
  #tablaGastos td[data-label="Descripción"],
  #tablaAhorros td[data-label="Descripción"] {
    order: 3;
    flex: 1 1 auto;
    width: auto !important;
    justify-content: flex-start;
    gap: 0;
    border-bottom: none !important;
    padding: 0 !important;
    text-align: left;
    font-size: 0.88rem;
    color: var(--text-secondary);
    overflow: hidden;
    min-width: 0;
  }
  /* El texto de descripción se trunca; el chevron siempre visible a la derecha */
  #tablaIngresos td[data-label="Descripción"] .desc-text,
  #tablaGastos td[data-label="Descripción"] .desc-text,
  #tablaAhorros td[data-label="Descripción"] .desc-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
    flex: 1 1 auto;
  }
  #tablaIngresos td[data-label="Descripción"]::after,
  #tablaGastos td[data-label="Descripción"]::after,
  #tablaAhorros td[data-label="Descripción"]::after {
    content: "▾";
    margin-left: 0.4rem;
    font-size: 0.95rem;
    color: var(--text-secondary);
    transition: transform 0.2s;
    flex-shrink: 0;
  }
  /* T. Cambio (Ahorros): oculto en resumen */
  #tablaAhorros td[data-label="T. Cambio"],
  #tablaIngresos td.celda-acciones,
  #tablaGastos td.celda-acciones,
  #tablaAhorros td.celda-acciones {
    display: none;
  }
  /* ── Al expandir: descripción completa ── */
  #tablaIngresos tr.expandida td[data-label="Descripción"] .desc-text,
  #tablaGastos tr.expandida td[data-label="Descripción"] .desc-text,
  #tablaAhorros tr.expandida td[data-label="Descripción"] .desc-text {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }
  /* Al expandir: rotar chevron */
  #tablaIngresos tr.expandida td[data-label="Descripción"]::after,
  #tablaGastos tr.expandida td[data-label="Descripción"]::after,
  #tablaAhorros tr.expandida td[data-label="Descripción"]::after {
    transform: rotate(180deg);
  }
  /* Al expandir: T. Cambio (Ahorros) */
  #tablaAhorros tr.expandida td[data-label="T. Cambio"] {
    display: flex;
    order: 4;
    flex-basis: 100%;
    width: 100%;
    justify-content: space-between;
    border-top: 1px solid rgba(255, 255, 255, 0.06) !important;
    border-bottom: none !important;
    margin-top: 0.4rem;
    padding-top: 0.55rem !important;
    text-align: right;
    font-size: 0.85rem;
  }
  #tablaAhorros tr.expandida td[data-label="T. Cambio"]::before {
    display: inline;
  }
  /* Fondo de selección al expandir */
  #tablaIngresos tr.expandida,
  #tablaGastos tr.expandida,
  #tablaAhorros tr.expandida {
    background: var(--bg-hover) !important;
  }
  /* Los td deben permanecer transparentes (Bootstrap inyecta --bs-table-* que pisamos) */
  #tablaIngresos tr.expandida td,
  #tablaGastos tr.expandida td,
  #tablaAhorros tr.expandida td {
    background: transparent !important;
    --bs-table-bg: transparent;
    --bs-table-accent-bg: transparent;
    --bs-table-striped-bg: transparent;
    --bs-table-hover-bg: transparent;
  }
  /* Al expandir: botones editar/borrar */
  #tablaIngresos tr.expandida td.celda-acciones,
  #tablaGastos tr.expandida td.celda-acciones,
  #tablaAhorros tr.expandida td.celda-acciones {
    display: flex;
    order: 5;
    flex-basis: 100%;
    width: 100%;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
    padding-top: 0 !important;
  }
}

/* =============================================
   TARJETA COMPACTA-EXPANDIBLE — Cuotas y Mensuales (mobile ≤600px)
   Botón de acción (Pagar / A Gastos) siempre visible en el resumen.
   ============================================= */
@media (max-width: 600px) {
  #tablaResumenCerrado tr,
  #tablaResumenProximo tr,
  #tablaCuotasTerminadas tr,
  #tablaGastosMensuales tr {
    display: flex !important;
    flex-wrap: wrap;
    align-items: center;
    position: relative;
    cursor: pointer;
    padding: 0.8rem 1rem;
    padding-right: 2.2rem;
    row-gap: 0.45rem;
    column-gap: 0.6rem;
    width: 100%;
    box-sizing: border-box;
  }
  /* Chevron arriba a la derecha */
  #tablaResumenCerrado tr::after,
  #tablaResumenProximo tr::after,
  #tablaCuotasTerminadas tr::after,
  #tablaGastosMensuales tr::after {
    content: "▾";
    position: absolute;
    top: 0.7rem;
    right: 1rem;
    font-size: 0.95rem;
    color: var(--text-secondary);
    transition: transform 0.2s;
  }
  /* Fondo de selección al expandir */
  #tablaResumenCerrado tr.expandida,
  #tablaResumenProximo tr.expandida,
  #tablaCuotasTerminadas tr.expandida,
  #tablaGastosMensuales tr.expandida {
    background: var(--bg-hover) !important;
  }
  /* Los td deben permanecer transparentes */
  #tablaResumenCerrado tr.expandida td,
  #tablaResumenProximo tr.expandida td,
  #tablaCuotasTerminadas tr.expandida td,
  #tablaGastosMensuales tr.expandida td {
    background: transparent !important;
    --bs-table-bg: transparent;
    --bs-table-accent-bg: transparent;
    --bs-table-striped-bg: transparent;
    --bs-table-hover-bg: transparent;
  }
  #tablaResumenCerrado tr.expandida::after,
  #tablaResumenProximo tr.expandida::after,
  #tablaCuotasTerminadas tr.expandida::after,
  #tablaGastosMensuales tr.expandida::after {
    transform: rotate(180deg);
  }
  /* Reset general de celdas en estas tablas */
  #tablaResumenCerrado td,
  #tablaResumenProximo td,
  #tablaCuotasTerminadas td,
  #tablaGastosMensuales td {
    border-bottom: none !important;
    padding: 0 !important;
    width: auto !important;
    display: flex;
  }
  #tablaResumenCerrado tr td::before,
  #tablaResumenProximo tr td::before,
  #tablaCuotasTerminadas tr td::before,
  #tablaGastosMensuales tr td::before {
    display: none;
  }

  /* ---------- RESUMEN CERRADO / PRÓXIMO ---------- */

  /* Separador invisible entre línea 1 y línea 2 */
  #tablaResumenCerrado tr::before,
  #tablaResumenProximo tr::before {
    content: "";
    order: 2;
    flex-basis: 100%;
    height: 0;
  }

  /* Línea 1: Fecha (izq, chica) */
  #tablaResumenCerrado td[data-label="Fecha"],
  #tablaResumenProximo td[data-label="Fecha"] {
    order: 1;
    flex: 0 0 auto;
    font-size: 0.72rem;
    color: var(--text-muted);
    white-space: nowrap;
  }

  /* Línea 1: Descripción (crece, truncada) */
  #tablaResumenCerrado td[data-label="Descripción"],
  #tablaResumenProximo td[data-label="Descripción"] {
    order: 1;
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 0.88rem;
    color: var(--text-primary);
    padding-right: 1.5rem !important;
  }

  /* Línea 2: Progreso (izq) */
  #tablaResumenCerrado td[data-label="Progreso"],
  #tablaResumenProximo td[data-label="Progreso"] {
    order: 3;
    flex: 0 0 auto;
    font-size: 0.82rem;
  }

  /* Línea 2: Monto (extremo derecha, destacado) */
  #tablaResumenCerrado td[data-label="Monto"],
  #tablaResumenProximo td[data-label="Monto"] {
    order: 3;
    flex: 1 1 auto;
    font-size: 1.05rem;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
    text-align: right !important;
    justify-content: flex-end;
  }

  /* Categoría: oculta en compacto */
  #tablaResumenCerrado td[data-label="Categoría"],
  #tablaResumenProximo td[data-label="Categoría"] {
    display: none;
  }

  /* Acciones: ocultas en compacto */
  #tablaResumenCerrado td.celda-acciones,
  #tablaResumenProximo td.celda-acciones {
    display: none;
  }

  /* Al expandir: Categoría */
  #tablaResumenCerrado tr.expandida td[data-label="Categoría"],
  #tablaResumenProximo tr.expandida td[data-label="Categoría"] {
    display: flex;
    order: 4;
    flex: 1 1 auto;
    font-size: 0.82rem;
    color: var(--text-muted);
    border-top: 1px solid rgba(255, 255, 255, 0.06) !important;
    margin-top: 0.35rem;
    padding-top: 0.5rem !important;
  }

  /* Al expandir: Acciones */
  #tablaResumenCerrado tr.expandida td.celda-acciones,
  #tablaResumenProximo tr.expandida td.celda-acciones {
    display: flex;
    order: 4;
    flex: 0 0 auto;
    border-top: 1px solid rgba(255, 255, 255, 0.06) !important;
    margin-top: 0.35rem;
    padding-top: 0.5rem !important;
    justify-content: flex-end;
  }

  /* ---------- CUOTAS TERMINADAS ---------- */

  /* Flex line-break invisible entre línea 1 y línea 2 */
  #tablaCuotasTerminadas tr::before {
    content: "";
    order: 4;
    flex-basis: 100%;
    height: 0;
  }

  /* Línea 1: Fecha (izq, crece) */
  #tablaCuotasTerminadas td[data-label="Fecha"] {
    order: 1;
    flex: 1 1 auto;
    width: auto !important;
    justify-content: flex-start;
    font-size: 0.72rem;
    color: var(--text-muted);
  }

  /* Línea 1: Valor de cuota (destacado) */
  #tablaCuotasTerminadas td[data-label="Valor"] {
    order: 2;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-end;
    font-size: 1.05rem;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }

  /* Línea 1: Cuotas X/X (extremo derecha, chico) */
  #tablaCuotasTerminadas td[data-label="Cuotas"] {
    order: 3;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-end;
    font-size: 0.82rem;
  }

  /* Línea 2: Badge tarjeta */
  #tablaCuotasTerminadas td[data-label="Tarjeta"] {
    order: 5;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-start;
  }

  /* Línea 2: Descripción (llena el espacio restante, truncada) */
  #tablaCuotasTerminadas td[data-label="Descripción"] {
    order: 6;
    flex: 1 1 auto;
    width: auto !important;
    min-width: 0;
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 0.88rem;
    color: var(--text-primary);
    padding-right: 1.5rem !important;
  }

  /* Oculto en resumen */
  #tablaCuotasTerminadas td[data-label="Total"],
  #tablaCuotasTerminadas td.celda-acciones {
    display: none;
  }

  /* Al expandir: Total */
  #tablaCuotasTerminadas tr.expandida td[data-label="Total"] {
    display: flex;
    order: 7;
    flex-basis: 100%;
    width: 100%;
    justify-content: space-between;
    font-size: 0.82rem;
    border-top: 1px solid rgba(255, 255, 255, 0.06) !important;
    margin-top: 0.35rem;
    padding-top: 0.5rem !important;
  }
  #tablaCuotasTerminadas tr.expandida td[data-label="Total"]::before {
    display: inline;
  }

  /* Al expandir: botón Borrar */
  #tablaCuotasTerminadas tr.expandida td.celda-acciones {
    display: flex;
    order: 8;
    flex-basis: 100%;
    justify-content: center;
    margin-top: 0.4rem;
  }

  /* ---------- MENSUALES FIJOS ---------- */

  /* Flex line-break invisible entre línea 1 y línea 2 */
  #tablaGastosMensuales tr::before {
    content: "";
    order: 3;
    flex-basis: 100%;
    height: 0;
  }

  /* Línea 1: Día de pago (izq, crece) */
  #tablaGastosMensuales td[data-label="Día"] {
    order: 1;
    flex: 1 1 auto;
    width: auto !important;
    justify-content: flex-start;
    font-size: 0.72rem;
    color: var(--text-muted);
  }

  /* Línea 1: Monto Base (der, destacado) */
  #tablaGastosMensuales td[data-label="Monto Base"] {
    order: 2;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-end;
    font-size: 1.05rem;
    font-weight: 700;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }

  /* Línea 2: Categoría badge */
  #tablaGastosMensuales td[data-label="Categoría"] {
    order: 4;
    flex: 0 0 auto;
    width: auto !important;
    justify-content: flex-start;
  }

  /* Línea 2: Descripción (truncada) */
  #tablaGastosMensuales td[data-label="Descripción"] {
    order: 5;
    flex: 1 1 auto;
    width: auto !important;
    min-width: 0;
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 0.88rem;
    color: var(--text-primary);
    padding-right: 1.5rem !important;
  }

  /* Línea 3: Botón pagar / badge abonado */
  #tablaGastosMensuales td[data-label="Informar pago"] {
    order: 6;
    flex-basis: 100%;
    width: 100%;
    margin-top: 0.2rem;
    justify-content: stretch;
  }

  /* Ocultos en resumen */
  #tablaGastosMensuales td[data-label="Monto Real"],
  #tablaGastosMensuales td.celda-acciones {
    display: none;
  }

  /* Al expandir: acciones editar/borrar */
  #tablaGastosMensuales tr.expandida td.celda-acciones {
    display: flex;
    order: 7;
    flex-basis: 100%;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 0.4rem;
  }
}

/* Botón pagar mobile vs desktop (mensuales fijos) */
.gm-dia-cell { display: none; }        /* oculto en desktop */
.btn-pagar-gm { display: none; }       /* oculto en desktop */
@media (max-width: 600px) {
  .gm-dia-cell { display: flex !important; }
  .btn-pagar-gm { display: block !important; }
  .btns-pagar-desktop { display: none !important; }
}
