/* ============================================================
   Chat IA CPT — Chat Widget CSS
   Aplica en frontend y en zona de prueba del backend.
   ============================================================ */

/* ── Variables de tema (se sobreescriben desde PHP con CSS vars) ─────────────── */
:root {
    --cai-primary:     #6366f1;
    --cai-secondary:   #8b5cf6;
    --cai-bubble:      #6366f1;
    --cai-bubble-size: 60px;
    --cai-radius:      16px;
    --cai-chat-w:      360px;
    --cai-chat-h:      520px;
    --cai-white:       #ffffff;
    --cai-gray-50:     #f8fafc;
    --cai-gray-100:    #f1f5f9;
    --cai-gray-200:    #e2e8f0;
    --cai-gray-400:    #94a3b8;
    --cai-gray-700:    #334155;
    --cai-gray-900:    #0f172a;
    --cai-shadow-bubble: 0 8px 32px rgba(99,102,241,.45);
    --cai-shadow-box:    0 16px 48px rgba(0,0,0,.18), 0 4px 12px rgba(0,0,0,.1);
    --cai-t: 280ms cubic-bezier(.4,0,.2,1);
}

/* ── Widget wrapper — solo contenedor lógico, sin posición propia ────────────── */
.chaiacpt-widget {
    display: contents; /* No crea caja de layout; los hijos se posicionan solos */
}

/* ── Burbuja: position fixed SOLO en el frontend, no en el admin ─────────────── */
body:not(.wp-admin) .chaiacpt-bubble {
    position: fixed !important;
    z-index: 999999 !important;
}

.chaiacpt-bubble {
    pointer-events: all;
    width:  var(--cai-bubble-size, 40px);
    height: var(--cai-bubble-size, 40px);
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cai-bubble, #6366f1), var(--cai-secondary, #8b5cf6));
    border: none;
    cursor: pointer;
    box-shadow: 0 8px 32px rgba(99,102,241,.45);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    transition: transform 280ms cubic-bezier(.4,0,.2,1), box-shadow 280ms cubic-bezier(.4,0,.2,1);
}

/* Posición de la burbuja según variante del wrapper */
.chaiacpt-widget--bottom-right .chaiacpt-bubble { bottom: 24px; right: 24px; }
.chaiacpt-widget--bottom-left  .chaiacpt-bubble { bottom: 24px; left:  24px; }
.chaiacpt-widget--top-right    .chaiacpt-bubble { top:    24px; right: 24px; }
.chaiacpt-widget--top-left     .chaiacpt-bubble { top:    24px; left:  24px; }

.chaiacpt-bubble:hover  { transform: scale(1.08); box-shadow: 0 12px 40px rgba(99,102,241,.6); }
.chaiacpt-bubble:active { transform: scale(.96); }

.chaiacpt-bubble__avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.chaiacpt-bubble__icon,
.chaiacpt-bubble__close {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 280ms cubic-bezier(.4,0,.2,1), transform 280ms cubic-bezier(.4,0,.2,1);
}

.chaiacpt-bubble__icon svg,
.chaiacpt-bubble__close svg { width: 50%; height: 50%; stroke: #fff; }

.chaiacpt-bubble__close { opacity: 0; transform: rotate(-90deg); }

.chaiacpt-bubble.is-open .chaiacpt-bubble__icon { opacity: 0; transform: rotate(90deg); }
.chaiacpt-bubble.is-open .chaiacpt-bubble__close { opacity: 1; transform: rotate(0deg); }

/* Pulso de atención */
.chaiacpt-bubble::after {
    content: '';
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    background: var(--cai-bubble, #6366f1);
    opacity: 0;
    animation: cai-pulse 2.5s ease-out 1.5s 3;
}

@keyframes cai-pulse {
    0%   { opacity: .6; transform: scale(1); }
    100% { opacity: 0;  transform: scale(1.7); }
}

/* ── Caja de chat: position fixed SOLO en el frontend ────────────────────────── */
body:not(.wp-admin) .chaiacpt-chat-box {
    position: fixed !important;
    z-index: 999998 !important;
}

.chaiacpt-chat-box {
    pointer-events: none;
    width: 360px;
    max-width: calc(100vw - 32px);
    height: 520px;
    max-height: calc(100vh - 120px);
    background: #ffffff;
    border-radius: var(--cai-radius, 16px);
    box-shadow: 0 16px 48px rgba(0,0,0,.18), 0 4px 12px rgba(0,0,0,.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(16px) scale(.97);
    transition: opacity 280ms cubic-bezier(.4,0,.2,1), transform 280ms cubic-bezier(.4,0,.2,1);
}

/* Posición de la caja según variante — aparece encima de la burbuja (o debajo si top) */
.chaiacpt-widget--bottom-right .chaiacpt-chat-box { bottom: calc(var(--cai-bubble-size, 40px) + 40px); right: 24px; }
.chaiacpt-widget--bottom-left  .chaiacpt-chat-box { bottom: calc(var(--cai-bubble-size, 40px) + 40px); left:  24px; }
.chaiacpt-widget--top-right    .chaiacpt-chat-box { top:    calc(var(--cai-bubble-size, 40px) + 40px); right: 24px; }
.chaiacpt-widget--top-left     .chaiacpt-chat-box { top:    calc(var(--cai-bubble-size, 40px) + 40px); left:  24px; }

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


/* ── Header del chat ─────────────────────────────────────────────────────────── */
.chaiacpt-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: linear-gradient(135deg, var(--cai-primary), var(--cai-secondary));
    color: #fff;
    flex-shrink: 0;
}

.chaiacpt-chat-header__info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chaiacpt-chat-header__avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255,255,255,.4);
}

.chaiacpt-chat-header__icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,.2);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chaiacpt-chat-header__icon svg { width: 18px; height: 18px; stroke: #fff; }

.chaiacpt-chat-header__info strong {
    display: block;
    font-size: 14px;
    font-weight: 700;
    line-height: 1.2;
    color: #fff;
}

.chaiacpt-chat-header__status {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    opacity: .85;
    color: #fff;
}

.chaiacpt-dot-online {
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
    box-shadow: 0 0 0 2px rgba(74,222,128,.3);
}

.chaiacpt-chat-header__close {
    background: rgba(255,255,255,.15);
    border: none;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--cai-t);
    color: #fff;
    padding: 0;
}

.chaiacpt-chat-header__close:hover { background: rgba(255,255,255,.3); }
.chaiacpt-chat-header__close svg { width: 16px; height: 16px; stroke: currentColor; }

/* ── Mensajes ────────────────────────────────────────────────────────────────── */
.chaiacpt-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    background: var(--cai-gray-50);
}

.chaiacpt-chat-messages::-webkit-scrollbar { width: 4px; }
.chaiacpt-chat-messages::-webkit-scrollbar-track { background: transparent; }
.chaiacpt-chat-messages::-webkit-scrollbar-thumb { background: var(--cai-gray-200); border-radius: 4px; }

.chaiacpt-msg { display: flex; flex-direction: column; max-width: 82%; }
.chaiacpt-msg--bot  { align-self: flex-start; align-items: flex-start; }
.chaiacpt-msg--user { align-self: flex-end;   align-items: flex-end; }

.chaiacpt-msg__bubble {
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 13.5px;
    line-height: 1.5;
    word-break: break-word;
}

.chaiacpt-msg--bot .chaiacpt-msg__bubble {
    background: var(--cai-white);
    color: var(--cai-gray-700);
    border-radius: 4px 16px 16px 16px;
    box-shadow: 0 1px 4px rgba(0,0,0,.07);
}

.chaiacpt-msg--user .chaiacpt-msg__bubble {
    background: linear-gradient(135deg, var(--cai-primary), var(--cai-secondary));
    color: #fff;
    border-radius: 16px 16px 4px 16px;
}

.chaiacpt-msg__time {
    font-size: 10px;
    color: var(--cai-gray-400);
    margin-top: 3px;
    padding: 0 4px;
}

/* Typing indicator */
.chaiacpt-typing {
    display: none;
    padding: 6px 16px 2px;
    gap: 4px;
    align-items: center;
    flex-shrink: 0;
}

.chaiacpt-typing.is-visible { display: flex; }

.chaiacpt-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--cai-gray-400);
    animation: cai-typing .9s ease-in-out infinite;
}

.chaiacpt-typing span:nth-child(2) { animation-delay: .15s; }
.chaiacpt-typing span:nth-child(3) { animation-delay: .30s; }

@keyframes cai-typing {
    0%,60%,100% { transform: translateY(0);    opacity: .4; }
    30%          { transform: translateY(-6px); opacity: 1;  }
}

/* ── Input ───────────────────────────────────────────────────────────────────── */
.chaiacpt-chat-input {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 14px;
    background: var(--cai-white);
    border-top: 1px solid var(--cai-gray-100);
    flex-shrink: 0;
}

.chaiacpt-chat-input__field {
    flex: 1;
    border: 1.5px solid var(--cai-gray-200);
    border-radius: 10px;
    padding: 9px 12px;
    font-size: 13px;
    font-family: inherit;
    resize: none;
    max-height: 100px;
    line-height: 1.5;
    color: var(--cai-gray-700);
    background: var(--cai-gray-50);
    transition: border-color var(--cai-t);
    overflow-y: auto;
}

.chaiacpt-chat-input__field:focus {
    outline: none;
    border-color: var(--cai-primary);
    background: var(--cai-white);
}

.chaiacpt-chat-input__field::placeholder { color: var(--cai-gray-400); }

.chaiacpt-chat-input__send {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--cai-primary), var(--cai-secondary));
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity var(--cai-t), transform var(--cai-t);
}

.chaiacpt-chat-input__send:hover { opacity: .88; }
.chaiacpt-chat-input__send:active { transform: scale(.94); }
.chaiacpt-chat-input__send svg { width: 16px; height: 16px; stroke: #fff; }

/* ── Footer ──────────────────────────────────────────────────────────────────── */
.chaiacpt-chat-footer {
    padding: 6px 14px;
    text-align: center;
    font-size: 10px;
    color: var(--cai-gray-400);
    background: var(--cai-white);
    border-top: 1px solid var(--cai-gray-100);
    flex-shrink: 0;
}

.chaiacpt-chat-footer strong { color: var(--cai-gray-700); }

/* ── Zona de prueba del backend ──────────────────────────────────────────────── */
.chaiacpt-test-container {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 24px;
    margin-bottom: 24px;
}

@media (max-width: 900px) {
    .chaiacpt-test-container { grid-template-columns: 1fr; }
}

.chaiacpt-test-screen {
    background: #fff;
    border: 1px solid var(--cai-gray-200);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 24px rgba(0,0,0,.06);
    min-height: 560px;
}

.chaiacpt-test-screen__header {
    background: var(--cai-gray-100);
    padding: 10px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid var(--cai-gray-200);
}

.chaiacpt-test-screen__dots { display: flex; gap: 5px; }

.chaiacpt-test-screen__dots span {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--cai-gray-300, #cbd5e1);
}

.chaiacpt-test-screen__dots span:nth-child(1) { background: #fc635d; }
.chaiacpt-test-screen__dots span:nth-child(2) { background: #fdbc40; }
.chaiacpt-test-screen__dots span:nth-child(3) { background: #34c749; }

.chaiacpt-test-screen__url {
    font-size: 11.5px;
    color: var(--cai-gray-400);
    background: var(--cai-white);
    border: 1px solid var(--cai-gray-200);
    border-radius: 6px;
    padding: 3px 10px;
    flex: 1;
    text-align: center;
}

.chaiacpt-test-screen__body {
    position: relative;
    min-height: 500px;
    padding: 24px;
    overflow: visible;
    display: flex;
    align-items: flex-start;
    gap: 24px;
    isolation: isolate;
}

.chaiacpt-test-page-dummy {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px 0;
}

.chaiacpt-test-dummy-line {
    height: 12px;
    background: var(--cai-gray-100);
    border-radius: 4px;
}

.chaiacpt-test-dummy-block {
    height: 120px;
    background: var(--cai-gray-100);
    border-radius: 8px;
    margin: 8px 0;
}

/* En el test, la burbuja y caja no usan position:fixed sino position:absolute dentro del contenedor */
.chaiacpt-test-widget-wrap {
    position: absolute;
    inset: 0;
    overflow: hidden;
}

/* Forzar position:absolute en vez de fixed dentro del test */
.chaiacpt-test-widget-wrap .chaiacpt-bubble {
    position: absolute !important;
}
.chaiacpt-test-widget-wrap .chaiacpt-chat-box {
    position: absolute !important;
    max-height: 420px;
    max-width: 320px;
}

.chaiacpt-test-widget-wrap .chaiacpt-widget--bottom-right .chaiacpt-bubble { bottom: 16px; right: 16px; }
.chaiacpt-test-widget-wrap .chaiacpt-widget--bottom-left  .chaiacpt-bubble { bottom: 16px; left:  16px; }
.chaiacpt-test-widget-wrap .chaiacpt-widget--top-right    .chaiacpt-bubble { top: 16px;    right: 16px; }
.chaiacpt-test-widget-wrap .chaiacpt-widget--top-left     .chaiacpt-bubble { top: 16px;    left:  16px; }

.chaiacpt-test-widget-wrap .chaiacpt-widget--bottom-right .chaiacpt-chat-box { bottom: calc(var(--cai-bubble-size, 40px) + 28px); right: 16px; }
.chaiacpt-test-widget-wrap .chaiacpt-widget--bottom-left  .chaiacpt-chat-box { bottom: calc(var(--cai-bubble-size, 40px) + 28px); left:  16px; }
.chaiacpt-test-widget-wrap .chaiacpt-widget--top-right    .chaiacpt-chat-box { top:    calc(var(--cai-bubble-size, 40px) + 28px); right: 16px; }
.chaiacpt-test-widget-wrap .chaiacpt-widget--top-left     .chaiacpt-chat-box { top:    calc(var(--cai-bubble-size, 40px) + 28px); left:  16px; }

/* Panel info */
.chaiacpt-test-info {
    background: var(--cai-white);
    border: 1px solid var(--cai-gray-200);
    border-radius: 12px;
    padding: 20px;
    align-self: start;
}

.chaiacpt-test-info h3 {
    font-size: 14px;
    font-weight: 700;
    color: var(--cai-gray-900);
    margin: 0 0 14px;
}

.chaiacpt-test-steps {
    padding-left: 18px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin: 0 0 16px;
}

.chaiacpt-test-steps li { font-size: 13px; color: var(--cai-gray-700); line-height: 1.5; }
.chaiacpt-test-steps li strong { color: var(--cai-gray-900); }
.chaiacpt-test-steps li a { color: var(--cai-primary); text-decoration: none; }
.chaiacpt-test-steps li a:hover { text-decoration: underline; }
.chaiacpt-test-steps kbd { background: var(--cai-gray-100); border: 1px solid var(--cai-gray-200); border-radius: 4px; padding: 1px 5px; font-size: 11px; }

.chaiacpt-test-note {
    font-size: 12px;
    color: var(--cai-gray-400);
    background: var(--cai-gray-50);
    border-radius: 6px;
    padding: 8px 10px;
    margin: 0;
}

/* ── Mobile ──────────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
    .chaiacpt-chat-box {
        --cai-chat-w: calc(100vw - 20px);
        --cai-chat-h: 70vh;
    }

    .chaiacpt-widget--bottom-right,
    .chaiacpt-widget--bottom-left { bottom: 12px; right: 12px; left: auto; }
}

/* ── Widget en zona de prueba del backend ────────────────────── */
.chaiacpt-test-widget-area {
    position: absolute;
    display: flex;
    flex-direction: column;
    gap: 12px;
    pointer-events: all;
}

.chaiacpt-test-widget-area.chaiacpt-widget--bottom-right { bottom: 16px; right: 16px; align-items: flex-end; }
.chaiacpt-test-widget-area.chaiacpt-widget--bottom-left  { bottom: 16px; left:  16px; align-items: flex-start; }
.chaiacpt-test-widget-area.chaiacpt-widget--top-right    { top: 16px;    right: 16px; align-items: flex-end;   flex-direction: column-reverse; }
.chaiacpt-test-widget-area.chaiacpt-widget--top-left     { top: 16px;    left:  16px; align-items: flex-start; flex-direction: column-reverse; }

.chaiacpt-test-widget-area .chaiacpt-bubble { position: relative !important; }
.chaiacpt-test-widget-area .chaiacpt-chat-box {
    position: relative !important;
    max-height: 380px;
    max-width: 300px;
    width: 300px;
}

/* ── Chat inline en zona de prueba ──────────────────────────── */
.chaiacpt-test-chat-preview {
    flex-shrink: 0;
}

.chaiacpt-test-widget-inline {
    display: flex !important;  /* Sobrescribe display:contents del .chaiacpt-widget base */
    flex-direction: column;
    gap: 10px;
    align-items: flex-end;
}

.chaiacpt-test-widget-inline .chaiacpt-chat-box {
    position: relative !important;
    width: 300px;
    height: 400px;
    max-height: none;
    opacity: 1 !important;
    transform: none !important;
    pointer-events: all !important;
}

.chaiacpt-test-widget-inline .chaiacpt-chat-box,
.chaiacpt-test-widget-inline .chaiacpt-bubble {
    z-index: 1 !important;
    position: relative !important;
}

.chaiacpt-bubble--preview {
    cursor: default;
}

/* ── Formulario de contacto dentro del chat ────────────────── */
.chaiacpt-contact-form {
    background: #f8fafc;
    border: 1.5px solid #e2e8f0;
    border-radius: 12px;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 220px;
}

.chaiacpt-contact-form__intro {
    font-size: 13px;
    color: #475569;
    margin: 0;
    line-height: 1.4;
}

.chaiacpt-contact-form__fields {
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.chaiacpt-contact-input {
    width: 100%;
    box-sizing: border-box;
    padding: 8px 10px;
    border: 1.5px solid #cbd5e1;
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    color: #1e293b;
    background: #fff;
    outline: none;
    transition: border-color .2s;
    resize: vertical;
}

.chaiacpt-contact-input:focus {
    border-color: var(--cai-indigo, #6366f1);
}

.chaiacpt-contact-submit {
    width: 100%;
    padding: 9px;
    background: var(--cai-primary, #6366f1);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    transition: opacity .2s;
}

.chaiacpt-contact-submit:hover   { opacity: .88; }
.chaiacpt-contact-submit:disabled { opacity: .55; cursor: not-allowed; }

.chaiacpt-contact-form__error {
    font-size: 12px;
    color: #dc2626;
    margin: 0;
}

.chaiacpt-contact-form__success {
    font-size: 13px;
    color: #166534;
    background: #dcfce7;
    border-radius: 8px;
    padding: 10px 12px;
    margin: 0;
    line-height: 1.4;
}

/* ── Formulario de contacto dentro del chat ──────────────────── */
.chaiacpt-contact-form {
    padding: 4px 0;
    width: 100%;
}

.chaiacpt-contact-form__intro {
    font-size: 13px;
    color: var(--cai-gray-600, #475569);
    margin: 0 0 12px;
    line-height: 1.5;
}

.chaiacpt-contact-form__fields {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 12px;
}

.chaiacpt-contact-input {
    width: 100%;
    padding: 9px 12px;
    border: 1.5px solid #e2e8f0;
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    color: #1e293b;
    background: #fff;
    outline: none;
    box-sizing: border-box;
    transition: border-color 200ms;
    resize: none;
}

.chaiacpt-contact-input:focus {
    border-color: var(--cai-primary, #6366f1);
    box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}

.chaiacpt-contact-input::placeholder { color: #94a3b8; }

.chaiacpt-contact-submit {
    width: 100%;
    padding: 10px;
    background: var(--cai-primary, #6366f1);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    transition: background 200ms, transform 150ms;
}

.chaiacpt-contact-submit:hover { background: var(--cai-secondary, #8b5cf6); }
.chaiacpt-contact-submit:active { transform: scale(.98); }
.chaiacpt-contact-submit:disabled { opacity: .6; cursor: not-allowed; }

.chaiacpt-contact-form__error {
    font-size: 12px;
    color: #dc2626;
    margin: 8px 0 0;
    padding: 7px 10px;
    background: #fef2f2;
    border-radius: 6px;
}

.chaiacpt-contact-form__success {
    font-size: 13px;
    color: #16a34a;
    padding: 14px;
    background: #f0fdf4;
    border-radius: 8px;
    text-align: center;
    line-height: 1.5;
    margin: 0;
}

/* El formulario ocupa todo el ancho del bubble del bot */
.chaiacpt-msg--bot .chaiacpt-msg__bubble:has(.chaiacpt-contact-form) {
    width: 100%;
    max-width: 100%;
    background: #f8fafc;
    border: 1.5px solid #e2e8f0;
}
