/* ============================================
   Chat Agent Widget — "Коріння та Крила"
   Matches site brand: #2c3e50, #e67e22, Montserrat
   ============================================ */

:root {
    --rw-safe-bottom: env(safe-area-inset-bottom, 0px);
    --rw-safe-top: env(safe-area-inset-top, 0px);
}

body.rw-chat-open {
    position: fixed;
    width: 100%;
    overflow: hidden;
}

body.menu-open .rw-chat-toggle,
body.menu-open .rw-chat-window,
body.menu-open .rw-chat-overlay {
    display: none !important;
}

/* Toggle Button */
.rw-chat-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 58px;
    height: 58px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(44, 62, 80, 0.4);
    z-index: 10020;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    animation: rw-pulse 3s ease-in-out infinite;
}

/* Backdrop */
.rw-chat-overlay {
    position: fixed;
    inset: 0;
    background: rgba(22, 32, 43, 0.46);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 10010;
}
.rw-chat-overlay.open {
    opacity: 1;
    pointer-events: auto;
}
.rw-chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 28px rgba(44, 62, 80, 0.55);
}
.rw-chat-toggle svg {
    width: 26px;
    height: 26px;
    fill: #ffffff;
    transition: transform 0.3s ease;
}
.rw-chat-toggle.active svg {
    transform: rotate(90deg);
}
.rw-chat-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 18px;
    height: 18px;
    background: #e67e22;
    border-radius: 50%;
    border: 2px solid #fff;
    animation: rw-badge-pulse 2s ease-in-out infinite;
}
@keyframes rw-pulse {
    0%, 100% { box-shadow: 0 4px 20px rgba(44, 62, 80, 0.4); }
    50% { box-shadow: 0 4px 20px rgba(230, 126, 34, 0.5); }
}
@keyframes rw-badge-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

/* Chat Window */
.rw-chat-window {
    position: fixed;
    bottom: 94px;
    right: 24px;
    width: 370px;
    height: 540px;
    max-height: calc(100vh - 120px);
    background: #ffffff;
    border-radius: 16px;
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.18);
    z-index: 10021;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
}
.rw-chat-window.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* Header */
.rw-chat-header {
    background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
    color: #ffffff;
    padding: 16px 18px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}
.rw-chat-avatar {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, #e67e22, #f39c12);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}
.rw-chat-header-info {
    flex: 1;
}
.rw-chat-header-title {
    font-family: 'Playfair Display', serif;
    font-size: 15px;
    font-weight: 600;
    line-height: 1.2;
}
.rw-chat-header-subtitle {
    font-size: 11px;
    color: #bdc3c7;
    margin-top: 2px;
}
.rw-chat-close {
    background: none;
    border: none;
    color: #bdc3c7;
    cursor: pointer;
    font-size: 20px;
    padding: 4px;
    line-height: 1;
    transition: color 0.2s;
}
.rw-chat-close:hover {
    color: #ffffff;
}

/* Messages Area */
.rw-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}
.rw-chat-messages::-webkit-scrollbar {
    width: 5px;
}
.rw-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}
.rw-chat-messages::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 3px;
}

/* Message Bubbles */
.rw-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.5;
    font-family: 'Montserrat', sans-serif;
    word-wrap: break-word;
    animation: rw-msg-in 0.3s ease;
}
@keyframes rw-msg-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}
.rw-msg-bot {
    background: #f0f2f5;
    color: #333;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
}
.rw-msg-user {
    background: linear-gradient(135deg, #2c3e50, #34495e);
    color: #ffffff;
    border-bottom-right-radius: 4px;
    align-self: flex-end;
}
.rw-msg-bot a {
    color: #e67e22;
    text-decoration: underline;
}

/* Typing indicator */
.rw-typing {
    display: flex;
    gap: 4px;
    padding: 10px 14px;
    background: #f0f2f5;
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    max-width: 60px;
}
.rw-typing span {
    width: 7px;
    height: 7px;
    background: #aaa;
    border-radius: 50%;
    animation: rw-typing-dot 1.4s ease-in-out infinite;
}
.rw-typing span:nth-child(2) { animation-delay: 0.2s; }
.rw-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes rw-typing-dot {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Quick Buttons */
.rw-quick-btns {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}
.rw-quick-btn {
    background: #ffffff;
    border: 1.5px solid #2c3e50;
    color: #2c3e50;
    padding: 6px 13px;
    border-radius: 18px;
    font-size: 12.5px;
    font-family: 'Montserrat', sans-serif;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}
.rw-quick-btn:hover {
    background: #2c3e50;
    color: #ffffff;
}
.rw-quick-btn.rw-btn-primary {
    background: linear-gradient(135deg, #e67e22, #f39c12);
    border-color: #e67e22;
    color: #ffffff;
    font-weight: 600;
}
.rw-quick-btn.rw-btn-primary:hover {
    background: linear-gradient(135deg, #d35400, #e67e22);
}
.rw-quick-btn.rw-btn-danger {
    border-color: #c0392b;
    color: #c0392b;
}
.rw-quick-btn.rw-btn-danger:hover {
    background: #c0392b;
    color: #ffffff;
}

/* Order Summary */
.rw-order-summary {
    background: #f9f9f9;
    border-radius: 10px;
    padding: 12px;
    margin-top: 6px;
    font-size: 13px;
    border-left: 3px solid #2c3e50;
}
.rw-order-summary .rw-order-row {
    display: flex;
    justify-content: space-between;
    padding: 3px 0;
}
.rw-order-summary .rw-order-label {
    color: #666;
}
.rw-order-summary .rw-order-value {
    color: #2c3e50;
    font-weight: 600;
}
.rw-order-summary .rw-order-total {
    border-top: 1px solid #ddd;
    margin-top: 6px;
    padding-top: 6px;
    font-size: 15px;
}
.rw-order-summary .rw-order-total .rw-order-value {
    color: #e67e22;
    font-weight: 700;
}

/* Input Area */
.rw-chat-input-area {
    padding: 12px 14px;
    border-top: 1px solid #eee;
    display: flex;
    gap: 8px;
    align-items: flex-end;
    flex-shrink: 0;
    background: #fafafa;
}
.rw-chat-input {
    flex: 1;
    border: 1.5px solid #ddd;
    border-radius: 20px;
    padding: 9px 16px;
    font-size: 13.5px;
    font-family: 'Montserrat', sans-serif;
    outline: none;
    resize: none;
    max-height: 80px;
    transition: border-color 0.2s;
    line-height: 1.4;
    min-height: 38px;
}
.rw-chat-input:focus {
    border-color: #2c3e50;
}
.rw-chat-input::placeholder {
    color: #aaa;
}
.rw-chat-send {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2c3e50, #34495e);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.2s, opacity 0.2s;
}
.rw-chat-send:hover {
    transform: scale(1.08);
}
.rw-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
.rw-chat-send svg {
    width: 18px;
    height: 18px;
    fill: #ffffff;
}

/* Mobile */
@media (max-width: 767px) {
    .rw-chat-toggle {
        bottom: calc(16px + var(--rw-safe-bottom));
        right: 16px;
        width: 56px;
        height: 56px;
        box-shadow: 0 6px 22px rgba(44, 62, 80, 0.45);
    }

    .rw-chat-toggle:hover {
        transform: none;
    }

    .rw-chat-window {
        width: 100vw;
        right: 0;
        left: 0;
        bottom: 0;
        height: calc(var(--rw-chat-vh, 100vh) - 6px);
        max-height: calc(var(--rw-chat-vh, 100vh) - 6px);
        border-radius: 18px 18px 0 0;
        transform: translateY(110%);
    }

    .rw-chat-window.open {
        transform: translateY(0);
    }

    .rw-chat-header {
        padding: 14px 14px 12px;
        position: sticky;
        top: 0;
        z-index: 2;
    }

    .rw-chat-header-title {
        font-size: 16px;
    }

    .rw-chat-header-subtitle {
        font-size: 12px;
    }

    .rw-chat-avatar {
        width: 38px;
        height: 38px;
    }

    .rw-chat-messages {
        padding: 12px;
        gap: 10px;
    }

    .rw-msg {
        max-width: 92%;
        font-size: 14px;
        padding: 10px 12px;
        line-height: 1.45;
    }

    .rw-quick-btns {
        width: 100%;
        gap: 8px;
    }

    .rw-quick-btn {
        padding: 9px 12px;
        font-size: 13px;
        border-radius: 14px;
        white-space: normal;
        text-align: left;
        line-height: 1.3;
    }

    .rw-order-summary {
        font-size: 13px;
    }

    .rw-order-summary .rw-order-row {
        gap: 10px;
    }

    .rw-order-summary .rw-order-value {
        text-align: right;
        max-width: 62%;
    }

    .rw-chat-input-area {
        padding: 10px 10px calc(10px + var(--rw-safe-bottom));
        gap: 10px;
    }

    .rw-chat-input {
        font-size: 16px;
        padding: 10px 14px;
        min-height: 42px;
    }

    .rw-chat-send {
        width: 42px;
        height: 42px;
    }

    .rw-chat-close {
        min-width: 36px;
        min-height: 36px;
        border-radius: 10px;
        background: rgba(255, 255, 255, 0.12);
        color: #fff;
    }
}

@media (max-width: 420px) {
    .rw-chat-toggle {
        right: 12px;
    }

    .rw-chat-window {
        border-radius: 16px 16px 0 0;
    }

    .rw-chat-header {
        padding: 12px;
    }

    .rw-chat-header-title {
        font-size: 15px;
    }

    .rw-chat-input-area {
        padding: 9px 9px calc(9px + var(--rw-safe-bottom));
    }
}

@media (prefers-reduced-motion: reduce) {
    .rw-chat-toggle,
    .rw-chat-window,
    .rw-chat-overlay,
    .rw-msg,
    .rw-quick-btn {
        animation: none !important;
        transition-duration: 0.01ms !important;
    }
}
