:root {
    /* Paleta de Cores Frias */
    --primary: #1e3a5f;
    --primary-dark: #0f2744;
    --primary-light: #2d5a8e;
    --secondary: #00838f;
    --secondary-dark: #005662;
    --secondary-light: #4fb3bf;
    --accent: #26a69a;
    --accent-dark: #00796b;
    --accent-light: #80cbc4;
    --info: #0288d1;
    --info-light: #b3e5fc;
    --success: #2e7d32;
    --success-light: #c8e6c9;
    --warning: #f9a825;
    --warning-light: #fff9c4;
    --danger: #c62828;
    --danger-light: #ffcdd2;
    --bg-main: #f0f4f8;
    --bg-card: #ffffff;
    --bg-sidebar: #1e3a5f;
    --text-primary: #1a2332;
    --text-secondary: #546e7a;
    --text-light: #90a4ae;
    --border: #cfd8dc;
    --border-light: #eceff1;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background-color: var(--bg-main);
    color: var(--text-primary);
    min-height: 100vh;
}

/* Layout Principal */
.app-layout {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 60px;
    background: linear-gradient(180deg, var(--primary) 0%, var(--primary-dark) 100%);
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 1000;
    transition: width 0.3s ease;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.sidebar.expanded {
    width: 240px;
}

.sidebar-header {
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    min-height: 64px;
}

.sidebar-header .logo-text {
    display: none;
    color: #fff;
    font-weight: 700;
    font-size: 1.1rem;
    white-space: nowrap;
}

.sidebar.expanded .logo-text {
    display: block;
}

.sidebar-toggle {
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: var(--transition);
}

.sidebar-toggle:hover {
    background: rgba(255,255,255,0.1);
}

.sidebar-nav {
    flex: 1;
    padding: 12px 8px;
    overflow-y: auto;
}

.nav-item {
    display: flex;
    align-items: center;
    padding: 12px;
    margin-bottom: 4px;
    border-radius: 8px;
    color: rgba(255,255,255,0.7);
    text-decoration: none;
    transition: var(--transition);
    white-space: nowrap;
}

.nav-item:hover, .nav-item.active {
    background: rgba(255,255,255,0.15);
    color: #fff;
}

.nav-item svg {
    width: 20px;
    height: 20px;
    min-width: 20px;
    margin-right: 12px;
}

.nav-item span {
    display: none;
    font-size: 0.875rem;
    font-weight: 500;
}

.sidebar.expanded .nav-item span {
    display: inline;
}

.nav-category {
    padding: 8px 12px;
    color: rgba(255,255,255,0.4);
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-top: 16px;
    display: none;
}

.sidebar.expanded .nav-category {
    display: block;
}

/* Conteúdo Principal */
.main-content {
    flex: 1;
    margin-left: 60px;
    padding: 24px;
    transition: margin-left 0.3s ease;
}

.sidebar.expanded ~ .main-content {
    margin-left: 240px;
}

/* Header da Página */
.page-header {
    margin-bottom: 24px;
}

.page-header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
}

.page-header p {
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Cards */
.card {
    background: var(--bg-card);
    border-radius: 12px;
    border: 1px solid var(--border-light);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
    transition: var(--transition);
}

.card:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

/* Stats Cards */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-card);
    border-radius: 12px;
    padding: 20px;
    border: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    gap: 16px;
}

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon.blue { background: var(--info-light); color: var(--info); }
.stat-icon.green { background: var(--success-light); color: var(--success); }
.stat-icon.teal { background: #e0f2f1; color: var(--accent-dark); }
.stat-icon.purple { background: #ede7f6; color: #5e35b1; }

.stat-info h3 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
}

.stat-info p {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

/* Tabelas */
.table-container {
    overflow-x: auto;
    border-radius: 12px;
    border: 1px solid var(--border-light);
    cursor: grab;
}

.table-container.table-dragging {
    cursor: grabbing;
    user-select: none;
}
.table-container.table-dragging * {
    user-select: none;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

thead {
    background: var(--bg-main);
}

th {
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}

td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-light);
    color: var(--text-primary);
}

tr:hover {
    background: rgba(0,131,143,0.03);
}

/* Badges */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-success { background: var(--success-light); color: var(--success); }
.badge-warning { background: var(--warning-light); color: #f57f17; }
.badge-danger { background: var(--danger-light); color: var(--danger); }
.badge-info { background: var(--info-light); color: var(--info); }

/* Botões */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    border: none;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}
.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-secondary {
    background: var(--secondary);
    color: #fff;
}
.btn-secondary:hover {
    background: var(--secondary-dark);
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-primary);
}
.btn-outline:hover {
    background: var(--bg-main);
    border-color: var(--primary);
    color: var(--primary);
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.8rem;
}

/* Inputs */
.input-group {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.form-input {
    padding: 10px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.875rem;
    transition: var(--transition);
    outline: none;
    width: 100%;
}

.form-input:focus {
    border-color: var(--secondary);
    box-shadow: 0 0 0 3px rgba(0,131,143,0.1);
}

select.form-input {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%23546e7a' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

/* Barra de progresso */
.progress-bar {
    height: 8px;
    background: var(--border-light);
    border-radius: 4px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.5s ease;
}

.progress-fill.low { background: var(--danger); }
.progress-fill.medium { background: var(--warning); }
.progress-fill.high { background: var(--success); }
.progress-fill.full { background: var(--secondary); }

/* Flash Messages / Toast */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 9999;
}

.toast {
    padding: 14px 20px;
    border-radius: 8px;
    margin-bottom: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    animation: slideIn 0.3s ease;
    display: flex;
    align-items: center;
    gap: 10px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.toast-success { background: var(--success); color: #fff; }
.toast-danger { background: var(--danger); color: #fff; }
.toast-warning { background: var(--warning); color: #333; }
.toast-info { background: var(--info); color: #fff; }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* Gráficos */
.chart-container {
    position: relative;
    height: 300px;
    width: 100%;
}

/* Grid responsivo */
.grid-2 {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 20px;
}

/* Mobile */
@media (max-width: 768px) {
    .sidebar {
        width: 0;
    }
    .sidebar.expanded {
        width: 240px;
    }
    .main-content {
        margin-left: 0;
        padding: 16px;
    }
    .sidebar.expanded ~ .main-content {
        margin-left: 0;
    }
    .stats-grid {
        grid-template-columns: 1fr;
    }
    .grid-2 {
        grid-template-columns: 1fr;
    }
    .mobile-toggle {
        display: block;
        position: fixed;
        top: 12px;
        left: 12px;
        z-index: 1001;
        background: var(--primary);
        color: #fff;
        border: none;
        border-radius: 8px;
        padding: 10px;
        cursor: pointer;
    }
    /* Cards para tabelas no mobile */
    .table-mobile-cards table thead {
        display: none;
    }
    .table-mobile-cards table tr {
        display: block;
        margin-bottom: 12px;
        border: 1px solid var(--border-light);
        border-radius: 8px;
        padding: 12px;
    }
    .table-mobile-cards table td {
        display: flex;
        justify-content: space-between;
        padding: 6px 0;
        border: none;
    }
    .table-mobile-cards table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary);
    }
}

@media (min-width: 769px) {
    .mobile-toggle {
        display: none;
    }
}

/* Scrollbar custom */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    background: var(--bg-main);
}
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--text-light);
}

/* ============================================================
   SEARCHABLE SELECT COMPONENT
   ============================================================ */
.ss-wrapper {
    position: relative;
    width: 100%;
    font-size: 0.85rem;
}

.ss-display {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-card);
    cursor: pointer;
    min-height: 38px;
    transition: var(--transition);
}

.ss-display:hover {
    border-color: var(--primary-light);
}

.ss-wrapper.ss-open .ss-display {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(30, 58, 95, 0.1);
}

.ss-placeholder {
    color: var(--text-light);
}

.ss-selected {
    color: var(--text-primary);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: calc(100% - 20px);
}

.ss-arrow {
    flex-shrink: 0;
    color: var(--text-secondary);
    transition: transform 0.2s;
}

.ss-wrapper.ss-open .ss-arrow {
    transform: rotate(180deg);
}

.ss-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    z-index: 1000;
    max-height: 320px;
    display: flex;
    flex-direction: column;
}

.ss-search {
    width: 100%;
    display: block;
    padding: 10px 12px;
    border: none;
    border-bottom: 1px solid var(--border-light);
    font-size: 0.85rem;
    outline: none;
    background: #f8fafb;
    border-radius: 8px 8px 0 0;
}

.ss-search::placeholder {
    color: var(--text-light);
}

.ss-options {
    overflow-y: auto;
    max-height: 260px;
    padding: 4px;
}

.ss-option {
    padding: 8px 12px;
    cursor: pointer;
    border-radius: 4px;
    font-size: 0.8rem;
    color: var(--text-primary);
    transition: background 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ss-option:hover {
    background: #e3f2fd;
    color: var(--primary);
}

.ss-option-disabled {
    cursor: not-allowed;
    color: var(--text-light);
    opacity: 0.6;
    font-style: italic;
}

.ss-option-disabled:hover {
    background: none;
    color: var(--text-light);
}

.ss-no-result {
    padding: 12px;
    text-align: center;
    color: var(--text-light);
    font-size: 0.8rem;
}

.ss-more {
    padding: 8px 12px;
    text-align: center;
    color: var(--text-light);
    font-size: 0.7rem;
    font-style: italic;
    border-top: 1px solid var(--border-light);
}

/* ============================================================
   Abas genéricas (comp-tabs) — usadas em componentes, MP e PA
   ============================================================ */
.comp-tabs {
    display: flex;
    gap: 4px;
    border-bottom: 2px solid #e0e0e0;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.comp-tab {
    padding: 8px 16px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-light);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: color .15s, border-color .15s;
}
.comp-tab:hover { color: var(--primary); }
.comp-tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.comp-tab-pane { display: none; }
.comp-tab-pane.active { display: block; }

/* ============================================================
   Sidebar: categorias recolhíveis
   ============================================================ */
.sidebar.expanded .nav-category.nav-collapsible {
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: color .15s;
}
.sidebar.expanded .nav-category.nav-collapsible:hover { color: rgba(255,255,255,0.8); }
.sidebar.expanded .nav-category.nav-collapsible::after {
    content: '▾';
    font-size: 0.7rem;
    margin-right: 8px;
    transition: transform .2s ease;
}
.sidebar.expanded .nav-category.nav-collapsible.collapsed::after {
    transform: rotate(-90deg);
}

/* ============================================================
   Modal de confirmação (substitui confirm() nativo)
   ============================================================ */
.confirm-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 10080;
    align-items: center;
    justify-content: center;
    padding: 16px;
}
.confirm-overlay.show { display: flex; }
.confirm-card {
    background: var(--bg-card, #fff);
    border-radius: 12px;
    padding: 28px;
    max-width: 440px;
    width: 100%;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    animation: confirmPop .15s ease-out;
}
@keyframes confirmPop {
    from { transform: scale(0.95); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* MP sem estoque para produção: linha piscando em vermelho */
@keyframes mpPiscar {
    0%, 100% { background: #ffebee; }
    50% { background: #ff8a80; }
}
tr.mp-bloqueada td {
    animation: mpPiscar 1.2s ease-in-out infinite;
    color: #b71c1c;
}
tr.mp-bloqueada td strong { color: #b71c1c; }

/* ============================================================
   Correção: scroll horizontal da página (ex: /entrada/nf/<id>)
   Tabelas largas devem rolar dentro do .table-container,
   nunca alargar a página inteira.
   ============================================================ */
body { overflow-x: hidden; }
.main-content {
    min-width: 0;      /* impede que conteúdo largo expanda o item flex */
    max-width: 100%;
}
.card { min-width: 0; }

/* Cabeçalhos ordenáveis */
th.th-sortable {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}
th.th-sortable:hover { background: rgba(30, 58, 95, 0.06); }
th.th-sortable::after {
    content: ' ↕';
    font-size: 0.72em;
    opacity: 0.35;
}
th.th-sortable.sort-asc::after { content: ' ▲'; opacity: 0.9; }
th.th-sortable.sort-desc::after { content: ' ▼'; opacity: 0.9; }

/* ============================================================
   Rodapé do usuário no sidebar
   ============================================================ */
.sidebar { display: flex; flex-direction: column; }
.sidebar-nav { flex: 1; }
.sidebar-user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 12px;
    border-top: 1px solid rgba(255,255,255,0.12);
}
.sidebar-user-info { display: none; min-width: 0; }
.sidebar.expanded .sidebar-user-info { display: block; }
.sidebar-user-info strong {
    display: block;
    color: #fff;
    font-size: 0.8rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sidebar-user-info span {
    display: block;
    color: rgba(255,255,255,0.5);
    font-size: 0.68rem;
}
.sidebar-user-info a {
    color: var(--accent-light, #80cbc4);
    font-size: 0.68rem;
    text-decoration: underline;
}
.sidebar-logout {
    color: rgba(255,255,255,0.7);
    padding: 8px;
    border-radius: 8px;
    flex-shrink: 0;
}
.sidebar-logout:hover { background: rgba(255,255,255,0.1); color: #fff; }
