/* ==========================================================================
   COMPUASIST — Widget de chat con IA
   Estilo alineado con el resto del sitio: mismo verde, Lexend en títulos,
   IBM Plex Sans en texto, mismos radios y sombras.
   ========================================================================== */

.ca-chat {
    /* Tokens propios con respaldo, por si el widget se usa suelto */
    --cc-verde:  var(--ca-green-cta, #2C8130);
    --cc-oscuro: var(--ca-green-dark, #24692A);
    --cc-tinta:  var(--ca-ink, #14181b);
    --cc-tinta2: var(--ca-ink-2, #3d474e);
    --cc-suave:  var(--ca-surface, #f4f6f7);
    --cc-linea:  var(--ca-line, #e2e6e9);
    --cc-radio:  16px;
    --cc-ease:   cubic-bezier(0.22, 0.61, 0.36, 1);

    position: fixed;
    right: 22px;
    bottom: 22px;
    z-index: 1100;          /* por encima del contenido, por debajo del menú (1000+) */
    font-family: 'IBM Plex Sans', system-ui, sans-serif;
}

/* ---------- Burbuja flotante -------------------------------------------- */
.ca-chat-burbuja {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 58px;
    height: 58px;
    padding: 0;
    background: var(--cc-verde);
    color: #fff;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 6px 22px rgba(20, 24, 27, 0.24), 0 2px 6px rgba(20, 24, 27, 0.14);
    transition: transform 0.22s var(--cc-ease), background-color 0.22s var(--cc-ease),
                box-shadow 0.22s var(--cc-ease);
}
.ca-chat-burbuja:hover {
    background: var(--cc-oscuro);
    transform: translateY(-3px) scale(1.04);
    box-shadow: 0 12px 30px rgba(20, 24, 27, 0.28);
}
.ca-chat-burbuja:active { transform: translateY(0) scale(0.98); }
.ca-chat-burbuja svg { width: 26px; height: 26px; }
.ca-chat-burbuja .cc-cerrar { display: none; }
.ca-chat.abierto .ca-chat-burbuja .cc-abrir  { display: none; }
.ca-chat.abierto .ca-chat-burbuja .cc-cerrar { display: block; }

/* Punto de aviso hasta la primera apertura */
.ca-chat-punto {
    position: absolute;
    top: 2px; right: 2px;
    width: 13px; height: 13px;
    background: #ef4444;
    border: 2px solid #fff;
    border-radius: 50%;
    animation: cc-latido 2.4s ease-in-out infinite;
}
.ca-chat.abierto .ca-chat-punto,
.ca-chat.visto  .ca-chat-punto { display: none; }
@keyframes cc-latido {
    0%,100% { transform: scale(1);    opacity: 1; }
    50%     { transform: scale(1.18); opacity: 0.75; }
}

/* ---------- Panel -------------------------------------------------------- */
.ca-chat-panel {
    position: absolute;
    right: 0;
    bottom: 72px;
    display: flex;
    flex-direction: column;
    width: min(384px, calc(100vw - 44px));
    height: min(560px, calc(100dvh - 150px));
    background: #fff;
    border: 1px solid var(--cc-linea);
    border-radius: var(--cc-radio);
    box-shadow: 0 24px 60px rgba(20, 24, 27, 0.22), 0 4px 14px rgba(20, 24, 27, 0.10);
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(14px) scale(0.97);
    transform-origin: bottom right;
    transition: opacity 0.24s var(--cc-ease), transform 0.24s var(--cc-ease), visibility 0.24s;
}
.ca-chat.abierto .ca-chat-panel {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* ---------- Cabecera ----------------------------------------------------- */
.ca-chat-cab {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.95rem 1.05rem;
    background: var(--cc-tinta);
    color: #fff;
    flex: none;
}
.ca-chat-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px; height: 36px;
    flex: none;
    background: var(--cc-verde);
    border-radius: 50%;
}
.ca-chat-avatar svg { width: 19px; height: 19px; }
.ca-chat-cab-txt { min-width: 0; flex: 1; }
.ca-chat-cab-txt strong {
    display: block;
    font-family: 'Lexend', system-ui, sans-serif;
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.01em;
    line-height: 1.25;
}
.ca-chat-cab-txt span {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.62);
}
.ca-chat-cab-txt span::before {
    content: "";
    width: 6px; height: 6px;
    border-radius: 50%;
    background: #4ade80;
}
.ca-chat-cerrar {
    display: flex; align-items: center; justify-content: center;
    width: 34px; height: 34px;
    flex: none;
    padding: 0;
    background: rgba(255, 255, 255, 0.10);
    color: #fff;
    border: none;
    border-radius: 9px;
    cursor: pointer;
    transition: background-color 0.18s var(--cc-ease);
}
.ca-chat-cerrar:hover { background: rgba(255, 255, 255, 0.22); }
.ca-chat-cerrar svg { width: 16px; height: 16px; }

/* ---------- Mensajes ----------------------------------------------------- */
.ca-chat-cuerpo {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 1.05rem;
    background: var(--cc-suave);
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    scrollbar-width: thin;
}
.ca-chat-msg {
    max-width: 86%;
    padding: 0.68rem 0.9rem;
    font-size: 14.5px;
    line-height: 1.55;
    border-radius: 14px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    animation: cc-entra 0.26s var(--cc-ease) both;
}
@keyframes cc-entra { from { opacity: 0; transform: translateY(7px); } }
.ca-chat-msg-bot {
    align-self: flex-start;
    background: #fff;
    color: var(--cc-tinta2);
    border: 1px solid var(--cc-linea);
    border-bottom-left-radius: 5px;
}
/* El Markdown que devuelve el modelo se convierte a nodos reales en
   chat.js (negrita, cursiva, codigo y vinetas). Aqui se les da peso
   visual sin romper el ritmo de la burbuja. */
.ca-chat-msg-bot strong { font-weight: 600; color: var(--cc-tinta); }
.ca-chat-msg-bot em     { font-style: italic; }
.ca-chat-msg-bot code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.92em;
    background: rgba(55, 148, 59, 0.09);
    padding: 0.1em 0.36em;
    border-radius: 4px;
}

.ca-chat-msg-yo {
    align-self: flex-end;
    background: var(--cc-verde);
    color: #fff;
    border-bottom-right-radius: 5px;
}
.ca-chat-msg-error {
    align-self: stretch;
    max-width: 100%;
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
    font-size: 13.5px;
}

/* Sugerencias iniciales */
.ca-chat-sugerencias { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.2rem; }
.ca-chat-sugerencias button {
    padding: 0.45rem 0.8rem;
    font: 500 13px/1.3 'IBM Plex Sans', system-ui, sans-serif;
    color: var(--cc-tinta2);
    background: #fff;
    border: 1px solid var(--cc-linea);
    border-radius: 999px;
    cursor: pointer;
    transition: border-color 0.18s var(--cc-ease), color 0.18s var(--cc-ease), background-color 0.18s var(--cc-ease);
}
.ca-chat-sugerencias button:hover {
    color: var(--cc-verde);
    border-color: var(--cc-verde);
    background: rgba(55, 148, 59, 0.06);
}

/* Indicador de escritura */
.ca-chat-escribiendo {
    align-self: flex-start;
    display: flex;
    gap: 4px;
    padding: 0.8rem 0.95rem;
    background: #fff;
    border: 1px solid var(--cc-linea);
    border-radius: 14px;
    border-bottom-left-radius: 5px;
}
.ca-chat-escribiendo span {
    width: 7px; height: 7px;
    background: #9aa4ab;
    border-radius: 50%;
    animation: cc-punto 1.3s ease-in-out infinite;
}
.ca-chat-escribiendo span:nth-child(2) { animation-delay: 0.18s; }
.ca-chat-escribiendo span:nth-child(3) { animation-delay: 0.36s; }
@keyframes cc-punto {
    0%,60%,100% { transform: translateY(0);    opacity: 0.45; }
    30%         { transform: translateY(-5px); opacity: 1; }
}

/* ---------- Barra de escritura ------------------------------------------ */
.ca-chat-pie {
    flex: none;
    padding: 0.7rem 0.8rem;
    background: #fff;
    border-top: 1px solid var(--cc-linea);
}
.ca-chat-form { display: flex; align-items: flex-end; gap: 0.5rem; }
.ca-chat-form textarea {
    flex: 1;
    min-height: 42px;
    max-height: 110px;
    padding: 0.6rem 0.8rem;
    font: 400 14.5px/1.45 'IBM Plex Sans', system-ui, sans-serif;
    color: var(--cc-tinta);
    background: var(--cc-suave);
    border: 1px solid var(--cc-linea);
    border-radius: 12px;
    resize: none;
    outline: none;
    transition: border-color 0.18s var(--cc-ease), background-color 0.18s var(--cc-ease);
}
.ca-chat-form textarea:focus {
    background: #fff;
    border-color: var(--cc-verde);
    box-shadow: 0 0 0 3px rgba(55, 148, 59, 0.14);
}
.ca-chat-form textarea::placeholder { color: #9aa4ab; }
.ca-chat-enviar {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 44px;
    flex: none;
    padding: 0;
    background: var(--cc-verde);
    color: #fff;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(44, 129, 48, 0.30);
    transition: background-color 0.18s var(--cc-ease), transform 0.18s var(--cc-ease), opacity 0.18s var(--cc-ease);
}
.ca-chat-enviar:hover:not(:disabled) { transform: scale(1.06); }
.ca-chat-enviar:hover:not(:disabled) { background: var(--cc-oscuro); }
.ca-chat-enviar:disabled { opacity: 0.42; cursor: not-allowed; }
.ca-chat-enviar svg { width: 18px; height: 18px; }
.ca-chat-nota {
    margin: 0.5rem 2px 0;
    font-size: 11px;
    line-height: 1.4;
    color: #9aa4ab;
    text-align: center;
}

/* ---------- Accesibilidad y foco ---------------------------------------- */
.ca-chat button:focus-visible,
.ca-chat textarea:focus-visible {
    outline: 2px solid var(--cc-verde);
    outline-offset: 2px;
}
.ca-chat-sr {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Móvil --------------------------------------------------------
   El panel ocupaba el 95% del ancho y el 60% del alto: en un teléfono se
   comía la pantalla. Aquí va más contenido en menos espacio. */
@media all and (max-width: 560px) {
    .ca-chat { right: 14px; bottom: 14px; }

    .ca-chat-panel {
        position: fixed;
        right: 14px;
        left: auto;
        bottom: 74px;
        /* 100vw incluye la barra de desplazamiento en escritorio; con 100%
           del contenedor fijo se evita que el panel se salga por la derecha */
        width: 330px;
        max-width: calc(100vw - 28px);
        box-sizing: border-box;
        height: min(56dvh, 430px);
        border-radius: 14px;
    }

    .ca-chat-burbuja { width: 50px; height: 50px; }
    .ca-chat-burbuja svg { width: 23px; height: 23px; }
    .ca-chat-punto { width: 11px; height: 11px; }

    /* Cabecera más baja */
    .ca-chat-cab { padding: 0.7rem 0.8rem; gap: 0.55rem; }
    .ca-chat-avatar { width: 30px; height: 30px; }
    .ca-chat-avatar svg { width: 16px; height: 16px; }
    .ca-chat-cab-txt strong { font-size: 14px; }
    .ca-chat-cab-txt span { font-size: 11px; }
    .ca-chat-cerrar { width: 30px; height: 30px; border-radius: 8px; }
    .ca-chat-cerrar svg { width: 14px; height: 14px; }

    /* Mensajes más compactos */
    .ca-chat-cuerpo { padding: 0.75rem; gap: 0.5rem; }
    .ca-chat-msg {
        max-width: 90%;
        padding: 0.55rem 0.72rem;
        font-size: 13.5px;
        line-height: 1.5;
        border-radius: 12px;
    }
    .ca-chat-sugerencias { gap: 0.32rem; }
    .ca-chat-sugerencias button { padding: 0.36rem 0.65rem; font-size: 12px; }

    /* Barra de escritura: se conserva el área táctil de 44px */
    .ca-chat-pie { padding: 0.55rem 0.6rem; }
    .ca-chat-form textarea {
        min-height: 44px;
        max-height: 88px;
        padding: 0.55rem 0.7rem;
        font-size: 16px;   /* 16px evita que iOS haga zoom al enfocar */
        border-radius: 11px;
    }
    .ca-chat-enviar { width: 44px; height: 44px; border-radius: 11px; }
    .ca-chat-nota { font-size: 10px; margin-top: 0.4rem; }
}

/* Pantallas muy bajas (teléfono en horizontal): el panel no debe taparlo todo */
@media all and (max-height: 620px) {
    .ca-chat-panel { height: min(74dvh, 360px); }
}

/* Con el menú de navegación abierto, el chat estorba */
.ca-menu-abierto .ca-chat { opacity: 0; visibility: hidden; pointer-events: none; }

@media (prefers-reduced-motion: reduce) {
    .ca-chat-burbuja:hover { transform: none; }
    .ca-chat-punto, .ca-chat-escribiendo span { animation: none; }
    .ca-chat-msg { animation: none; }
}

/* ==========================================================================
   TARJETA DE CAPTURA DENTRO DEL CHAT
   Aparece tras unos intercambios, al pulsar el botón de la cabecera, o
   cuando se agota la cuota — para no perder al prospecto en ningún caso.
   ========================================================================== */
.ca-chat-contacto {
    display: flex; align-items: center; justify-content: center;
    width: 34px; height: 34px;
    flex: none; padding: 0;
    background: rgba(255, 255, 255, 0.10);
    color: #fff;
    border: none; border-radius: 9px;
    cursor: pointer;
    transition: background-color 0.18s var(--cc-ease);
}
.ca-chat-contacto:hover { background: rgba(255, 255, 255, 0.22); }
.ca-chat-contacto svg { width: 17px; height: 17px; }

.ca-chat-ficha {
    align-self: stretch;
    padding: 0.95rem 1rem 1rem;
    background: #fff;
    border: 1px solid var(--cc-linea);
    border-left: 3px solid var(--cc-verde);
    border-radius: 13px;
    animation: cc-entra 0.28s var(--cc-ease) both;
}
.ca-chat-ficha-titulo {
    margin: 0 0 0.2rem !important;
    font-family: 'Lexend', system-ui, sans-serif;
    font-size: 14.5px;
    font-weight: 600;
    color: var(--cc-tinta);
    max-width: none;
}
.ca-chat-ficha-sub {
    margin: 0 0 0.8rem !important;
    font-size: 12.5px;
    line-height: 1.45;
    color: var(--cc-tinta2);
    max-width: none;
}
.ca-chat-ficha-form { display: grid; gap: 0.45rem; }
.ca-chat-ficha-form input {
    width: 100%;
    box-sizing: border-box;
    min-height: 42px;
    padding: 0.55rem 0.7rem;
    font: 400 16px/1.4 'IBM Plex Sans', system-ui, sans-serif;  /* 16px: iOS no hace zoom */
    color: var(--cc-tinta);
    background: var(--cc-suave);
    border: 1px solid var(--cc-linea);
    border-radius: 9px;
    outline: none;
    transition: border-color 0.18s var(--cc-ease), background-color 0.18s var(--cc-ease);
}
.ca-chat-ficha-form input:focus {
    background: #fff;
    border-color: var(--cc-verde);
    box-shadow: 0 0 0 3px rgba(55, 148, 59, 0.14);
}
/* Trampa antispam: fuera de la vista, no display:none */
.ca-chat-trampa {
    position: absolute !important;
    left: -9999px !important;
    width: 1px !important; height: 1px !important;
}
.ca-chat-ficha-error {
    margin: 0 !important;
    min-height: 0;
    font-size: 12.5px;
    line-height: 1.4;
    color: #b91c1c;
    max-width: none;
}
.ca-chat-ficha-error:empty { display: none; }

.ca-chat-ficha-form button[type="submit"] {
    min-height: 44px;
    padding: 0.6rem 1rem;
    font: 500 14px/1.3 'IBM Plex Sans', system-ui, sans-serif;
    color: #fff;
    background: var(--cc-verde);
    border: none; border-radius: 9px;
    cursor: pointer;
    transition: background-color 0.18s var(--cc-ease);
}
.ca-chat-ficha-form button[type="submit"]:hover:not(:disabled) { background: var(--cc-oscuro); }
.ca-chat-ficha-form button[type="submit"]:disabled { opacity: 0.6; cursor: wait; }

.ca-chat-ficha-no {
    padding: 0.4rem;
    font: 400 12.5px/1.3 'IBM Plex Sans', system-ui, sans-serif;
    color: var(--cc-tinta2);
    background: none; border: none;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}
.ca-chat-ficha-no:hover { color: var(--cc-tinta); }

.ca-chat-ficha-ok { display: flex; align-items: flex-start; gap: 0.65rem; }
.ca-chat-ficha-ok svg {
    flex: none; width: 22px; height: 22px;
    margin-top: 1px;
    padding: 3px;
    color: #fff;
    background: var(--cc-verde);
    border-radius: 50%;
    box-sizing: border-box;
}
.ca-chat-ficha-ok p {
    margin: 0 !important;
    font-size: 13.5px;
    line-height: 1.5;
    color: var(--cc-tinta2);
    max-width: none;
}

@media all and (max-width: 560px) {
    .ca-chat-contacto { width: 30px; height: 30px; border-radius: 8px; }
    .ca-chat-contacto svg { width: 15px; height: 15px; }
    .ca-chat-ficha { padding: 0.8rem 0.85rem 0.85rem; }
}
