/* AI Chat Drawer */

.ai-toggle-btn {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    z-index: 999;
    width: 3.2rem;
    height: 3.2rem;
    border-radius: 50%;
    border: none;
    background: var(--sf-blue);
    color: #fff;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    margin: 0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    line-height: 1;
}

.ai-toggle-btn:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(59, 130, 246, 0.35);
}

.ai-toggle-btn.ai-processing {
    animation: ai-pulse 1.5s ease-in-out infinite;
}

@keyframes ai-pulse {
    0%, 100% { box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4); }
    50% { box-shadow: 0 4px 20px rgba(59, 130, 246, 0.6); }
}

/*
 * Drawer width: CSS default clamp(400px, 40vw, 640px). Optional inline width from
 * localStorage is applied in ai-chat.js and clamped to [340px, 80% window.innerWidth].
 */
.ai-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    /* Comfortable for markdown + code; ~520px at typical viewports */
    width: clamp(400px, 40vw, 640px);
    max-width: 100vw;
    z-index: 1000;
    background: var(--pico-card-background-color, #1a1f2e);
    border-left: 1px solid var(--pico-muted-border-color, #374151);
    display: flex;
    flex-direction: row;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.3);
}

/* Left-edge resize strip (desktop); drag adjusts width; hidden on mobile via media query */
.ai-drawer-resize {
    flex-shrink: 0;
    width: 6px;
    margin: 0;
    padding: 0;
    border: none;
    cursor: col-resize;
    background: transparent;
    touch-action: none;
    align-self: stretch;
}

.ai-drawer-resize:hover,
.ai-drawer-resize:focus-visible {
    background: rgba(255, 255, 255, 0.08);
}

.ai-drawer-resize:focus {
    outline: none;
}

.ai-drawer-resize:focus-visible {
    outline: 2px solid var(--sf-blue);
    outline-offset: -2px;
}

/* Column stack: header, messages, form */
.ai-drawer-inner {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* During width drag: no transition so width tracks pointer; open/close uses transform only when not resizing */
.ai-drawer.ai-drawer--resizing {
    transition: none;
}

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

/* Header */
.ai-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--pico-muted-border-color, #374151);
    flex-shrink: 0;
    font-weight: 600;
}

.ai-close-btn {
    background: none;
    border: none;
    color: var(--pico-color, #c5c8d0);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    margin: 0;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    line-height: 1;
}

.ai-close-btn:hover {
    background: rgba(255, 255, 255, 0.08);
}

/* Messages area */
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    scroll-behavior: smooth;
}

/* Message bubbles */
.ai-message {
    display: flex;
    max-width: 90%;
}

.ai-message.user {
    align-self: flex-end;
    max-width: 85%;
}

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

.ai-message-content {
    padding: 0.75rem 1rem;
    border-radius: 12px;
    line-height: 1.55;
    font-size: 0.92rem;
    word-break: break-word;
    overflow-wrap: break-word;
}

.ai-message.user .ai-message-content {
    background: var(--sf-blue);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.ai-message.assistant .ai-message-content {
    background: rgba(255, 255, 255, 0.06);
    border-bottom-left-radius: 4px;
}

/* Markdown content inside AI messages */
.ai-message-content p {
    margin: 0 0 0.5em;
}

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

.ai-message-content ul,
.ai-message-content ol {
    margin: 0.4em 0;
    padding-left: 1.4em;
}

.ai-message-content li {
    margin-bottom: 0.2em;
}

.ai-message-content code {
    font-family: var(--pico-font-family-monospace, "JetBrains Mono", monospace);
    font-size: 0.85em;
    padding: 0.15em 0.35em;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.1);
}

.ai-message.user .ai-message-content code {
    background: rgba(255, 255, 255, 0.2);
}

.ai-message-content pre {
    margin: 0.5em 0;
    padding: 0.75rem;
    border-radius: 6px;
    background: #0d1117;
    overflow-x: hidden;
    max-width: 100%;
    border: 1px solid rgba(255, 255, 255, 0.08);
}

/* Wrap long lines (e.g. comments) instead of horizontal scroll inside the bubble */
.ai-message-content pre code {
    background: none;
    padding: 0;
    font-size: 0.84em;
    color: #e6edf3;
    white-space: pre-wrap;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.ai-message-content blockquote {
    margin: 0.5em 0;
    padding: 0.3em 0.75em;
    border-left: 3px solid var(--sf-blue);
    color: var(--sf-muted);
}

.ai-message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.5em 0;
    font-size: 0.88em;
}

.ai-message-content th,
.ai-message-content td {
    padding: 0.3em 0.5em;
    border: 1px solid var(--pico-muted-border-color, #374151);
    text-align: left;
}

.ai-message-content th {
    background: rgba(255, 255, 255, 0.05);
    font-weight: 600;
}

.ai-message-content h1,
.ai-message-content h2,
.ai-message-content h3,
.ai-message-content h4 {
    margin: 0.6em 0 0.3em;
    font-size: 1em;
    font-weight: 700;
}

.ai-message-content hr {
    margin: 0.6em 0;
    border-color: var(--pico-muted-border-color, #374151);
}

.ai-message-content strong {
    font-weight: 700;
}

/* Input form */
.ai-input-form {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--pico-muted-border-color, #374151);
    flex-shrink: 0;
    margin: 0;
}

.ai-input-form textarea {
    flex: 1;
    resize: none;
    min-height: 2.4rem;
    max-height: 8rem;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    border: 1px solid var(--pico-muted-border-color, #374151);
    background: var(--pico-background-color, #11191f);
    color: var(--pico-color, #c5c8d0);
    font-family: inherit;
    font-size: 0.9rem;
    line-height: 1.45;
    margin: 0;
    overflow-y: auto;
}

.ai-input-form textarea:focus {
    border-color: var(--sf-blue);
    outline: none;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.ai-input-form button[type="submit"] {
    width: 2.5rem;
    height: 2.5rem;
    padding: 0;
    margin: 0;
    border: none;
    border-radius: 8px;
    background: var(--sf-blue);
    color: #fff;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.15s ease;
}

.ai-input-form button[type="submit"]:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Loading / typing indicator */
.ai-loading .ai-message-content {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.75rem 1.25rem;
}

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

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

@keyframes ai-dots {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40% { opacity: 1; transform: scale(1.1); }
}

/* Error message styling */
.ai-message.error .ai-message-content {
    background: rgba(239, 68, 68, 0.12);
    color: var(--sf-red);
    border: 1px solid rgba(239, 68, 68, 0.25);
}

/* AI Proposal actions (Phase 2) */
.ai-proposal-actions {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    padding: 0.5rem 0.75rem;
    margin: 0 0 0.5rem;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.06);
    max-width: 90%;
}

.ai-proposal-summary {
    font-size: 0.85rem;
    color: var(--pico-color, #c5c8d0);
}

.ai-proposal-buttons {
    display: flex;
    gap: 0.5rem;
}

.ai-proposal-actions button {
    padding: 0.4rem 0.9rem;
    border: none;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    font-weight: 500;
    transition: opacity 0.15s ease;
    margin: 0;
    width: auto;
    height: auto;
    display: inline-flex;
    align-items: center;
    background: none;
}

.ai-proposal-actions button:first-child {
    background: var(--sf-green, #22c55e);
    color: #fff;
}

.ai-proposal-actions button:first-child:hover {
    opacity: 0.85;
}

.ai-proposal-actions button:last-child {
    background: rgba(255, 255, 255, 0.08);
    color: var(--pico-color, #c5c8d0);
}

.ai-proposal-actions button:last-child:hover {
    background: rgba(255, 255, 255, 0.14);
}

.ai-proposal-applied,
.ai-proposal-rejected,
.ai-proposal-resolved,
.ai-proposal-error {
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    font-size: 0.85rem;
    margin: 0 0 0.5rem;
    display: inline-block;
}

.ai-proposal-applied {
    background: rgba(34, 197, 94, 0.12);
    color: var(--sf-green, #22c55e);
    border: 1px solid rgba(34, 197, 94, 0.25);
}

.ai-proposal-rejected {
    background: rgba(255, 255, 255, 0.04);
    color: var(--sf-muted, #6b7280);
    border: 1px solid rgba(255, 255, 255, 0.08);
}

.ai-proposal-resolved {
    background: rgba(255, 255, 255, 0.04);
    color: var(--sf-muted, #6b7280);
}

.ai-proposal-error {
    background: rgba(239, 68, 68, 0.12);
    color: var(--sf-red, #ef4444);
    border: 1px solid rgba(239, 68, 68, 0.25);
}

/* Mobile: fullscreen drawer — no resize, no fixed width from JS */
@media (max-width: 768px) {
    .ai-drawer {
        width: 100vw;
        border-left: none;
    }

    .ai-drawer-resize {
        display: none;
    }

    .ai-toggle-btn {
        bottom: 1rem;
        right: 1rem;
        width: 2.8rem;
        height: 2.8rem;
        font-size: 1.3rem;
    }
}
