/* ============================================================
   SPACE AI - FLOATING CHAT WIDGET
   ============================================================ */

:root {
    --ai-cyan: #06b6d4;
    --ai-orange: #f97316;
    --ai-bg-panel: rgba(15, 23, 42, 0.85); /* Sfondo scuro semi-trasparente */
    --ai-border: rgba(255, 255, 255, 0.1);
}

#spaceAiFloating {
    position: fixed;
    bottom: 110px;
    right: 32px;
    z-index: 10000;
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

/* --- FAB (Floating Action Button) --- */
#spaceAiFab {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: 1px solid rgba(6, 182, 212, 0.3);
    background: #0f172a;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    z-index: 10001;
}

/* Glow pulsante dietro il FAB */
.fab-glow {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: var(--ai-cyan);
    filter: blur(20px);
    opacity: 0.3;
    z-index: -1;
    animation: aiBreathGlow 3s infinite alternate ease-in-out;
}

#spaceAiFab img {
    width: 44px; 
    height: 44px;
    object-fit: contain;
    transition: transform 0.3s ease;
}

#spaceAiFab:hover {
    transform: translateY(-4px) scale(1.05);
    border-color: var(--ai-cyan);
    box-shadow: 0 15px 35px rgba(6, 182, 212, 0.4);
}
#spaceAiFab:hover img { transform: scale(1.1); }
#spaceAiFab:active { transform: scale(0.95); }

@keyframes aiBreathGlow {
    0% { transform: scale(0.9); opacity: 0.2; }
    100% { transform: scale(1.4); opacity: 0.5; }
}

/* --- PANNELLO CHAT --- */
#spaceAiPanel {
    position: absolute;
    bottom: 80px; /* Spazio dal FAB */
    right: 0;
    width: 360px;
    height: 550px;
    max-height: calc(100vh - 120px);
    background: var(--ai-bg-panel);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 24px;
    border: 1px solid var(--ai-border);
    box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255,255,255,0.05);
    display: none;
    flex-direction: column;
    overflow: hidden;
    transform-origin: bottom right;
}

#spaceAiPanel.open {
    display: flex;
    animation: aiPanelOpen 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes aiPanelOpen {
    from { opacity: 0; transform: scale(0.9) translateY(20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

/* --- HEADER PANNELLO --- */
.spaceAiHeader {
    padding: 16px 20px;
    background: rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid var(--ai-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.spaceAiBrand { display: flex; align-items: center; gap: 12px; }

.ai-avatar {
    position: relative;
    width: 36px;
    height: 36px;
    background: #1e293b;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(255,255,255,0.1);
}
.ai-avatar img { width: 24px; }

/* Puntino verde Online */
.ai-status-dot {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 10px;
    height: 10px;
    background: #10b981;
    border-radius: 50%;
    border: 2px solid #0f172a;
}

.ai-title b { font-size: 15px; color: #fff; letter-spacing: -0.3px; display: block; line-height: 1.2; }
.ai-title small { font-size: 11px; color: #10b981; font-weight: 600; }

#spaceAiClose {
    background: transparent;
    border: none; 
    color: #94a3b8;
    width: 32px; height: 32px; 
    border-radius: 50%;
    cursor: pointer; 
    display: flex; align-items: center; justify-content: center;
    transition: 0.2s;
}
#spaceAiClose:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }

/* --- AREA MESSAGGI --- */
#spaceAiMessages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scrollbar-width: none;
}
#spaceAiMessages::-webkit-scrollbar { display: none; }

/* Stile Base Messaggio */
.spaceAiMsg {
    padding: 12px 16px;
    font-size: 14px;
    line-height: 1.5;
    max-width: 85%;
    word-wrap: break-word;
    animation: msgSlideIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

/* Messaggio Bot (A Sinistra) */
.spaceAiMsg.bot {
    background: rgba(30, 41, 59, 0.8);
    color: #f8fafc;
    align-self: flex-start;
    border: 1px solid var(--ai-border);
    border-radius: 16px 16px 16px 4px; /* Angolo inferiore sinistro acuto */
    transform-origin: bottom left;
}

/* Messaggio Utente (A Destra) */
.spaceAiMsg.user {
    background: linear-gradient(135deg, var(--ai-orange), #ea580c);
    color: #fff;
    align-self: flex-end;
    border-radius: 16px 16px 4px 16px; /* Angolo inferiore destro acuto */
    box-shadow: 0 4px 15px rgba(249, 115, 22, 0.2);
    transform-origin: bottom right;
}

/* Animazione di scrittura "Thinking" */
.spaceAiMsg.thinking {
    background: transparent;
    border: none;
    color: var(--ai-cyan);
    font-style: italic;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 4px;
}
.spaceAiMsg.thinking::after {
    content: '';
    width: 6px; height: 6px;
    background: var(--ai-cyan);
    border-radius: 50%;
    animation: typingBlink 1s infinite alternate;
}

@keyframes msgSlideIn {
    from { opacity: 0; transform: translateY(10px) scale(0.95); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes typingBlink {
    0% { opacity: 0.3; transform: scale(0.8); }
    100% { opacity: 1; transform: scale(1.2); box-shadow: 0 0 8px var(--ai-cyan); }
}

/* --- BARRA DI INPUT --- */
.spaceAiInputBar {
    padding: 16px;
    background: rgba(0, 0, 0, 0.3);
    border-top: 1px solid var(--ai-border);
    display: flex; 
    gap: 12px; 
    align-items: flex-end;
}

#spaceAiInput {
    flex: 1;
    background: rgba(15, 23, 42, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 16px;
    padding: 12px 16px;
    color: #fff;
    font-size: 14px;
    resize: none;
    outline: none;
    transition: border-color 0.2s;
    min-height: 44px;
    max-height: 120px;
    font-family: inherit;
}
#spaceAiInput::placeholder { color: #64748b; }
#spaceAiInput:focus { border-color: var(--ai-cyan); box-shadow: 0 0 0 2px rgba(6, 182, 212, 0.15); }

#spaceAiSend {
    background: var(--ai-cyan);
    color: #020617;
    border: none;
    width: 44px;
    height: 44px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    flex-shrink: 0;
}
#spaceAiSend:hover {
    background: #0891b2;
    transform: translateY(-2px) scale(1.05);
    box-shadow: 0 6px 15px rgba(6, 182, 212, 0.4);
}
#spaceAiSend:active { transform: scale(0.95); }

/* --- RESPONSIVE MOBILE --- */
@media (max-width: 768px) {
    #spaceAiFloating { 
        bottom: 110px; 
        right: 20px; 
    }
    #spaceAiPanel { 
        width: calc(100vw - 40px); 
        height: 60vh; 
        bottom: 80px;
        border-radius: 20px;
    }
    .spaceAiHeader { padding: 12px 16px; }
    #spaceAiInput { font-size: 15px; /* Previene lo zoom su iOS */ } 
}
/* ============================================================
   SPACE AI - MENU MANAGER PRO PREVIEW
   ============================================================ */

.spaceAiMsg.bot:has(.spaceAiActionBox) {
    max-width: 96%;
    width: 96%;
}

.spaceAiActionBox {
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.spaceAiPlan {
    width: 100%;
    border-radius: 18px;
    border: 1px solid rgba(255,255,255,0.10);
    background:
        radial-gradient(circle at top right, rgba(6,182,212,.12), transparent 34%),
        rgba(2, 6, 23, .42);
    padding: 12px;
}

.spaceAiPlanTitle {
    color: #fff;
    font-size: 13px;
    font-weight: 950;
    letter-spacing: .04em;
    text-transform: uppercase;
    margin-bottom: 10px;
}

.spaceAiPlanList {
    margin: 0;
    padding-left: 18px;
    color: #cbd5e1;
    font-size: 12px;
    line-height: 1.5;
}

.spaceAiPreviewSummary {
    margin: -2px 0 12px;
    padding: 9px 10px;
    border-radius: 12px;
    background: rgba(255,255,255,.04);
    border: 1px solid rgba(255,255,255,.06);
    color: #94a3b8;
    font-size: 11px;
    font-weight: 800;
}

.spaceAiPreviewCard {
    border-radius: 15px;
    border: 1px solid rgba(34,197,94,.16);
    background: rgba(15, 23, 42, .64);
    padding: 11px;
    margin-top: 10px;
}

.spaceAiPreviewCard.risk-medium {
    border-color: rgba(245,158,11,.24);
    background:
        radial-gradient(circle at top right, rgba(245,158,11,.10), transparent 40%),
        rgba(15, 23, 42, .64);
}

.spaceAiPreviewCard.risk-high,
.spaceAiPreviewCard.is-blocked {
    border-color: rgba(239,68,68,.28);
    background:
        radial-gradient(circle at top right, rgba(239,68,68,.12), transparent 40%),
        rgba(15, 23, 42, .64);
}

.spaceAiPreviewTop {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: flex-start;
}

.spaceAiPreviewMain {
    min-width: 0;
}

.spaceAiPreviewTitle {
    color: #f8fafc;
    font-size: 13px;
    font-weight: 900;
    line-height: 1.25;
}

.spaceAiPreviewTarget {
    margin-top: 4px;
    color: #94a3b8;
    font-size: 11px;
    font-weight: 700;
    overflow-wrap: anywhere;
}

.spaceAiPreviewBadge {
    flex: 0 0 auto;
    padding: 5px 8px;
    border-radius: 999px;
    background: rgba(6,182,212,.10);
    border: 1px solid rgba(6,182,212,.18);
    color: #67e8f9;
    font-size: 9px;
    font-weight: 950;
    text-transform: uppercase;
    letter-spacing: .04em;
}

.spaceAiPreviewCols {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    margin-top: 10px;
}

.spaceAiPreviewKVBox {
    padding: 9px;
    border-radius: 12px;
    background: rgba(0,0,0,.18);
    border: 1px solid rgba(255,255,255,.05);
}

.spaceAiPreviewKVTitle {
    color: #64748b;
    font-size: 9px;
    font-weight: 950;
    text-transform: uppercase;
    letter-spacing: .08em;
    margin-bottom: 6px;
}

.spaceAiPreviewKV {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 4px 0;
    border-top: 1px solid rgba(255,255,255,.04);
    color: #94a3b8;
    font-size: 11px;
}

.spaceAiPreviewKV:first-of-type {
    border-top: 0;
}

.spaceAiPreviewKV span {
    color: #64748b;
}

.spaceAiPreviewKV b {
    color: #e2e8f0;
    text-align: right;
    font-weight: 800;
    overflow-wrap: anywhere;
}

.spaceAiPreviewWarnings {
    margin-top: 10px;
    padding: 9px 10px;
    border-radius: 12px;
    border: 1px solid rgba(245,158,11,.22);
    background: rgba(245,158,11,.10);
    color: #fbbf24;
    font-size: 11px;
    font-weight: 800;
    line-height: 1.35;
}

.spaceAiActionBtns {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.spaceAiApplyBtn,
.spaceAiCancelBtn {
    border: 0;
    border-radius: 13px;
    padding: 11px 14px;
    font-size: 12px;
    font-weight: 950;
    cursor: pointer;
    transition: .18s ease;
}

.spaceAiApplyBtn {
    background: var(--ai-cyan);
    color: #020617;
    box-shadow: 0 8px 20px rgba(6,182,212,.20);
}

.spaceAiApplyBtn:hover:not(:disabled) {
    transform: translateY(-1px);
    filter: brightness(1.08);
}

.spaceAiApplyBtn:disabled {
    opacity: .55;
    cursor: not-allowed;
}

.spaceAiCancelBtn {
    background: rgba(255,255,255,.06);
    color: #cbd5e1;
    border: 1px solid rgba(255,255,255,.08);
}

.spaceAiCancelBtn:hover:not(:disabled) {
    background: rgba(255,255,255,.10);
}

@media (min-width: 520px) {
    .spaceAiPreviewCols {
        grid-template-columns: 1fr 1fr;
    }
}

.spaceAiAfterApply {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.spaceAiHistoryLink {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 9px 12px;
  border-radius: 12px;
  border: 1px solid rgba(34, 211, 238, .24);
  background: rgba(34, 211, 238, .08);
  color: #67e8f9;
  font-size: 12px;
  font-weight: 900;
  text-decoration: none;
}

.spaceAiHistoryLink:hover {
  background: rgba(34, 211, 238, .14);
}

/* ============================================================
   SPACE AI - PARTIAL APPLY / SCARTA MODIFICHE
   ============================================================ */

.spaceAiPreviewGuide {
    margin: -4px 0 10px;
    padding: 9px 10px;
    border-radius: 12px;
    background: rgba(6, 182, 212, .07);
    border: 1px solid rgba(6, 182, 212, .12);
    color: #a5f3fc;
    font-size: 11px;
    line-height: 1.35;
    font-weight: 800;
}

.spaceAiPreviewRight {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 7px;
    flex: 0 0 auto;
}

.spaceAiPreviewDiscard {
    border: 1px solid rgba(255, 255, 255, .10);
    background: rgba(255, 255, 255, .055);
    color: #cbd5e1;
    border-radius: 999px;
    padding: 5px 8px;
    font-size: 9px;
    font-weight: 950;
    text-transform: uppercase;
    letter-spacing: .04em;
    cursor: pointer;
    transition: .16s ease;
}

.spaceAiPreviewDiscard:hover {
    background: rgba(255, 255, 255, .10);
    color: #fff;
}

.spaceAiPreviewCard.is-discarded {
    opacity: .48;
    filter: grayscale(.18);
    border-style: dashed;
}

.spaceAiPreviewCard.is-discarded .spaceAiPreviewTitle,
.spaceAiPreviewCard.is-discarded .spaceAiPreviewTarget,
.spaceAiPreviewCard.is-discarded .spaceAiPreviewCols,
.spaceAiPreviewCard.is-discarded .spaceAiPreviewWarnings {
    text-decoration: line-through;
    text-decoration-thickness: 1px;
    text-decoration-color: rgba(148, 163, 184, .65);
}

.spaceAiPreviewCard.is-discarded .spaceAiPreviewDiscard {
    background: rgba(34, 211, 238, .10);
    border-color: rgba(34, 211, 238, .22);
    color: #67e8f9;
    text-decoration: none;
}

.spaceAiApplyControls {
    display: flex;
    flex-direction: column;
    gap: 9px;
}

.spaceAiApplyMeta {
    padding: 8px 10px;
    border-radius: 12px;
    background: rgba(0, 0, 0, .18);
    border: 1px solid rgba(255, 255, 255, .06);
    color: #94a3b8;
    font-size: 11px;
    font-weight: 850;
    line-height: 1.35;
}

.spaceAiScopeBtn {
    background: rgba(255, 255, 255, .06);
    color: #e2e8f0;
    border: 1px solid rgba(255, 255, 255, .10);
    box-shadow: none;
}

.spaceAiScopeBtn:hover:not(:disabled) {
    background: rgba(255, 255, 255, .10);
    transform: translateY(-1px);
}

.spaceAiAllergenBtn {
    border-color: rgba(249, 115, 22, .20);
    color: #fdba74;
    background: rgba(249, 115, 22, .08);
}

.spaceAiAllergenBtn:hover:not(:disabled) {
    background: rgba(249, 115, 22, .13);
}

.spaceAiApplyAllBtn {
    min-width: 128px;
}

.spaceAiPreviewDependencies {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(34, 211, 238, 0.20);
  background: rgba(34, 211, 238, 0.08);
  color: #67e8f9;
  font-size: 12px;
  line-height: 1.45;
  font-weight: 750;
}
