/* ==========================================================================
   WhatsApp Chat Button — Frontend Widget
   ========================================================================== */

/* ---- CSS custom props (overridden inline from PHP) ---- */
:root {
    --avwa-btn-color:    #25D366;
    --avwa-header-color: #075E54;
    --avwa-popup-bg:     #ffffff;
    --avwa-popup-w:      340px;
    --avwa-radius:       16px;
}

/* Isolate the widget from the host theme's global box-sizing/line-height —
   the most common cause of "buttons don't line up" when embedded in a
   WordPress theme that resets these differently. */
#avwa-widget,
#avwa-widget *,
#avwa-widget *::before,
#avwa-widget *::after {
    box-sizing: border-box;
}

/* ---- Wrapper ---- */
/* El widget solo ocupa el tamaño del botón; popup y saludo se posicionan
   de forma absoluta para NO bloquear elementos vecinos. */
#avwa-widget {
    position: fixed;
    z-index: 99999;
    bottom: 24px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    /* Tamaño mínimo: sólo el botón */
    width: 56px;
    height: 56px;
}

#avwa-widget.avwa--right { right: 24px; }
#avwa-widget.avwa--left  { left: 24px; }

/* Ajusta el tamaño del wrapper según el tamaño del botón */
.avwa--size-small  { width: 46px; height: 46px; }
.avwa--size-large  { width: 68px; height: 68px; }

/* ---- Greeting bubble ---- */
#avwa-greeting {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,.15);
    padding: 10px 16px;
    font-size: 13px;
    max-width: 220px;
    line-height: 1.45;
    /* Absoluto para no bloquear nada */
    position: absolute;
    bottom: calc(100% + 14px);
    right: 0;
    opacity: 0;
    transform: translateY(8px) scale(.96);
    transition: opacity .3s ease, transform .3s ease;
    pointer-events: none;
    white-space: normal;
    width: max-content;
}

.avwa--left #avwa-greeting {
    right: auto;
    left: 0;
}

#avwa-greeting.visible {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

#avwa-greeting::after {
    content: '';
    position: absolute;
    bottom: -8px;
    width: 16px;
    height: 16px;
    background: #fff;
    clip-path: polygon(0 0, 100% 0, 100% 100%);
    box-shadow: 2px 2px 4px rgba(0,0,0,.08);
}

.avwa--right #avwa-greeting::after { right: 20px; }
.avwa--left  #avwa-greeting::after { left: 20px; transform: scaleX(-1); }

#avwa-greeting-close {
    position: absolute;
    top: 6px;
    right: 8px;
    background: none;
    border: none;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    color: #aaa;
    padding: 0;
}

/* ---- Main button ---- */
#avwa-btn {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--avwa-btn-color);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px var(--avwa-btn-shadow, rgba(37,211,102,.5));
    transition: transform .25s cubic-bezier(.34,1.56,.64,1), box-shadow .25s;
    /* position y bottom/left/right se definen más abajo junto a los size variants */
}

#avwa-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 24px var(--avwa-btn-shadow-hover, rgba(37,211,102,.65));
}

#avwa-btn svg {
    width: 30px;
    height: 30px;
    fill: #fff;
    transition: transform .3s, opacity .3s;
}

/* Pulse ring */
#avwa-btn::before {
    content: '';
    position: absolute;
    inset: -6px;
    border-radius: 50%;
    border: 3px solid var(--avwa-btn-color);
    opacity: 0;
    animation: avwaPulse 2.4s ease-out infinite;
}

@keyframes avwaPulse {
    0%   { transform: scale(1); opacity: .6; }
    100% { transform: scale(1.7); opacity: 0; }
}

/* Size variants */
.avwa--size-small  #avwa-btn { width: 46px; height: 46px; }
.avwa--size-small  #avwa-btn svg { width: 24px; height: 24px; }
.avwa--size-large  #avwa-btn { width: 68px; height: 68px; }
.avwa--size-large  #avwa-btn svg { width: 36px; height: 36px; }

/* El botón ocupa todo el wrapper */
#avwa-btn {
    position: absolute;
    bottom: 0;
    right: 0;
}

.avwa--left #avwa-btn {
    right: auto;
    left: 0;
}

/* Icon swap: WA icon ↔ close icon */
.avwa-icon-wa    { display: block; }
.avwa-icon-close { display: none; }

#avwa-widget.open .avwa-icon-wa    { display: none; }
#avwa-widget.open .avwa-icon-close { display: block; }

/* ---- Popup panel ---- */
/* Absoluto para que nunca bloquee elementos del sitio cuando está cerrado */
#avwa-popup {
    position: absolute;
    bottom: calc(100% + 12px);
    right: 0;
    width: var(--avwa-popup-w);
    border-radius: var(--avwa-radius);
    box-shadow: 0 8px 40px rgba(0,0,0,.18);
    overflow: hidden;
    background: var(--avwa-popup-bg, #ffffff);
    transform-origin: bottom right;
    transform: scale(.85) translateY(12px);
    opacity: 0;
    pointer-events: none;
    transition: transform .3s cubic-bezier(.34,1.56,.64,1), opacity .25s ease;
}

.avwa--left #avwa-popup {
    right: auto;
    left: 0;
    transform-origin: bottom left;
}

#avwa-widget.open #avwa-popup {
    transform: scale(1) translateY(0);
    opacity: 1;
    pointer-events: auto;
}

/* ---- Popup header ---- */
.avwa-popup-header {
    background: var(--avwa-header-color);
    padding: 20px 18px 28px;
    color: #fff;
    position: relative;
}

.avwa-popup-header::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 28px;
    background: var(--avwa-popup-bg, #ffffff);
    border-radius: 50% 50% 0 0 / 100% 100% 0 0;
}

.avwa-popup-header h3 {
    font-size: 18px;
    font-weight: 700;
    margin: 0 0 4px;
    color: #fff;
}

.avwa-popup-header p {
    font-size: 13px;
    margin: 0;
    opacity: .85;
}

/* ---- Agents list ---- */
.avwa-agents-popup {
    padding: 8px 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 320px;
    overflow-y: auto;
    scrollbar-width: thin;
}

/* ---- Agent row ---- */
.avwa-agent-row {
    display: flex;
    align-items: center;
    gap: 12px;
    background: #fff;
    border-radius: 12px;
    padding: 12px 14px;
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    box-shadow: 0 1px 4px rgba(0,0,0,.08);
    transition: box-shadow .2s, transform .2s;
}

.avwa-agent-row:hover {
    box-shadow: 0 3px 12px rgba(0,0,0,.14);
    transform: translateY(-1px);
}

.avwa-agent-av {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    position: relative;
}

.avwa-agent-av img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.avwa-agent-av-initials {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: var(--avwa-btn-color);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 700;
    font-size: 18px;
    flex-shrink: 0;
}

/* Online indicator */
.avwa-status-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid #fff;
    position: absolute;
    bottom: 1px;
    right: 1px;
}

.avwa-status-dot.online  { background: #44cc44; }
.avwa-status-dot.offline { background: #aaa; }

.avwa-agent-row-info {
    flex: 1;
    min-width: 0;
}

.avwa-agent-row-name {
    font-weight: 700;
    font-size: 14px;
    color: #222;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.avwa-agent-row-role {
    font-size: 12px;
    color: #777;
}

.avwa-agent-row-status {
    font-size: 11px;
    margin-top: 2px;
}

.avwa-agent-row-status.online  { color: #2a9e2a; }
.avwa-agent-row-status.offline { color: #999; }

.avwa-agent-row-arrow {
    color: var(--avwa-btn-color);
    font-size: 20px;
    font-weight: 300;
    line-height: 1;
}

/* ==========================================================================
   AI Chat panel
   ========================================================================== */

#avwa-chat-panel {
    display: flex;
    flex-direction: column;
    height: 340px;
    overflow: hidden;
}

#avwa-chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    scroll-behavior: smooth;
}

/* Scrollbar */
#avwa-chat-messages::-webkit-scrollbar { width: 4px; }
#avwa-chat-messages::-webkit-scrollbar-track { background: transparent; }
#avwa-chat-messages::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }

/* ---- Message bubbles ---- */
.avwa-msg {
    max-width: 82%;
    padding: 9px 13px;
    border-radius: 16px;
    font-size: 13px;
    line-height: 1.45;
    word-break: break-word;
    animation: avwaMsgIn .18s ease;
}

@keyframes avwaMsgIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.avwa-msg--user {
    background: var(--avwa-btn-color, #25D366);
    color: #fff;
    border-bottom-right-radius: 4px;
    align-self: flex-end;
}

.avwa-msg--bot {
    background: #f0f0f0;
    color: #222;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
}

.avwa-msg--error {
    background: #fff0f0;
    color: #c62828;
    border: 1px solid #fbb;
    align-self: center;
    font-size: 12px;
    border-radius: 8px;
}

/* ---- Typing indicator ---- */
.avwa-msg--typing {
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.avwa-msg--typing span {
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #aaa;
    animation: avwaTypingBounce 1.2s infinite ease-in-out;
}

.avwa-msg--typing span:nth-child(2) { animation-delay: .2s; }
.avwa-msg--typing span:nth-child(3) { animation-delay: .4s; }

@keyframes avwaTypingBounce {
    0%, 60%, 100% { transform: translateY(0); }
    30%           { transform: translateY(-5px); }
}

/* ---- Input area ---- */
#avwa-chat-input-wrap {
    flex-shrink: 0;
    border-top: 1px solid #eee;
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

#avwa-chat-form {
    display: flex;
    align-items: center;
    gap: 6px;
}

#avwa-chat-input {
    flex: 1 1 auto;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 8px 14px;
    font-size: 13px;
    outline: none;
    transition: border-color .2s;
    background: #fafafa;
    color: #222;
}

#avwa-chat-input:focus {
    border-color: var(--avwa-btn-color, #25D366);
    background: #fff;
}

#avwa-chat-send {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--avwa-btn-color, #25D366);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity .2s, transform .15s;
    color: #fff;
}

#avwa-chat-send svg {
    width: 22px;
    height: 22px;
}

#avwa-chat-send:hover  { opacity: .88; }
#avwa-chat-send:active { transform: scale(.92); }
#avwa-chat-send:disabled { opacity: .45; cursor: not-allowed; }

/* ---- Transfer to agents button ---- */
#avwa-chat-to-agents {
    background: none;
    border: 1px solid rgba(0,0,0,.12);
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    color: var(--avwa-header-color, #075E54);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    width: 100%;
    padding: 8px 10px;
    border-radius: 20px;
    transition: background-color .2s, opacity .2s;
}

#avwa-chat-to-agents:hover { background-color: rgba(0,0,0,.04); }

/* ---- Back button in header ---- */
.avwa-chat-back-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: rgba(255,255,255,.8);
    padding: 0 6px 0 0;
    display: flex;
    align-items: center;
    line-height: 1;
    transition: color .2s;
}

.avwa-chat-back-btn:hover { color: #fff; }

/* Adjust header when back button is present */
.avwa-popup-header {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0 6px;
}

.avwa-popup-header h3,
.avwa-popup-header p {
    flex: 1 1 100%;
}

.avwa-popup-header:has(.avwa-chat-back-btn[style*="display: flex"]) h3,
.avwa-popup-header:has(.avwa-chat-back-btn:not([style*="none"])) h3 {
    flex: 1 1 auto;
}

/* ---- Agents panel transition ---- */
#avwa-agents-panel {
    animation: avwaMsgIn .2s ease;
}

/* ---- Entrance animation (agrega opacity al bloque principal) ---- */
#avwa-widget {
    opacity: 0;
    transition: opacity .4s ease;
}

#avwa-widget.avwa-ready {
    opacity: 1;
}

/* ---- Accessibility ---- */
#avwa-btn:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 3px;
}

/* ---- Mobile adjustments ---- */
@media (max-width: 480px) {
    :root { --avwa-popup-w: calc(100vw - 32px); }
    #avwa-widget.avwa--right { right: 16px; }
    #avwa-widget.avwa--left  { left: 16px; }
    /* En móvil el popup puede salirse a la izquierda; lo anclamos */
    #avwa-widget.avwa--right #avwa-popup { right: 0; left: auto; }
    #avwa-widget.avwa--left  #avwa-popup { left: 0;  right: auto; }
}
