/* ================================================================
   Conchita Chat Widget — CSS aislado
   ----------------------------------------------------------------
   · Todos los selectores están prefijados con #cw-chat-widget.
   · Todos los keyframes llevan prefijo cw.
   · Todas las medidas en px (no rem) para no verse afectadas
     por un posible `html { font-size: 62.5% }` del host.
   · Las variables CSS viven en #cw-chat-widget, no en :root.
   ================================================================ */

/* ---- Contenedor raíz: variables + reset local ---- */
#cw-chat-widget {
    --brand-orange: #f39200;
    --brand-green: #2b9f4a;
    --text-dark: #1c1c1c;
    --text-light: #666;
    --bg-light: #f9f9fb;
    --border-light: #e5e5e5;
    --bot-bg: #ffffff;
    --user-bg: #f0f7ff;

    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 2147483000; /* por encima de la mayoría de widgets del host */

    display: flex;
    flex-direction: column;
    align-items: flex-end;

    /* Fijamos lo que normalmente heredaría del body del host */
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 16px;
    font-weight: 400;
    font-style: normal;
    line-height: 1.45;
    letter-spacing: normal;
    word-spacing: normal;
    text-align: left;
    text-transform: none;
    color: var(--text-dark);
    -webkit-font-smoothing: antialiased;
}

/* Reset local: solo dentro del widget */
#cw-chat-widget *,
#cw-chat-widget *::before,
#cw-chat-widget *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Controles: no heredan fuente por defecto en algunos navegadores */
#cw-chat-widget button,
#cw-chat-widget input,
#cw-chat-widget textarea,
#cw-chat-widget select {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

/* ---- Bocadillo de bienvenida ---- */
#cw-chat-widget #cw-welcome-bubble {
    background: #fff;
    border: 1px solid var(--border-light);
    padding: 10px 16px;
    border-radius: 16px 16px 4px 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    font-size: 16px;
    color: var(--text-dark);
    margin-bottom: 16px;
    position: relative;
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 0.4s, transform 0.4s;
    pointer-events: none;
    cursor: pointer;
    max-width: 240px;
}
#cw-chat-widget #cw-welcome-bubble.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
#cw-chat-widget #cw-welcome-bubble::after {
    content: '';
    position: absolute;
    bottom: -6px;
    right: 24px;
    width: 12px;
    height: 12px;
    background: #fff;
    border-right: 1px solid var(--border-light);
    border-bottom: 1px solid var(--border-light);
    transform: rotate(45deg);
}

/* ---- Botón flotante (Conchita) con anillo giratorio ---- */
#cw-chat-widget #cw-chat-toggle {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    padding: 0;
}
#cw-chat-widget #cw-chat-toggle::before {
    content: '';
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    background: conic-gradient(
        var(--brand-orange) 0%,
        var(--brand-green) 33%,
        var(--brand-orange) 66%,
        var(--brand-orange) 100%
    );
    z-index: -1;
    animation: cwSpinRing 3s linear infinite;
}
@keyframes cwSpinRing {
    0%   { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}
#cw-chat-widget #cw-chat-toggle:hover {
    transform: scale(1.05);
}
#cw-chat-widget #cw-chat-toggle img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    border: 2px solid #fff;
    background: #fff;
    display: block;
}
#cw-chat-widget #cw-chat-toggle::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 16px;
    height: 16px;
    background: var(--brand-green);
    border: 2px solid #fff;
    border-radius: 50%;
    z-index: 1;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* ---- Ventana del chat ---- */
#cw-chat-widget #cw-chat-window {
    position: absolute;
    bottom: 84px;
    right: 0;
    width: 380px;
    height: 600px;
    max-height: calc(100vh - 120px);
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: opacity 0.3s, transform 0.3s;
    border: 1px solid var(--border-light);
}
#cw-chat-widget #cw-chat-window.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ---- Header ---- */
#cw-chat-widget .cw-chat-header {
    background: #fff;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-light);
    border-top: 4px solid var(--brand-orange);
    flex-shrink: 0;
}
#cw-chat-widget .cw-chat-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
}
#cw-chat-widget .cw-chat-header-info img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
}
#cw-chat-widget .cw-chat-header-info h2 {
    font-size: 19px;
    margin: 0;
    color: var(--text-dark);
    font-weight: 600;
    line-height: 1.2;
}
#cw-chat-widget .cw-chat-header-info p {
    font-size: 12px;
    color: var(--brand-green);
    margin: 0;
    font-weight: 500;
    line-height: 1.2;
}
#cw-chat-widget .cw-chat-header-actions {
    display: flex;
    gap: 8px;
}
#cw-chat-widget .cw-btn-icon {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-light);
    padding: 4px;
    border-radius: 4px;
    transition: background 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}
#cw-chat-widget .cw-btn-icon:hover {
    background: var(--bg-light);
    color: var(--text-dark);
}
#cw-chat-widget .cw-btn-icon svg {
    width: 18px;
    height: 18px;
    display: block;
}

/* ---- Área de mensajes ---- */
#cw-chat-widget #cw-chat-messages {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    background: var(--bg-light);
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    min-height: 0; /* importante para flex + overflow */
    overscroll-behavior: contain; /* evita scroll-chaining al host */
}

#cw-chat-widget .cw-msg {
    padding: 10px 14px;
    border-radius: 12px;
    max-width: 85%;
    font-size: 16px;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    animation: cwMsgIn 0.3s ease-out;
}
@keyframes cwMsgIn {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}
#cw-chat-widget .cw-msg.user {
    background: var(--user-bg);
    color: var(--text-dark);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}
#cw-chat-widget .cw-msg.bot {
    background: var(--bot-bg);
    color: var(--text-dark);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    border: 1px solid var(--border-light);
}

/* Markdown dentro de las burbujas */
#cw-chat-widget .cw-msg p { margin: 4px 0; }
#cw-chat-widget .cw-msg ul {
    margin: 6px 0 6px 20px;
    padding: 0;
    list-style: disc;
}
#cw-chat-widget .cw-msg li { margin-bottom: 2px; }
#cw-chat-widget .cw-msg a {
    color: var(--brand-orange);
    text-decoration: underline;
}
#cw-chat-widget .cw-msg a:hover { color: #d47d00; }
#cw-chat-widget .cw-msg strong { font-weight: 600; }

/* ---- Indicador "Escribiendo…" ---- */
#cw-chat-widget #cw-typing-indicator {
    display: none;
    align-self: flex-start;
    background: var(--bot-bg);
    border: 1px solid var(--border-light);
    padding: 12px 16px;
    border-radius: 12px;
    border-bottom-left-radius: 4px;
    width: fit-content;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
#cw-chat-widget #cw-typing-indicator.show {
    display: flex;
    gap: 4px;
}
#cw-chat-widget #cw-typing-indicator .cw-dot {
    width: 6px;
    height: 6px;
    background: var(--brand-orange);
    border-radius: 50%;
    animation: cwTypingBounce 1.4s infinite ease-in-out both;
}
#cw-chat-widget #cw-typing-indicator .cw-dot:nth-child(1) { animation-delay: -0.32s; }
#cw-chat-widget #cw-typing-indicator .cw-dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes cwTypingBounce {
    0%, 80%, 100% { transform: scale(0); }
    40%           { transform: scale(1); }
}

/* ---- Tarjetas de cursos ---- */
#cw-chat-widget .cw-cursos-wrapper {
    margin-top: 4px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
}
#cw-chat-widget .cw-cursos-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}
#cw-chat-widget .cw-curso-card {
    border: 1px solid var(--border-light);
    border-radius: 10px;
    padding: 10px;
    background: #fff;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
#cw-chat-widget .cw-curso-card h3 {
    font-size: 15px;
    margin: 0;
    line-height: 1.3;
    color: var(--text-dark);
    font-weight: 600;
}
#cw-chat-widget .cw-curso-meta {
    font-size: 14px;
    color: var(--text-light);
    line-height: 1.4;
}
#cw-chat-widget .cw-badge-bare {
    display: inline-block;
    font-size: 12px;
    padding: 2px 6px;
    border-radius: 4px;
    margin-top: 4px;
    font-weight: 600;
    background: #e6f7e6;
    color: var(--brand-green);
}
#cw-chat-widget .cw-btn-curso {
    display: block;
    margin-top: 4px;
    padding: 6px 10px;
    background: var(--brand-orange);
    color: #fff;
    text-decoration: none;
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
    font-weight: 500;
}
#cw-chat-widget .cw-btn-curso:hover { background: #d47d00; }
#cw-chat-widget .cw-btn-cat {
    display: inline-block;
    padding: 6px 10px;
    background: var(--bg-light);
    color: var(--text-dark);
    text-decoration: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    border: 1px solid var(--border-light);
    text-align: center;
}

/* ---- Ficha detalle curso / pack ---- */
#cw-chat-widget .cw-detalle-curso {
    background: #fff;
    border: 1px solid var(--border-light);
    border-radius: 10px;
    padding: 12px;
    margin-top: 4px;
    width: 100%;
}
#cw-chat-widget .cw-detalle-curso h2 {
    font-size: 17px;
    margin: 0 0 8px;
    color: var(--text-dark);
    font-weight: 600;
    line-height: 1.3;
}
#cw-chat-widget .cw-detalle-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin: 8px 0;
}
#cw-chat-widget .cw-detalle-item {
    background: var(--bg-light);
    padding: 6px 8px;
    border-radius: 6px;
    display: flex;
    flex-direction: column;
}
#cw-chat-widget .cw-detalle-label {
    font-size: 12px;
    text-transform: uppercase;
    color: var(--text-light);
    font-weight: 600;
    letter-spacing: 0.02em;
}
#cw-chat-widget .cw-detalle-value {
    font-size: 14px;
    color: var(--text-dark);
    font-weight: 500;
    margin-top: 2px;
}
#cw-chat-widget .cw-detalle-desc {
    font-size: 14px;
    color: var(--text-dark);
    line-height: 1.4;
    margin: 8px 0;
}

/* ---- Área de input ---- */
#cw-chat-widget #cw-form-chat {
    background: #fff;
    padding: 12px;
    border-top: 1px solid var(--border-light);
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}
#cw-chat-widget #cw-input-chat {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border-light);
    border-radius: 20px;
    font-size: 16px;
    outline: none;
    transition: border-color 0.2s;
    background: var(--bg-light);
    min-width: 0;
}
#cw-chat-widget #cw-input-chat:focus {
    border-color: var(--brand-orange);
    background: #fff;
}
#cw-chat-widget #cw-btn-send {
    background: var(--brand-orange);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s;
    padding: 0;
}
#cw-chat-widget #cw-btn-send:hover { background: #d47d00; }
#cw-chat-widget #cw-btn-send svg {
    width: 18px;
    height: 18px;
    transform: translateX(1px);
    display: block;
}

/* ---- Tarjetas de packs ---- */
#cw-chat-widget .cw-packs-wrapper {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
}
#cw-chat-widget .cw-packs-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-dark);
    margin-bottom: 2px;
}
#cw-chat-widget .cw-pack-card {
    border: 1px solid var(--brand-green);
    border-radius: 10px;
    padding: 10px;
    background: #f2fbf5;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
#cw-chat-widget .cw-pack-card h3 {
    font-size: 15px;
    margin: 0;
    line-height: 1.3;
    color: var(--text-dark);
    font-weight: 600;
}
#cw-chat-widget .cw-pack-card .cw-pack-meta {
    font-size: 14px;
    color: var(--text-light);
}
#cw-chat-widget .cw-btn-pack {
    display: block;
    margin-top: 4px;
    padding: 6px 10px;
    background: var(--brand-green);
    color: #fff;
    text-decoration: none;
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
    font-weight: 500;
}
#cw-chat-widget .cw-btn-pack:hover { background: #1f7a37; }

/* Packs / cursos dentro del detalle */
#cw-chat-widget .cw-detalle-packs {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px dashed var(--border-light);
}
#cw-chat-widget .cw-detalle-packs p {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 6px;
}
#cw-chat-widget .cw-detalle-packs ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
#cw-chat-widget .cw-detalle-packs a {
    display: block;
    font-size: 14px;
    color: var(--brand-green);
    text-decoration: none;
    background: #f2fbf5;
    padding: 6px 10px;
    border-radius: 6px;
    font-weight: 500;
    border: 1px solid #d1ebd9;
}
#cw-chat-widget .cw-detalle-packs a:hover { background: #e1f5e7; }

/* Botón de pack a ancho completo dentro del detalle */
#cw-chat-widget .cw-btn-pack-block {
    display: block;
    text-align: center;
    background: var(--brand-green);
    color: #fff;
    padding: 10px;
    border-radius: 6px;
    text-decoration: none;
    margin-top: 10px;
    font-weight: 500;
    font-size: 14px;
}
#cw-chat-widget .cw-btn-pack-block:hover { background: #1f7a37; }

/* Aviso legal */
#cw-chat-widget #cw-aviso-legal-chat {
    padding: 0 16px 8px;
    font-size: 13px;
    color: #888;
    flex-shrink: 0;
}

/* ---- Responsive ---- */
@media (max-width: 480px) {
    #cw-chat-widget {
        bottom: 16px;
        right: 16px;
    }
    #cw-chat-widget #cw-chat-toggle {
        width: 56px;
        height: 56px;
    }
    #cw-chat-widget #cw-chat-window {
        position: fixed;
        bottom: 84px;
        left: 16px;
        right: 16px;
        width: auto;
        height: auto;
        max-height: calc(100vh - 110px);
        max-height: calc(100dvh - 110px);
        border-radius: 12px;
        transform: translateY(20px);
    }
    #cw-chat-widget #cw-chat-window.open {
        transform: translateY(0);
    }
    #cw-chat-widget #cw-input-chat {
        font-size: 16px; /* evita zoom automático en iOS */
        padding: 8px 12px;
    }
    #cw-chat-widget #cw-welcome-bubble {
        right: 8px;
        font-size: 15px;
    }
    #cw-chat-widget .cw-detalle-grid {
        grid-template-columns: 1fr;
    }
}