/* AI Sidebar Styling - OneNote Style (Right Side) */
.ai-sidebar-container {
    position: fixed;
    top: 0;
    right: 0;
    width: 100vw;
    height: 100vh;
    z-index: var(--z-overlay);
    pointer-events: none;
    display: flex;
    justify-content: flex-end;
}

.ai-sidebar {
    width: 400px;
    height: 100%;
    background: var(--bg-surface-solid);
    border-left: 1px solid var(--border-glass);
    box-shadow: var(--shadow-main);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform var(--tr-smooth), width 0.3s ease;
    pointer-events: auto;
    z-index: var(--z-modal);
    position: relative;
}

.ai-sidebar.expanded {
    width: 600px;
}

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

/* CRITICAL: iOS Safari requires pointer-events: auto on the CONTAINER 
   when open. Touch events don't propagate through pe:none parents on WebKit. */
.ai-sidebar-container.open {
    pointer-events: auto;
}

/* Layout Shift - Connects slide to sidebar */
@media (min-width: 1024px) {
    /* body.main-ai-open .zen-workspace {
        width: calc(100vw - 400px);
        transition: width var(--tr-smooth);
    }

    body.main-ai-open.ai-sidebar-expanded .zen-workspace {
        width: calc(100vw - 600px);
    } */
}

/* Expand Toggle Button - Redesigned as a purple circular "robot" handle */
.ai-expand-toggle {
    position: absolute;
    top: calc(50% - 28px);
    transform: translateY(-50%);
    left: -22px;
    /* Overlaps precisely to look "attached" */
    width: 44px;
    height: 44px;
    background: #6366f1;
    /* Purple accent */
    border: 3px solid var(--bg-surface-solid);
    /* "Joint" look */
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: white;
    transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
    box-shadow: -4px 0 15px rgba(0, 0, 0, 0.2);
    opacity: 0;
    pointer-events: none;
    z-index: 10;
}

.ai-sidebar-container.open .ai-expand-toggle {
    opacity: 1;
    pointer-events: auto;
}

.ai-expand-toggle:hover {
    transform: translateY(-50%) scale(1.1) rotate(-10deg);
    background: #4f46e5;
    box-shadow: -6px 0 20px rgba(99, 102, 241, 0.4);
}

/* Side Close Button - Positioned exactly below the robot handle */
.ai-side-close-btn {
    position: absolute;
    top: calc(50% + 28px);
    transform: translateY(-50%);
    left: -22px;
    width: 44px;
    height: 44px;
    background: var(--bg-surface-solid);
    border: 2px solid var(--border-glass);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-muted);
    transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.1);
    opacity: 0;
    pointer-events: none;
    z-index: 10;
}

.ai-sidebar-container.open .ai-side-close-btn {
    opacity: 1;
    pointer-events: auto;
}

.ai-side-close-btn:hover {
    transform: translateY(-50%) scale(1.1);
    background: var(--bg-item-hover);
    color: var(--text-main);
    border-color: var(--text-muted);
}

.ai-expand-toggle i {
    font-size: 1.1rem;
    transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.ai-sidebar.expanded .ai-expand-toggle {
    left: -22px;
}

.ai-sidebar.expanded .ai-expand-toggle i {
    transform: rotate(360deg);
    /* Robot spins on expansion */
}

/* Backdrop for mobile or focus */
/* Backdrop removed to allow interaction with app */
.ai-backdrop {
    display: none;
}

/* Header */
.ai-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-glass);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-surface);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
}

.ai-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 1rem;
    color: var(--text-main);
    flex-shrink: 0;
}

.ai-sidebar:not(.expanded) .ai-title span {
    display: none;
}

.ai-controls {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    justify-content: flex-end;
    overflow: hidden;
}

.ai-badge {
    font-size: 0.75rem;
    background: rgba(99, 102, 241, 0.1);
    color: #6366f1;
    padding: 4px 8px;
    border-radius: 12px;
    border: 1px solid rgba(99, 102, 241, 0.2);
}

.ai-context-select {
    background: var(--bg-item);
    color: var(--text-main);
    border: 1px solid var(--border-glass);
    border-radius: 8px;
    padding: 4px 6px;
    font-size: 0.75rem;
    font-family: inherit;
    cursor: pointer;
    outline: none;
    transition: all 0.2s;
    max-width: 110px;
    text-overflow: ellipsis;
}

#aiLanguageSelect {
    width: 65px;
}

.ai-context-select:hover {
    background: var(--bg-item-hover);
    border-color: #6366f1;
}

.close-ai-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.1rem;
    padding: 4px;
    border-radius: 50%;
    transition: all 0.2s;
}

.close-ai-btn:hover {
    background: var(--bg-item-hover);
    color: var(--text-main);
}

.new-chat-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 6px 8px;
    border-radius: 8px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.new-chat-btn:hover {
    background: var(--bg-item-hover);
    color: #6366f1;
    transform: scale(1.05);
}


/* Body (Chat Area) */
.ai-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: var(--bg-app);
    background-image: radial-gradient(circle at 20% 20%, rgba(99, 102, 241, 0.05) 0%, transparent 50%);
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior: contain;
}

/* AI action chips — touch-friendly sizing & feedback */
.ai-action-chip {
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

@media (pointer: coarse) {
    .ai-action-chip {
        min-height: 44px;
        padding: 12px 8px !important;
        font-size: 0.85rem !important;
    }

    .ai-action-chip:active {
        transform: scale(0.95) !important;
        opacity: 0.85;
    }

    /* Larger touch targets for sidebar buttons */
    .ai-expand-toggle,
    .ai-side-close-btn {
        width: 48px;
        height: 48px;
    }

    .close-ai-btn,
    .new-chat-btn {
        min-width: 44px;
        min-height: 44px;
    }

    #btnSendAi {
        width: 44px;
        height: 44px;
    }
}

/* Messages */
.ai-message {
    display: flex;
    gap: 12px;
    max-width: 90%;
    animation: slideIn 0.3s ease-out;
}

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

.ai-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    flex-shrink: 0;
}

.system .ai-avatar {
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: white;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.user .ai-avatar {
    background: var(--bg-item);
    color: var(--text-muted);
}

.ai-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 0.95rem;
    line-height: 1.5;
    background: var(--bg-surface-solid);
    border: 1px solid var(--border-glass);
    color: var(--text-main);
    box-shadow: var(--shadow-glass);
    /* Prevent horizontal scrolling */
    white-space: pre-wrap;
    /* Preserve newlines but wrap text */
    word-wrap: break-word;
    /* Break long words */
    overflow-wrap: break-word;
    /* Standard property */
    max-width: 100%;
    /* Ensure it doesn't exceed container */
    user-select: text !important;
    -webkit-user-select: text !important;
}

.system .ai-bubble {
    border-top-left-radius: 4px;
}

.user .ai-bubble {
    background: linear-gradient(135deg, #eef2ff, #e0e7ff);
    color: #1e293b;
    border: 1px solid #c7d2fe;
    border-top-right-radius: 4px;
}

/* Markdown Styling inside AI bubbles */
.ai-bubble p {
    margin-bottom: 8px;
}

.ai-bubble p:last-child {
    margin-bottom: 0;
}

.ai-bubble code {
    background: rgba(0, 0, 0, 0.2);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9em;
}

.ai-bubble pre {
    background: #1e1e1e;
    padding: 10px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

.ai-bubble pre code {
    background: transparent;
    padding: 0;
    color: #efefef;
}

/* Message Actions (Copy / Add) */
.ai-msg-actions {
    display: flex;
    gap: 8px;
    margin-top: 6px;
    margin-left: 4px;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.ai-message:hover .ai-msg-actions {
    opacity: 1;
}

.ai-action-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.2s;
}

.ai-action-btn:hover {
    background: var(--bg-item-hover);
    color: var(--text-main);
}

.ai-action-btn i {
    font-size: 0.8rem;
}

.ai-footer {
    padding: 16px 20px;
    background: var(--bg-surface);
    border-top: 1px solid var(--border-glass);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ai-input-container {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    background: var(--bg-item);
    padding: 8px 12px;
    border-radius: 24px;
    border: 1px solid var(--border-glass);
    transition: border-color 0.2s;
}

.ai-input-container:focus-within {
    border-color: #6366f1;
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
}

#aiInput {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.95rem;
    padding: 8px 0;
    resize: none;
    max-height: 100px;
    outline: none;
}

#btnSendAi {
    background: #6366f1;
    color: white;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
}

#btnSendAi:hover:not(:disabled) {
    background: #4f46e5;
    transform: scale(1.05);
}

#btnSendAi:disabled {
    background: var(--bg-item-hover);
    color: var(--text-muted);
    cursor: not-allowed;
}

.ai-disclaimer {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-align: center;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Typing Indicator */
.typing-dots {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

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

.typing-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

/* RTL Support for Arabic */
.ai-sidebar.rtl-mode {
    direction: rtl;
    font-family: 'Cairo', 'Tajawal', sans-serif;
}

.ai-sidebar.rtl-mode .ai-title {
    flex-direction: row-reverse;
}

.ai-sidebar.rtl-mode .ai-message {
    flex-direction: row;
    /* Standard row for RTL means avatar on right if gap is used correctly */
}

.ai-sidebar.rtl-mode .ai-message.user {
    align-self: flex-start;
    /* Opposite of LTR align-self: flex-end */
}

.ai-sidebar.rtl-mode .ai-message.system {
    align-self: flex-end;
}

.ai-sidebar.rtl-mode .ai-bubble {
    text-align: right;
}

.ai-sidebar.rtl-mode .ai-msg-actions {
    margin-right: 4px;
    margin-left: 0;
    justify-content: flex-start;
}

.ai-sidebar.rtl-mode .ai-input-container {
    flex-direction: row-reverse;
}

.ai-sidebar.rtl-mode #aiInput {
    text-align: right;
}

/* ═══════════════════════════════════════════════════════
   AI Floating Toolbar — Appears on canvas after selection
   ═══════════════════════════════════════════════════════ */

.ai-floating-toolbar {
    position: fixed;
    z-index: 99999;
    transform: translate(-50%, 0) scale(0.85);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s cubic-bezier(0.19, 1, 0.22, 1),
                transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    filter: drop-shadow(0 8px 32px rgba(0, 0, 0, 0.18));
}

.ai-floating-toolbar.visible {
    opacity: 1;
    transform: translate(-50%, 0) scale(1);
    pointer-events: auto;
}

/* Buttons Container */
.ai-float-btns {
    display: flex;
    gap: 6px;
    padding: 8px 10px;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.6);
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12),
                0 1px 3px rgba(0, 0, 0, 0.06),
                inset 0 1px 0 rgba(255, 255, 255, 0.8);
}

/* Upward arrow pointer */
.ai-float-btns::before {
    content: '';
    position: absolute;
    top: -7px;
    left: 50%;
    transform: translateX(-50%) rotate(45deg);
    width: 14px;
    height: 14px;
    background: rgba(255, 255, 255, 0.92);
    border-left: 1px solid rgba(255, 255, 255, 0.6);
    border-top: 1px solid rgba(255, 255, 255, 0.6);
    border-radius: 3px 0 0 0;
}

/* Individual button */
.ai-float-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 8px 12px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    background: transparent;
    color: var(--btn-color, #475569);
    font-size: 0.72rem;
    font-weight: 600;
    font-family: 'Inter', 'Outfit', system-ui, sans-serif;
    transition: all 0.2s cubic-bezier(0.19, 1, 0.22, 1);
    position: relative;
    min-width: 56px;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

.ai-float-btn i {
    font-size: 1rem;
    transition: transform 0.2s ease;
}

.ai-float-btn span {
    white-space: nowrap;
    letter-spacing: 0.01em;
}

.ai-float-btn:hover {
    background: var(--btn-gradient, linear-gradient(135deg, #6366f1, #4f46e5));
    color: #fff;
    transform: translateY(-3px) scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.ai-float-btn:hover i {
    transform: scale(1.15);
}

.ai-float-btn:active {
    transform: translateY(-1px) scale(0.97);
}

/* Cancel button special style */
.ai-float-btn[data-action="undo"] {
    color: #94a3b8;
}
.ai-float-btn[data-action="undo"]:hover {
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: #fff;
}

/* Loading overlay */
.ai-float-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 14px 28px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.6);
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12),
                inset 0 1px 0 rgba(255, 255, 255, 0.8);
    font-size: 0.85rem;
    font-weight: 600;
    color: #6366f1;
    font-family: 'Inter', 'Outfit', system-ui, sans-serif;
}

.ai-float-loading i {
    font-size: 1.1rem;
    color: #6366f1;
}

.ai-float-loading .fa-exclamation-triangle {
    color: #ef4444 !important;
}

/* ─── Dark Mode Support ─── */
@media (prefers-color-scheme: dark) {
    .ai-float-btns {
        background: rgba(30, 41, 59, 0.92);
        border-color: rgba(51, 65, 85, 0.6);
        box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3),
                    inset 0 1px 0 rgba(255, 255, 255, 0.06);
    }

    .ai-float-btns::before {
        background: rgba(30, 41, 59, 0.92);
        border-color: rgba(51, 65, 85, 0.6);
    }

    .ai-float-loading {
        background: rgba(30, 41, 59, 0.95);
        border-color: rgba(51, 65, 85, 0.6);
        color: #818cf8;
    }
}

/* ─── Touch Devices ─── */
@media (pointer: coarse) {
    .ai-float-btn {
        min-width: 60px;
        padding: 10px 14px;
        min-height: 52px;
        font-size: 0.75rem;
    }

    .ai-float-btn i {
        font-size: 1.1rem;
    }

    .ai-float-btn:active {
        transform: scale(0.93) !important;
        opacity: 0.85;
    }

    .ai-floating-toolbar {
        filter: drop-shadow(0 6px 20px rgba(0, 0, 0, 0.2));
    }
}

/* ─── AI Sidebar Scrollbar ─── */
.ai-body::-webkit-scrollbar {
    width: 8px;
}

.ai-body::-webkit-scrollbar-track {
    background: rgba(128, 128, 128, 0.06);
    border-radius: 4px;
}

.ai-body::-webkit-scrollbar-thumb {
    background: rgba(128, 128, 128, 0.3);
    border-radius: 4px;
    border: 2px solid transparent;
    background-clip: padding-box;
    min-height: 40px;
}

.ai-body::-webkit-scrollbar-thumb:hover {
    background: rgba(128, 128, 128, 0.5);
    border: 2px solid transparent;
    background-clip: padding-box;
}

/* ─── Small screens: compact layout ─── */
@media (max-width: 600px) {
    .ai-float-btns {
        gap: 4px;
        padding: 6px 8px;
        border-radius: 12px;
        flex-wrap: wrap;
        max-width: 320px;
        justify-content: center;
    }

    .ai-float-btn {
        min-width: 48px;
        padding: 6px 8px;
        font-size: 0.65rem;
    }

    .ai-float-btn i {
        font-size: 0.9rem;
    }

    .ai-float-btn span {
        font-size: 0.6rem;
    }
}

/* ═══════════════════════════════════════════════════════
   AI Undo Result Button — Appears after AI adds content
   ═══════════════════════════════════════════════════════ */

.ai-undo-result-btn {
    position: fixed;
    z-index: 99998;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: 20px;
    cursor: pointer;
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: #fff;
    font-size: 0.8rem;
    font-weight: 700;
    font-family: 'Inter', 'Outfit', system-ui, sans-serif;
    box-shadow: 0 4px 16px rgba(239, 68, 68, 0.35),
                0 1px 3px rgba(0, 0, 0, 0.1);
    transition: all 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
    opacity: 0;
    transform: scale(0.8) translateY(6px);
    pointer-events: none;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
    letter-spacing: 0.02em;
}

.ai-undo-result-btn.visible {
    opacity: 1;
    transform: scale(1) translateY(0);
    pointer-events: auto;
}

.ai-undo-result-btn:hover {
    background: linear-gradient(135deg, #dc2626, #b91c1c);
    transform: scale(1.06) translateY(-2px);
    box-shadow: 0 6px 20px rgba(239, 68, 68, 0.45);
}

.ai-undo-result-btn:active {
    transform: scale(0.95) translateY(0);
}

.ai-undo-result-btn i {
    font-size: 0.85rem;
}

/* Touch devices */
@media (pointer: coarse) {
    .ai-undo-result-btn {
        padding: 10px 20px;
        font-size: 0.85rem;
        min-height: 44px;
    }

    .ai-undo-result-btn:active {
        transform: scale(0.92) !important;
        opacity: 0.85;
    }
}