/* ── Reset scope ────────────────────────────────────────────────────────── */
#crm-chat-root * {
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}
#crm-chat-root *:focus { outline: none; }

/* ── Chat Bubble ─────────────────────────────────────────────────────────── */
#crm-chat-bubble {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: #0369a1;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
    z-index: 99998;
    transition: transform 0.2s;
}
#crm-chat-bubble:hover { transform: scale(1.08); }
#crm-chat-bubble svg { width: 26px; height: 26px; fill: #fff; }
#crm-chat-unread-badge {
    position: absolute;
    top: 0; right: 0;
    background: #dc2626;
    color: #fff;
    border-radius: 50%;
    width: 18px; height: 18px;
    font-size: 10px;
    align-items: center;
    justify-content: center;
    display: none;
}

/* ── Chat Window ─────────────────────────────────────────────────────────── */
#crm-chat-window {
    position: fixed;
    bottom: 90px;
    right: 24px;
    width: 360px;
    max-width: calc(100vw - 32px);
    height: 520px;
    max-height: calc(100vh - 110px);
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    z-index: 99999;
    display: none; /* JS controls open/close via inline style */
    flex-direction: column;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 14px;
    color: #1f2937;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
#crm-chat-header {
    background: #0369a1;
    color: #fff;
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
#crm-chat-header h4 { margin: 0; font-size: 15px; font-weight: 600; }
#crm-chat-header p  { margin: 2px 0 0; font-size: 12px; opacity: 0.85; }
#crm-chat-close-btn {
    background: none; border: none; color: #fff;
    cursor: pointer; font-size: 22px; line-height: 1; padding: 0;
}

/* ── Intake form ─────────────────────────────────────────────────────────── */
#crm-chat-intake {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
}
#crm-chat-intake.crm-chat-hidden { display: none; }
#crm-chat-intake h5 { margin: 0 0 16px; font-size: 15px; color: #0f172a; }
#crm-chat-intake input,
#crm-chat-intake select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 16px; /* prevent iOS zoom */
    margin-bottom: 10px;
    box-sizing: border-box;
    color: #1f2937;
}
#crm-chat-intake button {
    width: 100%;
    background: #0369a1;
    color: #fff;
    border: none;
    padding: 12px;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
    margin-top: 4px;
}
#crm-chat-intake button:hover { background: #0284c7; }
#crm-chat-intake button:disabled { opacity: 0.6; cursor: not-allowed; }
#crm-chat-intake-error { color: #dc2626; font-size: 13px; margin-top: 6px; display: none; }

/* ── Message list ────────────────────────────────────────────────────────── */
#crm-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: none;
    flex-direction: column;
    gap: 10px;
    min-height: 0; /* critical — prevents flex child from overflowing */
}
#crm-chat-messages.crm-chat-active {
    display: flex;
}
.crm-chat-msg {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}
.crm-chat-msg.visitor {
    align-self: flex-end;
    background: #0369a1;
    color: #fff;
    border-bottom-right-radius: 4px;
}
.crm-chat-msg.rep,
.crm-chat-msg.ai {
    align-self: flex-start;
    background: #f1f5f9;
    color: #1f2937;
    border-bottom-left-radius: 4px;
}
.crm-chat-sender {
    font-size: 11px;
    font-weight: 600;
    margin-bottom: 3px;
    opacity: 0.7;
}

/* ── Input area ──────────────────────────────────────────────────────────── */
#crm-chat-input-area {
    padding: 12px 16px;
    border-top: 1px solid #e5e7eb;
    display: none;
    gap: 8px;
    flex-shrink: 0;
    align-items: flex-end;
}
#crm-chat-input-area.crm-chat-active {
    display: flex;
}
#crm-chat-input {
    flex: 1;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 16px; /* prevent iOS zoom */
    resize: none;
    height: 40px;
    font-family: inherit;
    box-sizing: border-box;
}
#crm-chat-send-btn {
    background: #0369a1;
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 0 16px;
    height: 40px;
    cursor: pointer;
    font-size: 18px;
    flex-shrink: 0;
}
#crm-chat-send-btn:hover { background: #0284c7; }

/* ── Typing indicator ────────────────────────────────────────────────────── */
.crm-chat-typing {
    align-self: flex-start;
    background: #f1f5f9;
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    padding: 10px 16px;
    display: flex;
    gap: 4px;
    align-items: center;
}
.crm-chat-typing span {
    width: 6px; height: 6px;
    border-radius: 50%;
    background: #94a3b8;
    animation: crm-chat-bounce 1.2s infinite;
}
.crm-chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.crm-chat-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes crm-chat-bounce {
    0%, 80%, 100% { transform: translateY(0); }
    40%           { transform: translateY(-6px); }
}

/* ── End chat bar ─────────────────────────────────────────────────────────── */
#crm-chat-end-bar {
    display: none;
    justify-content: center;
    padding: 6px 16px 10px;
    flex-shrink: 0;
}
#crm-chat-end-btn {
    background: none;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    color: #6b7280;
    font-size: 12px;
    padding: 4px 14px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
#crm-chat-end-btn:hover {
    background: #fee2e2;
    border-color: #fca5a5;
    color: #dc2626;
}
