/* AI Chat System Styles */

/* Floating Bubble */
.ai-bubble {
    position: fixed;
    bottom: 80px; /* Above the bottom nav bar */
    right: 20px;
    width: 56px;
    height: 56px;
    background: var(--primary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    border: 0;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.20);
    cursor: pointer;
    z-index: 1000;
    transition: transform 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94),
                box-shadow 0.35s ease,
                background-color 0.35s ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none; /* For dragging */
    isolation: isolate;
    animation: ai-bubble-breathe 9s ease-in-out infinite;
}

.ai-bubble::before {
    content: '';
    position: absolute;
    inset: -5px;
    border-radius: inherit;
    pointer-events: none;
    z-index: -1;
}

.ai-bubble::before {
    border: 1px solid color-mix(in srgb, var(--primary) 42%, transparent);
    opacity: 0;
    transform: scale(0.82);
    animation: ai-bubble-ring 9s ease-out infinite;
}

.ai-bubble:hover {
    transform: translateY(-2px) scale(1.05);
    box-shadow: 0 10px 22px rgba(0, 0, 0, 0.25);
}

.ai-bubble:active {
    transform: scale(0.95);
}

.ai-bubble-avatar {
    position: relative;
    z-index: 1;
    display: block;
    width: 34px;
    height: 34px;
}

.ai-bubble-avatar img {
    position: relative;
    display: block;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.ai-bubble-avatar .ai-bubble-eyes {
    position: absolute;
    inset: 0;
    transform: translate(var(--eye-x, 0), var(--eye-y, 0));
    transition: transform 280ms cubic-bezier(0.22, 1, 0.36, 1);
}

.ai-bubble-avatar .ai-bubble-mouth {
    position: absolute;
    inset: 0;
    transform-origin: center;
    transition: transform 360ms cubic-bezier(0.22, 1, 0.36, 1);
}

.ai-bubble-mouth.mouth-skeptical-mirrored { transform: scaleX(-1); }
.ai-bubble-mouth.mouth-flat-tight { transform: scaleX(0.78); }
.ai-bubble-mouth.mouth-flat-wide { transform: scaleX(1.10); }

@keyframes ai-bubble-breathe {
    0%, 74%, 100% {
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.20);
    }
    82% {
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.24);
    }
}

@keyframes ai-bubble-ring {
    0%, 70%, 100% {
        opacity: 0;
        transform: scale(0.82);
    }
    78% {
        opacity: 0.5;
        transform: scale(1);
    }
    90% {
        opacity: 0;
        transform: scale(1.28);
    }
}

@media (prefers-reduced-motion: reduce) {
    .ai-bubble,
    .ai-bubble::before {
        animation: none;
    }
}

body.clarity .ai-bubble {
    background: #050505;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.22);
}

body.clarity .ai-bubble::before {
    border-color: rgba(0, 0, 0, 0.28);
}

body.cyberpunk .ai-bubble {
    background: var(--primary);
    box-shadow:
        0 0 18px color-mix(in srgb, var(--primary) 36%, transparent),
        0 4px 12px rgba(0, 0, 0, 0.42);
}

body.cyberpunk .ai-bubble::before {
    border-color: var(--glass-border-strong);
}

/* Chat Window */
.ai-chat-window {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: calc(100% - 71px);
    height: calc(100dvh - 71px);
    border-top-left-radius: 24px;
    border-top-right-radius: 24px;
    box-shadow: 0 -5px 25px rgba(0, 0, 0, 0.3);
    background: var(--bg-primary);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    pointer-events: none;
}

/* Triangle pointing up to the bubble */
.ai-chat-window::before {
    content: '';
    position: absolute;
    top: -11px; /* Overlap by 1px to make it seamless */
    right: 31px; /* Aligns exactly with the center of the bubble */
    width: 0;
    height: 0;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
    border-bottom: 12px solid var(--glass-surface);
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
    z-index: 2001;
}

.ai-chat-window.open::before {
    opacity: 1;
    transition-delay: 0.3s;
}

.ai-chat-window.open {
    transform: translateY(0);
    pointer-events: auto;
}

/* Switch to a floating panel only when the app enters its desktop layout. */
@media (min-width: 1024px) {
    .ai-chat-window {
        top: auto;
        bottom: 90px;
        left: auto;
        right: 20px;
        width: 380px;
        height: 600px;
        max-height: calc(100vh - 120px);
        border-radius: 20px;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
        border: 1px solid var(--glass-border);
        transform: translateY(20px) scale(0.95);
        opacity: 0;
    }
    
    .ai-chat-window.open {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

/* Header */
.ai-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--glass-surface);
    backdrop-filter: var(--glass-blur);
    border-bottom: 1px solid var(--glass-border);
    border-top-left-radius: 24px;
    border-top-right-radius: 24px;
    flex-shrink: 0;
}

.ai-chat-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 16px;
    color: var(--primary);
}

#aiEnergyBar {
    background: var(--primary);
}

.ai-chat-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    transition: background 0.2s;
}

.ai-chat-close:hover {
    background: var(--glass-edge);
    color: var(--danger);
}

/* Messages Area */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.ai-msg {
    display: flex;
    max-width: 85%;
    animation: fadeIn 0.3s ease;
}

.ai-msg.ai-user {
    align-self: flex-end;
}

.ai-msg.ai-sys,
.ai-msg.ai-ai {
    align-self: flex-start;
}

.ai-msg-bubble {
    padding: 12px 16px;
    border-radius: 20px;
    font-size: 16.5px;
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
}

.ai-user .ai-msg-bubble {
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.ai-sys .ai-msg-bubble,
.ai-ai .ai-msg-bubble {
    background: var(--glass-surface);
    border: 1px solid var(--glass-border);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

/* Input Area */
.ai-chat-input-area {
    padding: 16px 20px;
    background: var(--glass-surface);
    backdrop-filter: var(--glass-blur);
    border-top: 1px solid var(--glass-border);
    flex-shrink: 0;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

.ai-chat-limit-info {
    font-size: 11px;
    color: var(--text-muted);
    text-align: right;
    margin-bottom: 6px;
}

#ai-chat-input {
    flex: 1;
    background: var(--bg-secondary);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    padding: 12px 16px;
    color: var(--text-primary);
    font-size: 16px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}

#ai-chat-input:focus {
    border-color: var(--primary);
}

#ai-chat-input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

#ai-chat-send {
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: transform 0.1s, opacity 0.2s;
}

#ai-chat-send:active {
    transform: scale(0.9);
}

#ai-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.ai-typing {
    display: flex;
    gap: 4px;
    padding: 14px 18px;
    background: var(--glass-surface);
    border-radius: 20px;
    border-bottom-left-radius: 4px;
    width: fit-content;
    border: 1px solid var(--glass-border);
}

.ai-typing-dot {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}

.ai-typing-dot:nth-child(1) { animation-delay: -0.32s; }
.ai-typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes typingBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* Add option in settings to hide AI */
.setting-ai-toggle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-top: 1px solid var(--glass-border);
}

.ai-bubble.chat-open {
    top: 10px;
    bottom: auto !important;
    right: 15px !important;
    left: auto !important;
    z-index: 2001;
    transform: scale(0.95) !important;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
    transition: all 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94) !important;
}

/* Chat Backdrop for Blur effect */
.ai-chat-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 1999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.35s ease;
}

.ai-chat-backdrop.open {
    opacity: 1;
    pointer-events: auto;
}

/* Desktop chat is a larger floating workspace; the mobile bottom sheet is unchanged. */
@media (min-width: 1024px) {
    .ai-bubble {
        right: 32px;
        bottom: 32px;
    }

    .ai-chat-window {
        right: 32px;
        bottom: 32px;
        width: min(540px, calc(100vw - 304px));
        height: min(720px, calc(100dvh - 64px));
        max-height: calc(100dvh - 64px);
        border-radius: 24px;
        background: var(--bg-secondary);
        border: 1px solid var(--glass-border-strong);
        box-shadow: 0 20px 64px rgba(0, 0, 0, 0.34);
        transform: translateY(24px) scale(0.98);
        transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.2s ease;
        will-change: transform, opacity;
    }

    .ai-chat-window.open {
        transform: translateY(0) scale(1);
    }

    .ai-chat-window::before {
        display: none;
    }

    .ai-bubble.chat-open {
        top: auto !important;
        right: 32px !important;
        bottom: 32px !important;
        opacity: 0;
        pointer-events: none;
        transform: scale(0.85) !important;
    }

    .ai-chat-header {
        padding: 20px 24px;
        border-top-left-radius: 24px;
        border-top-right-radius: 24px;
    }

    .ai-chat-title {
        font-size: 18px;
    }

    .ai-chat-title > svg,
    .ai-chat-title > i {
        width: 24px !important;
        height: 24px !important;
    }

    .ai-chat-close {
        width: 38px;
        height: 38px;
    }

    .ai-chat-messages {
        padding: 24px;
        gap: 18px;
    }

    .ai-msg {
        max-width: 80%;
    }

    .ai-msg-bubble {
        padding: 14px 18px;
        font-size: 17px;
        line-height: 1.6;
    }

    .ai-chat-input-area {
        padding: 18px 24px 22px;
        border-bottom-left-radius: 24px;
        border-bottom-right-radius: 24px;
    }

    #ai-chat-input {
        padding: 14px 18px;
        font-size: 16px;
    }

    #ai-chat-send {
        width: 48px;
        height: 48px;
    }

    .ai-chat-backdrop,
    .ai-chat-backdrop.open {
        background: transparent;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }
}

.ai-msg-bubble input, .ai-msg-bubble select {
    font-family: inherit;
}

.ai-action-info-box {
    margin-bottom: 12px;
    padding: 10px;
    background: var(--bg-secondary);
    border-radius: 8px;
    font-size: 13px;
    color: var(--text-secondary);
    border: 1px solid var(--border);
}
.ai-action-info-box div { margin-bottom: 4px; }
.ai-action-info-box div:last-child { margin-bottom: 0; }
.ai-action-info-box strong {
    color: var(--text-muted);
    font-weight: normal;
    margin-right: 4px;
}
.ai-action-info-box .info-val {
    color: var(--text-primary);
    font-weight: 500;
}
.ai-action-info-box .info-val.danger { color: var(--danger); }
.ai-action-info-box .info-val.primary { color: var(--primary); }

