/* ============================================
   Order Chat Component
   ============================================
   Per-order chat between admin and customer.
   Shared component — works in both admin and customer contexts.
   Uses --mine / --theirs pattern for role-agnostic bubble alignment.
   ============================================ */

/* ── Panel Container ── */
/* Matches .bim-internal-notes-fullwidth card style in admin-quote-form.css */
.bim-order-chat-panel {
    background-color: var(--bim-surface);
    border-radius: var(--bim-radius-lg);
    border: 1px solid var(--bim-border);
    padding: var(--bim-space-6);
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    margin-top: var(--bim-space-6);
    margin-bottom: var(--bim-space-8);
}

/* ── Header ── */
.bim-order-chat-header {
    margin-bottom: var(--bim-space-4);
}

.bim-order-chat-title {
    display: flex;
    align-items: center;
    gap: var(--bim-space-2);
}

.bim-order-chat-title h3 {
    margin: 0;
    font-size: var(--bim-text-base);
    font-weight: 900;
    color: var(--bim-text-primary);
    text-transform: uppercase;
    font-style: italic;
}

.bim-order-chat-icon {
    color: var(--bim-accent);
    font-size: 1rem;
}

.bim-chat-unread-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    background: var(--bim-danger);
    color: #fff;
    font-size: var(--bim-text-xs);
    font-weight: 700;
    border-radius: 10px;
    line-height: 1;
}

/* ── Draft Mode Hint ── */
.bim-order-chat-hint {
    margin: var(--bim-space-1) 0 0 0;
    font-size: var(--bim-text-xs);
    color: var(--bim-text-faint);
    font-style: italic;
}

/* ── Messages Container ── */
.bim-order-chat-messages {
    max-height: 380px;
    min-height: 80px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--bim-space-3);
    margin-bottom: var(--bim-space-4);
    padding: var(--bim-space-4);
    background: var(--bim-bg);
    border-radius: var(--bim-radius);
    border: 1px solid var(--bim-border-light);
}

/* Scrollbar styling */
.bim-order-chat-messages::-webkit-scrollbar {
    width: 6px;
}

.bim-order-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.bim-order-chat-messages::-webkit-scrollbar-thumb {
    background: var(--bim-border);
    border-radius: 3px;
}

.bim-order-chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--bim-text-faint);
}

/* ── Empty State ── */
.bim-order-chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--bim-space-6) var(--bim-space-4);
    color: var(--bim-text-faint);
    text-align: center;
}

.bim-order-chat-empty .material-symbols-outlined {
    font-size: 32px;
    margin-bottom: var(--bim-space-2);
    opacity: 0.35;
}

.bim-order-chat-empty p {
    margin: 0;
    font-size: var(--bim-text-sm);
}

/* ── Chat Bubble Base ── */
.bim-chat-bubble {
    max-width: 80%;
    padding: var(--bim-space-3) var(--bim-space-4);
    border-radius: var(--bim-radius);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* ── Mine (current user's messages) — right aligned ── */
.bim-chat-bubble--mine {
    align-self: flex-end;
    background: var(--bim-info-light);
    color: var(--bim-text-primary);
    border: 1px solid rgba(99, 102, 241, 0.2);
    border-bottom-right-radius: var(--bim-radius-xs);
}

.bim-chat-bubble--mine .bim-chat-bubble-sender {
    justify-content: flex-end;
}

.bim-chat-bubble--mine .bim-chat-sender-name {
    color: var(--bim-info);
}

.bim-chat-bubble--mine .bim-chat-sender-time {
    color: var(--bim-text-faint);
}

.bim-chat-bubble--mine .bim-chat-bubble-text {
    color: var(--bim-text-primary);
}

/* ── Theirs (other party's messages) — left aligned ── */
.bim-chat-bubble--theirs {
    align-self: flex-start;
    background: var(--bim-success-light);
    color: var(--bim-text-primary);
    border: 1px solid var(--bim-success-border);
    border-bottom-left-radius: var(--bim-radius-xs);
}

.bim-chat-bubble--theirs .bim-chat-sender-name {
    color: var(--bim-success);
}

.bim-chat-bubble--theirs .bim-chat-sender-time {
    color: var(--bim-text-faint);
}

/* ── Bubble Inner ── */
.bim-chat-bubble-sender {
    display: flex;
    align-items: center;
    gap: var(--bim-space-2);
    margin-bottom: var(--bim-space-1);
}

.bim-chat-sender-name {
    font-size: var(--bim-text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.bim-chat-sender-time {
    font-size: var(--bim-text-2xs);
}

.bim-chat-bubble-text {
    font-size: var(--bim-text-sm);
    line-height: 1.5;
}

/* ── Input Area ── */
.bim-order-chat-input {
    display: flex;
    align-items: flex-end;
    gap: var(--bim-space-3);
}

.bim-chat-textarea {
    flex: 1;
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
    resize: none;
    border: 1px solid var(--bim-border);
    border-radius: var(--bim-radius-sm);
    padding: var(--bim-space-3) var(--bim-space-4);
    font-family: var(--bim-font);
    font-size: var(--bim-text-sm);
    color: var(--bim-text-primary);
    background: var(--bim-bg-input);
    line-height: 1.5;
    transition: border-color var(--bim-transition-fast);
}

.bim-chat-textarea:focus {
    outline: none;
    border-color: var(--bim-border-focus);
    box-shadow: var(--bim-shadow-focus);
}

.bim-chat-textarea::placeholder {
    color: var(--bim-text-faint);
}

.bim-chat-send-btn {
    flex-shrink: 0;
    padding: var(--bim-space-3) var(--bim-space-4) !important;
    font-size: var(--bim-text-sm) !important;
}

/* ── Closed Notice ── */
.bim-order-chat-closed {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--bim-space-2);
    padding: var(--bim-space-4);
    background: var(--bim-bg);
    border-radius: var(--bim-radius);
    color: var(--bim-text-muted);
}

.bim-order-chat-closed .material-symbols-outlined {
    font-size: var(--bim-icon-sm);
    opacity: 0.6;
}

.bim-order-chat-closed p {
    margin: 0;
    font-size: var(--bim-text-sm);
}

/* ── Responsive ── */
@media (max-width: 600px) {
    .bim-order-chat-panel {
        padding: var(--bim-space-4);
        margin-bottom: var(--bim-space-4);
    }

    .bim-order-chat-title h3 {
        font-size: var(--bim-text-sm);
    }

    .bim-chat-bubble {
        max-width: 90%;
    }

    .bim-order-chat-input {
        flex-direction: column;
        gap: var(--bim-space-2);
    }

    .bim-chat-send-btn {
        align-self: flex-end;
    }

    .bim-order-chat-messages {
        max-height: 300px;
        padding: var(--bim-space-3);
    }
}

/* ================================================================
   MOBILE CHAT FAB + BOTTOM SHEET
   Hidden on desktop, visible only at ≤768px
   ================================================================ */

/* ── Hidden on desktop ── */
.bim-chat-fab-wrap,
.bim-chat-sheet-overlay,
.bim-chat-sheet {
    display: none;
}

@media (max-width: 768px) {

    /* Hide inline chat in summary column (moved to sheet by JS) */
    .bim-quote-summary-column > .bim-order-chat-panel {
        display: none;
    }

    /* ── FAB Button ── */
    .bim-chat-fab-wrap {
        display: block;
        position: fixed;
        bottom: 24px;
        right: 16px;
        z-index: 998;
    }

    /* When sticky price bar is present, raise FAB above it */
    .bim-has-sticky-bar .bim-chat-fab-wrap {
        bottom: 80px;
    }

    .bim-chat-fab {
        width: 52px;
        height: 52px;
        border-radius: 50%;
        border: none;
        background: var(--bim-accent, #e63737);
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        box-shadow: 0 4px 16px rgba(230, 55, 55, 0.35), 0 2px 4px rgba(0, 0, 0, 0.15);
        transition: transform 0.2s ease, box-shadow 0.2s ease;
        position: relative;
    }

    .bim-chat-fab:hover {
        transform: scale(1.08);
        box-shadow: 0 6px 20px rgba(230, 55, 55, 0.45), 0 2px 6px rgba(0, 0, 0, 0.2);
    }

    .bim-chat-fab:active {
        transform: scale(0.92);
    }

    @keyframes bimFabPulse {
        0%, 100% {
            box-shadow: 0 4px 16px rgba(230, 55, 55, 0.35), 0 2px 4px rgba(0, 0, 0, 0.15);
        }
        50% {
            box-shadow: 0 4px 24px rgba(230, 55, 55, 0.65), 0 0 0 8px rgba(230, 55, 55, 0.12);
        }
    }

    .bim-chat-fab--has-unread {
        animation: bimFabPulse 1.8s ease-in-out infinite;
    }

    .bim-chat-fab .material-symbols-outlined {
        font-size: 24px;
    }

    /* Unread badge on FAB */
    .bim-chat-fab-badge {
        position: absolute;
        top: -4px;
        right: -4px;
        min-width: 20px;
        height: 20px;
        padding: 0 6px;
        background: #fff;
        color: var(--bim-accent, #e63737);
        font-size: 11px;
        font-weight: 800;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        line-height: 1;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
        animation: bimFadeIn 0.3s ease-out;
    }

    /* ── Overlay ── */
    .bim-chat-sheet-overlay {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 9999;
        background: rgba(0, 0, 0, 0.4);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s ease;
    }

    .bim-chat-sheet-overlay--active {
        opacity: 1;
        pointer-events: all;
    }

    /* ── Bottom Sheet ── */
    .bim-chat-sheet {
        display: flex;
        flex-direction: column;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 10000;
        max-height: 80vh;
        background: var(--bim-surface, #fff);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.12);
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    }

    .bim-chat-sheet--open {
        transform: translateY(0);
    }

    /* Drag handle — spacer | pill | X button */
    .bim-chat-sheet-handle {
        display: flex;
        align-items: center;
        padding: 10px 12px 8px;
        flex-shrink: 0;
    }

    .bim-chat-sheet-handle-spacer {
        flex: 1;
    }

    .bim-chat-sheet-drag {
        flex: 1;
        display: flex;
        justify-content: center;
        cursor: pointer;
    }

    .bim-chat-sheet-drag::after {
        content: '';
        display: block;
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--bim-border, #d1d5db);
    }

    .bim-chat-sheet-close {
        flex-shrink: 0;
        width: 32px;
        height: 32px;
        border: 1.5px solid var(--bim-border, #e5e7eb);
        background: var(--bim-surface, #fff);
        color: var(--bim-text-muted, #6b7280);
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        border-radius: 50%;
        padding: 0;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
        transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
    }

    .bim-chat-sheet-close:hover {
        background: var(--bim-accent-light, rgba(255,0,0,0.06));
        border-color: var(--bim-accent-border, rgba(255,0,0,0.3));
        color: var(--bim-accent, #e63737);
        box-shadow: 0 2px 8px rgba(230, 55, 55, 0.15);
    }

    .bim-chat-sheet-close:active {
        transform: scale(0.92);
    }

    .bim-chat-sheet-close .material-symbols-outlined {
        font-size: 18px;
        font-variation-settings: 'wght' 600;
    }

    /* ── Chat panel overrides inside sheet ── */
    .bim-chat-sheet .bim-order-chat-panel {
        display: flex !important;
        flex-direction: column;
        flex: 1;
        min-height: 0;
        margin: 0;
        border: none;
        border-radius: 0;
        box-shadow: none;
        padding: 0 var(--bim-space-4) var(--bim-space-4);
        padding-bottom: max(var(--bim-space-4), env(safe-area-inset-bottom));
    }

    .bim-chat-sheet .bim-order-chat-messages {
        flex: 1;
        min-height: 120px;
        max-height: calc(80vh - 160px);
    }

    .bim-chat-sheet .bim-order-chat-input {
        flex-shrink: 0;
    }

    /* Hide FAB when sheet is open (class toggled by JS) */
    .bim-chat-fab-wrap.bim-fab-hidden {
        opacity: 0;
        pointer-events: none;
        transform: scale(0.5);
        transition: opacity 0.2s ease, transform 0.2s ease;
    }
}

/* ── FAB: smaller phones ── */
@media (max-width: 360px) {
    .bim-chat-fab {
        width: 46px;
        height: 46px;
    }

    .bim-chat-fab .material-symbols-outlined {
        font-size: 22px;
    }

    .bim-has-sticky-bar .bim-chat-fab-wrap {
        bottom: 70px;
    }
}
