/* ============================================
   MOBILE RESPONSIVE - Admin Dashboard
   Implementación completa de 6 fases
   ============================================ */

/* ============================================
   FASE 1: SIDEBAR MOBILE
   ============================================ */

/* Overlay para cerrar sidebar */
.sidebar-overlay,
#sidebarOverlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.active,
#sidebarOverlay.active {
    display: block;
    opacity: 1;
}

@media (max-width: 768px) {

    /* Sidebar oculto por defecto en mobile */
    .sidebar {
        position: fixed !important;
        left: -280px !important;
        top: 0 !important;
        height: 100vh !important;
        width: 280px !important;
        transition: left 0.3s ease !important;
        z-index: 999 !important;
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    }

    .sidebar.mobile-active,
    .sidebar.active {
        left: 0 !important;
        transform: translateX(0) !important;
    }

    /* Botón hamburger visible */
    .mobile-menu-btn {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        background: none;
        border: none;
        font-size: 1.5rem;
        color: var(--text-primary, #1f2937);
        cursor: pointer;
        padding: 0;
        margin-right: 1rem;
    }

    /* Main content ocupa todo el ancho */
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding-top: 0;
    }

    /* Top bar ajustado */
    .top-bar {
        padding-left: 1rem;
    }
}

/* Desktop: ocultar botón hamburger */
@media (min-width: 769px) {
    .mobile-menu-btn {
        display: none !important;
    }
}

/* ============================================
   FASE 2: TABLAS RESPONSIVE
   ============================================ */

@media (max-width: 768px) {

    /* Card layout para tablas */
    .data-table {
        display: block;
        width: 100%;
    }

    .data-table thead {
        display: none;
    }

    .data-table tbody {
        display: block;
    }

    .data-table tr {
        display: block;
        margin-bottom: 1rem;
        border: 1px solid var(--border-light, var(--border-light));
        border-radius: 12px;
        padding: 1rem;
        background: var(--bg-card, white);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        height: auto !important;
        min-height: auto !important;
    }

    .data-table td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        padding: 0.75rem 0;
        border-bottom: 1px solid var(--border-light, var(--border-light));
        font-size: 14px;
        color: var(--text-primary, #1f2937);
        /* Text overflow control */
        word-break: break-word;
        overflow-wrap: anywhere;
        hyphens: auto;
        max-width: 100%;
        height: auto !important;
        min-height: auto !important;
        max-height: none !important;
        white-space: normal !important;
    }

    /* Value content should wrap properly */
    .data-table td>*:not(:first-child),
    .data-table td:not(:empty)::after {
        flex: 1;
        text-align: right;
        word-break: break-word;
        overflow-wrap: anywhere;
    }

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

    /* Label antes del valor */
    .data-table td:before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted, #6b7280);
        flex: 0 0 40%;
        padding-right: 1rem;
    }

    /* Acciones al final */
    .data-table td.actions {
        justify-content: flex-end;
        gap: 0.5rem;
    }

    .data-table td.actions:before {
        display: none;
    }

    /* Fallback: scroll horizontal */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Para tablas que no usen card layout */
    .table-scroll-mobile {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .table-scroll-mobile table {
        min-width: 600px;
    }
}

/* ============================================
   FASE 3: MODALES FULLSCREEN
   ============================================ */

@media (max-width: 768px) {

    /* Modal fullscreen en mobile */
    .modal {
        padding: 0;
    }

    .modal-card {
        width: 100vw;
        height: 100vh;
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        margin: 0;
        display: flex;
        flex-direction: column;
    }

    .modal-header {
        flex-shrink: 0;
        padding: 1.25rem 1rem;
        border-bottom: 1px solid var(--border-light);
    }

    .modal-header h3 {
        font-size: 1.125rem;
    }

    .modal-body {
        flex: 1;
        overflow-y: auto;
        padding: 1rem;
        -webkit-overflow-scrolling: touch;
    }

    .modal-footer,
    .modal-actions {
        flex-shrink: 0;
        padding: 1rem;
        border-top: 1px solid var(--border-light);
    }

    /* Botones en modal fullwidth */
    .modal-footer button,
    .modal-actions button {
        width: 100%;
        margin-bottom: 0.5rem;
    }

    .modal-footer button:last-child,
    .modal-actions button:last-child {
        margin-bottom: 0;
    }
}

/* ============================================
   FASE 4: KPI GRID RESPONSIVE
   ============================================ */

.kpi-grid,
.stats-grid,
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
}

/* Tablet: 2 columnas */
@media (max-width: 1024px) and (min-width: 641px) {

    .kpi-grid,
    .stats-grid,
    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Mobile: 1 columna */
@media (max-width: 640px) {

    .kpi-grid,
    .stats-grid,
    .dashboard-grid {
        grid-template-columns: 1fr;
    }

    /* KPI Cards más compactos */
    .kpi-card,
    .stat-card {
        padding: 1rem;
    }

    .kpi-card h3,
    .stat-card h3 {
        font-size: 0.875rem;
    }

    .kpi-card .value,
    .stat-card .value {
        font-size: 1.5rem;
    }
}

/* ============================================
   FASE 5: TOUCH TARGETS & TYPOGRAPHY
   ============================================ */

@media (max-width: 768px) {

    /* Aumentar font-size base (previene zoom en iOS) */
    body {
        font-size: 16px;
    }

    /* Touch targets mínimo 44x44px */
    button,
    .btn,
    a.clickable,
    .action-icon,
    .icon-button {
        min-width: 44px;
        min-height: 44px;
        padding: 12px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Inputs táctiles */
    input,
    select,
    textarea,
    .form-control {
        font-size: 16px;
        min-height: 44px;
        padding: 12px;
    }

    /* Botones en formularios */
    .form-actions button,
    .btn-primary,
    .btn-secondary {
        width: 100%;
        min-height: 48px;
        font-size: 16px;
        margin-bottom: 0.75rem;
    }

    /* Aumentar espaciado entre elementos */
    .form-group {
        margin-bottom: 1.25rem;
    }

    /* Labels más grandes */
    label {
        font-size: 14px;
        font-weight: 600;
        margin-bottom: 0.5rem;
        display: block;
    }

    /* Checkboxes y radios */
    input[type="checkbox"],
    input[type="radio"] {
        width: 24px;
        height: 24px;
        margin-right: 0.75rem;
    }

    /* Tabs más grandes */
    .tabs button,
    .tab-button {
        min-height: 48px;
        padding: 12px 16px;
        font-size: 14px;
    }
}

/* ============================================
   FASE 6: CHAT MOBILE
   ============================================ */

/* ============================================
   FASE 6: CHAT MOBILE
   ============================================ */

@media (max-width: 768px) {
    /* Chat styles handled by chat_v2.css now */
}

/* ============================================
   MEJORAS ADICIONALES
   ============================================ */

@media (max-width: 768px) {

    /* Padding general reducido */
    .container,
    .main-container {
        padding: 1rem;
    }

    /* Headers más compactos */
    h1 {
        font-size: 1.5rem;
    }

    h2 {
        font-size: 1.25rem;
    }

    h3 {
        font-size: 1.125rem;
    }

    /* Top bar más compacto */
    .top-bar {
        padding: 0.75rem 1rem;
        min-height: 60px;
    }

    /* User info oculto en mobile muy pequeño */
    @media (max-width: 480px) {
        .user-info .user-details {
            display: none;
        }
    }

    /* Action columns en tablas */
    .table-actions {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    .table-actions button {
        flex: 1;
        min-width: 80px;
    }

    /* Filtros stack vertical */
    .filters,
    .filter-group {
        flex-direction: column;
        gap: 0.75rem;
    }

    .filter-group>* {
        width: 100%;
    }

    /* Pagination */
    .pagination {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .pagination button {
        min-width: 44px;
        min-height: 44px;
    }

    /* Dropdowns fullwidth */
    select,
    .select-container {
        width: 100%;
    }
}

/* ============================================
   LANDSCAPE ORIENTATION
   ============================================ */

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

    /* Chat más bajo en landscape */
    .chat-sidebar,
    #chatSidebar {
        height: 50vh;
        max-height: 50vh;
    }

    /* Modales scroll más eficiente */
    .modal-body {
        max-height: calc(100vh - 120px);
    }
}

/* ============================================
   SAFE AREAS (iOS notch, etc)
   ============================================ */

@supports (padding: max(0px)) {
    @media (max-width: 768px) {
        .sidebar {
            padding-top: max(1rem, env(safe-area-inset-top));
            padding-bottom: max(1rem, env(safe-area-inset-bottom));
        }

        .chat-sidebar,
        #chatSidebar {
            padding-bottom: max(1rem, env(safe-area-inset-bottom));
        }

        .modal-card {
            padding-top: max(0px, env(safe-area-inset-top));
            padding-bottom: max(0px, env(safe-area-inset-bottom));
        }
    }
}

/* ============================================
   ANIMATIONS & TRANSITIONS
   ============================================ */

/* Smooth transitions */
.sidebar,
.chat-sidebar,
.mobile-overlay,
.modal-card {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Reducir motion para accesibilidad */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ============================================
   TEXT OVERFLOW FIX FOR MOBILE
   ============================================ */
@media (max-width: 768px) {

    /* Fix text overflow in table cells */
    .data-table td,
    .data-table th {
        word-break: break-word !important;
        overflow-wrap: anywhere !important;
        hyphens: auto !important;
        max-width: 100% !important;
    }

    /* Fix long names/text in cards */
    .kpi-card,
    .chart-card,
    .profile-content-card {
        word-break: break-word;
        overflow-wrap: anywhere;
    }

    /* Ensure container doesn't overflow */
    .table-container,
    .view-section,
    .main-content {
        max-width: 100vw !important;
        overflow-x: hidden !important;
    }

    /* Fix select dropdowns */
    select,
    .status-select {
        max-width: 100% !important;
        text-overflow: ellipsis;
    }
}

/* ============================================
   FIX FOOTER SPACING (User Request)
   ============================================ */
@media (max-width: 768px) {

    /* 1. Container Padding: Moves EVERYTHING (text + box) away from edge */
    .footer-col {
        padding: 0 1.5rem !important;
    }

    /* 2. Box Alignment: Remove horizontal margin so it aligns with text */
    .capture-box {
        margin: 1.5rem 0 !important;
        /* Vertical spacing only */
        width: 100% !important;
        /* Fill the container */
    }

    /* 3. Form Layout: Stack Input and Button vertically for better UX */
    .footer-form {
        flex-direction: column !important;
        gap: 1rem !important;
    }

    .footer-form input,
    .footer-form button {
        width: 100% !important;
        height: 48px;
        /* Better touch target */
    }
}