/**
 * TFP AI Chat Widget — Floating chat bubble and container styles.
 * Brand colors: Red #E1251B, Orange/Gold #F1A800, Black #000, White #FFF.
 */

/* Floating chat toggle button */
#tfp-ai-chat-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #E1251B;
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    z-index: 99998;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    transition: transform 0.2s, background 0.2s;
}

#tfp-ai-chat-toggle:hover {
    transform: scale(1.08);
    background: #c41f17;
}

#tfp-ai-chat-toggle.open {
    background: #555;
}

/* Chat panel */
#tfp-ai-chat-panel {
    position: fixed;
    bottom: 96px;
    right: 24px;
    width: 420px;
    height: 600px;
    max-height: calc(100vh - 120px);
    background: #ffffff;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    z-index: 99999;
    display: none;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(0, 0, 0, 0.1);
    transition: width 0.25s ease, height 0.25s ease, bottom 0.25s ease, right 0.25s ease, border-radius 0.25s ease;
}

#tfp-ai-chat-panel.open {
    display: flex;
}

/* Expanded (maximized) state */
#tfp-ai-chat-panel.expanded {
    width: 680px;
    height: calc(100vh - 48px);
    max-height: calc(100vh - 48px);
    bottom: 24px;
    right: 24px;
    border-radius: 16px;
}

/* Header */
.tfp-chat-header {
    background: #E1251B;
    padding: 14px 18px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: none;
}

.tfp-chat-header-logo {
    width: 32px;
    height: 32px;
    border-radius: 50%;
}

.tfp-chat-header-title {
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    flex: 1;
}

.tfp-chat-header-tier {
    font-size: 11px;
    color: #fff;
    background: rgba(255, 255, 255, 0.2);
    padding: 2px 8px;
    border-radius: 8px;
    font-weight: 600;
}

/* Header action buttons (expand/collapse) */
.tfp-chat-header-btn {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.7);
    cursor: pointer;
    font-size: 16px;
    padding: 4px;
    line-height: 1;
    border-radius: 4px;
    transition: color 0.15s, background 0.15s;
}

.tfp-chat-header-btn:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.15);
}

/* Agent selector */
.tfp-agent-selector {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    background: #f7f7f7;
    overflow-x: auto;
    flex-shrink: 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.tfp-agent-selector::-webkit-scrollbar {
    display: none;
}

.tfp-agent-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 5px 10px;
    background: #fff;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    color: #666;
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.15s;
}

.tfp-agent-btn:hover {
    background: #fff5f4;
    color: #333;
    border-color: rgba(0, 0, 0, 0.15);
}

.tfp-agent-btn.active {
    background: rgba(225, 37, 27, 0.08);
    border-color: #E1251B;
    color: #E1251B;
    font-weight: 600;
}

/* Deep Chat wrapper — must shrink in flex layout */
#tfp-deep-chat-wrapper {
    flex: 1;
    min-height: 0; /* Critical: allows flex child to shrink below content size */
    overflow: hidden;
}

/* Loading indicator (shown while API calls complete) */
.tfp-chat-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 12px;
    color: #999;
    font-size: 13px;
}

.tfp-chat-loading-spinner {
    width: 28px;
    height: 28px;
    border: 3px solid rgba(0, 0, 0, 0.08);
    border-top-color: #E1251B;
    border-radius: 50%;
    animation: tfp-spin 0.7s linear infinite;
}

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

/* Deep Chat overrides — CSS custom properties as progressive-enhancement fallback */
#tfp-ai-chat-panel deep-chat {
    display: block;
    width: 100%;
    height: 100%;
    --deep-chat-bg: #ffffff;
    --deep-chat-text: #333;
    --deep-chat-input-bg: #fff;
    --deep-chat-input-text: #333;
    --deep-chat-input-border: rgba(0, 0, 0, 0.12);
    --deep-chat-send-btn-bg: #E1251B;
    --deep-chat-send-btn-hover: #c41f17;
    --deep-chat-user-msg-bg: #E1251B;
    --deep-chat-user-msg-text: #fff;
    --deep-chat-ai-msg-bg: #f2f2f2;
    --deep-chat-ai-msg-text: #333;
    border: none;
    border-radius: 0;
}

/* Rate limit banner */
.tfp-rate-limit-banner {
    padding: 8px 14px;
    background: #fff8f0;
    color: #b87a00;
    font-size: 12px;
    text-align: center;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.tfp-rate-limit-banner a {
    color: #E1251B;
    font-weight: 600;
    text-decoration: underline;
}

/* Leave-a-message form */
.tfp-leave-message-form {
    padding: 12px 16px;
    background: #f7f7f7;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
    flex-shrink: 0;
}

.tfp-lm-title {
    color: #333;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 8px;
}

.tfp-lm-input {
    width: 100%;
    padding: 8px 10px;
    margin-bottom: 6px;
    background: #fff;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 8px;
    color: #333;
    font-size: 13px;
    font-family: inherit;
    box-sizing: border-box;
    outline: none;
    transition: border-color 0.15s;
}

.tfp-lm-input:focus {
    border-color: #E1251B;
}

.tfp-lm-input::placeholder {
    color: #999;
}

.tfp-lm-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.tfp-lm-textarea {
    resize: none;
}

.tfp-lm-submit {
    width: 100%;
    padding: 8px;
    background: #E1251B;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.tfp-lm-submit:hover {
    background: #c41f17;
}

.tfp-lm-submit:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.tfp-lm-status {
    font-size: 12px;
    margin-top: 6px;
    text-align: center;
    min-height: 16px;
}

.tfp-lm-status.success {
    color: #2ecc71;
}

.tfp-lm-status.error {
    color: #E1251B;
}

/* ─── Conversation History Panel ─── */
.tfp-history-panel {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #fff;
    z-index: 10;
    display: flex;
    flex-direction: column;
    animation: tfp-slide-in 0.2s ease;
}

@keyframes tfp-slide-in {
    from { opacity: 0; transform: translateX(-12px); }
    to { opacity: 1; transform: translateX(0); }
}

.tfp-history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    flex-shrink: 0;
}

.tfp-history-title {
    font-size: 15px;
    font-weight: 600;
    color: #333;
}

.tfp-history-close {
    background: none;
    border: none;
    cursor: pointer;
    color: #999;
    padding: 4px;
    line-height: 1;
    border-radius: 4px;
    transition: color 0.15s;
}

.tfp-history-close:hover {
    color: #333;
}

.tfp-history-close svg {
    width: 16px;
    height: 16px;
}

.tfp-history-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.tfp-history-empty {
    text-align: center;
    color: #999;
    font-size: 13px;
    padding: 40px 16px;
}

/* Conversation list items */
.tfp-convo-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: background 0.12s, border-color 0.12s;
}

.tfp-convo-item:hover {
    background: #f7f7f7;
}

.tfp-convo-item.active {
    background: rgba(225, 37, 27, 0.04);
    border-left-color: #E1251B;
}

.tfp-convo-info {
    flex: 1;
    min-width: 0;
}

.tfp-convo-title {
    font-size: 13px;
    font-weight: 500;
    color: #333;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tfp-convo-item.active .tfp-convo-title {
    color: #E1251B;
    font-weight: 600;
}

.tfp-convo-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 2px;
}

.tfp-convo-date {
    font-size: 11px;
    color: #999;
}

.tfp-convo-count {
    font-size: 11px;
    color: #bbb;
}

.tfp-convo-delete {
    background: none;
    border: none;
    cursor: pointer;
    color: #ccc;
    padding: 4px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.12s, color 0.12s;
}

.tfp-convo-item:hover .tfp-convo-delete {
    opacity: 1;
}

.tfp-convo-delete:hover {
    color: #E1251B;
}

/* Responsive */
@media (max-width: 480px) {
    #tfp-ai-chat-panel,
    #tfp-ai-chat-panel.expanded {
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
    }

    #tfp-ai-chat-toggle {
        bottom: 16px;
        right: 16px;
    }

    /* Hide expand button on mobile — already fullscreen */
    .tfp-chat-header-btn.tfp-expand-btn {
        display: none;
    }
}

/* Tablets — cap expanded width */
@media (min-width: 481px) and (max-width: 768px) {
    #tfp-ai-chat-panel.expanded {
        width: calc(100vw - 48px);
    }
}
