/**
 * AGROTECH DIGITAL - Global Mobile Responsive V2
 * ================================================
 * CSS unificado para todas las páginas interiores Bootstrap
 * (RRHH, Labores, Inventario, Crop, Parcels, Billing, etc.)
 * Compatible con iPhone, Android y tablets
 * Aplicar en todas las páginas que usan topbar + left-sidebar
 */

/* ═══════════════════════════════════════════
   MOBILE: < 768px
   ═══════════════════════════════════════════ */
@media (max-width: 767px) {

    /* ── Variables de dark mode ── */
    :root {
        --mobile-bg: #0F1419;
        --mobile-card-bg: rgba(22, 28, 36, 0.85);
        --mobile-card-border: rgba(53, 184, 53, 0.1);
        --mobile-text: rgba(255, 255, 255, 0.9);
        --mobile-text-muted: rgba(255, 255, 255, 0.5);
        --mobile-green: #35B835;
        --mobile-blue: #4A9EFF;
        --mobile-orange: #FF9F0A;
        --mobile-radius: 16px;
    }

    /* ── Global Reset ── */
    * {
        -webkit-tap-highlight-color: transparent;
        -webkit-text-size-adjust: 100%;
    }

    body {
        background: linear-gradient(145deg, #0B0F14 0%, #131A24 50%, #0F1820 100%) !important;
        background-attachment: fixed !important;
        color: var(--mobile-text) !important;
        -webkit-overflow-scrolling: touch;
        overflow-x: hidden !important;
    }

    /* ── Hide desktop sidebar ── */
    .sidebar-neomorphic,
    #left-sidebar > .sidebar-neomorphic,
    .left-sidenav {
        display: none !important;
    }

    /* ── Topbar ── */
    .topbar-custom {
        background: rgba(11, 15, 20, 0.95) !important;
        backdrop-filter: blur(20px) !important;
        -webkit-backdrop-filter: blur(20px) !important;
        border-bottom: 1px solid rgba(53, 184, 53, 0.1) !important;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3) !important;
        height: 56px !important;
        padding: 0 16px !important;
    }

    .topbar-custom .topbar-title {
        color: #fff !important;
        font-weight: 600;
        font-size: 1rem !important;
    }

    .topbar-custom .menu-toggle {
        color: var(--mobile-green) !important;
        font-size: 1.4rem !important;
    }

    .topbar-custom .topbar-actions button,
    .topbar-custom .topbar-icon {
        color: rgba(255, 255, 255, 0.7) !important;
        background: rgba(255, 255, 255, 0.06) !important;
        border: 1px solid rgba(255, 255, 255, 0.08) !important;
        border-radius: 12px !important;
        width: 40px !important;
        height: 40px !important;
    }

    /* ── Mobile Sidebar Drawer ── */
    #mobile-sidebar,
    .mobile-sidebar {
        display: none;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 280px !important;
        height: 100vh !important;
        height: 100dvh !important;
        background: rgba(11, 15, 20, 0.97) !important;
        backdrop-filter: blur(30px) !important;
        -webkit-backdrop-filter: blur(30px) !important;
        border-right: 1px solid rgba(53, 184, 53, 0.12) !important;
        z-index: 9999 !important;
        transform: translateX(-100%);
        transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1) !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        padding: 24px 16px !important;
    }

    #mobile-sidebar.show,
    .mobile-sidebar.show {
        display: block !important;
        transform: translateX(0) !important;
    }

    #mobile-sidebar .menu li a,
    .mobile-sidebar .menu li a {
        color: rgba(255, 255, 255, 0.75) !important;
        padding: 14px 18px !important;
        border-radius: 14px !important;
        margin-bottom: 2px;
        font-size: 0.95rem !important;
        transition: all 0.2s ease;
    }

    #mobile-sidebar .menu li a:hover,
    #mobile-sidebar .menu li a:active,
    .mobile-sidebar .menu li a:hover,
    .mobile-sidebar .menu li a:active {
        background: rgba(53, 184, 53, 0.12) !important;
        color: var(--mobile-green) !important;
    }

    /* Sidebar overlay */
    #sidebar-overlay,
    .sidebar-overlay {
        position: fixed !important;
        inset: 0;
        background: rgba(0, 0, 0, 0.6) !important;
        z-index: 9998 !important;
        backdrop-filter: blur(4px) !important;
        -webkit-backdrop-filter: blur(4px) !important;
    }

    #sidebar-overlay.show,
    .sidebar-overlay.show {
        display: block !important;
    }

    /* ── Main Content Layout ── */
    .main-layout,
    .page-wrapper {
        margin-left: 0 !important;
        padding-left: 0 !important;
        width: 100% !important;
        max-width: 100vw !important;
    }

    #main-content,
    .container,
    .container-fluid {
        padding: 70px 14px 90px 14px !important;
        margin: 0 !important;
        max-width: 100vw !important;
        width: 100% !important;
    }

    /* ── Page Titles ── */
    h1, h2 {
        color: #fff !important;
        font-size: 1.4rem !important;
        font-weight: 700;
        letter-spacing: -0.3px;
    }

    h3, h4, h5, h6 {
        color: rgba(255, 255, 255, 0.9) !important;
    }

    p, span, label, small, .text-muted, .text-secondary {
        color: rgba(255, 255, 255, 0.6) !important;
    }

    /* ── Cards ── */
    .card,
    .dashboard-card {
        background: var(--mobile-card-bg) !important;
        backdrop-filter: blur(20px) !important;
        -webkit-backdrop-filter: blur(20px) !important;
        border: 1px solid var(--mobile-card-border) !important;
        border-radius: var(--mobile-radius) !important;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25) !important;
        color: var(--mobile-text) !important;
        margin-bottom: 16px !important;
        overflow: hidden;
    }

    .card-header {
        background: rgba(255, 255, 255, 0.03) !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06) !important;
        color: #fff !important;
        padding: 14px 16px !important;
    }

    .card-body {
        padding: 16px !important;
        color: var(--mobile-text) !important;
    }

    .card-title,
    .card-text {
        color: #fff !important;
    }

    /* ── Tables ── */
    .table {
        color: var(--mobile-text) !important;
        font-size: 0.85rem !important;
        --bs-table-bg: transparent !important;
        --bs-table-hover-bg: rgba(53, 184, 53, 0.06) !important;
    }

    .table thead th {
        background: rgba(53, 184, 53, 0.1) !important;
        color: rgba(255, 255, 255, 0.7) !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
        font-size: 0.75rem !important;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        padding: 10px 8px !important;
        white-space: nowrap;
    }

    .table tbody td {
        border-bottom: 1px solid rgba(255, 255, 255, 0.04) !important;
        padding: 12px 8px !important;
        color: rgba(255, 255, 255, 0.8) !important;
        vertical-align: middle;
    }

    .table-hover tbody tr:hover {
        background: rgba(53, 184, 53, 0.06) !important;
    }

    /* Table responsive: card layout en pantallas muy pequeñas */
    .table-responsive {
        border: none !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ── Buttons ── */
    .btn {
        border-radius: 12px !important;
        font-size: 0.9rem !important;
        padding: 10px 16px !important;
        font-weight: 500;
        transition: all 0.2s ease;
        min-height: 44px; /* iOS tap target */
    }

    .btn:active {
        transform: scale(0.97);
    }

    .btn-primary {
        background: linear-gradient(135deg, #35B835, #007A20) !important;
        border: none !important;
        box-shadow: 0 4px 12px rgba(53, 184, 53, 0.25) !important;
    }

    .btn-success {
        background: linear-gradient(135deg, #35B835, #2E8B57) !important;
        border: none !important;
        box-shadow: 0 4px 12px rgba(53, 184, 53, 0.25) !important;
    }

    .btn-secondary {
        background: rgba(255, 255, 255, 0.08) !important;
        border: 1px solid rgba(255, 255, 255, 0.12) !important;
        color: rgba(255, 255, 255, 0.85) !important;
    }

    .btn-outline-primary,
    .btn-outline-success {
        border-color: rgba(53, 184, 53, 0.4) !important;
        color: var(--mobile-green) !important;
        background: transparent !important;
    }

    .btn-danger {
        background: linear-gradient(135deg, #FF3B30, #DC2626) !important;
        border: none !important;
    }

    .btn-sm {
        padding: 6px 12px !important;
        font-size: 0.8rem !important;
        min-height: 36px;
    }

    /* ── Forms ── */
    .form-control,
    .form-select {
        background: rgba(255, 255, 255, 0.06) !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        color: #fff !important;
        border-radius: 12px !important;
        padding: 12px 14px !important;
        font-size: 16px !important; /* Prevent iOS zoom */
        min-height: 48px;
        transition: all 0.2s ease;
    }

    .form-control::placeholder {
        color: rgba(255, 255, 255, 0.3) !important;
    }

    .form-control:focus,
    .form-select:focus {
        border-color: rgba(53, 184, 53, 0.5) !important;
        box-shadow: 0 0 0 3px rgba(53, 184, 53, 0.1) !important;
        background: rgba(255, 255, 255, 0.08) !important;
    }

    .form-label {
        color: rgba(255, 255, 255, 0.7) !important;
        font-size: 0.85rem !important;
        font-weight: 500;
        margin-bottom: 6px !important;
    }

    .form-check-input {
        background-color: rgba(255, 255, 255, 0.1) !important;
        border-color: rgba(255, 255, 255, 0.2) !important;
    }

    .form-check-input:checked {
        background-color: var(--mobile-green) !important;
        border-color: var(--mobile-green) !important;
    }

    .form-check-label {
        color: rgba(255, 255, 255, 0.8) !important;
    }

    /* ── Modals ── */
    .modal-content {
        background: rgba(17, 22, 30, 0.97) !important;
        backdrop-filter: blur(30px) !important;
        -webkit-backdrop-filter: blur(30px) !important;
        border: 1px solid rgba(53, 184, 53, 0.12) !important;
        border-radius: 24px 24px 0 0 !important;
        color: var(--mobile-text) !important;
    }

    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
        align-items: flex-end !important;
    }

    .modal-dialog-centered {
        min-height: auto !important;
        align-items: flex-end !important;
    }

    .modal.show .modal-dialog {
        transform: translateY(0) !important;
    }

    .modal-header {
        background: transparent !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06) !important;
        padding: 18px 20px !important;
    }

    .modal-title {
        color: #fff !important;
        font-weight: 600;
        font-size: 1.1rem !important;
    }

    .modal-body {
        padding: 20px !important;
        max-height: 70vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-footer {
        background: transparent !important;
        border-top: 1px solid rgba(255, 255, 255, 0.06) !important;
        padding: 14px 20px !important;
        padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px)) !important;
    }

    .btn-close {
        filter: invert(1) !important;
        opacity: 0.7;
    }

    .modal-backdrop {
        background: rgba(0, 0, 0, 0.7) !important;
        backdrop-filter: blur(4px);
    }

    /* ── Badges ── */
    .badge {
        font-size: 0.7rem !important;
        padding: 4px 10px !important;
        border-radius: 8px !important;
        font-weight: 500;
    }

    .badge.bg-primary,
    .badge.bg-success {
        background: rgba(53, 184, 53, 0.2) !important;
        color: var(--mobile-green) !important;
    }

    .badge.bg-warning {
        background: rgba(255, 159, 10, 0.2) !important;
        color: var(--mobile-orange) !important;
    }

    .badge.bg-danger {
        background: rgba(255, 59, 48, 0.2) !important;
        color: #FF6B6B !important;
    }

    .badge.bg-secondary {
        background: rgba(255, 255, 255, 0.1) !important;
        color: rgba(255, 255, 255, 0.6) !important;
    }

    .badge.bg-info {
        background: rgba(74, 158, 255, 0.2) !important;
        color: var(--mobile-blue) !important;
    }

    /* ── Alerts ── */
    .alert {
        background: rgba(22, 28, 36, 0.85) !important;
        border: 1px solid rgba(255, 255, 255, 0.08) !important;
        border-radius: 14px !important;
        color: var(--mobile-text) !important;
        padding: 14px 16px !important;
    }

    .alert-success {
        border-color: rgba(53, 184, 53, 0.2) !important;
        background: rgba(53, 184, 53, 0.1) !important;
    }

    .alert-warning {
        border-color: rgba(255, 159, 10, 0.2) !important;
        background: rgba(255, 159, 10, 0.1) !important;
    }

    .alert-danger {
        border-color: rgba(255, 59, 48, 0.2) !important;
        background: rgba(255, 59, 48, 0.1) !important;
    }

    /* ── Progress Bars ── */
    .progress {
        background: rgba(255, 255, 255, 0.06) !important;
        border-radius: 8px !important;
        height: 8px !important;
    }

    .progress-bar {
        border-radius: 8px !important;
    }

    /* ── Row/Grid adjustments ── */
    .row {
        margin-left: -6px !important;
        margin-right: -6px !important;
    }

    .row > [class*="col-"] {
        padding-left: 6px !important;
        padding-right: 6px !important;
    }

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

    /* Allow 2-col for small stat cards */
    .row > .col-6 {
        width: 50% !important;
        flex: 0 0 50% !important;
        max-width: 50% !important;
    }

    /* ── Spinners ── */
    .spinner-border {
        color: var(--mobile-green) !important;
    }

    /* ── Dropdown ── */
    .dropdown-menu {
        background: rgba(17, 22, 30, 0.97) !important;
        backdrop-filter: blur(20px) !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        border-radius: 14px !important;
        box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5) !important;
        padding: 8px !important;
    }

    .dropdown-item {
        color: rgba(255, 255, 255, 0.8) !important;
        border-radius: 10px !important;
        padding: 10px 14px !important;
    }

    .dropdown-item:hover,
    .dropdown-item:active {
        background: rgba(53, 184, 53, 0.12) !important;
        color: var(--mobile-green) !important;
    }

    /* ── Pagination ── */
    .page-link {
        background: rgba(255, 255, 255, 0.06) !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        color: rgba(255, 255, 255, 0.7) !important;
        border-radius: 10px !important;
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .page-item.active .page-link {
        background: var(--mobile-green) !important;
        border-color: var(--mobile-green) !important;
        color: #fff !important;
    }

    /* ── Scrollbar (WebKit) ── */
    ::-webkit-scrollbar {
        width: 4px;
        height: 4px;
    }

    ::-webkit-scrollbar-track {
        background: transparent;
    }

    ::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.1);
        border-radius: 4px;
    }

    /* ── Toast/Notification ── */
    .toast {
        background: rgba(17, 22, 30, 0.95) !important;
        border: 1px solid rgba(53, 184, 53, 0.15) !important;
        border-radius: 14px !important;
        color: #fff !important;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4) !important;
    }

    /* ── Accordion ── */
    .accordion-item {
        background: var(--mobile-card-bg) !important;
        border: 1px solid rgba(255, 255, 255, 0.06) !important;
        border-radius: 14px !important;
        margin-bottom: 8px !important;
        overflow: hidden;
    }

    .accordion-button {
        background: transparent !important;
        color: var(--mobile-text) !important;
        font-weight: 500;
    }

    .accordion-button:not(.collapsed) {
        background: rgba(53, 184, 53, 0.06) !important;
        color: var(--mobile-green) !important;
    }

    .accordion-body {
        color: var(--mobile-text) !important;
    }

    /* ── Nav Tabs ── */
    .nav-tabs {
        border-bottom: 1px solid rgba(255, 255, 255, 0.08) !important;
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
    }

    .nav-tabs .nav-link {
        color: rgba(255, 255, 255, 0.5) !important;
        border: none !important;
        padding: 10px 16px !important;
        white-space: nowrap;
        font-size: 0.85rem !important;
        border-radius: 10px 10px 0 0 !important;
    }

    .nav-tabs .nav-link.active {
        background: rgba(53, 184, 53, 0.1) !important;
        color: var(--mobile-green) !important;
        border-bottom: 2px solid var(--mobile-green) !important;
    }

    /* ── List Group ── */
    .list-group-item {
        background: rgba(255, 255, 255, 0.03) !important;
        border: 1px solid rgba(255, 255, 255, 0.06) !important;
        color: var(--mobile-text) !important;
    }

    /* ── Horizontal overflow helpers ── */
    .d-flex.justify-content-between {
        flex-wrap: wrap;
        gap: 8px;
    }

    /* ── Empty state ── */
    .text-center.py-3,
    .text-center.py-4 {
        color: rgba(255, 255, 255, 0.4) !important;
    }

    /* ── Background helpers override ── */
    .bg-light, .bg-white {
        background: rgba(255, 255, 255, 0.04) !important;
    }

    .bg-success { background: rgba(53, 184, 53, 0.15) !important; }
    .bg-primary { background: rgba(74, 158, 255, 0.15) !important; }
    .bg-warning { background: rgba(255, 159, 10, 0.15) !important; }
    .bg-danger { background: rgba(255, 59, 48, 0.15) !important; }
    .bg-info { background: rgba(74, 158, 255, 0.12) !important; }

    .text-dark { color: var(--mobile-text) !important; }
    .text-white { color: #fff !important; }
    .fw-bold, .fw-semibold { color: rgba(255, 255, 255, 0.95) !important; }

    .border { border-color: rgba(255, 255, 255, 0.08) !important; }
    .border-top, .border-bottom { border-color: rgba(255, 255, 255, 0.06) !important; }

    hr {
        border-color: rgba(255, 255, 255, 0.06) !important;
        opacity: 1 !important;
    }

}

/* ═══════════════════════════════════════════
   SAFE AREA (iPhone notch/Dynamic Island)
   ═══════════════════════════════════════════ */
@supports (padding-top: env(safe-area-inset-top)) {
    @media (max-width: 767px) {
        .topbar-custom {
            padding-top: env(safe-area-inset-top) !important;
            height: calc(56px + env(safe-area-inset-top)) !important;
        }

        #main-content,
        .container,
        .container-fluid {
            padding-top: calc(70px + env(safe-area-inset-top)) !important;
            padding-bottom: calc(90px + env(safe-area-inset-bottom)) !important;
        }

        .modal-footer {
            padding-bottom: calc(14px + env(safe-area-inset-bottom)) !important;
        }
    }
}

/* ═══════════════════════════════════════════
   EXTRA SMALL DEVICES (iPhone SE, etc)
   ═══════════════════════════════════════════ */
@media (max-width: 380px) {
    h1, h2 {
        font-size: 1.2rem !important;
    }

    .btn {
        font-size: 0.85rem !important;
        padding: 8px 12px !important;
    }

    .card-body {
        padding: 12px !important;
    }

    #main-content,
    .container,
    .container-fluid {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }
}

/* ═══════════════════════════════════════════
   TABLET (768px - 1024px)
   ═══════════════════════════════════════════ */
@media (min-width: 768px) and (max-width: 1024px) {
    .sidebar-neomorphic {
        width: 200px !important;
    }

    .main-layout,
    .page-wrapper {
        margin-left: 200px !important;
    }

    .card {
        border-radius: 14px !important;
    }

    .modal-dialog {
        max-width: 600px !important;
        margin: auto !important;
    }

    .modal-content {
        border-radius: 20px !important;
    }
}

/* ═══════════════════════════════════════════
   LANDSCAPE MOBILE (short height)
   ═══════════════════════════════════════════ */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    #main-content,
    .container,
    .container-fluid {
        padding-top: 60px !important;
        padding-bottom: 20px !important;
    }

    .modal-body {
        max-height: 50vh;
    }

    .topbar-custom {
        height: 48px !important;
    }
}

/* ═══════════════════════════════════════════
   ANIMATIONS
   ═══════════════════════════════════════════ */
@media (max-width: 767px) {
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(16px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .card,
    .dashboard-card {
        animation: fadeInUp 0.4s ease-out both;
    }

    .card:nth-child(1) { animation-delay: 0s; }
    .card:nth-child(2) { animation-delay: 0.05s; }
    .card:nth-child(3) { animation-delay: 0.1s; }
    .card:nth-child(4) { animation-delay: 0.15s; }
    .card:nth-child(5) { animation-delay: 0.2s; }
    .card:nth-child(6) { animation-delay: 0.25s; }

    /* Smooth transitions on interactive elements */
    .btn, .form-control, .form-select, .card, .nav-link, .dropdown-item {
        transition: all 0.2s ease !important;
    }
}

/* ═══════════════════════════════════════════
   PRINT - Hide mobile specific elements
   ═══════════════════════════════════════════ */
@media print {
    .topbar-custom,
    #mobile-sidebar,
    .mobile-sidebar,
    #sidebar-overlay,
    .sidebar-overlay,
    .mobile-bottom-nav {
        display: none !important;
    }

    body {
        background: #fff !important;
        color: #000 !important;
    }
}
