/* ============================================================
   Chat Feature: Digital Doctors CRM
   ============================================================ */

/* Floating Chat Button (FAB)
   ============================================================ */
.chat-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.4), 0 2px 8px rgba(0,0,0,0.1);
    transition: transform 0.2s, box-shadow 0.2s;
    user-select: none;
}

.chat-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 28px rgba(99, 102, 241, 0.5), 0 3px 12px rgba(0,0,0,0.12);
}

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

.chat-fab-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    background: #ef4444;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
    animation: fabPulse 2s infinite;
}

@keyframes fabPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.12); }
}

/* Floating Chat Panel
   ============================================================ */
.chat-panel {
    position: fixed;
    bottom: 92px;
    right: 24px;
    z-index: 9998;
    width: 380px;
    height: 520px;
    max-height: calc(100vh - 120px);
    background: var(--color-bg, #fff);
    border-radius: 16px;
    border: 1px solid var(--color-border, #e5e7eb);
    box-shadow: 0 12px 48px rgba(0,0,0,0.15), 0 4px 16px rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(12px) scale(0.95);
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.chat-panel.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* Panel Header
   ============================================================ */
.chat-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: #fff;
}

.chat-panel-header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 15px;
    font-weight: 700;
}

.chat-panel-header-right {
    display: flex;
    align-items: center;
    gap: 4px;
}

.chat-panel-btn {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    border: none;
    background: rgba(255,255,255,0.15);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}

.chat-panel-btn:hover {
    background: rgba(255,255,255,0.3);
    color: #fff;
}

/* Panel Body
   ============================================================ */
.chat-panel-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

.chat-panel-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 40px 20px;
    color: var(--color-text-muted, #9ca3af);
    font-size: 13px;
}

.chat-spinner {
    width: 20px;
    height: 20px;
    border: 2px solid var(--color-border, #e5e7eb);
    border-top-color: var(--color-primary, #6366f1);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Conversation Items in Panel
   ============================================================ */
.chat-panel-conv {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    cursor: pointer;
    transition: background 0.12s;
    border-bottom: 1px solid var(--color-border-light, #f3f4f6);
}

.chat-panel-conv:hover {
    background: var(--color-bg-secondary, #f9fafb);
}

.chat-panel-conv.unread {
    background: rgba(99, 102, 241, 0.04);
}

.chat-panel-conv.unread .chat-panel-conv-name {
    font-weight: 700;
}

.chat-panel-conv-avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #fff;
    font-size: 13px;
    font-weight: 700;
}

.chat-panel-conv-info {
    flex: 1;
    min-width: 0;
}

.chat-panel-conv-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text, #111827);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-panel-conv-preview {
    font-size: 12px;
    color: var(--color-text-muted, #9ca3af);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}

.chat-panel-conv-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    flex-shrink: 0;
}

.chat-panel-conv-time {
    font-size: 10px;
    color: var(--color-text-muted, #9ca3af);
}

.chat-panel-conv-badge {
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--color-primary, #6366f1);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Thread View
   ============================================================ */
.chat-panel-thread {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.chat-panel-thread-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    background: var(--color-bg-secondary, #f9fafb);
    border-bottom: 1px solid var(--color-border, #e5e7eb);
}

.chat-panel-thread-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    flex-shrink: 0;
}

.chat-panel-thread-name {
    font-size: 13px;
    font-weight: 700;
    color: var(--color-text, #111827);
}

/* Messages in Panel
   ============================================================ */
.chat-panel-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-panel-msg {
    display: flex;
    max-width: 80%;
    animation: panelMsgIn 0.2s ease-out;
}

@keyframes panelMsgIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.chat-panel-msg.mine {
    align-self: flex-end;
}

.chat-panel-msg.theirs {
    align-self: flex-start;
}

.chat-panel-msg-bubble {
    padding: 8px 12px;
    border-radius: 14px;
    font-size: 13px;
    line-height: 1.45;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.chat-panel-msg.mine .chat-panel-msg-bubble {
    background: linear-gradient(135deg, #6366f1, #818cf8);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-panel-msg.theirs .chat-panel-msg-bubble {
    background: var(--color-bg-secondary, #f3f4f6);
    color: var(--color-text, #111827);
    border: 1px solid var(--color-border, #e5e7eb);
    border-bottom-left-radius: 4px;
}

.chat-panel-msg-time {
    font-size: 10px;
    margin-top: 3px;
    opacity: 0.55;
    text-align: right;
}

.chat-panel-msg.mine .chat-panel-msg-time {
    color: rgba(255,255,255,0.7);
}

.chat-panel-msg.theirs .chat-panel-msg-time {
    color: var(--color-text-muted, #9ca3af);
}

.chat-panel-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--color-text-muted, #9ca3af);
    font-size: 13px;
    gap: 6px;
    padding: 20px;
    text-align: center;
}

/* Panel Input
   ============================================================ */
.chat-panel-input {
    padding: 10px 12px;
    background: var(--color-bg, #fff);
    border-top: 1px solid var(--color-border, #e5e7eb);
}

.chat-panel-input-form {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--color-bg-secondary, #f9fafb);
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 24px;
    padding: 4px 4px 4px 14px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-panel-input-form:focus-within {
    border-color: var(--color-primary, #6366f1);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.1);
}

.chat-panel-input-form input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 13px;
    color: var(--color-text, #111827);
    padding: 6px 0;
}

.chat-panel-input-form input::placeholder {
    color: var(--color-text-muted, #9ca3af);
}

.chat-panel-send {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: none;
    background: var(--color-primary, #6366f1);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s;
}

.chat-panel-send:hover {
    background: var(--color-primary-dark, #4f46e5);
}

/* ============================================================
   Full Page Chat (Chat Page at /chat/)
   ============================================================ */
.chat-layout {
    display: flex;
    height: calc(100vh - var(--topbar-height, 64px) - 24px);
    background: var(--color-bg, #fff);
    border-radius: var(--radius-lg, 16px);
    border: 1px solid var(--color-border, #e5e7eb);
    overflow: hidden;
    margin-top: 20px;
}

.chat-sidebar {
    width: 360px;
    min-width: 300px;
    border-right: 1px solid var(--color-border, #e5e7eb);
    display: flex;
    flex-direction: column;
    background: var(--color-bg, #fff);
}

.chat-sidebar-compact {
    width: 300px;
    min-width: 260px;
}

.chat-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 20px 0;
}

.chat-sidebar-header h6 {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    color: var(--color-text, #111827);
}

.chat-contact-count {
    font-size: 12px;
    color: var(--color-text-muted, #9ca3af);
}

.chat-back-link {
    font-size: 12px;
    color: var(--color-primary, #6366f1);
    text-decoration: none;
    font-weight: 500;
}

.chat-back-link:hover {
    text-decoration: underline;
}

.chat-search-box {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 12px 16px;
    padding: 10px 14px;
    background: var(--color-bg-secondary, #f9fafb);
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: var(--radius, 8px);
    transition: border-color 0.2s;
}

.chat-search-box:focus-within {
    border-color: var(--color-primary, #6366f1);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.1);
}

.chat-search-box svg {
    color: var(--color-text-muted, #9ca3af);
    flex-shrink: 0;
}

.chat-search-box input {
    border: none;
    background: transparent;
    outline: none;
    font-size: 13px;
    color: var(--color-text, #111827);
    width: 100%;
}

.chat-search-box input::placeholder {
    color: var(--color-text-muted, #9ca3af);
}

.chat-contact-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.chat-contact-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border-radius: var(--radius, 8px);
    text-decoration: none;
    color: inherit;
    transition: background 0.15s;
    position: relative;
}

.chat-contact-item:hover {
    background: var(--color-bg-secondary, #f9fafb);
}

.chat-contact-item.active {
    background: var(--color-primary-light, #eef2ff);
    border: 1px solid rgba(99,102,241,0.15);
}

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

.chat-contact-item.unread .chat-contact-preview {
    color: var(--color-text, #111827);
    font-weight: 500;
}

.chat-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--color-primary, #6366f1), #8b5cf6);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    position: relative;
}

.chat-avatar-sm {
    width: 36px;
    height: 36px;
}

.chat-avatar-initials {
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.chat-avatar-sm .chat-avatar-initials {
    font-size: 12px;
}

.chat-online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 10px;
    height: 10px;
    background: #22c55e;
    border: 2px solid var(--color-bg, #fff);
    border-radius: 50%;
    display: none;
}

.chat-contact-info {
    flex: 1;
    min-width: 0;
}

.chat-contact-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text, #111827);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-contact-role {
    font-size: 11px;
    color: var(--color-text-muted, #9ca3af);
    margin-top: 1px;
    text-transform: capitalize;
}

.chat-contact-preview {
    font-size: 12px;
    color: var(--color-text-muted, #9ca3af);
    margin-top: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-contact-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    flex-shrink: 0;
}

.chat-contact-time {
    font-size: 11px;
    color: var(--color-text-muted, #9ca3af);
}

.chat-unread-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    background: var(--color-primary, #6366f1);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
}

.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
    gap: 8px;
}

.chat-empty-state p {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text, #111827);
    margin: 8px 0 0;
}

.chat-empty-state span {
    font-size: 12px;
    color: var(--color-text-muted, #9ca3af);
}

.chat-main-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-bg-secondary, #f9fafb);
}

.chat-main-empty-content {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.chat-main-empty-content h4 {
    margin: 12px 0 0;
    font-size: 18px;
    font-weight: 700;
    color: var(--color-text, #111827);
}

.chat-main-empty-content p {
    font-size: 14px;
    color: var(--color-text-muted, #9ca3af);
    margin: 0;
}

.chat-room {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--color-bg-secondary, #f9fafb);
    min-width: 0;
}

.chat-room-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    background: var(--color-bg, #fff);
    border-bottom: 1px solid var(--color-border, #e5e7eb);
}

.chat-room-back {
    display: none;
    color: var(--color-text-muted, #9ca3af);
    text-decoration: none;
    padding: 4px;
    border-radius: var(--radius, 8px);
}

.chat-room-back:hover {
    background: var(--color-bg-secondary, #f9fafb);
    color: var(--color-text, #111827);
}

.chat-room-user {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-room-name {
    font-size: 15px;
    font-weight: 700;
    color: var(--color-text, #111827);
}

.chat-room-status {
    font-size: 12px;
    color: var(--color-text-muted, #9ca3af);
}

.chat-room-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-room-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 12px;
}

.chat-room-empty p {
    font-size: 14px;
    color: var(--color-text-muted, #9ca3af);
    margin: 0;
}

.chat-msg {
    display: flex;
    max-width: 65%;
    animation: msgIn 0.2s ease-out;
}

@keyframes msgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.chat-msg.mine {
    align-self: flex-end;
}

.chat-msg.theirs {
    align-self: flex-start;
}

.chat-msg-bubble {
    padding: 10px 14px;
    border-radius: 16px;
    position: relative;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.chat-msg.mine .chat-msg-bubble {
    background: linear-gradient(135deg, var(--color-primary, #6366f1), #818cf8);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-msg.theirs .chat-msg-bubble {
    background: var(--color-bg, #fff);
    color: var(--color-text, #111827);
    border: 1px solid var(--color-border, #e5e7eb);
    border-bottom-left-radius: 4px;
}

.chat-msg-body {
    font-size: 14px;
    line-height: 1.5;
    white-space: pre-wrap;
}

.chat-msg-time {
    font-size: 10px;
    margin-top: 4px;
    opacity: 0.6;
    text-align: right;
}

.chat-msg.mine .chat-msg-time {
    color: rgba(255,255,255,0.7);
}

.chat-msg.theirs .chat-msg-time {
    color: var(--color-text-muted, #9ca3af);
}

.chat-room-input {
    padding: 16px 20px;
    background: var(--color-bg, #fff);
    border-top: 1px solid var(--color-border, #e5e7eb);
}

.chat-input-form {
    width: 100%;
}

.chat-input-wrap {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--color-bg-secondary, #f9fafb);
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 24px;
    padding: 6px 6px 6px 16px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-input-wrap:focus-within {
    border-color: var(--color-primary, #6366f1);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.1);
}

.chat-input-field {
    flex: 1;
    border: none !important;
    background: transparent !important;
    outline: none !important;
    font-size: 14px;
    color: var(--color-text, #111827);
    resize: none;
    min-height: 24px;
    max-height: 120px;
    line-height: 1.5;
    padding: 6px 0;
    box-shadow: none !important;
}

.chat-input-field::placeholder {
    color: var(--color-text-muted, #9ca3af);
}

.chat-send-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: none;
    background: var(--color-primary, #6366f1);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s, transform 0.15s;
    flex-shrink: 0;
}

.chat-send-btn:hover {
    background: var(--color-primary-dark, #4f46e5);
    transform: scale(1.05);
}

.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.sidebar-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--color-danger, #ef4444);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    margin-left: auto;
}

/* Responsive
   ============================================================ */
@media (max-width: 768px) {
    .chat-layout {
        height: calc(100vh - var(--topbar-height, 64px));
        border-radius: 0;
        border: none;
        margin-top: 0;
    }

    .chat-sidebar {
        width: 100%;
        min-width: 100%;
    }

    .chat-sidebar-compact {
        display: none;
    }

    .chat-room-back {
        display: flex;
    }

    .chat-msg {
        max-width: 85%;
    }

    .chat-panel {
        width: calc(100vw - 16px);
        right: 8px;
        bottom: 84px;
        height: calc(100vh - 100px);
        border-radius: 12px;
    }
}
