/**
 * SafeKeyLab Mobile Admin Enhancements
 * Fortune 100 Enterprise Mobile UI/UX
 *
 * Features:
 * - Touch-friendly target sizes (min 44px per WCAG)
 * - Bottom navigation for primary actions
 * - Swipe gesture support indicators
 * - Mobile-optimized forms and tables
 * - Device management specific styles
 * - Pull-to-refresh indicators
 * - Mobile-first admin actions
 */

/* ===========================================
   MOBILE TOUCH TARGETS
   Minimum 44x44px for all interactive elements
   =========================================== */

@media (max-width: 768px) {
    /* Ensure all buttons and interactive elements meet 44px minimum */
    .btn,
    .nav-item,
    .security-tab-btn,
    button,
    [role="button"],
    input[type="submit"],
    input[type="button"] {
        min-height: 44px;
        min-width: 44px;
    }

    /* Larger touch targets for icon buttons */
    .btn-icon {
        padding: 12px;
        min-width: 48px;
        min-height: 48px;
    }

    /* Checkbox and radio touch area */
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 24px;
        min-height: 24px;
    }

    /* Form inputs with proper touch sizing */
    .form-input,
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    select,
    textarea {
        min-height: 48px;
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 12px 16px;
    }

    /* Dropdown selects */
    select {
        appearance: none;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 16px center;
        padding-right: 48px;
    }
}

/* ===========================================
   MOBILE BOTTOM NAVIGATION
   Quick access to primary admin actions
   =========================================== */

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-secondary);
        border-top: 1px solid var(--border);
        padding: 8px 16px;
        padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
        z-index: 1000;
        justify-content: space-around;
        align-items: center;
    }

    .mobile-bottom-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 8px 12px;
        color: var(--text-muted);
        text-decoration: none;
        font-size: 10px;
        min-width: 64px;
        min-height: 48px;
        justify-content: center;
        transition: color 0.2s;
    }

    .mobile-bottom-nav-item svg {
        width: 24px;
        height: 24px;
        stroke: currentColor;
        fill: none;
        stroke-width: 1.5;
        margin-bottom: 4px;
    }

    .mobile-bottom-nav-item.active,
    .mobile-bottom-nav-item:active {
        color: var(--accent);
    }

    .mobile-bottom-nav-item .nav-badge {
        position: absolute;
        top: 4px;
        right: 8px;
        font-size: 9px;
        padding: 2px 5px;
        min-width: 16px;
        height: 16px;
    }

    /* Add padding to main content for bottom nav */
    .main-content {
        padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
    }
}

/* Hide on desktop */
@media (min-width: 769px) {
    .mobile-bottom-nav {
        display: none;
    }
}

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

@media (max-width: 768px) {
    /* Hamburger menu button */
    .mobile-menu-toggle {
        position: fixed;
        top: 16px;
        left: 16px;
        z-index: 200;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 48px;
        height: 48px;
        background: var(--bg-secondary);
        border: 1px solid var(--border);
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .mobile-menu-toggle:active {
        background: var(--bg-tertiary);
    }

    .mobile-menu-toggle svg {
        width: 24px;
        height: 24px;
        stroke: var(--text-primary);
        fill: none;
        stroke-width: 2;
    }

    /* Sidebar overlay */
    .mobile-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        z-index: 99;
        opacity: 0;
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
    }

    .mobile-overlay.open {
        display: block;
        opacity: 1;
    }

    /* Sidebar slide animation */
    .sidebar {
        width: 85%;
        max-width: 320px;
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
    }

    /* Close button in sidebar */
    .sidebar-close {
        position: absolute;
        top: 16px;
        right: 16px;
        width: 44px;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: transparent;
        border: none;
        cursor: pointer;
    }

    .sidebar-close svg {
        width: 24px;
        height: 24px;
        stroke: var(--text-muted);
    }

    /* Nav items with proper spacing */
    .nav-item {
        padding: 14px 24px;
        margin: 2px 8px;
        border-radius: 8px;
        border-left: none;
    }

    .nav-item.active {
        background: var(--accent-bg);
        border-left: none;
    }
}

/* ===========================================
   MOBILE TABLES & DATA VIEWS
   Card-based responsive tables
   =========================================== */

@media (max-width: 768px) {
    /* Convert tables to card view */
    .mobile-card-table {
        display: block;
    }

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

    .mobile-card-table tbody {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .mobile-card-table tr {
        display: flex;
        flex-direction: column;
        background: var(--bg-tertiary);
        border: 1px solid var(--border);
        border-radius: 8px;
        padding: 16px;
        gap: 8px;
    }

    .mobile-card-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0;
        border: none;
    }

    .mobile-card-table td::before {
        content: attr(data-label);
        font-weight: 500;
        color: var(--text-muted);
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* Data table specific */
    .data-table {
        font-size: 13px;
    }

    .data-table td {
        padding: 12px 8px;
    }

    /* Log entries */
    .log-entry {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .log-entry-time {
        order: -1;
        font-size: 11px;
    }

    .log-entry-content {
        width: 100%;
    }
}

/* ===========================================
   DEVICE MANAGEMENT MOBILE STYLES
   =========================================== */

@media (max-width: 768px) {
    /* Device list cards */
    .device-card {
        background: var(--bg-secondary);
        border: 1px solid var(--border);
        border-radius: 12px;
        padding: 16px;
        margin-bottom: 12px;
    }

    .device-card-header {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 12px;
    }

    .device-icon {
        width: 48px;
        height: 48px;
        background: var(--bg-tertiary);
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .device-icon svg {
        width: 24px;
        height: 24px;
        stroke: var(--text-secondary);
    }

    .device-icon.trusted svg {
        stroke: var(--accent);
    }

    .device-icon.pending svg {
        stroke: var(--warning);
    }

    .device-icon.revoked svg {
        stroke: var(--error);
    }

    .device-info {
        flex: 1;
        min-width: 0;
    }

    .device-name {
        font-weight: 600;
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .device-meta {
        font-size: 12px;
        color: var(--text-muted);
    }

    .device-status-badge {
        padding: 4px 10px;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .device-status-badge.trusted {
        background: var(--accent-bg);
        color: var(--accent);
    }

    .device-status-badge.pending {
        background: var(--warning-bg);
        color: var(--warning);
    }

    .device-status-badge.revoked {
        background: var(--error-bg);
        color: var(--error);
    }

    .device-card-body {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
        padding-top: 12px;
        border-top: 1px solid var(--border);
        font-size: 12px;
    }

    .device-detail {
        color: var(--text-muted);
    }

    .device-detail strong {
        display: block;
        color: var(--text-secondary);
        margin-bottom: 2px;
    }

    .device-card-actions {
        display: flex;
        gap: 8px;
        margin-top: 16px;
        padding-top: 16px;
        border-top: 1px solid var(--border);
    }

    .device-card-actions .btn {
        flex: 1;
        justify-content: center;
    }
}

/* ===========================================
   MOBILE PULL-TO-REFRESH INDICATOR
   =========================================== */

@media (max-width: 768px) {
    .pull-to-refresh {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border);
        transform: translateY(-100%);
        transition: transform 0.3s ease;
        z-index: 50;
    }

    .pull-to-refresh.visible {
        transform: translateY(0);
    }

    .pull-to-refresh-spinner {
        width: 24px;
        height: 24px;
        border: 2px solid var(--border);
        border-top-color: var(--accent);
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }

    .pull-to-refresh-text {
        margin-left: 12px;
        font-size: 13px;
        color: var(--text-secondary);
    }

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

/* ===========================================
   MOBILE ACTION SHEETS
   iOS/Android style bottom sheets
   =========================================== */

@media (max-width: 768px) {
    .action-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-secondary);
        border-top-left-radius: 16px;
        border-top-right-radius: 16px;
        padding: 16px;
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 1001;
        max-height: 80vh;
        overflow-y: auto;
    }

    .action-sheet.open {
        transform: translateY(0);
    }

    .action-sheet-handle {
        width: 40px;
        height: 4px;
        background: var(--border-bright);
        border-radius: 2px;
        margin: 0 auto 16px;
    }

    .action-sheet-title {
        font-size: 16px;
        font-weight: 600;
        text-align: center;
        margin-bottom: 20px;
        padding-bottom: 16px;
        border-bottom: 1px solid var(--border);
    }

    .action-sheet-option {
        display: flex;
        align-items: center;
        gap: 16px;
        padding: 16px;
        margin: 0 -16px;
        color: var(--text-primary);
        text-decoration: none;
        transition: background 0.2s;
        min-height: 56px;
    }

    .action-sheet-option:active {
        background: var(--bg-tertiary);
    }

    .action-sheet-option svg {
        width: 24px;
        height: 24px;
        stroke: var(--text-secondary);
        flex-shrink: 0;
    }

    .action-sheet-option.danger {
        color: var(--error);
    }

    .action-sheet-option.danger svg {
        stroke: var(--error);
    }

    .action-sheet-cancel {
        margin-top: 12px;
        padding-top: 12px;
        border-top: 1px solid var(--border);
    }

    .action-sheet-cancel .btn {
        width: 100%;
        justify-content: center;
    }

    /* Overlay for action sheet */
    .action-sheet-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1000;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .action-sheet-overlay.open {
        display: block;
        opacity: 1;
    }
}

/* ===========================================
   MOBILE MODALS
   =========================================== */

@media (max-width: 768px) {
    .modal {
        margin: 0;
        max-width: 100%;
        max-height: 100%;
        width: 100%;
        height: auto;
        max-height: 90vh;
        border-radius: 16px 16px 0 0;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        top: auto;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .modal.open {
        transform: translateY(0);
    }

    .modal-header {
        padding: 20px;
        border-bottom: 1px solid var(--border);
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .modal-body {
        padding: 20px;
        overflow-y: auto;
        max-height: calc(90vh - 140px);
    }

    .modal-footer {
        padding: 16px 20px;
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
        border-top: 1px solid var(--border);
        display: flex;
        gap: 12px;
    }

    .modal-footer .btn {
        flex: 1;
        justify-content: center;
    }
}

/* ===========================================
   MOBILE TOAST NOTIFICATIONS
   =========================================== */

@media (max-width: 768px) {
    .toast-container {
        bottom: calc(80px + env(safe-area-inset-bottom, 0px));
        left: 16px;
        right: 16px;
        top: auto;
    }

    .toast {
        width: 100%;
        max-width: none;
        border-radius: 12px;
        padding: 16px;
    }
}

/* ===========================================
   MOBILE FORMS
   =========================================== */

@media (max-width: 768px) {
    /* Form sections */
    .form-section {
        margin-bottom: 24px;
    }

    .form-section-title {
        font-size: 14px;
        font-weight: 600;
        margin-bottom: 16px;
        padding-bottom: 8px;
        border-bottom: 1px solid var(--border);
    }

    /* Form groups stacked */
    .form-row {
        flex-direction: column;
        gap: 16px;
    }

    /* Full width form actions */
    .form-actions {
        position: sticky;
        bottom: 0;
        background: var(--bg-secondary);
        padding: 16px;
        margin: 0 -16px -16px;
        border-top: 1px solid var(--border);
        display: flex;
        gap: 12px;
    }

    .form-actions .btn {
        flex: 1;
        justify-content: center;
    }

    /* Toggle switches */
    .toggle-switch {
        width: 52px;
        height: 32px;
    }

    .toggle-switch::before {
        width: 26px;
        height: 26px;
    }

    /* Search inputs */
    .search-input-wrapper {
        width: 100%;
    }

    .search-input {
        width: 100%;
    }
}

/* ===========================================
   MOBILE API KEY MANAGEMENT
   =========================================== */

@media (max-width: 768px) {
    .api-key-card {
        padding: 16px;
    }

    .api-key-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }

    .api-key-value {
        width: 100%;
        word-break: break-all;
        font-size: 12px;
    }

    .api-key-actions {
        width: 100%;
        display: flex;
        gap: 8px;
    }

    .api-key-actions .btn {
        flex: 1;
        justify-content: center;
    }
}

/* ===========================================
   MOBILE USER MANAGEMENT
   =========================================== */

@media (max-width: 768px) {
    .user-card {
        display: flex;
        flex-direction: column;
        gap: 12px;
        padding: 16px;
        background: var(--bg-secondary);
        border: 1px solid var(--border);
        border-radius: 8px;
        margin-bottom: 12px;
    }

    .user-card-header {
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .user-avatar-lg {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        background: var(--accent);
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        font-size: 18px;
        color: var(--bg-primary);
        flex-shrink: 0;
    }

    .user-details {
        flex: 1;
        min-width: 0;
    }

    .user-name-lg {
        font-weight: 600;
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .user-email-lg {
        font-size: 12px;
        color: var(--text-muted);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .user-card-body {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
        padding-top: 12px;
        border-top: 1px solid var(--border);
    }

    .user-stat {
        font-size: 12px;
        color: var(--text-muted);
    }

    .user-stat strong {
        display: block;
        color: var(--text-secondary);
        margin-bottom: 2px;
    }

    .user-card-actions {
        display: flex;
        gap: 8px;
        padding-top: 12px;
        border-top: 1px solid var(--border);
    }

    .user-card-actions .btn {
        flex: 1;
        justify-content: center;
    }
}

/* ===========================================
   MOBILE CHARTS
   =========================================== */

@media (max-width: 768px) {
    .chart-container {
        height: 200px;
        margin: 0 -8px;
    }

    .chart-legend {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        justify-content: center;
        margin-top: 12px;
    }

    .chart-legend-item {
        font-size: 11px;
        display: flex;
        align-items: center;
        gap: 6px;
    }
}

/* ===========================================
   SWIPE GESTURE HINTS
   =========================================== */

@media (max-width: 768px) {
    .swipe-hint {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 12px;
        font-size: 12px;
        color: var(--text-muted);
        background: var(--bg-tertiary);
        border-radius: 8px;
        margin-bottom: 16px;
    }

    .swipe-hint svg {
        width: 16px;
        height: 16px;
        stroke: currentColor;
    }

    /* Swipe to reveal actions */
    .swipe-actions-container {
        position: relative;
        overflow: hidden;
    }

    .swipe-actions {
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        display: flex;
        transform: translateX(100%);
        transition: transform 0.3s ease;
    }

    .swipe-actions.revealed {
        transform: translateX(0);
    }

    .swipe-action {
        width: 72px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        font-size: 11px;
    }

    .swipe-action.edit {
        background: var(--info);
        color: white;
    }

    .swipe-action.delete {
        background: var(--error);
        color: white;
    }
}

/* ===========================================
   FLOATING ACTION BUTTON (FAB)
   =========================================== */

@media (max-width: 768px) {
    .fab {
        position: fixed;
        bottom: calc(88px + env(safe-area-inset-bottom, 0px));
        right: 16px;
        width: 56px;
        height: 56px;
        border-radius: 16px;
        background: var(--accent);
        color: var(--bg-primary);
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 4px 12px rgba(0, 255, 136, 0.3);
        z-index: 100;
        transition: transform 0.2s, box-shadow 0.2s;
    }

    .fab:active {
        transform: scale(0.95);
        box-shadow: 0 2px 8px rgba(0, 255, 136, 0.2);
    }

    .fab svg {
        width: 24px;
        height: 24px;
        stroke: currentColor;
        stroke-width: 2;
    }

    /* Extended FAB */
    .fab-extended {
        width: auto;
        padding: 0 20px;
        gap: 8px;
        border-radius: 28px;
    }

    .fab-extended span {
        font-size: 14px;
        font-weight: 600;
    }
}

/* ===========================================
   LANDSCAPE MODE ADJUSTMENTS
   =========================================== */

@media (max-width: 768px) and (orientation: landscape) {
    .mobile-bottom-nav {
        flex-direction: row;
        padding: 4px 16px;
    }

    .mobile-bottom-nav-item {
        padding: 4px 16px;
        min-height: 40px;
    }

    .sidebar {
        width: 60%;
    }

    .modal {
        max-height: 80vh;
    }

    .action-sheet {
        max-height: 70vh;
    }
}

/* ===========================================
   SAFE AREA INSETS (Notched devices)
   =========================================== */

@supports (padding: env(safe-area-inset-bottom)) {
    @media (max-width: 768px) {
        .main-content {
            padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
        }

        .mobile-bottom-nav {
            padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
        }

        .action-sheet {
            padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
        }

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

        .fab {
            bottom: calc(88px + env(safe-area-inset-bottom, 0px));
        }
    }
}

/* ===========================================
   DARK MODE SPECIFIC (System preference)
   =========================================== */

@media (prefers-color-scheme: dark) {
    @media (max-width: 768px) {
        .mobile-overlay {
            background: rgba(0, 0, 0, 0.8);
        }

        .action-sheet-overlay {
            background: rgba(0, 0, 0, 0.6);
        }
    }
}

/* ===========================================
   REDUCED MOTION (Accessibility)
   =========================================== */

@media (prefers-reduced-motion: reduce) {
    .sidebar,
    .modal,
    .action-sheet,
    .pull-to-refresh,
    .swipe-actions {
        transition: none;
    }
}
