/* ============================================================================
   MOBILE-FIRST OPTIMIZATIONS
   Enhanced responsive design for mobile devices
   ============================================================================ */

/* ============================================================================
   MOBILE-SPECIFIC IMPROVEMENTS (< 768px)
   ============================================================================ */

@media (max-width: 768px) {
    /* Enhanced Typography for Mobile */
    body {
        font-size: 14px;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    h1 {
        font-size: 22px !important;
    }

    h2 {
        font-size: 18px !important;
    }

    h3 {
        font-size: 16px !important;
    }

    /* Login Logo Optimization */
    .logo-admin {
        max-width: 160px !important;
        margin-bottom: 1rem !important;
    }

    .login-box {
        padding: 32px !important;
    }

    .login-header {
        margin-bottom: 30px !important;
    }

    /* Touch-Friendly Button Sizes */
    .btn,
    button {
        min-height: 44px !important; /* Apple HIG recommendation */
        min-width: 44px !important;
        padding: 12px 16px !important;
        font-size: 14px !important;
    }

    .btn-sm {
        min-height: 36px !important;
        min-width: 36px !important;
        padding: 8px 12px !important;
        font-size: 13px !important;
    }

    /* Header Optimization */
    .dashboard-header {
        position: sticky;
        top: 0;
        z-index: 100;
        background: var(--bg-secondary);
        backdrop-filter: blur(10px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    }

    .header-logo {
        height: 28px !important;
    }

    .header-divider {
        font-size: 20px !important;
    }

    .header-left h1 {
        font-size: 16px !important;
    }

    .header-right {
        gap: 8px;
    }

    /* Hide monitoring button on small screens */
    .header-right button:first-of-type {
        display: none;
    }

    /* Stats Grid - Single Column */
    .stats-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .stat-card {
        display: flex;
        align-items: center;
        gap: 16px;
        padding: 16px !important;
        min-height: 80px;
    }

    .stat-icon {
        width: 48px !important;
        height: 48px !important;
        font-size: 20px !important;
        flex-shrink: 0;
    }

    .stat-info {
        flex: 1;
        min-width: 0; /* Allow text truncation */
    }

    .stat-info h3 {
        font-size: 24px !important;
        margin-bottom: 2px;
    }

    .stat-info p {
        font-size: 12px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Tabs - Horizontal Scroll */
    .tabs {
        overflow-x: auto;
        overflow-y: hidden;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE/Edge */
        padding-bottom: 4px;
        gap: 8px;
    }

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

    .tab-btn {
        flex: 0 0 auto;
        min-width: 120px;
        padding: 10px 16px !important;
        font-size: 13px !important;
        white-space: nowrap;
    }

    .tab-btn i {
        display: inline-block;
        margin-right: 6px;
    }

    /* Panel Optimization */
    .panel {
        border-radius: 16px;
        overflow: hidden;
    }

    .panel-header {
        padding: 16px;
        flex-direction: column;
        align-items: stretch;
    }

    .panel-header h2 {
        font-size: 16px !important;
        margin-bottom: 12px;
    }

    .panel-actions {
        width: 100%;
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .panel-actions input[type="text"],
    .panel-actions select {
        width: 100% !important;
        margin-right: 0 !important;
        margin-bottom: 8px;
    }

    .panel-actions button {
        width: 100%;
    }

    .panel-body {
        padding: 12px;
    }

    /* Table Container - Horizontal Scroll */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: -12px;
        padding: 12px;
    }

    /* TABLE AS CARDS ON MOBILE */
    .data-table {
        display: block;
        width: 100%;
    }

    .data-table thead {
        display: none; /* Hide headers on mobile */
    }

    .data-table tbody {
        display: block;
    }

    .data-table tr {
        display: block;
        background: var(--bg-tertiary);
        border-radius: 12px;
        margin-bottom: 12px;
        padding: 16px;
        border: 1px solid var(--border-color);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .data-table td {
        display: block;
        text-align: left !important;
        padding: 8px 0 !important;
        border: none !important;
        position: relative;
        padding-left: 50% !important;
        word-wrap: break-word;
    }

    .data-table td::before {
        content: attr(data-label);
        position: absolute;
        left: 0;
        width: 45%;
        padding-right: 10px;
        font-weight: 600;
        color: var(--text-secondary);
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* Special handling for status badges */
    .status-badge {
        display: inline-block !important;
        margin-top: 4px;
    }

    /* Connection table specific */
    #connections-table-body td:nth-child(1)::before {
        content: "User ID";
    }

    #connections-table-body td:nth-child(2)::before {
        content: "Username";
    }

    #connections-table-body td:nth-child(3)::before {
        content: "Client Info";
    }

    #connections-table-body td:nth-child(4)::before {
        content: "Protocol";
    }

    #connections-table-body td:nth-child(5)::before {
        content: "Last Seen";
    }

    #connections-table-body td:nth-child(6)::before {
        content: "Status";
    }

    /* Users table specific */
    #users-table-body td:nth-child(1)::before {
        content: "User ID";
    }

    #users-table-body td:nth-child(2)::before {
        content: "Username";
    }

    #users-table-body td:nth-child(3)::before {
        content: "Name";
    }

    #users-table-body td:nth-child(4)::before {
        content: "Subscription";
    }

    #users-table-body td:nth-child(5)::before {
        content: "Expires";
    }

    #users-table-body td:nth-child(6)::before {
        content: "Days Left";
    }

    #users-table-body td:nth-child(7)::before {
        content: "Revenue";
    }

    #users-table-body td:nth-child(8)::before {
        content: "Registered";
    }

    /* Payments table */
    #payments-table-body td:nth-child(1)::before {
        content: "Payment ID";
    }

    #payments-table-body td:nth-child(2)::before {
        content: "User ID";
    }

    #payments-table-body td:nth-child(3)::before {
        content: "Username";
    }

    #payments-table-body td:nth-child(4)::before {
        content: "Amount";
    }

    #payments-table-body td:nth-child(5)::before {
        content: "Currency";
    }

    #payments-table-body td:nth-child(6)::before {
        content: "Date";
    }

    /* Promo codes table */
    #promo-table-body td:nth-child(1)::before {
        content: "Code";
    }

    #promo-table-body td:nth-child(2)::before {
        content: "Discount %";
    }

    #promo-table-body td:nth-child(3)::before {
        content: "Bonus Days";
    }

    #promo-table-body td:nth-child(4)::before {
        content: "Uses";
    }

    #promo-table-body td:nth-child(5)::before {
        content: "Max Uses";
    }

    #promo-table-body td:nth-child(6)::before {
        content: "Valid Until";
    }

    #promo-table-body td:nth-child(7)::before {
        content: "Status";
    }

    #promo-table-body td:nth-child(8)::before {
        content: "Created";
    }

    /* Referrals table */
    #referrals-table-body td:nth-child(1)::before {
        content: "User ID";
    }

    #referrals-table-body td:nth-child(2)::before {
        content: "Username";
    }

    #referrals-table-body td:nth-child(3)::before {
        content: "Name";
    }

    #referrals-table-body td:nth-child(4)::before {
        content: "Clicks";
    }

    #referrals-table-body td:nth-child(5)::before {
        content: "Conversions";
    }

    #referrals-table-body td:nth-child(6)::before {
        content: "Successful";
    }

    #referrals-table-body td:nth-child(7)::before {
        content: "Rate";
    }

    /* Modals */
    .modal-content {
        width: 96%;
        max-height: 94vh;
        border-radius: 16px;
    }

    .modal-header {
        padding: 16px;
    }

    .modal-header h2 {
        font-size: 18px !important;
    }

    .modal-body {
        padding: 16px;
    }

    .modal-footer {
        padding: 16px;
        flex-direction: column-reverse;
    }

    .modal-footer button {
        width: 100%;
    }

    /* Form Groups in Modals */
    .form-group {
        margin-bottom: 16px;
    }

    .form-group label {
        font-size: 13px;
        margin-bottom: 8px;
    }

    .form-group input,
    .form-group select,
    .form-group textarea {
        font-size: 14px;
        padding: 12px;
    }

    .form-row {
        flex-direction: column;
    }

    .form-row .form-group {
        width: 100% !important;
    }

    /* User Info Grid */
    .info-grid {
        grid-template-columns: 1fr !important;
        gap: 12px;
    }

    .info-item {
        padding: 12px;
        border-radius: 8px;
        background: var(--bg-primary);
    }

    .info-item label {
        font-size: 11px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: var(--text-muted);
        display: block;
        margin-bottom: 4px;
    }

    .info-item span {
        font-size: 14px;
        font-weight: 500;
    }

    /* Action Buttons */
    .action-buttons {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .action-buttons button {
        width: 100%;
    }

    /* Footer */
    .dashboard-footer {
        padding: 16px;
        text-align: center;
        font-size: 12px;
    }

    .dashboard-footer p {
        flex-direction: column;
        gap: 8px;
    }

    /* Login Screen */
    .login-box {
        width: 100%;
        max-width: 100%;
        padding: 24px !important;
        border-radius: 16px;
    }

    .login-header h1 {
        font-size: 24px !important;
    }

    .login-header p {
        font-size: 13px;
    }

    /* Settings Section */
    .settings-section {
        padding: 16px;
        margin-bottom: 16px;
    }

    .settings-section h3 {
        font-size: 15px !important;
        margin-bottom: 12px;
    }

    .settings-section p {
        font-size: 13px;
        line-height: 1.5;
    }

    /* Badge Optimization */
    .badge {
        font-size: 11px;
        padding: 4px 8px;
    }

    /* Status Indicator */
    .status-indicator {
        font-size: 12px;
        padding: 6px 10px;
    }

    /* Admin User Badge */
    .admin-user {
        font-size: 12px;
        padding: 6px 10px;
    }

    /* Charts */
    canvas {
        max-height: 250px !important;
    }

    /* Loading States */
    .skeleton-card {
        padding: 16px;
    }
}

/* ============================================================================
   EXTRA SMALL DEVICES (< 480px)
   ============================================================================ */

@media (max-width: 480px) {
    /* Even more compact */
    .dashboard-header {
        padding: 12px;
    }

    .header-left {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }

    .header-left i {
        display: none;
    }

    .header-left h1 {
        font-size: 16px !important;
    }

    .stats-grid {
        gap: 10px;
    }

    .stat-card {
        padding: 12px !important;
        min-height: 70px;
    }

    .stat-icon {
        width: 40px !important;
        height: 40px !important;
        font-size: 18px !important;
    }

    .stat-info h3 {
        font-size: 20px !important;
    }

    .stat-info p {
        font-size: 11px;
    }

    .tab-btn {
        min-width: 100px;
        padding: 8px 12px !important;
        font-size: 12px !important;
    }

    .panel-header h2 {
        font-size: 15px !important;
    }

    .data-table tr {
        padding: 12px;
    }

    .data-table td {
        padding: 6px 0 !important;
        padding-left: 45% !important;
        font-size: 13px;
    }

    .data-table td::before {
        font-size: 11px;
        width: 40%;
    }

    .btn,
    button {
        font-size: 13px !important;
    }

    .modal-content {
        width: 98%;
        border-radius: 12px;
    }
}

/* ============================================================================
   LANDSCAPE ORIENTATION ON MOBILE
   ============================================================================ */

@media (max-width: 768px) and (orientation: landscape) {
    .dashboard-header {
        padding: 8px 16px;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .stat-card {
        min-height: 60px;
        padding: 10px !important;
    }

    .stat-icon {
        width: 36px !important;
        height: 36px !important;
        font-size: 16px !important;
    }

    .stat-info h3 {
        font-size: 18px !important;
    }

    .modal-content {
        max-height: 96vh;
    }
}

/* ============================================================================
   ACCESSIBILITY & TOUCH ENHANCEMENTS
   ============================================================================ */

@media (hover: none) and (pointer: coarse) {
    /* Device has touch capability */

    /* Larger tap targets */
    a, button, input, select, textarea {
        -webkit-tap-highlight-color: rgba(79, 70, 229, 0.3);
    }

    /* Remove hover effects that don't work on touch */
    *:hover {
        transition: none !important;
    }

    /* Better touch feedback */
    button:active,
    .btn:active {
        transform: scale(0.97);
        transition: transform 0.1s;
    }

    /* Improve select menus on mobile */
    select {
        font-size: 16px; /* Prevents zoom on iOS */
    }

    /* Improve input fields on mobile */
    input[type="text"],
    input[type="password"],
    input[type="number"],
    input[type="email"],
    input[type="tel"],
    textarea {
        font-size: 16px; /* Prevents zoom on iOS */
        min-height: 44px;
    }
}

/* ============================================================================
   SAFE AREAS FOR MODERN MOBILE DEVICES (Notches, etc.)
   ============================================================================ */

@supports (padding: env(safe-area-inset-top)) {
    .dashboard-header {
        padding-top: calc(12px + env(safe-area-inset-top));
        padding-left: calc(16px + env(safe-area-inset-left));
        padding-right: calc(16px + env(safe-area-inset-right));
    }

    .dashboard-footer {
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }

    .modal-content {
        margin-top: calc(8px + env(safe-area-inset-top));
        margin-bottom: calc(8px + env(safe-area-inset-bottom));
    }
}

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

@media print {
    .dashboard-header,
    .tabs,
    .panel-actions,
    .modal-backdrop,
    .btn {
        display: none !important;
    }

    .dashboard-content {
        background: white;
        color: black;
    }

    .data-table {
        display: table !important;
    }

    .data-table thead {
        display: table-header-group !important;
    }

    .data-table tbody {
        display: table-row-group !important;
    }

    .data-table tr {
        display: table-row !important;
        page-break-inside: avoid;
    }

    .data-table td {
        display: table-cell !important;
        padding: 8px !important;
    }

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