/* ===================================================
   MOBILE UX OPTIMIZATION CSS
   Optimizaciones para experiencia móvil perfecta
   =================================================== */

/* ===== TOUCH OPTIMIZATION ===== */

/* Eliminar delay 300ms en todos los elementos interactivos */
button,
a,
input,
select,
textarea,
[role="button"],
.btn,
.menu-item {
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(67, 97, 238, 0.2);
}

/* Áreas táctiles mínimas (44x44px según WCAG) */
button,
.btn,
select {
    min-height: 44px;
    min-width: 44px;
}

input[type="checkbox"],
input[type="radio"] {
    min-width: 24px;
    min-height: 24px;
}

.menu-item a {
    min-height: 48px;
}

/* Estados táctiles claros */
button:active,
.btn:active {
    transform: scale(0.98);
    opacity: 0.8;
    transition: transform 0.1s ease, opacity 0.1s ease;
}

button:focus-visible,
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

/* ===== LOADING STATES ===== */

.btn-loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}

.btn-loading::after {
    content: "";
    position: absolute;
    width: 20px;
    height: 20px;
    top: 50%;
    left: 50%;
    margin-left: -10px;
    margin-top: -10px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spinner 0.6s linear infinite;
}

@keyframes spinner {
    to {
        transform: rotate(360deg);
    }
}

/* ===== RIPPLE EFFECT ===== */

.ripple {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.6);
    transform: scale(0);
    animation: ripple-animation 0.6s ease-out;
    pointer-events: none;
}

@keyframes ripple-animation {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

/* ===== VALIDATION FEEDBACK ===== */

.form-control.is-invalid,
input.is-invalid,
select.is-invalid,
textarea.is-invalid {
    border-color: var(--danger) !important;
    padding-right: 40px;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23dc3545' stroke='none'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 20px;
}

.form-control.is-valid,
input.is-valid,
select.is-valid,
textarea.is-valid {
    border-color: var(--success) !important;
    padding-right: 40px;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%2310b981' d='M2.3 6.73L.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 20px;
}

.invalid-feedback,
.valid-feedback {
    display: none;
    margin-top: 5px;
    font-size: 14px;
}

.invalid-feedback {
    color: var(--danger);
}

.valid-feedback {
    color: var(--success);
}

.is-invalid~.invalid-feedback {
    display: block;
}

.is-valid~.valid-feedback {
    display: block;
}

/* ===== RESPONSIVE TABLES ===== */

@media (max-width: 768px) {

    /* Convertir tablas a cards en móvil. min-width anula la regla genérica
       ".table-inventario, .table { min-width: 800px; }" de styles.css, que
       aplica a CUALQUIER tabla Bootstrap con clase .table -- sin esto, una
       tabla .table.responsive-mobile queda forzada a 800px y el layout de
       tarjetas nunca se ve, aunque display:block sí se aplique. */
    table.responsive-mobile {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
    }

    table.responsive-mobile thead {
        display: none;
    }

    table.responsive-mobile tbody {
        display: block;
    }

    table.responsive-mobile tr {
        display: block;
        margin-bottom: 15px;
        border: 1px solid var(--gray-light, #e9ecef);
        border-radius: 8px;
        padding: 15px;
        background: white;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    table.responsive-mobile td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 8px 0;
        border-bottom: 1px solid var(--gray-light, #e9ecef);
        text-align: right;
    }

    table.responsive-mobile td:last-child {
        border-bottom: none;
    }

    table.responsive-mobile td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--gray, #6c757d);
        margin-right: 10px;
        text-align: left;
        flex: 1;
    }

    /* Botones de acción en cards */
    table.responsive-mobile td .btn-icon {
        margin: 0 2px;
    }

    /* Fila de detalle anidado (ej. facturas de un cliente en Cartera): es un
       contenedor de ancho completo para OTRA tabla, no un par etiqueta/valor
       -- se excluye del layout flex + ::before de las celdas normales. */
    table.responsive-mobile td.td-detalle-anidado {
        display: block;
        text-align: left;
        padding: 0;
    }
    table.responsive-mobile td.td-detalle-anidado::before {
        content: none;
    }
}

/* ===== RESPONSIVE FORMS ===== */

@media (max-width: 768px) {

    /* Formularios de 2 columnas → 1 columna */
    .form-grid,
    .row.g-3,
    .row.g-2 {
        display: flex !important;
        flex-direction: column !important;
    }

    .col-md-6,
    .col-md-4,
    .col-md-3,
    .col-lg-6,
    .col-lg-4,
    .col-lg-3 {
        width: 100% !important;
        max-width: 100% !important;
        flex: 0 0 100% !important;
    }

    /* Inputs más grandes para táctil */
    input:not([type="checkbox"]):not([type="radio"]),
    select,
    textarea {
        min-height: 44px;
        font-size: 16px;
        /* Evita zoom automático en iOS */
        padding: 12px 15px;
    }

    /* Botones más grandes */
    .btn {
        min-height: 48px;
        padding: 12px 24px;
        font-size: 16px;
        width: 100%;
        margin-bottom: 10px;
    }

    .btn-sm {
        min-height: 40px;
        font-size: 14px;
    }

    /* Labels más legibles */
    label {
        font-size: 15px;
        margin-bottom: 8px;
        font-weight: 500;
    }
}

/* ===== MODAL OPTIMIZATION ===== */

@media (max-width: 768px) {

    /* Side Panel Detalle Producto */
    #detalle-producto {
        width: 100% !important;
        right: -100% !important;
    }

    #detalle-producto.active {
        right: 0 !important;
    }

    /* Bootstrap & Custom Modals */
    .modal-content-custom,
    .modal-content,
    .custom-modal-content,
    .confirmation-modal-pro {
        width: 95% !important;
        max-width: none !important;
        margin: 10px auto !important;
        max-height: 90vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-header,
    .custom-modal-header {
        position: sticky;
        top: 0;
        background: white;
        z-index: 10;
        border-bottom: 2px solid var(--gray-light, #e9ecef);
        padding: 15px;
    }

    .modal-footer,
    .custom-modal-footer {
        position: sticky;
        bottom: 0;
        background: white;
        z-index: 10;
        border-top: 2px solid var(--gray-light, #e9ecef);
        padding: 15px;
        flex-direction: column;
    }

    .modal-footer .btn,
    .custom-modal-footer .btn {
        width: 100%;
        margin: 5px 0;
    }

    /* Flex helpers for mobile forms */
    .mobile-stack {
        flex-direction: column !important;
        gap: 10px !important;
    }

    .mobile-stack>* {
        width: 100% !important;
    }
}

/* ===== SIDEBAR MOBILE ===== */

@media (max-width: 991px) {

    /* Overlay cuando sidebar está abierto */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 4999;
    }

    .sidebar-overlay.active {
        display: block;
    }

    /* Mejorar área táctil del toggle */
    .btn-icon.d-lg-none {
        min-width: 48px;
        min-height: 48px;
        padding: 12px;
    }
}

/* ===== CARDS MOBILE ===== */

@media (max-width: 768px) {

    .kpi-card,
    .dashboard-card {
        margin-bottom: 15px;
    }

    .kpi-grid {
        gap: 15px;
    }

    .card-content {
        padding: 15px;
    }
}

/* ===== INPUTS ESPECIALES ===== */

@media (max-width: 768px) {

    /* Date/time inputs más grandes */
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"] {
        min-height: 48px;
        font-size: 16px;
    }

    /* Select más grande */
    select {
        background-size: 20px;
        padding-right: 40px;
    }
}

/* ===== ACCESIBILIDAD TÁCTIL ===== */

/* Aumentar área de click en iconos */
.btn-icon {
    padding: 12px;
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Mejorar contraste en focus */
*:focus-visible {
    outline: 3px solid var(--primary, #4361ee);
    outline-offset: 2px;
}

/* ===== SCROLL SUAVE ===== */

html {
    scroll-behavior: smooth;
}

/* Scroll suave en iOS */
.modal-body,
.sidebar-menu,
.main-content {
    -webkit-overflow-scrolling: touch;
}

/* ===== PREVENIR ZOOM NO DESEADO ===== */

@media (max-width: 768px) {

    /* Evitar zoom en inputs pequeños */
    input,
    select,
    textarea {
        font-size: 16px !important;
    }

    /* Contenedor de Total en Pedidos */
    .pedidos-total-container {
        display: flex !important;
        flex-direction: row !important;
        justify-content: space-between !important;
        align-items: center !important;
        background: #ecfdf5 !important;
        border: 1px solid #10b981 !important;
        padding: 15px 20px !important;
        border-radius: 12px !important;
        margin-top: 10px !important;
        gap: 10px !important;
    }

    .pedidos-total-label-box {
        display: flex !important;
        align-items: center !important;
        gap: 10px !important;
        color: #065f46 !important;
    }

    .pedidos-total-label-box i {
        font-size: 1.4rem !important;
        color: #059669 !important;
    }

    .pedidos-total-label-text {
        font-weight: 700 !important;
        font-size: 1rem !important;
        line-height: 1 !important;
    }

    .pedidos-total-value {
        font-size: 1.8rem !important;
        font-weight: 800 !important;
        color: #059669 !important;
        white-space: nowrap !important;
    }
}

/* ===== ORIENTACIÓN LANDSCAPE ===== */

@media (max-width: 768px) and (orientation: landscape) {

    .modal-content-custom,
    .modal-content {
        max-height: 85vh;
    }

    .sidebar {
        width: 240px;
    }
}

/* ===== SAFE AREAS (iOS) ===== */

@supports (padding: max(0px)) {
    .main-content {
        padding-left: max(1.5rem, env(safe-area-inset-left));
        padding-right: max(1.5rem, env(safe-area-inset-right));
    }

    .sidebar {
        padding-left: max(1.25rem, env(safe-area-inset-left));
    }
}

/* ==========================================================================
   INVENTARIO - CARDS MÓVILES (REDISEÑO TOTAL)
   ========================================================================== */

/* Limpiar tabla en modo móvil para que no parezca tabla */
@media (max-width: 991px) {
    /* El wrapper .table-container comparte regla de padding:25px con .pedidos-card/.metals-card
       (ver styles.css). En modo tarjeta ese padding le roba casi 50px al contenido interno,
       así que aquí se reduce solo cuando envuelve la tabla de Inventario. */
    .table-container:has(.table-inventario.responsive-mobile) {
        padding: 10px !important;
    }

    .table-inventario.responsive-mobile {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        border: none !important;
        background: transparent !important;
    }

    .table-inventario.responsive-mobile thead {
        display: none !important;
    }

    .table-inventario.responsive-mobile tbody {
        display: block !important;
        width: 100% !important;
        background: transparent !important;
    }

    /* La fila de la tabla ahora es un contenedor invisible */
    .mobile-product-card-row {
        display: block !important;
        margin-bottom: 20px !important;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
    }

    .mobile-card-cell {
        display: block !important;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
        white-space: normal !important;
    }

    /* La tarjeta real */
    .mobile-product-card {
        background: #ffffff;
        border-radius: 20px;
        box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05), 0 2px 5px rgba(0, 0, 0, 0.02);
        display: flex;
        padding: 16px;
        gap: 16px;
        position: relative;
        border: 1px solid rgba(0, 0, 0, 0.03);
        transition: transform 0.2s ease, background-color 0.2s ease;
        margin: 0 4px;
    }

    .mobile-product-card:active {
        transform: scale(0.97);
        background: #f8fafc;
    }

    /* Imagen con badge flotante */
    .card-image-wrapper {
        width: 90px;
        height: 90px;
        flex-shrink: 0;
        position: relative;
        background: #f1f5f9;
        border-radius: 16px;
        overflow: hidden;
        box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.02);
    }

    /* En celulares angostos, el thumbnail de 90px le deja muy poco ancho
       al grid de 7 estadísticas; se reduce para darle aire al contenido */
    @media (max-width: 480px) {
        .card-image-wrapper {
            width: 64px;
            height: 64px;
        }
    }

    .card-img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    .mobile-status-badge {
        position: absolute;
        top: 8px;
        right: 8px;
        width: 12px;
        height: 12px;
        border-radius: 50%;
        border: 2.5px solid #fff;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
    }

    /* Contenido de la tarjeta */
    .card-content {
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    .card-header-flex {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 6px;
    }

    .card-code {
        font-size: 11px;
        font-weight: 800;
        background: #eff6ff;
        color: #3b82f6;
        padding: 3px 10px;
        border-radius: 6px;
        text-transform: uppercase;
        letter-spacing: 0.8px;
    }

    .card-status-text {
        font-size: 11px;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .card-title {
        font-size: 15px;
        font-weight: 700;
        color: #0f172a;
        margin: 0 0 12px 0;
        line-height: 1.25;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    /* Grid de estadísticas PWA Style */
    .card-stats-grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        background: #f8fafc;
        border-radius: 12px;
        padding: 8px;
        gap: 8px;
        border: 1px solid #f1f5f9;
    }

    .stat-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
    }

    .stat-label {
        font-size: 9px;
        font-weight: 700;
        color: #94a3b8;
        margin-bottom: 2px;
        text-transform: uppercase;
        overflow-wrap: anywhere;
    }

    .stat-value {
        font-size: 14px;
        font-weight: 800;
        color: #1e293b;
        overflow-wrap: anywhere;
    }

    .stat-value.secondary {
        color: #64748b;
    }

    .stat-value.success {
        color: #10b981;
    }

    /* Flecha de detalle */
    .card-arrow {
        display: flex;
        align-items: center;
        color: #e2e8f0;
        padding-left: 0px;
        font-size: 14px;
        justify-content: center;
    }
}

@media (max-width: 991px) {
    .pagination-container {
        display: flex !important;
        flex-direction: column !important;
        gap: 15px !important;
        padding: 20px 10px !important;
        background: #f8fafc;
        border-radius: 12px;
        margin-top: 10px;
        border: 1px solid #e2e8f0;
        align-items: center !important;
    }

    .pagination-info {
        text-align: center !important;
        font-size: 13px !important;
        color: #64748b !important;
        order: 2;
    }

    .pagination-buttons {
        display: flex !important;
        gap: 4px !important;
        justify-content: center !important;
        width: 100%;
        order: 1;
    }

    .pagination-btn {
        padding: 8px 10px !important;
        min-width: 38px !important;
        height: 38px !important;
        font-size: 13px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border: 1px solid #ddd !important;
        background: white !important;
        border-radius: 6px !important;
        cursor: pointer !important;
        color: #333 !important;
    }

    .pagination-btn .btn-text {
        display: none !important;
    }

    .pagination-btn i {
        font-size: 14px !important;
        margin: 0 !important;
    }
}
/* ===== MES TABS SCROLLABLE (Juan Sebastian Mobile Fix) ===== */
@media (max-width: 576px) {
    #mes-tabs {
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        justify-content: flex-start !important;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 5px !important;
        border-radius: 12px !important;
        scrollbar-width: none; /* Firefox */
    }

    #mes-tabs::-webkit-scrollbar {
        display: none; /* Chrome/Safari */
    }

    #mes-tabs .nav-item {
        flex: 0 0 auto !important;
        margin-right: 5px;
    }

    #mes-tabs .nav-link {
        white-space: nowrap !important;
        padding: 10px 15px !important;
        font-size: 13px !important;
    }
}

/* ===== ENSAMBLE RECIPE SELECTOR MÓVIL ===== */
@media (max-width: 768px) {
    #ens-opciones-btns {
        flex-direction: column !important;
    }

    #ens-opciones-btns button {
        width: 100% !important;
        margin-bottom: 5px;
    }
    
    .bom-item {
        min-width: 100% !important;
        margin-bottom: 10px !important;
    }
}
