/* ========================================
   Chat Mobile Redesign
   Mobile-first styles for the new chat interface
   ======================================== */

/* ========================================
   Mobile Spacing Tokens
   ======================================== */
:root {
    --mob-sp-xs: 4px;
    --mob-sp-sm: 8px;
    --mob-sp-md: 12px;
    --mob-sp-lg: 16px;
    --mob-sp-xl: 20px;
    --mob-header-h: 48px;
    --mob-input-h: 52px;
    --mob-contact-h: 68px;
    --mob-chip-h: 34px;
    --mob-tabs-h: 42px;
    --mob-transition: 300ms cubic-bezier(0.4, 0, 0.2, 1);
    --mob-transition-fast: 200ms ease;
    --mob-sheet-peek: 38%;
    --mob-sheet-full: 88%;
}

/* ========================================
   Mobile View Switching System
   Only active on screens < 992px
   ======================================== */
@media (max-width: 991.98px) {

    /* --- Lock viewport: prevent ANY page-level scrolling --- */
    html.mob-chat-active,
    html.mob-chat-active body {
        height: 100%;
        overflow: hidden;
        position: fixed;
        width: 100%;
        overscroll-behavior: none;
    }

    /* Hide navbar + section-tabs in conversation view to maximize chat space */
    html.mob-chat-active .app-navbar,
    html.mob-chat-active .section-tabs {
        display: none !important;
    }

    /* Reset container constraints for full-screen chat */
    html.mob-chat-active .app-content {
        padding: 0;
        max-width: none;
        height: 100%;
    }

    html.mob-chat-active .chat-app-wrapper {
        padding: 0;
        min-height: 100%;
        height: 100%;
    }

    html.mob-chat-active .chat-app-wrapper>.container-fluid {
        height: 100%;
    }

    /* --- App container --- */
    .chat-app {
        position: relative;
        overflow: hidden;
        display: block;
    }

    /* When in conversation view, fill entire viewport */
    html.mob-chat-active .chat-app {
        height: 100vh;
        height: 100dvh;
        border-radius: 0;
        box-shadow: none;
    }

    /* --- Panels base --- */
    .chat-sidebar,
    .chat-main {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        transition: transform var(--mob-transition), opacity var(--mob-transition);
        will-change: transform, opacity;
        contain: layout style;
        overflow: hidden;
    }

    /* Reset existing sidebar mobile overrides */
    .chat-sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        z-index: 2;
        box-shadow: none;
        transition: transform var(--mob-transition), opacity var(--mob-transition);
    }

    .chat-sidebar.show {
        left: 0;
    }

    .chat-main {
        z-index: 1;
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    /* Order sidebar: same mobile treatment */
    .order-sidebar {
        display: none;
    }

    /* Sidebar overlay is no longer needed for mobile */
    .sidebar-overlay {
        display: none !important;
    }

    /* --- View States --- */

    /* Default: contact-list */
    .chat-app[data-view="contact-list"] .chat-sidebar {
        transform: translateX(0);
        opacity: 1;
        pointer-events: auto;
        z-index: 2;
    }

    .chat-app[data-view="contact-list"] .chat-main {
        transform: translateX(100%);
        pointer-events: none;
        z-index: 1;
    }

    /* Conversation view */
    .chat-app[data-view="conversation"] .chat-sidebar {
        transform: translateX(-30%);
        opacity: 0;
        pointer-events: none;
        z-index: 1;
    }

    .chat-app[data-view="conversation"] .chat-main {
        transform: translateX(0);
        pointer-events: auto;
        z-index: 2;
    }

    /* Reduce motion */
    @media (prefers-reduced-motion: reduce) {

        .chat-sidebar,
        .chat-main {
            transition-duration: 0.01ms !important;
        }
    }

    /* ========================================
       Mobile Contact List Header
       ======================================== */

    .sidebar-header {
        padding: var(--mob-sp-md) var(--mob-sp-lg);
        min-height: var(--mob-header-h);
        border-bottom: none;
    }

    .sidebar-header-content {
        align-items: center;
    }

    .sidebar-title {
        font-size: 18px;
        font-weight: 700;
        letter-spacing: -0.02em;
    }

    .user-count-badge {
        background: var(--chat-primary);
        color: #fff;
        min-width: 24px;
        height: 24px;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 600;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0 8px;
    }

    /* Close sidebar button hidden (view switching replaces it) */
    .btn-close-sidebar {
        display: none !important;
    }

    /* ========================================
       Mobile Search Bar
       ======================================== */
    .search-container {
        padding: 0 var(--mob-sp-lg) var(--mob-sp-sm);
    }

    .search-input-wrapper {
        background: var(--chat-bg-app);
        border-radius: 20px;
        border: 1.5px solid transparent;
        transition: border-color var(--mob-transition-fast), box-shadow var(--mob-transition-fast);
    }

    .search-input-wrapper:focus-within {
        border-color: var(--chat-primary);
        box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.1);
    }

    .search-input {
        height: 38px;
        font-size: 15px;
        background: transparent;
        border: none;
    }

    /* ========================================
       Mobile Filter Chips (Horizontal Scroll)
       ======================================== */
    .mob-filter-chips {
        display: flex;
        gap: 8px;
        padding: var(--mob-sp-sm) var(--mob-sp-lg);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        scroll-snap-type: x mandatory;
        white-space: nowrap;
    }

    .mob-filter-chips::-webkit-scrollbar {
        display: none;
    }

    .mob-chip {
        flex-shrink: 0;
        scroll-snap-align: start;
        display: inline-flex;
        align-items: center;
        gap: 4px;
        height: var(--mob-chip-h);
        padding: 0 14px;
        border-radius: 17px;
        font-size: 13px;
        font-weight: 500;
        border: 1.5px solid var(--chat-border-color);
        background: #fff;
        color: var(--chat-text-secondary);
        cursor: pointer;
        transition: all var(--mob-transition-fast);
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    .mob-chip:active {
        transform: scale(0.95);
    }

    .mob-chip.active {
        background: var(--chat-primary);
        border-color: var(--chat-primary);
        color: #fff;
        font-weight: 600;
    }

    .mob-chip .chip-count {
        background: rgba(255, 255, 255, 0.3);
        color: inherit;
        font-size: 11px;
        font-weight: 700;
        padding: 2px 6px;
        border-radius: 10px;
        min-width: 20px;
        text-align: center;
    }

    .mob-chip.active .chip-count {
        background: rgba(255, 255, 255, 0.35);
    }

    .mob-chip:not(.active) .chip-count {
        background: var(--chat-bg-app);
        color: var(--chat-text-secondary);
    }

    /* Hide the old filter panel on mobile */
    .filter-panel {
        display: none !important;
    }

    .btn-filter {
        display: none !important;
    }

    /* ========================================
       Mobile Contact Item (Compact, 68px)
       ======================================== */
    .user-item {
        padding: var(--mob-sp-md) var(--mob-sp-lg);
        min-height: var(--mob-contact-h);
        gap: var(--mob-sp-md);
        border-bottom: 1px solid rgba(var(--border-rgb), 0.5);
        border-left: 3px solid transparent;
        transition:
            background var(--mob-transition-fast),
            border-color var(--mob-transition-fast);
        -webkit-tap-highlight-color: transparent;
        cursor: pointer;
    }

    .user-item:active {
        background: var(--chat-bg-hover);
    }

    .user-item.active {
        border-left-color: var(--chat-primary);
        background: var(--chat-bg-active);
    }

    .user-item.unread {
        border-left-color: var(--chat-info);
    }

    .user-item.unread .user-name {
        font-weight: 700;
    }

    .user-item.unread .user-last-message {
        font-weight: 500;
        color: var(--chat-text-primary);
    }

    .user-avatar {
        width: 42px;
        height: 42px;
        flex-shrink: 0;
        font-size: 15px;
        border-width: 2px;
    }

    .user-item-header {
        margin-bottom: 1px;
    }

    .user-name {
        font-size: 14px;
        font-weight: 600;
        line-height: 1.3;
    }

    .user-time {
        font-size: 11px;
        white-space: nowrap;
    }

    .user-last-message {
        font-size: 13px;
        line-height: 1.3;
        color: var(--chat-text-tertiary);
        display: -webkit-box;
        -webkit-line-clamp: 1;
        line-clamp: 1;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .user-badges {
        margin-top: 2px;
        gap: 4px;
    }

    .badge-sm {
        font-size: 10px;
        padding: 1px 6px;
        border-radius: 4px;
        font-weight: 600;
    }

    .user-tags {
        display: none;
        /* Hide tags in compact view */
    }

    .unread-count {
        font-size: 11px;
        min-width: 20px;
        height: 20px;
        padding: 0 6px;
        border-radius: 10px;
        font-weight: 700;
    }

    /* Platform ring on avatar */
    .user-avatar.platform-line {
        border-color: var(--chat-line);
    }

    .user-avatar.platform-facebook {
        border-color: var(--chat-facebook);
    }

    /* User list scrolling */
    .user-list {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        max-height: none;
        overscroll-behavior: contain;
    }

    /* ========================================
       Chat Header v2 — Mobile Overrides
       ======================================== */
    .ch2-bar {
        padding: 6px 10px;
        min-height: 48px;
        gap: 8px;
    }

    .ch2-back {
        display: flex !important;
        -webkit-tap-highlight-color: transparent;
    }

    .ch2-back:active {
        background: var(--chat-bg-hover);
    }

    .ch2-user {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .ch2-avatar {
        width: 32px;
        height: 32px;
        font-size: 12px;
    }

    .ch2-name {
        font-size: 13.5px;
    }

    .ch2-meta {
        font-size: 10.5px;
    }

    /* Hide primary action buttons on mobile — use dropdown instead */
    .ch2-btn#btnAssignConversation,
    .ch2-btn#btnCloseConversation,
    .ch2-btn#btnReopenConversation {
        display: none !important;
    }

    /* Show mobile-only dropdown items */
    .ch2-mob-only {
        display: flex !important;
    }

    /* Context strip compact */
    .ch2-context {
        padding: 0 10px 6px;
        gap: 3px;
    }

    /* Dropdown touch-friendly overrides */
    .ch2-drop-item {
        padding: 11px 16px;
        font-size: 14px;
        -webkit-tap-highlight-color: transparent;
    }

    .ch2-drop-item:active {
        background: var(--chat-bg-hover, var(--cv2-surface-3));
    }

    .ch2-drop-item i {
        width: 20px;
        font-size: 14px;
    }

    /* ========================================
       Mobile Messages Area
       ======================================== */
    .messages-wrapper {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .messages-container {
        padding: var(--mob-sp-md);
    }

    .message-bubble {
        max-width: min(85%, 400px);
        border-radius: 16px;
        padding: var(--mob-sp-md);
    }

    /* Incoming message (user) */
    .message.user .message-bubble {
        border-bottom-left-radius: 4px;
    }

    /* Outgoing message (admin) */
    .message.admin .message-bubble {
        border-bottom-right-radius: 4px;
    }

    .message-header {
        font-size: 11px;
        margin-bottom: 4px;
    }

    .message-content {
        font-size: 14px;
        line-height: 1.5;
    }

    .message-time {
        font-size: 11px;
        margin-top: 4px;
    }

    .message-separator {
        font-size: 11px;
        margin: var(--mob-sp-lg) 0;
    }

    /* ========================================
       Mobile Message Input Area
       ======================================== */
    .message-input-area {
        padding: var(--mob-sp-sm) var(--mob-sp-md);
        padding-bottom: calc(var(--mob-sp-sm) + env(safe-area-inset-bottom, 0px));
        border-top: 1px solid var(--chat-border-color);
        background: #fff;
        flex-shrink: 0;
    }

    .message-input-container {
        gap: var(--mob-sp-xs);
        padding: var(--mob-sp-xs);
        background: var(--chat-bg-app);
        border-radius: 24px;
        border: 1.5px solid var(--chat-border-color);
        transition: border-color var(--mob-transition-fast);
    }

    .message-input-container:focus-within {
        border-color: var(--chat-primary);
    }

    /* Hide extra input actions on mobile, keep attach + send */
    .btn-input-action#btnEmoji {
        display: none;
    }

    .btn-input-action {
        width: 36px;
        height: 36px;
        font-size: 16px;
        flex-shrink: 0;
    }

    .message-input {
        font-size: 16px;
        /* Prevent iOS zoom */
        line-height: 1.4;
    }

    .btn-send {
        width: 36px;
        height: 36px;
        flex-shrink: 0;
    }

    .message-input-footer {
        display: none;
        /* Hide char counter on mobile */
    }

    /* ========================================
       Mobile Bottom Sheet (Customer Info)
       ======================================== */
    .mob-sheet-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0);
        z-index: 200;
        pointer-events: none;
        transition: background 300ms ease;
    }

    .mob-sheet-overlay.show {
        background: rgba(0, 0, 0, 0.35);
        pointer-events: auto;
    }

    .mob-bottom-sheet {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        background: #fff;
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.12);
        z-index: 201;
        transform: translateY(100%);
        transition: transform 350ms cubic-bezier(0.32, 0.72, 0, 1);
        will-change: transform;
        max-height: var(--mob-sheet-full);
        display: flex;
        flex-direction: column;
        contain: layout style;
    }

    .mob-bottom-sheet.peek {
        transform: translateY(calc(100% - var(--mob-sheet-peek)));
    }

    .mob-bottom-sheet.full {
        transform: translateY(calc(100% - var(--mob-sheet-full)));
    }

    .mob-bottom-sheet.dragging {
        transition: none !important;
    }

    /* Sheet handle */
    .sheet-handle {
        display: flex;
        justify-content: center;
        padding: 12px 0 8px;
        cursor: grab;
        flex-shrink: 0;
    }

    .sheet-handle::after {
        content: '';
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: #D1D5DB;
    }

    /* Sheet body/content */
    .sheet-body {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 0 var(--mob-sp-lg) var(--mob-sp-xl);
        overscroll-behavior: contain;
    }

    /* --- Customer profile in sheet --- */
    .sheet-profile {
        display: flex;
        align-items: center;
        gap: var(--mob-sp-md);
        margin-bottom: var(--mob-sp-lg);
    }

    .sheet-profile-avatar {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 22px;
        font-weight: 600;
        color: #fff;
        background: var(--chat-primary);
        flex-shrink: 0;
        overflow: hidden;
    }

    .sheet-profile-avatar img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    .sheet-profile-avatar.platform-line {
        border: 2.5px solid var(--chat-line);
    }

    .sheet-profile-avatar.platform-facebook {
        border: 2.5px solid var(--chat-facebook);
    }

    .sheet-profile-info {
        flex: 1;
        min-width: 0;
    }

    .sheet-profile-name {
        font-size: 17px;
        font-weight: 700;
        line-height: 1.3;
        color: var(--chat-text-primary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .sheet-profile-sub {
        font-size: 13px;
        color: var(--chat-text-tertiary);
        display: flex;
        align-items: center;
        gap: 6px;
        margin-top: 2px;
    }

    .sheet-profile-sub i {
        font-size: 12px;
    }

    /* Sheet quick actions row */
    .sheet-actions {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
        margin-bottom: var(--mob-sp-lg);
    }

    .sheet-action-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 4px;
        padding: 10px 0;
        border-radius: 12px;
        background: var(--chat-bg-app);
        border: none;
        cursor: pointer;
        transition: all var(--mob-transition-fast);
        -webkit-tap-highlight-color: transparent;
    }

    .sheet-action-btn:active {
        transform: scale(0.93);
        background: var(--chat-bg-hover);
    }

    .sheet-action-btn i {
        font-size: 18px;
        color: var(--chat-primary);
    }

    .sheet-action-btn span {
        font-size: 11px;
        font-weight: 500;
        color: var(--chat-text-secondary);
    }

    .sheet-action-btn.is-active i {
        color: var(--chat-success);
    }

    .sheet-action-btn.is-danger i {
        color: var(--chat-danger);
    }

    /* Sheet stats */
    .sheet-stats {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
        margin-bottom: var(--mob-sp-lg);
    }

    .sheet-stat {
        text-align: center;
        padding: 12px 8px;
        background: var(--chat-bg-app);
        border-radius: 12px;
    }

    .sheet-stat-value {
        font-size: 20px;
        font-weight: 700;
        color: var(--chat-text-primary);
        line-height: 1.2;
    }

    .sheet-stat-label {
        font-size: 11px;
        color: var(--chat-text-tertiary);
        margin-top: 2px;
    }

    /* Sheet section titles */
    .sheet-section {
        margin-bottom: var(--mob-sp-lg);
    }

    .sheet-section-title {
        font-size: 13px;
        font-weight: 600;
        color: var(--chat-text-tertiary);
        text-transform: uppercase;
        letter-spacing: 0.04em;
        margin-bottom: var(--mob-sp-sm);
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .sheet-section-title i {
        font-size: 12px;
    }

    /* Sheet tags */
    .sheet-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
    }

    .sheet-tag {
        display: inline-flex;
        align-items: center;
        padding: 4px 10px;
        border-radius: 6px;
        background: var(--chat-bg-app);
        color: var(--chat-text-secondary);
        font-size: 12px;
        font-weight: 500;
    }

    .sheet-tag-add {
        border: 1.5px dashed var(--chat-border-color);
        background: transparent;
        cursor: pointer;
        color: var(--chat-primary);
    }

    /* Sheet badges */
    .sheet-badges {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
        margin-bottom: var(--mob-sp-lg);
    }

    .sheet-badge {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 4px 10px;
        border-radius: 6px;
        font-size: 12px;
        font-weight: 600;
    }

    .sheet-badge.is-success {
        background: rgba(var(--success-rgb), 0.1);
        color: var(--chat-success);
    }

    .sheet-badge.is-warning {
        background: rgba(var(--warning-rgb), 0.1);
        color: var(--chat-warning);
    }

    .sheet-badge.is-info {
        background: rgba(var(--info-rgb), 0.1);
        color: var(--chat-info);
    }

    .sheet-badge.is-muted {
        background: var(--chat-bg-app);
        color: var(--chat-text-tertiary);
    }

    /* Sheet notes */
    .sheet-note {
        padding: 10px 12px;
        background: var(--chat-bg-app);
        border-radius: 10px;
        margin-bottom: 8px;
    }

    .sheet-note.pinned {
        border-left: 3px solid var(--chat-warning);
    }

    .sheet-note-content {
        font-size: 13px;
        color: var(--chat-text-primary);
        line-height: 1.5;
    }

    .sheet-note-meta {
        font-size: 11px;
        color: var(--chat-text-tertiary);
        margin-top: 4px;
        display: flex;
        gap: 8px;
    }

    /* Sheet orders */
    .sheet-order {
        padding: 12px;
        background: var(--chat-bg-app);
        border-radius: 10px;
        margin-bottom: 8px;
    }

    .sheet-order-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 6px;
    }

    .sheet-order-status {
        font-size: 11px;
        font-weight: 600;
        padding: 3px 8px;
        border-radius: 4px;
    }

    .sheet-order-status.pending {
        background: rgba(var(--warning-rgb), 0.12);
        color: var(--chat-warning);
    }

    .sheet-order-status.confirmed,
    .sheet-order-status.completed {
        background: rgba(var(--success-rgb), 0.12);
        color: var(--chat-success);
    }

    .sheet-order-status.shipped {
        background: rgba(var(--info-rgb), 0.12);
        color: var(--chat-info);
    }

    .sheet-order-status.cancelled {
        background: rgba(var(--danger-rgb), 0.12);
        color: var(--chat-danger);
    }

    .sheet-order-date {
        font-size: 11px;
        color: var(--chat-text-tertiary);
    }

    .sheet-order-items {
        font-size: 13px;
        color: var(--chat-text-secondary);
        margin-bottom: 4px;
    }

    .sheet-order-total {
        font-size: 14px;
        font-weight: 700;
        color: var(--chat-text-primary);
    }

    /* Sheet link to full profile */
    .sheet-profile-link {
        display: block;
        text-align: center;
        padding: 12px;
        margin-top: 4px;
        color: var(--chat-primary);
        font-size: 14px;
        font-weight: 600;
        text-decoration: none;
        border-radius: 10px;
        background: rgba(var(--primary-rgb), 0.06);
        transition: background var(--mob-transition-fast);
    }

    .sheet-profile-link:active {
        background: rgba(var(--primary-rgb), 0.12);
    }

    /* Empty state in sheet */
    .sheet-empty {
        text-align: center;
        padding: 16px;
        color: var(--chat-text-tertiary);
        font-size: 13px;
    }

    /* ========================================
       Mobile Empty State
       ======================================== */
    .empty-state.app-empty {
        padding: 2rem var(--mob-sp-lg);
    }

    .app-empty__icon i {
        font-size: 40px;
    }

    .app-empty__title {
        font-size: 16px;
    }

    .app-empty__desc {
        font-size: 13px;
    }

    /* ========================================
       Mobile Loading & Toast
       ======================================== */
    .loading-placeholder {
        padding: 3rem var(--mob-sp-lg);
    }

    .app-toast-container {
        bottom: calc(var(--mob-input-h) + env(safe-area-inset-bottom, 0px) + 8px);
    }
}

/* ========================================
   Desktop: Hide mobile-only elements
   ======================================== */
@media (min-width: 992px) {
    .mob-filter-chips {
        display: none;
    }

    .btn-mob-more {
        display: none !important;
    }

    .mob-more-dropdown {
        display: none !important;
    }

    .mob-sheet-overlay,
    .mob-bottom-sheet {
        display: none !important;
    }
}