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

:root {
    --primary: #1565C0;
    --primary-light: #E3F2FD;
    --danger: #C62828;
    --danger-light: #FFEBEE;
    --success: #2E7D32;
    --success-light: #E8F5E9;
    --warning: #E65100;
    --warning-light: #FFF3E0;
    --bg: #F4F6F9;
    --surface: #FFFFFF;
    --border: #E2E8F0;
    --text: #1E293B;
    --muted: #64748B;
    --sidebar-bg: #FFFFFF;
    --sidebar-border: #E2E8F0;
    --sidebar-text: #374151;
    --sidebar-muted: #9CA3AF;
    --sidebar-active-bg: #EFF6FF;
    --sidebar-active-text: #1565C0;
    --sidebar-hover-bg: #F8FAFC;
    --sidebar-w: 240px;
    --radius: 8px;
    --font: 'Poppins', system-ui, sans-serif;
    --shadow: 0 1px 3px rgba(0,0,0,.06);
    --shadow-md: 0 4px 12px rgba(0,0,0,.08);
}

body { font-family: var(--font); background: var(--bg); color: var(--text); font-size: 0.9375rem; line-height: 1.5; min-height: 100vh; }

/* ── App loader ─────────────────────────────────────────────────────────── */
#app-loader {
    position: fixed; inset: 0; z-index: 9999;
    background: #fff;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2rem;
    transition: opacity .25s ease;
}
#app-loader img { height: 120px; width: auto; object-fit: contain; }
#app-loader.fade-out { opacity: 0; pointer-events: none; }

.app-loader-spinner {
    width: 40px; height: 40px;
    border: 3px solid #E3F2FD;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Login ─────────────────────────────────────────────────────────────── */
#screen-login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; }

.login-card { background: var(--surface); border-radius: 12px; padding: 2.5rem; width: 100%; max-width: 400px; text-align: center; border: 1px solid var(--border); }

.login-logo { height: 250px; width: auto; object-fit: contain; margin-bottom: 1.5rem; }

.login-card h1 { font-size: 1.4rem; font-weight: 600; margin-bottom: 0.25rem; }

.login-sub { font-size: 0.875rem; color: var(--muted); margin-bottom: 2rem; }

.login-error { color: var(--danger); font-size: 0.85rem; margin-top: 0.75rem; min-height: 1.25rem; }

.input-eye { position: relative; }
.input-eye input { padding-right: 2.75rem; }
.eye-btn {
    position: absolute; right: .625rem; top: 50%; transform: translateY(-50%);
    background: none; border: none; cursor: pointer;
    color: var(--muted); padding: .25rem; display: flex; align-items: center;
    transition: color .15s;
}
.eye-btn:hover { color: var(--text); }
.eye-btn svg { width: 18px; height: 18px; }

/* ── Layout ────────────────────────────────────────────────────────────── */
#screen-admin { display: flex; min-height: 100vh; }

/* ── Sidebar ───────────────────────────────────────────────────────────── */
#sidebar {
    width: var(--sidebar-w);
    background: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    z-index: 100;
    overflow: hidden;
    box-shadow: 2px 0 8px rgba(0,0,0,.04);
}

.sidebar-brand {
    padding: 1.375rem 1.25rem 1.125rem;
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-brand-text { display: block; font-size: 1.05rem; font-weight: 700; color: var(--text); letter-spacing: .01em; }
.sidebar-brand-sub  { display: block; font-size: 0.68rem; font-weight: 600; color: var(--primary); letter-spacing: .1em; text-transform: uppercase; margin-top: .15rem; opacity: .8; }

#sidebar-nav { padding: .625rem 0; overflow-y: auto; }

/* ── Municipality context bar (superadmin) ───────────────────────────────── */
#mun-context-bar {
    padding: .75rem .875rem;
    border-top: 1px solid var(--sidebar-border);
    background: #F8FAFC;
}
#mun-context-bar .ctx-label {
    font-size: .63rem; font-weight: 700; color: var(--sidebar-muted);
    letter-spacing: .1em; text-transform: uppercase; margin-bottom: .4rem;
}
#mun-ctx-search {
    width: 100%; background: #fff; border: 1px solid var(--border);
    border-radius: 6px; color: var(--text); font-size: .78rem; font-family: var(--font);
    padding: .35rem .625rem; margin-bottom: .4rem; outline: none;
    transition: border-color .15s;
}
#mun-ctx-search::placeholder { color: var(--sidebar-muted); }
#mun-ctx-search:focus { border-color: var(--primary); }
#mun-ctx-select {
    width: 100%; background: #fff; border: 1px solid var(--border);
    border-radius: 6px; color: var(--text); font-size: .8rem; font-family: var(--font);
    padding: .375rem .5rem; outline: none; cursor: pointer;
    transition: border-color .15s;
}
#mun-ctx-select:focus { border-color: var(--primary); }
.mun-ctx-badge {
    margin-top: .4rem; font-size: .72rem; color: var(--sidebar-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
@media (max-width: 900px) { #mun-context-bar { display: none !important; } }

.nav-item {
    display: flex; align-items: center; gap: .75rem;
    padding: .625rem 1rem; margin: .125rem .625rem;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: .875rem; font-weight: 400;
    border-radius: 8px;
    transition: background .15s, color .15s;
    cursor: pointer;
}

.nav-item:hover  { background: var(--sidebar-hover-bg); color: var(--text); }
.nav-item.active { background: var(--sidebar-active-bg); color: var(--sidebar-active-text); font-weight: 600; }

.nav-icon { font-size: 1rem; width: 1.375rem; text-align: center; flex-shrink: 0; }

.sidebar-footer {
    padding: .875rem 1.125rem;
    border-top: 1px solid var(--sidebar-border);
    display: flex; align-items: center; gap: .5rem;
    background: #FAFBFC;
}

.sidebar-user { flex: 1; min-width: 0; }
.sidebar-user-name { font-size: .8125rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user-role { font-size: .7rem; color: var(--sidebar-muted); margin-top: .1rem; }

.btn-logout {
    background: none; border: none; cursor: pointer;
    color: var(--sidebar-muted); padding: .375rem;
    border-radius: 6px; display: flex; align-items: center;
    transition: color .15s, background .15s;
    flex-shrink: 0;
}
.btn-logout:hover { color: var(--danger); background: var(--danger-light); }
.btn-logout svg { width: 17px; height: 17px; }

/* ── Main content ──────────────────────────────────────────────────────── */
#main-content { margin-left: var(--sidebar-w); flex: 1; padding: 2rem; min-height: 100vh; }

.page-loading { padding: 5rem; text-align: center; color: var(--muted); }
.page-error   { padding: 3rem; text-align: center; color: var(--danger); }

/* ── Page header ───────────────────────────────────────────────────────── */
.page-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 1.5rem; flex-wrap: wrap; gap: 1rem; }
.page-header h1 { font-size: 1.5rem; font-weight: 600; }
.page-header p  { font-size: .875rem; color: var(--muted); margin-top: .125rem; }

/* ── Stats ─────────────────────────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px,1fr)); gap: 1rem; margin-bottom: 1.5rem; }

.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; }
.stat-value { font-size: 2rem; font-weight: 700; color: var(--primary); line-height: 1; }
.stat-label { font-size: .75rem; font-weight: 500; color: var(--muted); margin-top: .25rem; text-transform: uppercase; letter-spacing: .03em; }

/* ── Filters ───────────────────────────────────────────────────────────── */
.filters-bar { display: flex; gap: .625rem; margin-bottom: 1rem; flex-wrap: wrap; }

.filters-bar input[type="search"],
.filters-bar select {
    font-family: var(--font); font-size: .875rem;
    padding: .5rem .875rem;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--text);
    outline: none; height: 38px;
    transition: border-color .15s;
}

.filters-bar input[type="search"] { min-width: 220px; flex: 1; }
.filters-bar input:focus,
.filters-bar select:focus { border-color: var(--primary); }

/* ── Table card ────────────────────────────────────────────────────────── */
.table-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }

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

thead th {
    background: #FAFAFA; padding: .7rem 1rem;
    text-align: left; font-size: .75rem; font-weight: 600;
    color: var(--muted); letter-spacing: .04em; text-transform: uppercase;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

tbody td { padding: .875rem 1rem; border-bottom: 1px solid #F5F5F5; font-size: .875rem; vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #FAFAFA; }

.cell-main { font-weight: 500; }
.cell-sub  { font-size: .78rem; color: var(--muted); margin-top: .125rem; }

.empty-state { padding: 3rem; text-align: center; color: var(--muted); font-size: .9rem; }

/* ── Badges ────────────────────────────────────────────────────────────── */
.badge {
    display: inline-flex; align-items: center;
    padding: .2em .65em;
    border-radius: 4px; font-size: .73rem; font-weight: 600;
    letter-spacing: .02em; white-space: nowrap;
}

.badge-primary   { background: var(--primary-light); color: var(--primary); }
.badge-success   { background: var(--success-light); color: var(--success); }
.badge-warning   { background: var(--warning-light); color: var(--warning); }
.badge-danger    { background: var(--danger-light);  color: var(--danger); }
.badge-secondary { background: #F0F0F0; color: var(--muted); }
.badge-dark      { background: #1A1A2E; color: #fff; }
.badge-info      { background: #E1F5FE; color: #0277BD; }

/* ── Color chips ───────────────────────────────────────────────────────── */
.color-chips { display: flex; gap: 4px; }
.color-chip  { width: 20px; height: 20px; border-radius: 4px; border: 1px solid rgba(0,0,0,.1); display: inline-block; }

/* ── Actions ───────────────────────────────────────────────────────────── */
.action-btns { display: flex; gap: .375rem; flex-wrap: wrap; }

/* ── Buttons ───────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    gap: .375rem; padding: .5rem 1rem;
    border-radius: var(--radius); font-family: var(--font);
    font-size: .875rem; font-weight: 500;
    cursor: pointer; border: 1px solid transparent;
    transition: opacity .15s, background .15s, box-shadow .15s;
    text-decoration: none; white-space: nowrap;
    -webkit-user-select: none; user-select: none;
}

.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn:not(:disabled):hover { opacity: .88; }

.btn-primary   { background: var(--primary);  color: #fff; border-color: var(--primary); }
.btn-danger    { background: var(--danger);   color: #fff; border-color: var(--danger); }
.btn-success   { background: var(--success);  color: #fff; border-color: var(--success); }
.btn-warning   { background: var(--warning);  color: #fff; border-color: var(--warning); }
.btn-secondary { background: #F0F0F0; color: var(--text); border-color: var(--border); }
.btn-outline   { background: transparent; color: var(--primary); border-color: var(--primary); }
.btn-outline:not(:disabled):hover { background: var(--primary-light); opacity: 1; }

.btn-sm  { padding: .28rem .625rem; font-size: .78rem; }
.btn-block { width: 100%; }

/* ── Form fields ───────────────────────────────────────────────────────── */
.field-group { margin-bottom: 1rem; }

.field-group label {
    display: block; font-size: .75rem; font-weight: 600;
    color: var(--muted); margin-bottom: .375rem;
    text-transform: uppercase; letter-spacing: .04em;
}

.field-group input,
.field-group select,
.field-group textarea {
    width: 100%; font-family: var(--font); font-size: 1rem;
    padding: .625rem .875rem;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--text);
    outline: none; transition: border-color .15s;
    -webkit-appearance: none; appearance: none;
}

.field-group input:focus,
.field-group select:focus,
.field-group textarea:focus { border-color: var(--primary); }

.field-group textarea { resize: vertical; min-height: 80px; }

.field-group input[type="color"] { padding: .25rem; height: 42px; cursor: pointer; }

.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px,1fr)); gap: .875rem; }

/* ── Permissions grid ──────────────────────────────────────────────────── */
.perms-grid {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(200px,1fr));
    gap: .75rem; max-height: 280px; overflow-y: auto;
    padding: .875rem; border: 1px solid var(--border);
    border-radius: var(--radius); background: #FAFAFA;
}

.perms-module-title {
    font-size: .68rem; font-weight: 700; color: var(--muted);
    text-transform: uppercase; letter-spacing: .06em; margin-bottom: .4rem;
}

.perm-item { display: flex; align-items: flex-start; gap: .45rem; font-size: .85rem; cursor: pointer; padding: .15rem 0; }
.perm-item input[type="checkbox"] { width: auto; margin-top: .15rem; cursor: pointer; accent-color: var(--primary); flex-shrink: 0; }

/* ── Toggle switch ─────────────────────────────────────────────────────── */
.toggle-switch { position: relative; width: 44px; height: 24px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle-slider { position: absolute; cursor: pointer; inset: 0; background: #C0C0C0; border-radius: 24px; transition: background .2s; }
.toggle-slider::before { content: ''; position: absolute; width: 18px; height: 18px; left: 3px; bottom: 3px; background: #fff; border-radius: 50%; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.toggle-switch input:checked + .toggle-slider { background: var(--primary); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(20px); }

/* ── Module toggle list ────────────────────────────────────────────────── */
.module-toggle { display: flex; align-items: center; justify-content: space-between; padding: .875rem 1rem; border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: .5rem; background: var(--surface); }
.module-toggle-info { flex: 1; }
.module-toggle-name { font-weight: 500; font-size: .9rem; }
.module-toggle-desc { font-size: .78rem; color: var(--muted); margin-top: .1rem; }

/* ── Section card ──────────────────────────────────────────────────────── */
.section-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.5rem; margin-bottom: 1.5rem; box-shadow: var(--shadow); }
.section-card h2 { font-size: 1rem; font-weight: 600; margin-bottom: 1.25rem; padding-bottom: .75rem; border-bottom: 1px solid var(--border); }

/* ── Pagination ────────────────────────────────────────────────────────── */
.pagination { display: flex; align-items: center; justify-content: center; gap: 1rem; margin-top: 1rem; color: var(--muted); font-size: .875rem; }

/* ── Modal ─────────────────────────────────────────────────────────────── */
#modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 1rem; }

#modal-box { background: var(--surface); border-radius: 12px; width: 100%; max-width: 600px; max-height: 90vh; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.2); }

/* Wide modal — fills the space to the right of the sidebar */
#modal-overlay.modal-wide { left: var(--sidebar-w); padding: .75rem; align-items: stretch; }
#modal-overlay.modal-wide #modal-box { max-width: 100%; max-height: 100%; border-radius: 10px; flex: 1; }
#modal-overlay.modal-wide #modal-body { padding: 1.25rem 1.5rem; }
#modal-overlay.modal-wide #mun-map { height: calc(100vh - 380px); min-height: 260px; }

.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 1.25rem 1.5rem; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.modal-header h2 { font-size: 1.05rem; font-weight: 600; }

.modal-close { background: none; border: none; font-size: 1rem; cursor: pointer; color: var(--muted); padding: .25rem .375rem; border-radius: 4px; line-height: 1; }
.modal-close:hover { background: #F0F0F0; color: var(--text); }

#modal-body { padding: 1.5rem; overflow-y: auto; flex: 1; }
.modal-footer { display: flex; gap: .75rem; justify-content: flex-end; padding: 1rem 1.5rem; border-top: 1px solid var(--border); flex-shrink: 0; }

/* ── Toasts ────────────────────────────────────────────────────────────── */
#toast-container {
    position: fixed; top: 1.25rem; left: 50%; transform: translateX(-50%);
    z-index: 9000; display: flex; flex-direction: column; gap: .625rem;
    pointer-events: none; align-items: center; width: max-content; max-width: calc(100vw - 2rem);
}

.toast {
    display: flex; align-items: center; gap: .875rem;
    background: #1a1a2e; color: #fff;
    padding: .875rem 1.25rem .875rem 1rem;
    border-radius: 12px;
    font-size: .9rem; font-weight: 500;
    box-shadow: 0 8px 32px rgba(0,0,0,.22), 0 2px 8px rgba(0,0,0,.12);
    opacity: 0; transform: translateY(-12px) scale(.96);
    transition: opacity .22s ease, transform .22s ease;
    pointer-events: auto; position: relative; overflow: hidden;
    min-width: 280px; max-width: 480px;
}
.toast.show { opacity: 1; transform: none; }

.toast-icon {
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; font-size: 1.1rem;
}
.toast-msg { flex: 1; line-height: 1.4; }

.toast-progress {
    position: absolute; bottom: 0; left: 0; height: 3px;
    border-radius: 0 0 12px 12px;
    animation: toast-shrink var(--toast-dur, 3.8s) linear forwards;
}
@keyframes toast-shrink { from { width: 100%; } to { width: 0%; } }

.toast-success { background: #1B3A2A; border: 1px solid rgba(46,125,50,.4); }
.toast-success .toast-icon  { background: rgba(46,125,50,.25); color: #4CAF50; }
.toast-success .toast-progress { background: #4CAF50; }

.toast-danger  { background: #3A1B1B; border: 1px solid rgba(198,40,40,.4); }
.toast-danger  .toast-icon  { background: rgba(198,40,40,.25); color: #EF5350; }
.toast-danger  .toast-progress { background: #EF5350; }

.toast-warning { background: #3A2A10; border: 1px solid rgba(230,81,0,.4); }
.toast-warning .toast-icon  { background: rgba(230,81,0,.25); color: #FFA726; }
.toast-warning .toast-progress { background: #FFA726; }

.toast-info    { background: #1A2A3A; border: 1px solid rgba(21,101,192,.4); }
.toast-info    .toast-icon  { background: rgba(21,101,192,.25); color: #42A5F5; }
.toast-info    .toast-progress { background: #42A5F5; }

/* ── Responsive ────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
    #sidebar { width: 60px; }
    .sidebar-brand-text, .sidebar-brand-sub, .nav-item span:last-child,
    .sidebar-user, .sidebar-brand { display: none; }
    .nav-item { justify-content: center; padding: .85rem; }
    .nav-icon { width: auto; font-size: 1.2rem; }
    #main-content { margin-left: 60px; padding: 1.25rem; }
}

@media (max-width: 600px) {
    #main-content { padding: 1rem; }
    .page-header { flex-direction: column; }
    .field-row { grid-template-columns: 1fr; }
}

/* ── Form page card ────────────────────────────────────────────────────── */
.form-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 1.5rem;
    box-shadow: var(--shadow); margin-bottom: 1.25rem;
}
.form-card-title {
    font-size: .7rem; font-weight: 700; color: var(--muted);
    text-transform: uppercase; letter-spacing: .06em;
    margin: 0 0 1rem;
}

/* ── Searchable select ─────────────────────────────────────────────────── */
.ss-wrap { position: relative; }

.ss-trigger {
    width: 100%; display: flex; align-items: center; justify-content: space-between;
    gap: .5rem; padding: .625rem .875rem;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--text);
    font-family: var(--font); font-size: 1rem; cursor: pointer;
    text-align: left; transition: border-color .15s;
}
.ss-trigger:hover, .ss-trigger:focus { border-color: var(--primary); outline: none; }
.ss-trigger svg { flex-shrink: 0; color: var(--muted); transition: transform .15s; }
.ss-wrap.ss-open .ss-trigger { border-color: var(--primary); }
.ss-wrap.ss-open .ss-trigger svg { transform: rotate(180deg); }

.ss-dropdown {
    display: none; position: absolute; top: calc(100% + 4px); left: 0; right: 0;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.12);
    z-index: 1000; overflow: hidden;
}
.ss-wrap.ss-open .ss-dropdown { display: block; }

.ss-search {
    width: 100%; box-sizing: border-box;
    padding: .625rem .875rem; border: none; border-bottom: 1px solid var(--border);
    font-family: var(--font); font-size: .9rem;
    background: #f9fafb; outline: none;
}
.ss-search:focus { background: #fff; }

.ss-list { max-height: 220px; overflow-y: auto; }

.ss-item {
    padding: .6rem .875rem; font-size: .9rem; cursor: pointer;
    transition: background .1s;
}
.ss-item:hover    { background: var(--primary-light); }
.ss-item.ss-selected { background: var(--primary-light); font-weight: 600; color: var(--primary); }

/* ── Municipality typeahead ────────────────────────────────────────────── */
.mun-typeahead { position: relative; }
.mun-typeahead-drop {
    display: none; position: absolute; top: calc(100% + 4px); left: 0; right: 0;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.12);
    z-index: 1000; max-height: 240px; overflow-y: auto;
}
.mun-typeahead-drop.open { display: block; }
.mun-ta-item {
    padding: .6rem .875rem; font-size: .9rem; cursor: pointer;
    border-bottom: 1px solid #f3f4f6;
}
.mun-ta-item:last-child { border-bottom: none; }
.mun-ta-item:hover { background: var(--primary-light); }
.mun-ta-name  { font-weight: 500; }
.mun-ta-sub   { font-size: .78rem; color: var(--muted); margin-top: 2px; }
