/* ==================== MOBILE BASE STYLES ==================== */
@media (max-width: 767px) {
    /* Fix scrolling and viewport */
    html {
        overflow: hidden;
        height: 100%;
    }
    
    body {
        padding: 0;
        margin: 0;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        height: 100%;
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
    
    /* Container takes full height */
    .container {
        margin: 0;
        border-radius: 0;
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        background: white;
        overflow: hidden;
    }
    
    /* Ultra compact header */
    .header {
        padding: 8px 12px;
        min-height: 40px;
        max-height: 40px;
        position: relative;
        z-index: 100;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    }
    
    .header h1 {
        font-size: 14px;
        letter-spacing: -0.5px;
        flex: 1;
        margin: 0;
    }
    
    /* HIDE USER INFO COMPLETELY ON MOBILE */
    .user-info {
        display: none !important;
    }
    
    .user-avatar {
        display: none !important;
    }
    
    .user-details {
        display: none !important;
    }
    
    .user-name {
        display: none !important;
    }
    
    .user-role {
        display: none !important;
    }
    
    .user-menu-btn {
        display: none !important;
    }
    
    .switch-user {
        display: none !important;
    }
    
    .user-dropdown {
        display: none !important;
    }
    
    .user-dropdown-content {
        display: none !important;
    }
    
    /* Main content takes remaining space */
    .main-content {
        grid-template-columns: 1fr;
        height: calc(100% - 40px);
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }
    
    /* Hide sidebar completely */
    .sidebar {
        display: none !important;
    }
    
    /* Calendar container - scrollable area */
    .calendar-container {
        padding: 10px;
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        background: white;
        height: 100%;
        position: relative;
        z-index: 1;
    }
    
    /* Ultra compact calendar header */
    .calendar-header {
        flex-direction: column;
        gap: 8px;
        margin-bottom: 10px;
        align-items: stretch;
        position: relative;
        z-index: 10;
    }
    
    .calendar-header h2 {
        font-size: 16px;
        color: #2c3e50;
        text-align: center;
        margin: 0;
    }
    
    /* Compact month navigation - ensure clickable */
    .month-nav {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
        position: relative;
        z-index: 20;
    }
    
    .month-btn {
        width: 36px;
        height: 36px;
        min-width: 36px;
        border: 2px solid #667eea;
        background: white;
        color: #667eea;
        font-size: 18px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        padding: 0;
        cursor: pointer;
        position: relative;
        z-index: 30;
    }
    
    .month-btn:active {
        background: #667eea;
        color: white;
    }
    
    .current-month {
        font-size: 14px;
        flex: 1;
        text-align: center;
        font-weight: 600;
        color: #2c3e50;
    }
    
    /* Compact view toggle - ensure clickable */
    .view-toggle {
        width: 100%;
        margin-left: 0;
        display: flex;
        gap: 5px;
        position: relative;
        z-index: 20;
    }
    
    .toggle-btn {
        flex: 1;
        padding: 6px;
        font-size: 12px;
        border: 1px solid #667eea;
        background: white;
        color: #667eea;
        border-radius: 6px;
        font-weight: 600;
        text-align: center;
        cursor: pointer;
        position: relative;
        z-index: 30;
    }
    
    .toggle-btn.active {
        background: #667eea;
        color: white;
    }
    
    .toggle-btn:active {
        opacity: 0.8;
    }
    
    /* Ultra compact calendar grid */
    .calendar-grid {
        display: grid;
        grid-template-columns: repeat(7, 1fr);
        gap: 1px;
        padding: 0;
        background: #dee2e6;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        overflow: hidden;
        position: relative;
        z-index: 1;
    }
    
    .day-header {
        font-size: 10px;
        padding: 4px 1px;
        text-align: center;
        font-weight: 600;
        color: #667eea;
        background: white;
        border: none;
    }
    
    .day-cell {
        min-height: 40px;
        max-height: 40px;
        padding: 2px;
        border-radius: 0;
        background: white;
        position: relative;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }
    
    .day-cell.today {
        background: rgba(102, 126, 234, 0.1);
        box-shadow: inset 0 0 0 2px #667eea;
    }
    
    .day-cell.other-month {
        opacity: 0.3;
        background: #f8f9fa;
    }
    
    .day-number {
        font-size: 10px;
        margin-bottom: 1px;
        font-weight: 600;
        color: #495057;
        line-height: 1;
    }
    
    .job-item {
        font-size: 7px;
        padding: 1px 2px;
        margin-bottom: 1px;
        border-radius: 2px;
        background: #667eea;
        color: white;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: 1.2;
    }
    
    /* Different colors for job types */
    .job-item[data-period="AM"] {
        background: #28a745;
    }
    
    .job-item[data-period="PM"] {
        background: #dc3545;
    }
    
    .job-item[data-period="FULL"] {
        background: #007bff;
    }
    
    /* Small floating Add Button */
    .admin-controls {
        position: fixed;
        bottom: 15px;
        right: 15px;
        z-index: 1000;
    }
    
    .btn-primary {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0;
        box-shadow: 0 3px 8px rgba(102, 126, 234, 0.5);
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        cursor: pointer;
        position: relative;
        z-index: 1001;
    }
    
    /* Replace text with + symbol */
    .btn-primary span {
        display: none;
    }
    
    .btn-primary::after {
        content: '+';
        font-size: 24px;
        line-height: 1;
    }
    
    /* Week view - compact */
    .week-view-single {
        background: white;
        padding: 10px;
        border-radius: 8px;
    }
    
    .week-view-single .week-header {
        grid-template-columns: repeat(7, 1fr);
        gap: 1px;
        background: #dee2e6;
    }
    
    .week-view-single .week-row {
        min-height: 250px;
        grid-template-columns: repeat(7, 1fr);
        gap: 1px;
        background: #dee2e6;
    }
    
    .day-cell-week {
        min-height: 250px;
        background: white;
    }
    
    .am-section,
    .pm-section {
        padding: 3px;
    }
    
    .job-am, .job-pm, .job-all-day {
        font-size: 9px;
        padding: 2px;
    }
    
    /* Today view - compact */
    .today-view {
        background: white;
        padding: 10px;
        border-radius: 8px;
    }
    
    .today-header {
        margin-bottom: 10px;
        padding-bottom: 10px;
        border-bottom: 1px solid #dee2e6;
    }
    
    .today-header h3 {
        font-size: 18px;
        margin: 0;
    }
    
    .today-list-item {
        padding: 8px;
        background: #f8f9fa;
        border-radius: 6px;
        margin-bottom: 6px;
        font-size: 12px;
    }
    
    .today-person {
        font-weight: 600;
        color: #667eea;
        font-size: 12px;
    }
    
    .today-job {
        font-size: 12px;
    }
    
    .today-period {
        font-size: 10px;
        background: #667eea;
        color: white;
        padding: 1px 4px;
        border-radius: 8px;
        display: inline-block;
    }
    
    /* Modals */
    .modal {
        padding: 0;
        z-index: 2000;
    }
    
    .modal-content {
        width: 100%;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
        margin: 0;
        padding: 15px;
        overflow-y: auto;
        position: relative;
        z-index: 2001;
    }
    
    .form-group input,
    .form-group select,
    .form-group textarea {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 10px;
        border-radius: 6px;
    }
    
    .form-actions {
        flex-direction: column-reverse;
        gap: 8px;
        margin-top: 15px;
        position: sticky;
        bottom: 0;
        background: white;
        padding: 10px 0;
    }
    
    .form-actions .btn {
        width: 100%;
        padding: 12px;
        font-size: 14px;
    }
    
    /* Hide all user-related elements */
    .badge,
    .dropdown-divider,
    .dropdown-item {
        display: none !important;
    }
}

/* ==================== PORTRAIT SPECIFIC ==================== */
@media (max-width: 767px) and (orientation: portrait) {
    /* Extra compact for portrait */
    .calendar-grid {
        gap: 0;
    }
    
    .day-cell {
        min-height: 38px;
        max-height: 38px;
        border-right: 1px solid #f0f0f0;
        border-bottom: 1px solid #f0f0f0;
    }
    
    .day-number {
        font-size: 9px;
    }
    
    .job-item {
        font-size: 6px;
        padding: 1px;
        margin-bottom: 0;
    }
}

/* ==================== LANDSCAPE MODE ==================== */
@media (max-width: 767px) and (orientation: landscape) {
    /* Even smaller header in landscape */
    .header {
        padding: 5px 10px;
        min-height: 35px;
        max-height: 35px;
    }
    
    .header h1 {
        font-size: 12px;
    }
    
    /* Adjust calendar for landscape */
    .calendar-container {
        padding: 8px;
        height: calc(100% - 35px);
    }
    
    .calendar-header {
        margin-bottom: 8px;
        gap: 5px;
    }
    
    .calendar-header h2 {
        font-size: 14px;
    }
    
    .current-month {
        font-size: 12px;
    }
    
    .month-btn {
        width: 30px;
        height: 30px;
        font-size: 14px;
    }
    
    .toggle-btn {
        padding: 4px;
        font-size: 11px;
    }
    
    .day-header {
        font-size: 9px;
        padding: 3px 1px;
    }
    
    .day-cell {
        min-height: 30px;
        max-height: 30px;
    }
    
    .day-number {
        font-size: 8px;
    }
    
    .job-item {
        font-size: 5px;
        padding: 0 1px;
    }
    
    /* Smaller floating button in landscape */
    .admin-controls {
        bottom: 10px;
        right: 10px;
    }
    
    .btn-primary {
        width: 40px;
        height: 40px;
    }
    
    .btn-primary::after {
        font-size: 20px;
    }
}

/* ==================== SMALL PHONES ==================== */
@media (max-width: 375px) {
    .header h1 {
        font-size: 12px;
    }
    
    .day-cell {
        min-height: 35px;
        max-height: 35px;
    }
    
    .day-number {
        font-size: 8px;
    }
    
    .job-item {
        font-size: 5px;
    }
}

/* ==================== iOS SPECIFIC FIXES ==================== */
@supports (-webkit-touch-callout: none) {
    /* Fix iOS viewport and scrolling */
    body {
        position: fixed;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height */
    }
    
    .container {
        height: 100vh;
        height: 100dvh;
    }
    
    .calendar-container {
        /* Smooth scrolling on iOS */
        -webkit-overflow-scrolling: touch;
        /* Prevent elastic bounce */
        overscroll-behavior: contain;
    }
    
    /* Adjust for notch */
    .header {
        padding-top: env(safe-area-inset-top);
    }
    
    /* Adjust for home indicator */
    .admin-controls {
        bottom: calc(15px + env(safe-area-inset-bottom));
    }
}

/* ==================== TOUCH OPTIMIZATIONS ==================== */
@media (pointer: coarse) {
    /* Touch feedback */
    .btn:active,
    .toggle-btn:active,
    .month-btn:active,
    .day-cell:active {
        opacity: 0.7;
    }
    
    /* Ensure minimum touch targets where critical */
    .month-btn {
        min-width: 36px;
        min-height: 36px;
    }
    
    .btn-primary {
        min-width: 48px;
        min-height: 48px;
    }
}

/* ==================== TABLET ADJUSTMENTS ==================== */
@media (min-width: 768px) and (max-width: 1023px) {
    .container {
        margin: 10px;
        border-radius: 20px;
    }
    
    .header {
        padding: 15px 20px;
        min-height: auto;
        max-height: none;
    }
    
    .header h1 {
        font-size: 22px;
    }
    
    /* Show user info on tablets */
    .user-info {
        display: flex !important;
        padding: 8px 15px;
        gap: 10px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 20px;
    }
    
    .user-avatar {
        display: flex !important;
        width: 36px;
        height: 36px;
        font-size: 16px;
        background: white;
        color: #667eea;
        border-radius: 50%;
        align-items: center;
        justify-content: center;
    }
    
    .user-details {
        display: flex !important;
    }
    
    .main-content {
        grid-template-columns: 200px 1fr;
        height: calc(100vh - 100px);
    }
    
    .sidebar {
        display: block !important;
        padding: 15px;
    }
    
    .calendar-container {
        padding: 20px;
    }
    
    .calendar-grid {
        gap: 3px;
        padding: 10px;
    }
    
    .day-cell {
        min-height: 60px;
        max-height: none;
        padding: 5px;
    }
    
    .day-number {
        font-size: 12px;
    }
    
    .job-item {
        font-size: 10px;
        padding: 3px 5px;
    }
}

/* ==================== SCROLLBAR STYLING ==================== */
@media (max-width: 767px) {
    /* Hide scrollbar but keep scrolling */
    .calendar-container::-webkit-scrollbar {
        width: 0;
        height: 0;
        display: none;
    }
    
    .calendar-container {
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE/Edge */
    }
}

/* ==================== DEBUG Z-INDEX ==================== */
/* Ensure proper stacking order */
@media (max-width: 767px) {
    .modal {
        z-index: 9000 !important;
    }
    
    .modal-content {
        z-index: 9001 !important;
    }
    
    .admin-controls {
        z-index: 1000 !important;
    }
    
    .btn-primary {
        z-index: 1001 !important;
    }
    
    /* Calendar elements should be below buttons */
    .calendar-grid,
    .day-cell,
    .job-item {
        z-index: 1 !important;
    }
}

@media (max-width: 767px) {
    .export-btn {
        display: none; /* Hide in toggle area on mobile */
    }
    
    /* Add floating export button on mobile */
    .floating-export-btn {
        position: fixed;
        bottom: 80px; /* Above the Add button */
        right: 15px;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        box-shadow: 0 3px 8px rgba(102, 126, 234, 0.5);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        z-index: 999;
        cursor: pointer;
    }
    
    .floating-export-btn:active {
        transform: scale(0.95);
    }
    
    #calendarExportModal .modal-content {
        width: 100%;
        max-width: 100%;
        height: 100vh;
        border-radius: 0;
        padding: 20px;
        overflow-y: auto;
    }
    
    .calendar-url-input {
        font-size: 10px;
    }
}