:root {
    --undesa-primary:      #0d6efd;
    --undesa-primary-dark: #0a58ca;
    --undesa-primary-soft: #e8f0ff;
    --undesa-accent:       #38bdf8;

    --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI",
                 Roboto, "Helvetica Neue", Arial, sans-serif;

    --chat-bg:          #f4f6fb;
    --chat-surface:     #ffffff;
    --chat-text:        #0f172a;
    --chat-muted:       #64748b;
    --chat-border:      #e2e8f0;
    --chat-user-bg:     linear-gradient(135deg, #0d6efd 0%, #0a58ca 100%);
    --chat-user-fg:     #ffffff;
    --chat-agent-bg:    #ffffff;
    --chat-agent-fg:    #0f172a;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06);
    --shadow-md: 0 4px 16px rgba(15, 23, 42, .08);
    --shadow-lg: 0 18px 48px -12px rgba(13, 110, 253, .35),
                 0 8px 24px -8px rgba(15, 23, 42, .12);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    font-family: var(--font-sans);
    margin: 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-feature-settings: "cv11", "ss01";
    letter-spacing: -0.005em;
}

/* ===== Login ===== */
.login-body {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #0f172a;
    background:
        radial-gradient(at 18% 12%, rgba(56, 189, 248, .35) 0px, transparent 55%),
        radial-gradient(at 82% 88%, rgba(10, 88, 202, .45) 0px, transparent 50%),
        linear-gradient(135deg, var(--undesa-primary) 0%, var(--undesa-primary-dark) 100%);
    padding: 1rem;
    padding-top: max(1rem, env(safe-area-inset-top));
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
    position: relative;
    overflow: hidden;
}

/* Halo decorativo sutil detrás de la tarjeta */
.login-body::before {
    content: "";
    position: absolute;
    width: 520px;
    height: 520px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255,255,255,.18) 0%, transparent 70%);
    top: -180px;
    left: 50%;
    transform: translateX(-50%);
    pointer-events: none;
}

.login-wrapper {
    width: 100%;
    max-width: 420px;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    z-index: 1;
}

.login-card {
    background: rgba(255, 255, 255, .98);
    border-radius: 20px;
    padding: 2.25rem 2rem;
    width: 100%;
    border: 1px solid rgba(255, 255, 255, .6);
    box-shadow: var(--shadow-lg);
    backdrop-filter: saturate(160%) blur(6px);
    -webkit-backdrop-filter: saturate(160%) blur(6px);
}

.login-header h1 {
    font-weight: 700;
    letter-spacing: -0.02em;
    color: #0f172a;
}
.login-header p { color: #64748b; }

.login-logo {
    background: transparent;
    padding: 0;
    border-radius: 0;
    filter: drop-shadow(0 6px 16px rgba(13, 110, 253, .25));
}

.login-form .form-label {
    font-weight: 500;
    font-size: .82rem;
    color: #475569;
    letter-spacing: .01em;
    margin-bottom: .35rem;
}

.login-form .input-group {
    border-radius: 12px;
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    transition: box-shadow .15s ease, transform .15s ease;
}
.login-form .input-group:focus-within {
    box-shadow: 0 0 0 4px rgba(13, 110, 253, .15), var(--shadow-md);
}
.login-form .input-group-text {
    background: #f8fafc;
    border-color: #e2e8f0;
    color: #64748b;
}
.login-form .form-control {
    border-color: #e2e8f0;
    padding: .6rem .9rem;
    font-size: .95rem;
    color: #0f172a;
}
.login-form .form-control:focus {
    border-color: var(--undesa-primary);
    box-shadow: none;
}
.login-form .btn-outline-secondary {
    border-color: #e2e8f0;
    color: #64748b;
    background: #f8fafc;
}
.login-form .btn-outline-secondary:hover {
    background: #eef2f7;
    color: #0f172a;
}

.login-form #submit-btn {
    background: linear-gradient(135deg, var(--undesa-primary) 0%, var(--undesa-primary-dark) 100%);
    border: none;
    border-radius: 12px;
    padding: .7rem 1rem;
    font-weight: 600;
    letter-spacing: .01em;
    box-shadow: 0 6px 16px -6px rgba(13, 110, 253, .55);
    transition: transform .15s ease, box-shadow .2s ease, filter .15s ease;
}
.login-form #submit-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 10px 22px -8px rgba(13, 110, 253, .65);
    filter: brightness(1.04);
}
.login-form #submit-btn:active { transform: translateY(0); }

.login-footer {
    border-top: 1px solid #eef2f7;
    padding-top: 1rem;
    color: #94a3b8;
}

#install-btn {
    border-radius: 999px;
    backdrop-filter: blur(8px);
    background: rgba(255, 255, 255, .12);
    border-color: rgba(255, 255, 255, .35);
    font-weight: 500;
    padding: .4rem 1rem;
}
#install-btn:hover {
    background: rgba(255, 255, 255, .22);
    color: #fff;
}

/* ===== App ===== */
.app-body {
    background: var(--chat-bg);
    min-height: 100dvh;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
}

/* Estado disabled visual del botón submit */
#submit-btn[disabled] { opacity: .75; cursor: progress; }

/* ============================================================
   ===== Chat ================================================
   ============================================================ */

.chat-body {
    margin: 0;
    background:
        radial-gradient(at 0% 0%, rgba(13, 110, 253, .05) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(56, 189, 248, .04) 0px, transparent 50%),
        var(--chat-bg);
    color: var(--chat-text);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-header {
    background: linear-gradient(135deg, var(--undesa-primary) 0%, var(--undesa-primary-dark) 100%);
    color: #fff;
    display: flex;
    align-items: center;
    padding: .85rem 1.1rem;
    padding-top: max(.85rem, env(safe-area-inset-top));
    box-shadow: 0 6px 20px -8px rgba(13, 110, 253, .45);
    z-index: 10;
    position: relative;
    overflow: hidden;
}
/* El grupo de botones derecho nunca debe encogerse; el bloque central
   (título "Fidias") es el que trunca cuando el espacio es justo. */
.chat-header > div:last-child { flex-shrink: 0; }
.chat-header::after {
    content: "";
    position: absolute;
    left: 0; right: 0; bottom: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,.25), transparent);
}
.chat-header .fw-semibold { letter-spacing: -0.01em; }
.chat-header .btn-link {
    border-radius: 10px;
    transition: background .15s ease;
}
.chat-header .btn-link:hover {
    background: rgba(255, 255, 255, .12);
}

/* Avatar */
.chat-avatar {
    width: 38px; height: 38px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center; justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
    letter-spacing: -0.02em;
}
.chat-avatar-agent {
    background: linear-gradient(135deg, #0d6efd 0%, #0a58ca 100%);
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.2), 0 4px 10px -2px rgba(13, 110, 253, .35);
}
.chat-avatar-lg {
    width: 72px; height: 72px; font-size: 1.7rem;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.25),
                0 14px 32px -10px rgba(13, 110, 253, .45);
}

/* Messages area */
.chat-messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 1.2rem 1rem;
    scroll-behavior: smooth;
}
.chat-messages::-webkit-scrollbar { width: 10px; }
.chat-messages::-webkit-scrollbar-thumb {
    background: #d8dee9;
    border-radius: 999px;
    border: 2px solid var(--chat-bg);
}
.chat-messages::-webkit-scrollbar-thumb:hover { background: #c5cdd9; }

.chat-empty {
    max-width: 640px;
    margin: 3rem auto 1rem;
    text-align: center;
}
.chat-empty h2 {
    font-weight: 700;
    letter-spacing: -0.02em;
    color: #0f172a;
}

.chat-suggestion {
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    border-radius: 14px;
    padding: .85rem 1.1rem;
    font-size: .92rem;
    color: #0f172a;
    box-shadow: var(--shadow-sm);
    transition: transform .15s ease, box-shadow .2s ease, border-color .15s ease;
}
.chat-suggestion:hover {
    transform: translateY(-1px);
    border-color: rgba(13, 110, 253, .25);
    box-shadow: 0 6px 18px -8px rgba(13, 110, 253, .35);
}

/* Bubbles */
.chat-row {
    display: flex;
    align-items: flex-end;
    gap: .55rem;
    margin-bottom: 1rem;
    max-width: 100%;
}
.chat-row-user { justify-content: flex-end; }
.chat-row-assistant { justify-content: flex-start; }

.chat-bubble {
    max-width: min(75ch, 80%);
    padding: .8rem 1rem;
    border-radius: 18px;
    line-height: 1.55;
    overflow-wrap: anywhere;
    font-size: .95rem;
    animation: bubble-in .22s ease;
}
@keyframes bubble-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: none; }
}

.chat-bubble-user {
    background: var(--chat-user-bg);
    color: var(--chat-user-fg);
    border-bottom-right-radius: 6px;
    box-shadow: 0 8px 20px -8px rgba(13, 110, 253, .45),
                inset 0 0 0 1px rgba(255,255,255,.08);
}
.chat-bubble-assistant {
    background: var(--chat-agent-bg);
    color: var(--chat-agent-fg);
    border: 1px solid var(--chat-border);
    border-bottom-left-radius: 6px;
    box-shadow: var(--shadow-sm);
}

/* Markdown rendering inside agent bubbles */
.chat-bubble-assistant p:last-child { margin-bottom: 0; }
.chat-bubble-assistant pre {
    background: #0f172a;
    color: #e2e8f0;
    padding: .6rem .75rem;
    border-radius: 8px;
    overflow-x: auto;
    font-size: .82rem;
}
.chat-bubble-assistant code {
    background: rgba(15,23,42,.06);
    padding: .1rem .35rem;
    border-radius: 4px;
    font-size: .85em;
}
.chat-bubble-assistant pre code {
    background: transparent;
    padding: 0;
    color: inherit;
    font-size: inherit;
}
.chat-bubble-assistant a {
    color: var(--undesa-primary);
    overflow-wrap: anywhere;
}
.chat-md-table {
    width: 100%;
    max-width: 100%;
    margin: .5rem 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 6px;
}
.chat-bubble-assistant table {
    border-collapse: collapse;
    margin: 0;
    font-size: .85rem;
    width: auto;
    table-layout: auto;
}
.chat-bubble-assistant th,
.chat-bubble-assistant td {
    border: 1px solid var(--chat-border);
    padding: .5rem .65rem;
    vertical-align: top;
    min-width: 120px;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
}
.chat-bubble-assistant th {
    background: rgba(15, 23, 42, .04);
    text-align: left;
    font-weight: 600;
}

/* Indicador de tiempo de respuesta — píldora discreta al pie de la burbuja. */
.chat-bubble-meta {
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    margin-top: .5rem;
    padding: .15rem .5rem;
    border-radius: 999px;
    background: rgba(15, 23, 42, .05);
    border: 1px solid rgba(15, 23, 42, .08);
    color: rgba(15, 23, 42, .55);
    font-size: .7rem;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    user-select: none;
}
.chat-bubble-meta i {
    font-size: .75rem;
    opacity: .8;
}

/* Typing indicator */
.chat-typing .chat-bubble {
    display: inline-flex;
    gap: 4px;
    padding: .9rem 1rem;
    position: relative;
    isolation: isolate;
    /* Aura externa que respira, en tonos del azul Undesa. */
    animation: chat-typing-aura 2.4s ease-in-out infinite;
}

/* Animar el ángulo del conic-gradient requiere @property. Si el navegador
   no lo soporta, el borde queda como un gradiente estático (sigue
   reforzando el "estoy pensando" junto a los puntos y el aura). */
@property --typing-angle {
    syntax: '<angle>';
    initial-value: 0deg;
    inherits: false;
}

/* Brillo que recorre todo el perímetro de la burbuja. La técnica:
   1. Pseudo-elemento absoluto con padding = grosor del "borde".
   2. background: conic-gradient anclado al ángulo --typing-angle.
   3. mask con "exclude" deja sólo el anillo del padding (borde luminoso).
   4. La animación gira --typing-angle de 0 a 360°. */
.chat-typing .chat-bubble::before {
    content: "";
    position: absolute;
    inset: 0;
    padding: 2px;
    border-radius: inherit;
    background: conic-gradient(
        from var(--typing-angle),
        rgba(56, 189, 248, 0) 0deg,
        rgba(56, 189, 248, 0) 220deg,
        rgba(56, 189, 248, .65) 270deg,
        var(--undesa-primary) 315deg,
        var(--undesa-primary-dark) 350deg,
        rgba(56, 189, 248, 0) 360deg
    );
    -webkit-mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
            mask-composite: exclude;
    animation: chat-typing-spin 2s linear infinite;
    pointer-events: none;
    z-index: 1;
}

@keyframes chat-typing-spin {
    to { --typing-angle: 360deg; }
}
@keyframes chat-typing-aura {
    0%, 100% { box-shadow: var(--shadow-sm), 0 0 0 0 rgba(13, 110, 253, 0); }
    50%      { box-shadow: var(--shadow-sm), 0 0 22px 2px rgba(13, 110, 253, .28); }
}

@media (prefers-reduced-motion: reduce) {
    .chat-typing .chat-bubble,
    .chat-typing .chat-bubble::before { animation: none; }
}

.typing-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: #94a3b8;
    animation: typing-bounce 1.2s infinite ease-in-out;
    position: relative;
    z-index: 2;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes typing-bounce {
    0%, 60%, 100% { transform: translateY(0);   opacity: .4; }
    30%           { transform: translateY(-4px); opacity: 1; }
}

/* ── Progreso en vivo (SSE): "FIDiAS está trabajando…" ─────────────── */
.chat-bubble.chat-progress {
    min-width: 240px;
    position: relative;
    /* La card "respira" con un glow suave: nunca se ve congelada. */
    animation: chat-progress-breath 2.4s ease-in-out infinite;
}
@keyframes chat-progress-breath {
    0%, 100% { box-shadow: 0 0 0 0 rgba(13, 110, 253, 0); }
    50%      { box-shadow: 0 0 16px 1px rgba(13, 110, 253, .18); }
}
.chat-progress-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--undesa-primary-dark);
    margin-bottom: 8px;
    animation: chat-progress-title 2s ease-in-out infinite;
}
@keyframes chat-progress-title {
    0%, 100% { opacity: 1; }
    50%      { opacity: .68; }
}
.chat-progress-bar-wrap {
    position: relative;
    height: 5px;
    background: var(--undesa-primary-soft);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 10px;
}
.chat-progress-bar {
    position: relative;
    height: 100%;
    width: 6%;
    background: linear-gradient(90deg, var(--undesa-primary), var(--undesa-accent));
    border-radius: 3px;
    overflow: hidden;
    transition: width .5s cubic-bezier(.4, 0, .2, 1);
}
/* Brillo que recorre la barra en bucle → movimiento constante aunque el % no cambie. */
.chat-progress-bar::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .6), transparent);
    transform: translateX(-100%);
    animation: chat-progress-shimmer 1.3s ease-in-out infinite;
}
@keyframes chat-progress-shimmer {
    0%   { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}
.chat-progress-steps {
    display: flex;
    flex-direction: column;
    gap: 5px;
}
.chat-progress-step {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12.5px;
    color: var(--chat-muted);
    opacity: .7;
    transition: opacity .2s, color .2s;
}
.chat-progress-step .cps-ico { flex: 0 0 auto; }
.chat-progress-step .cps-msg { flex: 1 1 auto; }
.chat-progress-step .cps-state { flex: 0 0 auto; font-size: 12px; }
.chat-progress-step.activo { opacity: 1; color: var(--undesa-primary-dark); font-weight: 500; }
.chat-progress-step.activo .cps-msg { animation: chat-step-pulse 1.6s ease-in-out infinite; }
@keyframes chat-step-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .55; } }
.chat-progress-step.done   { opacity: .6; color: #059669; }
.chat-progress-step.error  { opacity: 1; color: #dc2626; }

/* Streaming de texto: cada trozo se AGREGA con fade-in (nada de re-render del
   subárbol, que era lo que parpadeaba). Un cursor de tecleo acompaña mientras
   fluye; al finalizar, el markdown entra con un fade suave. */
.chat-stream-answer {
    white-space: pre-wrap;
    word-break: break-word;
}
.chat-stream-answer::after {
    content: '';
    display: inline-block;
    width: 2px;
    height: 1em;
    margin-left: 2px;
    background: currentColor;
    vertical-align: text-bottom;
    animation: chat-caret 1s steps(1) infinite;
}
.chat-stream-chunk { animation: chat-chunk-in .28s ease-out; }
.chat-stream-final { animation: chat-stream-fade .22s ease-out; }
@keyframes chat-chunk-in  { from { opacity: 0; } to { opacity: 1; } }
@keyframes chat-stream-fade { from { opacity: .35; } to { opacity: 1; } }
@keyframes chat-caret { 0%, 50% { opacity: .55; } 51%, 100% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
    .chat-stream-chunk, .chat-stream-final { animation: none; }
    .chat-stream-answer::after { animation: none; opacity: .55; }
}
.cps-spin {
    display: inline-block;
    width: 12px; height: 12px;
    border: 2px solid var(--undesa-primary-soft);
    border-top-color: var(--undesa-primary);
    border-radius: 50%;
    animation: chat-typing-spin .8s linear infinite;
}

/* Botón flotante "ir al último mensaje". El JS ajusta `--composer-h` con la
   altura real del composer para que el botón nunca se monte sobre el textarea. */
.chat-scroll-bottom {
    position: fixed;
    right: .85rem;
    bottom: calc(var(--composer-h, 92px) + 10px + env(safe-area-inset-bottom));
    z-index: 1030;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--chat-border);
    background: var(--chat-surface);
    color: var(--chat-text);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
    cursor: pointer;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity .15s ease, transform .15s ease, background .15s, color .15s;
    pointer-events: none;
}
.chat-scroll-bottom[hidden] { display: none; }
.chat-scroll-bottom.is-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
.chat-scroll-bottom:hover {
    background: var(--undesa-primary);
    color: #fff;
    border-color: var(--undesa-primary);
}
.chat-scroll-bottom i { font-size: 1.25rem; line-height: 1; }
.chat-scroll-bottom .chat-scroll-bottom-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--undesa-primary);
    color: #fff;
    font-size: .68rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--chat-surface);
}

/* Composer */
.chat-composer {
    border-top: 1px solid var(--chat-border);
    background: var(--chat-surface);
    padding: .85rem 1rem;
    padding-bottom: max(.85rem, env(safe-area-inset-bottom));
    box-shadow: 0 -6px 20px -12px rgba(15, 23, 42, .12);
}
.chat-composer-inner {
    display: flex;
    align-items: flex-end;
    gap: .5rem;
    background: #f1f4fa;
    border-radius: 22px;
    padding: .35rem .35rem .35rem .95rem;
    border: 1px solid #e2e8f0;
    transition: border-color .15s ease, box-shadow .2s ease, background .15s ease;
}
.chat-composer-inner:focus-within {
    border-color: var(--undesa-primary);
    background: #fff;
    box-shadow: 0 0 0 4px rgba(13, 110, 253, .14);
}
.chat-composer--drag .chat-composer-inner {
    border-color: var(--undesa-primary);
    background: #eaf1ff;
    box-shadow: 0 0 0 4px rgba(13, 110, 253, .18);
}
.chat-input {
    flex: 1;
    border: none;
    background: transparent;
    resize: none;
    outline: none;
    padding: .55rem 0;
    font: inherit;
    font-size: .95rem;
    color: #0f172a;
    max-height: 200px;
    line-height: 1.45;
}
.chat-input::placeholder { color: #94a3b8; }
.chat-composer-actions {
    display: flex;
    align-items: center;
    gap: .25rem;
}
.btn-send {
    width: 38px; height: 38px;
    padding: 0;
    border-radius: 50%;
    display: inline-flex;
    align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--undesa-primary) 0%, var(--undesa-primary-dark) 100%);
    border: none;
    box-shadow: 0 6px 14px -6px rgba(13, 110, 253, .6);
    transition: transform .15s ease, box-shadow .2s ease, filter .15s ease;
}
.btn-send:hover:not(:disabled) {
    transform: translateY(-1px);
    filter: brightness(1.05);
    box-shadow: 0 10px 20px -8px rgba(13, 110, 253, .7);
}
.btn-send:active { transform: translateY(0); }
.btn-send:disabled {
    opacity: .4;
    box-shadow: none;
    background: #cbd5e1;
}
.chat-composer-hint {
    font-size: .72rem;
    margin-top: .4rem;
    padding: 0 .5rem;
    color: #94a3b8;
}
.chat-mic-active {
    color: #dc3545 !important;
    animation: mic-pulse 1.2s infinite;
}
@keyframes mic-pulse {
    0%, 100% { transform: scale(1); }
    50%      { transform: scale(1.15); }
}

/* Responsive */
@media (max-width: 767.98px) {
    .login-card { padding: 1.75rem 1.4rem; border-radius: 18px; }
    .login-logo { width: 64px; height: 64px; }
    .login-header h1 { font-size: 1.15rem; }

    .chat-bubble { max-width: 92%; padding: .7rem .85rem; }
    .chat-messages { padding: .8rem .45rem; }
    .chat-composer { padding: .6rem .5rem; padding-bottom: max(.6rem, env(safe-area-inset-bottom)); }
    .chat-composer-inner { padding: .3rem .3rem .3rem .8rem; }
    .chat-composer-hint { padding: 0 .3rem; font-size: .68rem; }

    /* Header: reducir padding y separación entre botones para que no se desborde
       el botón de cerrar sesión a la derecha en pantallas estrechas. */
    .chat-header { padding: .75rem .55rem; padding-top: max(.75rem, env(safe-area-inset-top)); }
    .chat-header .gap-1 { gap: 0 !important; }
    .chat-header .btn-link { padding: .2rem .35rem !important; }
    .chat-header .chat-avatar { width: 34px; height: 34px; font-size: .95rem; }
    .chat-header .me-2 { margin-right: .5rem !important; }
    .chat-header .lh-sm small { font-size: .68rem; }

    /* Botón de adjuntar/abrir modal de subida (icono cloud-arrow) se prioriza
       sobre el espacio horizontal, sin agrandar el header. */
    #sessions-toggle { padding: .2rem .35rem !important; margin-right: .15rem !important; }

    /* Estado vacío compacto: avatar y márgenes reducidos para que el
       composer siempre quede visible sin scroll. */
    .chat-empty {
        margin: 1rem auto;
    }
    .chat-empty .chat-avatar-lg {
        width: 56px;
        height: 56px;
        font-size: 1.3rem;
        margin-bottom: .5rem !important;
    }
    .chat-empty h2 { font-size: 1.05rem; }
    .chat-empty p  { margin-bottom: 1rem !important; }
    .chat-suggestion {
        padding: .6rem .8rem;
        font-size: .88rem;
    }
}

/* Desktop: limitar ancho del chat para mejor legibilidad */
@media (min-width: 1200px) {
    .chat-messages {
        padding: 1.5rem 2rem;
    }
    .chat-row { max-width: 980px; margin-left: auto; margin-right: auto; }
    .chat-composer-inner { max-width: 980px; margin: 0 auto; }
    .chat-composer-hint { max-width: 980px; margin-left: auto; margin-right: auto; }
}

/* Pantallas muy bajas (iPhone SE landscape, navegadores con barra alta) */
@media (max-height: 640px) {
    .chat-empty { margin: .5rem auto; }
    .chat-empty .chat-avatar-lg {
        width: 44px; height: 44px; font-size: 1.1rem;
        margin-bottom: .35rem !important;
    }
    .chat-empty p { margin-bottom: .75rem !important; }
}

/* ============================================================
   ===== Adjuntos (PDF) =======================================
   ============================================================ */

/* Botón de clip dentro del composer */
.chat-attach-btn {
    align-self: flex-end;
    padding: .25rem .4rem;
    margin-bottom: .15rem;
    font-size: 1.1rem;
    line-height: 1;
}
.chat-attach-btn:hover { color: var(--undesa-primary); }

/* Chip de adjunto pendiente (sobre la barra del composer) */
.chat-attachments {
    display: flex;
    flex-direction: column;
    max-height: 220px;
    overflow-y: auto;
}

.chat-attachment {
    display: flex;
    align-items: center;
    gap: .65rem;
    background: #eef3ff;
    border: 1px solid #c9d8ff;
    border-radius: 12px;
    padding: .55rem .65rem;
    margin-bottom: .5rem;
}

.chat-attachment-icon {
    width: 36px; height: 36px;
    flex-shrink: 0;
    background: #dc3545;
    color: #fff;
    border-radius: 8px;
    display: inline-flex;
    align-items: center; justify-content: center;
    font-size: 1.1rem;
}

.chat-attachment-info {
    flex: 1;
    min-width: 0;
    line-height: 1.2;
}

.chat-attachment-name {
    font-size: .9rem;
    font-weight: 500;
    color: var(--chat-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-attachment-meta {
    font-size: .75rem;
    color: var(--chat-muted);
    margin-top: 2px;
}

.chat-attachment-progress {
    height: 3px;
    background: rgba(13,110,253,.15);
    border-radius: 2px;
    margin-top: .35rem;
    overflow: hidden;
}
.chat-attachment-bar {
    height: 100%;
    width: 0%;
    background: var(--undesa-primary);
    transition: width .15s ease;
}

.chat-attachment-remove {
    flex-shrink: 0;
    background: transparent;
    border: none;
    color: var(--chat-muted);
    width: 28px; height: 28px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center; justify-content: center;
    cursor: pointer;
    transition: background .15s, color .15s;
}
.chat-attachment-remove:hover {
    background: rgba(0,0,0,.06);
    color: #dc3545;
}

/* Chip de archivo dentro de la burbuja del usuario */
.chat-bubble-file {
    display: flex;
    align-items: center;
    gap: .55rem;
    background: rgba(255,255,255,.18);
    border: 1px solid rgba(255,255,255,.25);
    border-radius: 10px;
    padding: .45rem .6rem;
    margin-bottom: .4rem;
    text-decoration: none;
    color: inherit;
    max-width: 320px;
}
.chat-bubble-file:hover {
    background: rgba(255,255,255,.28);
    color: inherit;
}
.chat-bubble-file i {
    font-size: 1.5rem;
    color: #ffd6d6;
    flex-shrink: 0;
}
.chat-bubble-file-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.2;
}
.chat-bubble-file-name {
    font-size: .85rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat-bubble-file-meta {
    font-size: .72rem;
    opacity: .85;
    margin-top: 1px;
}

/* En burbuja de usuario (fondo azul) el chip va más "vidrioso";
   en assistant (poco usado, pero por si acaso) lo dejamos sobrio. */
.chat-bubble-assistant .chat-bubble-file {
    background: #f8fafc;
    border-color: var(--chat-border);
}
.chat-bubble-assistant .chat-bubble-file i { color: #dc3545; }

/* ===== Botón de descarga (archivos que el agente devuelve) =====
   Aparece dentro de .chat-bubble-assistant cuando la respuesta incluye
   una URL bajo /downloads/fidias/. El icono respeta la clase text-*
   asignada por fileIconClass(tipo) en JS. */
.chat-download-btn {
    display: inline-flex;
    align-items: center;
    gap: .65rem;
    width: 100%;
    max-width: 360px;
    margin: .35rem 0;
    padding: .55rem .7rem;
    background: #fff;
    border: 1px solid var(--chat-border);
    border-radius: 12px;
    box-shadow: var(--shadow-sm);
    color: inherit;
    text-align: left;
    cursor: pointer;
    transition: transform .12s ease, box-shadow .15s ease, border-color .15s ease;
}
.chat-download-btn:hover {
    transform: translateY(-1px);
    border-color: rgba(13, 110, 253, .35);
    box-shadow: 0 6px 18px -10px rgba(13, 110, 253, .45);
}
.chat-download-btn:active { transform: none; }
.chat-download-btn:focus-visible {
    outline: 2px solid rgba(13, 110, 253, .55);
    outline-offset: 2px;
}
.chat-download-btn.is-loading { opacity: .65; cursor: progress; }

.chat-download-icon {
    flex-shrink: 0;
    width: 36px; height: 36px;
    display: inline-flex; align-items: center; justify-content: center;
    background: rgba(15, 23, 42, .04);
    border-radius: 9px;
}
.chat-download-icon i { font-size: 1.25rem; }

.chat-download-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
    flex: 1 1 auto;
    line-height: 1.25;
}
.chat-download-name {
    font-size: .9rem;
    font-weight: 600;
    color: #0f172a;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat-download-meta {
    font-size: .72rem;
    color: #64748b;
    margin-top: 2px;
}

.chat-download-action {
    flex-shrink: 0;
    width: 32px; height: 32px;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--chat-user-bg, #0d6efd);
    color: #fff;
    border-radius: 8px;
    font-size: .95rem;
    transition: background .15s ease;
}
.chat-download-btn:hover .chat-download-action {
    background: #0b5ed7;
}
.chat-download-btn.is-loading .chat-download-action i {
    animation: chat-download-spin .8s linear infinite;
}
@keyframes chat-download-spin {
    to { transform: rotate(360deg); }
}

/* ===== Panel de sesiones (historial) ===== */
.sessions-panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(320px, 86vw);
    background: #fff;
    border-right: 1px solid var(--chat-border);
    display: flex;
    flex-direction: column;
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.2, .8, .2, 1);
    z-index: 1050;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    box-shadow: 12px 0 36px -16px rgba(15, 23, 42, .25);
}
.sessions-panel.is-open { transform: translateX(0); }

.sessions-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .9rem 1rem;
    border-bottom: 1px solid var(--chat-border);
    font-weight: 600;
    letter-spacing: -0.01em;
}

.sessions-new {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: .85rem;
    padding: .6rem .75rem;
    border: 1px solid rgba(13, 110, 253, .25);
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
    border-radius: 12px;
    font-weight: 600;
    font-size: .9rem;
    cursor: pointer;
    transition: background .15s ease, transform .1s ease, box-shadow .2s ease;
}
.sessions-new:hover {
    background: #dde9ff;
    box-shadow: 0 6px 14px -6px rgba(13, 110, 253, .35);
}
.sessions-new:active { transform: translateY(1px); }

.sessions-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 .5rem .75rem;
}

.session-item {
    display: block;
    width: 100%;
    text-align: left;
    background: transparent;
    border: 0;
    border-radius: 10px;
    padding: .65rem .75rem;
    margin-bottom: 4px;
    color: #1f2937;
    cursor: pointer;
    transition: background .15s ease, color .15s ease;
}
.session-item:hover { background: #f1f5f9; }
.session-item.is-active {
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
    font-weight: 600;
}
.session-item-title {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: .875rem;
    line-height: 1.25;
}
.session-item-meta {
    font-size: .72rem;
    color: #6b7280;
    margin-top: 2px;
}
.session-item.is-active .session-item-meta { color: var(--undesa-primary-dark, #0a58ca); }

.sessions-empty { padding: 1rem; }

.sessions-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .35);
    z-index: 1040;
}
@media (min-width: 992px) {
    /* En desktop el backdrop oscurece menos para que el chat siga visible. */
    .sessions-backdrop { background: rgba(0, 0, 0, .15); }
}

/* ===== Notificaciones (campanita) ===== */
.lex-notif-btn { position: relative; }
.lex-notif-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: #ef4444;
    color: #fff;
    font-size: .65rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--undesa-primary-dark);
    line-height: 1;
}

.lex-notif-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .35);
    z-index: 1055;
}
.lex-notif-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 380px;
    background: var(--chat-surface);
    z-index: 1060;
    display: flex;
    flex-direction: column;
    box-shadow: -12px 0 40px -12px rgba(15, 23, 42, .25);
    animation: lex-notif-slide-in .2s ease-out;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
}
@keyframes lex-notif-slide-in {
    from { transform: translateX(100%); opacity: .6; }
    to   { transform: translateX(0);    opacity: 1; }
}

.lex-notif-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: .75rem;
    padding: 1rem 1.1rem .75rem;
    border-bottom: 1px solid var(--chat-border);
}
.lex-notif-title {
    font-size: 1rem;
    font-weight: 700;
    margin: 0;
    color: var(--chat-text);
    letter-spacing: -.01em;
}
.lex-notif-subtitle {
    margin: .15rem 0 0;
    font-size: .78rem;
    color: var(--chat-muted);
}
.lex-notif-close {
    background: transparent;
    border: 0;
    width: 32px; height: 32px;
    border-radius: 8px;
    color: var(--chat-muted);
    display: flex; align-items: center; justify-content: center;
    transition: background .15s;
}
.lex-notif-close:hover { background: var(--chat-border); color: var(--chat-text); }

.lex-notif-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: .25rem 0;
}
.lex-notif-empty {
    padding: 2.5rem 1.25rem;
    text-align: center;
    color: var(--chat-muted);
    font-size: .9rem;
}

.lex-notif-item {
    display: flex;
    align-items: flex-start;
    gap: .75rem;
    width: 100%;
    padding: .8rem 1.1rem;
    background: transparent;
    border: 0;
    border-bottom: 1px solid var(--chat-border);
    text-align: left;
    cursor: pointer;
    transition: background .15s;
}
.lex-notif-item:hover { background: var(--chat-bg); }
.lex-notif-item:last-child { border-bottom: 0; }

.lex-notif-unread { background: var(--undesa-primary-soft); }
.lex-notif-unread:hover { background: #dde8ff; }
.lex-notif-unread .lex-notif-msg { font-weight: 600; }

.lex-notif-icon {
    width: 36px; height: 36px;
    border-radius: 10px;
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    font-size: 1.05rem;
}
.lex-notif-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: .15rem;
}
.lex-notif-msg {
    font-size: .9rem;
    color: var(--chat-text);
    line-height: 1.35;
    word-break: break-word;
}
.lex-notif-time {
    font-size: .72rem;
    color: var(--chat-muted);
}

@media (max-width: 480px) {
    .lex-notif-panel { max-width: 100%; }
}

/* ===== Modal: Indexar documento en LEX ===== */
body.lex-modal-open { overflow: hidden; }

.lex-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .55);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    z-index: 1060;
    animation: lex-fade-in .15s ease-out;
}

.lex-modal {
    position: fixed;
    inset: 0;
    z-index: 1070;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    padding-top: max(1rem, env(safe-area-inset-top));
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

.lex-modal-dialog {
    width: 100%;
    max-width: 520px;
    max-height: calc(100dvh - 2rem);
    background: var(--chat-surface);
    border-radius: 18px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--chat-border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: lex-pop-in .18s cubic-bezier(.2,.9,.4,1.2);
}

@keyframes lex-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}
@keyframes lex-pop-in {
    from { opacity: 0; transform: translateY(8px) scale(.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

.lex-modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: .75rem;
    padding: 1.1rem 1.25rem .75rem;
    border-bottom: 1px solid var(--chat-border);
}
.lex-modal-title-wrap { display: flex; gap: .75rem; align-items: center; min-width: 0; }
.lex-modal-icon {
    width: 42px; height: 42px;
    border-radius: 12px;
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary);
    display: flex; align-items: center; justify-content: center;
    font-size: 1.25rem;
    flex-shrink: 0;
}
.lex-modal-title {
    font-size: 1.05rem;
    font-weight: 700;
    margin: 0;
    color: var(--chat-text);
    letter-spacing: -.01em;
}
.lex-modal-subtitle {
    margin: .1rem 0 0;
    font-size: .82rem;
    color: var(--chat-muted);
}
.lex-modal-close {
    background: transparent;
    border: 0;
    width: 32px; height: 32px;
    border-radius: 8px;
    color: var(--chat-muted);
    display: flex; align-items: center; justify-content: center;
    transition: background .15s;
    flex-shrink: 0;
}
.lex-modal-close:hover { background: var(--chat-border); color: var(--chat-text); }

.lex-modal-body {
    padding: 1rem 1.25rem 1.1rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.lex-field { display: flex; flex-direction: column; gap: .4rem; }
.lex-label {
    font-size: .82rem;
    font-weight: 600;
    color: var(--chat-text);
    margin: 0;
}

.lex-select {
    border-radius: 10px;
    border: 1px solid var(--chat-border);
    padding: .6rem .75rem;
    font-size: .92rem;
    background-color: #fff;
}
.lex-select:focus {
    border-color: var(--undesa-primary);
    box-shadow: 0 0 0 3px rgba(13, 110, 253, .15);
}

.lex-dropzone {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .35rem;
    text-align: center;
    padding: 1.4rem 1rem;
    border: 1.5px dashed var(--chat-border);
    border-radius: 12px;
    background: var(--chat-bg);
    color: var(--chat-muted);
    cursor: pointer;
    transition: border-color .15s, background .15s, color .15s;
}
.lex-dropzone:hover {
    border-color: var(--undesa-primary);
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
}
.lex-dropzone-drag {
    border-color: var(--undesa-primary);
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
}
.lex-dropzone-hidden { display: none; }
.lex-dropzone-icon { font-size: 1.75rem; color: var(--undesa-primary); }
.lex-dropzone-text { font-size: .9rem; color: var(--chat-text); }
.lex-dropzone-text strong { color: var(--undesa-primary); }
.lex-dropzone-hint { font-size: .75rem; }

.lex-selected {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .75rem .9rem;
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 12px;
}
.lex-selected-icon {
    font-size: 1.6rem;
    color: var(--undesa-primary);
    flex-shrink: 0;
}
.lex-selected-info { flex: 1; min-width: 0; }
.lex-selected-name {
    font-weight: 600;
    font-size: .9rem;
    color: var(--chat-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.lex-selected-meta {
    font-size: .75rem;
    color: var(--chat-muted);
}
.lex-selected-clear {
    background: transparent;
    border: 0;
    width: 30px; height: 30px;
    border-radius: 8px;
    color: var(--chat-muted);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: background .15s, color .15s;
}
.lex-selected-clear:hover { background: #fee2e2; color: #b91c1c; }
.lex-selected-clear:disabled { opacity: .5; cursor: not-allowed; }

/* ---- Subida múltiple: cola de archivos ---- */
.lex-dropzone-compact { padding: .8rem 1rem; }
.lex-dropzone-disabled { opacity: .55; pointer-events: none; }

.lex-files {
    display: flex;
    flex-direction: column;
    gap: .4rem;
    margin-top: .6rem;
    max-height: 240px;
    overflow-y: auto;
}
.lex-file-row {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .55rem .7rem;
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 10px;
}
.lex-file-status {
    font-size: 1.05rem;
    flex-shrink: 0;
    width: 1.5rem;
    text-align: center;
}
.lex-file-info { flex: 1; min-width: 0; }
.lex-file-name {
    font-weight: 600;
    font-size: .85rem;
    color: var(--chat-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.lex-file-meta {
    font-size: .72rem;
    color: var(--chat-muted);
}
.lex-file-meta-error { color: #b91c1c; }
.lex-file-meta-done  { color: #166534; }

.lex-file-progress {
    margin-top: .3rem;
    height: 4px;
    border-radius: 2px;
    background: var(--chat-border);
    overflow: hidden;
}
.lex-file-progress-bar {
    height: 100%;
    width: 0%;
    border-radius: 2px;
    background: var(--undesa-primary);
    transition: width .2s ease;
}
.lex-file-progress-indexing {
    background: repeating-linear-gradient(
        45deg,
        var(--undesa-primary),
        var(--undesa-primary) 8px,
        rgba(13, 110, 253, .55) 8px,
        rgba(13, 110, 253, .55) 16px
    );
    animation: lex-indexing-stripes 1s linear infinite;
}
@keyframes lex-indexing-stripes {
    from { background-position: 0 0; }
    to   { background-position: 22px 0; }
}
.lex-file-remove { width: 26px; height: 26px; }

/* ---- Gestión de carpetas LEX ---- */
.lex-folder-new {
    display: flex;
    gap: .5rem;
}
.lex-folder-new input { flex: 1; }
.lex-folder-preview {
    margin-top: .4rem;
    font-size: .78rem;
    color: var(--chat-muted);
}
.lex-folder-preview code {
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 6px;
    padding: 1px 6px;
}
.lex-folders-list {
    display: flex;
    flex-direction: column;
    gap: .4rem;
    max-height: 300px;
    overflow-y: auto;
}
.lex-folder-aviso {
    font-size: .75rem;
    color: #92400e;
    background: #fef3c7;
    border: 1px solid rgba(146, 64, 14, .25);
    border-radius: 8px;
    padding: .4rem .6rem;
}
.lex-folder-row {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .5rem .7rem;
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 10px;
}
.lex-folder-icon {
    font-size: 1.15rem;
    color: var(--undesa-primary);
    flex-shrink: 0;
}
.lex-folder-info { flex: 1; min-width: 0; }
.lex-folder-nombre {
    font-weight: 600;
    font-size: .85rem;
    color: var(--chat-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.lex-folder-meta {
    font-size: .72rem;
    color: var(--chat-muted);
}
.lex-folder-acciones {
    display: flex;
    gap: .35rem;
    flex-shrink: 0;
}

/* Progreso general del lote */
.lex-overall {
    display: flex;
    align-items: center;
    gap: .65rem;
    margin-top: .6rem;
}
.lex-overall-track {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: var(--chat-border);
    overflow: hidden;
}
.lex-overall-bar {
    height: 100%;
    width: 0%;
    border-radius: 3px;
    background: var(--undesa-primary);
    transition: width .25s ease;
}
.lex-overall-text {
    font-size: .75rem;
    color: var(--chat-muted);
    white-space: nowrap;
}

.lex-status {
    padding: .65rem .85rem;
    border-radius: 10px;
    font-size: .85rem;
    line-height: 1.35;
}
.lex-status-info {
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
    border: 1px solid rgba(13, 110, 253, .2);
}
.lex-status-success {
    background: #dcfce7;
    color: #166534;
    border: 1px solid rgba(22, 101, 52, .2);
}
.lex-status-error {
    background: #fee2e2;
    color: #b91c1c;
    border: 1px solid rgba(185, 28, 28, .2);
}

.lex-modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: .5rem;
    padding-top: .25rem;
}
.lex-modal-footer .btn {
    border-radius: 10px;
    padding: .55rem 1.1rem;
    font-weight: 600;
    font-size: .9rem;
}

@media (max-width: 480px) {
    .lex-modal { padding: 0; align-items: flex-end; }
    .lex-modal-dialog {
        max-width: 100%;
        max-height: 92dvh;
        border-radius: 18px 18px 0 0;
        animation: lex-slide-up .2s ease-out;
    }
}
@keyframes lex-slide-up {
    from { transform: translateY(20px); opacity: 0; }
    to   { transform: translateY(0); opacity: 1; }
}

/* ===== Documentos Indexados LEX ===== */

.lex-docs-body {
    background: var(--chat-bg);
    height: 100dvh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    /* Sin overflow:hidden el scroll cae al <body> y la tabla nunca queda
       constreñida. Con esto, el scroll vive dentro de .lex-docs-table-wrap. */
    overflow: hidden;
}

.lex-docs-main {
    flex: 1 1 auto;
    width: 100%;
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.25rem 1rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    /* CRÍTICO: sin min-height:0 los hijos flex se niegan a contraerse y
       el overflow del table-wrap nunca dispara. */
    min-height: 0;
}

.lex-docs-toolbar {
    display: flex;
    align-items: center;
    gap: .65rem;
    flex-wrap: wrap;
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    border-radius: 14px;
    padding: .75rem .9rem;
    box-shadow: var(--shadow-sm);
}

.lex-docs-search {
    position: relative;
    flex: 1 1 240px;
    min-width: 200px;
}
.lex-docs-search i {
    position: absolute;
    left: .8rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--chat-muted);
    pointer-events: none;
}
.lex-docs-search input {
    border-radius: 10px;
    padding-left: 2.1rem;
    border: 1px solid var(--chat-border);
    background: var(--chat-bg);
    font-size: .92rem;
}
.lex-docs-search input:focus {
    border-color: var(--undesa-primary);
    box-shadow: 0 0 0 3px rgba(13, 110, 253, .15);
    background: #fff;
}

.lex-docs-filter { flex: 0 0 auto; }
.lex-docs-filter .lex-select { min-width: 180px; }

.lex-docs-stats {
    display: flex;
    align-items: center;
    gap: .4rem;
    margin-left: auto;
    font-size: .82rem;
    color: var(--chat-muted);
    white-space: nowrap;
}
.lex-stat strong {
    color: var(--chat-text);
    font-weight: 700;
    margin-right: .15rem;
}
.lex-stat-divider { opacity: .5; }

.lex-docs-table-wrap {
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    border-radius: 14px;
    box-shadow: var(--shadow-sm);
    /* auto = scroll vertical y horizontal cuando hagan falta. El border-radius
       sigue recortando los bordes del contenido scrolleado. */
    overflow: auto;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.lex-docs-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1.5rem;
    text-align: center;
}
.lex-docs-state-icon {
    font-size: 2.4rem;
    color: var(--undesa-primary);
    opacity: .65;
}
.lex-docs-state .spinner-border {
    width: 2rem; height: 2rem;
    color: var(--undesa-primary);
}

.lex-docs-empty-filter {
    padding: 2rem 1rem 2.5rem;
    text-align: center;
}

.lex-docs-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: .9rem;
}
.lex-docs-table thead th {
    position: sticky;
    top: 0;
    background: var(--chat-bg);
    color: var(--chat-muted);
    font-weight: 600;
    font-size: .76rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--chat-border);
    z-index: 1;
}
.lex-docs-table tbody td {
    padding: .85rem 1rem;
    border-bottom: 1px solid var(--chat-border);
    color: var(--chat-text);
    vertical-align: middle;
}
.lex-docs-table tbody tr:last-child td { border-bottom: 0; }
.lex-docs-table tbody tr {
    transition: background-color .12s ease;
}
.lex-docs-table tbody tr:hover { background: rgba(13, 110, 253, .035); }

.lex-row-busy { opacity: .55; pointer-events: none; }
.lex-row-busy td { position: relative; }

.lex-col-chunks   { width: 130px; }
.lex-col-acciones { width: 140px; }
.lex-col-carpeta  { width: 200px; }

.lex-archivo-cell {
    display: flex;
    align-items: center;
    gap: .55rem;
    min-width: 0;
}
.lex-archivo-icon {
    color: var(--undesa-primary);
    font-size: 1.15rem;
    flex-shrink: 0;
}
.lex-archivo-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
    font-weight: 500;
}

.lex-chunks-pill {
    display: inline-block;
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
    border-radius: 999px;
    padding: .2rem .65rem;
    font-weight: 600;
    font-size: .78rem;
    min-width: 38px;
    text-align: center;
}

.lex-badge {
    display: inline-block;
    padding: .25rem .6rem;
    border-radius: 999px;
    font-size: .72rem;
    font-weight: 600;
    letter-spacing: .01em;
    line-height: 1.2;
    border: 1px solid transparent;
    white-space: nowrap;
}
.lex-badge-blue   { background: #dbeafe; color: #1e40af; border-color: rgba(30,64,175,.18); }
.lex-badge-indigo { background: #e0e7ff; color: #3730a3; border-color: rgba(55,48,163,.18); }
.lex-badge-violet { background: #ede9fe; color: #5b21b6; border-color: rgba(91,33,182,.18); }
.lex-badge-amber  { background: #fef3c7; color: #92400e; border-color: rgba(146,64,14,.2);  }
.lex-badge-teal   { background: #ccfbf1; color: #115e59; border-color: rgba(17,94,89,.2);   }
.lex-badge-rose   { background: #ffe4e6; color: #9f1239; border-color: rgba(159,18,57,.18); }
.lex-badge-emerald{ background: #d1fae5; color: #065f46; border-color: rgba(6,95,70,.18);   }
.lex-badge-slate  { background: #e2e8f0; color: #334155; border-color: rgba(51,65,85,.18);  }

.lex-actions {
    display: inline-flex;
    gap: .35rem;
}
.lex-action-btn {
    background: transparent;
    border: 1px solid var(--chat-border);
    color: var(--chat-muted);
    width: 34px; height: 34px;
    border-radius: 9px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    cursor: pointer;
    transition: background-color .12s, color .12s, border-color .12s, transform .12s;
}
.lex-action-btn:hover {
    background: var(--undesa-primary-soft);
    color: var(--undesa-primary-dark);
    border-color: rgba(13,110,253,.3);
}
.lex-action-btn:active { transform: scale(.96); }
.lex-action-btn:disabled {
    opacity: .55;
    cursor: not-allowed;
    transform: none;
}
.lex-action-delete:hover {
    background: #fee2e2;
    color: #b91c1c;
    border-color: rgba(185,28,28,.25);
}
.lex-action-reindex:hover i { animation: lex-spin .6s linear; }
@keyframes lex-spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

/* Botón del header del chat para entrar al gestor (oculto si !acceso_lex) */
.lex-docs-nav-btn[hidden] { display: none !important; }

@media (max-width: 720px) {
    .lex-docs-stats { width: 100%; justify-content: flex-end; }
    .lex-col-carpeta { width: auto; }
    .lex-col-chunks  { width: 90px; }
    .lex-col-acciones { width: 110px; }
    .lex-docs-table thead th { padding: .6rem .65rem; }
    .lex-docs-table tbody td { padding: .7rem .65rem; }
    .lex-archivo-name { font-size: .85rem; }
}

@media (max-width: 480px) {
    .lex-docs-main { padding: .9rem .6rem 2rem; }
    .lex-docs-toolbar { padding: .65rem .65rem; }
    .lex-docs-filter .lex-select { min-width: 0; width: 100%; }
    .lex-badge { font-size: .68rem; padding: .2rem .5rem; }
}

/* ============================================================
   Selector de modelo (⚡/⚖️/🧠), badge de modelo y feedback 👍👎
   ============================================================ */
.chat-model-selector {
    display: flex; align-items: center; gap: .35rem; flex-wrap: wrap;
    margin: 0 0 .5rem 0; padding: 0 .15rem;
}
.chat-model-opt {
    border: 1px solid rgba(0,0,0,.12); background: #fff; color: #475569;
    font-size: .78rem; font-weight: 600; line-height: 1;
    padding: .35rem .6rem; border-radius: 999px; cursor: pointer;
    transition: background .15s, color .15s, border-color .15s;
}
.chat-model-opt:hover { border-color: #94a3b8; }
.chat-model-opt.is-active { background: #1e3a8a; border-color: #1e3a8a; color: #fff; }
.chat-model-warn { font-size: .72rem; color: #b45309; margin-left: .25rem; }

.chat-bubble-meta .chat-model-badge {
    display: inline-block; font-size: .68rem; font-weight: 700;
    padding: .12rem .45rem; border-radius: 999px; margin-right: .4rem; color: #fff;
}
.chat-model-badge.modelo-haiku  { background: #38bdf8; }
.chat-model-badge.modelo-sonnet { background: #2563eb; }
.chat-model-badge.modelo-opus   { background: #1e3a8a; }
.chat-model-badge.modelo-otro   { background: #64748b; }

.chat-feedback { display: inline-flex; gap: .3rem; margin-left: .5rem; vertical-align: middle; }
.chat-feedback button {
    border: none; background: transparent; cursor: pointer; font-size: .85rem;
    line-height: 1; opacity: .35; padding: .1rem .2rem; border-radius: 6px;
    transition: opacity .15s, transform .1s;
}
.chat-row-assistant:hover .chat-feedback button { opacity: .6; }
.chat-feedback button:hover { transform: scale(1.15); opacity: 1; }
.chat-feedback button.fb-up.is-active  { opacity: 1; color: #16a34a; }
.chat-feedback button.fb-down.is-active { opacity: 1; color: #dc2626; }
.chat-feedback button[disabled] { cursor: default; }
.chat-feedback .fb-note-icon { font-size: .7rem; color: #dc2626; opacity: .8; margin-left: .15rem; }
@media (max-width: 575.98px) { .chat-feedback button { opacity: .55; } }


/* ──────────────────────────────────────────────────────────────────
   Mis Reportes Programados (panel de tareas de FIDiAS)
   Reusa el contenedor .lex-notif-panel; esto estila las tarjetas.
   ────────────────────────────────────────────────────────────────── */
.tarea-item {
    padding: .85rem 1.1rem;
    border-bottom: 1px solid var(--chat-border);
}
.tarea-item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: .35rem;
}
.tarea-badge {
    display: inline-flex;
    align-items: center;
    font-size: .72rem;
    font-weight: 600;
    color: var(--undesa-primary, #0d6efd);
    background: rgba(13, 110, 253, .08);
    border-radius: 999px;
    padding: .15rem .6rem;
}
.tarea-id { font-size: .75rem; }
.tarea-desc {
    font-weight: 600;
    font-size: .92rem;
    color: var(--chat-text);
    margin-bottom: .35rem;
    line-height: 1.35;
}
.tarea-meta {
    display: flex;
    flex-wrap: wrap;
    gap: .35rem .9rem;
    font-size: .78rem;
    color: var(--chat-muted);
    margin-bottom: .25rem;
}
.tarea-actions {
    display: flex;
    gap: .5rem;
    margin-top: .55rem;
}
.tarea-actions .btn { font-size: .78rem; padding: .25rem .6rem; }
.tareas-footer {
    padding: .7rem 1.1rem;
    border-top: 1px solid var(--chat-border);
    font-size: .75rem;
    line-height: 1.4;
}
