/**
 * ==========================================================================
 * RESPONSIVE STYLES
 * Mobile-first responsive design for the Growth Analytics Platform
 * ==========================================================================
 *
 * Breakpoints:
 * - Mobile:  < 640px
 * - Tablet:  640px - 1023px
 * - Desktop: >= 1024px
 *
 * Key Features:
 * 1. Table to Card Conversion
 * 2. Touch Target Optimization
 * 3. Navigation Adaptations
 * 4. Form Optimizations
 * 5. Chart Responsiveness
 */

/* ==========================================================================
   BASE RESPONSIVE TYPOGRAPHY
   ========================================================================== */

/* Fluid typography scale */
@media (max-width: 640px) {
    :root {
        --text-4xl: 2rem;
        --text-5xl: 2.5rem;
        --text-6xl: 3rem;
    }

    body {
        font-size: 16px; /* Prevent zoom on iOS */
    }

    h1, .dashboard-header h1 {
        font-size: var(--text-2xl);
        line-height: 1.2;
    }

    h2 {
        font-size: var(--text-xl);
    }

    h3 {
        font-size: var(--text-lg);
    }
}

/* ==========================================================================
   1. TABLE TO CARD CONVERSION
   ========================================================================== */

@media (max-width: 768px) {
    /* Hide table headers on mobile - data will be shown as cards */
    .table-responsive thead {
        display: none;
    }

    /* Convert table rows to cards */
    .table-responsive tbody tr {
        display: block;
        padding: var(--space-4);
        margin-bottom: var(--space-3);
        background: var(--bg-card);
        border: var(--border-3) solid var(--border-primary);
        box-shadow: var(--shadow-md);
        position: relative;
    }

    .table-responsive tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: var(--space-2) 0;
        border: none;
        border-bottom: 1px solid var(--border-light);
    }

    .table-responsive tbody td:last-child {
        border-bottom: none;
    }

    /* Add data labels on mobile */
    .table-responsive tbody td::before {
        content: attr(data-label);
        font-weight: var(--font-bold);
        font-size: var(--text-xs);
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-secondary);
        flex-shrink: 0;
        margin-right: var(--space-3);
    }

    /* Hide checkbox column label */
    .table-responsive tbody td[data-label=""]::before {
        display: none;
    }

    /* Actions section styling */
    .table-responsive .action-buttons {
        justify-content: flex-end;
        width: 100%;
        padding-top: var(--space-3);
        margin-top: var(--space-2);
        border-top: 1px solid var(--border-light);
    }
}

/* Alternative: Horizontal scroll for complex tables */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(var(--space-4) * -1);
    padding: 0 var(--space-4);
}

.table-scroll::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 40px;
    background: linear-gradient(to right, transparent, var(--bg-secondary));
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.table-scroll.is-scrollable::after {
    opacity: 1;
}

/* ==========================================================================
   2. TOUCH TARGET OPTIMIZATION
   ========================================================================== */

@media (pointer: coarse), (max-width: 768px) {
    /* Ensure minimum 44x44px touch targets */
    .btn,
    button,
    a.btn,
    .btn-icon,
    .nav-menu a,
    input[type="checkbox"],
    input[type="radio"],
    select,
    .form-check {
        min-height: 44px;
        min-width: 44px;
    }

    .btn-icon {
        padding: var(--space-3);
    }

    /* Increase spacing between interactive elements */
    .action-buttons {
        gap: var(--space-3);
    }

    .nav-menu {
        gap: var(--space-1);
    }

    .nav-menu a {
        padding: var(--space-3) var(--space-4);
    }

    /* Larger form controls */
    .form-control,
    .form-select {
        min-height: 48px;
        padding: var(--space-3) var(--space-4);
        font-size: 16px; /* Prevents iOS zoom */
    }

    /* Larger checkboxes */
    .form-check__input {
        width: 24px;
        height: 24px;
    }

    /* Spacious table cells */
    .table td,
    .data-table td {
        padding: var(--space-4);
    }

    /* Badge tap targets */
    .badge,
    .tag,
    .status-badge {
        min-height: 32px;
        display: inline-flex;
        align-items: center;
    }
}

/* ==========================================================================
   3. NAVIGATION ADAPTATIONS
   ========================================================================== */

@media (max-width: 768px) {
    /* Page header stacking */
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-4);
    }

    .page-header__actions {
        width: 100%;
        flex-wrap: wrap;
    }

    /* Full-width buttons on mobile */
    .page-header .btn {
        width: 100%;
        justify-content: center;
    }

    /* Bulk actions stacking */
    .bulk-actions {
        flex-wrap: wrap;
        width: 100%;
        padding: var(--space-3);
        background: var(--bg-secondary);
        border-radius: var(--radius-md);
        margin-bottom: var(--space-3);
    }

    .bulk-actions__count {
        width: 100%;
        text-align: center;
        margin-bottom: var(--space-2);
    }

    .bulk-actions .btn {
        flex: 1;
        min-width: calc(50% - var(--space-2));
    }

    /* Dashboard header */
    .dashboard-header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-4);
    }

    .dashboard-header .btn {
        width: 100%;
    }
}

/* ==========================================================================
   4. FORM OPTIMIZATIONS
   ========================================================================== */

@media (max-width: 768px) {
    /* Stack form rows */
    .form-row {
        grid-template-columns: 1fr;
        gap: var(--space-4);
    }

    /* Full-width form groups */
    .form-group {
        width: 100%;
    }

    /* Modal optimizations */
    .modal-content {
        width: 100%;
        max-width: none;
        margin: var(--space-3);
        max-height: calc(100vh - var(--space-6));
    }

    .modal-footer {
        flex-direction: column;
        gap: var(--space-2);
    }

    .modal-footer .btn {
        width: 100%;
    }

    /* Filters stacking */
    .filters-section,
    .filters {
        flex-direction: column;
    }

    .filter-group {
        width: 100%;
    }

    .filter-group select,
    .filter-group input {
        width: 100%;
    }
}

/* ==========================================================================
   5. CHART RESPONSIVENESS
   ========================================================================== */

@media (max-width: 768px) {
    .chart-container {
        height: 250px;
    }

    .chart-container.small {
        height: 180px;
    }

    .chart-container.large {
        height: 300px;
    }

    /* Stack chart details below */
    .chart-details {
        margin-top: var(--space-4);
    }

    .chart-details summary {
        padding: var(--space-3);
    }
}

/* ==========================================================================
   6. GRID ADJUSTMENTS
   ========================================================================== */

@media (max-width: 1024px) {
    .grid-cols-4 {
        grid-template-columns: repeat(2, 1fr);
    }

    .grid-cols-3 {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 768px) {
    .grid,
    .grid-cols-2,
    .grid-cols-3,
    .grid-cols-4,
    .stats-grid {
        grid-template-columns: 1fr;
    }

    /* Allow 2-col on wider phones */
    @media (min-width: 480px) {
        .stats-grid {
            grid-template-columns: repeat(2, 1fr);
        }
    }
}

/* ==========================================================================
   7. CARD ADJUSTMENTS
   ========================================================================== */

@media (max-width: 768px) {
    .card,
    .metric-card,
    .stat-card {
        padding: var(--space-4);
    }

    /* Remove hover transforms on touch devices */
    @media (hover: none) {
        .card:hover,
        .metric-card:hover,
        .stat-card:hover {
            transform: none;
            box-shadow: var(--shadow-lg);
        }
    }

    .card__header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-3);
    }

    .metric-card__value {
        font-size: var(--text-3xl);
    }

    .stat-value {
        font-size: var(--text-2xl);
    }

    .metric-card__icon {
        width: 3rem;
        height: 3rem;
        font-size: 1.5rem;
    }
}

/* ==========================================================================
   8. TOAST POSITIONING
   ========================================================================== */

@media (max-width: 640px) {
    .toast-container {
        top: auto;
        bottom: var(--space-4);
        right: var(--space-4);
        left: var(--space-4);
        max-width: none;
    }

    .toast {
        min-width: auto;
        width: 100%;
    }
}

/* ==========================================================================
   9. UTILITY CLASSES FOR RESPONSIVE
   ========================================================================== */

/* Hide on mobile */
.hide-mobile {
    display: block;
}

@media (max-width: 768px) {
    .hide-mobile {
        display: none !important;
    }
}

/* Hide on desktop */
.hide-desktop {
    display: none;
}

@media (max-width: 768px) {
    .hide-desktop {
        display: block !important;
    }
}

/* Stack on mobile */
@media (max-width: 768px) {
    .stack-mobile {
        flex-direction: column !important;
    }

    .stack-mobile > * {
        width: 100% !important;
    }
}

/* Full width on mobile */
@media (max-width: 768px) {
    .full-mobile {
        width: 100% !important;
    }
}

/* Text alignment on mobile */
@media (max-width: 768px) {
    .text-center-mobile {
        text-align: center !important;
    }
}

/* ==========================================================================
   10. SCROLL TO TOP BUTTON
   ========================================================================== */

.scroll-to-top {
    position: fixed;
    bottom: var(--space-6);
    right: var(--space-6);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--color-primary);
    color: var(--text-inverse);
    border: var(--border-3) solid var(--border-primary);
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    transition: all var(--duration-normal) var(--ease-spring);
    z-index: 99;
    display: flex;
    align-items: center;
    justify-content: center;
}

.scroll-to-top:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-xl);
}

.scroll-to-top.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

@media (max-width: 640px) {
    .scroll-to-top {
        bottom: var(--space-4);
        right: var(--space-4);
        width: 44px;
        height: 44px;
    }
}

/* ==========================================================================
   11. BOTTOM NAVIGATION (MOBILE)
   ========================================================================== */

.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-navbar);
    border-top: var(--border-3) solid var(--color-primary);
    padding: var(--space-2) var(--space-4);
    z-index: 1000;
}

@media (max-width: 768px) {
    .bottom-nav {
        display: flex;
        justify-content: space-around;
        align-items: center;
    }

    /* Add padding to main content to account for bottom nav */
    .main-content {
        padding-bottom: calc(60px + var(--space-4));
    }
}

.bottom-nav__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1);
    color: var(--text-inverse);
    text-decoration: none;
    padding: var(--space-2);
    min-width: 64px;
    min-height: 44px;
    transition: color var(--duration-fast);
}

.bottom-nav__item:hover,
.bottom-nav__item.active {
    color: var(--color-primary);
}

.bottom-nav__icon {
    font-size: 20px;
}

.bottom-nav__label {
    font-size: var(--text-xs);
    font-weight: var(--font-semibold);
}

/* ==========================================================================
   12. SAFE AREA INSETS (iPhone X+)
   ========================================================================== */

@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .bottom-nav {
        padding-bottom: calc(var(--space-2) + env(safe-area-inset-bottom));
    }

    .main-content {
        padding-bottom: calc(60px + var(--space-4) + env(safe-area-inset-bottom));
    }

    .scroll-to-top {
        bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
    }

    @media (max-width: 640px) {
        .scroll-to-top {
            bottom: calc(70px + env(safe-area-inset-bottom));
        }
    }
}

/* ==========================================================================
   REDUCED MOTION
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .scroll-to-top {
        transition: opacity 0.01ms, visibility 0.01ms;
    }

    .scroll-to-top:hover {
        transform: translateY(0);
    }
}
