/* === SISTEMA DE GRÁFICOS OTIMIZADO === */

/* Configuração global para evitar conflitos */
.chart-container * {
    box-sizing: border-box;
}

/* Grid dos gráficos - sobrescreve o style.css */
.charts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 2rem;
    margin: 2rem 0;
    max-width: 100%;
}

/* Container base dos gráficos - PADDING REDUZIDO */
.chart-container {
    position: relative !important;
    width: 100%;
    height: 400px !important; /* Aumentado de 370px */
    max-height: 400px !important;
    min-height: 350px; /* Aumentado */
    margin-bottom: 2rem;
    padding: 1.5rem !important; /* REDUZIDO de 2.5rem para 1.5rem */
    background: var(--card);
    border-radius: var(--radius);
    border: 1px solid var(--borda);
    box-shadow: var(--sombra);
    overflow: visible !important;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

/* Hover effect sutil */
.chart-container:hover {
    transform: translateY(-2px);
    box-shadow: 
        var(--sombra), 
        0 8px 25px rgba(0, 0, 0, 0.1);
}

/* Canvas dos gráficos - TAMANHOS MAIORES */
.chart-container canvas {
    max-width: 100% !important;
    max-height: 320px !important; /* Aumentado de 250px para 320px */
    width: 100% !important; /* Força ocupar toda largura */
    height: auto !important;
    display: block !important;
    margin: 0 auto !important;
    padding: 5px !important; /* REDUZIDO de 15px para 5px */
    box-sizing: border-box !important;
}

/* Wrapper interno do Chart.js */
.chart-container .chartjs-render-monitor {
    padding: 10px !important; /* REDUZIDO de 20px */
    margin: 5px !important; /* REDUZIDO de 10px */
}

/* Títulos dos gráficos - ESPAÇO REDUZIDO */
.chart-container h3 {
    margin: 0 0 1rem 0 !important; /* REDUZIDO de 2rem para 1rem */
    padding-bottom: 0.5rem; /* REDUZIDO */
    border-bottom: 2px solid var(--borda);
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--texto);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-align: left !important;
}

/* Ações dos gráficos */
.chart-actions {
    position: absolute;
    top: 1rem !important; /* REDUZIDO de 2rem */
    right: 1rem !important; /* REDUZIDO de 2rem */
    display: flex;
    gap: 0.5rem;
    z-index: 10;
    justify-content: flex-start !important;
    margin-top: 0 !important;
}

/* Botões dos gráficos */
.btn-chart {
    background: var(--fundo-secundario, rgba(255, 255, 255, 0.9)) !important;
    border: 1px solid var(--borda) !important;
    border-radius: 6px !important;
    padding: 0.4rem !important; /* REDUZIDO */
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    width: auto !important;
    height: auto !important;
    font-size: 0.8rem !important; /* REDUZIDO */
}

.btn-chart:hover {
    background: var(--azul) !important;
    color: white !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.btn-chart:active {
    transform: translateY(0);
}

/* === DASHBOARD - AJUSTES ESPECÍFICOS === */

/* Gráficos no dashboard - TAMANHOS MAIORES */
#dashboard .chart-container {
    height: 420px !important; /* AUMENTADO de 380px */
    max-height: 420px !important;
    padding: 1.25rem !important; /* Padding reduzido mas confortável */
}

#dashboard .chart-container canvas {
    max-height: 340px !important; /* MUITO MAIOR - era 280px */
    height: 340px !important; /* Força altura específica */
    width: 100% !important;
    padding: 5px !important;
}

/* Título do dashboard com menos espaço */
#dashboard .chart-container h3 {
    margin-bottom: 0.75rem !important; /* REDUZIDO */
    font-size: 1.05rem !important; /* Ligeiramente menor */
    padding-bottom: 0.4rem;
}

/* Ações mais compactas no dashboard */
#dashboard .chart-actions {
    top: 0.75rem !important;
    right: 0.75rem !important;
}

#dashboard .btn-chart {
    padding: 0.35rem !important;
    font-size: 0.75rem !important;
}

/* Grid do dashboard - mais espaço */
#dashboard .charts-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 1.5rem !important; /* Reduzido para dar mais espaço aos gráficos */
}

/* Gráfico de pizza no dashboard */
#dashboard #grafico-pizza {
    max-height: 340px !important;
    height: 340px !important;
}

/* Gráfico de barras no dashboard */
#dashboard #grafico-barras {
    max-height: 340px !important;
    height: 340px !important;
}

/* Wrapper do Chart.js no dashboard */
#dashboard .chart-container .chartjs-render-monitor {
    padding: 5px !important; /* MUITO REDUZIDO */
    margin: 2px !important; /* MUITO REDUZIDO */
}

/* === PÁGINA ANÁLISE - GRÁFICO MAIOR === */

/* Container específico para a página análise */
#analise .chart-container {
    grid-column: 1 / -1 !important;
    height: 500px !important; /* MUITO MAIOR - era 420px */
    max-height: 500px !important;
    padding: 1.5rem !important; /* Padding consistente */
}

#analise .chart-container canvas {
    max-height: 420px !important; /* MUITO MAIOR - era 300px */
    height: 420px !important; /* Força altura específica */
}

#analise .chart-container h3 {
    margin-bottom: 1.5rem !important; /* Pouco mais de espaço na análise */
}

/* === ESTADOS ESPECIAIS === */

/* Estado vazio */
.chart-empty {
    display: flex !important;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100% !important;
    text-align: center;
    padding: 2rem 1rem !important; /* REDUZIDO */
    color: var(--texto-secundario);
    min-height: 200px !important;
    background: transparent !important;
    border: none !important;
}

.chart-empty-icon {
    font-size: 2.5rem !important; /* REDUZIDO */
    margin-bottom: 1rem !important; /* REDUZIDO */
    opacity: 0.6;
    filter: grayscale(50%);
}

.chart-empty-message {
    font-size: 1rem !important; /* REDUZIDO */
    margin-bottom: 1.5rem !important; /* REDUZIDO */
    line-height: 1.4;
    max-width: 280px;
}

/* Estado de erro */
.chart-error {
    display: flex !important;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100% !important;
    text-align: center;
    padding: 2rem 1rem !important; /* REDUZIDO */
    color: var(--vermelho, #e74c3c);
    min-height: 200px !important;
    background: transparent !important;
    border: none !important;
}

.chart-error-icon {
    font-size: 2rem !important; /* REDUZIDO */
    margin-bottom: 1rem !important; /* REDUZIDO */
    opacity: 0.8;
}

.chart-error-message {
    font-size: 0.95rem !important; /* REDUZIDO */
    margin-bottom: 1.5rem !important; /* REDUZIDO */
    line-height: 1.4;
}

/* Estado de carregamento */
.chart-loading {
    display: flex !important;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100% !important;
    color: var(--texto-secundario);
    text-align: center;
    padding: 2rem 1rem !important; /* REDUZIDO */
}

.chart-loading-text {
    margin-bottom: 1rem !important; /* REDUZIDO */
    font-size: 0.95rem; /* REDUZIDO */
}

.chart-loading-spinner {
    width: 35px !important; /* REDUZIDO */
    height: 35px !important; /* REDUZIDO */
    border: 3px solid var(--borda) !important; /* REDUZIDO */
    border-top: 3px solid var(--azul) !important; /* REDUZIDO */
    border-radius: 50%;
    animation: chartSpin 1s linear infinite;
    margin-left: 0 !important;
}

@keyframes chartSpin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* === CORES CUSTOMIZADAS === */
:root {
    --chart-cor-alimentacao: #E74C3C;
    --chart-cor-transporte: #3498DB;
    --chart-cor-lazer: #9B59B6;
    --chart-cor-saude: #2ECC71;
    --chart-cor-educacao: #F39C12;
    --chart-cor-vestuario: #E67E22;
    --chart-cor-casa: #34495E;
    --chart-cor-moradia: #8E44AD;
    --chart-cor-seguros: #16A085;
    --chart-cor-outros: #95A5A6;
    
    --chart-cor-salario: #27AE60;
    --chart-cor-freelance: #3498DB;
    --chart-cor-investimentos: #8E44AD;
    --chart-cor-bonus: #F39C12;
    --chart-cor-vendas: #E67E22;
}

/* === RESPONSIVIDADE DASHBOARD === */

/* Tablets - dashboard */
@media (max-width: 1024px) {
    .charts-grid {
        grid-template-columns: 1fr !important;
        gap: 1.5rem;
    }
    
    .chart-container {
        height: 370px !important; /* AUMENTADO */
        max-height: 370px !important;
        padding: 1.25rem !important;
    }
    
    .chart-container canvas {
        max-height: 280px !important; /* AUMENTADO */
        padding: 5px !important;
    }
    
    .chart-actions {
        top: 1rem !important;
        right: 1rem !important;
    }
    
    #dashboard .charts-grid {
        grid-template-columns: 1fr !important; /* Coluna única em tablet */
        gap: 1.25rem !important;
    }
    
    #dashboard .chart-container {
        height: 400px !important;
        max-height: 400px !important;
        padding: 1rem !important;
    }
    
    #dashboard .chart-container canvas {
        max-height: 320px !important;
        height: 320px !important;
    }
    
    /* Análise em tablet */
    #analise .chart-container {
        height: 450px !important; /* MAIOR */
        max-height: 450px !important;
    }
    
    #analise .chart-container canvas {
        max-height: 350px !important; /* MAIOR */
        height: 350px !important;
    }
}

/* Mobile */
@media (max-width: 768px) {
    .charts-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem;
        margin: 1rem 0;
    }
    
    .chart-container {
        height: 320px !important; /* MANTIDO */
        max-height: 320px !important;
        padding: 1rem !important; /* REDUZIDO */
        margin-bottom: 1rem;
    }
    
    .chart-container canvas {
        max-height: 240px !important; /* AUMENTADO de 200px */
        padding: 5px !important;
    }
    
    .chart-container h3 {
        font-size: 1rem !important;
        padding-bottom: 0.4rem;
        margin-bottom: 1rem !important; /* REDUZIDO */
    }
    
    .chart-actions {
        top: 0.75rem !important;
        right: 0.75rem !important;
    }
    
    .btn-chart {
        padding: 0.35rem !important;
        font-size: 0.75rem !important;
    }
    
    /* Dashboard mobile */
    #dashboard .chart-container {
        height: 340px !important; /* AUMENTADO para mobile */
        max-height: 340px !important;
        padding: 0.75rem !important;
    }
    
    #dashboard .chart-container canvas {
        max-height: 280px !important; /* MAIOR no mobile */
        height: 280px !important;
    }
    
    #dashboard .chart-container h3 {
        margin-bottom: 0.5rem !important;
        font-size: 0.95rem !important;
    }
    
    #dashboard .charts-grid {
        gap: 1rem !important;
    }
    
    /* Estados especiais em mobile */
    .chart-empty,
    .chart-error {
        padding: 1.5rem 1rem !important; /* REDUZIDO */
        min-height: 120px !important; /* REDUZIDO */
    }
    
    .chart-empty-icon,
    .chart-error-icon {
        font-size: 1.8rem !important; /* REDUZIDO */
        margin-bottom: 0.75rem !important;
    }
    
    .chart-empty-message,
    .chart-error-message {
        font-size: 0.9rem !important;
        margin-bottom: 1rem !important;
    }
    
    /* Análise em mobile */
    #analise .chart-container {
        height: 350px !important; /* MAIOR */
        max-height: 350px !important;
    }
    
    #analise .chart-container canvas {
        max-height: 270px !important; /* MAIOR */
        height: 270px !important;
    }
}

/* Mobile muito pequeno */
@media (max-width: 480px) {
    .chart-container {
        height: 280px !important; /* REDUZIDO ligeiramente */
        max-height: 280px !important;
        padding: 0.75rem !important; /* REDUZIDO */
    }
    
    .chart-container canvas {
        max-height: 200px !important;
        padding: 3px !important;
    }
    
    .chart-container h3 {
        margin-bottom: 0.75rem !important;
        font-size: 0.95rem !important;
    }
    
    .chart-actions {
        top: 0.5rem !important;
        right: 0.5rem !important;
    }
    
    /* Dashboard mobile pequeno */
    #dashboard .chart-container {
        height: 300px !important;
        max-height: 300px !important;
        padding: 0.5rem !important;
    }
    
    #dashboard .chart-container canvas {
        max-height: 240px !important;
        height: 240px !important;
    }
    
    #dashboard .chart-container h3 {
        margin-bottom: 0.4rem !important;
        font-size: 0.9rem !important;
    }
    
    .chart-empty,
    .chart-error {
        min-height: 100px !important;
        padding: 1rem 0.75rem !important;
    }
    
    /* Análise em mobile pequeno */
    #analise .chart-container {
        height: 300px !important;
        max-height: 300px !important;
    }
    
    #analise .chart-container canvas {
        max-height: 220px !important;
        height: 220px !important;
    }
}

/* === OTIMIZAÇÕES E CORREÇÕES === */

/* Garantir que os gráficos não quebrem o layout */
.charts-grid .chart-container:last-child {
    margin-bottom: 0 !important;
}

/* Performance */
.chart-container,
.chart-container canvas {
    will-change: auto;
}

/* Wrapper adicional para Chart.js */
.chart-wrapper {
    width: 100%;
    height: 100%;
    padding: 10px; /* REDUZIDO de 20px */
    box-sizing: border-box;
}

/* Legenda com espaçamento controlado */
.chart-container .chartjs-legend {
    padding: 10px !important; /* REDUZIDO de 15px */
    margin: 5px 0 !important; /* REDUZIDO de 10px */
}

/* Tooltips */
.chart-container .chartjs-tooltip {
    max-width: 250px;
    word-wrap: break-word;
}

/* Elementos do gráfico */
.chart-container .Chart {
    margin: 10px !important; /* REDUZIDO de 20px */
    padding: 10px !important; /* REDUZIDO de 20px */
}

/* === ACESSIBILIDADE === */

.btn-chart:focus {
    outline: 2px solid var(--azul) !important;
    outline-offset: 2px;
}

/* === TEMAS === */

[data-theme="dark"] .chart-container {
    background: var(--card) !important;
    border-color: var(--borda) !important;
}

[data-theme="dark"] .btn-chart {
    background: rgba(255, 255, 255, 0.1) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: #fff !important;
}

[data-theme="dark"] .btn-chart:hover {
    background: var(--azul) !important;
}

/* === CORREÇÕES FINAIS === */

/* Remove conflitos */
.chart-container .chart-empty,
.chart-container .chart-error {
    background: transparent !important;
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
}

/* Force override para charts-grid */
.charts-grid.charts-grid {
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)) !important;
}

@media (max-width: 1024px) {
    .charts-grid.charts-grid {
        grid-template-columns: 1fr !important;
    }
}

@media (max-width: 768px) {
    .charts-grid.charts-grid {
        grid-template-columns: 1fr !important;
    }
}