/* AI Chat Widget — floating assistant on the storefront.
   Phase 2 (docs/AI/AI_plan_01.md §3.13).
   - Bottom-right floating bubble.
   - Slide-out panel with messages + input.
   - RTL support via dir="auto" on message containers.
   - Responsive: collapses to bubble on mobile.
*/

#ai-chat-widget {
    position: fixed;
    bottom: 84px; /* above the floating view-cart bar (.cbar, ~70px tall) and the cookie banner */
    right: 20px;
    /* Above the customer page's floating cart bar (z-index 200) so the chat
       launcher/panel never render under it; below modals/drawers (300+),
       the promo popup (960) and the AR/fullscreen overlays (9999). */
    z-index: 210;
    font-family: -apple-system, BlinkMacSystemFont, 'Google Sans', 'Inter', sans-serif;
}

/* Cookie banner (z-index 99997, ~66px tall fixed bottom bar) — while it's
   visible, lift the whole widget so the bubble sits just above the banner
   instead of under it. Class toggled by the observer in ai-chat-widget.blade.php. */
.ai-cookie-offset#ai-chat-widget {
    bottom: 150px;
}

/* "Order via AI Chat" label above the bubble */
.ai-chat-label {
    position: absolute;
    bottom: 68px; /* sits just above the bubble */
    right: 0;
    background: var(--amber, #f59e0b);
    color: #000;
    font-size: 11px;
    font-weight: 700;
    padding: 3px 10px;
    border-radius: 6px;
    white-space: nowrap;
    letter-spacing: 0.02em;
    pointer-events: none;
    box-shadow: 0 2px 8px rgba(245, 158, 11, 0.3);
}
.ai-chat-label::after {
    content: '';
    position: absolute;
    bottom: -5px;
    right: 18px;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid var(--amber, #f59e0b);
}

/* ─── Bubble ─────────────────────────────────────────────────────────── */
#ai-chat-bubble {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: none;
    background: var(--amber, #f59e0b);
    color: #000;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(245, 158, 11, 0.45);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    outline: none;
}
#ai-chat-bubble:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 28px rgba(245, 158, 11, 0.55);
}
#ai-chat-bubble svg {
    width: 28px;
    height: 28px;
}

/* ─── Panel ──────────────────────────────────────────────────────────── */
#ai-chat-panel {
    display: none;
    position: absolute;
    /* Above legal overlays (cookie banner 99997) when open — the bubble must
       stay below them, but the open panel needs to be usable. */
    z-index: 100000;
    bottom: 80px; /* adjusted for larger bubble */
    right: 0;
    width: 360px;
    max-height: 520px;
    background: var(--bg1, #1a1512);
    border: 1px solid var(--border, #3a322c);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    flex-direction: column;
    overflow: hidden;
    transition: opacity 0.2s ease, transform 0.2s ease;
    opacity: 0;
    transform: translateY(8px);
}
#ai-chat-panel.open {
    display: flex;
    opacity: 1;
    transform: translateY(0);
}

.ai-chat-header {
    padding: 14px 18px;
    background: var(--bg2, #2a2320);
    border-bottom: 1px solid var(--border, #3a322c);
    font-size: 14px;
    font-weight: 600;
    color: var(--t1, #ece7e2);
    display: flex;
    align-items: center;
    gap: 8px;
}
.ai-chat-header::before {
    content: "🤖";
    font-size: 16px;
}
.ai-chat-header > span {
    flex: 1;
}

/* X close button — inline-start edge of the header (left in LTR, right in RTL) */
.ai-chat-close {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    margin-inline-end: 4px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--t3, #8a8a90);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    transition: color 0.15s ease, background 0.15s ease;
}
.ai-chat-close:hover {
    color: var(--t1, #ece7e2);
    background: rgba(255, 255, 255, 0.08);
}

/* ─── Messages ───────────────────────────────────────────────────────── */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    max-height: 320px;
    scrollbar-width: thin;
    scrollbar-color: var(--border, #3a322c) transparent;
}
.ai-chat-messages::-webkit-scrollbar {
    width: 4px;
}
.ai-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}
.ai-chat-messages::-webkit-scrollbar-thumb {
    background: var(--border, #3a322c);
    border-radius: 2px;
}

.ai-msg {
    max-width: 88%;
    padding: 6px 11px;
    border-radius: 10px;
    font-size: 12.5px;
    line-height: 1.45;
    word-wrap: break-word;
}

.ai-msg-user {
    align-self: flex-end;
    background: var(--amber, #f59e0b);
    color: #000;
    border-bottom-right-radius: 4px;
}

.ai-msg-assistant {
    align-self: flex-start;
    background: var(--card, #2a2320);
    color: var(--t1, #ece7e2);
    border: 1px solid var(--border, #3a322c);
    border-bottom-left-radius: 4px;
}

.ai-msg-system {
    align-self: center;
    background: transparent;
    color: var(--t3, #8a8a90);
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 6px;
}

/* Staff reply bubble (human handoff) — green-tinted variant of assistant */
.ai-msg-staff {
    background: rgba(34, 197, 94, 0.10);
    border-color: rgba(34, 197, 94, 0.35);
    color: #ffffff;
}
.ai-msg-staff-name {
    font-size: 10px;
    font-weight: 700;
    color: #4ade80;
    margin-bottom: 3px;
    letter-spacing: 0.02em;
}

/* ─── Suggestion chips (quick replies) ────────────────────────────────── */
.ai-chat-chips {
    display: none;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 14px 4px;
}
.ai-chip {
    padding: 7px 14px;
    border-radius: 16px;
    border: 1px solid var(--amber, #f59e0b);
    background: transparent;
    color: var(--amber, #f59e0b);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease;
    font-family: inherit;
}
.ai-chip:hover {
    background: rgba(245, 158, 11, 0.12);
}

/* ─── Status ─────────────────────────────────────────────────────────── */
.ai-chat-status {
    padding: 4px 14px;
    font-size: 11px;
    color: var(--t3, #8a8a90);
    min-height: 18px;
}

/* ─── Input area ─────────────────────────────────────────────────────── */
.ai-chat-input-area {
    padding: 12px 14px;
    background: var(--bg2, #2a2320);
    border-top: 1px solid var(--border, #3a322c);
    display: flex;
    gap: 8px;
    align-items: center;
}

.ai-chat-input {
    flex: 1;
    padding: 9px 14px;
    background: var(--card, #2a2320);
    border: 1px solid var(--border, #3a322c);
    border-radius: 20px;
    color: var(--t1, #ece7e2);
    font-size: 13px;
    outline: none;
    resize: none;
    font-family: inherit;
    max-height: 100px;
    overflow-y: auto;
    line-height: 1.4;
}
.ai-chat-input:focus {
    border-color: var(--amber, #f59e0b);
}
.ai-chat-input::placeholder {
    color: var(--t3, #8a8a90);
}

.ai-chat-send {
    width: 72px;
    height: 72px;
    border-radius: 18px;
    border: none;
    background: var(--amber, #f59e0b);
    color: #000;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    font-size: 14px;
    flex-shrink: 0;
    outline: none;
    transition: transform 0.15s ease;
}
.ai-chat-send svg {
    width: 28px;
    height: 28px;
}

/* Caption under the icon on the big mic / send buttons */
.ai-chat-btn-label {
    font-size: 11px;
    font-weight: 600;
    line-height: 1.1;
    text-align: center;
    letter-spacing: 0.01em;
    white-space: nowrap;
    pointer-events: none;
}
.ai-chat-send:hover {
    transform: scale(1.05);
}
.ai-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ─── RTL ────────────────────────────────────────────────────────────── */
[dir="rtl"] .ai-msg-user {
    align-self: flex-start;
    border-bottom-right-radius: 14px;
    border-bottom-left-radius: 4px;
}
[dir="rtl"] .ai-msg-assistant {
    align-self: flex-end;
    border-bottom-left-radius: 14px;
    border-bottom-right-radius: 4px;
}

/* ─── Mobile ─────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
    #ai-chat-panel {
        width: calc(100vw - 32px);
        right: -4px;
        max-height: 70vh;
    }
    #ai-chat-widget {
        bottom: 84px; /* stay above the floating view-cart bar on mobile too */
        right: 16px;
    }
    .ai-chat-messages {
        max-height: 40vh;
    }
}

/* ─── Dark-mode ready (uses CSS vars with defaults) ───────────────────── */

/* ─── Hold-to-talk mic (press & hold to record, release to send) ───────── */
.ai-chat-mic {
    width: 72px;
    height: 72px;
    border-radius: 18px;
    border: 2px solid var(--amber, #f59e0b);
    background: transparent;
    color: var(--amber, #f59e0b);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    flex-shrink: 0;
    outline: none;
    /* Keep the hold gesture on the button: no scroll/zoom, no text selection,
       no long-press callout on iOS. */
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}
.ai-chat-mic svg {
    width: 28px;
    height: 28px;
}
.ai-chat-mic:hover {
    transform: scale(1.05);
}
.ai-chat-mic:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}
.ai-chat-mic.recording {
    background: #dc2626;
    border-color: #dc2626;
    color: #fff;
    animation: ai-mic-pulse 1s ease-in-out infinite;
}
@keyframes ai-mic-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.55); }
    50%      { box-shadow: 0 0 0 10px rgba(220, 38, 38, 0); }
}

/* Narrow phones: keep the text box usable next to two 72px buttons */
@media (max-width: 360px) {
    .ai-chat-mic,
    .ai-chat-send {
        width: 64px;
        height: 64px;
    }
    .ai-chat-btn-label {
        font-size: 10px;
    }
}

/* ─── Spoken replies (text-to-speech) ─────────────────────────────────── */
.ai-chat-tts {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    background: var(--bg2, #2a2320);
    border-bottom: 1px solid var(--border, #3a322c);
    flex-shrink: 0;
}
.ai-chat-tts-btn {
    height: 48px;
    min-width: 48px;
    padding: 0 16px;
    border-radius: 24px;
    border: 1px solid var(--border, #3a322c);
    background: var(--card, #2a2320);
    color: var(--t2, #c9c2bb);
    font-size: 15px;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    white-space: nowrap;
}
.ai-chat-tts-btn:hover {
    color: var(--t1, #ece7e2);
    border-color: var(--amber, #f59e0b);
}
.ai-chat-tts-btn svg {
    width: 24px;
    height: 24px;
}
.ai-chat-tts-mute {
    padding: 0;
    width: 48px;
    color: var(--amber, #f59e0b);
    border-color: var(--amber, #f59e0b);
}
.ai-chat-tts-gender {
    flex: 1;
    justify-content: center;
}
.ai-spk-mute { display: none; }
.ai-chat-tts-mute.muted { color: var(--t3, #8a8a90); }
.ai-chat-tts-mute.muted .ai-spk-wave { display: none; }
.ai-chat-tts-mute.muted .ai-spk-mute { display: block; }

.ai-chat-speaking {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 4px 14px 0;
    font-size: 11px;
    color: var(--amber, #f59e0b);
}
.ai-chat-speaking.on {
    display: flex;
}
.ai-chat-speaking.loading {
    opacity: 0.6;
}
.ai-chat-bars {
    display: inline-flex;
    align-items: flex-end;
    gap: 2px;
    height: 12px;
}
.ai-chat-bars i {
    display: block;
    width: 3px;
    height: 4px;
    border-radius: 2px;
    background: currentColor;
    animation: ai-bars 0.9s ease-in-out infinite;
}
.ai-chat-bars i:nth-child(2) { animation-delay: 0.15s; }
.ai-chat-bars i:nth-child(3) { animation-delay: 0.3s; }
.ai-chat-speaking.loading .ai-chat-bars i { animation-duration: 1.8s; }
@keyframes ai-bars {
    0%, 100% { height: 4px; }
    50%      { height: 12px; }
}
.ai-chat-speaking-text {
    flex: 1;
}
.ai-chat-speaking-tap {
    display: none;
    background: var(--amber, #f59e0b);
    border: none;
    color: #000;
    border-radius: 14px;
    padding: 4px 12px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
}
.ai-chat-speaking.tap .ai-chat-speaking-tap {
    display: inline-block;
}
.ai-chat-speaking.tap .ai-chat-bars {
    visibility: hidden;
}
.ai-chat-speaking-stop {
    background: transparent;
    border: 1px solid var(--amber, #f59e0b);
    color: var(--amber, #f59e0b);
    border-radius: 10px;
    padding: 1px 8px;
    font-size: 10px;
    font-weight: 700;
    cursor: pointer;
}
.ai-msg-replay {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin-left: 6px;
    vertical-align: middle;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--t3, #8a8a90);
    cursor: pointer;
    opacity: 0.7;
}
.ai-msg-replay:hover,
.ai-msg-replay.playing {
    color: var(--amber, #f59e0b);
    opacity: 1;
}
.ai-msg-replay svg {
    width: 18px;
    height: 18px;
}
.ai-msg-replay .ai-spk-mute { display: none; }
@media (prefers-reduced-motion: reduce) {
    .ai-chat-bars i { animation: none; height: 8px; }
}

/* ─── Voice layouts (admin-selected, ai_voice_layout) ─────────────────── */

/* Shared: slow amber "breathing" glow on idle controls, red pulse while recording */
@keyframes ai-breathe {
    0%, 100% { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.45); }
    50%      { box-shadow: 0 0 0 10px rgba(245, 158, 11, 0); }
}
@media (prefers-reduced-motion: reduce) {
    .ai-chat-voicebar, .ai-chat-orb, .ai-chat-micbubble, .ai-chat-pad-orb, .ai-chat-mic.recording {
        animation: none !important;
    }
}
.ai-chat-voicebar,
.ai-chat-orb,
.ai-chat-micbubble,
.ai-chat-pad-orb {
    cursor: pointer;
    outline: none;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}
.ai-chat-voicebar:disabled,
.ai-chat-orb:disabled,
.ai-chat-micbubble:disabled,
.ai-chat-pad-orb:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    animation: none;
}
.ai-chat-voicebar.recording,
.ai-chat-orb.recording,
.ai-chat-micbubble.recording,
.ai-chat-pad-orb.recording {
    background: #dc2626 !important;
    border-color: #dc2626 !important;
    color: #fff !important;
    animation: ai-mic-pulse 1s ease-in-out infinite;
}
.ai-voice-timer {
    font-variant-numeric: tabular-nums;
    font-size: 12px;
    font-weight: 600;
    opacity: 0.85;
    min-width: 32px;
    text-align: right;
}

/* bar: full-width "Hold & speak" under the text row */
.ai-chat-voicebar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin: 0 14px 12px;
    height: 56px;
    border-radius: 28px;
    border: none;
    background: linear-gradient(135deg, #fbbf24, var(--amber, #f59e0b));
    color: #000;
    animation: ai-breathe 2.6s ease-in-out infinite;
}
.ai-chat-voicebar .ai-voice-icon svg {
    width: 24px;
    height: 24px;
    display: block;
}
.ai-chat-voicebar .ai-chat-btn-label {
    font-size: 15px;
    font-weight: 700;
}
.ai-chat-voicebar:hover {
    transform: translateY(-1px);
}
.ai-chat-voicebar-wrap {
    background: var(--bg2, #2a2320);
    padding: 0 14px 12px;
}
.ai-chat-voicebar-wrap .ai-chat-voicebar {
    width: 100%;
    margin: 0;
}
.ai-voice-bar .ai-chat-input-area {
    padding-bottom: 8px;
}
.ai-chat-voice-row {
    background: var(--bg2, #2a2320);
}

/* toggle: small mic in the text row ⇄ voice row with the bar */
.ai-chat-modeswitch {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--amber, #f59e0b);
    background: transparent;
    color: var(--amber, #f59e0b);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    outline: none;
}
.ai-chat-modeswitch svg {
    width: 20px;
    height: 20px;
}
.ai-chat-modeswitch:hover {
    background: rgba(245, 158, 11, 0.12);
}
.ai-chat-voice-row {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid var(--border, #3a322c);
}
.ai-chat-voice-row .ai-chat-voicebar {
    flex: 1;
    margin: 0;
}
.ai-voice-mode .ai-chat-voice-row {
    display: flex;
}
.ai-voice-mode .ai-chat-input-area {
    display: none;
}

/* orb: floating circle over the messages, shrinks to the corner while typing */
.ai-chat-orb {
    position: absolute;
    left: 50%;
    bottom: 76px;
    transform: translateX(-50%);
    width: 84px;
    height: 84px;
    border-radius: 50%;
    border: none;
    background: radial-gradient(circle at 35% 30%, #f87171, #dc2626 70%);
    color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
    animation: ai-orb-breathe 2.4s ease-in-out infinite;
    z-index: 2;
}
@keyframes ai-orb-breathe {
    0%, 100% { box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35), 0 0 0 0 rgba(220, 38, 38, 0.45); }
    50%      { box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35), 0 0 0 14px rgba(220, 38, 38, 0); }
}
.ai-chat-orb svg {
    width: 30px;
    height: 30px;
}
.ai-chat-orb .ai-chat-btn-label {
    font-size: 10px;
}
.ai-chat-orb.recording {
    transform: translateX(-50%) scale(1.08);
}
.ai-voice-orb .ai-chat-messages {
    padding-bottom: 96px;
}
.ai-orb-mini .ai-chat-orb {
    left: auto;
    right: 12px;
    bottom: 70px;
    transform: none;
    width: 44px;
    height: 44px;
    opacity: 0.85;
    animation: none;
}
.ai-orb-mini .ai-chat-orb svg {
    width: 20px;
    height: 20px;
}
.ai-orb-mini .ai-chat-orb .ai-chat-btn-label {
    display: none;
}
.ai-orb-mini .ai-chat-messages {
    padding-bottom: 10px;
}

/* bubble: draggable mic inside the chat panel */
.ai-chat-micbubble {
    position: absolute;
    right: 14px;
    bottom: 84px;
    width: 68px;
    height: 68px;
    border-radius: 50%;
    border: none;
    background: radial-gradient(circle at 35% 30%, #fbbf24, var(--amber, #f59e0b) 70%);
    color: #000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
    animation: ai-breathe 2.6s ease-in-out infinite;
    cursor: grab;
    z-index: 2;
}
.ai-chat-micbubble svg {
    width: 26px;
    height: 26px;
}
.ai-chat-micbubble .ai-chat-btn-label {
    font-size: 9px;
}
.ai-chat-micbubble.dragging {
    cursor: grabbing;
    transform: scale(1.08);
    animation: none;
    opacity: 0.9;
}

/* pad: voice-first area instead of the text row */
.ai-chat-pad {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 12px 14px 12px;
    background: var(--bg2, #2a2320);
    border-top: 1px solid var(--border, #3a322c);
}
.ai-chat-pad-orb {
    width: 96px;
    height: 96px;
    border-radius: 50%;
    border: none;
    background: radial-gradient(circle at 35% 30%, #fbbf24, var(--amber, #f59e0b) 70%);
    color: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: ai-breathe 2.6s ease-in-out infinite;
}
.ai-chat-pad-orb svg {
    width: 40px;
    height: 40px;
}
.ai-chat-pad-caption {
    font-size: 14px;
    font-weight: 700;
    color: var(--t1, #ece7e2);
}
.ai-chat-pad .ai-voice-timer {
    min-height: 16px;
    text-align: center;
    color: #f87171;
}
.ai-chat-pad-type {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: transparent;
    border: none;
    color: var(--t3, #8a8a90);
    font-size: 12px;
    cursor: pointer;
    padding: 4px 8px;
}
.ai-chat-pad-type svg {
    width: 16px;
    height: 16px;
}
.ai-chat-pad-type:hover {
    color: var(--t1, #ece7e2);
}
.ai-voice-pad .ai-chat-input-area {
    display: none;
}
.ai-voice-pad.ai-pad-typing .ai-chat-input-area {
    display: flex;
}
.ai-voice-pad.ai-pad-typing .ai-chat-pad {
    display: none;
}
