/* ============================================================
   Cell Dealers Platform — Mobile-First Responsive Styles
   Breakpoints: 360, 375, 390, 414, 768, 1024
   ============================================================ */

/* ----- Design tokens for responsive ----- */
:root {
    --tap-min: 44px;
    --header-h: 70px;
    --drawer-w: min(360px, 85vw);
    --mobile-pad: 1rem;
}

/* Prevent zoomed-out / tiny text on mobile - no scale hacks */
@media (max-width: 768px) {
    html {
        font-size: 16px;
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    body {
        min-width: 0;
        overflow-x: hidden;
    }
}

/* Body scroll lock when mobile nav is open */
body.nav-open {
    overflow: hidden;
    touch-action: none;
}

@media (max-width: 1024px) {
    body.filters-drawer-open {
        overflow: hidden;
    }
}

/* ===== NAV OVERLAY (mobile menu backdrop) ===== */
.nav-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 999;
    opacity: 0;
    transition: opacity 0.25s ease;
}

.main-nav.active + .nav-overlay,
body.nav-open .nav-overlay {
    display: block;
    opacity: 1;
}

@media (max-width: 768px) {
    .nav-overlay {
        display: block;
        pointer-events: none;
    }

    body.nav-open .nav-overlay {
        pointer-events: auto;
    }
}

/* ===== TABLET (≤ 1024px) — Dashboard/Market filters drawer ===== */
@media (max-width: 1024px) {
    /* Force single column: no grid on mobile so filters bar and content use full width */
    .market-layout,
    .dashboard-layout {
        display: flex !important;
        flex-direction: column !important;
        align-items: stretch !important;
        grid-template-columns: none !important;
        max-width: 100%;
        gap: var(--spacing-md);
    }

    /* Filters button: full width like market/offers page, same as .market-layout .filters-drawer-toggle */
    .dashboard-layout .filters-drawer-toggle,
    .market-layout .filters-drawer-toggle {
        width: 100%;
        min-height: var(--tap-min);
    }

    /* Sidebar must not take space; it becomes off-canvas drawer */
    .market-filters,
    .dashboard-filters {
        position: fixed !important;
        top: 0;
        right: 0;
        width: var(--drawer-w) !important;
        max-width: 360px !important;
        height: 100vh;
        height: 100dvh;
        z-index: 1002;
        margin: 0;
        padding: 0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        box-shadow: -4px 0 24px rgba(0, 0, 0, 0.2);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        transform: translateX(100%);
        border-radius: 0;
    }

    .dashboard-filters .filters-drawer-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-shrink: 0;
        padding: calc(var(--header-h) + 0.75rem) 1rem 0.75rem;
        padding-left: max(1rem, env(safe-area-inset-left));
        border-bottom: 1px solid var(--border);
        background: var(--bg-card);
    }

    .dashboard-filters .filters-drawer-header h3 {
        margin: 0;
        font-size: 1.125rem;
        font-weight: 700;
    }

    .dashboard-filters .filter-form .filters-form-title {
        display: none;
    }

    .dashboard-filters .filters-drawer-close {
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: transparent;
        border: none;
        border-radius: var(--radius-sm);
        font-size: 1.5rem;
        line-height: 1;
        color: var(--text-secondary);
        cursor: pointer;
        transition: background-color 0.2s, color 0.2s;
    }

    .dashboard-filters .filters-drawer-close:hover {
        background: var(--bg-elevated);
        color: var(--text-primary);
    }

    .dashboard-filters .filter-form,
    .market-filters .filter-form {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 1rem;
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-bottom: 5rem;
    }

    /* Sticky Apply/Reset at bottom of filters drawer (mobile) */
    .dashboard-filters .filter-actions,
    .market-filters .filter-actions {
        position: sticky;
        bottom: 0;
        left: 0;
        right: 0;
        margin: 0 -1rem -1rem;
        padding: 1rem;
        padding-left: max(1rem, env(safe-area-inset-left));
        background: var(--bg-card);
        border-top: 1px solid var(--border);
        flex-shrink: 0;
    }

    .dashboard-filters .filter-actions .btn,
    .market-filters .filter-actions .btn {
        width: 100%;
    }

    .market-filters .filter-form {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 1rem;
        padding-top: calc(var(--header-h) + 1rem);
        padding-left: max(1rem, env(safe-area-inset-left));
    }

    body.filters-drawer-open .market-filters,
    body.filters-drawer-open .dashboard-filters {
        transform: translateX(0);
    }

    .filters-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1001;
    }

    body.filters-drawer-open .filters-overlay {
        display: block;
    }

    /* Filters button: same style as market/offers page */
    .filters-drawer-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        min-height: var(--tap-min);
        padding: 0.5rem 1rem;
    }

    .market-content,
    .dashboard-content {
        width: 100%;
        flex: 1 1 auto;
        min-width: 0;
    }
}

/* Desktop: dashboard grid + sidebar visible; hide mobile Filters button */
@media (min-width: 1025px) {
    .dashboard-layout {
        display: grid !important;
        grid-template-columns: 280px 1fr !important;
        gap: var(--spacing-lg);
    }

    .filters-drawer-toggle {
        display: none !important;
    }

    .dashboard-filters .filters-drawer-header {
        display: none;
    }

    .dashboard-filters .filter-form .filters-form-title {
        display: block;
        margin-bottom: var(--spacing-sm);
        font-size: 0.9rem;
    }
}

/* ===== TABLET (≤ 768px) ===== */
@media (max-width: 768px) {
    .container {
        padding-left: var(--mobile-pad);
        padding-right: var(--mobile-pad);
        padding-left: max(var(--mobile-pad), env(safe-area-inset-left));
        padding-right: max(var(--mobile-pad), env(safe-area-inset-right));
    }

    .dashboard-page .container {
        padding-left: var(--mobile-pad);
        padding-right: var(--mobile-pad);
    }

    /* ----- Header: actions group [ 🌙 Toggle ] [ ☰ Menu ] — mobile only ----- */
    .main-header .container {
        padding-left: max(var(--spacing-sm), env(safe-area-inset-left));
        padding-right: max(var(--spacing-sm), env(safe-area-inset-right));
    }

    .header-content {
        justify-content: space-between;
        align-items: center;
        gap: 0.5rem;
        min-height: var(--header-h);
    }

    /* Single grouped block on the right: theme + hamburger, 8px gap, no wrap */
    .header-content .header-actions {
        order: 2;
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: flex-end;
        gap: 8px;
        margin-left: auto;
        flex-shrink: 0;
    }

    .mobile-menu-toggle {
        display: flex;
        min-width: 40px;
        min-height: 40px;
        width: 40px;
        height: 40px;
        padding: 0;
        align-items: center;
        justify-content: center;
        background: var(--bg-elevated);
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        flex-shrink: 0;
    }

    /* Theme toggle: same visual style as hamburger, tight next to menu, no float */
    .main-header .header-actions .theme-toggle {
        margin-left: 0 !important;
        min-width: 40px;
        min-height: 40px;
        width: 40px;
        height: 40px;
        padding: 0;
        border-radius: var(--radius-sm);
        background: var(--bg-elevated);
        border: 1px solid var(--border);
        flex-shrink: 0;
        transition: background-color 0.25s ease, border-color 0.25s ease, color 0.25s ease;
    }

    .main-header .header-actions .theme-toggle:hover {
        transform: none;
        box-shadow: none;
    }

    .main-header .header-actions .theme-toggle svg {
        width: 18px;
        height: 18px;
    }

    /* Nav is off-canvas drawer: remove from flex flow so only logo + header-actions show in header */
    .header-content .logo {
        order: 1;
        flex-shrink: 0;
    }

    .header-content .main-nav {
        order: 3;
        position: fixed;
        top: var(--header-h);
        right: -100%;
        width: min(360px, 85vw);
        max-width: 85vw;
        height: calc(100vh - var(--header-h));
        height: calc(100dvh - var(--header-h));
        background-color: var(--bg-card);
        flex-direction: column;
        align-items: stretch;
        padding: var(--spacing-md);
        box-shadow: -4px 0 24px rgba(0, 0, 0, 0.15);
        transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        gap: 0.25rem;
        border-left: 1px solid var(--border);
        z-index: 1000;
    }

    .main-nav.active {
        right: 0;
    }

    .nav-link,
    .nav-dropdown-toggle {
        width: 100%;
        min-height: var(--tap-min);
        padding: 0.75rem 1rem;
        font-size: 0.9375rem;
        display: inline-flex;
        align-items: center;
        box-sizing: border-box;
    }

    .nav-dropdown {
        width: 100%;
    }

    .nav-dropdown-toggle {
        text-align: left;
        justify-content: space-between;
    }

    .nav-dropdown-menu {
        position: static;
        display: none;
        box-shadow: none;
        border: none;
        border-radius: var(--radius-md);
        margin-left: 0;
        margin-top: 0.25rem;
        padding: 0.25rem 0;
        background: var(--bg-elevated);
        animation: none;
    }

    .nav-dropdown.active .nav-dropdown-menu {
        display: block;
    }

    .nav-dropdown-menu a {
        min-height: var(--tap-min);
        padding: 0.65rem 1rem;
        display: flex;
        align-items: center;
    }

    /* ----- Hero ----- */
    .hero-title {
        font-size: 2rem;
        line-height: 1.2;
    }

    .hero-subtitle {
        font-size: 1rem;
        line-height: 1.6;
    }

    .hero-actions {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .hero-actions .btn {
        min-height: var(--tap-min);
        width: 100%;
    }

    .hero-stats {
        flex-direction: column;
        gap: var(--spacing-md);
    }

    .stat-number {
        font-size: 1.75rem;
    }

    /* ----- Grids ----- */
    .members-grid,
    .features-grid,
    .offers-grid {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: var(--spacing-sm);
    }

    .section-title {
        font-size: 1.75rem;
    }

    .cta h2 {
        font-size: 1.75rem;
    }

    /* ----- Market: toolbar and filters ----- */
    .market-toolbar {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .sort-options {
        flex-wrap: wrap;
    }

    .sort-options select {
        min-height: var(--tap-min);
    }

    .market-layout {
        position: relative;
    }

    .market-filters {
        position: fixed;
        top: 0;
        right: 0;
        width: var(--drawer-w);
        height: 100%;
        padding-top: calc(var(--header-h) + 1rem);
    }

    /* ----- No horizontal page overflow on mobile ----- */
    .main-content {
        overflow-x: hidden;
        max-width: 100%;
    }

    .dashboard-page,
    .market-page {
        overflow-x: hidden;
        max-width: 100%;
    }

    .dashboard-layout,
    .dashboard-content {
        min-width: 0;
        max-width: 100%;
    }

    .container {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    /* ----- Tables: horizontal scroll inside container only ----- */
    .offers-table,
    .requests-table {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-left: 0;
        margin-right: 0;
        padding-left: 0;
        padding-right: 0;
        max-width: 100%;
    }

    .offers-table table,
    .requests-table table {
        min-width: 700px;
    }

    .offers-table th,
    .offers-table td,
    .requests-table th,
    .requests-table td {
        padding: 0.65rem 0.5rem;
        font-size: 0.8125rem;
    }

    .product-thumb {
        width: 40px;
        height: 40px;
    }

    /* ----- Forms ----- */
    .form-row {
        grid-template-columns: 1fr;
    }

    .form-control {
        font-size: 16px; /* prevents zoom on iOS */
    }

    .filter-group .input-group {
        flex-wrap: wrap;
    }

    .filter-group .input-group .form-control {
        min-width: 0;
        flex: 1 1 80px;
    }

    /* ----- Buttons: tap targets ----- */
    .btn {
        min-height: var(--tap-min);
    }

    .btn-sm {
        min-height: var(--tap-min);
        padding: 0.5rem 0.875rem;
    }

    .pagination .btn {
        min-height: var(--tap-min);
        min-width: 44px;
    }

    /* ----- Footer ----- */
    .footer-content {
        grid-template-columns: 1fr 1fr;
        gap: var(--spacing-md);
    }

    .footer-section a,
    .footer-section h4 {
        padding: 0.25rem 0;
    }

    /* ----- Auth ----- */
    .auth-page {
        padding: var(--spacing-md) 0;
        min-height: calc(100vh - 300px);
    }

    .auth-container {
        padding: var(--spacing-sm);
    }

    .auth-box {
        padding: var(--spacing-lg);
    }

    /* ----- Flash ----- */
    .flash-messages {
        top: calc(var(--header-h) + 0.5rem);
        left: var(--spacing-sm);
        right: var(--spacing-sm);
        max-width: none;
        margin: 0;
    }

    .alert {
        padding: 0.875rem 2.5rem 0.875rem 1rem;
    }

    .alert-close {
        min-width: var(--tap-min);
        min-height: var(--tap-min);
    }

    /* ----- Page header ----- */
    .page-header h1 {
        font-size: 1.75rem;
    }

    .page-header p {
        font-size: 0.9375rem;
    }

    /* ----- Page title row (e.g. My Offers, Create New) ----- */
    .page-title-row {
        flex-direction: column;
        align-items: stretch;
        gap: var(--spacing-sm);
        margin-bottom: var(--spacing-md);
    }

    .page-title-row .btn {
        width: 100%;
        justify-content: center;
    }

    .table-actions .btn {
        min-height: var(--tap-min);
    }

    /* ----- Create form page ----- */
    .create-form-page .container {
        grid-template-columns: 1fr;
    }

    .form-sidebar {
        order: -1;
    }

    .tips-card,
    .form-sidebar .info-card {
        position: static;
    }

    /* ----- Dashboard tabs: scrollable if needed, no tiny text ----- */
    .dashboard-tabs {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 0;
        padding-bottom: 0;
        margin-bottom: var(--spacing-md);
        border-bottom: 2px solid var(--border);
        scrollbar-width: none;
    }

    .dashboard-tabs::-webkit-scrollbar {
        display: none;
    }

    .tab-link {
        flex-shrink: 0;
        min-height: var(--tap-min);
        padding: 0.65rem 1rem;
        font-size: 0.9375rem;
        font-weight: 500;
    }

    /* ----- IMEI (page-specific overrides already in view) ----- */
    .imei-form-card,
    .imei-result-card {
        padding: 1.25rem;
    }

    .imei-radios {
        flex-direction: column;
    }

    .imei-radio {
        min-height: var(--tap-min);
        align-items: center;
    }

    /* ----- Device pages ----- */
    .device-page .breadcrumb {
        flex-wrap: wrap;
    }

    .device-page .offers-grid {
        grid-template-columns: 1fr;
    }

    .device-page .offer-card-link {
        min-height: var(--tap-min);
    }

    /* ----- Tool pages ----- */
    .tool-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .tool-actions .btn {
        width: 100%;
    }

    .tool-page .tool-section {
        padding: 1.5rem;
    }

    .device-link-card {
        min-height: var(--tap-min);
        display: flex;
        align-items: center;
    }
}

/* ===== SMALL MOBILE (≤ 414px) ===== */
@media (max-width: 414px) {
    .container {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    .hero-title {
        font-size: 1.75rem;
    }

    .hero-subtitle {
        font-size: 0.9375rem;
    }

    .section-title {
        font-size: 1.5rem;
    }

    .cta h2 {
        font-size: 1.5rem;
    }

    .page-header h1 {
        font-size: 1.5rem;
    }

    .footer-content {
        grid-template-columns: 1fr;
    }

    .auth-box {
        padding: var(--spacing-md);
    }

    .auth-box h1 {
        font-size: 1.5rem;
    }
}

/* ===== EXTRA SMALL (≤ 390px, ≤ 375px, ≤ 360px) ===== */
@media (max-width: 390px) {
    .hero-title {
        font-size: 1.6rem;
    }

    .main-nav {
        width: 100%;
        max-width: 100%;
    }

    .logo-text h1 {
        font-size: 1rem;
    }
}

@media (max-width: 375px) {
    .container {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .hero-title {
        font-size: 1.5rem;
    }

    .section-title {
        font-size: 1.35rem;
    }

    .btn-large {
        padding: 0.75rem 1.25rem;
        font-size: 0.9375rem;
    }
}

@media (max-width: 360px) {
    .logo-text .tagline {
        display: none;
    }

    .members-grid,
    .features-grid,
    .offers-grid {
        grid-template-columns: 1fr;
    }

    .offer-image {
        height: 160px;
    }
}

/* ===== TABLE CARD VIEW ===== */
/* Dashboard feed: card layout from 1024px down (same as offers page feel, no horizontal scroll) */
@media (max-width: 1024px) {
    .dashboard-items .table-cards,
    .dashboard-items .offers-table.table-cards {
        display: block;
    }

    .dashboard-items .table-cards thead,
    .dashboard-items .offers-table.table-cards thead {
        display: none;
    }

    .dashboard-items .table-cards tbody tr,
    .dashboard-items .offers-table.table-cards tbody tr {
        display: block;
        margin-bottom: var(--spacing-md);
        padding: var(--spacing-sm);
        background: var(--bg-card);
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        box-shadow: var(--shadow-sm);
    }

    .dashboard-items .table-cards tbody td,
    .dashboard-items .offers-table.table-cards tbody td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        padding: 0.5rem 0;
        border-bottom: 1px solid var(--border);
        font-size: 0.875rem;
    }

    .dashboard-items .table-cards tbody td:last-child,
    .dashboard-items .offers-table.table-cards tbody td:last-child {
        border-bottom: none;
    }

    .dashboard-items .table-cards tbody td::before,
    .dashboard-items .offers-table.table-cards tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary);
        margin-right: 0.5rem;
        flex-shrink: 0;
    }

    .dashboard-items .table-cards .product-thumb,
    .dashboard-items .offers-table.table-cards .product-thumb {
        width: 48px;
        height: 48px;
        min-width: 48px;
        min-height: 48px;
        object-fit: cover;
        border-radius: var(--radius-sm);
    }

    .dashboard-items .table-cards .product-title,
    .dashboard-items .offers-table.table-cards .product-title {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .dashboard-items .table-cards tbody td[data-label="Actions"] .btn,
    .dashboard-items .offers-table.table-cards tbody td[data-label="Details"] .btn {
        min-height: var(--tap-min);
    }
}

@media (max-width: 768px) {
    .table-cards {
        display: block;
    }

    .table-cards thead {
        display: none;
    }

    .table-cards tbody tr {
        display: block;
        margin-bottom: var(--spacing-md);
        padding: var(--spacing-sm);
        background: var(--bg-card);
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        box-shadow: var(--shadow-sm);
    }

    .table-cards tbody td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        padding: 0.5rem 0;
        border-bottom: 1px solid var(--border);
        font-size: 0.875rem;
    }

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

    .table-cards tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary);
        margin-right: 0.5rem;
        flex-shrink: 0;
    }

    .table-cards .product-info {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .table-cards .product-thumb {
        width: 48px;
        height: 48px;
        min-width: 48px;
        min-height: 48px;
        object-fit: cover;
        border-radius: var(--radius-sm);
    }

    .table-cards .product-title {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .table-cards tbody td[data-label="Select"] input[type="checkbox"] {
        min-width: var(--tap-min);
        min-height: var(--tap-min);
    }

    .table-cards tbody td[data-label="Actions"] .btn {
        min-height: var(--tap-min);
    }
}

/* Card view: no min-width so table doesn't force horizontal scroll */
.dashboard-items .offers-table.table-cards table,
.offers-table.table-cards table {
    min-width: 0;
}

/* ===== DARK MODE MOBILE ===== */
[data-theme="dark"] .main-nav {
    background-color: var(--bg-card);
    border-left-color: var(--border);
}

[data-theme="dark"] .nav-dropdown-menu {
    background-color: var(--bg-elevated);
}

@media (max-width: 1024px) {
    [data-theme="dark"] .market-filters,
    [data-theme="dark"] .dashboard-filters {
        background-color: var(--bg-card);
        border-left: 1px solid var(--border);
    }
}

/* ===== REDUCED MOTION ===== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.05ms !important;
    }
}
