/* Global Chat Widget Styles */
.chat-widget {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    z-index: 1000;
    font-family: inherit;
}

/* Strict icon constraints to prevent giant SVG rendering */
.chat-widget svg,
.chat-toggle svg,
.chat-panel svg,
.chat-header svg,
.chat-header-info svg {
    max-width: 24px !important;
    max-height: 24px !important;
}

.chat-header-info svg {
    width: 18px !important;
    height: 18px !important;
    min-width: 18px !important;
    min-height: 18px !important;
    flex-shrink: 0 !important;
    display: inline-block !important;
    vertical-align: middle !important;
}

.header-chat-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid var(--border, rgba(255, 255, 255, 0.1));
    color: var(--text, #f1f5f9);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    position: relative;
}
.header-chat-btn:hover {
    background: rgba(139, 92, 246, 0.15);
    border-color: rgba(139, 92, 246, 0.4);
    color: var(--accent, #8b5cf6);
    transform: translateY(-1px);
}
.header-chat-btn svg {
    width: 18px !important;
    height: 18px !important;
    flex-shrink: 0;
}

.chat-toggle {
    width: 54px;
    height: 54px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent, #8b5cf6), var(--accent2, #d946ef));
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(139,92,246,0.3);
    transition: transform 0.2s, box-shadow 0.2s;
    color: white;
    position: relative;
}

/* Gentle idle pulse ring on the floating button */
.chat-toggle::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid rgba(139,92,246,0.55);
    animation: chatRingPulse 2.6s ease-out infinite;
    pointer-events: none;
}
@keyframes chatRingPulse {
    0% { transform: scale(1); opacity: 0.7; }
    70% { transform: scale(1.55); opacity: 0; }
    100% { transform: scale(1.55); opacity: 0; }
}

/* Squash-and-stretch pop when opening/closing */
.chat-pop {
    animation: chatTogglePop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) !important;
}
@keyframes chatTogglePop {
    0% { transform: scale(1) rotate(0); }
    35% { transform: scale(0.82) rotate(-6deg); }
    65% { transform: scale(1.12) rotate(3deg); }
    100% { transform: scale(1) rotate(0); }
}

/* Sparkle burst emitted from the floating button */
.chat-sparkles {
    position: absolute;
    bottom: 27px;
    right: 27px;
    width: 0;
    height: 0;
    pointer-events: none;
    z-index: 1001;
}
.chat-sparkle {
    position: absolute;
    left: 0;
    top: 0;
    width: 8px;
    height: 8px;
    border-radius: 3px;
    opacity: 0;
    animation: chatSparkleFly 0.75s cubic-bezier(0.22, 0.9, 0.28, 1) forwards;
}
@keyframes chatSparkleFly {
    0% { transform: translate(0, 0) scale(1.1) rotate(0deg); opacity: 1; }
    100% { transform: translate(var(--dx, 0), var(--dy, -70px)) scale(0.15) rotate(var(--rot, 240deg)); opacity: 0; }
}

.chat-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 28px rgba(139,92,246,0.4);
}

.chat-toggle:focus-visible {
    outline: 2px solid var(--accent, #8b5cf6);
    outline-offset: 2px;
}

.chat-toggle svg {
    width: 20px !important;
    height: 20px !important;
    min-width: 20px !important;
    min-height: 20px !important;
}

.chat-panel {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 380px;
    max-width: calc(100vw - 2rem);
    height: 500px;
    max-height: 70vh;
    background: var(--bg, #0f172a);
    border: 1px solid var(--border, #334155);
    border-radius: 1rem;
    box-shadow: 0 20px 50px rgba(0,0,0,0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform-origin: bottom right;
    transform: translateY(28px) scale(0.8) rotate(-2deg);
    transition: opacity 0.3s ease, visibility 0.3s, transform 0.5s cubic-bezier(0.34, 1.4, 0.64, 1);
}

.chat-panel.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1) rotate(0);
    animation: chatPanelSpringIn 0.55s cubic-bezier(0.34, 1.45, 0.64, 1) both;
}

.chat-panel.closing {
    pointer-events: none;
    animation: chatPanelSpringOut 0.28s cubic-bezier(0.55, 0, 0.55, 0.2) both;
}

/* Springy "bubble grows from the button" entrance */
@keyframes chatPanelSpringIn {
    0%   { opacity: 0; transform: translateY(36px) scale(0.72) rotate(-3deg); border-radius: 3rem; }
    55%  { opacity: 1; transform: translateY(-7px) scale(1.03) rotate(0.7deg); }
    78%  { transform: translateY(3px) scale(0.99) rotate(-0.3deg); }
    100% { opacity: 1; transform: translateY(0) scale(1) rotate(0); border-radius: 1rem; }
}

@keyframes chatPanelSpringOut {
    from { opacity: 1; transform: translateY(0) scale(1) rotate(0); }
    to   { opacity: 0; transform: translateY(26px) scale(0.85) rotate(2deg); }
}

/* Staggered cascade of the panel regions */
.chat-panel.open .chat-header { animation: chatPanelItemUp 0.45s 0.1s both; }
.chat-panel.open .chat-messages { animation: chatPanelItemUp 0.45s 0.18s both; }
.chat-panel.open .chat-input-area { animation: chatPanelItemUp 0.45s 0.26s both; }
@keyframes chatPanelItemUp {
    from { opacity: 0; transform: translateY(16px); }
    to   { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
    .chat-toggle::after,
    .chat-pop,
    .chat-sparkle,
    .chat-panel.open,
    .chat-panel.closing,
    .chat-panel.open .chat-header,
    .chat-panel.open .chat-messages,
    .chat-panel.open .chat-input-area {
        animation: none !important;
    }
}

.chat-header {
    padding: 1rem 1.25rem;
    background: var(--bg2, #1e293b);
    border-bottom: 1px solid var(--border, #334155);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chat-header h3 {
    font-size: 1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
}

.chat-header h3::before {
    content: '💬';
    font-size: 1.2rem;
}

.chat-unread-badge {
    background: var(--danger, #ef4444);
    color: white;
    font-size: 0.65rem;
    font-weight: 700;
    padding: 0.1rem 0.4rem;
    border-radius: 999px;
    min-width: 1.2rem;
    text-align: center;
}

.chat-close {
    background: none;
    border: none;
    color: var(--muted, #94a3b8);
    cursor: pointer;
    padding: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.5rem;
    transition: color 0.15s, background 0.15s;
}

.chat-close:hover {
    color: var(--text, #f1f5f9);
    background: var(--bg3, #0d1526);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.chat-message {
    display: flex;
    gap: 0.75rem;
    animation: chatFadeInUp 0.3s ease;
}

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

.chat-message.ai {
    flex-direction: row-reverse;
}

.chat-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--h-grad, linear-gradient(135deg, #8b5cf6, #d946ef));
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 0.75rem;
    flex-shrink: 0;
}

.chat-message.ai .chat-avatar {
    background: linear-gradient(135deg, #10b981, #14b8a6);
}

.chat-bubble {
    max-width: 75%;
    background: var(--bg2, #1e293b);
    border: 1px solid var(--border, #334155);
    border-radius: 1rem;
    padding: 0.75rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.chat-message.ai .chat-bubble {
    background: rgba(16,185,129,0.1);
    border-color: rgba(16,185,129,0.2);
}

.chat-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.65rem;
    color: var(--muted, #94a3b8);
    font-weight: 600;
}

.chat-meta .ai-tag {
    background: rgba(16,185,129,0.15);
    color: #10b981;
    padding: 0.1rem 0.4rem;
    border-radius: 0.25rem;
    font-size: 0.55rem;
    font-weight: 700;
    text-transform: uppercase;
}

.chat-flag {
    font-size: 1rem;
}

/* Mensajes escritos por la IA: se marcan para no hacerlos pasar por personas reales */
.chat-message.bot .chat-avatar {
    background: linear-gradient(135deg, #10b981, #06b6d4);
}
.chat-message.bot .chat-bubble {
    background: rgba(16, 185, 129, 0.08);
    border-color: rgba(16, 185, 129, 0.25);
}

.chat-notice {
    font-size: 0.72rem;
    line-height: 1.4;
    color: var(--muted, #94a3b8);
    background: rgba(139, 92, 246, 0.08);
    border: 1px solid rgba(139, 92, 246, 0.25);
    border-radius: 0.5rem;
    padding: 0.5rem 0.65rem;
    margin-bottom: 0.5rem;
}
.chat-notice a {
    color: var(--accent, #8b5cf6);
    font-weight: 700;
}
.chat-notice.error {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.08);
    border-color: rgba(239, 68, 68, 0.3);
}

.chat-body {
    font-size: 0.85rem;
    line-height: 1.5;
    color: var(--text, #f1f5f9);
    white-space: pre-wrap;
    word-wrap: break-word;
}

.chat-time {
    font-size: 0.6rem;
    color: var(--muted, #94a3b8);
    align-self: flex-end;
    margin-top: 0.25rem;
}

.chat-input-area {
    padding: 1rem;
    border-top: 1px solid var(--border, #334155);
    background: var(--bg2, #1e293b);
    flex-shrink: 0;
}

.chat-input-form {
    display: flex;
    gap: 0.5rem;
}

.chat-input-form select {
    padding: 0.5rem 0.75rem;
    background: var(--bg3, #0d1526);
    border: 1px solid var(--border, #334155);
    border-radius: 0.5rem;
    color: var(--text, #f1f5f9);
    font-size: 0.8rem;
    outline: none;
    flex-shrink: 0;
    min-width: 100px;
}

.chat-input-form select:focus {
    border-color: var(--accent, #8b5cf6);
}

.chat-input-form input {
    flex: 1;
    padding: 0.6rem 1rem;
    background: var(--bg3, #0d1526);
    border: 1px solid var(--border, #334155);
    border-radius: 0.5rem;
    color: var(--text, #f1f5f9);
    font-size: 0.85rem;
    outline: none;
    min-width: 0;
}

.chat-input-form input:focus {
    border-color: var(--accent, #8b5cf6);
}

.chat-input-form button {
    padding: 0.6rem 1rem;
    background: linear-gradient(135deg, var(--accent, #8b5cf6), var(--accent2, #d946ef));
    color: white;
    border: none;
    border-radius: 0.5rem;
    font-weight: 700;
    font-size: 0.8rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    flex-shrink: 0;
    transition: transform 0.15s, box-shadow 0.15s;
}

.chat-input-form button:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 12px rgba(16,185,129,0.3);
}

.chat-input-form button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.chat-auth-prompt {
    padding: 1.5rem;
    text-align: center;
    color: var(--muted, #94a3b8);
}

.chat-auth-prompt a {
    color: var(--accent, #8b5cf6);
    font-weight: 700;
    text-decoration: underline;
}

.chat-toast {
    position: fixed;
    bottom: 8rem;
    right: 2rem;
    z-index: 1100;
    animation: chatSlideInRight 0.3s ease;
}

@keyframes chatSlideInRight {
    from { opacity: 0; transform: translateX(100%); }
    to { opacity: 1; transform: translateX(0); }
}

.chat-toast {
    background: var(--bg2, #1e293b);
    border: 1px solid var(--border, #334155);
    border-radius: 0.75rem;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}

.chat-toast.success {
    border-color: var(--accent, #8b5cf6);
}

.chat-toast.error {
    border-color: var(--danger, #ef4444);
}

.chat-toast button {
    background: none;
    border: none;
    color: var(--muted, #94a3b8);
    cursor: pointer;
    padding: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
