/* ==============================================
   MOBILE-FIRST RESPONSIVE CSS
   Compatible with all phone types and screen sizes
   ============================================== */

/* CSS Custom Properties for theming */
:root {
    /* Colors */
    --primary: #4A90E2;
    --primary-dark: #357ABD;
    --primary-light: #7FB3F0;
    --secondary: #2ECC71;
    --danger: #E74C3C;
    --warning: #F39C12;
    --dark: #2C3E50;
    --gray: #95A5A6;
    --light: #ECF0F1;
    --white: #FFFFFF;
    --text-primary: #333333;
    --text-secondary: #666666;

    /* Surfaces (light mode) */
    --body-bg: #F5F7FA;
    --surface-elevated: #FFFFFF;
    --input-bg: #FFFFFF;
    --input-border: #ECF0F1;
    --input-focus-border: #4A90E2;
    --table-header-bg: #4A90E2;
    --table-stripe: transparent;
    --sidebar-bg: #FFFFFF;
    --sidebar-hover: #ECF0F1;
    --sidebar-border: #ECF0F1;
    --nav-bg: #FFFFFF;
    --nav-border: #ECF0F1;
    --nav-shadow: 0 -2px 8px rgba(0,0,0,0.05);
    --modal-overlay: rgba(0,0,0,0.5);
    --modal-bg: #FFFFFF;
    --skeleton-from: #ECF0F1;
    --skeleton-via: #F0F0F0;
    --skeleton-to: #ECF0F1;
    --focus-ring: #4A90E2;
    
    /* Spacing - scaled for mobile */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    
    /* Typography - mobile optimized */
    --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, sans-serif;
    --font-size-xs: 11px;
    --font-size-sm: 13px;
    --font-size-md: 15px;
    --font-size-lg: 17px;
    --font-size-xl: 20px;
    --font-size-xxl: 24px;
    
    /* Mobile UI elements */
    --header-height: 56px;
    --bottom-nav-height: 60px;
    --touch-target: 44px;
    --border-radius: 8px;
    --card-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Safe area insets for notched phones (iPhone X+) */
@supports(padding: max(0px)) {
    .safe-area-inset {
        padding-top: max(var(--spacing-md), env(safe-area-inset-top));
        padding-bottom: max(var(--bottom-nav-height), env(safe-area-inset-bottom));
        padding-left: max(var(--spacing-md), env(safe-area-inset-left));
        padding-right: max(var(--spacing-md), env(safe-area-inset-right));
    }
}

/* Base Reset - Mobile First */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    height: 100%;
    overflow-x: hidden;
}

body {
    font-family: var(--font-family);
    font-size: var(--font-size-md);
    line-height: 1.5;
    color: var(--text-primary);
    background-color: var(--body-bg);
    min-height: 100vh;
    min-height: -webkit-fill-available;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    touch-action: manipulation;
}

/* Mobile App Container */
.app-container {
    max-width: 480px;
    margin: 0 auto;
    background: var(--white);
    min-height: 100vh;
    position: relative;
    overflow: hidden;
}

/* Sticky Header - Mobile Optimized */
.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--primary);
    color: var(--white);
    height: var(--header-height);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    padding: 0 var(--spacing-md);
}

.hamburger-menu {
    width: var(--touch-target);
    height: var(--touch-target);
    background: none;
    border: none;
    padding: 8px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
}

.hamburger-menu span {
    display: block;
    width: 22px;
    height: 2px;
    background: var(--white);
    border-radius: 2px;
    transition: all 0.3s;
}

.app-title {
    font-size: var(--font-size-lg);
    font-weight: 600;
    letter-spacing: -0.3px;
}

.user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: var(--font-size-md);
    cursor: pointer;
}

/* Main Content Area */
.app-main {
    padding-bottom: var(--bottom-nav-height);
    min-height: calc(100vh - var(--header-height) - var(--bottom-nav-height));
}

.content-wrapper {
    padding: var(--spacing-md);
}

/* Bottom Navigation - Mobile */
.mobile-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: 480px;
    margin: 0 auto;
    background: var(--white);
    border-top: 1px solid var(--light);
    height: var(--bottom-nav-height);
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 100;
    box-shadow: 0 -2px 8px rgba(0,0,0,0.05);
}

.nav-items {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 100%;
}

.nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    color: var(--gray);
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    transition: color 0.2s;
    padding: var(--spacing-xs);
}

.nav-item.active {
    color: var(--primary);
}

.nav-icon {
    width: 24px;
    height: 24px;
    fill: currentColor;
    margin-bottom: 2px;
}

.nav-label {
    font-size: var(--font-size-xs);
    font-weight: 500;
}

/* Mobile Cards */
.mobile-card {
    background: var(--white);
    border-radius: var(--border-radius);
    padding: var(--spacing-md);
    margin-bottom: var(--spacing-md);
    box-shadow: var(--card-shadow);
    transition: transform 0.2s, box-shadow 0.2s;
}

.mobile-card:active {
    transform: scale(0.98);
}

/* Mobile Forms */
.mobile-form {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-label {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: var(--spacing-xs);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.form-input {
    width: 100%;
    height: var(--touch-target);
    padding: 0 var(--spacing-md);
    border: 2px solid var(--light);
    border-radius: var(--border-radius);
    font-size: var(--font-size-md);
    font-family: var(--font-family);
    background: var(--white);
    transition: border-color 0.2s;
    -webkit-appearance: none;
    appearance: none;
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
}

.form-input::placeholder {
    color: var(--gray);
}

/* Mobile Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--touch-target);
    padding: 0 var(--spacing-lg);
    border: none;
    border-radius: var(--border-radius);
    font-size: var(--font-size-md);
    font-weight: 600;
    font-family: var(--font-family);
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
    -webkit-appearance: none;
    appearance: none;
    touch-action: manipulation;
}

.btn-primary {
    background: var(--primary);
    color: var(--white);
    box-shadow: 0 2px 8px rgba(74, 144, 226, 0.3);
}

.btn-primary:active {
    background: var(--primary-dark);
    transform: scale(0.98);
}

.btn-secondary {
    background: var(--light);
    color: var(--text-primary);
}

.btn-danger {
    background: var(--danger);
    color: var(--white);
}

.btn-full {
    width: 100%;
}

/* Touch-friendly List Items */
.list-item {
    display: flex;
    align-items: center;
    min-height: var(--touch-target);
    padding: var(--spacing-sm) var(--spacing-md);
    background: var(--white);
    border-bottom: 1px solid var(--light);
    cursor: pointer;
    transition: background 0.2s;
}

.list-item:active {
    background: var(--light);
}

.list-item:last-child {
    border-bottom: none;
}

/* Mobile Tables - Horizontal Scroll */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(-1 * var(--spacing-md));
    padding: 0 var(--spacing-md);
}

.mobile-table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--font-size-sm);
}

.mobile-table th {
    background: var(--primary);
    color: var(--white);
    padding: var(--spacing-sm) var(--spacing-md);
    text-align: left;
    font-weight: 600;
    white-space: nowrap;
}

.mobile-table td {
    padding: var(--spacing-sm) var(--spacing-md);
    border-bottom: 1px solid var(--light);
    white-space: nowrap;
}

.mobile-table tr:active {
    background: var(--light);
}

/* Sidebar - Mobile Slide-in Menu */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    max-width: 85vw;
    height: 100vh;
    height: -webkit-fill-available;
    background: var(--white);
    box-shadow: 2px 0 16px rgba(0,0,0,0.1);
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

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

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

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

/* Sidebar header */
.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--spacing-md);
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-header h2 {
    font-size: var(--font-size-lg);
    font-weight: 700;
}

.close-btn {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    padding: var(--spacing-xs);
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
}

.sidebar-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-md);
    text-decoration: none;
    color: var(--text-primary);
    border-bottom: 1px solid var(--sidebar-border);
    transition: background 0.2s;
}

.sidebar-item:active {
    background: var(--sidebar-hover);
}

.sidebar-item.active {
    background: var(--sidebar-hover);
    color: var(--primary);
}

/* User dropdown */
.user-dropdown {
    position: absolute;
    top: var(--header-height);
    right: var(--spacing-md);
    background: var(--white);
    border-radius: var(--border-radius);
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    min-width: 160px;
    z-index: 150;
    overflow: hidden;
}

.dropdown-item {
    padding: var(--spacing-sm) var(--spacing-md);
    font-size: var(--font-size-sm);
    border-bottom: 1px solid var(--light);
}

.dropdown-item:last-child {
    border-bottom: none;
}

.dropdown-item a {
    color: var(--text-primary);
    text-decoration: none;
    display: block;
}

.dropdown-item:first-child {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Modals - Mobile Optimized */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--modal-overlay);
    z-index: 300;
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-md);
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--modal-bg);
    border-radius: 12px;
    width: 100%;
    max-width: 400px;
    max-height: 80vh;
    overflow-y: auto;
    padding: var(--spacing-lg);
    transform: translateY(20px);
    transition: transform 0.3s;
}

.modal.active .modal-content {
    transform: translateY(0);
}

/* Toast Notifications */
.toast {
    position: fixed;
    bottom: calc(var(--bottom-nav-height) + var(--spacing-md));
    left: 50%;
    transform: translateX(-50%);
    background: var(--dark);
    color: var(--white);
    padding: var(--spacing-sm) var(--spacing-lg);
    border-radius: 20px;
    font-size: var(--font-size-sm);
    z-index: 400;
    opacity: 0;
    transition: opacity 0.3s;
    white-space: nowrap;
    max-width: 90vw;
}

.toast.show {
    opacity: 1;
}

/* Skeleton Loading */
.skeleton {
    background: linear-gradient(90deg, var(--skeleton-from) 25%, var(--skeleton-via) 50%, var(--skeleton-to) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
    border-radius: var(--border-radius);
}

@keyframes shimmer {
    0% { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}

/* Button loading state */
.btn.loading {
    position: relative;
    pointer-events: none;
    opacity: 0.8;
}

.btn.loading::after {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #FFFFFF;
    border-radius: 50%;
    animation: btn-spin 0.6s linear infinite;
    margin-left: 8px;
}

.btn-secondary.loading::after {
    border-color: rgba(0,0,0,0.2);
    border-top-color: var(--text-primary);
}

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

/* Session timeout warning modal */
.timeout-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.6);
    z-index: 9999;
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-md);
}

.timeout-modal-overlay.active {
    display: flex;
}

.timeout-modal {
    background: var(--white);
    border-radius: 12px;
    padding: var(--spacing-lg);
    width: 100%;
    max-width: 360px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

.timeout-modal h3 {
    margin-bottom: var(--spacing-sm);
    color: var(--warning);
}

.timeout-modal p {
    color: var(--text-secondary);
    margin-bottom: var(--spacing-md);
    font-size: var(--font-size-sm);
}

.timeout-modal .timeout-countdown {
    font-size: var(--font-size-xxl);
    font-weight: 700;
    color: var(--danger);
    margin-bottom: var(--spacing-md);
}

.timeout-modal .timeout-actions {
    display: flex;
    gap: var(--spacing-sm);
}

.timeout-modal .btn {
    flex: 1;
}

/* Responsive Typography */
@media (max-width: 320px) {
    /* Small phones (iPhone SE, etc.) */
    :root {
        --font-size-md: 14px;
        --font-size-lg: 16px;
        --spacing-md: 12px;
    }
}

/* Tablet and larger phones */
@media (min-width: 481px) and (max-width: 768px) {
    .app-container {
        max-width: 100%;
    }
    
    .mobile-nav {
        max-width: 100%;
    }
}

/* Landscape orientation */
@media (max-width: 768px) and (orientation: landscape) {
    .app-header {
        height: 48px;
    }
    
    .bottom-nav-height {
        height: 48px;
    }
}

/* Dark mode support - Full WCAG AA palette */
@media (prefers-color-scheme: dark) {
    :root {
        /* Surfaces */
        --white: #1A1A2E;
        --light: #252540;
        --body-bg: #121220;
        --surface-elevated: #2A2A45;

        /* Text - proper contrast ratios (4.5:1+ on dark backgrounds) */
        --text-primary: #E8E8F0;
        --text-secondary: #A0A0C0;

        /* Brand colors - dark-mode variants with safe contrast */
        --primary: #6BB3FF;
        --primary-dark: #4A90E2;
        --primary-light: #8EC5FF;
        --secondary: #4ADE80;
        --danger: #FF6B6B;
        --warning: #FBBF24;
        --dark: #1E1E3A;
        --gray: #7A7AA0;

        /* Form inputs */
        --input-bg: #1E1E3A;
        --input-border: #3A3A5C;
        --input-focus-border: #6BB3FF;

        /* Table */
        --table-header-bg: #252540;
        --table-stripe: rgba(255,255,255,0.02);

        /* Shadows */
        --card-shadow: 0 2px 8px rgba(0,0,0,0.4);
        --header-shadow: 0 2px 8px rgba(0,0,0,0.5);

        /* Sidebar */
        --sidebar-bg: #1A1A2E;
        --sidebar-hover: #252540;
        --sidebar-border: #2A2A45;

        /* Nav */
        --nav-bg: #1A1A2E;
        --nav-border: #252540;
        --nav-shadow: 0 -2px 8px rgba(0,0,0,0.3);

        /* Modal */
        --modal-overlay: rgba(0,0,0,0.7);
        --modal-bg: #1A1A2E;

        /* Skeleton */
        --skeleton-from: #252540;
        --skeleton-via: #2A2A45;
        --skeleton-to: #252540;

        /* Focus ring */
        --focus-ring: #6BB3FF;
    }

    body {
        background-color: var(--body-bg);
        color: var(--text-primary);
    }

    .app-container {
        background: var(--white);
    }

    .app-header {
        box-shadow: var(--header-shadow);
    }

    .form-input {
        background: var(--input-bg);
        border-color: var(--input-border);
        color: var(--text-primary);
    }

    .form-input:focus {
        border-color: var(--input-focus-border);
    }

    .form-input::placeholder {
        color: var(--gray);
    }

    .btn-secondary {
        background: var(--light);
        color: var(--text-primary);
    }

    .list-item {
        background: var(--white);
        border-bottom-color: var(--light);
    }

    .list-item:active {
        background: var(--surface-elevated);
    }

    .mobile-nav {
        background: var(--nav-bg);
        border-top-color: var(--nav-border);
        box-shadow: var(--nav-shadow);
    }

    .sidebar {
        background: var(--sidebar-bg);
        box-shadow: 2px 0 16px rgba(0,0,0,0.5);
    }

    .sidebar-item {
        color: var(--text-secondary);
        border-bottom-color: var(--sidebar-border);
    }

    .sidebar-item:active,
    .sidebar-item.active {
        background: var(--sidebar-hover);
        color: var(--primary);
    }

    .modal-content {
        background: var(--modal-bg);
    }

    .modal {
        background: var(--modal-overlay);
    }

    .mobile-table td {
        border-bottom-color: var(--light);
    }

    .mobile-table tr:active {
        background: var(--surface-elevated);
    }

    .toast {
        background: var(--surface-elevated);
        color: var(--text-primary);
    }

    .skeleton {
        background: linear-gradient(90deg, var(--skeleton-from) 25%, var(--skeleton-via) 50%, var(--skeleton-to) 75%);
        background-size: 200% 100%;
    }

    .search-input {
        background-color: var(--input-bg);
        border-color: var(--input-border);
        color: var(--text-primary);
    }

    .chip {
        background: var(--light);
        color: var(--text-primary);
    }

    .chip.active {
        background: var(--primary);
        color: #FFFFFF;
    }

    .dropdown-item a {
        color: var(--text-primary);
    }

    .dropdown-item:active {
        background: var(--surface-elevated);
    }

    /* Login specific dark overrides */
    .login-logo {
        box-shadow: 0 4px 12px rgba(107, 179, 255, 0.3);
    }

    .empty-icon {
        opacity: 0.5;
    }

    :focus-visible {
        outline-color: var(--focus-ring);
    }

    .sidebar-header {
        border-bottom-color: var(--sidebar-border);
    }
}
