/* ==========================================================================
   AI GENERATOR — Bouton IA, badge de statut et drawer de génération
   --------------------------------------------------------------------------
   Module 100 % front-end (WebLLM via WebGPU). Réservé au desktop : le
   bouton et le badge sont masqués sous 1025 px de large. Les couleurs
   reposent sur les variables de theme.css (mode clair / sombre).
   ========================================================================== */

/* ----- Bouton IA dans le panel-header de l'éditeur ----- */

#btn-ai-generator {
    position: relative;
    color: var(--accent);
}

#btn-ai-generator:hover {
    background: var(--accent-soft);
    color: var(--accent-hover);
}

#btn-ai-generator.is-active {
    background: var(--accent-soft);
    color: var(--accent-hover);
}

/* ----- Badge de statut du modèle ----- */

.ai-status-pill {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: 2px var(--sp-2);
    border-radius: 999px;
    font-size: var(--fs-xs);
    font-weight: 500;
    line-height: 1.4;
    border: 1px solid transparent;
    white-space: nowrap;
    user-select: none;
}

.ai-status-pill > i {
    font-size: 0.85em;
    opacity: 0.9;
}

.ai-status-idle {
    color: var(--text-tertiary);
    background: var(--bg-tertiary);
    border-color: var(--border-subtle);
}

.ai-status-loading,
.ai-status-streaming {
    color: var(--warning);
    background: rgba(245, 158, 11, 0.1);
    border-color: rgba(245, 158, 11, 0.25);
}

.ai-status-loading > i,
.ai-status-streaming > i {
    animation: aiPulse 1.4s ease-in-out infinite;
}

.ai-status-ready {
    color: var(--success);
    background: rgba(16, 185, 129, 0.1);
    border-color: rgba(16, 185, 129, 0.25);
}

.ai-status-error {
    color: var(--danger);
    background: var(--danger-soft);
    border-color: rgba(239, 68, 68, 0.25);
}

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

/* Sous 1025 px (tablette / mobile) : la fonctionnalité IA est désactivée */
@media (max-width: 1024px) {
    #btn-ai-generator,
    #ai-status-badge {
        display: none !important;
    }
}

/* ----- Backdrop ----- */

.ai-drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1090;
    background: rgba(16, 24, 40, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 220ms ease;
}

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

/* ----- Drawer (panneau latéral droit) ----- */

.ai-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(440px, 96vw);
    z-index: 1100;
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border-left: 1px solid var(--border-subtle);
    box-shadow: -16px 0 40px -8px rgba(16, 24, 40, 0.18);
    transform: translateX(100%);
    transition: transform 280ms cubic-bezier(0.16, 1, 0.3, 1);
    overflow: hidden;
}

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

[data-theme="dark"] .ai-drawer {
    box-shadow: -16px 0 40px -8px rgba(0, 0, 0, 0.5);
}

/* ----- Header ----- */

.ai-drawer-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-tertiary);
}

.ai-drawer-header h3 {
    font-size: var(--fs-md);
    font-weight: 600;
    color: var(--text-primary);
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    margin: 0;
}

.ai-drawer-header h3 i {
    color: var(--accent);
}

.ai-icon-btn {
    width: 30px;
    height: 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-tertiary);
    cursor: pointer;
    transition: all var(--t-fast);
}

.ai-icon-btn:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}

/* ----- Body (scrollable) ----- */

.ai-drawer-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: var(--sp-4);
    display: flex;
    flex-direction: column;
    gap: var(--sp-4);
}

/* ----- Bandeau d'avertissement ----- */

.ai-warning {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-2);
    padding: var(--sp-3);
    border-radius: var(--radius-md);
    background: var(--accent-soft);
    border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
    color: var(--text-secondary);
    font-size: var(--fs-xs);
    line-height: 1.5;
}

.ai-warning > i {
    color: var(--accent);
    margin-top: 2px;
    flex-shrink: 0;
}

.ai-warning strong { color: var(--text-primary); }

/* ----- Champs ----- */

.ai-field {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
}

.ai-label {
    font-size: var(--fs-xs);
    font-weight: 600;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-family: var(--font-mono);
}

.ai-input {
    width: 100%;
    padding: var(--sp-2) var(--sp-3);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: var(--fs-sm);
    font-family: var(--font-sans);
    transition: border-color var(--t-fast), box-shadow var(--t-fast);
}

.ai-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent);
}

.ai-textarea {
    min-height: 110px;
    resize: vertical;
    line-height: 1.5;
    font-family: inherit;
}

select.ai-input {
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: var(--fs-xs);
}

/* ----- Type de diagramme (pills) ----- */

.ai-type-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-2);
}

.ai-type-btn {
    padding: var(--sp-1) var(--sp-3);
    border-radius: 999px;
    font-size: var(--fs-xs);
    font-family: var(--font-mono);
    font-weight: 500;
    border: 1px solid var(--border-subtle);
    background: var(--bg-tertiary);
    color: var(--text-tertiary);
    cursor: pointer;
    transition: all var(--t-fast);
}

.ai-type-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.ai-type-btn.active {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent);
    font-weight: 600;
}

/* ----- Chips d'exemples ----- */

.ai-chips {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
}

.ai-chip {
    text-align: left;
    padding: var(--sp-2) var(--sp-3);
    border: 1px dashed var(--border-default);
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-tertiary);
    font-size: var(--fs-xs);
    line-height: 1.4;
    cursor: pointer;
    transition: all var(--t-fast);
}

.ai-chip:hover {
    border-style: solid;
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--accent);
}

/* ----- Barre de progression ----- */

.ai-progress {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    padding: var(--sp-3);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
}

.ai-progress-track {
    height: 4px;
    border-radius: 999px;
    background: var(--border-subtle);
    overflow: hidden;
}

.ai-progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent);
    border-radius: 999px;
    transition: width 280ms ease;
}

.ai-progress-text {
    font-size: var(--fs-xs);
    font-family: var(--font-mono);
    color: var(--text-tertiary);
    line-height: 1.4;
    word-break: break-word;
}

/* ----- Sortie de code en streaming ----- */

.ai-code-section {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
}

.ai-code-output {
    margin: 0;
    padding: var(--sp-3);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.55;
    max-height: 220px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
}

.ai-code-output.streaming::after {
    content: '▌';
    color: var(--accent);
    animation: aiBlink 0.7s step-end infinite;
}

/* Indicateur de phase de raisonnement (modèles Qwen3, DeepSeek-R1…) */
.ai-code-output.streaming[data-thinking="true"]::before {
    content: 'Réflexion en cours…';
    display: block;
    color: var(--text-muted);
    font-style: italic;
    font-family: var(--font-sans, sans-serif);
    font-size: 11px;
}

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

/* ----- Footer (boutons d'action) ----- */

.ai-drawer-footer {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--sp-2);
    padding: var(--sp-3) var(--sp-4);
    border-top: 1px solid var(--border-subtle);
    background: var(--bg-tertiary);
}

.ai-drawer-footer .btn {
    min-height: 36px;
}

#ai-generate-btn {
    flex: 1;
    justify-content: center;
}

/* ----- Actions groupées dans le header du drawer ----- */

.ai-drawer-header-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
}

/* ----- Modal de gestion du cache ----- */

.ai-cache-overlay {
    position: fixed;
    inset: 0;
    z-index: 1200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--sp-4);
    background: rgba(16, 24, 40, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 200ms ease;
}

.ai-cache-overlay.open {
    opacity: 1;
    pointer-events: auto;
}

.ai-cache-box {
    width: min(480px, 100%);
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    box-shadow: 0 24px 56px -8px rgba(16, 24, 40, 0.28);
    overflow: hidden;
    transform: translateY(8px) scale(0.98);
    transition: transform 220ms cubic-bezier(0.16, 1, 0.3, 1);
    max-height: calc(100vh - 4rem);
}

.ai-cache-overlay.open .ai-cache-box {
    transform: translateY(0) scale(1);
}

[data-theme="dark"] .ai-cache-box {
    box-shadow: 0 24px 56px -8px rgba(0, 0, 0, 0.55);
}

.ai-cache-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-tertiary);
}

.ai-cache-header h4 {
    margin: 0;
    font-size: var(--fs-sm);
    font-weight: 600;
    color: var(--text-primary);
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
}

.ai-cache-header h4 i {
    color: var(--accent);
}

.ai-cache-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: var(--sp-2) 0;
}

/* ----- Ligne d'un modèle ----- */

.ai-cache-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--border-subtle);
    transition: background var(--t-fast);
}

.ai-cache-row:last-child { border-bottom: none; }

.ai-cache-row:hover { background: var(--surface-hover); }

.ai-cache-row-info {
    display: flex;
    flex-direction: column;
    gap: var(--sp-1);
    min-width: 0;
}

.ai-cache-model-name {
    font-size: var(--fs-sm);
    font-weight: 500;
    color: var(--text-primary);
    font-family: var(--font-mono);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-cache-tags {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--sp-1);
}

.ai-cache-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 1px 8px;
    border-radius: 999px;
    font-size: var(--fs-xs);
    font-weight: 500;
    border: 1px solid transparent;
    white-space: nowrap;
}

.ai-cache-tag i { font-size: 0.8em; }

.ai-cache-tag-cached {
    color: var(--success);
    background: rgba(16, 185, 129, 0.1);
    border-color: rgba(16, 185, 129, 0.25);
}

.ai-cache-tag-none {
    color: var(--text-tertiary);
    background: var(--bg-tertiary);
    border-color: var(--border-subtle);
}

.ai-cache-tag-active {
    color: var(--accent);
    background: var(--accent-soft);
    border-color: color-mix(in srgb, var(--accent) 30%, transparent);
}

/* ----- Bouton supprimer (dans la ligne) ----- */

.ai-cache-delete-btn {
    flex-shrink: 0;
    color: var(--text-tertiary);
}

.ai-cache-delete-btn:not([disabled]):hover {
    background: var(--danger-soft);
    color: var(--danger);
    border-color: rgba(239, 68, 68, 0.25);
}

.ai-cache-delete-btn[disabled] {
    opacity: 0.3;
    cursor: not-allowed;
}

/* ----- Placeholder (chargement / erreur) ----- */

.ai-cache-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: var(--sp-6) var(--sp-4);
    font-size: var(--fs-sm);
    color: var(--text-tertiary);
    font-family: var(--font-mono);
}

.ai-cache-error { color: var(--danger); }

/* ----- Footer ----- */

.ai-cache-footer {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-2);
    padding: var(--sp-3) var(--sp-4);
    border-top: 1px solid var(--border-subtle);
    background: var(--bg-tertiary);
}

/* ----- Bouton ghost danger ----- */

.btn-danger-ghost {
    color: var(--danger);
    background: transparent;
    border-color: transparent;
}

.btn-danger-ghost:hover:not([disabled]) {
    background: var(--danger-soft);
    border-color: rgba(239, 68, 68, 0.3);
}

.btn-danger-ghost[disabled] {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-sm {
    min-height: 30px;
    padding: var(--sp-1) var(--sp-3);
    font-size: var(--fs-xs);
}
