/**
 * STIVO Agent — asistente flotante (launcher + panel chat)
 * Requiere: html con clase .dark para modo oscuro (mismo criterio que el resto del sitio)
 */

#stivo-agent-root {
    --stivo-agent-accent: #059669;
    --stivo-agent-accent-mid: #10b981;
    --stivo-agent-surface: #ffffff;
    --stivo-agent-border: #e5e7eb;
    --stivo-agent-text: #111827;
    --stivo-agent-muted: #6b7280;
    --stivo-agent-user-bg: #ecfdf5;
    --stivo-agent-bot-bg: #f3f4f6;
    /* Bordes con acento verde STIVO (launcher + panel) */
    --stivo-agent-launcher-border: rgba(5, 150, 105, 0.52);
    --stivo-agent-launcher-border-hover: rgba(5, 150, 105, 0.88);
    --stivo-agent-launcher-icon: #0d9488;
    --stivo-agent-launcher-icon-hover: #047857;
    --stivo-agent-panel-edge: rgba(5, 150, 105, 0.4);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    -webkit-font-smoothing: antialiased;
}

html.dark #stivo-agent-root {
    --stivo-agent-accent: #34d399;
    --stivo-agent-accent-mid: #10b981;
    --stivo-agent-surface: #18181b;
    --stivo-agent-border: #3f3f46;
    --stivo-agent-text: #f3f4f6;
    --stivo-agent-muted: #a1a1aa;
    --stivo-agent-user-bg: rgba(16, 185, 129, 0.15);
    --stivo-agent-bot-bg: #27272a;
    --stivo-agent-launcher-border: rgba(52, 211, 153, 0.48);
    --stivo-agent-launcher-border-hover: rgba(52, 211, 153, 0.82);
    --stivo-agent-launcher-icon: #5eead4;
    --stivo-agent-launcher-icon-hover: #34d399;
    --stivo-agent-panel-edge: rgba(52, 211, 153, 0.35);
}

.stivo-agent-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1898;
    background: rgba(15, 23, 42, 0.25);
    backdrop-filter: blur(2px);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.22s ease, visibility 0.22s ease;
}

.stivo-agent-backdrop.is-visible {
    opacity: 1;
    visibility: visible;
}

@media (min-width: 640px) {
    .stivo-agent-backdrop {
        display: none;
    }
}

/* Launcher — superficie neutra, borde verde STIVO visible */
.stivo-agent-launcher {
    position: fixed;
    z-index: 1900;
    bottom: max(1.25rem, env(safe-area-inset-bottom));
    right: max(1.25rem, env(safe-area-inset-right));
    width: 48px;
    height: 48px;
    border: 1.5px solid var(--stivo-agent-launcher-border);
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    background: var(--stivo-agent-launcher-bg, #ffffff);
    color: var(--stivo-agent-launcher-icon);
    box-shadow:
        0 0 0 1px rgba(5, 150, 105, 0.08),
        0 1px 2px rgba(15, 23, 42, 0.06),
        0 4px 14px rgba(5, 150, 105, 0.1);
    transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
}

.stivo-agent-launcher:hover {
    background: var(--stivo-agent-launcher-bg-hover, #f0fdf9);
    border-color: var(--stivo-agent-launcher-border-hover);
    color: var(--stivo-agent-launcher-icon-hover);
    box-shadow:
        0 0 0 1px rgba(5, 150, 105, 0.14),
        0 2px 8px rgba(5, 150, 105, 0.18),
        0 4px 16px rgba(15, 23, 42, 0.06);
}

.stivo-agent-launcher:active {
    transform: scale(0.98);
}

.stivo-agent-launcher:focus-visible {
    outline: 2px solid rgba(5, 150, 105, 0.45);
    outline-offset: 2px;
}

html.dark .stivo-agent-launcher {
    --stivo-agent-launcher-bg: #18181b;
    --stivo-agent-launcher-bg-hover: #1c2724;
    box-shadow:
        0 0 0 1px rgba(52, 211, 153, 0.12),
        0 1px 2px rgba(0, 0, 0, 0.35),
        0 4px 18px rgba(16, 185, 129, 0.12);
}

html.dark .stivo-agent-launcher:hover {
    box-shadow:
        0 0 0 1px rgba(52, 211, 153, 0.2),
        0 2px 10px rgba(16, 185, 129, 0.22),
        0 4px 20px rgba(0, 0, 0, 0.35);
}

.stivo-agent-launcher-icon {
    width: 19px;
    height: 19px;
    opacity: 0.88;
}

@media (prefers-reduced-motion: reduce) {

    .stivo-agent-launcher,
    .stivo-agent-panel,
    .stivo-agent-backdrop {
        transition: none;
    }
}

/* Panel (encima del FAB: 48px launcher + separación) */
.stivo-agent-panel {
    position: fixed;
    z-index: 1901;
    bottom: calc(max(1.25rem, env(safe-area-inset-bottom)) + 3.65rem);
    right: max(1.25rem, env(safe-area-inset-right));
    width: min(100vw - 2rem, 400px);
    max-height: min(560px, calc(100vh - 6rem));
    display: flex;
    flex-direction: column;
    background: var(--stivo-agent-surface);
    border: 1.5px solid var(--stivo-agent-panel-edge);
    border-radius: 16px;
    box-shadow:
        0 0 0 1px rgba(5, 150, 105, 0.06),
        0 24px 48px rgba(15, 23, 42, 0.14),
        0 8px 20px rgba(5, 150, 105, 0.08);
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(12px) scale(0.98);
    transition: opacity 0.24s ease, transform 0.24s ease, visibility 0.24s ease;
}

/* Escritorio: panel más ancho; el ancho exacto lo fija JS (localStorage) al abrir */
@media (min-width: 640px) {
    .stivo-agent-panel {
        width: min(100vw - 3rem, 520px);
        max-height: min(620px, calc(100vh - 5.5rem));
    }
}

/* Asas de redimensionado: solo escritorio */
.stivo-agent-resize-handle {
    display: none;
}

.stivo-agent-resize-handle--bottom {
    display: none;
}

@media (min-width: 640px) {
    .stivo-agent-resize-handle {
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 10px;
        z-index: 2;
        cursor: ew-resize;
        touch-action: none;
        border-radius: 16px 0 0 16px;
        background: linear-gradient(90deg, rgba(5, 150, 105, 0.06) 0%, transparent 100%);
    }

    .stivo-agent-resize-handle:hover,
    .stivo-agent-resize-handle:focus-visible {
        background: linear-gradient(90deg, rgba(5, 150, 105, 0.14) 0%, transparent 100%);
    }

    .stivo-agent-resize-handle::after {
        content: '';
        position: absolute;
        left: 3px;
        top: 50%;
        transform: translateY(-50%);
        width: 3px;
        height: 36px;
        border-radius: 2px;
        background: var(--stivo-agent-border);
        opacity: 0.65;
    }

    html.dark .stivo-agent-resize-handle::after {
        background: var(--stivo-agent-muted);
        opacity: 0.45;
    }

    /* Borde inferior: redimensionar alto (solo escritorio; en flujo flex, no tapa el compositor) */
    .stivo-agent-resize-handle--bottom {
        display: block;
        position: relative;
        flex-shrink: 0;
        width: 100%;
        height: 10px;
        cursor: ns-resize;
        border-radius: 0 0 14px 14px;
        margin-top: -1px;
        background: linear-gradient(0deg, rgba(5, 150, 105, 0.08) 0%, transparent 100%);
        z-index: 3;
    }

    .stivo-agent-resize-handle--bottom:hover,
    .stivo-agent-resize-handle--bottom:focus-visible {
        background: linear-gradient(0deg, rgba(5, 150, 105, 0.16) 0%, transparent 100%);
    }

    .stivo-agent-resize-handle--bottom::after {
        content: '';
        position: absolute;
        left: 50%;
        top: 3px;
        transform: translateX(-50%);
        width: 36px;
        height: 3px;
        border-radius: 2px;
        background: var(--stivo-agent-border);
        opacity: 0.65;
    }

    html.dark .stivo-agent-resize-handle--bottom::after {
        background: var(--stivo-agent-muted);
        opacity: 0.45;
    }

    html.dark .stivo-agent-resize-handle--bottom {
        background: linear-gradient(0deg, rgba(52, 211, 153, 0.1) 0%, transparent 100%);
    }

    html.dark .stivo-agent-resize-handle--bottom:hover,
    html.dark .stivo-agent-resize-handle--bottom:focus-visible {
        background: linear-gradient(0deg, rgba(52, 211, 153, 0.18) 0%, transparent 100%);
    }
}

.stivo-agent-panel.is-open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

.stivo-agent-panel[hidden] {
    display: none !important;
}

.stivo-agent-panel.is-open:not([hidden]) {
    display: flex !important;
}

html.dark .stivo-agent-panel {
    box-shadow:
        0 0 0 1px rgba(52, 211, 153, 0.1),
        0 24px 48px rgba(0, 0, 0, 0.5),
        0 8px 22px rgba(16, 185, 129, 0.12);
}

.stivo-agent-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--stivo-agent-border);
    background: linear-gradient(180deg, var(--stivo-agent-user-bg) 0%, transparent 100%);
}

html.dark .stivo-agent-header {
    background: linear-gradient(180deg, rgba(16, 185, 129, 0.08) 0%, transparent 100%);
}

.stivo-agent-main {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

.stivo-agent-auth-gate {
    position: absolute;
    inset: 0;
    z-index: 6;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.25rem;
    background: var(--stivo-agent-surface);
    text-align: center;
}

.stivo-agent-auth-gate[hidden] {
    display: none !important;
}

.stivo-agent-auth-gate__inner {
    max-width: 17rem;
}

.stivo-agent-auth-gate__text {
    margin: 0 0 1rem;
    font-size: 0.92rem;
    line-height: 1.5;
    color: var(--stivo-agent-text);
}

.stivo-agent-auth-gate__btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.55rem 1.25rem;
    border-radius: 10px;
    font-weight: 700;
    font-size: 0.9rem;
    text-decoration: none;
    background: var(--stivo-agent-accent);
    color: #fff;
    border: none;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
}

.stivo-agent-auth-gate__btn:hover {
    background: #047857;
    color: #fff;
}

html.dark .stivo-agent-auth-gate__btn:hover {
    background: #10b981;
}

.stivo-agent-header-text {
    min-width: 0;
}

.stivo-agent-title {
    font-size: 1rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--stivo-agent-text);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.45rem;
}

.stivo-agent-header-actions {
    display: flex;
    align-items: center;
    gap: 0.15rem;
    flex-shrink: 0;
}

.stivo-agent-close,
.stivo-agent-clear {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--stivo-agent-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.stivo-agent-close:hover,
.stivo-agent-clear:hover {
    background: var(--stivo-agent-bot-bg);
    color: var(--stivo-agent-text);
}

.stivo-agent-clear:hover {
    color: #b91c1c;
}

html.dark .stivo-agent-clear:hover {
    color: #f87171;
}

.stivo-agent-close svg,
.stivo-agent-clear svg {
    width: 20px;
    height: 20px;
}

.stivo-agent-messages {
    flex: 1;
    min-height: 200px;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.stivo-agent-msg {
    max-width: 92%;
    padding: 0.65rem 0.85rem;
    border-radius: 12px;
    font-size: 0.88rem;
    line-height: 1.55;
    word-break: break-word;
}

.stivo-agent-msg--bot {
    align-self: flex-start;
    background: var(--stivo-agent-bot-bg);
    color: var(--stivo-agent-text);
    border-bottom-left-radius: 4px;
}

.stivo-agent-msg--user {
    align-self: flex-end;
    background: var(--stivo-agent-user-bg);
    color: var(--stivo-agent-text);
    border: 1px solid var(--stivo-agent-border);
    border-bottom-right-radius: 4px;
}

/* Markdown renderizado (respuesta del asistente) */
.stivo-agent-msg--md {
    line-height: 1.6;
}

.stivo-agent-msg--md>*:first-child {
    margin-top: 0;
}

.stivo-agent-msg--md>*:last-child {
    margin-bottom: 0;
}

.stivo-agent-msg--md p {
    margin: 0 0 0.55rem;
}

.stivo-agent-msg--md h2,
.stivo-agent-msg--md h3 {
    font-size: 0.95rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    margin: 0.65rem 0 0.4rem;
    color: var(--stivo-agent-text);
}

.stivo-agent-msg--md h2:first-child,
.stivo-agent-msg--md h3:first-child {
    margin-top: 0;
}

.stivo-agent-msg--md ul,
.stivo-agent-msg--md ol {
    margin: 0.35rem 0 0.55rem;
    padding-left: 1.15rem;
}

.stivo-agent-msg--md li {
    margin: 0.2rem 0;
}

.stivo-agent-msg--md a {
    color: var(--stivo-agent-accent);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.stivo-agent-msg--md a:hover {
    opacity: 0.9;
}

.stivo-agent-msg--md code {
    font-size: 0.84em;
    padding: 0.1em 0.35em;
    border-radius: 6px;
    background: var(--stivo-agent-bot-bg);
    border: 1px solid var(--stivo-agent-border);
}

.stivo-agent-msg--md pre {
    margin: 0.5rem 0;
    padding: 0.55rem 0.65rem;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 0.8rem;
    background: var(--stivo-agent-bot-bg);
    border: 1px solid var(--stivo-agent-border);
}

.stivo-agent-msg--md pre code {
    border: none;
    padding: 0;
    background: none;
}

.stivo-agent-msg--md hr {
    border: none;
    border-top: 1px solid var(--stivo-agent-border);
    margin: 0.65rem 0;
}

.stivo-agent-msg--streaming {
    position: relative;
}

.stivo-agent-msg--streaming::after {
    content: '';
    display: inline-block;
    width: 2px;
    height: 0.95em;
    margin-left: 2px;
    vertical-align: -0.1em;
    background: var(--stivo-agent-accent);
    animation: stivo-agent-caret 0.85s ease-in-out infinite;
}

@keyframes stivo-agent-caret {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.2;
    }
}

@media (prefers-reduced-motion: reduce) {
    .stivo-agent-msg--streaming::after {
        animation: none;
        opacity: 0.6;
    }
}

.stivo-agent-msg-meta {
    font-size: 0.72rem;
    color: var(--stivo-agent-muted);
    margin-top: 0.35rem;
}

.stivo-agent-composer {
    flex-shrink: 0;
    padding: 0.75rem;
    border-top: 1px solid var(--stivo-agent-border);
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
    background: var(--stivo-agent-surface);
}

.stivo-agent-input {
    flex: 1;
    min-height: 44px;
    max-height: 120px;
    resize: none;
    padding: 0.65rem 0.8rem;
    font-size: 0.9rem;
    font-family: inherit;
    line-height: 1.45;
    border: 1px solid var(--stivo-agent-border);
    border-radius: 12px;
    background: var(--stivo-agent-surface);
    color: var(--stivo-agent-text);
    box-sizing: border-box;
}

.stivo-agent-input::placeholder {
    color: var(--stivo-agent-muted);
}

.stivo-agent-input:focus {
    outline: none;
    border-color: var(--stivo-agent-accent);
    box-shadow: 0 0 0 3px rgba(5, 150, 105, 0.15);
}

html.dark .stivo-agent-input:focus {
    box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.12);
}

.stivo-agent-send {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 12px;
    background: var(--stivo-agent-accent);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, opacity 0.15s;
}

.stivo-agent-send:hover:not(:disabled) {
    background: #047857;
}

html.dark .stivo-agent-send:hover:not(:disabled) {
    background: #10b981;
}

.stivo-agent-send:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.stivo-agent-send svg {
    width: 20px;
    height: 20px;
}

/* Modal confirmación (borrar conversación) */
.stivo-agent-modal {
    position: fixed;
    inset: 0;
    z-index: 1920;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: max(1rem, env(safe-area-inset-bottom));
}

.stivo-agent-modal[hidden] {
    display: none !important;
}

.stivo-agent-modal__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.48);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.stivo-agent-modal__dialog {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 360px;
    margin: 0 auto;
    padding: 1.25rem 1.35rem;
    background: var(--stivo-agent-surface);
    border: 1.5px solid var(--stivo-agent-border);
    border-radius: 14px;
    box-shadow:
        0 24px 48px rgba(15, 23, 42, 0.16),
        0 0 0 1px rgba(5, 150, 105, 0.06);
}

html.dark .stivo-agent-modal__dialog {
    box-shadow:
        0 24px 48px rgba(0, 0, 0, 0.45),
        0 0 0 1px rgba(52, 211, 153, 0.1);
}

.stivo-agent-modal__title {
    font-size: 1rem;
    font-weight: 800;
    margin: 0 0 0.5rem;
    color: var(--stivo-agent-text);
    letter-spacing: -0.02em;
}

.stivo-agent-modal__text {
    font-size: 0.875rem;
    line-height: 1.55;
    color: var(--stivo-agent-muted);
    margin: 0 0 1.2rem;
}

.stivo-agent-modal__actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
    flex-wrap: wrap;
}

.stivo-agent-modal__btn {
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 600;
    padding: 0.55rem 1rem;
    border-radius: 10px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.stivo-agent-modal__btn--secondary {
    background: var(--stivo-agent-bot-bg);
    color: var(--stivo-agent-text);
    border-color: var(--stivo-agent-border);
}

.stivo-agent-modal__btn--secondary:hover {
    background: var(--stivo-agent-border);
}

.stivo-agent-modal__btn--danger {
    background: #b91c1c;
    color: #fff;
    border-color: #b91c1c;
}

.stivo-agent-modal__btn--danger:hover {
    background: #991b1b;
}

html.dark .stivo-agent-modal__btn--danger {
    background: #dc2626;
    border-color: #dc2626;
}

html.dark .stivo-agent-modal__btn--danger:hover {
    background: #b91c1c;
}

@media (prefers-reduced-motion: reduce) {

    .stivo-agent-modal__backdrop {
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }
}