/* ============================================================
   Asistente IA — panel flotante esquina inferior derecha.
   Sin overlay ni difuminado: el dashboard sigue usable con el chat abierto.
   ============================================================ */

.assistant-chat .visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.assistant-chat {
    --ac-surface: var(--app-surface);
    --ac-surface-elevated: var(--app-surface-alt);
    --ac-border: var(--app-border);
    --ac-text: var(--app-text);
    --ac-text-soft: var(--app-text-soft);
    --ac-text-muted: var(--app-text-muted);
    --ac-user-bubble: var(--app-primary-soft);
    --ac-assistant-bubble: var(--app-surface-alt);
    --ac-accent: var(--app-primary);
    --ac-accent-text: #fff;
    --ac-shadow: var(--app-shadow-md);
    --ac-radius: var(--app-radius-md);
    --ac-font: "Segoe UI", "Inter", system-ui, -apple-system, sans-serif;
    font-family: var(--ac-font);
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 1080;
}

.assistant-chat__fab,
.assistant-chat__panel {
    pointer-events: auto;
}

/* Backdrop desactivado: no tapar ni difuminar la página */
.assistant-chat__backdrop {
    display: none !important;
}

/* El FAB solo se muestra si JS añade .assistant-chat--fab-visible tras GET /assistant/health */
.assistant-chat__fab {
    position: fixed;
    right: max(20px, env(safe-area-inset-right, 0px));
    bottom: max(22px, env(safe-area-inset-bottom, 0px));
    width: 58px;
    height: 58px;
    border-radius: 20px;
    border: none;
    background: linear-gradient(145deg, var(--app-primary) 0%, var(--app-primary-dark) 100%);
    color: #fff;
    box-shadow:
        0 4px 14px rgba(28, 79, 161, 0.45),
        0 12px 28px rgba(15, 36, 77, 0.18);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 1.35rem;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    z-index: 1082;
}

.assistant-chat.assistant-chat--fab-visible .assistant-chat__fab {
    display: flex;
}

.assistant-chat__fab:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow:
        0 6px 20px rgba(28, 79, 161, 0.5),
        0 14px 32px rgba(15, 36, 77, 0.22);
}

.assistant-chat__fab:focus-visible {
    outline: 3px solid var(--app-primary);
    outline-offset: 3px;
}

.assistant-chat__fab-pulse {
    position: absolute;
    inset: -4px;
    border-radius: 22px;
    border: 2px solid rgba(77, 159, 255, 0.45);
    animation: ac-fab-pulse 2.4s ease-out infinite;
    pointer-events: none;
}

@keyframes ac-fab-pulse {
    0% {
        transform: scale(1);
        opacity: 0.7;
    }
    70% {
        transform: scale(1.12);
        opacity: 0;
    }
    100% {
        opacity: 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    .assistant-chat__fab-pulse {
        animation: none;
    }

    .assistant-chat__panel {
        transition: none;
    }
}

.assistant-chat.is-open .assistant-chat__fab {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.88);
}

/* Panel: anclado abajo a la derecha; sube desde abajo */
.assistant-chat__panel {
    position: fixed;
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    height: min(85dvh, 640px);
    max-height: min(85dvh, 640px);
    background: var(--ac-surface);
    border: 1px solid var(--ac-border);
    border-bottom: none;
    box-shadow: 0 -12px 40px rgba(15, 36, 77, 0.12);
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
    z-index: 1083;
    border-radius: var(--ac-radius) var(--ac-radius) 0 0;
}

.assistant-chat.is-open .assistant-chat__panel {
    transform: translateY(0);
}

@media (min-width: 768px) {
    .assistant-chat__panel {
        left: auto;
        right: max(16px, env(safe-area-inset-right, 0px));
        bottom: max(16px, env(safe-area-inset-bottom, 0px));
        width: min(420px, calc(100vw - 32px));
        height: min(560px, calc(100vh - 32px));
        max-height: min(560px, calc(100vh - 32px));
        border: 1px solid var(--ac-border);
        border-radius: var(--ac-radius);
        box-shadow:
            0 16px 48px rgba(15, 36, 77, 0.14),
            0 4px 16px rgba(15, 36, 77, 0.06);
    }
}

.assistant-chat__header {
    flex-shrink: 0;
    padding: 1rem 1rem 0.85rem;
    border-bottom: 1px solid var(--ac-border);
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    background: linear-gradient(180deg, var(--ac-surface-elevated) 0%, var(--ac-surface) 100%);
    border-radius: inherit;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

@media (min-width: 768px) {
    .assistant-chat__header {
        border-radius: var(--ac-radius) var(--ac-radius) 0 0;
    }
}

.assistant-chat__avatar {
    width: 44px;
    height: 44px;
    border-radius: 14px;
    background: linear-gradient(135deg, var(--ac-accent) 0%, #6366f1 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 1.1rem;
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(28, 79, 161, 0.25);
}

.assistant-chat__headlines {
    flex: 1;
    min-width: 0;
}

.assistant-chat__title {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--ac-text);
    line-height: 1.25;
}

.assistant-chat__subtitle {
    margin: 0.2rem 0 0;
    font-size: 0.78rem;
    color: var(--ac-text-muted);
    line-height: 1.35;
}

.assistant-chat__status {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.45rem;
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--app-success);
}

.assistant-chat__status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--app-success);
    box-shadow: 0 0 0 3px rgba(31, 139, 95, 0.2);
    animation: ac-status-blink 2s ease-in-out infinite;
}

@keyframes ac-status-blink {
    0%,
    100% {
        opacity: 1;
    }
    50% {
        opacity: 0.45;
    }
}

@media (prefers-reduced-motion: reduce) {
    .assistant-chat__status-dot {
        animation: none;
    }
}

.assistant-chat__close {
    flex-shrink: 0;
    width: 38px;
    height: 38px;
    border: 1px solid var(--ac-border);
    border-radius: 12px;
    background: var(--ac-surface-elevated);
    color: var(--ac-text-soft);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.assistant-chat__close:hover {
    background: var(--ac-border);
    color: var(--ac-text);
}

.assistant-chat__close:focus-visible {
    outline: 2px solid var(--ac-accent);
    outline-offset: 2px;
}

.assistant-chat__messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem 1rem 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    scroll-behavior: smooth;
    min-height: 0;
}

.assistant-chat__messages:focus {
    outline: none;
}

.assistant-chat__hint {
    margin: 0 0 0.25rem;
    padding: 0.65rem 0.85rem;
    font-size: 0.8rem;
    line-height: 1.45;
    color: var(--ac-text-soft);
    background: var(--ac-surface-elevated);
    border: 1px dashed var(--ac-border);
    border-radius: 12px;
}

.assistant-chat__row {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
    max-width: 100%;
}

.assistant-chat__row--user {
    flex-direction: row-reverse;
}

.assistant-chat__bubble {
    max-width: 88%;
    padding: 0.65rem 0.85rem;
    border-radius: 16px;
    font-size: 0.875rem;
    line-height: 1.5;
    color: var(--ac-text);
    word-wrap: break-word;
    white-space: pre-wrap;
}

.assistant-chat__row--assistant .assistant-chat__bubble {
    background: var(--ac-assistant-bubble);
    border: 1px solid var(--ac-border);
    border-bottom-left-radius: 6px;
}

.assistant-chat__row--user .assistant-chat__bubble {
    background: var(--ac-user-bubble);
    border: 1px solid rgba(28, 79, 161, 0.12);
    border-bottom-right-radius: 6px;
    color: var(--ac-text);
}

.assistant-chat__meta {
    font-size: 0.65rem;
    color: var(--ac-text-muted);
    padding: 0 0.15rem;
    flex-shrink: 0;
}

.assistant-chat__typing {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 0.75rem 1rem;
    background: var(--ac-assistant-bubble);
    border: 1px solid var(--ac-border);
    border-radius: 16px;
    border-bottom-left-radius: 6px;
    width: fit-content;
    max-width: 72px;
}

.assistant-chat__typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--ac-text-muted);
    animation: ac-typing 1.2s ease-in-out infinite;
}

.assistant-chat__typing span:nth-child(2) {
    animation-delay: 0.15s;
}

.assistant-chat__typing span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes ac-typing {
    0%,
    60%,
    100% {
        transform: translateY(0);
        opacity: 0.35;
    }
    30% {
        transform: translateY(-5px);
        opacity: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    .assistant-chat__typing span {
        animation: none;
        opacity: 0.6;
    }
}

.assistant-chat__composer {
    flex-shrink: 0;
    padding: 0.75rem 1rem max(0.85rem, env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--ac-border);
    background: var(--ac-surface);
    border-radius: 0 0 var(--ac-radius) var(--ac-radius);
}

@media (max-width: 767.98px) {
    .assistant-chat__composer {
        border-radius: 0;
        padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    }
}

.assistant-chat__composer-inner {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
    background: var(--ac-surface-elevated);
    border: 1px solid var(--ac-border);
    border-radius: 14px;
    padding: 0.45rem 0.45rem 0.45rem 0.65rem;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.assistant-chat__composer-inner:focus-within {
    border-color: rgba(28, 79, 161, 0.35);
    box-shadow: 0 0 0 3px rgba(28, 79, 161, 0.08);
}

html[data-theme="dark"] body.app-shell .assistant-chat__composer-inner:focus-within {
    border-color: rgba(77, 159, 255, 0.4);
    box-shadow: 0 0 0 3px rgba(77, 159, 255, 0.12);
}

.assistant-chat__input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--ac-text);
    font-size: 0.875rem;
    line-height: 1.45;
    resize: none;
    min-height: 40px;
    max-height: 120px;
    padding: 0.45rem 0.25rem;
    font-family: inherit;
}

.assistant-chat__input::placeholder {
    color: var(--ac-text-muted);
}

.assistant-chat__input:focus {
    outline: none;
}

.assistant-chat__send {
    flex-shrink: 0;
    width: 42px;
    height: 42px;
    border: none;
    border-radius: 12px;
    background: var(--ac-accent);
    color: var(--ac-accent-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    transition: filter 0.15s, transform 0.1s;
}

.assistant-chat__send:hover:not(:disabled) {
    filter: brightness(1.06);
}

.assistant-chat__send:active:not(:disabled) {
    transform: scale(0.96);
}

.assistant-chat__send:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.assistant-chat__send:focus-visible {
    outline: 2px solid var(--ac-accent);
    outline-offset: 2px;
}

.assistant-chat__kbd-hint {
    margin: 0.45rem 0 0;
    font-size: 0.68rem;
    color: var(--ac-text-muted);
    text-align: center;
}

.assistant-chat__kbd-hint kbd {
    font-family: inherit;
    font-size: 0.65rem;
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
    border: 1px solid var(--ac-border);
    background: var(--ac-surface-elevated);
}

/* Opportunities (opp-shell) */
body.opp-shell .assistant-chat {
    --ac-surface: rgba(16, 20, 32, 0.97);
    --ac-surface-elevated: rgba(255, 255, 255, 0.05);
    --ac-border: rgba(255, 255, 255, 0.1);
    --ac-text: var(--opp-text);
    --ac-text-soft: var(--opp-text-soft);
    --ac-text-muted: var(--opp-text-muted);
    --ac-user-bubble: rgba(77, 159, 255, 0.18);
    --ac-assistant-bubble: rgba(255, 255, 255, 0.06);
    --ac-accent: var(--opp-accent-blue);
    --ac-accent-text: #fff;
}

body.opp-shell .assistant-chat__fab {
    background: linear-gradient(145deg, var(--opp-accent-blue) 0%, #6366f1 100%);
    box-shadow:
        0 4px 18px rgba(77, 159, 255, 0.4),
        0 12px 32px rgba(0, 0, 0, 0.35);
}

body.opp-shell .assistant-chat__panel {
    box-shadow:
        0 16px 48px rgba(0, 0, 0, 0.45),
        0 4px 16px rgba(0, 0, 0, 0.25);
}

body.opp-shell .assistant-chat__status {
    color: var(--opp-accent-green);
}

body.opp-shell .assistant-chat__status-dot {
    background: var(--opp-accent-green);
    box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.2);
}

html[data-theme="dark"] body.app-shell .assistant-chat__panel {
    box-shadow:
        0 16px 48px rgba(0, 0, 0, 0.45),
        0 4px 16px rgba(0, 0, 0, 0.2);
}

html[data-theme="dark"] body.app-shell .assistant-chat__fab {
    box-shadow:
        0 4px 14px rgba(77, 159, 255, 0.35),
        0 12px 28px rgba(0, 0, 0, 0.35);
}
