/* ── WP Live Chat – Frontend Widget v1.1.0 ─────────────────────────────────
   Colors are injected via CSS variables set dynamically by chat-widget.js
   from the values stored in WordPress settings.
   ───────────────────────────────────────────────────────────────────────── */
:root {
    --wplc-primary:    #0babdc;
    --wplc-secondary:  #0c3254;
    --wplc-primary-d:  #0990ba;
    --wplc-radius:     16px;
    --wplc-shadow:     0 12px 40px rgba(11,171,220,.25), 0 4px 16px rgba(12,50,84,.18);
    --wplc-font:       -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#wplc-root * { box-sizing: border-box; margin: 0; padding: 0; font-family: var(--wplc-font); }

/* ══════════════════════════════════════════════════════════════════════════
   FAB — Fancy animated launch button
   ══════════════════════════════════════════════════════════════════════════ */
#wplc-fab {
    position: fixed;
    bottom: 28px; right: 28px;
    /* BUG 4 fix — respect iOS home bar */
    bottom: calc( 28px + env(safe-area-inset-bottom, 0px) );
    right:  calc( 28px + env(safe-area-inset-right,  0px) );
    width: 64px; height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--wplc-secondary) 0%, var(--wplc-primary) 100%);
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow: var(--wplc-shadow);
    display: flex; align-items: center; justify-content: center;
    z-index: 999998;
    transition: transform .3s cubic-bezier(.34,1.56,.64,1), box-shadow .3s;
    outline: 3px solid rgba(11,171,220,.22);
    outline-offset: 3px;
    /* BUG 3 fix — eliminates 300ms tap delay on iOS/Android */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}
#wplc-fab:hover {
    transform: scale(1.12) rotate(-6deg);
    box-shadow: 0 18px 50px rgba(11,171,220,.38), 0 6px 20px rgba(12,50,84,.25);
}
#wplc-fab:active { transform: scale(.96); }

/* ── Idle pulse rings ────────────────────────────────────────────────────── */
#wplc-fab::before,
#wplc-fab::after {
    content: '';
    position: absolute;
    inset: -8px;
    border-radius: 50%;
    border: 2px solid var(--wplc-primary);
    opacity: 0;
    animation: wplcRing 3s ease-out infinite;
    pointer-events: none;
}
#wplc-fab::after {
    inset: -16px;
    animation-delay: .8s;
}
/* Stop rings when chat is open */
#wplc-fab.is-open::before,
#wplc-fab.is-open::after { animation: none; opacity: 0; }

@keyframes wplcRing {
    0%   { opacity: .7; transform: scale(.85); }
    60%  { opacity: .15; }
    100% { opacity: 0; transform: scale(1.35); }
}

/* ── Icon layers inside FAB ──────────────────────────────────────────────── */
#wplc-fab-icon-chat,
#wplc-fab-icon-close {
    position: absolute;
    display: flex; align-items: center; justify-content: center;
    transition: opacity .25s, transform .3s cubic-bezier(.34,1.56,.64,1);
}
#wplc-fab-icon-chat  { opacity: 1; transform: scale(1) rotate(0deg); }
#wplc-fab-icon-close { opacity: 0; transform: scale(.4) rotate(-90deg); }
#wplc-fab.is-open #wplc-fab-icon-chat  { opacity: 0; transform: scale(.4) rotate(90deg); }
#wplc-fab.is-open #wplc-fab-icon-close { opacity: 1; transform: scale(1) rotate(0deg); }

/* Animated dots inside the chat icon */
#wplc-fab-dots {
    display: flex; gap: 3px; align-items: center;
    position: absolute;
    bottom: 16px;
}
#wplc-fab-dots span {
    width: 5px; height: 5px;
    border-radius: 50%;
    background: rgba(255,255,255,.9);
    animation: wplcDotBounce 1.6s ease-in-out infinite;
}
#wplc-fab-dots span:nth-child(2) { animation-delay: .18s; }
#wplc-fab-dots span:nth-child(3) { animation-delay: .36s; }
@keyframes wplcDotBounce {
    0%, 80%, 100% { transform: translateY(0); opacity: .7; }
    40%           { transform: translateY(-4px); opacity: 1; }
}
#wplc-fab.is-open #wplc-fab-dots { display: none; }

/* ── Unread badge ────────────────────────────────────────────────────────── */
#wplc-fab-badge {
    position: absolute;
    top: -2px; right: -2px;
    min-width: 20px; height: 20px;
    padding: 0 5px;
    background: #ef4444;
    border: 2px solid #fff;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 700;
    color: #fff;
    display: none;
    align-items: center;
    justify-content: center;
    animation: wplcBadgePop .3s cubic-bezier(.34,1.56,.64,1);
}
@keyframes wplcBadgePop {
    from { transform: scale(0); }
    to   { transform: scale(1); }
}

/* ══════════════════════════════════════════════════════════════════════════
   Chat window
   ══════════════════════════════════════════════════════════════════════════ */
#wplc-window {
    position: fixed;
    bottom: 106px; right: 28px;
    /* BUG 4 fix — respect iOS home bar & notch */
    bottom: calc( 106px + env(safe-area-inset-bottom, 0px) );
    right:  calc(  28px + env(safe-area-inset-right,  0px) );
    width: 370px;
    height: 540px;
    background: #fff;
    border-radius: var(--wplc-radius);
    box-shadow: 0 20px 60px rgba(12,50,84,.22), 0 6px 20px rgba(11,171,220,.14);
    display: flex; flex-direction: column;
    z-index: 999999;
    overflow: hidden;
    transform-origin: bottom right;
    transform: scale(0) translateY(10px);
    opacity: 0;
    transition: transform .3s cubic-bezier(.34,1.56,.64,1), opacity .25s;
    pointer-events: none;
}
#wplc-window.open {
    transform: scale(1) translateY(0);
    opacity: 1;
    pointer-events: auto;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
#wplc-header {
    background: linear-gradient(135deg, var(--wplc-secondary) 0%, var(--wplc-primary) 100%);
    color: #fff;
    padding: 16px 20px;
    display: flex; align-items: center; gap: 13px;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}
/* Decorative shimmer on header */
#wplc-header::after {
    content: '';
    position: absolute;
    top: -50%; right: -20%;
    width: 140px; height: 140px;
    border-radius: 50%;
    background: rgba(255,255,255,.06);
    pointer-events: none;
}

#wplc-header-avatar {
    width: 42px; height: 42px;
    border-radius: 50%;
    background: rgba(255,255,255,.22);
    border: 2px solid rgba(255,255,255,.35);
    display: flex; align-items: center; justify-content: center;
    font-weight: 800; font-size: 17px;
    flex-shrink: 0;
    /* Soft glow */
    box-shadow: 0 0 0 4px rgba(255,255,255,.10);
}
#wplc-header-info { flex: 1; }
#wplc-header-info strong { display: block; font-size: 15px; font-weight: 700; }
#wplc-header-status { font-size: 12px; opacity: .85; margin-top: 1px; display: block; }

/* Online indicator dot in status */
.wplc-status-online::before {
    content: '';
    display: inline-block;
    width: 7px; height: 7px;
    background: #4ade80;
    border-radius: 50%;
    margin-right: 5px;
    animation: wplcPulseGreen 2s ease infinite;
    vertical-align: middle;
}
@keyframes wplcPulseGreen {
    0%, 100% { box-shadow: 0 0 0 0 rgba(74,222,128,.6); }
    50%       { box-shadow: 0 0 0 4px rgba(74,222,128,0); }
}

#wplc-close-win {
    background: rgba(255,255,255,.15);
    border: none;
    color: rgba(255,255,255,.9);
    cursor: pointer;
    width: 28px; height: 28px;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 14px;
    transition: background .2s;
    flex-shrink: 0;
}
#wplc-close-win:hover { background: rgba(255,255,255,.28); }

/* ── Pre-chat form ──────────────────────────────────────────────────────── */
#wplc-prechat {
    flex: 1;
    padding: 24px 22px;
    overflow-y: auto;
    display: flex; flex-direction: column; gap: 16px;
}
#wplc-prechat .wplc-pre-note {
    font-size: 14px; color: #374151; line-height: 1.6;
    background: #f0f9ff;
    border-left: 3px solid var(--wplc-primary);
    padding: 10px 14px;
    border-radius: 0 8px 8px 0;
}
#wplc-prechat label {
    font-size: 13px; font-weight: 600; color: #374151;
    display: block; margin-bottom: 5px;
}
#wplc-prechat input {
    width: 100%;
    padding: 10px 14px;
    border: 1.5px solid #d1d5db;
    border-radius: 9px;
    font-size: 14px;
    outline: none;
    transition: border-color .2s, box-shadow .2s;
    color: #1a2540;
}
#wplc-prechat input:focus {
    border-color: var(--wplc-primary);
    box-shadow: 0 0 0 3px rgba(11,171,220,.12);
}
#wplc-start-btn {
    padding: 12px;
    background: linear-gradient(135deg, var(--wplc-secondary) 0%, var(--wplc-primary) 100%);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 14px; font-weight: 700;
    cursor: pointer;
    transition: opacity .2s, transform .15s;
    width: 100%;
    letter-spacing: .02em;
    box-shadow: 0 4px 14px rgba(11,171,220,.3);
}
#wplc-start-btn:hover  { opacity: .92; transform: translateY(-1px); }
#wplc-start-btn:active { transform: translateY(0); }
#wplc-start-btn:disabled { opacity: .55; cursor: default; transform: none; }
.wplc-field-error { font-size: 12px; color: #ef4444; display: none; margin-top: 4px; }

/* ── Chat area ──────────────────────────────────────────────────────────── */
#wplc-chat { display: none; flex: 1; flex-direction: column; overflow: hidden; }
#wplc-chat.active { display: flex; }

#wplc-msgs {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    display: flex; flex-direction: column; gap: 8px;
    background: #f7fafc;
    scroll-behavior: smooth;
}

/* Custom scrollbar */
#wplc-msgs::-webkit-scrollbar { width: 4px; }
#wplc-msgs::-webkit-scrollbar-track { background: transparent; }
#wplc-msgs::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 2px; }

/* ── Bubbles ──────────────────────────────────────────────────────────── */
.wplc-bubble {
    max-width: 75%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
    overflow-wrap: break-word;
    animation: wplcPop .2s cubic-bezier(.34,1.56,.64,1);
}
@keyframes wplcPop {
    from { opacity:0; transform: scale(.85) translateY(6px); }
    to   { opacity:1; transform: scale(1) translateY(0);     }
}
.wplc-bubble.agent {
    background: #fff;
    border: 1px solid #e5e7eb;
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    color: #111827;
    box-shadow: 0 2px 6px rgba(0,0,0,.06);
    margin-left: 4px;
    margin-right: 20px;
}
.wplc-bubble.visitor {
    background: linear-gradient(135deg, var(--wplc-secondary) 0%, var(--wplc-primary) 100%);
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
    box-shadow: 0 4px 12px rgba(11,171,220,.28);
    margin-right: 4px;
    margin-left: 20px;
}
.wplc-bubble .wplc-btime {
    display: block;
    font-size: 10px;
    margin-top: 5px;
    opacity: .55;
}

/* Typing indicator */
.wplc-typing-dots {
    display: flex; gap: 4px;
    padding: 10px 14px;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    box-shadow: 0 2px 6px rgba(0,0,0,.06);
    animation: wplcPop .2s ease;
}
.wplc-typing-dots span {
    width: 6px; height: 6px;
    border-radius: 50%;
    background: var(--wplc-primary);
    animation: wplcDot 1.2s ease-in-out infinite;
}
.wplc-typing-dots span:nth-child(2) { animation-delay: .2s; }
.wplc-typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes wplcDot {
    0%,80%,100% { transform: scale(1);   opacity:.5; }
    40%          { transform: scale(1.4); opacity:1;  }
}

/* Session closed notice */
.wplc-closed-notice {
    text-align: center;
    font-size: 13px; color: #6b7280;
    padding: 14px 12px;
    border-top: 1px solid #e5e7eb;
    background: #f9fafb;
    flex-shrink: 0;
}

/* ── Input bar ──────────────────────────────────────────────────────────── */
#wplc-inputbar {
    display: flex; gap: 8px; align-items: flex-end;
    padding: 10px 14px;
    border-top: 1px solid #e5e7eb;
    background: #fff;
    flex-shrink: 0;
}
#wplc-input {
    flex: 1;
    padding: 9px 14px;
    border: 1.5px solid #d1d5db;
    border-radius: 22px;
    font-size: 14px;
    resize: none;
    outline: none;
    max-height: 100px;
    overflow-y: auto;
    transition: border-color .2s, box-shadow .2s;
    font-family: var(--wplc-font);
    color: #1a2540;
}
#wplc-input:focus {
    border-color: var(--wplc-primary);
    box-shadow: 0 0 0 3px rgba(11,171,220,.12);
}
#wplc-send {
    width: 42px; height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--wplc-secondary) 0%, var(--wplc-primary) 100%);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: opacity .2s, transform .2s cubic-bezier(.34,1.56,.64,1), box-shadow .2s;
    box-shadow: 0 3px 10px rgba(11,171,220,.3);
}
#wplc-send:hover  { opacity: .9; transform: scale(1.1); }
#wplc-send:active { transform: scale(.92); }
#wplc-send:disabled {
    background: #e5e7eb;
    box-shadow: none;
    cursor: default;
    transform: none;
    opacity: 1;
}
#wplc-send svg { width: 17px; height: 17px; }

/* ── Responsive ─────────────────────────────────────────────────────────── */

/* BUG 6 fix — medium phones (421–600px): window can touch right edge */
@media (max-width: 600px) {
    #wplc-window {
        right: 12px;
        right: calc( 12px + env(safe-area-inset-right, 0px) );
        width: calc( 100vw - 24px );
        max-width: 370px;
    }
    #wplc-fab {
        right: 16px;
        right: calc( 16px + env(safe-area-inset-right, 0px) );
    }
}

/* Full-screen mode on small phones */
@media (max-width: 420px) {
    #wplc-window {
        bottom: 0 !important;        /* override safe-area calc above */
        right: 0 !important;
        width: 100vw;
        /* BUG 2 fix — dvh fallback first, then override if supported */
        height: 100vh;
        height: 100dvh;
        border-radius: 0;
        transform-origin: bottom center;
        /* BUG 4 fix — full-screen: push content above home bar */
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    #wplc-fab {
        bottom: 16px;
        bottom: calc( 16px + env(safe-area-inset-bottom, 0px) );
        right: 16px;
        right: calc( 16px + env(safe-area-inset-right,  0px) );
    }
    /* BUG 5 fix — 16px minimum prevents iOS Safari auto-zoom on input focus */
    #wplc-prechat input,
    #wplc-input {
        font-size: 16px !important;
    }
    /* Compensate so the pre-chat form doesn't feel oversized */
    #wplc-prechat { padding: 20px 18px; gap: 14px; }
    #wplc-prechat .wplc-pre-note { font-size: 14px; }
}
