/* Embedded CRM AI chat panel (SystemAdmin) */

#claude-chat-fab {
    position: fixed;
    bottom: 28px;
    right: 28px;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--SideNavBackground, #0F2742);
    color: var(--SideNavTextColor, #CDD6DF);
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: transform 0.2s, box-shadow 0.2s;
}

#claude-chat-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 18px rgba(0,0,0,0.3);
}

#claude-chat-fab svg {
    width: 22px;
    height: 22px;
}

#claude-chat-panel {
    position: fixed;
    bottom: 92px;
    right: 28px;
    width: 380px;
    /* ~75% of viewport (per design), capped so the panel does not run past the top above the FAB */
    height: min(75vh, calc(100vh - 104px));
    height: min(75vh, calc(100dvh - 104px));
    background: var(--Surface-4, #FDFDFD);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    display: flex;
    flex-direction: column;
    z-index: 9998;
    overflow: hidden;
    transform-origin: bottom right;
    transform: scale(0.85) translateY(12px);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.25s cubic-bezier(0.34,1.56,0.64,1), opacity 0.2s ease;
}

#claude-chat-panel.open {
    transform: scale(1) translateY(0);
    opacity: 1;
    pointer-events: all;
}

/* Header */
.claude-chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    background: var(--SideNavBackground, #0F2742);
    color: var(--SideNavTextColor, #CDD6DF);
    flex-shrink: 0;
}

.claude-chat-header .claude-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255,255,255,0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.claude-chat-header .claude-avatar svg {
    width: 16px;
    height: 16px;
}

.claude-header-info {
    flex: 1;
}

.claude-header-info .claude-title {
    font-weight: 600;
    font-size: 14px;
    line-height: 1.2;
}

.claude-header-info .claude-status {
    font-size: 11px;
    opacity: 0.7;
    margin-top: 1px;
}

.claude-header-actions {
    display: flex;
    gap: 4px;
}

.claude-header-actions button {
    background: none !important;
    border: none !important;
    color: var(--SideNavTextColor, #CDD6DF) !important;
    cursor: pointer;
    padding: 5px 7px;
    border-radius: 6px;
    line-height: 0;
    opacity: 0.85;
    transition: opacity 0.15s, background 0.15s;
    box-shadow: none !important;
}

.claude-header-actions button svg {
    width: 16px;
    height: 16px;
    display: block;
}

.claude-header-actions button:hover {
    opacity: 1;
    background: rgba(255,255,255,0.12);
}

/* Messages area */
.claude-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
}

.claude-empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: var(--Interactive-Default, #666);
    padding: 20px;
    margin: auto 0;
}

.claude-empty-state .claude-empty-icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--Surface-1, #E5E5E5);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 12px;
}

.claude-empty-state .claude-empty-icon svg {
    width: 24px;
    height: 24px;
    opacity: 0.5;
}

.claude-empty-state p {
    font-size: 13px;
    margin: 0;
    opacity: 0.7;
}

/* Bubbles */
.claude-message {
    max-width: 85%;
    padding: 10px 13px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
}

.claude-message.user {
    align-self: flex-end;
    background: var(--SideNavBackground, #0F2742);
    color: var(--SideNavTextColor, #CDD6DF);
    border-bottom-right-radius: 4px;
}

.claude-message.assistant {
    align-self: flex-start;
    background: var(--Surface-1, #E5E5E5);
    color: var(--Text, #333);
    border-bottom-left-radius: 4px;
}

/* Rich (A2UI) assistant messages — lists, tables, cards rendered by a2ui-render.js */
.claude-message--rich {
    max-width: 100%;
    align-self: stretch;
    white-space: normal;
}

.claude-message.assistant .md-h2 { font-size: 1.05rem; font-weight: 600; margin: 4px 0 2px; display: block; }
.claude-message.assistant .md-h3 { font-size: 1rem; font-weight: 600; margin: 4px 0 2px; display: block; }
.claude-message.assistant .md-bold { font-weight: 600; }
.claude-message.assistant .md-list-item { display: block; padding-left: 2px; }
.claude-message.assistant .md-list-item::before { content: '\2022'; margin-right: 6px; opacity: .5; }
.claude-message.assistant code { background: rgba(0,0,0,.06); padding: 1px 5px; border-radius: 4px; font-size: .9em; }

.claude-message.assistant .a2ui-card {
    border: 1px solid var(--Outline, #D2D2D2);
    border-radius: 10px;
    padding: 12px;
    margin: 8px 0;
    background: var(--Surface-4, #fff);
    color: var(--Text, #333);
}
.claude-message.assistant .a2ui-text--h3 { font-size: 1rem; font-weight: 600; margin: 3px 0; }
.claude-message.assistant .a2ui-text--body { margin: 3px 0; }
.claude-message.assistant .a2ui-text--caption { font-size: .8rem; opacity: .7; margin: 3px 0; }

/* Tables scroll horizontally inside the narrow panel */
.claude-message.assistant .a2ui-report-table-wrap {
    overflow-x: auto;
    margin-top: 8px;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
    border: 1px solid #dee2e6;
    border-radius: 8px;
}
.claude-message.assistant .a2ui-report-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12.5px;
    background: #fff;
}
.claude-message.assistant .a2ui-report-table thead th {
    background: #e9ecef;
    color: #111;
    text-transform: uppercase;
    font-weight: 600;
    font-size: 10.5px;
    letter-spacing: 0.02em;
    padding: 8px 10px;
    border: 1px solid #dee2e6;
    text-align: left;
    white-space: nowrap;
}
.claude-message.assistant .a2ui-report-table thead th.num { text-align: right; }
.claude-message.assistant .a2ui-report-table thead th.qty { text-align: center; }
.claude-message.assistant .a2ui-report-table tbody td {
    padding: 7px 10px;
    border: 1px solid #dee2e6;
    vertical-align: middle;
}
.claude-message.assistant .a2ui-report-table tbody td.num { text-align: right; font-variant-numeric: tabular-nums; }
.claude-message.assistant .a2ui-report-table tbody td.cat { text-align: left; }
.claude-message.assistant .a2ui-report-table tbody tr:nth-child(even) { background: rgba(0,0,0,0.02); }
.claude-message.assistant .a2ui-report-cat-link { color: #0b5ed7; text-decoration: underline; }
.claude-message.assistant .a2ui-report-table tfoot td {
    padding: 8px 10px;
    border: 1px solid #dee2e6;
    font-weight: 700;
    background: #f1f3f5;
}
.claude-message.assistant .a2ui-report-table tfoot td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Email result grid — stacked single column for the narrow panel */
.claude-message.assistant .a2ui-email-grid {
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: 8px;
    overflow: hidden;
    margin-top: 6px;
    max-width: 100%;
}
.claude-message.assistant .a2ui-email-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 4px;
    padding: 9px 10px;
    border-top: 1px solid rgba(0,0,0,0.08);
}
.claude-message.assistant .a2ui-email-row:first-child { border-top: none; }
.claude-message.assistant .a2ui-email-grid .a2ui-email-row:nth-child(even) { background: rgba(0,0,0,0.03); }
.claude-message.assistant .a2ui-email-meta { font-size: 11px; color: rgba(0,0,0,0.7); }
.claude-message.assistant .a2ui-email-meta-line { display: block; }
.claude-message.assistant .a2ui-email-subject { font-weight: 600; font-size: 12.5px; margin-bottom: 4px; word-break: break-word; }
.claude-message.assistant .a2ui-email-summary { font-size: 12px; line-height: 1.45; color: rgba(0,0,0,0.85); word-break: break-word; white-space: pre-wrap; }

.claude-message.assistant .ai-assistant-hitl-btn {
    background: var(--Surface-4, #fff);
    border: 1px solid var(--Outline, #D2D2D2);
    color: var(--Text, #333);
    padding: 7px 13px;
    border-radius: 8px;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    margin: 6px 6px 0 0;
}

/* Typing indicator */
.claude-typing {
    align-self: flex-start;
    background: var(--Surface-1, #E5E5E5);
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    padding: 10px 14px;
    display: flex;
    gap: 4px;
    align-items: center;
}

.claude-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--Interactive-Default, #666);
    animation: claudeBounce 1.2s infinite ease-in-out;
}

.claude-typing span:nth-child(2) { animation-delay: 0.2s; }
.claude-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes claudeBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Upload bar */
.claude-upload-bar {
    display: none;
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 14px;
    border-top: 1px solid var(--Divider, #F0F0F0);
    background: var(--Surface-3, #FBFBFB);
    flex-shrink: 0;
}

.claude-upload-bar.has-files {
    display: flex;
}

.claude-file-chip {
    display: flex;
    align-items: center;
    gap: 5px;
    background: var(--Surface-1, #E5E5E5);
    border-radius: 20px;
    padding: 3px 10px 3px 8px;
    font-size: 12px;
    color: var(--Text, #333);
    max-width: 160px;
}

.claude-file-chip span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.claude-file-chip button {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    color: var(--Interactive-Default, #666);
    line-height: 1;
    font-size: 11px;
    flex-shrink: 0;
}

/* Input area */
.claude-chat-input-area {
    padding: 10px 14px 12px;
    border-top: 1px solid var(--Divider, #F0F0F0);
    background: var(--Surface-3, #FBFBFB);
    flex-shrink: 0;
}

.claude-input-box {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    background: var(--Surface-4, #FDFDFD);
    border: 1px solid var(--Outline, #D2D2D2);
    border-radius: 12px;
    padding: 6px 6px 6px 12px;
    transition: border-color 0.15s;
}

.claude-input-box:focus-within {
    border-color: var(--buttonprimarybackground, #6c8eaf);
}

#claude-chat-input {
    flex: 1;
    /* Global forms.css forces a border/box-shadow on every textarea (!important); defeat it so only the rounded input box shows. */
    border: none !important;
    outline: none !important;
    box-shadow: none !important;
    background: transparent;
    resize: none;
    font-size: 13.5px;
    color: var(--Text, #333);
    min-height: 0;
    max-height: 100px;
    overflow-y: auto;
    font-family: inherit;
    padding: 2px 0;
    line-height: 1.4;
}

#claude-chat-input:focus,
#claude-chat-input:focus-visible {
    border: none !important;
    outline: none !important;
    box-shadow: none !important;
}

#claude-chat-input::placeholder {
    color: var(--Interactive-Default, #666);
    opacity: 0.7;
}

.claude-input-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.claude-input-actions button {
    background: none !important;
    border: none !important;
    cursor: pointer;
    padding: 6px 7px;
    border-radius: 8px;
    color: var(--Interactive-Default, #666);
    line-height: 0;
    transition: background 0.15s, color 0.15s;
    box-shadow: none !important;
}

.claude-input-actions button svg {
    width: 17px;
    height: 17px;
    display: block;
}

.claude-input-actions button:hover {
    background: var(--Surface-1, #E5E5E5);
}

.claude-input-actions .claude-send-btn {
    border-radius: 8px;
    color: var(--Interactive-Default, #666);
}

/* Enabled (text present): bold dark-blue icon so the send action clearly stands out, like the AI Assistant page. */
.claude-input-actions .claude-send-btn:not(:disabled) {
    color: var(--SideNavBackground, #0F2742);
}

.claude-input-actions .claude-send-btn:disabled {
    opacity: 0.5;
    cursor: default;
}

/* Blinking cursor on the assistant bubble while streaming */
.claude-message.assistant.claude-streaming::after {
    content: '';
    display: inline-block;
    width: 2px;
    height: 13px;
    background: var(--Interactive-Default, #666);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: claudeCursorBlink .7s step-end infinite;
}

@keyframes claudeCursorBlink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* Stop mode: square icon shown while a request is in flight */
.claude-input-actions .claude-send-btn.is-stop {
    color: #dc2626 !important;
    opacity: 1 !important;
    cursor: pointer !important;
}

.claude-input-actions .claude-send-btn.is-stop:hover {
    background: rgba(220, 38, 38, 0.1) !important;
}

/* "Stopped" badge appended to the bubble when the user aborts mid-stream */
.claude-message--stopped-badge {
    display: block;
    font-size: 11px;
    opacity: 0.5;
    margin-top: 6px;
    padding-top: 5px;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
}

/* Tool-call progress shown inside the streaming bubble while the model executes tools silently */
.claude-tool-progress-box {
    display: flex;
    flex-direction: column;
    gap: 5px;
    padding: 2px 0;
}

.claude-tool-progress-item {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    color: var(--Interactive-Default, #666);
    opacity: 0.7;
}

.claude-tool-progress-spinner {
    width: 11px;
    height: 11px;
    border: 2px solid rgba(0, 0, 0, 0.12);
    border-top-color: var(--SideNavBackground, #0F2742);
    border-radius: 50%;
    flex-shrink: 0;
    animation: claudeToolSpin 0.75s linear infinite;
}

@keyframes claudeToolSpin {
    to { transform: rotate(360deg); }
}

.claude-input-footer {
    text-align: center;
    font-size: 10.5px;
    color: var(--Interactive-Default, #666);
    opacity: 0.6;
    margin-top: 7px;
}

/* Mobile */
@media (max-width: 480px) {
    #claude-chat-panel {
        width: calc(100vw - 20px);
        right: 10px;
        bottom: 80px;
        height: min(75vh, calc(100vh - 92px));
        height: min(75vh, calc(100dvh - 92px));
    }

    #claude-chat-fab {
        bottom: 16px;
        right: 16px;
    }
}
