/**
 * AI Assistant v2 — Intelligence Orb + Side Drawer
 * Glassmorphism design with bento-box output formatting
 */

/* ===== CSS Custom Properties ===== */
:root {
    --ai-z-modal-backdrop: 1040;
    --ai-z-modal: 1050;
    --ai-z-orb: 1060;
    --ai-drawer-width: 420px;
    --ai-accent: #667eea;
    --ai-accent-secondary: #764ba2;
    --ai-glass-bg: rgba(17, 24, 39, 0.92);
    --ai-glass-blur: 4px;
}

/* ===== Backdrop ===== */
#ai-assistant-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(var(--ai-glass-blur));
    -webkit-backdrop-filter: blur(var(--ai-glass-blur));
    z-index: var(--ai-z-modal-backdrop);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

#ai-assistant-backdrop.ai-backdrop-visible {
    opacity: 1;
    pointer-events: auto;
}

/* ===== Intelligence Orb (replaces bubble) ===== */
.ai-orb {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    z-index: var(--ai-z-orb);
    cursor: pointer;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.25s ease;
}

.ai-orb:hover {
    transform: scale(1.1);
}

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

.ai-orb.ai-orb-hidden {
    transform: scale(0);
    opacity: 0;
    pointer-events: none;
}

/* Orb Core */
.ai-orb-core {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 36px;
    height: 36px;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ai-accent) 0%, var(--ai-accent-secondary) 100%);
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.5), 0 0 40px rgba(102, 126, 234, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    z-index: 3;
}

/* Orbiting Rings */
.ai-orb-ring {
    position: absolute;
    top: 50%;
    left: 50%;
    border-radius: 50%;
    border: 1.5px solid rgba(102, 126, 234, 0.35);
    z-index: 2;
}

.ai-orb-ring:nth-child(2) {
    width: 48px;
    height: 48px;
    margin-top: -24px;
    margin-left: -24px;
    animation: aiOrbSpin1 4s linear infinite;
}

.ai-orb-ring:nth-child(3) {
    width: 58px;
    height: 58px;
    margin-top: -29px;
    margin-left: -29px;
    border-color: rgba(118, 75, 162, 0.25);
    animation: aiOrbSpin2 6s linear infinite reverse;
}

@keyframes aiOrbSpin1 {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

@keyframes aiOrbSpin2 {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Outer Glow Pulse */
.ai-orb-glow {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 60px;
    height: 60px;
    margin-top: -30px;
    margin-left: -30px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(102, 126, 234, 0.25) 0%, transparent 70%);
    animation: aiOrbGlow 2.5s ease-in-out infinite;
    z-index: 1;
}

@keyframes aiOrbGlow {
    0%, 100% { transform: scale(1); opacity: 0.5; }
    50% { transform: scale(1.4); opacity: 0; }
}

/* ===== Right Side Drawer (replaces popup window) ===== */
.ai-drawer {
    position: fixed;
    top: 0;
    right: 0;
    width: var(--ai-drawer-width);
    height: 100dvh;
    background: var(--ai-glass-bg);
    backdrop-filter: blur(var(--ai-glass-blur)) saturate(1.4);
    -webkit-backdrop-filter: blur(var(--ai-glass-blur)) saturate(1.4);
    border-left: 1px solid rgba(102, 126, 234, 0.2);
    box-shadow:
        -8px 0 40px rgba(0, 0, 0, 0.4),
        -2px 0 16px rgba(102, 126, 234, 0.1),
        inset 1px 0 0 rgba(255, 255, 255, 0.05);
    display: flex;
    flex-direction: column;
    z-index: var(--ai-z-modal);
    transform: translateX(100%);
    transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
    overflow: hidden;
}

.ai-drawer.ai-drawer-open {
    transform: translateX(0);
}

/* ===== Drawer Header ===== */
.ai-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.12) 0%, rgba(118, 75, 162, 0.12) 100%);
    border-bottom: 1px solid rgba(102, 126, 234, 0.12);
    flex-shrink: 0;
}

.ai-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.ai-header-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--ai-accent) 0%, var(--ai-accent-secondary) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
}

.ai-header-title {
    font-size: 14px;
    font-weight: 600;
    color: #f3f4f6;
    letter-spacing: -0.01em;
}

.ai-header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: #9ca3af;
}

.ai-status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #10b981;
    box-shadow: 0 0 6px rgba(16, 185, 129, 0.5);
}

.ai-status-thinking .ai-status-dot {
    background: #f59e0b;
    box-shadow: 0 0 6px rgba(245, 158, 11, 0.5);
    animation: aiPulse 1s ease-in-out infinite;
}

@keyframes aiPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.ai-header-actions {
    display: flex;
    gap: 4px;
}

.ai-header-btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: none;
    background: rgba(255, 255, 255, 0.05);
    color: #9ca3af;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.ai-header-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #f3f4f6;
}

/* ===== Messages Area ===== */
.ai-drawer-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: rgba(102, 126, 234, 0.3) transparent;
}

.ai-drawer-messages::-webkit-scrollbar {
    width: 4px;
}

.ai-drawer-messages::-webkit-scrollbar-track {
    background: transparent;
}

.ai-drawer-messages::-webkit-scrollbar-thumb {
    background: rgba(102, 126, 234, 0.3);
    border-radius: 4px;
}

/* ===== Messages ===== */
.ai-message {
    display: flex;
    gap: 10px;
    max-width: 95%;
    animation: aiMsgIn 0.3s ease;
}

@keyframes aiMsgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.ai-message-assistant {
    align-self: flex-start;
}

.ai-message-user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ai-message-avatar {
    width: 28px;
    height: 28px;
    min-width: 28px;
    border-radius: 8px;
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.2), rgba(118, 75, 162, 0.2));
    display: flex;
    align-items: center;
    justify-content: center;
    color: #667eea;
    flex-shrink: 0;
    margin-top: 2px;
}

.ai-message-content {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 13px;
    line-height: 1.6;
    color: #e5e7eb;
    word-break: break-word;
}

.ai-message-assistant .ai-message-content {
    background: rgba(31, 41, 55, 0.8);
    border: 1px solid rgba(55, 65, 81, 0.5);
    border-top-left-radius: 4px;
}

.ai-message-user .ai-message-content {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.25), rgba(118, 75, 162, 0.25));
    border: 1px solid rgba(102, 126, 234, 0.2);
    border-top-right-radius: 4px;
    color: #f3f4f6;
}

.ai-message-content strong {
    color: #a5b4fc;
    font-weight: 600;
}

.ai-message-content ul {
    margin: 6px 0;
    padding-left: 16px;
}

.ai-message-content li {
    margin-bottom: 4px;
    color: #d1d5db;
}

/* ===== Typing Indicator ===== */
.ai-typing-indicator {
    display: inline-flex;
    gap: 4px;
    padding: 4px 0;
}

.ai-typing-indicator span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #667eea;
    animation: aiTypingDot 1.4s ease-in-out infinite;
}

.ai-typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes aiTypingDot {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-4px); }
}

/* ===== Quick Actions ===== */
.ai-quick-actions {
    padding: 8px 20px 4px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    flex-shrink: 0;
}

.ai-quick-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 20px;
    border: 1px solid rgba(102, 126, 234, 0.2);
    background: rgba(102, 126, 234, 0.08);
    color: #a5b4fc;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.ai-quick-btn:hover {
    background: rgba(102, 126, 234, 0.18);
    border-color: rgba(102, 126, 234, 0.35);
    color: #c7d2fe;
    transform: translateY(-1px);
}

/* ===== Input Area ===== */
.ai-drawer-input {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 20px 16px;
    border-top: 1px solid rgba(55, 65, 81, 0.5);
    background: rgba(17, 24, 39, 0.6);
    flex-shrink: 0;
}

.ai-drawer-input textarea {
    flex: 1;
    background: rgba(31, 41, 55, 0.6);
    border: 1px solid rgba(55, 65, 81, 0.5);
    border-radius: 12px;
    padding: 10px 14px;
    color: #f3f4f6;
    font-size: 13px;
    font-family: inherit;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 100px;
    transition: border-color 0.2s ease;
}

.ai-drawer-input textarea::placeholder {
    color: #6b7280;
}

.ai-drawer-input textarea:focus {
    border-color: rgba(102, 126, 234, 0.5);
}

.ai-send-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 12px;
    border: none;
    background: linear-gradient(135deg, var(--ai-accent) 0%, var(--ai-accent-secondary) 100%);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.ai-send-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
}

.ai-send-btn:active {
    transform: translateY(0);
}

.ai-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ===== Bento Cards ===== */
.ai-bento-card {
    background: rgba(17, 24, 39, 0.7);
    border: 1px solid rgba(55, 65, 81, 0.4);
    border-radius: 10px;
    padding: 10px 12px;
    margin: 6px 0;
    border-left: 3px solid var(--ai-accent);
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.ai-bento-card .ai-bento-icon {
    font-size: 14px;
    line-height: 1;
    margin-top: 2px;
    flex-shrink: 0;
}

.ai-bento-card .ai-bento-body {
    flex: 1;
    min-width: 0;
}

.ai-bento-card .ai-bento-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #9ca3af;
    margin-bottom: 2px;
}

.ai-bento-card .ai-bento-value {
    font-size: 13px;
    font-weight: 500;
    color: #e5e7eb;
}

/* Bento type colors */
.ai-bento-card[data-bento="price"] {
    border-left-color: #818cf8;
}

.ai-bento-card[data-bento="levels"] {
    border-left-color: #34d399;
}

.ai-bento-card[data-bento="metric"] {
    border-left-color: #fbbf24;
}

.ai-bento-card[data-bento="setup"] {
    border-left-color: #a78bfa;
}

/* ===== AI Search Suggestion Card (for news page) ===== */
.ai-search-suggestion {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
    border: 1px solid rgba(102, 126, 234, 0.2);
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    margin: 16px 0;
}

.ai-search-suggestion p {
    color: #d1d5db;
    font-size: 14px;
    margin-bottom: 12px;
    line-height: 1.5;
}

.ai-search-suggestion button {
    background: linear-gradient(135deg, var(--ai-accent) 0%, var(--ai-accent-secondary) 100%);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 8px 20px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.ai-search-suggestion button:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
}

/* ===== Action Chips (Suggested Questions) ===== */
.ai-action-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 4px 0 8px;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.ai-action-chips.ai-chips-dismiss {
    opacity: 0;
    transform: translateY(-6px);
    pointer-events: none;
}

.ai-action-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border-radius: 20px;
    border: 1px solid rgba(99, 102, 241, 0.2);
    background: rgba(99, 102, 241, 0.1);
    color: #c7d2fe;
    font-size: 12.5px;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s ease;
    animation: aiChipFadeIn 0.4s ease both;
}

.ai-action-chip:hover {
    background: rgba(99, 102, 241, 0.22);
    border-color: rgba(99, 102, 241, 0.4);
    color: #e0e7ff;
    box-shadow: 0 0 12px rgba(99, 102, 241, 0.15);
    transform: translateY(-1px);
}

.ai-action-chip:active {
    transform: translateY(0);
}

.ai-chip-emoji {
    font-size: 14px;
    line-height: 1;
}

.ai-chip-label {
    line-height: 1.2;
}

@keyframes aiChipFadeIn {
    from {
        opacity: 0;
        transform: translateY(8px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Light theme chips */
[data-theme="light"] .ai-action-chip {
    background: rgba(99, 102, 241, 0.08);
    border-color: rgba(99, 102, 241, 0.18);
    color: #4338ca;
}

[data-theme="light"] .ai-action-chip:hover {
    background: rgba(99, 102, 241, 0.15);
    border-color: rgba(99, 102, 241, 0.3);
    color: #3730a3;
    box-shadow: 0 0 12px rgba(99, 102, 241, 0.1);
}

/* ===== Mobile ===== */
@media (max-width: 768px) {
    .ai-drawer {
        width: 100%;
        border-left: none;
        border-radius: 0;
    }

    .ai-orb {
        bottom: 16px;
        right: 16px;
        width: 52px;
        height: 52px;
    }

    .ai-orb-core {
        width: 30px;
        height: 30px;
    }

    .ai-orb-ring:nth-child(2) {
        width: 42px;
        height: 42px;
        margin-top: -21px;
        margin-left: -21px;
    }

    .ai-orb-ring:nth-child(3) {
        width: 50px;
        height: 50px;
        margin-top: -25px;
        margin-left: -25px;
    }

    .ai-orb-glow {
        width: 52px;
        height: 52px;
        margin-top: -26px;
        margin-left: -26px;
    }

    #ai-assistant-backdrop {
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
    }

    .ai-quick-actions {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 8px;
    }

    .ai-action-chips {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 6px;
        scrollbar-width: none;
    }

    .ai-action-chips::-webkit-scrollbar {
        display: none;
    }
}

/* ===== RTL Support ===== */
html[dir="rtl"] .ai-orb {
    right: auto;
    left: 24px;
}

html[dir="rtl"] .ai-drawer {
    right: auto;
    left: 0;
    border-left: none;
    border-right: 1px solid rgba(102, 126, 234, 0.2);
    transform: translateX(-100%);
    box-shadow:
        8px 0 40px rgba(0, 0, 0, 0.4),
        2px 0 16px rgba(102, 126, 234, 0.1),
        inset -1px 0 0 rgba(255, 255, 255, 0.05);
}

html[dir="rtl"] .ai-drawer.ai-drawer-open {
    transform: translateX(0);
}

html[dir="rtl"] .ai-message-user {
    flex-direction: row;
}

html[dir="rtl"] .ai-message-assistant .ai-message-content {
    border-top-left-radius: 12px;
    border-top-right-radius: 4px;
}

html[dir="rtl"] .ai-message-user .ai-message-content {
    border-top-right-radius: 12px;
    border-top-left-radius: 4px;
}

html[dir="rtl"] .ai-message-content ul {
    padding-left: 0;
    padding-right: 16px;
}

html[dir="rtl"] .ai-bento-card {
    border-left: none;
    border-right: 3px solid var(--ai-accent);
}

html[dir="rtl"] .ai-bento-card[data-bento="price"] {
    border-right-color: #818cf8;
}

html[dir="rtl"] .ai-bento-card[data-bento="levels"] {
    border-right-color: #34d399;
}

html[dir="rtl"] .ai-bento-card[data-bento="metric"] {
    border-right-color: #fbbf24;
}

html[dir="rtl"] .ai-bento-card[data-bento="setup"] {
    border-right-color: #a78bfa;
}

@media (max-width: 768px) {
    html[dir="rtl"] .ai-drawer {
        border-right: none;
    }

    html[dir="rtl"] .ai-orb {
        left: 16px;
        right: auto;
    }
}

/* ===== Light Theme ===== */
[data-theme="light"] .ai-drawer {
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(var(--ai-glass-blur)) saturate(1.2);
    -webkit-backdrop-filter: blur(var(--ai-glass-blur)) saturate(1.2);
    border-left-color: rgba(102, 126, 234, 0.15);
}

[data-theme="light"] .ai-drawer-header {
    border-bottom-color: rgba(102, 126, 234, 0.1);
}

[data-theme="light"] .ai-header-title {
    color: #111827;
}

[data-theme="light"] .ai-message-content {
    color: #374151;
}

[data-theme="light"] .ai-message-assistant .ai-message-content {
    background: rgba(243, 244, 246, 0.9);
    border-color: rgba(229, 231, 235, 0.8);
}

[data-theme="light"] .ai-message-content strong {
    color: #4f46e5;
}

[data-theme="light"] .ai-drawer-input textarea {
    background: rgba(243, 244, 246, 0.8);
    border-color: rgba(209, 213, 219, 0.5);
    color: #111827;
}

[data-theme="light"] .ai-drawer-input {
    background: rgba(249, 250, 251, 0.8);
    border-top-color: rgba(229, 231, 235, 0.5);
}

[data-theme="light"] .ai-header-btn {
    color: #6b7280;
}

[data-theme="light"] .ai-header-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #374151;
}

[data-theme="light"] .ai-bento-card {
    background: rgba(243, 244, 246, 0.8);
    border-color: rgba(229, 231, 235, 0.6);
}

[data-theme="light"] .ai-bento-card .ai-bento-value {
    color: #111827;
}

[data-theme="light"] .ai-search-suggestion {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
    border-color: rgba(102, 126, 234, 0.15);
}

[data-theme="light"] .ai-search-suggestion p {
    color: #4b5563;
}

/* RTL + Light theme */
html[dir="rtl"] [data-theme="light"] .ai-drawer {
    border-left-color: transparent;
    border-right-color: rgba(102, 126, 234, 0.15);
}

/* ===== Body scroll lock when drawer open ===== */
body.ai-assistant-active {
    overflow: hidden;
}
