/* =====================================================
   APEX - Responsive Styles
   Mobile-first approach
   ===================================================== */

/* =====================================================
   TABLET (768px - 1024px)
   ===================================================== */
@media (max-width: 1024px) {
    /* Sidebar */
    .sidebar {
        transform: translateX(-100%);
        z-index: var(--z-modal);
    }

    .sidebar.mobile-open {
        transform: translateX(0);
    }

    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(15, 23, 42, 0.5);
        backdrop-filter: blur(4px);
        z-index: calc(var(--z-modal) - 1);
        opacity: 0;
        visibility: hidden;
        transition: all var(--transition-fast);
    }

    .sidebar-overlay.visible {
        opacity: 1;
        visibility: visible;
    }

    /* Main Content */
    .main-content {
        margin-left: 0;
    }

    .mobile-menu-toggle {
        display: flex;
    }

    /* Search */
    .global-search {
        width: 200px;
    }

    .global-search:focus {
        width: 280px;
    }

    .search-shortcut {
        display: none;
    }

    /* Grids */
    .grid-4 {
        grid-template-columns: repeat(2, 1fr);
    }

    .grid-3 {
        grid-template-columns: repeat(2, 1fr);
    }

    /* Page Header */
    .page-header {
        flex-direction: column;
        align-items: stretch;
    }

    .page-header-actions {
        flex-wrap: wrap;
    }

    /* Filters Bar */
    .filters-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-group {
        width: 100%;
    }

    .filter-select {
        flex: 1;
    }

    .filter-search {
        width: 100%;
    }

    .filter-actions {
        margin-left: 0;
        width: 100%;
    }

    /* Slideout */
    .slideout-container {
        max-width: 100%;
    }

    /* Stat Cards */
    .stat-card {
        flex-direction: column;
        text-align: center;
    }

    .stat-icon {
        margin: 0 auto;
    }
}

/* =====================================================
   MOBILE (max-width: 768px)
   ===================================================== */
@media (max-width: 768px) {
    :root {
        --spacing-lg: 16px;
        --spacing-xl: 24px;
    }

    /* Hide desktop sidebar toggle */
    .sidebar-toggle {
        display: none;
    }

    /* Main Content */
    .main-content {
        padding-bottom: var(--mobile-nav-height);
    }

    /* Top Header */
    .top-header {
        height: 56px;
        padding: 0 var(--spacing-md);
    }

    .header-left {
        flex: 1;
    }

    .search-container {
        flex: 1;
    }

    .global-search {
        width: 100%;
    }

    .global-search:focus {
        width: 100%;
    }

    .header-date {
        display: none;
    }

    /* Mobile Navigation */
    .mobile-nav {
        display: flex;
    }

    /* Page Content */
    .page-content {
        padding: var(--spacing-md);
    }

    /* Grids */
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr;
    }

    .grid-auto {
        grid-template-columns: 1fr;
    }

    /* Page Header */
    .page-header-content {
        flex-direction: column;
        align-items: flex-start;
    }

    .page-header-icon {
        width: 40px;
        height: 40px;
    }

    .page-header-icon .material-icons-outlined {
        font-size: 24px;
    }

    .page-header-text h1 {
        font-size: var(--font-size-xl);
    }

    /* Cards */
    .card-header {
        padding: var(--spacing-md);
    }

    .card-body {
        padding: var(--spacing-md);
    }

    /* Modal */
    .modal {
        padding: var(--spacing-md);
    }

    .modal-container {
        max-height: calc(100vh - 32px);
    }

    .modal-container.modal-lg,
    .modal-container.modal-xl {
        max-width: 100%;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: var(--spacing-md);
    }

    /* Slideout */
    .slideout-container {
        width: 100%;
    }

    /* Buttons */
    .btn {
        padding: var(--spacing-sm) var(--spacing-md);
    }

    .btn-block-mobile {
        width: 100%;
    }

    /* Forms */
    .form-row {
        grid-template-columns: 1fr;
    }

    /* Tables */
    .table-responsive {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Tabs */
    .tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .tabs::-webkit-scrollbar {
        display: none;
    }

    .tab {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Notifications Panel */
    .notifications-panel {
        left: var(--spacing-md);
        right: var(--spacing-md);
        width: auto;
        max-height: 70vh;
    }

    /* Toast */
    .toast-container {
        left: var(--spacing-md);
        right: var(--spacing-md);
        bottom: calc(var(--mobile-nav-height) + var(--spacing-md));
    }

    .toast {
        min-width: auto;
        max-width: none;
    }

    /* Quick Actions */
    .quick-actions-grid {
        gap: var(--spacing-sm);
    }

    .quick-action-item {
        padding: var(--spacing-md);
    }

    /* List Items */
    .list-item {
        padding: var(--spacing-sm) var(--spacing-md);
    }

    /* Stat Cards - Stack */
    .stats-grid {
        display: flex;
        overflow-x: auto;
        gap: var(--spacing-md);
        padding-bottom: var(--spacing-sm);
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .stats-grid::-webkit-scrollbar {
        display: none;
    }

    .stats-grid .stat-card {
        flex: 0 0 280px;
        flex-direction: row;
        text-align: left;
    }

    .stats-grid .stat-icon {
        margin: 0;
    }

    /* Stage Progress */
    .stage-progress {
        overflow-x: auto;
        padding-bottom: var(--spacing-md);
    }

    .stage-item {
        min-width: 80px;
    }

    .stage-label {
        font-size: 10px;
    }

    /* Search Modal */
    .search-modal-container {
        margin-top: 0;
        height: 100%;
        max-height: none;
        border-radius: 0;
    }

    /* User Switch Modal */
    .user-switch-list {
        max-height: 50vh;
    }

    /* Timeline */
    .timeline-item {
        padding-left: var(--spacing-xl);
    }

    /* Kanban */
    .kanban-board {
        flex-direction: column;
    }

    .kanban-column {
        width: 100%;
        max-height: 400px;
    }
}

/* =====================================================
   SMALL MOBILE (max-width: 480px)
   ===================================================== */
@media (max-width: 480px) {
    /* Login */
    .login-container {
        padding: var(--spacing-lg);
    }

    .login-logo img {
        width: 60px;
        height: 60px;
    }

    .login-title {
        font-size: var(--font-size-2xl);
    }

    /* Header */
    .top-header {
        padding: 0 var(--spacing-sm);
    }

    .header-btn:not(#mobile-menu-toggle) {
        padding: var(--spacing-xs);
    }

    /* Page Header */
    .page-header-actions {
        width: 100%;
    }

    .page-header-actions .btn {
        flex: 1;
    }

    /* Cards */
    .card {
        border-radius: var(--radius-md);
    }

    /* Forms */
    .form-control {
        height: 44px;
    }

    /* Modals */
    .modal-footer {
        flex-direction: column;
    }

    .modal-footer .btn {
        width: 100%;
    }

    /* Filters */
    .filter-group {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-label {
        margin-bottom: var(--spacing-xs);
    }

    /* Avatar */
    .avatar-group .avatar {
        margin-left: -12px;
    }

    /* Badges */
    .badge {
        font-size: 10px;
        padding: 2px 6px;
    }

    /* Text sizing */
    .stat-value {
        font-size: var(--font-size-2xl);
    }

    h1 { font-size: var(--font-size-2xl); }
    h2 { font-size: var(--font-size-xl); }
    h3 { font-size: var(--font-size-lg); }
}

/* =====================================================
   LANDSCAPE PHONE
   ===================================================== */
@media (max-width: 768px) and (orientation: landscape) {
    .mobile-nav {
        height: 56px;
    }

    .mobile-nav-fab {
        top: -12px;
        width: 48px;
        height: 48px;
    }

    .modal-body {
        max-height: 60vh;
    }

    .notifications-panel {
        max-height: 60vh;
    }
}

/* =====================================================
   HIGH DPI SCREENS
   ===================================================== */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .sidebar-logo,
    .login-logo img {
        image-rendering: -webkit-optimize-contrast;
    }
}

/* =====================================================
   PRINT STYLES
   ===================================================== */
@media print {
    .sidebar,
    .mobile-nav,
    .top-header,
    .modal,
    .slideout,
    .toast-container,
    .notifications-panel,
    .btn,
    .header-btn {
        display: none !important;
    }

    .main-content {
        margin: 0;
    }

    .page-content {
        padding: 0;
    }

    .card {
        box-shadow: none;
        border: 1px solid #ddd;
        page-break-inside: avoid;
    }

    body {
        background: white;
        color: black;
    }

    a {
        color: black;
        text-decoration: underline;
    }
}

/* =====================================================
   REDUCED MOTION
   ===================================================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* =====================================================
   MAP SPECIFIC RESPONSIVE
   ===================================================== */
@media (max-width: 768px) {
    /* Map Container */
    .map-container {
        height: calc(100vh - var(--header-height) - var(--mobile-nav-height));
    }

    /* Map Controls */
    .map-controls {
        top: var(--spacing-sm);
        right: var(--spacing-sm);
        gap: var(--spacing-xs);
    }

    .map-control-btn {
        width: 40px;
        height: 40px;
    }

    /* Map View Toggle */
    .map-view-toggle {
        top: var(--spacing-sm);
        left: var(--spacing-sm);
    }

    /* Map Info Window */
    .map-info-window {
        width: calc(100vw - 32px);
        max-width: 320px;
    }

    /* Map Search */
    .map-search-container {
        left: var(--spacing-sm);
        right: var(--spacing-sm);
        top: auto;
        bottom: var(--spacing-md);
    }

    .map-search-input {
        height: 44px;
    }

    /* Lead Quick View */
    .lead-quick-view {
        position: fixed;
        left: 0;
        right: 0;
        bottom: var(--mobile-nav-height);
        max-height: 50vh;
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
        box-shadow: var(--shadow-2xl);
        transform: translateY(100%);
        transition: transform var(--transition-normal);
    }

    .lead-quick-view.visible {
        transform: translateY(0);
    }

    /* Map Legend */
    .map-legend {
        bottom: var(--spacing-md);
        left: var(--spacing-sm);
        right: var(--spacing-sm);
        max-width: none;
    }

    .map-legend.collapsed {
        width: auto;
    }
}

/* =====================================================
   FORM BUILDER RESPONSIVE
   ===================================================== */
@media (max-width: 1024px) {
    .form-builder-container {
        flex-direction: column;
    }

    .form-builder-sidebar {
        width: 100%;
        height: auto;
        max-height: 200px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .form-builder-canvas {
        min-height: 400px;
    }

    .form-builder-properties {
        width: 100%;
        border-left: none;
        border-top: 1px solid var(--border-color);
    }
}

@media (max-width: 768px) {
    .field-palette {
        display: flex;
        overflow-x: auto;
        gap: var(--spacing-sm);
        padding-bottom: var(--spacing-sm);
    }

    .field-palette-item {
        flex: 0 0 auto;
        flex-direction: column;
        width: 80px;
        padding: var(--spacing-sm);
    }

    .field-palette-item .material-icons-outlined {
        margin-bottom: var(--spacing-xs);
    }

    .canvas-field {
        padding: var(--spacing-sm);
    }

    .property-panel {
        padding: var(--spacing-sm);
    }
}

/* =====================================================
   DASHBOARD RESPONSIVE
   ===================================================== */
@media (max-width: 1280px) {
    .quick-stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 1024px) {
    .dashboard-grid {
        grid-template-columns: 1fr;
    }

    .dashboard-grid-new {
        grid-template-columns: 1fr;
    }

    .dashboard-sidebar {
        order: -1;
    }

    .dashboard-widget.wide {
        grid-column: span 1;
    }
}

@media (max-width: 768px) {
    .quick-stats-grid {
        grid-template-columns: 1fr;
    }

    .quick-stat-card {
        padding: var(--spacing-md);
    }

    .quick-stat-value {
        font-size: 24px;
    }

    .action-card {
        flex-wrap: wrap;
        gap: var(--spacing-sm);
    }

    .action-card .btn {
        width: 100%;
        justify-content: center;
    }

    .dashboard-stats {
        display: flex;
        overflow-x: auto;
        gap: var(--spacing-md);
        margin: 0 calc(var(--spacing-md) * -1);
        padding: 0 var(--spacing-md) var(--spacing-sm);
    }

    .dashboard-stats .stat-card {
        flex: 0 0 260px;
    }

    .chart-container {
        height: 250px;
    }

    .leaderboard-item {
        padding: var(--spacing-sm);
    }

    .alerts-list {
        max-height: 300px;
    }

    .recent-order-item {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--spacing-sm);
    }

    .order-item-meta {
        flex-direction: row;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .schedule-item {
        flex-direction: column;
        align-items: flex-start;
    }

    .schedule-item-time {
        min-width: auto;
    }
}

/* =====================================================
   CALENDAR RESPONSIVE
   ===================================================== */
@media (max-width: 1024px) {
    .calendar-container {
        flex-direction: column;
    }

    .calendar-sidebar {
        width: 100%;
        flex-direction: row;
        flex-wrap: wrap;
        gap: var(--spacing-md);
    }

    .calendar-sidebar > * {
        flex: 1;
        min-width: 250px;
    }

    .mini-calendar {
        min-width: 280px;
    }
}

@media (max-width: 768px) {
    .calendar-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: var(--spacing-sm);
    }

    .calendar-nav {
        justify-content: space-between;
    }

    .calendar-title {
        font-size: var(--font-size-lg);
        min-width: auto;
    }

    .calendar-views {
        width: 100%;
    }

    .calendar-views .btn-group {
        width: 100%;
        display: flex;
    }

    .calendar-views .btn {
        flex: 1;
    }

    .calendar-sidebar {
        flex-direction: column;
        max-height: fit-content;
    }

    .calendar-sidebar > * {
        min-width: 100%;
    }

    .calendar-grid {
        font-size: var(--font-size-xs);

    }

    .calendar-header-cell {
        padding: var(--spacing-xs);
        font-size: var(--font-size-xs);
    }

    .calendar-row {
        min-height: 70px;
    }

    .calendar-cell {
        padding: 2px;
    }

    .cell-date {
        font-size: var(--font-size-xs);
    }

    .calendar-event {
        font-size: 10px;
        padding: 2px 4px;
    }

    /* Hide sidebar on mobile, show only main calendar */
    .calendar-sidebar {
        display: none;
    }

    /* Week view mobile */
    .week-view .calendar-header-row,
    .hour-row {
        grid-template-columns: 50px repeat(7, 1fr);
    }

    .time-cell {
        font-size: 10px;
        padding: 2px;
    }

    /* Day view takes full width */
    .day-view .hour-row {
        grid-template-columns: 50px 1fr;
    }
}

/* =====================================================
   KANBAN RESPONSIVE
   ===================================================== */
@media (max-width: 1024px) {
    .kanban-board {
        overflow-x: auto;
        padding-bottom: var(--spacing-md);
    }

    .kanban-column {
        min-width: 280px;
        flex-shrink: 0;
    }
}

@media (max-width: 768px) {
    .kanban-board {
        flex-direction: column;
        overflow-x: visible;
    }

    .kanban-column {
        width: 100%;
        min-width: auto;
        max-height: none;
    }

    .kanban-column-body {
        display: flex;
        overflow-x: auto;
        gap: var(--spacing-sm);
        padding-bottom: var(--spacing-sm);
    }

    .kanban-card {
        flex: 0 0 260px;
    }
}

/* =====================================================
   ANALYTICS RESPONSIVE
   ===================================================== */
@media (max-width: 1024px) {
    .analytics-grid {
        grid-template-columns: 1fr;
    }

    .chart-row {
        flex-direction: column;
    }

    .chart-widget {
        width: 100%;
    }
}

@media (max-width: 768px) {
    .analytics-filters {
        flex-direction: column;
    }

    .date-range-picker {
        width: 100%;
    }

    .export-buttons {
        width: 100%;
        justify-content: stretch;
    }

    .export-buttons .btn {
        flex: 1;
    }

    .kpi-grid {
        display: flex;
        overflow-x: auto;
        gap: var(--spacing-md);
    }

    .kpi-card {
        flex: 0 0 200px;
    }
}

/* =====================================================
   USER MANAGEMENT RESPONSIVE
   ===================================================== */
@media (max-width: 768px) {
    .user-card {
        flex-direction: column;
        text-align: center;
    }

    .user-card-avatar {
        margin-bottom: var(--spacing-sm);
    }

    .user-card-actions {
        width: 100%;
        justify-content: center;
    }

    .hierarchy-tree {
        padding-left: var(--spacing-md);
    }

    .hierarchy-node {
        margin-left: var(--spacing-sm);
    }
}

/* =====================================================
   TEAM PERFORMANCE RESPONSIVE
   ===================================================== */
@media (max-width: 768px) {
    .team-member-card {
        flex-direction: column;
    }

    .member-stats {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        gap: var(--spacing-md);
    }

    .member-stat {
        text-align: center;
    }
}

/* =====================================================
   PROFILE RESPONSIVE
   ===================================================== */
@media (max-width: 768px) {
    .profile-header {
        flex-direction: column;
        text-align: center;
    }

    .profile-avatar {
        margin-bottom: var(--spacing-md);
    }

    .profile-actions {
        width: 100%;
        justify-content: center;
    }

    .profile-tabs {
        overflow-x: auto;
    }

    .activity-timeline {
        padding-left: var(--spacing-lg);
    }
}

/* =====================================================
   ORDERS FILTERS RESPONSIVE
   ===================================================== */
@media (max-width: 1024px) {
    .filters-bar-extended .filters-row-secondary {
        flex-wrap: wrap;
    }

    .filters-bar-extended .filter-chips {
        width: 100%;
        margin-bottom: var(--spacing-xs);
    }
}

@media (max-width: 768px) {
    .filters-bar-extended {
        padding: var(--spacing-sm);
    }

    .filters-bar-extended .filters-row-primary {
        flex-direction: column;
        align-items: stretch;
    }

    .filters-bar-extended .filter-group {
        width: 100%;
    }

    .filters-bar-extended .filter-search {
        min-width: unset;
    }

    .filters-bar-extended .filters-row-secondary {
        flex-direction: column;
        align-items: stretch;
    }

    .filters-bar-extended .filter-chips {
        justify-content: flex-start;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: var(--spacing-xs);
    }

    .filters-bar-extended .filter-chip {
        flex-shrink: 0;
    }

    .filters-bar-extended .filter-toggle {
        width: 100%;
        justify-content: flex-start;
    }

    .filters-bar-extended .filter-actions {
        width: 100%;
        margin-left: 0;
        justify-content: center;
    }

    .filters-bar-extended .filters-row-dates {
        flex-direction: column;
        align-items: stretch;
    }

    .filters-bar-extended .filters-row-dates .filter-group-sm {
        width: 100%;
    }

    .filters-bar-extended .filters-row-dates .filter-group-sm .form-control {
        width: 100%;
    }
}
