/* ShopAssist AI — Chat Widget Styles (Phase 3) */

:root {
    --sa-primary: #007bff;
    --sa-bg: #ffffff;
    --sa-text: #333333;
    --sa-shadow: 0 4px 24px rgba(0,0,0,0.18);
    --sa-radius: 12px;
    --sa-header-h: 52px;
    --sa-input-h: 56px;
}

/* ── Toggle button ───────────────────────────────────────── */
.sa-widget {
    --sa-offset-bottom: 112px;
    --sa-offset-right: 24px;
    --sa-offset-left: 88px;
    --sa-launcher-height: 56px;
    --sa-panel-room: calc(100dvh - var(--sa-offset-bottom) - var(--sa-launcher-height) - 36px - env(safe-area-inset-bottom, 0px));
}
.sa-widget--bottom-left { --sa-offset-bottom: 24px; }
.sa-widget--shipping-pilot { --sa-launcher-height: 40px; }
.sa-toggle-btn {
    position: fixed;
    bottom: calc(var(--sa-offset-bottom, 112px) + env(safe-area-inset-bottom, 0px));
    right: var(--sa-offset-right, 24px);
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--sa-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    font-size: 24px;
    box-shadow: var(--sa-shadow);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease;
}
.sa-toggle-btn:hover { transform: scale(1.08); }

.sa-widget--bottom-left .sa-toggle-btn { right: auto; left: var(--sa-offset-left, 88px); }

.sa-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #dc3545;
    color: #fff;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

/* ── Chat panel ──────────────────────────────────────────── */
.sa-chat-panel {
    position: fixed;
    bottom: calc(var(--sa-offset-bottom, 112px) + var(--sa-launcher-height, 56px) + 12px + env(safe-area-inset-bottom, 0px));
    right: var(--sa-offset-right, 24px);
    width: 360px;
    max-height: min(560px, var(--sa-panel-room));
    background: var(--sa-bg);
    border-radius: var(--sa-radius);
    box-shadow: var(--sa-shadow);
    display: flex;
    flex-direction: column;
    z-index: 9998;
    overflow: hidden;
}

.sa-widget--bottom-left .sa-chat-panel { right: auto; left: var(--sa-offset-left, 88px); }

.sa-widget--compact .sa-chat-panel  { width: 300px; max-height: min(420px, var(--sa-panel-room)); }
.sa-widget--standard .sa-chat-panel { width: 360px; max-height: min(560px, var(--sa-panel-room)); }

@media (max-width: 600px) {
    .sa-widget { --sa-offset-right: 16px; }
    .sa-widget--bottom-left { --sa-offset-left: 20px; --sa-offset-bottom: 88px; }
    .sa-widget .sa-toggle-label { white-space: normal; }
    .sa-widget.sa-widget--bottom-left .sa-toggle-btn { max-width: calc(100vw - 40px); }
    .sa-widget:not(.sa-widget--floating):not(.sa-widget--fullscreen-mobile) .sa-chat-panel {
        width: auto;
        left: 16px;
        right: 16px;
    }
    .sa-widget--fullscreen-mobile .sa-chat-panel {
        width: 100vw;
        height: 100vh;
        max-height: 100vh;
        bottom: 0;
        right: 0;
        left: 0;
        border-radius: 0;
    }
}

/* ── Header ──────────────────────────────────────────────── */
.sa-chat-header {
    background: var(--sa-primary);
    color: #fff;
    padding: 0 16px;
    height: var(--sa-header-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
.sa-header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}
.sa-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255,255,255,0.18);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.sa-chat-title { font-weight: 600; font-size: 15px; }

.sa-header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.sa-close-btn {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 22px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
}

/* Transfer to operator button */
.sa-transfer-btn {
    background: rgba(255,255,255,0.15);
    border: 1px solid rgba(255,255,255,0.5);
    border-radius: 6px;
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    padding: 3px 8px;
    line-height: 1.5;
    transition: background 0.15s;
}
.sa-transfer-btn:hover { background: rgba(255,255,255,0.28); }
.sa-transfer-btn:disabled { opacity: 0.5; cursor: default; }

/* Custom icon inside toggle button */
.sa-toggle-icon {
    width: 28px;
    height: 28px;
    object-fit: contain;
    border-radius: 2px;
    /* Hide broken-image alt text if the src fails to load */
    overflow: hidden;
    font-size: 0;
    color: transparent;
}
.sa-toggle-label {
    font-size: 15px;
    font-weight: 600;
    margin-left: 6px;
    white-space: nowrap;
}
/* Widen toggle button when it has a text label */
.sa-toggle-btn:has(.sa-toggle-label) {
    width: auto;
    border-radius: 28px;
    padding: 0 16px;
}
/* Prevent icon SVG from being hidden by overflow in the circle */
.sa-toggle-btn > svg {
    flex-shrink: 0;
    display: block;
    overflow: visible;
}

/* ── Messages area ───────────────────────────────────────── */
.sa-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sa-msg {
    max-width: 86%;
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 14px;
    line-height: 1.5;
    color: var(--sa-text);
    word-break: break-word;
}
.sa-msg--user {
    background: var(--sa-primary);
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 2px;
}
.sa-msg--assistant {
    background: #f1f3f5;
    align-self: flex-start;
    border-bottom-left-radius: 2px;
    max-width: 92%;
}
.sa-msg--typing {
    background: #f1f3f5;
    align-self: flex-start;
}

/* Animated typing dots */
.sa-dots span {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #999;
    margin: 0 2px;
    animation: sa-blink 1.2s infinite;
}
.sa-dots span:nth-child(2) { animation-delay: 0.2s; }
.sa-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes sa-blink {
    0%, 80%, 100% { opacity: 0.2; transform: translateY(0); }
    40%           { opacity: 1;   transform: translateY(-4px); }
}

/* ── Product carousel ─────────────────────────────────────── */
.sa-product-carousel {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
}

.sa-product-card {
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-size: 13px;
    transition: box-shadow 0.15s;
}
.sa-product-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.10); }

.sa-product-card__img-link {
    flex-shrink: 0;
    display: block;
    line-height: 0;
}
.sa-product-card__img {
    width: 60px;
    height: 60px;
    object-fit: contain;
    border-radius: 4px;
    border: 1px solid #f0f0f0;
    background: #fafafa;
}

.sa-product-card__body {
    flex: 1;
    min-width: 0;
}
.sa-product-card__name {
    display: block;
    color: var(--sa-primary);
    text-decoration: none;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sa-product-card__name:hover { text-decoration: underline; }
.sa-product-card__price {
    font-weight: 700;
    color: #222;
    margin-top: 2px;
}
.sa-product-card__cat {
    color: #888;
    font-size: 11px;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sa-product-card__actions {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex-shrink: 0;
}
.sa-product-card__view {
    display: block;
    text-align: center;
    padding: 4px 10px;
    border: 1px solid var(--sa-primary);
    border-radius: 4px;
    color: var(--sa-primary);
    text-decoration: none;
    font-size: 12px;
    font-weight: 500;
}
.sa-product-card__view:hover { background: var(--sa-primary); color: #fff; }
.sa-product-card__cart {
    padding: 4px 10px;
    background: var(--sa-primary);
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
}
.sa-product-card__cart:hover { opacity: 0.88; }
.sa-product-card__cart:disabled { opacity: 0.6; cursor: default; }
.sa-product-card__cart--added { background: #28a745; }
/* "Select Options" is a link styled to match the cart button (products needing attribute choices) */
.sa-product-card__options { display: block; text-align: center; text-decoration: none; }

/* ── Cross-sell label ─────────────────────────────────────── */
.sa-cards-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #888;
    margin-top: 10px;
    margin-bottom: 4px;
}

/* ── Rating bar ───────────────────────────────────────────── */
.sa-rating {
    display: flex;
    gap: 6px;
    margin-top: 6px;
    align-items: center;
}
.sa-rating__btn {
    background: transparent;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s;
}
.sa-rating__btn:hover { background: #e8e8e8; }
.sa-rating__thanks {
    font-size: 12px;
    color: #888;
    font-style: italic;
}

/* ── Input area ──────────────────────────────────────────── */
.sa-input-area {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    border-top: 1px solid #e8e8e8;
    gap: 8px;
    min-height: var(--sa-input-h);
    height: auto;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.sa-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 8px 14px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
}
.sa-input:focus { border-color: var(--sa-primary); }
.sa-send-btn {
    background: var(--sa-primary);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    flex-shrink: 0;
}
.sa-send-btn:hover { opacity: 0.88; }
.sa-send-btn:disabled { opacity: 0.5; cursor: default; }

/* ── Mic button ──────────────────────────────────────────── */
.sa-mic-btn {
    background: transparent;
    color: #888;
    border: 1px solid #ddd;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.sa-mic-btn:hover {
    color: var(--sa-primary);
    border-color: var(--sa-primary);
}
.sa-mic-btn:disabled { opacity: 0.4; cursor: default; }
.sa-mic-btn--recording {
    color: #dc3545;
    border-color: #dc3545;
    background: rgba(220, 53, 69, 0.08);
    animation: sa-mic-pulse 1s ease-in-out infinite;
}
@keyframes sa-mic-pulse {
    0%, 100% { transform: scale(1);    box-shadow: 0 0 0 0   rgba(220, 53, 69, 0.4); }
    50%       { transform: scale(1.12); box-shadow: 0 0 0 6px rgba(220, 53, 69, 0);   }
}
.sa-voice-error {
    font-size: 12px;
    color: #dc3545;
    padding: 2px 4px 0;
    flex-basis: 100%;
    text-align: center;
    display: none;
}
.sa-voice-error--visible { display: block; }

/* ── Footer ──────────────────────────────────────────────── */
.sa-footer {
    text-align: center;
    padding: 4px 0 6px;
    font-size: 11px;
    color: #aaa;
    border-top: 1px solid #f0f0f0;
    flex-shrink: 0;
}
.sa-footer a { color: #aaa; }
.sa-footer-disclosure { display: block; margin-top: 2px; }

/* ── Panel open/close animation ──────────────────────────── */
@keyframes sa-panel-in {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes sa-panel-out {
    from { opacity: 1; transform: translateY(0); }
    to   { opacity: 0; transform: translateY(12px); }
}
.sa-chat-panel--animating-in {
    display: flex !important;
    animation: sa-panel-in 0.25s ease forwards;
}
.sa-chat-panel--animating-out {
    display: flex !important;
    animation: sa-panel-out 0.2s ease forwards;
}

/* ── Floating / draggable widget ─────────────────────────── */
.sa-widget--floating {
    position: fixed;
    bottom: calc(var(--sa-offset-bottom, 112px) + env(safe-area-inset-bottom, 0px));
    right: var(--sa-offset-right, 24px);
    z-index: 9997;
}
.sa-widget--floating .sa-toggle-btn {
    position: relative;
    bottom: auto;
    right: auto;
    left: auto;
    cursor: grab;
    user-select: none;
}
.sa-widget--floating .sa-toggle-btn:active { cursor: grabbing; }
.sa-widget--floating .sa-chat-panel {
    position: absolute;
    bottom: 68px;
    right: 0;
    left: auto;
    top: auto;
}

/* ── Guest call-to-action panel (login required) ──────────── */
.sa-cta-body {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    padding: 28px 22px;
    text-align: center;
    color: var(--sa-text);
}
.sa-cta-icon {
    color: var(--sa-primary);
    opacity: 0.85;
}
.sa-cta-text {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
}
.sa-cta-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-top: 4px;
}
.sa-cta-btn {
    display: inline-block;
    padding: 9px 18px;
    border-radius: 6px;
    background: var(--sa-primary);
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    text-decoration: none;
    transition: opacity 0.15s ease;
}
.sa-cta-btn:hover,
.sa-cta-btn:focus {
    opacity: 0.88;
    color: #fff;
    text-decoration: none;
}
.sa-cta-btn--secondary {
    background: transparent;
    color: var(--sa-primary);
    border: 1px solid var(--sa-primary);
}
.sa-cta-btn--secondary:hover,
.sa-cta-btn--secondary:focus {
    color: var(--sa-primary);
}

/* Shipping pilot: quiet, user-opened launcher and visible human contact. */
.sa-widget--shipping-pilot .sa-toggle-btn { width:auto; max-width:calc(100vw - 32px); height:40px; padding:0 16px; border:1px solid #ddd; border-radius:5px; background:#fff; color:#333; box-shadow:0 1px 4px rgba(0,0,0,.08); font-size:13px; transition:none; }
.sa-widget--shipping-pilot .sa-toggle-btn:hover {transform:none; border-color:#999;}
.sa-widget--shipping-pilot .sa-toggle-btn > svg, .sa-widget--shipping-pilot .sa-toggle-icon, .sa-widget--shipping-pilot .sa-badge {display:none!important;}
.sa-widget--shipping-pilot .sa-toggle-label {margin:0; white-space:normal;}
.sa-footer .sa-team-contact {display:block; color:#333; font-size:13px; text-decoration:underline; padding:4px;}
.sa-widget--shipping-pilot .sa-footer-disclosure {color:#666;}

.sa-helpdesk[hidden] { display: none; }
.sa-helpdesk { padding: 12px 16px; border-top: 1px solid #ddd; max-height: 260px; overflow-y: auto; font-size: 13px; }
.sa-helpdesk p { margin: 0 0 8px; }
.sa-helpdesk input[type=email] { display: block; width: 100%; box-sizing: border-box; margin: 6px 0 10px; padding: 8px; border: 1px solid #bbb; border-radius: 4px; }
.sa-helpdesk-consent { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 10px; }
.sa-helpdesk-consent input { flex: 0 0 auto; margin-top: 3px; }
.sa-helpdesk-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.sa-helpdesk-actions button { padding: 7px 10px; border: 1px solid #aaa; border-radius: 4px; cursor: pointer; }
.sa-helpdesk-actions button:disabled { opacity: .6; cursor: default; }
