/* ── AI Agent Slide-out Panel ────────────────────────────────────────────── */

/*
  Desktop: the panel slides out from the right edge of .container.
  It is positioned via JS by measuring .container's right edge each time
  it opens (see toggleAiSidebar). The .ai-sidebar wrapper is fixed so it
  can overflow .container without clipping, but the card is aligned to the
  container right edge.
*/

/* The outer wrapper — no backdrop on desktop */
.ai-sidebar {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9800;
    pointer-events: none; /* let clicks pass through to notes underneath */
    /* No background overlay on desktop */
}

.ai-sidebar.open {
    display: block;
}

/* The actual panel card — independent panel next to .container */
.ai-modal-card {
    pointer-events: all;
    position: fixed;
    /* left, top set by JS; height is fixed on desktop */
    width: 320px;
    height: min(840px, calc(100dvh - 16px));
    background: var(--container-bg, #fff);
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Start off-screen to the right, slide in */
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
                box-shadow 0.28s ease;
    border-radius: 20px;
    left: 0;
    right: auto;
}

.ai-sidebar.open .ai-modal-card {
    transform: translateX(0);
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.dark-mode .ai-modal-card {
    border: 1px solid rgba(255,255,255,0.06);
    box-shadow: 0 0 40px rgba(0,0,0,0.5);
}

/* Header */
.ai-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 14px 12px;
    border-bottom: 1px solid var(--border-color, rgba(128,128,128,0.15));
    flex-shrink: 0;
    gap: 8px;
    position: relative;
}

/* Subtle accent line under header */
.ai-sidebar-header::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg,
        transparent,
        var(--btn-accent, #f472b6) 30%,
        var(--btn-accent, #f472b6) 70%,
        transparent);
    opacity: 0.3;
    pointer-events: none;
}

.ai-sidebar-title {
    font-size: 13.5px;
    font-weight: 700;
    color: var(--text-color, #222);
    display: flex;
    align-items: flex-end;
    gap: 7px;
    letter-spacing: 0.01em;
}

.ai-sidebar-title svg {
    color: var(--btn-accent, #f472b6);
}

.ai-sidebar-close {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    color: var(--muted-text, #888);
    padding: 4px 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    transition: color 0.15s, background 0.15s;
}
.ai-sidebar-close:hover {
    color: var(--text-color, #222);
    background: rgba(128,128,128,0.1);
}

.ai-overlay { display: none !important; }

/* Draggable header — desktop only */
@media (min-width: 601px) {
    .ai-sidebar-header {
        cursor: grab;
    }
    .ai-modal-card.ai-dragging .ai-sidebar-header {
        cursor: grabbing;
    }
    .ai-modal-card.ai-dragging,
    .ai-modal-card.ai-resizing {
        transition: none !important;
        user-select: none;
    }
    /* Resize handles */
    .ai-resize-handle {
        position: absolute;
        z-index: 10;
        transition: background 0.15s;
    }
    .ai-resize-e {
        right: 0; top: 22px; bottom: 22px;
        width: 6px;
        cursor: ew-resize;
        border-radius: 0 20px 20px 0;
    }
    .ai-resize-s {
        bottom: 0; left: 22px; right: 22px;
        height: 6px;
        cursor: ns-resize;
    }
    .ai-resize-se {
        right: 0; bottom: 0;
        width: 18px; height: 18px;
        cursor: nwse-resize;
        border-radius: 0 0 20px 0;
    }
    .ai-resize-handle:hover {
        background: rgba(128,128,128,0.08);
    }
}
/* Hide handles on mobile */
@media (max-width: 600px) {
    .ai-resize-handle { display: none; }
}
/* Suppress text selection on the page while dragging the panel */
body.ai-panel-dragging {
    user-select: none;
    -webkit-user-select: none;
}

/* Message scroll area */
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 14px 14px 8px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    mask-image: linear-gradient(to bottom, transparent 0%, black 20px);
    -webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 20px);
}

.ai-messages::-webkit-scrollbar { width: 4px; }
.ai-messages::-webkit-scrollbar-track { background: transparent; }
.ai-messages::-webkit-scrollbar-thumb {
    background: rgba(128,128,128,0.2);
    border-radius: 4px;
}

/* Chat bubbles */
.ai-bubble {
    max-width: 88%;
    padding: 9px 13px;
    border-radius: 16px;
    font-size: 13px;
    line-height: 1.55;
    word-wrap: break-word;
    animation: ai-bubble-in 0.18s ease;
}

@keyframes ai-bubble-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.ai-bubble-user {
    align-self: flex-end;
    background: var(--btn-accent, #f472b6);
    color: #fff;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 8px rgba(244,114,182,0.2);
}

.ai-bubble-agent {
    align-self: flex-start;
    background: var(--container-bg, #f5f5f5);
    color: var(--text-color, #222);
    border: 1px solid var(--border-color, rgba(128,128,128,0.15));
    border-bottom-left-radius: 4px;
}

.dark-mode .ai-bubble-agent {
    background: rgba(255,255,255,0.06);
    border-color: rgba(255,255,255,0.08);
}

/* Agent message row: sparkles avatar + bubble */
.ai-msg-row-agent {
    align-self: flex-start;
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    gap: 7px;
    max-width: 100%;
}
.ai-msg-row-agent .ai-bubble-agent {
    align-self: auto;
}
.ai-agent-avatar {
    width: 26px;
    height: 26px;
    min-width: 26px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--btn-accent, #f472b6);
    border-radius: 50%;
    margin-bottom: 2px;
}
.ai-agent-avatar svg {
    width: 13px;
    height: 13px;
    stroke: #fff;
    color: #fff;
}

.ai-bubble code {
    background: rgba(0,0,0,0.07);
    border-radius: 4px;
    padding: 1px 5px;
    font-family: monospace;
    font-size: 12px;
}
.dark-mode .ai-bubble code {
    background: rgba(255,255,255,0.1);
}

/* Typing indicator */
.ai-typing {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 12px 14px;
    min-width: 60px;
}
.ai-typing span {
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--muted-text, #888);
    animation: ai-dot-bounce 1.2s infinite ease-in-out;
}
.ai-typing span:nth-child(1) { animation-delay: 0s; }
.ai-typing span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes ai-dot-bounce {
    0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
    40%           { transform: scale(1.1); opacity: 1; }
}

/* Input bar */
.ai-input-bar {
    display: flex;
    align-items: flex-end;
    padding: 10px 12px 14px;
    border-top: 1px solid var(--border-color, rgba(128,128,128,0.15));
    gap: 8px;
    flex-shrink: 0;
    background: var(--popup-bg, #fff);
}

.ai-input {
    flex: 1;
    resize: none;
    padding: 9px 13px;
    border-radius: 22px;
    border: 1px solid var(--border-color, rgba(128,128,128,0.25));
    background: var(--container-bg, #f8f8f8);
    color: var(--text-color, #222);
    font-size: 13px;
    font-family: inherit;
    line-height: 1.5;
    outline: none;
    max-height: 120px;
    overflow-y: auto;
    transition: border-color 0.18s, box-shadow 0.18s;
    scrollbar-width: none;
}
.ai-input::-webkit-scrollbar { display: none; }
.ai-input:focus {
    border-color: var(--btn-accent, #f472b6);
    box-shadow: 0 0 0 3px rgba(244,114,182,0.12);
}
.ai-input::placeholder {
    color: var(--muted-text, #aaa);
    font-size: 13px;
}

/* Send & Mic buttons */
.ai-send-btn,
.ai-mic-btn {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.ai-send-btn {
    background: var(--btn-accent, #f472b6);
    color: #fff;
    box-shadow: 0 2px 8px rgba(244,114,182,0.3);
}
.ai-send-btn:hover {
    filter: brightness(1.1);
    transform: scale(1.06);
}
.ai-send-btn svg { width: 16px; height: 16px; }

.ai-mic-btn {
    background: rgba(128,128,128,0.1);
    color: var(--muted-text, #888);
}
.ai-mic-btn:hover {
    background: rgba(128,128,128,0.18);
    color: var(--text-color, #222);
}
.ai-mic-btn svg { width: 16px; height: 16px; }

.ai-mic-btn.listening {
    background: rgba(239,68,68,0.12);
    color: #ef4444;
    animation: ai-mic-pulse 1s infinite;
}
@keyframes ai-mic-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.3); }
    50%       { box-shadow: 0 0 0 7px rgba(239,68,68,0); }
}

/* Suggested prompt chips */
.ai-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 12px 10px;
    flex-shrink: 0;
}

.ai-chip {
    font-size: 12px;
    padding: 5px 12px;
    border-radius: 20px;
    border: 1px solid var(--border-color, rgba(128,128,128,0.25));
    background: var(--container-bg, #f5f5f5);
    color: var(--text-color, #222);
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
    white-space: nowrap;
}
.ai-chip:hover {
    border-color: var(--btn-accent, #f472b6);
    background: rgba(244,114,182,0.07);
    color: var(--btn-accent, #f472b6);
}

/* ── Mobile: bottom sheet (≤600px) — unchanged behaviour ─────────────────── */
@media (max-width: 600px) {
    .ai-sidebar {
        pointer-events: all;
        background: var(--popup-overlay, rgba(0,0,0,0.55));
        display: none;
        position: fixed;
        inset: 0;
        align-items: flex-end;
        padding: 0;
        z-index: 9800;
    }
    .ai-sidebar.open {
        display: flex;
    }
    .ai-modal-card {
        position: static;
        transform: translateY(100%);
        transition: transform 0.26s cubic-bezier(0.34, 1.1, 0.64, 1);
        max-width: 100%;
        width: 100%;
        height: 82dvh;
        border-radius: 20px 20px 0 0;
        border-left: none;
        border-top: 1px solid var(--border-color, rgba(128,128,128,0.15));
        box-shadow: 0 -8px 32px rgba(0,0,0,0.15);
        right: auto;
    }
    .ai-sidebar.open .ai-modal-card {
        transform: translateY(0);
    }
}
