/* ============================================
   Módulo Banco de Falhas & Chat IA
   Design Industrial Quadrado e Dark Mode
   OTIMIZADO: Layout responsivo + Desktop melhorado
   CORRECAO MOBILE: Botoes (2/linha) + Filtros (coluna unica)
   CORRECAO SCROLL: Prevenção de scroll no fundo quando chat aberto
   ============================================ */

/* ========== PREVENIR SCROLL DO FUNDO QUANDO CHAT ABERTO ========== */
/* Classe para travar o scroll do body quando modal de chat está aberto */
body.chat-modal-open,
body:has(.chat-fullscreen-modal),
body:has(.modal:has(#chatWindow)),
body:has(.chat-full-screen),
body:has(.voice-fullscreen-modal) {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

/* Garantir que o backdrop do modal também previna scroll */
.chat-fullscreen-modal,
.modal:has(#chatWindow),
.chat-full-screen,
.voice-fullscreen-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #000000;
    z-index: 1000;
}

/* Garantir que o conteúdo do modal seja o único com scroll */
.chat-fullscreen-modal .modal-body,
.modal:has(#chatWindow) .modal-body,
.chat-full-screen .modal-body,
.voice-fullscreen-modal .voice-transcription {
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1 1 auto;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
}

/* Para o overlay/modal pai */
.modal-overlay:has(.chat-fullscreen-modal),
.modal-overlay:has(#chatWindow),
.modal-overlay:has(.voice-fullscreen-modal) {
    position: fixed;
    overflow: hidden;
}

/* ========== BANCO DE FALHAS - LAYOUT PRINCIPAL ========== */
.failure-bank-module {
    display: flex;
    flex-direction: column;
    gap: 20px;
    padding: 0;
}

/* Header */
.failure-bank-module .module-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
    padding: 18px 24px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    border-left: 4px solid var(--accent-blue);
}

.failure-bank-module .module-title h2 {
    font-size: 20px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.failure-bank-module .module-title p {
    font-size: 13px;
    color: var(--text-muted);
    margin: 5px 0 0 0;
}

.failure-bank-module .module-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.failure-bank-module .module-actions button {
    padding: 10px 18px;
    font-size: 13px;
    font-weight: 500;
    transition: all var(--transition-fast);
}

.failure-bank-module .module-actions button:active {
    transform: scale(0.97);
}

/* Filtros */
.failure-bank-module .module-filters {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    align-items: flex-end;
    padding: 18px 20px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
}

.failure-bank-module .module-filters .form-group {
    flex: 1;
    min-width: 180px;
    margin-bottom: 0;
}

.failure-bank-module .module-filters .form-group label {
    display: block;
    font-size: 11px;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 6px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.failure-bank-module .module-filters .form-control {
    width: 100%;
}

.failure-bank-module .module-filters button {
    padding: 10px 24px;
    height: 42px;
    align-self: flex-end;
}

/* Conteúdo */
.failure-bank-module .module-content {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

/* ========== CATÁLOGO DE FALHAS (TABELA) ========== */
.failure-bank-module .data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.failure-bank-module .data-table th {
    padding: 14px 16px;
    text-align: left;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: 600;
    border-bottom: 1px solid var(--border-color);
}

.failure-bank-module .data-table td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
}

.failure-bank-module .data-table tbody tr:hover {
    background: var(--bg-hover);
}

/* Action buttons */
.action-buttons {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.btn-icon.btn-sm {
    padding: 8px 12px;
    font-size: 14px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    color: var(--text-muted);
}

.btn-icon.btn-sm:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--accent-blue);
}

/* Badges */
.badge {
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
}

/* Empty state */
.module-placeholder {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}

.module-placeholder i {
    font-size: 48px;
    margin-bottom: 16px;
    opacity: 0.4;
}

.module-placeholder h2 {
    font-size: 20px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

/* ========== MODAL DE FALHA ========== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    backdrop-filter: blur(4px);
}

.modal-container {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 700px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: failure-fadeInScale var(--transition-normal) ease;
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.modal-header h2, .modal-header h3 {
    margin: 0;
    font-size: 18px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    transition: color var(--transition-fast);
}

.modal-close:hover {
    color: var(--danger);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
}

.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.modal-footer button {
    padding: 10px 20px;
}

/* ========== FORMULÁRIO DE FALHA ========== */
#formFalha .form-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    margin-bottom: 16px;
}

#formFalha .form-group {
    margin-bottom: 16px;
}

#formFalha .form-group label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

#formFalha .form-group label i {
    margin-right: 6px;
    color: var(--accent-blue);
}

#formFalha .form-control {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 13px;
}

#formFalha .form-control:focus {
    outline: none;
    border-color: var(--accent-blue);
    box-shadow: var(--glow-blue);
}

textarea.form-control {
    resize: vertical;
    min-height: 80px;
}

/* ========== CHAT IA FULL SCREEN ========== */
/* Modal pai do chat */
.modal:has(#chatWindow),
.modal.chat-fullscreen-modal,
.chat-full-screen {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-width: 100%;
    max-height: 100%;
    margin: 0;
    padding: 0;
    z-index: 1000;
    display: flex;
    align-items: stretch;
    justify-content: stretch;
    background-color: #000000;
    border: none;
    overflow: hidden;
}

.modal-dialog:has(#chatWindow),
.chat-fullscreen-modal .modal-dialog,
.chat-full-screen .modal-dialog {
    position: relative;
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    background-color: #000000;
    border: none;
}

.modal-content:has(#chatWindow),
.chat-fullscreen-modal .modal-content,
.chat-full-screen .modal-content {
    position: relative;
    background-color: #000000;
    color: #e2e8f0;
    border-radius: 0;
    border: none;
    box-shadow: none;
    width: 100%;
    height: 100%;
    max-height: 100%;
    margin: 0;
    display: flex;
    flex-direction: column;
    flex: 1;
}

.modal-body:has(#chatWindow),
.chat-fullscreen-modal .modal-body,
.chat-full-screen .modal-body {
    position: relative;
    padding: 0;
    margin: 0;
    background-color: #000000;
    border: none;
    max-height: none;
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    overflow: hidden;
}

/* Chat Header */
.chat-header-fixed {
    background-color: #0a0e17;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow-sm);
    z-index: 10;
    flex-shrink: 0;
}

.chat-header-fixed h2 {
    margin: 0;
    font-size: 18px;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-primary);
}

/* Chat Window */
#chatWindow,
.chat-window {
    flex: 1 1 0;
    min-height: 0;
    height: auto;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
    background-color: #000000;
}

/* Mensagens */
.chat-message-row {
    display: flex;
    gap: 12px;
    max-width: 85%;
    animation: failure-fadeIn 0.3s ease-out;
}

.chat-message-row.user {
    align-self: flex-end;
    flex-direction: row-reverse;
    max-width: 90%;
}

.chat-message-row.ai {
    align-self: flex-start;
    max-width: 90%;
}

/* Balões (cantos retos - design industrial) */
.chat-user-bubble {
    background: #1e3a8a;
    color: #ffffff;
    padding: 12px 18px;
    border-radius: 0;
    font-size: 14px;
    box-shadow: var(--shadow-sm);
    line-height: 1.5;
    word-break: break-word;
}

.chat-ai-bubble {
    background: #0f172a;
    color: #e2e8f0;
    padding: 12px 18px;
    border-radius: 0;
    border: 1px solid #1e293b;
    font-size: 14px;
    box-shadow: var(--shadow-sm);
    line-height: 1.5;
    word-break: break-word;
}

.chat-ai-avatar {
    width: 36px;
    height: 36px;
    border-radius: 0;
    background: linear-gradient(135deg, var(--accent-purple), var(--accent-blue));
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    flex-shrink: 0;
    margin-top: auto;
    margin-bottom: auto;
    box-shadow: var(--shadow-sm);
}

/* Chat Footer */
.chat-footer-fixed {
    position: relative;
    background-color: #0a0e17;
    padding: 16px 24px;
    border-top: 1px solid #1e293b;
    border-bottom: none;
    border-radius: 0;
    margin: 0;
    flex-shrink: 0;
    z-index: 10;
}

.chat-input-wrapper {
    display: flex;
    gap: 12px;
    align-items: center;
    background-color: #020617;
    padding: 6px 6px 6px 18px;
    border-radius: var(--radius-md);
    border: 1px solid #334155;
    transition: all var(--transition-fast);
}

.chat-input-wrapper:focus-within {
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.4);
}

#inputChatMsg {
    flex: 1;
    min-width: 0;
    background: transparent;
    border: none;
    font-size: 14px;
    color: var(--text-primary);
    outline: none;
    padding: 10px 0;
}

#inputChatMsg::placeholder {
    color: var(--text-muted);
}

#btnEnviarChatIA {
    background: var(--accent-blue);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    width: 44px;
    height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all var(--transition-fast);
    box-shadow: var(--shadow-sm);
    padding: 0;
    margin: 0;
}

#btnEnviarChatIA i {
    font-size: 16px;
    margin: 0;
    display: block;
}

#btnEnviarChatIA span {
    display: none;
}

#btnEnviarChatIA:hover:not(:disabled) {
    background: var(--accent-cyan);
}

#btnEnviarChatIA:active:not(:disabled) {
    transform: scale(0.95);
}

#btnEnviarChatIA:disabled {
    background: var(--border-color);
    box-shadow: none;
    cursor: not-allowed;
}

/* ========== VOZ MODE SPECIFIC ========== */
.voice-fullscreen-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100dvh;
    background: #020617;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

.voice-mode-container { 
    position: relative; 
    width: 100%; 
    height: 100%; 
    background: #020617; 
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    justify-content: space-between; 
    font-family: system-ui, sans-serif; 
    color: white; 
    padding: 20px;
    overflow: hidden;
}

.voice-transcription { 
    flex: 1; 
    width: 100%; 
    max-width: 600px; 
    overflow-y: auto; 
    padding: 20px; 
    font-size: 20px; 
    line-height: 1.6; 
    text-align: left;
    -webkit-overflow-scrolling: touch;
}

.voice-orb { 
    width: 80px; 
    height: 80px; 
    border-radius: 50%; 
    background: linear-gradient(135deg, #3b82f6, #8b5cf6); 
    cursor: pointer; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    font-size: 24px; 
    transition: all 0.3s ease; 
    margin-bottom: 100px;
    flex-shrink: 0;
}

.state-speaking .voice-orb { 
    animation: failure-pulse 1s infinite alternate; 
}

.btn-end-call { 
    position: absolute; 
    bottom: 25px; 
    background: #ef4444; 
    color: white; 
    border: none; 
    width: 56px; 
    height: 56px; 
    border-radius: 50%; 
    font-size: 20px; 
    cursor: pointer; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    box-shadow: 0 4px 15px rgba(239, 68, 68, 0.4); 
    z-index: 11; 
}

.voice-header { 
    width: 100%; 
    text-align: right; 
    flex-shrink: 0;
}

.voice-header button { 
    background: none; 
    border: none; 
    color: white; 
    font-size: 24px; 
    cursor: pointer; 
    padding: 8px;
}

@keyframes failure-pulse { 
    from { transform: scale(1); } 
    to { transform: scale(1.1); box-shadow: 0 0 20px #10b981; } 
}

/* ============================================
   CORRECAO MOBILE - BOTOES E FILTROS
   ============================================ */

/* Tablet (481px - 768px) */
@media (min-width: 481px) and (max-width: 768px) {
    .failure-bank-module .module-header {
        flex-direction: column;
        align-items: stretch;
    }
    
    .failure-bank-module .module-actions {
        justify-content: center;
        flex-wrap: wrap;
    }
    
    .failure-bank-module .module-actions button {
        flex: 1 1 calc(33.333% - 10px);
        font-size: 12px;
        padding: 10px 12px;
        justify-content: center;
        white-space: normal;
        word-break: break-word;
    }
    
    .failure-bank-module .module-filters {
        flex-direction: column;
        align-items: stretch;
    }
    
    .failure-bank-module .module-filters button {
        width: 100%;
        margin-top: 8px;
    }
    
    .chat-message-row {
        max-width: 92%;
    }
    
    #chatWindow, .chat-window {
        padding: 20px;
    }
    
    .voice-transcription { 
        font-size: 18px; 
        padding: 18px; 
    }
    
    .voice-orb { 
        width: 75px; 
        height: 75px; 
        margin-bottom: 90px; 
    }
    
    .btn-end-call { 
        width: 52px; 
        height: 52px; 
        bottom: 22px; 
    }
}

/* Mobile (< 480px) - CORRECAO PRINCIPAL */
@media (max-width: 480px) {
    .failure-bank-module .module-header {
        padding: 16px;
        flex-direction: column;
        align-items: stretch;
    }
    
    .failure-bank-module .module-title h2 {
        font-size: 18px;
    }
    
    .failure-bank-module .module-title p {
        font-size: 11px;
    }
    
    /* BOTOES SUPERIORES: 2 por linha */
    .failure-bank-module .module-actions {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }
    
    .failure-bank-module .module-actions button {
        width: 100%;
        min-width: 0;
        font-size: 11px;
        padding: 10px 8px;
        justify-content: center;
        white-space: normal;
        word-break: break-word;
        text-align: center;
        margin: 0;
        box-sizing: border-box;
    }
    
    /* FILTROS: coluna única */
    .failure-bank-module .module-filters {
        padding: 14px;
        flex-direction: column;
        gap: 12px;
    }
    
    .failure-bank-module .module-filters .form-group {
        width: 100%;
        min-width: auto;
    }
    
    .failure-bank-module .module-filters button {
        width: 100%;
        margin-top: 0;
    }
    
    /* Tabela mobile - estilo cards */
    .failure-bank-module .data-table thead {
        display: none;
    }
    
    .failure-bank-module .data-table tbody tr {
        display: block;
        margin-bottom: 16px;
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        padding: 12px;
        background: var(--bg-card);
    }
    
    .failure-bank-module .data-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0;
        border-bottom: 1px solid var(--border-color);
        text-align: right;
        gap: 16px;
    }
    
    .failure-bank-module .data-table td:last-child {
        border-bottom: none;
    }
    
    .failure-bank-module .data-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        text-align: left;
        flex: 1;
        font-size: 11px;
        text-transform: uppercase;
        letter-spacing: 0.3px;
    }
    
    /* Labels para cada coluna no mobile */
    .failure-bank-module .data-table td:nth-child(1)::before { content: "Glossário"; }
    .failure-bank-module .data-table td:nth-child(2)::before { content: "Subconjunto"; }
    .failure-bank-module .data-table td:nth-child(3)::before { content: "Elemento"; }
    .failure-bank-module .data-table td:nth-child(4)::before { content: "Descrição"; }
    .failure-bank-module .data-table td:nth-child(5)::before { content: "Código"; }
    .failure-bank-module .data-table td:nth-child(6)::before { content: "Ações"; }
    
    .action-buttons {
        justify-content: flex-end;
    }
    
    /* Modal */
    .modal-container {
        width: 95%;
        max-height: 85vh;
    }
    
    .modal-header h2, .modal-header h3 {
        font-size: 16px;
    }
    
    .modal-footer {
        flex-direction: column;
    }
    
    .modal-footer button {
        width: 100%;
    }
    
    /* Formulário */
    #formFalha .form-row {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    /* Chat */
    .chat-header-fixed {
        padding: 12px 16px;
    }
    
    .chat-header-fixed h2 {
        font-size: 16px;
    }
    
    #chatWindow, .chat-window {
        padding: 16px 12px;
    }
    
    .chat-footer-fixed {
        padding: 12px 16px;
    }
    
    .chat-message-row {
        max-width: 95%;
    }
    
    .chat-user-bubble,
    .chat-ai-bubble {
        padding: 10px 14px;
        font-size: 13px;
    }
    
    .chat-ai-avatar {
        width: 30px;
        height: 30px;
    }
    
    .chat-input-wrapper {
        padding: 4px 4px 4px 12px;
    }
    
    #inputChatMsg {
        font-size: 14px;
        padding: 8px 0;
    }
    
    #btnEnviarChatIA {
        width: 40px;
        height: 40px;
        min-width: 40px;
    }
    
    #btnEnviarChatIA i {
        font-size: 14px;
    }
    
    /* Voice Mode Mobile */
    .voice-transcription { 
        font-size: 16px; 
        padding: 16px; 
    }
    
    .voice-orb { 
        width: 70px; 
        height: 70px; 
        margin-bottom: 80px; 
    }
    
    .btn-end-call { 
        width: 50px; 
        height: 50px; 
        bottom: 20px; 
    }
    
    .voice-header button { 
        font-size: 20px; 
        padding: 6px;
    }
}

/* Mobile medio (350px - 480px) - manter 2 colunas */
@media (min-width: 351px) and (max-width: 480px) {
    .failure-bank-module .module-actions {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Mobile muito pequeno (< 350px) - 1 coluna */
@media (max-width: 350px) {
    .failure-bank-module .module-actions {
        grid-template-columns: 1fr;
    }
    
    .voice-orb { 
        width: 60px; 
        height: 60px; 
        margin-bottom: 70px; 
    }
    
    .voice-transcription { 
        font-size: 14px; 
        padding: 14px; 
    }
    
    .btn-end-call { 
        width: 45px; 
        height: 45px; 
        bottom: 18px; 
    }
}

/* Desktop (> 768px) */
@media (min-width: 769px) {
    .failure-bank-module .module-actions button:hover {
        transform: translateY(-1px);
    }
    
    .chat-message-row {
        max-width: 80%;
    }
    
    .chat-user-bubble:hover,
    .chat-ai-bubble:hover {
        filter: brightness(1.02);
    }
}

/* Large Desktop (> 1200px) */
@media (min-width: 1201px) {
    .failure-bank-module .module-content {
    }
    
    .chat-message-row {
        max-width: 75%;
    }
}

/* ========== TOUCH OTIMIZAÇÕES ========== */
@media (hover: none) and (pointer: coarse) {
    .failure-bank-module .module-actions button:active,
    .btn-icon.btn-sm:active,
    .modal-footer button:active {
        transform: scale(0.96);
        transition: transform 0.05s ease;
    }
    
    .failure-bank-module .module-actions button:hover,
    .btn-icon.btn-sm:hover {
        transform: none;
    }
}

/* ========== ANIMAÇÕES ========== */
@keyframes failure-fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes failure-fadeInScale {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes chatBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* ========== REDUCED MOTION ========== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms;
        transition-duration: 0.01ms;
    }
    
    .chat-message-row {
        animation: none;
    }
}
