/**
 * Responsive Design Styles v2
 * Mobile-first breakpoints for Timesheet Express
 * Bainbridge Builders Inc.
 * Version: v2025-12-19 - RcodexUI Review Fixes
 *
 * CHANGES FROM v1:
 * - Added safe area insets for notched devices
 * - Fixed iOS input zoom issue (16px minimum)
 * - Improved landscape orientation support
 * - Better horizontal scroll indicators
 * - Enhanced touch device hover state handling
 * - Optimized print styles
 */

/* ========== BREAKPOINTS ========== */
/*
 * xs: 320px  - Mobile phones
 * sm: 640px  - Large phones
 * md: 768px  - Tablets
 * lg: 1024px - Small desktops
 * xl: 1280px - Desktops
 * 2xl: 1536px - Large desktops
 */

/* ========== SAFE AREA SUPPORT (Notched Devices) ========== */
.app-container {
    /* Support iPhone X+ notches and safe areas */
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-bottom: env(safe-area-inset-bottom);
}

/* Ensure fixed elements respect safe areas */
.app-header,
.left-sidebar,
.right-sidebar {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
}

.app-header {
    padding-top: max(12px, env(safe-area-inset-top));
}

/* ========== BASE (Mobile First - 320px+) ========== */
.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
}

.left-sidebar,
.right-sidebar {
    display: none; /* Hidden on mobile */
}

.main-content {
    flex: 1;
    width: 100%;
}

/* Mobile: Stack everything vertically */
.summary-header-top {
    flex-direction: column;
    gap: 12px;
}

.report-dates {
    flex-direction: column;
    gap: 10px;
}

.date-picker-group {
    width: 100%;
}

.date-picker-group input[type="date"] {
    width: 100%;
    font-size: 16px; /* CRITICAL: Prevent iOS zoom */
}

.header-actions {
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
}

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

.stat-box {
    min-width: 0; /* Allow flex shrink */
}

.stat-separator {
    display: none; /* Hide on mobile */
}

/* ========== MOBILE TABLE - HORIZONTAL SCROLL ========== */
.table-scroll-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
}

/* Scroll indicator shadow */
.table-scroll-wrapper::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 30px;
    background: linear-gradient(to left, rgba(0, 0, 0, 0.1), transparent);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.table-scroll-wrapper.has-scroll::after {
    opacity: 1;
}

.detail-table {
    min-width: 800px;
    font-size: 12px;
}

.detail-table th,
.detail-table td {
    padding: 10px 6px; /* Increased for touch */
    white-space: nowrap;
}

/* Alternative: Hide less critical columns on mobile */
@media (max-width: 640px) {
    .detail-table .col-miles,
    .detail-table .col-notes {
        /* Optionally hide - uncomment if horizontal scroll is problematic */
        /* display: none; */
    }
}

/* Employee header - stack on mobile */
.employee-header-row {
    flex-direction: column;
    gap: 12px;
}

.employee-header-right {
    width: 100%;
}

.employee-badges-header {
    flex-wrap: wrap;
    gap: 6px;
}

/* ========== IOS INPUT ZOOM FIX ========== */
/* CRITICAL: All inputs must be 16px or larger on mobile to prevent auto-zoom */
@media (max-width: 768px) {
    input,
    select,
    textarea,
    .date-input,
    .setting-input,
    .form-input {
        font-size: 16px !important;
    }

    /* Exception for display-only elements */
    .stat-value,
    .summary-value,
    .day-hours {
        font-size: inherit; /* Can be smaller */
    }
}

/* ========== SMALL PHONES (640px+) ========== */
@media (min-width: 640px) {
    .summary-header-top {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .report-dates {
        flex-direction: row;
        gap: 12px;
    }

    .date-picker-group {
        width: auto;
    }

    .summary-stats {
        display: flex;
        flex-wrap: wrap;
    }

    .stat-box {
        flex: 0 0 auto;
        min-width: 100px;
    }

    .detail-table {
        font-size: 12px;
    }

    .detail-table th,
    .detail-table td {
        padding: 10px 8px;
    }
}

/* ========== TABLETS (768px+) ========== */
@media (min-width: 768px) {
    .app-container {
        flex-direction: row;
    }

    /* Show left sidebar */
    .left-sidebar {
        display: flex;
        flex-direction: column;
        width: 280px;
        min-width: 280px;
        position: sticky;
        top: 0;
        height: 100dvh;
        overflow-y: auto;
    }

    .main-content {
        flex: 1;
        min-width: 0; /* Prevent flex overflow */
    }

    /* Right sidebar still hidden on tablet */
    .right-sidebar {
        display: none;
    }

    .stat-separator {
        display: block;
    }

    /* Show all table columns on tablet */
    .detail-table th,
    .detail-table td {
        display: table-cell;
    }

    .employee-header-row {
        flex-direction: row;
    }

    /* Inputs can be smaller on tablet */
    input,
    select,
    textarea {
        font-size: 14px;
    }
}

/* ========== SMALL DESKTOPS (1024px+) ========== */
@media (min-width: 1024px) {
    .left-sidebar {
        width: 300px;
        min-width: 300px;
    }

    .detail-table {
        font-size: 12px;
        min-width: auto;
    }

    /* Show all table columns */
    .detail-table th,
    .detail-table td {
        display: table-cell;
    }
}

/* ========== DESKTOPS (1280px+) ========== */
@media (min-width: 1280px) {
    .left-sidebar {
        width: 320px;
        min-width: 320px;
    }

    /* Show right sidebar */
    .right-sidebar {
        display: flex;
        flex-direction: column;
        width: 360px;
        min-width: 360px;
        position: sticky;
        top: 0;
        height: 100dvh;
        overflow-y: auto;
    }

    .summary-stats {
        flex-wrap: nowrap;
    }
}

/* ========== LARGE DESKTOPS (1536px+) ========== */
@media (min-width: 1536px) {
    .left-sidebar {
        width: 360px;
        min-width: 360px;
    }

    .right-sidebar {
        width: 400px;
        min-width: 400px;
    }

    .stat-box {
        min-width: 110px;
    }
}

/* ========== MODAL RESPONSIVENESS ========== */
.modal-overlay {
    padding: 16px;
    align-items: center;
    justify-content: center;
}

.settings-modal {
    width: 100%;
    max-width: 700px;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

@media (max-width: 640px) {
    .settings-modal {
        width: 100%;
        max-width: none;
        max-height: 100dvh;
        height: 100dvh;
        border-radius: 0;
        margin: 0;
    }

    .modal-overlay {
        padding: 0;
        align-items: stretch;
    }

    /* Full-screen modal on mobile */
    .modal-header {
        padding: 16px;
        flex-shrink: 0;
    }

    .modal-body {
        flex: 1;
        overflow-y: auto;
        padding: 16px;
    }

    .modal-footer {
        padding: 16px;
        flex-shrink: 0;
    }

    .tabs-nav {
        overflow-x: auto;
        flex-wrap: nowrap;
        padding: 0 12px;
        -webkit-overflow-scrolling: touch;
    }

    .tab-btn {
        flex-shrink: 0;
        padding: 12px 16px;
        font-size: 14px;
    }

    .tabs-content {
        padding: 16px;
    }

    .settings-group {
        margin-bottom: 20px;
    }

    .btn-row {
        flex-direction: column;
        gap: 10px;
    }

    .btn-row .btn-sm,
    .btn-row .btn {
        width: 100%;
        justify-content: center;
    }
}

/* ========== CONTEXT MENU RESPONSIVENESS ========== */
.context-menu {
    max-width: calc(100vw - 32px);
}

@media (max-width: 640px) {
    /* Bottom sheet style on mobile */
    .context-menu {
        position: fixed !important;
        left: 16px !important;
        right: 16px !important;
        bottom: env(safe-area-inset-bottom, 16px) !important;
        top: auto !important;
        width: auto;
        max-height: 60vh;
        overflow-y: auto;
        border-radius: 12px 12px 0 0;
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.2);
    }

    /* Add handle for bottom sheet */
    .context-menu::before {
        content: '';
        display: block;
        width: 40px;
        height: 4px;
        background: var(--neutral-gray-border);
        border-radius: 2px;
        margin: 8px auto 12px;
    }
}

/* ========== LANDSCAPE ORIENTATION ========== */
@media (orientation: landscape) and (max-height: 500px) {
    /* Compact header in landscape */
    .summary-header,
    .app-header {
        padding: 8px 16px;
    }

    .summary-stats {
        gap: 6px;
    }

    .stat-box {
        padding: 8px 12px;
    }

    .stat-box .value {
        font-size: 20px;
    }

    .stat-box .label {
        font-size: 10px;
    }
}

/* Landscape tablets - optimize sidebar width */
@media (orientation: landscape) and (min-width: 768px) and (max-width: 1279px) {
    .left-sidebar {
        width: 240px;
        min-width: 240px;
    }

    /* Show right sidebar earlier in landscape */
    .right-sidebar {
        display: flex;
        width: 280px;
        min-width: 280px;
    }
}

/* ========== PRINT STYLES ========== */
@media print {
    /* Force simple layout */
    .app-container {
        display: block !important;
    }

    /* Hide interactive elements */
    .left-sidebar,
    .right-sidebar,
    .summary-header,
    .toolbar-spacer,
    .header-actions,
    .modal-overlay,
    .context-menu,
    .version-watermark,
    .toast,
    .loading-spinner,
    button,
    .btn {
        display: none !important;
    }

    .main-content {
        width: 100%;
        height: auto;
        overflow: visible;
    }

    .content-area {
        padding: 0;
        overflow: visible;
    }

    /* Optimize table for print */
    .detail-table {
        min-width: auto;
        font-size: 9pt;
        page-break-inside: avoid;
    }

    .detail-table th,
    .detail-table td {
        padding: 4pt 6pt;
        border: 1px solid #999;
    }

    .detail-table thead {
        display: table-header-group; /* Repeat on each page */
    }

    .detail-table tbody tr {
        page-break-inside: avoid;
    }

    /* Print header */
    .employee-header-row {
        border-bottom: 2pt solid #000;
        padding-bottom: 8pt;
        margin-bottom: 8pt;
    }

    /* Show full URLs for links */
    a[href]:after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
    }
}

/* ========== HOVER STATES - DEVICE-AWARE ========== */
/* Only apply hover effects on devices with hover capability */
@media (hover: hover) and (pointer: fine) {
    .toolbar-btn:hover,
    .tab-btn:hover,
    .context-menu-item:hover {
        background: rgba(0, 0, 0, 0.05);
    }

    .employee-item:hover {
        background: rgba(255, 255, 255, 0.08);
        border-color: var(--neutral-gray-hover);
    }

    .btn:hover:not(:disabled) {
        transform: translateY(-1px);
    }
}

/* Touch devices - remove hover, add active states */
@media (hover: none) {
    /* Remove all hover styles */
    .toolbar-btn:hover,
    .employee-item:hover,
    .tab-btn:hover,
    .context-menu-item:hover,
    .btn:hover {
        background: inherit;
        transform: none;
    }

    /* Add touch feedback via active state */
    .toolbar-btn:active,
    .employee-item:active,
    .tab-btn:active,
    .context-menu-item:active {
        opacity: 0.7;
        transition: opacity 0.1s;
    }

    .btn:active {
        opacity: 0.8;
        transform: scale(0.98);
    }
}

/* ========== DARK MODE SUPPORT (Future Enhancement) ========== */
@media (prefers-color-scheme: dark) {
    /* Placeholder for dark mode implementation */
    /*
    :root {
        --bg-primary: #1A1A1A;
        --bg-secondary: #2D2D2D;
        --text-primary: #E5E7EB;
        --text-secondary: #9CA3AF;
        --border-color: #404040;
    }
    */
}

/* ========== ACCESSIBILITY - FORCED COLORS MODE ========== */
@media (forced-colors: active) {
    /* Ensure borders and outlines are visible */
    button,
    .btn,
    input,
    select {
        border: 1px solid currentColor;
    }

    .employee-item.active {
        outline: 2px solid currentColor;
        outline-offset: -2px;
    }
}

/* ========== CONTAINER QUERIES (Future Enhancement) ========== */
/* When broader browser support is available, replace media queries with container queries */
/*
@container (min-width: 768px) {
    .sidebar { width: 280px; }
}
*/
