/*
   Asistente de IA - Widget flotante del panel de tracking.

   Portado del componente React del frontend de CTPS y adaptado a los tokens
   del tracking (--bg-secondary, --text-primary, body.dark-theme…). El prefijo
   `acw-` evita colisiones con las clases existentes.
 */

.acw-root {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 940;
    font-family: var(--font-primary);
    font-size: var(--font-size-base);
    line-height: 1.5;
}

/* El toast de notificaciones debe quedar por encima del panel. */
body:has(.acw-root) .notifications-container,
body:has(.acw-root) .toast-container {
    bottom: 96px;
    z-index: 990;
}

.acw-fab {
    position: relative;
    width: 56px;
    height: 56px;
    border: 0;
    border-radius: var(--border-radius-full);
    background: var(--bg-secondary);
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.18);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: transform 0.16s ease, box-shadow 0.16s ease, opacity 0.16s ease;
    opacity: 0.94;
}

.acw-fab:hover {
    transform: translateY(-2px);
    opacity: 1;
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.24);
}

.acw-fab:focus-visible {
    outline: 2px solid var(--green-primary);
    outline-offset: 3px;
}

.acw-fab img {
    width: 40px;
    height: 40px;
    border-radius: var(--border-radius-full);
    object-fit: cover;
    display: block;
}

.acw-fab--active::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: var(--border-radius-full);
    border: 2px solid var(--green-primary);
    animation: acwPulse 1.4s ease-out infinite;
}

@keyframes acwPulse {
    0% { opacity: 0.7; transform: scale(0.94); }
    70% { opacity: 0; transform: scale(1.16); }
    100% { opacity: 0; transform: scale(1.16); }
}

/* ---------- Panel ---------- */

.acw-panel {
    display: flex;
    flex-direction: column;
    position: relative;
    width: min(392px, calc(100vw - 32px));
    height: min(620px, calc(100vh - 120px));
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-2xl);
    box-shadow: 0 18px 44px rgba(15, 23, 42, 0.22);
    overflow: hidden;
    animation: acwIn 0.16s ease-out;
}

/*
   El atributo `hidden` lo anula el `display: flex` del user-agent: sin estas
   reglas el panel y el boton flotante se verian siempre.
 */
.acw-panel[hidden],
.acw-fab[hidden] {
    display: none !important;
}

@keyframes acwIn {
    from { opacity: 0; transform: translateY(8px) scale(0.985); }
    to { opacity: 1; transform: none; }
}

.acw-panel--expanded {
    position: fixed;
    top: calc(var(--header-height) + 14px);
    left: calc(var(--sidenav-width) + 18px);
    right: 18px;
    bottom: 16px;
    width: auto;
    height: auto;
}

body:has(.sidenav.collapsed) .acw-panel--expanded {
    left: calc(var(--sidenav-collapsed-width) + 18px);
}

/* ---------- Cabecera ---------- */

.acw-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
    flex-shrink: 0;
}

.acw-head__avatar {
    width: 30px;
    height: 30px;
    border-radius: var(--border-radius-full);
    object-fit: cover;
    flex-shrink: 0;
}

.acw-head__text {
    display: flex;
    flex-direction: column;
    min-width: 0;
    flex: 1 1 auto;
    line-height: 1.25;
}

.acw-head__text strong {
    font-size: 0.8125rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.acw-head__phase {
    font-size: 0.6875rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.acw-head__phase--active {
    color: var(--green-primary);
    font-weight: 600;
}

.acw-head__btn {
    width: 28px;
    height: 28px;
    border: 0;
    border-radius: var(--border-radius-md);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    transition: background 0.14s ease, color 0.14s ease;
}

.acw-head__btn:hover {
    background: var(--hover-color);
    color: var(--text-primary);
}

.acw-head__btn:focus-visible {
    outline: 2px solid var(--green-primary);
    outline-offset: 1px;
}

.acw-head__btn--active {
    background: var(--hover-color);
    color: var(--text-primary);
}

.acw-head__btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.acw-head__btn svg {
    width: 15px;
    height: 15px;
}

/* ---------- Historial ---------- */

.acw-history {
    position: absolute;
    top: 52px;
    right: 10px;
    z-index: 20;
    width: min(286px, calc(100% - 20px));
    max-height: 300px;
    overflow-y: auto;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-lg);
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.18);
    padding: 6px;
}

.acw-history__empty {
    padding: 14px 10px;
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.acw-history__title {
    padding: 6px 8px 4px;
    font-size: 0.6875rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    font-weight: 700;
}

.acw-history__item {
    display: flex;
    align-items: center;
    gap: 4px;
    width: 100%;
    border: 0;
    background: transparent;
    border-radius: var(--border-radius-md);
    padding: 7px 8px;
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    font-size: 0.75rem;
    color: var(--text-primary);
}

.acw-history__item:hover {
    background: var(--hover-color);
}

.acw-history__item--current {
    background: var(--green-bg-soft);
    color: var(--text-primary);
    font-weight: 600;
}

body.dark-theme .acw-history__item--current {
    background: rgba(0, 166, 81, 0.18);
}

.acw-history__item-main {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.acw-history__item-date {
    font-size: 0.625rem;
    color: var(--text-secondary);
    font-weight: 400;
    margin-left: 6px;
}

/* ---------- Mensajes ---------- */

.acw-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--bg-primary);
}

.acw-messages__loading,
.acw-empty {
    margin: auto;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.75rem;
    max-width: 280px;
}

.acw-empty h3 {
    font-size: 0.875rem;
    color: var(--text-primary);
    margin: 10px 0 4px;
}

.acw-suggestions {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 12px;
}

.acw-suggestion {
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    border-radius: var(--border-radius-md);
    padding: 7px 10px;
    font-family: inherit;
    font-size: 0.75rem;
    color: var(--text-primary);
    cursor: pointer;
    text-align: left;
}

.acw-suggestion:hover {
    border-color: var(--green-primary);
    background: var(--hover-color);
}

.acw-row {
    display: flex;
    gap: 8px;
    align-items: flex-start;
}

.acw-row--user {
    justify-content: flex-end;
}

.acw-row__avatar {
    width: 24px;
    height: 24px;
    border-radius: var(--border-radius-full);
    object-fit: cover;
    flex-shrink: 0;
    margin-top: 2px;
}

.acw-bubble {
    max-width: 86%;
    min-width: 0;
    padding: 8px 11px;
    border-radius: var(--border-radius-lg);
    font-size: 0.75rem;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.acw-bubble--user {
    background: var(--green-primary);
    color: #ffffff;
    border-bottom-right-radius: var(--border-radius-xs);
}

.acw-bubble--agent {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: var(--border-radius-xs);
}

.acw-bubble__time {
    display: block;
    margin-top: 4px;
    font-size: 0.625rem;
    opacity: 0.7;
}

.acw-content > *:first-child { margin-top: 0; }
.acw-content > *:last-child { margin-bottom: 0; }
.acw-content p { margin: 0 0 6px; }
.acw-content ul,
.acw-content ol { margin: 0 0 6px; padding-left: 18px; }
.acw-content li { margin-bottom: 2px; }
.acw-content h3,
.acw-content h4,
.acw-content h5,
.acw-content h6 { margin: 10px 0 4px; font-size: 0.8125rem; }
.acw-content blockquote {
    margin: 0 0 6px;
    padding: 4px 8px;
    border-left: 3px solid var(--border-color);
    color: var(--text-secondary);
}
.acw-content hr {
    border: 0;
    border-top: 1px solid var(--border-color);
    margin: 8px 0;
}
.acw-content a { color: var(--green-dark); text-decoration: underline; }
body.dark-theme .acw-content a { color: #4ade80; }
.acw-content code {
    font-family: var(--font-mono, 'Cascadia Code', Consolas, monospace);
    font-size: 0.6875rem;
    background: var(--hover-color);
    padding: 1px 4px;
    border-radius: var(--border-radius-xs);
}
.acw-content table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.6875rem;
    margin-bottom: 6px;
}
.acw-content th,
.acw-content td {
    border: 1px solid var(--border-color);
    padding: 3px 6px;
    text-align: left;
}
.acw-content th { background: var(--bg-primary); font-weight: 700; }

.acw-table-wrap {
    max-width: 100%;
    overflow-x: auto;
    margin-bottom: 6px;
}

.acw-code {
    background: #0f172a;
    border-radius: var(--border-radius-md);
    overflow: hidden;
    margin: 6px 0;
}

.acw-code__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 8px;
    background: #1e293b;
    font-size: 0.625rem;
    color: #cbd5e1;
}

.acw-code__copy {
    border: 0;
    background: transparent;
    color: #cbd5e1;
    font-family: inherit;
    font-size: 0.625rem;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: var(--border-radius-xs);
}

.acw-code__copy:hover { background: rgba(255, 255, 255, 0.12); color: #f8fafc; }

.acw-code pre {
    margin: 0;
    padding: 8px;
    overflow-x: auto;
}

.acw-code pre code {
    background: transparent;
    color: #e2e8f0;
    padding: 0;
    font-size: 0.6875rem;
    line-height: 1.45;
}

/* ---------- Herramientas y razonamiento ---------- */

.acw-tools {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin: 2px 0 0 32px;
}

.acw-tool {
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-md);
    background: var(--bg-secondary);
    font-size: 0.6875rem;
    overflow: hidden;
}

.acw-tool summary {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 8px;
    cursor: pointer;
    color: var(--text-secondary);
    list-style: none;
}

.acw-tool summary::-webkit-details-marker { display: none; }

.acw-tool__dot {
    width: 7px;
    height: 7px;
    border-radius: var(--border-radius-full);
    background: var(--text-secondary);
    flex-shrink: 0;
}

.acw-tool--running .acw-tool__dot {
    background: var(--green-primary);
    animation: acwBlink 1s ease-in-out infinite;
}

.acw-tool--error .acw-tool__dot { background: var(--status-error); }

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

.acw-tool__name {
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.acw-tool__time { margin-left: auto; flex-shrink: 0; }

.acw-tool__section { padding: 0 8px 6px; }
.acw-tool__label {
    display: block;
    font-size: 0.625rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    margin-bottom: 2px;
}
.acw-tool__body {
    margin: 0;
    max-height: 160px;
    overflow: auto;
    background: var(--bg-primary);
    border-radius: var(--border-radius-xs);
    padding: 5px 6px;
    font-family: var(--font-mono, 'Cascadia Code', Consolas, monospace);
    font-size: 0.625rem;
    color: var(--text-primary);
    white-space: pre-wrap;
    word-break: break-word;
}

.acw-typing {
    display: inline-flex;
    gap: 3px;
    padding: 2px 0;
}

.acw-typing span {
    width: 5px;
    height: 5px;
    border-radius: var(--border-radius-full);
    background: var(--text-secondary);
    animation: acwTyping 1.1s ease-in-out infinite;
}

.acw-typing span:nth-child(2) { animation-delay: 0.15s; }
.acw-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes acwTyping {
    0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-2px); }
}

.acw-cursor {
    display: inline-block;
    width: 2px;
    height: 12px;
    background: var(--green-primary);
    vertical-align: text-bottom;
    animation: acwBlink 1s step-end infinite;
}

.acw-error {
    margin-left: 32px;
    padding: 6px 9px;
    border-radius: var(--border-radius-md);
    background: #fee2e2;
    color: #b91c1c;
    font-size: 0.6875rem;
}

body.dark-theme .acw-error {
    background: rgba(220, 53, 69, 0.16);
    color: #fca5a5;
}

.acw-error__retry {
    margin-top: 4px;
    border: 0;
    background: transparent;
    color: inherit;
    font-family: inherit;
    font-size: 0.6875rem;
    font-weight: 700;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
}

.acw-jump {
    position: sticky;
    bottom: 0;
    align-self: center;
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-radius: var(--border-radius-full);
    padding: 4px 10px;
    font-family: inherit;
    font-size: 0.6875rem;
    cursor: pointer;
    box-shadow: var(--card-shadow);
}

/* ---------- Preguntas ---------- */

.acw-question {
    margin-left: 32px;
    border: 1px solid var(--green-primary);
    border-radius: var(--border-radius-lg);
    background: var(--bg-secondary);
    padding: 9px 10px;
}

.acw-question__head {
    font-size: 0.6875rem;
    font-weight: 700;
    color: var(--green-dark);
    margin-bottom: 6px;
}

body.dark-theme .acw-question__head { color: #4ade80; }

.acw-question__block { margin-bottom: 8px; }

.acw-question__tag {
    display: inline-block;
    font-size: 0.625rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    margin-bottom: 2px;
}

.acw-question__text {
    margin: 0 0 5px;
    font-size: 0.75rem;
    color: var(--text-primary);
    font-weight: 600;
}

.acw-question__options {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.acw-question__option {
    display: flex;
    align-items: flex-start;
    gap: 6px;
    padding: 5px 7px;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-md);
    cursor: pointer;
    font-size: 0.6875rem;
    color: var(--text-primary);
}

.acw-question__option:hover { background: var(--hover-color); }

.acw-question__option input { margin-top: 2px; flex-shrink: 0; }

.acw-question__option small {
    display: block;
    color: var(--text-secondary);
    font-size: 0.625rem;
}

.acw-question__input {
    width: 100%;
    margin-top: 4px;
    padding: 5px 7px;
    border: 1px solid var(--input-border);
    border-radius: var(--border-radius-md);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.6875rem;
}

.acw-question__input:focus {
    outline: none;
    border-color: var(--input-focus);
}

.acw-question__actions {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
    margin-top: 8px;
}

.acw-btn {
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-radius: var(--border-radius-md);
    padding: 5px 10px;
    font-family: inherit;
    font-size: 0.6875rem;
    cursor: pointer;
}

.acw-btn:hover { background: var(--hover-color); }

.acw-btn--primary {
    background: var(--green-primary);
    border-color: var(--green-primary);
    color: #ffffff;
    font-weight: 600;
}

.acw-btn--primary:hover { background: var(--green-dark); }

.acw-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Composer ---------- */

.acw-composer {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 8px 10px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.acw-composer textarea {
    flex: 1 1 auto;
    min-height: 34px;
    max-height: 100px;
    resize: none;
    border: 1px solid var(--input-border);
    border-radius: var(--border-radius-md);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.75rem;
    line-height: 1.45;
    padding: 7px 9px;
    overflow-y: auto;
}

.acw-composer textarea:focus {
    outline: none;
    border-color: var(--input-focus);
}

.acw-composer textarea::placeholder { color: var(--text-secondary); }

.acw-composer__send,
.acw-composer__stop {
    width: 34px;
    height: 34px;
    flex-shrink: 0;
    border: 0;
    border-radius: var(--border-radius-md);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.acw-composer__send {
    background: var(--green-primary);
    color: #ffffff;
}

.acw-composer__send:hover { background: var(--green-dark); }

.acw-composer__stop {
    background: var(--status-error);
    color: #ffffff;
}

.acw-composer__send:disabled { opacity: 0.45; cursor: not-allowed; }

.acw-composer svg { width: 16px; height: 16px; }

.acw-hidden { display: none !important; }

/* ---------- Aviso de conexión ---------- */

/*
   El asistente puede estar apagado sin que sea un error del tracking: esta
   franja sustituye la alerta roja y ofrece reintentar sin recargar la página.
 */
.acw-offline {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 6px 10px;
    border: 1px solid #fcd34d;
    border-radius: var(--border-radius-md);
    background: #fef3c7;
    color: #92400e;
    font-size: 0.6875rem;
}

body.dark-theme .acw-offline {
    border-color: #78350f;
    background: #422006;
    color: #fcd34d;
}

.acw-offline__dot {
    width: 7px;
    height: 7px;
    border-radius: var(--border-radius-full);
    background: currentColor;
    flex-shrink: 0;
    opacity: 0.8;
}

.acw-offline__text {
    flex: 1 1 auto;
    font-weight: 600;
}

.acw-offline__retry {
    border: 0;
    background: transparent;
    color: inherit;
    font-family: inherit;
    font-size: 0.6875rem;
    font-weight: 700;
    text-decoration: underline;
    cursor: pointer;
    padding: 0;
    flex-shrink: 0;
}

.acw-offline__retry:focus-visible {
    outline: 2px solid currentColor;
    outline-offset: 2px;
}

.acw-head__phase--offline {
    color: #b45309;
}

body.dark-theme .acw-head__phase--offline {
    color: #fcd34d;
}

/* ---------- Responsive ---------- */

@media (max-width: 980px) {
    .acw-root { right: 12px; bottom: 12px; }

    .acw-panel {
        width: calc(100vw - 24px);
        height: min(560px, calc(100vh - 100px));
    }

    .acw-panel--expanded {
        top: calc(var(--header-height) + 8px);
        left: 8px;
        right: 8px;
        bottom: 8px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .acw-fab,
    .acw-panel,
    .acw-fab--active::after,
    .acw-typing span,
    .acw-tool--running .acw-tool__dot,
    .acw-cursor {
        animation: none !important;
        transition: none !important;
    }
}
