:root {
    --primary-color: #6366f1;
    --primary-dark: #4f46e5;
    --secondary-color: #10b981;
    --background: #f8fafc;
    --surface: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --radius: 8px;
    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --stage-color-contato: #3b82f6;
    --stage-color-qualificacao: #10b981;
    --stage-color-proposta: #f59e0b;
    --stage-color-negociacao: #ef4444;
    --stage-color-fechamento: #8b5cf6;
}

[data-theme="dark"] {
    --background: #0f172a;
    --surface: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --border: #334155;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.3), 0 1px 2px 0 rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
    --stage-color-contato: #60a5fa;
    --stage-color-qualificacao: #34d399;
    --stage-color-proposta: #fbbf24;
    --stage-color-negociacao: #f87171;
    --stage-color-fechamento: #a78bfa;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--background);
    color: var(--text-primary);
    line-height: 1.6;
    display: flex;
    min-height: 100vh;
}

/* Login Overlay */
.login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 4000;
    backdrop-filter: blur(4px);
}

.login-container {
    background: var(--surface);
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 90%;
    max-width: 400px;
    text-align: center;
}

.login-container h2 {
    margin-bottom: 1.5rem;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.login-container .form-group {
    margin-bottom: 1rem;
    text-align: left;
}

.login-container .add-btn {
    margin-top: 1rem;
}

.login-error {
    color: #ef4444;
    font-size: 0.875rem;
    margin-top: 1rem;
}

/* Sidebar */
.sidebar {
    width: 250px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem 0;
    flex-shrink: 0;
    overflow-y: auto;
    transition: width 0.3s ease-in-out, padding 0.3s ease-in-out;
    z-index: 1000;
}

.sidebar.hidden {
    width: 0;
    padding: 0;
    overflow: hidden;
}

.sidebar ul {
    list-style: none;
}

.sidebar li {
    margin-bottom: 0.5rem;
}

.sidebar a {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    color: var(--text-secondary);
    text-decoration: none;
    transition: var(--transition);
    border-left: 3px solid transparent;
}

.sidebar a:hover,
.sidebar a.active {
    background: rgba(99, 102, 241, 0.05);
    color: var(--primary-color);
    border-left-color: var(--primary-color);
}

.sidebar i {
    width: 20px;
    text-align: center;
}

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: margin-left 0.3s ease-in-out;
}

.main-content.full-width {
    margin-left: 0;
    width: 100%;
}

.breadcrumbs {
    padding: 1rem 2rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}

.breadcrumbs a {
    color: var(--primary-color);
    text-decoration: none;
    font-size: 0.875rem;
}

.breadcrumbs span {
    color: var(--text-secondary);
}

.header {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
    color: white;
    padding: 1rem 2rem;
    box-shadow: var(--shadow-lg);
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
    opacity: 0.1;
}

.header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin: 0;
    position: relative;
    z-index: 1;
    flex: 1;
    text-align: center;
}

.header-actions {
    display: flex;
    gap: 1rem;
    align-items: center;
}

#theme-toggle {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.3);
    padding: 0.75rem 1.5rem;  /* Isso aumenta a largura e altura do botão */
    border-radius: var(--radius);
    cursor: pointer;
    transition: var(--transition);
    min-width: 60px;  /* Largura mínima para facilitar o clique */
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;  /* Ícone um pouco maior, opcional */
}

#theme-toggle:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
}

.logout-btn {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.3);
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 500;
    transition: var(--transition);
    position: relative;
    z-index: 1;
    backdrop-filter: blur(10px);
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.logout-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.header .add-lead-btn,
.burger-btn {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.3);
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 500;
    transition: var(--transition);
    position: relative;
    z-index: 1;
    backdrop-filter: blur(10px);
    min-width: 44px;
    min-height: 44px;
}

.header .add-lead-btn:hover,
.burger-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.burger-btn {
    background: none;
    border: none;
    padding: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.content {
    padding: 2rem;
    flex: 1;
    overflow-y: auto;
}

.search-filter {
    margin-bottom: 2rem;
    position: relative;
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
    align-items: center;
}

.search-filter input {
    flex: 1;
    min-width: 300px;
    padding: 0.75rem 1rem 0.75rem 3rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 1rem;
    transition: var(--transition);
    background: var(--surface);
    min-height: 44px;
}

.search-filter input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.search-filter i {
    position: absolute;
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
}

.filters {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

.filters select,
.filters input {
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    min-height: 44px;
}

.dashboard {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.widget {
    background: var(--surface);
    padding: 1.5rem;
    border-radius: var(--radius);
    text-align: center;
    box-shadow: var(--shadow);
    transition: var(--transition);
    border: 1px solid var(--border);
    cursor: move;
    user-select: none;
    position: relative;
}

/* Linha de widgets do topo do dashboard */
.widgets-row {
    display: grid;
    grid-template-columns: repeat(1, 1fr);
    gap: 1.5rem;
}

@media (min-width: 700px) {
    .widgets-row {
        /* manter colunas responsivas com largura mínima para evitar compressão */
        grid-template-columns: repeat(2, minmax(220px, 1fr));
    }
}

@media (min-width: 1100px) {
    .widgets-row {
        grid-template-columns: repeat(4, minmax(220px, 1fr));
    }
}

.widget:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.widget.dragging {
    opacity: 0.5;
    z-index: 100;
}

.stats {
    /* Não ocupar múltiplas colunas por padrão para evitar empilhamento indesejado.
       Em telas muito largas, itens ainda podem expandir. */
    grid-column: span 1;
}

/* Tentar preencher lacunas e manter widgets lado a lado sempre que possível */
.dashboard {
    grid-auto-flow: dense;
}

/* Em telas largas, permitir que a grade do dashboard se adapte sem forçar 4 colunas fixas.
   Isso evita que os widgets principais fiquem muito estreitos em telas com largura limitada. */
@media (min-width: 900px) {
    .dashboard {
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    }
    .stats {
        grid-column: span 1;
    }
}

.funnel-container {
    display: flex;
    gap: 1.5rem;
    overflow-x: auto;
    padding: 1rem 0;
    scrollbar-width: thin;
    scrollbar-color: var(--primary-color) transparent;
    flex-direction: row;
    flex-wrap: nowrap; /* manter os stages em linha com rolagem horizontal se necessário */
}

.funnel-container::-webkit-scrollbar {
    height: 6px;
}

.funnel-container::-webkit-scrollbar-track {
    background: var(--border);
}

.funnel-container::-webkit-scrollbar-thumb {
    background: var(--primary-color);
    border-radius: 3px;
}

.stage {
    flex: 0 0 240px;
    min-width: 220px;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
    transition: var(--transition);
    border: 1px solid var(--border);
    max-height: 80vh;
    overflow-y: auto;
}

.stage:hover {
    box-shadow: var(--shadow-lg);
}

.stage-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);
}

.stage-title {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 1.125rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.stage-title i {
    color: var(--primary-color);
}

.stage[data-stage="Primeiro Contato"] .stage-title i { color: var(--stage-color-contato); }
.stage[data-stage="Qualificação"] .stage-title i { color: var(--stage-color-qualificacao); }
.stage[data-stage="Proposta"] .stage-title i { color: var(--stage-color-proposta); }
.stage[data-stage="Negociação"] .stage-title i { color: var(--stage-color-negociacao); }
.stage[data-stage="Fechamento"] .stage-title i { color: var(--stage-color-fechamento); }

.stage-count {
    background: var(--primary-color);
    color: white;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    font-weight: 600;
}

.lead-card {
    background: #f1f5f9;
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 1rem;
    cursor: move;
    border-left: 4px solid var(--primary-color);
    transition: var(--transition);
    position: relative;
    overflow: hidden;
    animation: slideInUp 0.3s ease-out;
}

@keyframes slideInUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.lead-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
}

.lead-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
    background: #e2e8f0;
}

.lead-card.dragging {
    opacity: 0.5;
    transform: rotate(5deg) scale(1.05);
    animation: shake 0.5s ease-in-out;
}

@keyframes shake {
    0%, 100% { transform: rotate(0) scale(1.05); }
    25% { transform: rotate(5deg) scale(1.05); }
    75% { transform: rotate(-5deg) scale(1.05); }
}

[data-tooltip] {
    position: relative;
}

[data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-primary);
    color: white;
    padding: 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    z-index: 10;
    margin-bottom: 0.5rem;
}

.lead-name {
    font-weight: 600;
    margin-bottom: 0.5rem; /* Aumentado para garantir espaço */
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-right: 80px; /* Espaço reservado para os botões de ação */
}

.lead-name i {
    color: var(--secondary-color);
}

.lead-info {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem; /* Aumentado para consistência */
}

.lead-stage {
    font-size: 0.75rem;
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary-color);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-weight: 500;
}

.lead-actions {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    display: flex;
    gap: 0.25rem;
    opacity: 1; /* Sempre visível para facilitar a interação */
}

.lead-actions button {
    background: rgba(255, 255, 255, 0.8);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 4px;
    transition: var(--transition);
    font-size: 0.875rem;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lead-actions button:hover {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.add-lead-btn {
    background: var(--secondary-color);
    color: white;
    border: none;
    padding: 0.75rem;
    border-radius: var(--radius);
    cursor: pointer;
    width: 100%;
    margin-top: 1rem;
    font-weight: 500;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 44px;
}

.add-lead-btn:hover {
    background: #059669;
    transform: translateY(-1px);
}

.modal {
    display: none;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--surface);
    margin: 5% auto;
    padding: 2rem;
    border-radius: var(--radius);
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    transform: scale(0.95);
    animation: scaleIn 0.2s ease-out forwards;
    position: relative;
    display: flex;
    flex-direction: column;
}

@keyframes scaleIn {
    to { transform: scale(1); }
}

.close {
    color: var(--text-secondary);
    font-size: 1.75rem;
    font-weight: bold;
    cursor: pointer;
    position: absolute;
    top: 1rem;
    right: 1rem;
    transition: var(--transition);
}

.close:hover {
    color: var(--text-primary);
    transform: scale(1.1);
}

.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--text-primary);
    font-size: 0.875rem;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 1rem;
    transition: var(--transition);
    background: var(--surface);
    min-height: 44px;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.add-btn {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius);
    cursor: pointer;
    width: 100%;
    font-weight: 500;
    transition: var(--transition);
    margin-top: 1rem;
    min-height: 44px;
}

.add-btn:hover {
    background: var(--primary-dark);
    transform: translateY(-1px);
}

.delete-btn {
    background: #ef4444;
    color: white;
}

.delete-btn:hover {
    background: #dc2626;
}

.toast {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 1rem 1.5rem;
    border-radius: var(--radius);
    color: white;
    font-weight: 500;
    z-index: 3000;
    transform: translateX(100%);
    transition: transform 0.3s ease-out;
    min-width: 300px;
    box-shadow: var(--shadow-lg);
}

.toast.show {
    transform: translateX(0);
}

.toast.success {
    background: var(--secondary-color);
}

.toast.error {
    background: #ef4444;
}

.history-section {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.history-section h3 {
    margin-bottom: 1rem;
    color: var(--text-primary);
    font-size: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#history-list {
    max-height: 200px;
    overflow-y: auto;
}

#history-list li {
    padding: 0.75rem;
    background: #f8fafc;
    margin-bottom: 0.5rem;
    border-radius: var(--radius);
    border-left: 3px solid var(--primary-color);
    list-style: none;
    animation: fadeIn 0.3s ease-out;
}

@media (prefers-contrast: high) {
    :root {
        --text-primary: #000000;
        --text-secondary: #333333;
        --border: #000000;
    }
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        transform: translateX(-100%);
        z-index: 1001;
    }

    .sidebar.hidden {
        transform: translateX(-100%);
    }

    .sidebar:not(.hidden) {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0;
        width: 100%;
    }

    .main-content.full-width {
        margin-left: 0;
    }

    .burger-btn {
        display: flex;
    }

    .content {
        padding: 1rem;
    }

    .funnel-container {
        flex-direction: column;
        gap: 1rem;
    }

    .stats {
        grid-column: span 1 !important;
    }

    .search-filter {
        flex-direction: column;
        align-items: stretch;
    }

    .search-filter input {
        min-width: auto;
    }

    .filters {
        justify-content: center;
    }

    .breadcrumbs {
        padding: 1rem;
    }

    .header {
        padding: 1rem;
        flex-direction: row; /* Mantido como row para melhor layout */
        gap: 1rem;
    }

    .header h1 {
        font-size: 1.5rem;
    }

    .header-actions {
        justify-content: flex-end;
    }
}

/* Observação: ajustado para garantir que os widgets principais do dashboard fiquem em uma única linha em
    telas desktop, e o funil (stages) permaneça abaixo como uma linha separada. */

/* Mantém o funil como linha horizontal em telas maiores sem usar !important.
   Assim, as funções de navegação que ocultam o funil (display: none) continuam funcionando. */
@media (min-width: 600px) {
    .funnel-container {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap; /* manter os stages em linha com rolagem horizontal se necessário */
        align-items: flex-start;
        gap: 1.25rem;
        overflow-x: auto;
    }

    .funnel-container .stage {
        /* largura base que garante várias colunas lado-a-lado em desktop; a rolagem aparece quando necessário */
        flex: 0 0 260px;
        min-width: 220px;
        max-width: 360px;
        display: block;
    }
}

@media (min-width: 1200px) {
    .funnel-container .stage {
        flex: 0 0 20rem; /* colunas mais largas em telas muito grandes */
    }
}