/* --- PAINEL ADMIN: ESTILO GERAL E RESPONSIVO --- */
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap');

:root {
    --bg-primary: #f8fafc;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f1f5f9;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --accent-color: #2563eb;
    --accent-glow: rgba(37, 99, 235, 0.12);
    --border-color: #e2e8f0;
    --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.03);
    --font-family: 'Outfit', sans-serif;

    --table-header-bg: #f1f5f9;
    --table-hover-bg: #f8fafc;
    --table-row-even: #f8fafc;
}

html.dark {
    --bg-primary: #0b0f19;
    --bg-secondary: #111827;
    --bg-tertiary: #1f2937;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent-color: #06b6d4;
    --accent-glow: rgba(6, 182, 212, 0.3);
    --border-color: rgba(255, 255, 255, 0.08);
    --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);

    --table-header-bg: #1f2937;
    --table-hover-bg: rgba(255, 255, 255, 0.02);
    --table-row-even: rgba(255, 255, 255, 0.01);
}

/* Transições de tema globais suaves */
body,
.container,
table,
td,
th,
input,
select,
textarea,
.metric-card,
.btn,
.message {
    transition: background-color 0.4s cubic-bezier(0.4, 0, 0.2, 1),
        color 0.4s cubic-bezier(0.4, 0, 0.2, 1),
        border-color 0.4s cubic-bezier(0.4, 0, 0.2, 1),
        box-shadow 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Estilo do Botão Alternador de Tema */
.theme-toggle-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 46px;
    height: 46px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);
    box-shadow: var(--card-shadow);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    z-index: 10001;
    backdrop-filter: blur(8px);
    outline: none;
}

.theme-toggle-btn:hover {
    transform: scale(1.08);
    border-color: var(--accent-color);
    box-shadow: 0 0 15px var(--accent-glow);
}

/* Configurações da View Transitions API */
::view-transition-old(root),
::view-transition-new(root) {
    animation: none;
    mix-blend-mode: normal;
}

::view-transition-old(root) {
    z-index: 1;
}

::view-transition-new(root) {
    z-index: 9999;
}

.dark::view-transition-old(root) {
    z-index: 9999;
}

.dark::view-transition-new(root) {
    z-index: 1;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    margin: 0;
    padding: 20px;
    box-sizing: border-box;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    background-color: var(--bg-secondary);
    padding: 30px;
    border-radius: 20px;
    box-shadow: var(--card-shadow);
    border: 1px solid var(--border-color);
    width: 100%;
    box-sizing: border-box;
}

h1 {
    color: var(--text-primary);
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 12px;
    margin-bottom: 25px;
    font-weight: 700;
}

h2 {
    color: var(--text-primary);
    font-weight: 600;
}

h3 {
    margin: 0;
    color: var(--text-primary);
    font-weight: 600;
}

/* --- FORMULÁRIOS --- */
.form-group {
    margin-bottom: 20px;
}

label {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-primary);
}

input[type="text"],
input[type="email"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-sizing: border-box;
    font-size: 15px;
    font-family: var(--font-family);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    outline: none;
}

textarea {
    resize: vertical;
    min-height: 100px;
}

.help-text {
    font-size: 13px;
    color: var(--text-secondary);
    margin-top: 5px;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus,
select:focus,
textarea:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

/* --- BOTÕES --- */
.button-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 25px;
}

.btn {
    display: inline-block;
    padding: 10px 18px;
    background-color: #022c5e;
    color: #fff;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 600;
    border: none;
    cursor: pointer;
    font-size: 15px;
    transition: transform 0.2s, box-shadow 0.2s;
    text-align: center;
    box-sizing: border-box;
    font-family: var(--font-family);
    box-shadow: 0 1px 0px var(--accent-glow);
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 15px var(--accent-glow);
}

.btn-success {
    background-color: #10b981;
    --accent-glow: rgba(16, 185, 129, 0.2);
}

.btn-secondary {
    background-color: #64748b;
    --accent-glow: rgba(100, 116, 139, 0.2);
}

/* --- MENSAGENS DE FEEDBACK --- */
.message {
    padding: 15px;
    margin-bottom: 25px;
    border-radius: 10px;
    font-weight: 500;
}

.success {
    background-color: rgba(16, 185, 129, 0.1);
    color: #10b981;
    border: 1px solid rgba(16, 185, 129, 0.2);
}

.error {
    background-color: rgba(239, 68, 68, 0.1);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.2);
}

/* --- BARRA DE BUSCA E FILTRO --- */
.admin-search-container {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    margin-bottom: 25px;
    width: 100%;
}

.admin-search-container input[type="text"] {
    flex-grow: 1;
    flex-basis: 300px;
    min-width: 200px;
}

.admin-search-container select {
    flex-grow: 1;
    flex-basis: 200px;
}

/* --- TABELAS --- */
table {
    width: 100%;
    border-collapse: collapse;
}

thead {
    background-color: var(--table-header-bg);
}

th,
td {
    padding: 15px;
    border-bottom: 1px solid var(--border-color);
    text-align: left;
    vertical-align: middle;
    color: var(--text-primary);
}

thead th {
    color: var(--text-secondary);
    font-size: 13px;
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.5px;
}

tbody tr:nth-child(even) {
    background-color: var(--table-row-even);
}

tbody tr:hover {
    background-color: var(--table-hover-bg);
}

.status-active {
    color: #10b981;
    font-weight: bold;
}

.status-inactive {
    color: #ef4444;
    font-weight: bold;
}

.actions a {
    margin-right: 12px;
    text-decoration: none;
    font-weight: 600;
}

.actions a.edit {
    color: var(--accent-color);
}

.actions a.delete {
    color: #ef4444;
}

.actions a:hover {
    text-decoration: underline;
}

/* --- LOGIN CENTRALIZADO --- */
.login-container {
    max-width: 400px;
    width: 100%;
    padding: 40px;
    margin: 50px auto;
}

/* --- SEÇÕES NO GERENCIADOR --- */
.secao-container {
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    background-color: var(--bg-secondary);
}

.secao-container:nth-child(even) {
    background-color: var(--bg-tertiary);
}

.secao-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 15px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}

.form-inline {
    display: flex;
    gap: 10px;
}

.form-inline input[type="text"] {
    flex-grow: 1;
}

.form-inline button {
    flex-shrink: 0;
}

.edit-mode {
    width: 100%;
    display: flex;
    gap: 10px;
}

.edit-mode input[type="text"] {
    flex-grow: 1;
}

.edit-mode button {
    flex-shrink: 0;
}

/* --- PAINEL DE MÉTRICAS --- */
.metrics-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.metric-card {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    box-shadow: var(--card-shadow);
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.metric-card:nth-child(3) {
    background: rgba(16, 185, 129, 0.05);
    /* Leve verde */
    border-color: rgba(16, 185, 129, 0.2);
}

.metric-card:nth-child(4) {
    background: rgba(59, 130, 246, 0.05);
    /* Leve azul */
    border-color: rgba(59, 130, 246, 0.2);
}

.metric-value {
    font-size: 2.2em;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.1;
}

.metric-label {
    font-size: 0.85em;
    color: var(--text-secondary);
    margin-top: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 700;
}

.progress-bar-container {
    background-color: var(--border-color);
    border-radius: 4px;
    height: 8px;
    width: 100%;
    margin-top: 10px;
    overflow: hidden;
}

.progress-bar {
    background-color: #10b981;
    height: 100%;
    border-radius: 4px;
    transition: width 0.5s ease-in-out;
}

/* --- RESPONSIVIDADE --- */
@media (max-width: 768px) {
    body {
        padding: 10px;
    }

    .container {
        padding: 15px;
    }

    h1 {
        font-size: 1.8em;
    }

    .button-bar .btn {
        width: 100%;
        margin-left: 0 !important;
    }

    .button-bar .btn-secondary {
        float: none !important;
        width: 100%;
    }

    .admin-search-container input[type="text"],
    .admin-search-container select {
        width: 100%;
        flex-basis: 100%;
    }

    table thead {
        display: none;
    }

    table,
    tbody,
    tr,
    td {
        display: block;
        width: 100%;
    }

    table tr {
        margin-bottom: 15px;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        overflow: hidden;
    }

    table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 50%;
        position: relative;
        text-align: right;
        border-bottom: 1px solid var(--border-color);
        min-height: 40px;
    }

    table td:last-child {
        border-bottom: none;
    }

    table td:before {
        content: attr(data-label);
        position: absolute;
        left: 15px;
        padding-right: 10px;
        font-weight: bold;
        text-align: left;
        color: var(--text-secondary);
    }

    table td.actions {
        padding-left: 15px;
        text-align: left;
        justify-content: flex-start;
        gap: 15px;
    }

    table td.actions:before {
        display: none;
    }

    table td.edit-mode {
        padding-left: 15px;
        text-align: left;
        display: block;
    }

    table td.edit-mode:before {
        display: none;
    }

    table td.edit-mode .form-inline {
        flex-direction: column;
        width: 93%;
    }

    table td.edit-mode .form-inline input,
    table td.edit-mode .form-inline button {
        width: 100%;
    }

    .secao-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .secao-header div {
        margin-left: 0 auto;
    }

    .form-inline,
    .edit-mode {
        flex-direction: column;
    }

    .form-inline input[type="text"],
    .form-inline button,
    .edit-mode input[type="text"],
    .edit-mode button {
        width: 100%;
    }
}

/* --- DUAS COLUNAS PARA MÉTRICAS --- */
.metrics-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 25px;
}

.metrics-column {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 20px;
    box-shadow: var(--card-shadow);
}

.metrics-column-title {
    font-size: 1.15em;
    font-weight: 700;
    margin-top: 0;
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}

.metrics-column .metrics-container {
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 12px;
    margin-bottom: 0;
}

.metrics-column .metric-card {
    background: var(--bg-tertiary);
    padding: 15px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
}

.metrics-column.casas .metric-card:nth-child(3) {
    background: rgba(16, 185, 129, 0.05);
    border-color: rgba(16, 185, 129, 0.15);
}

.metrics-column.predios .metric-card:nth-child(3) {
    background: rgba(59, 130, 246, 0.05);
    border-color: rgba(59, 130, 246, 0.15);
}

.last-activity-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 20px 25px;
    box-shadow: var(--card-shadow);
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 25px;
}

.last-activity-icon {
    font-size: 1.8em;
    color: var(--accent-color);
}

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

/* Grade de métricas com coluna única (quando prédios estão desabilitados) */
.metrics-grid.single-column {
    grid-template-columns: 1fr;
}

/* --- HEADER E NAVBAR REFORMULADOS --- */
.admin-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-secondary);
    border-bottom: 2px solid var(--border-color);
    padding: 15px 0;
    margin-bottom: 30px;
    width: 100%;
}

.admin-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.admin-logo {
    max-height: 44px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    padding: 3px;
    background: transparent;
}

.admin-logo-fallback {
    width: 40px;
    height: 40px;
    background: var(--accent-glow);
    color: var(--accent-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
}

.admin-cong-name {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
}

.admin-nav {
    display: flex;
    align-items: center;
    gap: 15px;
}

.nav-link {
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    border-radius: 8px;
    transition: all 0.25s ease;
}

.nav-link:hover {
    color: var(--accent-color);
    background-color: var(--bg-tertiary);
}

.nav-logout {
    color: #ef4444;
}

.nav-logout:hover {
    color: #ffffff;
    background-color: #ef4444;
}

/* Dropdown de Navegação do Admin */
.nav-dropdown {
    position: relative;
    display: inline-block;
}

button.nav-dropdown-toggle {
    background: none;
    border: none;
    cursor: pointer;
    font-family: inherit;
    width: auto;
}

.nav-dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 5px;
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    min-width: 220px;
    padding: 6px;
    visibility: hidden;
    opacity: 0;
    transform: translateY(-8px);
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

@media (min-width: 901px) {
    .nav-dropdown:hover .nav-dropdown-menu,
    .nav-dropdown.show .nav-dropdown-menu {
        visibility: visible;
        opacity: 1;
        transform: translateY(0);
        pointer-events: auto;
    }
}

.dropdown-item {
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-radius: 8px;
    transition: all 0.2s ease;
}

.dropdown-item:hover {
    color: var(--accent-color);
    background-color: var(--bg-tertiary);
}

.dropdown-item i {
    font-size: 0.95rem;
    width: 16px;
    text-align: center;
}

.dropdown-icon {
    font-size: 0.75rem;
    transition: transform 0.2s ease;
    margin-left: 2px;
}

.nav-dropdown:hover .dropdown-icon,
.nav-dropdown.show .dropdown-icon {
    transform: rotate(180deg);
}

.mobile-menu-toggle {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 8px;
    outline: none;
}

/* --- ESTILO DOS LINKS DOS TERRITÓRIOS --- */
.territory-name-link {
    color: var(--text-primary);
    text-decoration: none;
    transition: color 0.2s;
}

.territory-name-link:hover {
    color: var(--accent-color);
    text-decoration: underline;
}

.btn-manage-territory {
    color: var(--text-secondary);
    transition: color 0.25s, transform 0.2s;
    font-size: 0.9em;
    display: inline-flex;
    align-items: center;
    padding: 4px;
}

.btn-manage-territory:hover {
    color: var(--accent-color);
    transform: scale(1.1);
}

/* --- RESPONSIVIDADE DO MENU --- */
@media (max-width: 900px) {
    .mobile-menu-toggle {
        display: block;
    }

    .admin-nav {
        display: none;
        flex-direction: column;
        width: 100%;
        position: absolute;
        top: 80px;
        left: 0;
        background: var(--bg-secondary);
        border: 1px solid var(--border-color);
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.05);
        border-radius: 12px;
        padding: 15px;
        z-index: 1000;
        box-sizing: border-box;
        gap: 8px;
    }

    .admin-nav.active {
        display: flex;
    }

    .nav-link {
        width: 100%;
        padding: 12px;
    }

    .admin-header {
        position: relative;
    }

    .nav-dropdown {
        width: 100%;
    }
    
    .nav-dropdown-toggle {
        width: 100%;
        justify-content: space-between;
        box-sizing: border-box;
    }
    
    .nav-dropdown-menu {
        position: static;
        box-shadow: none;
        border: none;
        background-color: transparent;
        padding: 4px 0 4px 16px;
        min-width: unset;
        visibility: visible;
        opacity: 1;
        pointer-events: auto;
        transform: none;
        display: none;
        transition: none;
        flex-direction: column;
        gap: 2px;
    }
    
    .nav-dropdown.show .nav-dropdown-menu {
        display: flex;
    }
    
    .dropdown-item {
        width: 100%;
        padding: 12px;
        box-sizing: border-box;
    }
}

/* --- BARRA FLUTUANTE DE SALVAR (CONFIGURAÇÕES) --- */
.floating-save-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    padding: 15px 30px;
    box-sizing: border-box;
    box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.05);
    z-index: 999;
    display: flex;
    justify-content: center;
}

.floating-save-bar .btn-success {
    max-width: 600px;
    width: 100%;
    margin: 0;
}

.container-with-padding-bottom {
    padding-bottom: 90px !important;
}

/* --- ACCORDION TABLE ROW CUSTOM STYLES (OVERRIDE MOBILE TABLE RESPONSIVENESS) --- */
.territory-row {
    transition: background-color 0.2s ease;
}

.territory-row:hover {
    background-color: var(--table-hover-bg) !important;
}

.territory-row td {
    border-bottom: 1px solid var(--border-color) !important;
}

.details-info-grid {
    display: flex;
    gap: 30px;
    flex-wrap: wrap;
    margin-bottom: 15px;
}

@media (max-width: 768px) {

    table tbody tr.territory-row,
    table tbody tr#no-results-row {
        display: block;
        border: none !important;
        margin-bottom: 0 !important;
        border-radius: 0 !important;
        overflow: visible !important;
    }

    table tbody tr.territory-row td[colspan="5"],
    table tbody tr#no-results-row td[colspan="5"] {
        display: block !important;
        /* Use block to prevent flex container behavior */
        padding: 0 !important;
        position: static !important;
        text-align: left !important;
        border-bottom: 1px solid var(--border-color) !important;
        min-height: auto !important;
        width: 100% !important;
    }

    table tbody tr#no-results-row td[colspan="5"] {
        padding: 15px !important;
        text-align: center !important;
    }

    table tbody tr.territory-row td[colspan="5"]::before,
    table tbody tr#no-results-row td[colspan="5"]::before {
        display: none !important;
    }

    .details-info-grid {
        flex-direction: column;
        gap: 15px;
    }
}

@media (max-width: 500px) {
    .type-badge .badge-text {
        display: none !important;
    }

    .type-badge {
        padding: 4px 6px !important;
    }
}

/* Custom responsive rules for share card */
@media (max-width: 768px) {
    .share-card {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 15px !important;
    }
    
    .share-card .share-info {
        width: 100%;
        min-width: 0 !important;
        flex: none !important;
    }
    
    .share-card .share-actions {
        width: 100%;
        justify-content: flex-start;
        flex-wrap: wrap;
    }
}

/* --- CONFIGURAÇÕES DE CABEÇALHO (CARDS SELECIONÁVEIS) --- */
.header-options-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
    width: 100%;
}

.cabecalho-card {
    border: 2px solid var(--border-color);
    border-radius: 12px;
    padding: 20px;
    background: var(--bg-secondary);
    cursor: pointer;
    position: relative;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0.6;
}

.cabecalho-card.active {
    border-color: var(--accent-color) !important;
    opacity: 1 !important;
    cursor: default;
    box-shadow: 0 4px 20px var(--accent-glow);
}

.cabecalho-card:not(.active) .form-group-inner * {
    pointer-events: none;
}

.cabecalho-card .form-group-inner {
    margin-top: 15px;
}

.cabecalho-card h3 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
}

/* Estilo personalizado de upload de arquivo */
.custom-file-upload {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.custom-file-upload label.btn {
    margin: 0;
    padding: 8px 16px;
    font-size: 0.85em;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    pointer-events: auto;
}

.custom-file-upload .file-name {
    font-size: 0.85em;
    color: var(--text-secondary);
    word-break: break-all;
}