/**
 * Mobile-First Responsive CSS for EconoClub ERP
 *
 * This file contains all mobile-responsive overrides and enhancements
 * for the EconoClub ERP system to ensure excellent mobile UX.
 *
 * Breakpoints:
 * - Mobile: < 768px
 * - Tablet: 768px - 1024px
 * - Desktop: > 1024px
 */

/* ============================================
   GLOBAL OVERFLOW FIXES
   ============================================ */

/* Prevent horizontal scrolling on mobile */
@media (max-width: 768px) {
    html {
        overflow-x: hidden;
        max-width: 100%;
    }

    body {
        overflow-x: hidden;
        max-width: 100%;
        position: relative;
    }

    /* Ensure main containers respect viewport width */
    .main-content,
    .content-area,
    .container,
    .card,
    .page-header,
    .topbar,
    .form-section,
    .filter-bar,
    .filters-section,
    .stat-grid,
    .stat-card,
    .grid,
    .grid-2,
    .grid-3,
    .quick-actions {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Allow specific elements to overflow intentionally */
    .tabs,
    .horizontal-scroll {
        max-width: 100vw;
    }

    /* Constrain images */
    img {
        max-width: 100%;
        height: auto;
    }

    /* Constrain tables */
    table {
        max-width: 100%;
        table-layout: fixed;
        word-wrap: break-word;
    }

    /* Prevent pre and code blocks from causing overflow */
    pre, code {
        max-width: 100%;
        overflow-x: auto;
        word-wrap: break-word;
        white-space: pre-wrap;
    }
}

/* ============================================
   MOBILE NAVIGATION & SIDEBAR
   ============================================ */

/* Hamburger Menu Button (mobile only) */
.mobile-menu-btn {
    display: none;
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 1100;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--sidebar-bg);
    color: white;
    border-radius: var(--radius-md);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-lg);
    transition: all 0.2s ease;
}

.mobile-menu-btn:active {
    transform: scale(0.95);
}

.mobile-menu-btn svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

/* Mobile Overlay */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.active {
    opacity: 1;
}

@media (max-width: 768px) {
    /* Show mobile menu button */
    .mobile-menu-btn {
        display: flex;
    }

    /* Transform sidebar to mobile slide-in menu */
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 1000;
        max-width: 280px;
        width: 80%;
    }

    .sidebar.mobile-open {
        transform: translateX(0);
    }

    /* Remove margin from main content on mobile */
    .main-content {
        margin-left: 0;
        width: 100%;
        max-width: 100vw;
        overflow-x: hidden;
    }

    /* Adjust topbar for mobile */
    .topbar {
        padding: 0 8px 0 60px !important; /* Leave space for hamburger, reduce padding */
        height: auto;
        min-height: var(--header-height);
        width: 100%;
        max-width: 100vw;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .topbar-welcome {
        flex: 1;
        min-width: 0; /* Allows text to shrink */
        overflow: hidden;
        max-width: calc(100% - 120px); /* Leave space for avatar and logout */
    }

    .topbar-welcome h3 {
        font-size: 0.875rem !important;
        font-weight: 500 !important;
        overflow: hidden;
        line-height: 1.3;
        margin: 0;
        display: flex;
        flex-direction: column;
        gap: 2px;
    }

    .topbar-welcome .welcome-text {
        font-size: 0.75rem;
        font-weight: 400;
        color: var(--text-tertiary);
        white-space: nowrap;
    }

    .topbar-welcome .user-name {
        font-size: 0.875rem;
        font-weight: 600;
        color: var(--text-primary);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .topbar-welcome p {
        display: none; /* Hide date on mobile */
    }

    /* Simplify user info on mobile */
    .topbar-user {
        gap: 6px;
        flex-shrink: 0;
    }

    .user-info {
        display: none; /* Hide username/role on mobile */
    }

    .user-avatar {
        width: 32px;
        height: 32px;
        font-size: 0.8125rem;
    }

    .btn-logout {
        padding: 6px 10px;
        font-size: 0.75rem;
    }

    /* Content area padding */
    .content-area {
        padding: 16px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }
}

/* ============================================
   RESPONSIVE TABLES - CARD VIEW ON MOBILE
   ============================================ */

@media (max-width: 768px) {
    /* Hide table headers on mobile */
    .member-table thead,
    .user-table thead,
    .data-table thead,
    .payment-table thead {
        display: none;
    }

    /* Transform table rows into cards */
    .member-table tbody,
    .user-table tbody,
    .data-table tbody,
    .payment-table tbody {
        display: block;
    }

    .member-table tr,
    .user-table tr,
    .data-table tr,
    .payment-table tr {
        display: block;
        margin-bottom: 16px;
        border: 1px solid var(--border-light);
        border-radius: var(--radius-md);
        padding: 16px;
        background: var(--bg-primary);
        box-shadow: var(--shadow-sm);
    }

    .member-table tr:hover,
    .user-table tr:hover,
    .data-table tr:hover,
    .payment-table tr:hover {
        background: var(--bg-primary);
        box-shadow: var(--shadow-md);
    }

    .member-table td,
    .user-table td,
    .data-table td,
    .payment-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 8px 0;
        border-bottom: 1px solid var(--border-light);
        word-wrap: break-word;
        overflow-wrap: break-word;
        max-width: 100%;
    }

    .member-table td:last-child,
    .user-table td:last-child,
    .data-table td:last-child,
    .payment-table td:last-child {
        border-bottom: none;
        padding-top: 12px;
        margin-top: 8px;
        border-top: 2px solid var(--border-light);
    }

    /* Add data labels using pseudo-elements */
    .member-table td:nth-child(1)::before { content: "Registration #"; }
    .member-table td:nth-child(2)::before { content: "Name"; }
    .member-table td:nth-child(3)::before { content: "Phone"; }
    .member-table td:nth-child(4)::before { content: "Email"; }
    .member-table td:nth-child(5)::before { content: "Status"; }
    .member-table td:nth-child(6)::before { content: "Contract"; }
    .member-table td:nth-child(7)::before { content: "Actions"; }

    .user-table td:nth-child(1)::before { content: "Username"; }
    .user-table td:nth-child(2)::before { content: "Full Name"; }
    .user-table td:nth-child(3)::before { content: "Email"; }
    .user-table td:nth-child(4)::before { content: "Role"; }
    .user-table td:nth-child(5)::before { content: "Status"; }
    .user-table td:nth-child(6)::before { content: "Last Login"; }
    .user-table td:nth-child(7)::before { content: "Actions"; }

    .data-table td:nth-child(1)::before { content: "Name"; }
    .data-table td:nth-child(2)::before { content: "Relationship"; }
    .data-table td:nth-child(3)::before { content: "DOB"; }
    .data-table td:nth-child(4)::before { content: "Age"; }
    .data-table td:nth-child(5)::before { content: "Status"; }
    .data-table td:nth-child(6)::before { content: "Payment"; }
    .data-table td:nth-child(7)::before { content: "Actions"; }

    /* Dashboard specific table labels - more specific to override default data-table */
    .dashboard-grid .card:nth-child(1) .data-table td:nth-child(1)::before { content: "Name"; }
    .dashboard-grid .card:nth-child(1) .data-table td:nth-child(2)::before { content: "Email"; }
    .dashboard-grid .card:nth-child(1) .data-table td:nth-child(3)::before { content: "Status"; }

    .dashboard-grid .card:nth-child(2) .data-table td:nth-child(1)::before { content: "Member"; }
    .dashboard-grid .card:nth-child(2) .data-table td:nth-child(2)::before { content: "Amount"; }
    .dashboard-grid .card:nth-child(2) .data-table td:nth-child(3)::before { content: "Status"; }

    .payment-table td:nth-child(1)::before { content: "Date"; }
    .payment-table td:nth-child(2)::before { content: "Member"; }
    .payment-table td:nth-child(3)::before { content: "Amount"; }
    .payment-table td:nth-child(4)::before { content: "Method"; }
    .payment-table td:nth-child(5)::before { content: "Status"; }
    .payment-table td:nth-child(6)::before { content: "Reference"; }
    .payment-table td:nth-child(7)::before { content: "Actions"; }

    .member-table td::before,
    .user-table td::before,
    .data-table td::before,
    .payment-table td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-tertiary);
        flex-basis: 40%;
        flex-shrink: 0;
    }

    /* Action buttons in mobile cards */
    .action-btns {
        flex-wrap: wrap;
        width: 100%;
        justify-content: flex-end;
    }

    /* Increase touch target size */
    .btn-icon {
        width: 40px;
        height: 40px;
        min-width: 40px;
        min-height: 40px;
    }

    .btn-icon svg {
        width: 18px;
        height: 18px;
    }
}

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

@media (max-width: 768px) {
    /* Force single column for all form grids */
    .form-grid {
        grid-template-columns: 1fr !important;
        gap: 16px;
        width: 100%;
        max-width: 100%;
    }

    .info-grid {
        grid-template-columns: 1fr !important;
        gap: 16px;
        width: 100%;
        max-width: 100%;
    }

    /* Form sections */
    .form-section {
        padding: 16px;
    }

    .form-section-title {
        font-size: 1rem;
        margin-bottom: 16px;
    }

    /* Form actions */
    .form-actions {
        flex-direction: column;
        gap: 8px;
    }

    .form-actions .btn {
        width: 100%;
        justify-content: center;
    }

    /* Radio groups */
    .radio-group {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }

    /* Increase input padding for easier touch */
    .form-control,
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="date"],
    input[type="tel"],
    input[type="url"],
    textarea,
    select {
        padding: 12px 14px;
        font-size: 16px; /* Prevents iOS zoom on focus */
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }
}

/* ============================================
   FILTER BARS & SEARCH
   ============================================ */

@media (max-width: 768px) {
    .filter-bar {
        flex-direction: column;
        gap: 12px;
        padding: 16px !important;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .filter-bar .search-box {
        width: 100%;
        min-width: 0;
        max-width: 100%;
        box-sizing: border-box;
    }

    .filter-bar select,
    .filter-bar .form-control {
        width: 100%;
        min-width: 0;
        max-width: 100%;
        box-sizing: border-box;
    }

    .filter-bar .btn {
        width: 100%;
        max-width: 100%;
        justify-content: center;
        box-sizing: border-box;
    }

    /* Payment filters grid */
    .filters-section {
        padding: 16px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .filter-grid {
        grid-template-columns: 1fr !important;
        gap: 12px;
        width: 100%;
        max-width: 100%;
    }

    .filter-actions {
        flex-direction: column !important;
        gap: 8px !important;
        width: 100%;
        max-width: 100%;
    }

    .filter-actions .btn {
        width: 100%;
        max-width: 100%;
        justify-content: center;
        box-sizing: border-box;
    }
}

/* ============================================
   PAGE HEADERS
   ============================================ */

@media (max-width: 768px) {
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 20px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .page-header h1 {
        font-size: 1.5rem;
        line-height: 1.2;
        word-wrap: break-word;
        overflow-wrap: break-word;
        max-width: 100%;
    }

    .page-header > div {
        width: 100%;
        max-width: 100%;
        flex-direction: column;
        box-sizing: border-box;
    }

    .page-header .btn {
        width: 100%;
        max-width: 100%;
        justify-content: center;
        box-sizing: border-box;
    }
}

/* ============================================
   TABS - HORIZONTAL SCROLL ON MOBILE
   ============================================ */

@media (max-width: 768px) {
    .tabs {
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE/Edge */
        flex-wrap: nowrap;
        gap: 0;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

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

    .tab-btn {
        flex-shrink: 0;
        padding: 12px 20px;
        white-space: nowrap;
        font-size: 0.875rem;
        min-width: 120px;
        text-align: center;
    }
}

/* ============================================
   MEMBER DETAIL VIEW
   ============================================ */

@media (max-width: 768px) {
    .member-header {
        padding: 16px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .member-title {
        flex-direction: column;
        gap: 12px;
        align-items: flex-start;
        width: 100%;
        max-width: 100%;
    }

    .member-title h2 {
        font-size: 1.25rem;
    }

    .member-meta {
        flex-direction: column;
        gap: 12px;
    }

    .member-meta-item {
        width: 100%;
        padding-bottom: 12px;
        border-bottom: 1px solid var(--border-light);
    }

    .member-meta-item:last-child {
        border-bottom: none;
        padding-bottom: 0;
    }

    /* Info cards */
    .info-card {
        padding: 16px;
    }

    .info-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
        padding: 12px 0;
    }

    .info-label {
        font-weight: 600;
        color: var(--text-tertiary);
    }

    .info-value {
        text-align: left;
        word-break: break-word;
    }
}

/* ============================================
   CARDS & CONTAINERS
   ============================================ */

@media (max-width: 768px) {
    .card {
        padding: 16px;
        border-radius: var(--radius-md);
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .container {
        padding: 20px 16px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }
}

/* ============================================
   BUTTONS & ACTIONS
   ============================================ */

@media (max-width: 768px) {
    /* Ensure all buttons have proper touch targets */
    .btn {
        min-height: 44px;
        padding: 12px 20px;
        font-size: 1rem;
    }

    .btn-sm {
        min-height: 40px;
        padding: 10px 16px;
        font-size: 0.9375rem;
    }

    /* Navigation links */
    .nav-links {
        flex-direction: column;
        width: 100%;
    }

    .nav-links .btn {
        width: 100%;
        text-align: center;
    }

    /* Action buttons in table cells */
    .payment-table td:last-child a {
        display: inline-block;
        margin: 4px 0;
        min-width: 80px;
        text-align: center;
    }
}

/* ============================================
   CONTRACT TIMELINE
   ============================================ */

@media (max-width: 768px) {
    .contract-timeline {
        padding: 16px;
    }

    .timeline-dates {
        flex-direction: column;
        gap: 8px;
        font-size: 0.8125rem;
    }
}

/* ============================================
   PAGINATION
   ============================================ */

@media (max-width: 768px) {
    .pagination {
        flex-wrap: wrap;
        gap: 4px;
        justify-content: center;
    }

    .pagination .page-link {
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 8px 12px;
    }

    /* Hide page numbers on very small screens, keep only prev/next */
    @media (max-width: 480px) {
        .pagination .page-item:not(.prev):not(.next) {
            display: none;
        }

        .pagination .page-item.prev,
        .pagination .page-item.next,
        .pagination .page-item.active {
            display: block;
        }
    }
}

/* ============================================
   BADGES & STATUS INDICATORS
   ============================================ */

@media (max-width: 768px) {
    .status-badge,
    .role-badge,
    .payment-badge {
        font-size: 0.75rem;
        padding: 6px 10px;
        white-space: nowrap;
    }
}

/* ============================================
   ALERTS & NOTIFICATIONS
   ============================================ */

@media (max-width: 768px) {
    .alert {
        padding: 12px 14px;
        font-size: 0.875rem;
        margin-bottom: 16px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
}

/* ============================================
   DASHBOARD SPECIFIC
   ============================================ */

@media (max-width: 768px) {
    /* Force stat cards to single column on mobile */
    .stat-grid {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
        width: 100%;
        max-width: 100%;
    }

    .stat-card {
        padding: 16px !important;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    .stat-value {
        font-size: 1.75rem !important;
    }

    .stat-label,
    .stat-detail {
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    /* Force grid-2 and grid-3 to single column on mobile */
    .grid,
    .grid-2,
    .grid-3 {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
        width: 100%;
        max-width: 100%;
    }

    /* Quick actions grid */
    .quick-actions {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
        width: 100%;
        max-width: 100%;
    }

    .action-btn {
        padding: 16px;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        display: flex !important;
        align-items: center;
        text-align: left;
        gap: 12px;
    }

    .action-icon {
        margin: 0 !important;
        flex-shrink: 0;
    }

    /* Dashboard tables */
    .dashboard-grid .data-table {
        width: 100%;
        max-width: 100%;
    }

    /* Section titles and card headers */
    .section-title,
    .card-header {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .card-header h3 {
        margin: 0;
        font-size: 1.125rem;
    }
}

/* ============================================
   EMPTY STATES
   ============================================ */

@media (max-width: 768px) {
    .empty-state {
        padding: 32px 16px;
    }

    .empty-state .btn {
        width: 100%;
        max-width: 300px;
    }
}

/* ============================================
   LOGIN PAGE
   ============================================ */

@media (max-width: 768px) {
    .login-container {
        padding: 16px;
    }

    .login-card {
        max-width: 100%;
    }

    .login-header h1 {
        font-size: 1.5rem;
    }
}

/* ============================================
   UTILITY CLASSES FOR MOBILE
   ============================================ */

@media (max-width: 768px) {
    .mobile-hide {
        display: none !important;
    }

    .mobile-show {
        display: block !important;
    }

    .mobile-text-center {
        text-align: center !important;
    }

    .mobile-full-width {
        width: 100% !important;
    }
}

/* ============================================
   TABLET (768px - 1024px) ADJUSTMENTS
   ============================================ */

@media (min-width: 768px) and (max-width: 1024px) {
    .sidebar {
        width: 200px;
    }

    .main-content {
        margin-left: 200px;
    }

    .topbar {
        padding: 0 var(--space-4);
    }

    .content-area {
        padding: var(--space-4);
    }

    /* Adjust table font sizes */
    .member-table th,
    .user-table th,
    .data-table th {
        font-size: 0.6875rem;
        padding: 10px;
    }

    .member-table td,
    .user-table td,
    .data-table td {
        padding: 10px;
        font-size: 0.875rem;
    }
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
    .sidebar,
    .topbar,
    .mobile-menu-btn,
    .sidebar-overlay,
    .btn,
    .action-btns,
    .filter-bar,
    .page-header > div {
        display: none !important;
    }

    .main-content {
        margin-left: 0;
    }

    .content-area {
        padding: 0;
    }
}

/* ============================================
   ACCESSIBILITY ENHANCEMENTS
   ============================================ */

/* Focus indicators for keyboard navigation */
@media (max-width: 768px) {
    a:focus,
    button:focus,
    input:focus,
    select:focus,
    textarea:focus {
        outline: 3px solid var(--accent-primary);
        outline-offset: 2px;
    }

    /* Ensure touch targets are large enough */
    a,
    button,
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 44px;
        min-height: 44px;
    }
}

/* ============================================
   LANDSCAPE MOBILE OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) and (orientation: landscape) {
    .topbar {
        height: 56px;
        min-height: 56px;
    }

    .topbar-welcome h3 {
        font-size: 0.875rem;
    }

    .sidebar {
        max-width: 280px;
    }
}
/* VERY SMALL SCREENS (< 375px) */
@media (max-width: 374px) {
    .content-area {
        padding: 12px;
    }

    .card,
    .stat-card {
        padding: 12px;
    }

    .page-header h1 {
        font-size: 1.25rem;
    }

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