*, *::before, *::after { box-sizing: border-box; }

:root {
    --bg: #f3f5f9;
    --surface: #ffffff;
    --surface-2: #f8f9fc;
    --sidebar-bg: #12162a;
    --sidebar-bg-2: #171c34;
    --sidebar-text: #aab1cc;
    --sidebar-text-dim: #6f76958f;
    --sidebar-border: #262c4a;
    --border: #e2e5ec;
    --text: #171b26;
    --text-muted: #667085;
    --primary: #2451e0;
    --primary-2: #6c5ce7;
    --primary-hover: #1c3fb8;
    --primary-soft: #eaefff;
    --ok-bg: #e4f7ec;
    --ok-text: #0f7a3d;
    --bad-bg: #fdecec;
    --bad-text: #b3261e;
    --warn-bg: #fff4e0;
    --warn-text: #935600;
    --neutral-bg: #eef0f4;
    --neutral-text: #4a5165;
    --radius-lg: 16px;
    --radius-md: 10px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
    --shadow-md: 0 4px 10px rgba(16, 24, 40, .06), 0 2px 4px rgba(16, 24, 40, .05);
    --shadow-lg: 0 16px 32px rgba(16, 24, 40, .12), 0 4px 10px rgba(16, 24, 40, .06);
    --sidebar-w: 260px;
    --ease: cubic-bezier(.2, .8, .2, 1);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1420;
        --surface: #161d2c;
        --surface-2: #1c2436;
        --sidebar-bg: #0b0e1a;
        --sidebar-bg-2: #10142400;
        --border: #2a3348;
        --text: #e7eaf3;
        --text-muted: #93a0bb;
        --primary: #5b82ff;
        --primary-2: #9b7bff;
        --primary-hover: #7a99ff;
        --primary-soft: #1c2748;
        --ok-bg: #123023;
        --ok-text: #4ade8a;
        --bad-bg: #341c1c;
        --bad-text: #ff8783;
        --warn-bg: #362a12;
        --warn-text: #f0b746;
        --neutral-bg: #202a3d;
        --neutral-text: #a7b1c6;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3);
        --shadow-md: 0 8px 20px rgba(0, 0, 0, .35);
        --shadow-lg: 0 24px 48px rgba(0, 0, 0, .45), 0 6px 16px rgba(0, 0, 0, .3);
    }
}

html { color-scheme: light dark; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    margin: 0;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
}

a { color: inherit; }

@keyframes fade-up {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ============ App shell / sidebar ============ */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: linear-gradient(180deg, var(--sidebar-bg), var(--sidebar-bg-2));
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    padding: 22px 16px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    border-right: 1px solid var(--sidebar-border);
}

.sidebar .brand {
    display: flex;
    align-items: center;
    gap: 11px;
    text-decoration: none;
    color: #fff;
    padding: 4px 10px 22px;
}

.sidebar .brand-mark {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 4px 14px rgba(108, 92, 231, .35);
}

.sidebar .brand-mark svg { width: 21px; height: 21px; }

.sidebar .brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.sidebar .brand-title { font-weight: 700; font-size: 1rem; color: #fff; }
.sidebar .brand-subtitle { font-size: .68rem; color: var(--sidebar-text); opacity: .7; }

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 3px;
    flex: 1 1 auto;
}

.nav-section-label {
    font-size: .68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--sidebar-text);
    opacity: .55;
    padding: 16px 12px 6px;
}
.nav-section-label:first-child { padding-top: 4px; }

.nav-item {
    position: relative;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--sidebar-text);
    font-size: .89rem;
    font-weight: 600;
    transition: background .18s var(--ease), color .18s var(--ease), transform .18s var(--ease), padding-left .18s var(--ease);
}

.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; opacity: .85; transition: opacity .18s; }

.nav-item::before {
    content: "";
    position: absolute;
    left: -16px;
    top: 50%;
    transform: translateY(-50%);
    height: 0;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: linear-gradient(180deg, var(--primary), var(--primary-2));
    transition: height .18s var(--ease);
}

.nav-item:hover {
    background: rgba(255, 255, 255, .06);
    color: #fff;
    padding-left: 16px;
}

.nav-item.active {
    background: linear-gradient(90deg, rgba(108, 92, 231, .22), rgba(108, 92, 231, .04));
    color: #fff;
}
.nav-item.active svg { opacity: 1; }
.nav-item.active::before { height: 20px; }

.sidebar-footer {
    border-top: 1px solid var(--sidebar-border);
    padding-top: 14px;
    margin-top: 14px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.user-chip {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px 14px;
}

.user-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary-soft);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: .85rem;
    flex-shrink: 0;
}

.user-info { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.user-email {
    font-size: .8rem;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.user-papel { font-size: .7rem; color: var(--sidebar-text); opacity: .65; }

.nav-item-logout:hover { background: var(--bad-bg); color: var(--bad-text); }
.nav-item-logout:hover svg { opacity: 1; }

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(8, 10, 20, .55);
    z-index: 30;
    opacity: 0;
    transition: opacity .2s var(--ease);
}

.app-main {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.topbar {
    display: none;
    align-items: center;
    gap: 14px;
    padding: 14px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.topbar-title { font-weight: 700; font-size: .95rem; }

.nav-toggle {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    cursor: pointer;
    flex-shrink: 0;
    transition: border-color .15s, transform .15s;
}
.nav-toggle:hover { border-color: var(--primary); }
.nav-toggle:active { transform: scale(.93); }
.nav-toggle span { display: block; height: 2px; width: 18px; margin: 0 auto; background: var(--text); border-radius: 2px; }

main {
    max-width: 1080px;
    width: 100%;
    margin: 0 auto;
    padding: 28px 28px 56px;
    flex: 1 1 auto;
    animation: fade-up .35s var(--ease);
}

/* ============ Buttons (with motion) ============ */
button, .btn {
    font-family: inherit;
    cursor: pointer;
}

form.busca button,
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 11px 24px;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    background-size: 140% 140%;
    background-position: 0% 50%;
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: .93rem;
    font-weight: 700;
    text-decoration: none;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(36, 81, 224, .25);
    transition: transform .16s var(--ease), box-shadow .16s var(--ease), background-position .35s var(--ease);
}

form.busca button:hover,
.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 18px rgba(36, 81, 224, .32);
    background-position: 100% 50%;
}

form.busca button:active,
.btn:active {
    transform: translateY(0) scale(.97);
    box-shadow: 0 2px 6px rgba(36, 81, 224, .3);
}

form.busca button:focus-visible,
.btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--primary-soft), 0 8px 18px rgba(36, 81, 224, .32);
}

.btn-secundario {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    box-shadow: none;
}
.btn-secundario:hover {
    background: var(--surface-2);
    border-color: var(--primary);
    color: var(--primary);
    box-shadow: var(--shadow-md);
}

/* ============ Search form ============ */
form.busca {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 22px;
    margin-bottom: 24px;
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    align-items: flex-end;
    box-shadow: var(--shadow);
}

form.busca .campo {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1 1 200px;
}

form.busca .campo-checkbox {
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 2px;
}

form.busca .campo-checkbox label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-weight: 500;
}

form.busca .campo-checkbox input:disabled + * ,
form.busca .campo-checkbox label:has(input:disabled) {
    opacity: 0.5;
    cursor: not-allowed;
}

form.busca .dica-checkbox {
    font-size: 0.8rem;
    opacity: 0.65;
    margin-left: 26px;
}

form.busca label {
    font-size: .78rem;
    font-weight: 600;
    color: var(--text-muted);
}

form.busca input, form.busca select, form.busca textarea {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: .95rem;
    background: var(--surface-2);
    color: var(--text);
    font-family: inherit;
    transition: border-color .15s, box-shadow .15s;
}

form.busca textarea { resize: vertical; }

form.busca input:focus, form.busca select:focus, form.busca textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

form.form-contato .campo-observacao { flex: 1 1 100%; }

/* ============ Alerts ============ */
.alerta {
    background: var(--bad-bg);
    border: 1px solid transparent;
    color: var(--bad-text);
    padding: 14px 18px;
    border-radius: var(--radius-md);
    margin-bottom: 20px;
    font-size: .92rem;
}

/* ============ Cards ============ */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
    animation: fade-up .35s var(--ease);
    transition: box-shadow .2s var(--ease);
}

.card h2 {
    margin-top: 0;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.nome-fantasia {
    margin: -8px 0 20px;
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-muted);
}

.badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 999px;
    font-size: .74rem;
    font-weight: 700;
    letter-spacing: .02em;
    text-transform: uppercase;
}

.badge.ativa { background: var(--ok-bg); color: var(--ok-text); }
.badge.baixada { background: var(--bad-bg); color: var(--bad-text); }
.badge.suspensa, .badge.inapta { background: var(--warn-bg); color: var(--warn-text); }
.badge.outra { background: var(--neutral-bg); color: var(--neutral-text); }

dl.detalhes {
    display: grid;
    grid-template-columns: 200px 1fr;
    row-gap: 14px;
    column-gap: 16px;
    margin: 0;
}

dl.detalhes dt {
    color: var(--text-muted);
    font-size: .82rem;
    font-weight: 600;
    padding-top: 2px;
}

dl.detalhes dd {
    margin: 0;
    font-size: .96rem;
}

.lista-cnaes {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

/* ============ Results table -> responsive cards on mobile ============ */
.tabela-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow-x: auto;
    box-shadow: var(--shadow);
}

table.resultados {
    width: 100%;
    border-collapse: collapse;
}

table.resultados th, table.resultados td {
    padding: 12px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: .88rem;
}

table.resultados th {
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: .74rem;
    text-transform: uppercase;
    letter-spacing: .03em;
    font-weight: 700;
}

table.resultados tbody tr { transition: background .12s; }
table.resultados tbody tr:hover { background: var(--surface-2); }
table.resultados tr:last-child td { border-bottom: none; }
table.resultados a { color: var(--primary); text-decoration: none; font-weight: 600; }
table.resultados a:hover { text-decoration: underline; }

.paginacao {
    display: flex;
    gap: 6px;
    margin-top: 18px;
    align-items: center;
    flex-wrap: wrap;
}

.paginacao a, .paginacao span.atual {
    padding: 7px 13px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--text);
    font-size: .85rem;
    font-weight: 600;
    background: var(--surface);
    min-width: 20px;
    text-align: center;
    transition: transform .15s var(--ease), border-color .15s, box-shadow .15s, background .15s;
}

.paginacao a:hover {
    border-color: var(--primary);
    color: var(--primary);
    transform: translateY(-1px);
    box-shadow: var(--shadow);
}

.paginacao a.paginacao-nav { font-weight: 600; color: var(--text-muted); }
.paginacao a.paginacao-salto { color: var(--text-muted); font-weight: 700; }

.paginacao span.atual {
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: #fff;
    border-color: transparent;
    font-weight: 700;
}

.paginacao-info {
    border: none !important;
    background: none !important;
    color: var(--text-muted);
    font-size: .8rem !important;
    font-weight: 500 !important;
    padding-left: 6px !important;
    margin-left: auto;
}

@media (max-width: 880px) {
    .paginacao-info { margin-left: 0; width: 100%; order: 10; padding-left: 0 !important; }
}

.info-total {
    color: var(--text-muted);
    font-size: .85rem;
    margin: -6px 2px 16px;
}

/* ============ Footer ============ */
.site-footer {
    padding: 20px 4px;
    color: var(--text-muted);
    font-size: .78rem;
    text-align: center;
}

/* ============ Responsive ============ */
@media (max-width: 880px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        z-index: 40;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
        transition: transform .25s var(--ease);
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-backdrop.open { display: block; opacity: 1; }

    .topbar { display: flex; }

    main { padding: 20px 16px 44px; }

    form.busca { flex-direction: column; align-items: stretch; }
    form.busca .campo { flex: 1 1 auto; }
    form.busca button { width: 100%; }

    dl.detalhes { grid-template-columns: 1fr; row-gap: 4px; }
    dl.detalhes dd { margin-bottom: 8px; }

    table.resultados thead { display: none; }
    table.resultados, table.resultados tbody, table.resultados tr, table.resultados td { display: block; width: 100%; }
    table.resultados tr {
        border-bottom: 1px solid var(--border);
        padding: 12px 14px;
    }
    table.resultados tr:last-child { border-bottom: none; }
    table.resultados td {
        border-bottom: none;
        padding: 4px 0;
        display: flex;
        justify-content: space-between;
        gap: 12px;
        text-align: right;
    }
    table.resultados td::before {
        content: attr(data-label);
        color: var(--text-muted);
        font-size: .74rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: .03em;
        text-align: left;
    }
}
