/**
 * Chat mobile/Android – design system & components
 * Primary: #1E5EFF | Gradient: #1E5EFF → #2F7BFF | Bg: #F4F7FB | Card: #FFFFFF
 * Text: #101828 | Muted: #667085 | Border: #E6EAF2 | Success: #12B76A | Danger: #F04438
 */
:root {
    --chat-primary: #1E5EFF;
    --chat-primary-end: #2F7BFF;
    --chat-bg: #F4F7FB;
    --chat-card: #FFFFFF;
    --chat-text: #101828;
    --chat-muted: #667085;
    --chat-border: #E6EAF2;
    --chat-success: #12B76A;
    --chat-danger: #F04438;
    --chat-shadow: 0 6px 18px rgba(16, 24, 40, 0.08);
    --chat-radius: 16px;
    --chat-radius-btn: 14px;
    --chat-space-8: 8px;
    --chat-space-16: 16px;
    --chat-space-24: 24px;
}

/* ----- Page shell ----- */
.m-chat-page {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    background: var(--chat-bg);
    font-family: Roboto, system-ui, sans-serif;
    overflow-x: hidden;
}

/* ----- Header sticky (3 zone: stânga / centru / dreapta) ----- */
.m-chat-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 56px;
    padding: 12px var(--chat-space-16);
    background: linear-gradient(135deg, var(--chat-primary) 0%, var(--chat-primary-end) 100%);
    color: #fff;
    box-shadow: var(--chat-shadow);
}
.m-chat-header-three-zones {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 8px;
}
.m-chat-header-three-zones .m-chat-header-left {
    justify-content: flex-start;
}
.m-chat-header-three-zones .m-chat-header-center {
    justify-content: center;
    text-align: center;
}
.m-chat-header-three-zones .m-chat-header-right {
    justify-content: flex-end;
}
.m-chat-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}
.m-chat-header-center {
    flex: 1;
    min-width: 0;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
}
.m-chat-header-title {
    font-size: 18px;
    font-weight: 600;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.m-chat-header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}
.m-chat-back-btn,
.m-chat-close-btn {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    border: none;
    background: rgba(255,255,255,0.2);
    color: #fff;
    border-radius: var(--chat-radius-btn);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.m-chat-back-btn:hover,
.m-chat-close-btn:hover {
    background: rgba(255,255,255,0.3);
}
.m-chat-plus-btn {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    border: none;
    background: rgba(255,255,255,0.2);
    color: #fff;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.m-chat-plus-btn:hover {
    background: rgba(255,255,255,0.3);
}

/* ----- Search (UI only) ----- */
.m-chat-search-wrap {
    padding: var(--chat-space-8) var(--chat-space-16);
    flex-shrink: 0;
}
.m-chat-search {
    width: 100%;
    padding: 12px 16px 12px 40px;
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    background: var(--chat-card);
    font-size: 14px;
    color: var(--chat-text);
}
.m-chat-search::placeholder {
    color: var(--chat-muted);
}

/* ----- List: ChatRoomCard ----- */
.m-chat-list {
    flex: 1;
    overflow-y: auto;
    padding: var(--chat-space-8) var(--chat-space-16);
    min-height: 0;
}
.m-chat-list-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: var(--chat-space-16);
    margin-bottom: 8px;
    background: var(--chat-card);
    border-radius: var(--chat-radius);
    box-shadow: var(--chat-shadow);
    cursor: pointer;
    border: 1px solid transparent;
    min-height: 0;
}
.m-chat-list-item:hover {
    border-color: var(--chat-border);
}
.m-chat-list-item.active {
    border-color: var(--chat-primary);
    background: rgba(30, 94, 255, 0.06);
}
.m-chat-list-item-avatar {
    width: 48px;
    height: 48px;
    min-width: 48px;
    border-radius: 50%;
    background: var(--chat-bg);
    color: var(--chat-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}
.m-chat-list-item-body {
    flex: 1;
    min-width: 0;
}
.m-chat-list-item-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--chat-text);
    margin: 0 0 2px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.m-chat-list-item-sub {
    font-size: 12px;
    color: var(--chat-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.m-chat-list-item-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}
.m-chat-chip {
    font-size: 11px;
    font-weight: 600;
    padding: 4px 8px;
    border-radius: 10px;
    background: var(--chat-danger);
    color: #fff;
}
.m-chat-list-item .m-chat-delete-btn {
    width: 36px;
    height: 36px;
    min-width: 36px;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--chat-muted);
    border-radius: var(--chat-radius-btn);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.m-chat-list-item .m-chat-delete-btn:hover {
    color: var(--chat-danger);
    background: rgba(240, 68, 56, 0.08);
}

/* ----- Empty state ----- */
.m-chat-empty {
    padding: var(--chat-space-24);
    text-align: center;
    background: var(--chat-card);
    border-radius: var(--chat-radius);
    box-shadow: var(--chat-shadow);
    margin: var(--chat-space-16);
}
.m-chat-empty-icon {
    font-size: 40px;
    color: var(--chat-border);
    margin-bottom: 12px;
    display: block;
}
.m-chat-empty-text {
    font-size: 14px;
    color: var(--chat-muted);
    margin: 0 0 16px 0;
}
.m-chat-empty .m-primary-btn {
    min-height: 44px;
    padding: 0 24px;
    border-radius: var(--chat-radius-btn);
    font-weight: 500;
}

/* ----- Conversation: messages area ----- */
.m-chat-conv {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}
.m-chat-conv-header {
    flex-shrink: 0;
    padding: 8px var(--chat-space-16);
    border-bottom: 1px solid var(--chat-border);
    background: var(--chat-card);
}
.m-chat-conv-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 4px;
}
.m-chat-conv-chip {
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 8px;
    background: var(--chat-bg);
    color: var(--chat-muted);
}
.m-chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--chat-space-16);
    min-height: 0;
    -webkit-overflow-scrolling: touch;
}

/* ----- Chat thread: msg-row + msg-bubble (un singur container per mesaj, delete în interior) ----- */
.chat-thread {
    display: flex;
    flex-direction: column;
    gap: 0;
}
.msg-row {
    display: flex;
    margin: 10px 12px;
    max-width: 100%;
}
.msg-row.msg-in {
    justify-content: flex-start;
}
.msg-row.msg-out {
    justify-content: flex-end;
}
.msg-bubble,
.m-chat-bubble {
    max-width: 78%;
    min-width: 120px;
    width: max-content;
    padding: 12px 12px;
    padding-bottom: 28px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.4;
    position: relative;
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: normal;
    white-space: normal;
    flex-shrink: 0;
}
.msg-in .msg-bubble,
.msg-in .m-chat-bubble,
.m-chat-msg-wrapper.other .m-chat-bubble {
    background: #fff;
    border: 1px solid var(--chat-border);
    border-bottom-left-radius: 4px;
}
.msg-out .msg-bubble,
.msg-out .m-chat-bubble,
.m-chat-msg-wrapper.own .m-chat-bubble {
    background: rgba(30, 94, 255, 0.1);
    border: 1px solid rgba(30, 94, 255, 0.15);
    border-bottom-right-radius: 4px;
}
.m-chat-msg-wrapper {
    margin: 10px 12px;
    display: flex;
    max-width: 100%;
}
.m-chat-msg-wrapper.own {
    justify-content: flex-end;
}
.m-chat-msg-wrapper.other {
    justify-content: flex-start;
}
.m-chat-bubble-content {
    margin-bottom: 4px;
    padding-right: 28px;
}
#chatMessages .message-content,
#mobileChatMessages .message-content,
.msg-bubble .message-content,
.m-chat-bubble .message-content {
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: normal;
    white-space: normal;
    max-width: none;
}
.msg-out .m-chat-bubble-content {
    padding-right: 28px;
}
.m-chat-bubble-sender {
    font-weight: 600;
    font-size: 12px;
    color: var(--chat-muted);
    margin-bottom: 2px;
}
.msg-manager .m-chat-bubble-sender,
.m-chat-msg-wrapper.msg-manager .m-chat-bubble-sender {
    color: #0f6e56;
}
.msg-manager .msg-bubble,
.m-chat-msg-wrapper.msg-manager .m-chat-bubble {
    border-left: 3px solid #0f6e56;
}
.msg-manager .message-content {
    white-space: pre-line;
    line-height: 1.45;
}
.msg-manager .m-chat-bubble-delete + .m-chat-bubble-content {
    padding-right: 28px;
}
.m-chat-bubble-time {
    font-size: 11px;
    color: var(--chat-muted);
    position: absolute;
    bottom: 6px;
    right: 12px;
}
.msg-in .m-chat-bubble-time,
.m-chat-msg-wrapper.other .m-chat-bubble-time {
    right: auto;
    left: 12px;
}
/* Delete în colțul bubble-ului, NU ca element separat */
.m-chat-bubble-delete {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--chat-muted);
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 12px;
}
.m-chat-bubble-delete:hover {
    color: var(--chat-danger);
    background: rgba(240, 68, 56, 0.1);
}
.m-chat-attachment {
    margin-top: 8px;
    padding: 8px 12px;
    background: var(--chat-bg);
    border-radius: 10px;
    border: 1px solid var(--chat-border);
}
.m-chat-attachment a {
    color: var(--chat-primary);
    text-decoration: none;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.m-chat-attachment a:hover {
    text-decoration: underline;
}

/* ----- FileCard pentru atașamente (PDF etc.) ----- */
.file-card {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    padding: 10px;
    border: 1px solid var(--chat-border);
    border-radius: 12px;
    background: #F8FAFF;
}
.file-card-inner {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1;
}
.file-card-icon {
    color: var(--chat-primary);
    font-size: 20px;
    flex-shrink: 0;
}
.file-card-name {
    font-size: 13px;
    color: var(--chat-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.file-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}
.file-actions a,
.file-action-btn {
    display: inline-flex;
    align-items: center;
    padding: 8px 10px;
    border-radius: 10px;
    font-size: 13px;
    color: var(--chat-primary);
    background: rgba(30, 94, 255, 0.08);
    border: 1px solid rgba(30, 94, 255, 0.15);
    text-decoration: none;
}
.file-actions a:hover,
.file-action-btn:hover {
    background: rgba(30, 94, 255, 0.15);
}

/* ----- Composer sticky (compact: rând 1 input+send, rând 2 atașare + „niciun fișier”) ----- */
.m-chat-composer {
    flex-shrink: 0;
    padding: var(--chat-space-8) var(--chat-space-16);
    padding-bottom: calc(var(--chat-space-16) + env(safe-area-inset-bottom, 0));
    background: var(--chat-card);
    border-top: 1px solid var(--chat-border);
}
.m-chat-composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}
.m-chat-composer-input {
    flex: 1;
    min-height: 40px;
    max-height: 100px;
    padding: 10px 14px;
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    font-size: 14px;
    resize: none;
    font-family: inherit;
}
.m-chat-composer-input:focus {
    outline: none;
    border-color: var(--chat-primary);
}
.m-chat-composer-input::placeholder {
    color: var(--chat-muted);
}
.m-chat-send-btn {
    width: 44px;
    height: 40px;
    min-width: 44px;
    min-height: 40px;
    padding: 0;
    border: none;
    background: var(--chat-primary);
    color: #fff;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
}
.m-chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
.m-chat-attach-row {
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 28px;
}
.m-chat-attach-label {
    font-size: 12px;
    color: var(--chat-muted);
    padding: 6px 10px;
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius-btn);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.m-chat-attach-label:hover {
    background: var(--chat-bg);
}
.m-chat-no-file {
    font-size: 11px;
    color: var(--chat-muted);
}
.m-chat-file-chosen {
    font-size: 12px;
    color: var(--chat-muted);
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 4px;
}
.chat-attach-preview-wrap {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border-radius: 8px;
    overflow: hidden;
    background: var(--chat-bg, #f0f0f0);
    display: flex;
    align-items: center;
    justify-content: center;
}
.chat-attach-preview-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.chat-attach-preview-icon {
    font-size: 20px;
    color: var(--chat-muted, #666);
}
.chat-attach-preview-icon .fa-file-pdf { color: #c00; }
.chat-attach-preview-icon .fa-file-word { color: #2b579a; }
.chat-attach-preview-icon .fa-file-excel { color: #217346; }
.chat-attach-preview-icon .fa-file-image { color: #0a0; }
.chat-attach-filename {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.m-chat-file-chosen .clear-file {
    color: var(--chat-danger);
    cursor: pointer;
    padding: 0 4px;
}

/* ----- Buttons ----- */
.m-primary-btn {
    background: var(--chat-primary);
    color: #fff;
    border: none;
    border-radius: var(--chat-radius-btn);
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.m-primary-btn:hover {
    background: var(--chat-primary-end);
}
.m-secondary-btn {
    background: var(--chat-bg);
    color: var(--chat-text);
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius-btn);
    font-weight: 500;
    cursor: pointer;
}

/* ----- Delete confirm modal ----- */
.m-chat-delete-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 1050;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.m-chat-delete-modal-backdrop.show {
    display: flex;
}
.m-chat-delete-modal {
    background: var(--chat-card);
    border-radius: var(--chat-radius);
    padding: var(--chat-space-24);
    max-width: 320px;
    width: 100%;
    box-shadow: var(--chat-shadow);
}
.m-chat-delete-modal h6 {
    margin: 0 0 8px 0;
    font-size: 18px;
    font-weight: 600;
    color: var(--chat-text);
}
.m-chat-delete-modal p {
    margin: 0 0 20px 0;
    font-size: 14px;
    color: var(--chat-muted);
}
.m-chat-delete-modal-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}
.m-chat-delete-modal-actions .m-secondary-btn {
    min-height: 44px;
    padding: 0 20px;
}
.m-chat-delete-modal-actions .m-primary-btn {
    min-height: 44px;
    padding: 0 20px;
    background: var(--chat-danger);
}
.m-chat-delete-modal-actions .m-primary-btn:hover {
    background: #d93a30;
}

/* ----- Sidebar layout (desktop + Android same structure) ----- */
#chatSidebar.m-chat-sidebar .chat-sidebar-header {
    background: linear-gradient(135deg, var(--chat-primary) 0%, var(--chat-primary-end) 100%);
    min-height: 56px;
    padding: 12px 16px;
    border: none;
}
#chatSidebar.m-chat-sidebar .chat-sidebar-content {
    background: var(--chat-bg);
    display: flex;
    flex-direction: column;
    min-height: 0;
}
#chatSidebar.m-chat-sidebar .chat-rooms-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 8px 16px;
    border: none;
    max-height: none;
}
#chatSidebar.m-chat-sidebar .chat-rooms-list {
    max-height: none;
    flex: 1;
    overflow-y: auto;
}
#chatSidebar.m-chat-sidebar .chat-messages-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
#chatSidebar.m-chat-sidebar .chat-messages-container {
    background: var(--chat-bg);
}
#chatSidebar.m-chat-sidebar .chat-input-section {
    flex-shrink: 0;
    padding: 8px 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0));
    background: var(--chat-card);
    border-top: 1px solid var(--chat-border);
}

/* Mobile section: list vs conversation view */
.m-chat-page.m-chat-conv-open .m-chat-search-wrap,
.m-chat-page.m-chat-conv-open #mobileChatRooms {
    display: none !important;
}
.m-chat-page.m-chat-conv-open #mobileChatConvWrap {
    display: flex !important;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.m-chat-page #mobileChatConvWrap {
    flex: 1;
    min-height: 0;
    flex-direction: column;
}

/* Legacy class compatibility: keep .chat-room-item, .chat-message for JS selectors */
#chatSidebar .chat-room-item.m-chat-list-item,
#chat-section .chat-room-item.m-chat-list-item { }
#chatMessages .chat-message-wrapper.m-chat-msg-wrapper,
#chatMessages .chat-message.m-chat-bubble { }

/* ----- Desktop: același comportament ca Android – doar camere SAU doar conversație ----- */
/* View listă camere: la deschidere doar camerele (fără mesaje) */
#chatSidebar.chat-sidebar-modern.android-chat-rooms-view .chat-messages-section {
    display: none !important;
}
#chatSidebar.chat-sidebar-modern.android-chat-rooms-view .chat-sidebar-content {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow: hidden !important;
}
#chatSidebar.chat-sidebar-modern.android-chat-rooms-view .chat-rooms-section {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    overflow: hidden !important;
    max-height: none !important;
    border-bottom: none !important;
    padding: 0.75rem 1rem !important;
}
#chatSidebar.chat-sidebar-modern.android-chat-rooms-view .chat-rooms-list {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    max-height: none !important;
    overflow-y: auto !important;
}

/* View în cameră: după click pe cameră – doar mesaje + input; lista de camere ascunsă */
#chatSidebar.chat-sidebar-modern.android-chat-room-view .chat-rooms-section {
    display: none !important;
}
#chatSidebar.chat-sidebar-modern.android-chat-room-view .chat-messages-section {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
}
/* Numele camerei e deja în header – ascundem sub-header-ul duplicat */
#chatSidebar.chat-sidebar-modern.android-chat-room-view .chat-room-header {
    display: none !important;
}

/* Manager Digital: butoane de confirmare sub mesaj */
.mact { margin-top: .5rem; margin-bottom: 1.1rem; }
.mact-buttons { display: flex; flex-wrap: wrap; gap: .4rem; }
.mact-btn { border: 1px solid #6d28d9; border-radius: 999px; padding: .3rem .8rem; font-size: .82rem; font-weight: 600; cursor: pointer; line-height: 1.2; }
.mact-primary { background: #6d28d9; color: #fff; }
.mact-secondary { background: #fff; color: #5b21b6; }
.mact-btn:disabled { opacity: .55; cursor: default; }
.mact-state { display: inline-block; font-size: .78rem; font-weight: 600; color: #475569; background: #f1f5f9; border-radius: 999px; padding: .2rem .65rem; }
.mact-state.mact-confirmed, .mact-state.mact-undone { color: #166534; background: #dcfce7; }
.mact-state.mact-rejected, .mact-state.mact-stopped, .mact-state.mact-failed { color: #991b1b; background: #fee2e2; }
[data-theme="dark"] .mact-secondary { background: #1e1b2e; color: #ddd6fe; border-color: #8b5cf6; }
[data-theme="dark"] .mact-state { background: #1e293b; color: #cbd5e1; }
