:root {
  --color-brand-primary: #2563EB;
  --color-brand-primary-hover: #1D4ED8;
  --color-brand-primary-text: #FFFFFF;
  --color-brand-accent: #0EA5E9;
  --color-secondary: #64748B;
  --color-success: #16A34A;
  --color-success-bg: #F0FDF4;
  --color-warning: #D97706;
  --color-warning-bg: #FFFBEB;
  --color-danger: #DC2626;
  --color-danger-bg: #FEF2F2;
  --color-info: #0284C7;
  --color-info-bg: #F0F9FF;
  --color-surface: #FFFFFF;
  --color-surface-secondary: #F4F7FB;
  --color-surface-tertiary: #E8EEF6;
  --color-border: #D7E0EC;
  --color-border-focus: var(--color-brand-primary);
  --color-text-primary: #0F172A;
  --color-text-secondary: #475569;
  --color-text-muted: #94A3B8;
  --color-text-on-brand: #FFFFFF;
  --sidebar-width: 272px;
  --topbar-height: 64px;
  --font-sans: "IBM Plex Sans", system-ui, -apple-system, sans-serif;
  --font-mono: "IBM Plex Mono", "Fira Code", monospace;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 18px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05), 0 1px 3px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 8px 24px -12px rgba(15, 23, 42, 0.18);
  --shadow-lg: 0 18px 40px -20px rgba(37, 99, 235, 0.35);
}

[data-theme="dark"] {
  --color-brand-primary: #3B82F6;
  --color-brand-primary-hover: #60A5FA;
  --color-brand-accent: #38BDF8;
  --color-surface: #0B1220;
  --color-surface-secondary: #111827;
  --color-surface-tertiary: #1E293B;
  --color-border: #334155;
  --color-text-primary: #F1F5F9;
  --color-text-secondary: #94A3B8;
  --color-text-muted: #64748B;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 10px 28px -14px rgba(0, 0, 0, 0.55);
  --shadow-lg: 0 18px 40px -18px rgba(59, 130, 246, 0.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  background:
    radial-gradient(1200px 500px at 100% -10%, color-mix(in srgb, var(--color-brand-accent) 18%, transparent), transparent 55%),
    radial-gradient(900px 420px at -10% 0%, color-mix(in srgb, var(--color-brand-primary) 14%, transparent), transparent 50%),
    var(--color-surface-secondary);
  color: var(--color-text-primary);
}
a { color: var(--color-brand-primary); text-decoration: none; }
.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;
}

/* —— Shell —— */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100vh;
}
.workspace {
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* —— Sidebar —— */
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(180deg, color-mix(in srgb, var(--color-brand-primary) 8%, var(--color-surface)) 0%, var(--color-surface) 28%),
    var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: 1rem .85rem 1rem;
  z-index: 40;
}
.sidebar-brand {
  display: flex;
  gap: .75rem;
  align-items: center;
  padding: .55rem .65rem 1.1rem;
  margin-bottom: .25rem;
}
.brand-mark {
  width: 2.1rem;
  height: 2.1rem;
  max-width: 2.1rem;
  max-height: 2.1rem;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: .75rem;
  letter-spacing: .02em;
  color: #fff;
  background: linear-gradient(145deg, var(--color-brand-primary), var(--color-brand-accent));
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  flex-shrink: 0;
}
.brand-mark-logo {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: none;
  padding: .15rem;
}
.brand-mark-logo img {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.brand-name {
  font-weight: 750;
  font-size: 1.05rem;
  line-height: 1.15;
  letter-spacing: -.01em;
}
.brand-tag {
  color: var(--color-text-muted);
  font-size: .72rem;
  margin-top: .15rem;
  line-height: 1.3;
}

.sidebar-nav {
  flex: 1;
  overflow: auto;
  padding: 0 .2rem;
  scrollbar-width: thin;
}
.nav-section {
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  margin: 1rem 0 .4rem .85rem;
}
.nav-section:first-child { margin-top: .2rem; }

.nav-link {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .62rem .75rem;
  border-radius: 12px;
  color: var(--color-text-secondary);
  margin-bottom: .2rem;
  font-weight: 550;
  font-size: .9rem;
  transition: background .15s ease, color .15s ease, transform .15s ease;
  position: relative;
}
.nav-link:hover {
  background: var(--color-surface-tertiary);
  color: var(--color-text-primary);
}
.nav-link.is-active {
  background: color-mix(in srgb, var(--color-brand-primary) 14%, transparent);
  color: var(--color-brand-primary);
  font-weight: 650;
  box-shadow: inset 3px 0 0 var(--color-brand-primary);
}
.nav-ico {
  width: 1.15rem;
  height: 1.15rem;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  opacity: .9;
}
.nav-ico svg { width: 100%; height: 100%; }
.nav-badge {
  margin-left: auto;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 .35rem;
  border-radius: 999px;
  background: var(--color-brand-primary);
  color: #fff;
  font-size: .68rem;
  font-weight: 700;
  display: inline-grid;
  place-items: center;
}

.sidebar-foot {
  border-top: 1px solid var(--color-border);
  padding-top: .85rem;
  margin-top: .75rem;
}
.user-chip {
  display: flex;
  gap: .65rem;
  align-items: center;
  padding: .45rem .55rem .7rem;
}
.user-avatar {
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-weight: 750;
  color: #fff;
  background: linear-gradient(145deg, #0EA5E9, #2563EB);
  flex: 0 0 auto;
}
.user-meta {
  min-width: 0;
  display: grid;
}
.user-meta strong {
  font-size: .82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-meta span {
  font-size: .7rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-logout { color: var(--color-text-secondary); }

/* —— Topbar —— */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  min-height: var(--topbar-height);
  height: auto;
  max-height: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .75rem;
  padding: .55rem 1.1rem;
  background: color-mix(in srgb, var(--color-surface) 82%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid color-mix(in srgb, var(--color-border) 80%, transparent);
}
.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: .55rem;
  min-width: 0;
}
.topbar-right {
  flex-wrap: nowrap;
  justify-content: flex-end;
}
.page-heading { min-width: 0; }
.page-kicker {
  margin: 0;
  font-size: .62rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--color-brand-primary);
}
.page-title {
  margin: .05rem 0 0;
  font-size: 1.05rem;
  font-weight: 750;
  letter-spacing: -.015em;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  position: relative;
  width: 2.15rem;
  height: 2.15rem;
  border-radius: 10px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-secondary);
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease, transform .15s ease;
  flex: 0 0 auto;
}
.icon-btn:hover {
  color: var(--color-brand-primary);
  border-color: color-mix(in srgb, var(--color-brand-primary) 35%, var(--color-border));
  background: color-mix(in srgb, var(--color-brand-primary) 8%, var(--color-surface));
}
.icon-btn svg { width: 1.05rem; height: 1.05rem; display: block; }
.icon-btn .ico-moon { display: none; }
[data-theme="dark"] .icon-btn .ico-sun { display: none; }
[data-theme="dark"] .icon-btn .ico-moon { display: block; }
.dot-badge {
  position: absolute;
  top: .2rem;
  right: .2rem;
  min-width: .85rem;
  height: .85rem;
  padding: 0 .15rem;
  border-radius: 999px;
  background: var(--color-danger);
  color: #fff;
  font-size: .58rem;
  font-weight: 700;
  display: grid;
  place-items: center;
  border: 2px solid var(--color-surface);
}
.sidebar-toggle { display: none; }

/* —— Period switcher (header) —— */
.period-switch {
  position: relative;
  display: flex;
  align-items: center;
  gap: .5rem;
  min-width: 0;
  max-width: 300px;
  padding: .35rem .55rem;
  border-radius: 12px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
  margin: 0;
  overflow: hidden;
  flex: 0 1 auto;
}
.period-switch-glow { display: none; }
.period-switch-icon {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--color-brand-accent) 16%, transparent);
  color: var(--color-brand-accent);
  flex: 0 0 auto;
  z-index: 1;
}
.period-switch-icon svg { width: 1rem; height: 1rem; display: block; }
.period-switch-body {
  display: grid;
  gap: .05rem;
  min-width: 0;
  flex: 1;
  z-index: 1;
}
.period-controls {
  display: flex;
  align-items: center;
  gap: .35rem;
  min-width: 0;
}
.period-select {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--color-text-primary);
  font: inherit;
  font-weight: 700;
  font-size: .82rem;
  padding: 0;
  margin: 0;
  max-width: 100%;
  cursor: pointer;
  min-width: 0;
  line-height: 1.2;
}
.period-param {
  display: inline-flex;
  align-items: center;
  gap: .25rem;
  padding: .1rem .35rem;
  border-radius: 7px;
  background: color-mix(in srgb, var(--color-surface) 80%, transparent);
  border: 1px solid var(--color-border);
  font-size: .62rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  flex: 0 0 auto;
}
.period-param input {
  width: 3.1rem;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: .8rem;
  font-weight: 750;
  color: var(--color-text-primary);
  text-transform: none;
  letter-spacing: 0;
  padding: 0;
  line-height: 1.2;
}
.period-param input:focus { outline: none; }

/* —— Municipality switcher —— */
.mun-switch {
  position: relative;
  display: flex;
  align-items: center;
  gap: .55rem;
  min-width: 0;
  max-width: 280px;
  padding: .4rem .6rem;
  border-radius: 12px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  text-decoration: none;
  color: inherit;
  flex: 0 1 auto;
}
.mun-switch-glow { display: none; }
.mun-switch.is-interactive { cursor: pointer; }
.mun-switch.is-interactive:hover {
  border-color: color-mix(in srgb, var(--color-brand-primary) 45%, var(--color-border));
  box-shadow: var(--shadow-md);
}
.mun-switch-icon {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--color-brand-primary) 16%, transparent);
  color: var(--color-brand-primary);
  flex: 0 0 auto;
  z-index: 1;
}
.mun-switch-icon svg { width: 1rem; height: 1rem; display: block; }
.mun-switch-body {
  min-width: 0;
  flex: 1;
  display: grid;
  z-index: 1;
}
.mun-label {
  font-size: .62rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  line-height: 1.1;
}
.mun-name {
  font-size: .85rem;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mun-meta {
  font-size: .68rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mun-select {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--color-text-primary);
  font: inherit;
  font-size: .85rem;
  font-weight: 700;
  padding: 0;
  margin: 0;
  width: 100%;
  max-width: 100%;
  cursor: pointer;
  outline: none;
}
.mun-chevron {
  width: .9rem;
  height: .9rem;
  color: var(--color-text-muted);
  flex: 0 0 auto;
  z-index: 1;
}
.mun-chevron svg { width: 100%; height: 100%; display: block; }
.mun-empty { min-width: 180px; max-width: 220px; }

.main {
  padding: 1.15rem 1.35rem 2.25rem;
  flex: 1;
}

.flash {
  border-radius: 12px;
  padding: .75rem 1rem;
  margin-bottom: .85rem;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
}
.flash-success { background: var(--color-success-bg); color: var(--color-success); border-color: transparent; }
.flash-error, .flash-danger { background: var(--color-danger-bg); color: var(--color-danger); border-color: transparent; }
.flash-warning { background: var(--color-warning-bg); color: var(--color-warning); border-color: transparent; }
.flash-info { background: var(--color-info-bg); color: var(--color-info); border-color: transparent; }

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .45);
  backdrop-filter: blur(2px);
  z-index: 35;
}

/* —— Shared components —— */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow-sm);
}
.grid-kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .85rem; margin-bottom: 1rem; }
.kpi .label { color: var(--color-text-muted); font-size: .8rem; }
.kpi .value { font-size: 1.6rem; font-weight: 700; }
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  background: var(--color-brand-primary); color: #fff; border: 0;
  border-radius: var(--radius-md); padding: .55rem .9rem; cursor: pointer; font-weight: 600;
  font: inherit;
}
.btn:hover { background: var(--color-brand-primary-hover); }
.btn.secondary { background: var(--color-surface-tertiary); color: var(--color-text-primary); }
.btn.secondary:hover { filter: brightness(.97); }
.btn.danger { background: var(--color-danger); }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { padding: .55rem .45rem; border-bottom: 1px solid var(--color-border); text-align: left; }
.badge { display: inline-block; padding: .15rem .45rem; border-radius: 999px; font-size: .75rem; background: var(--color-surface-tertiary); }
.badge.ok { background: var(--color-success-bg); color: var(--color-success); }
.badge.warn { background: var(--color-warning-bg); color: var(--color-warning); }
.badge.off { background: var(--color-danger-bg); color: var(--color-danger); }
.auth-shell { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; }
.auth-panel {
  background: linear-gradient(145deg, var(--color-brand-primary), color-mix(in srgb, var(--color-brand-accent) 70%, #0f172a));
  color: #fff;
  display: flex; align-items: center; justify-content: center; padding: 2rem;
}
.auth-card-wrap { display: flex; align-items: center; justify-content: center; padding: 2rem; }
.auth-card {
  width: min(440px, 100%); background: var(--color-surface);
  border-radius: var(--radius-lg); padding: 2rem; box-shadow: var(--shadow-md);
  border: 1px solid var(--color-border);
}
.auth-card h1 { margin-top: 0; font-size: 1.4rem; }
.form-field { margin-bottom: 1rem; }
.form-field label { display: block; margin-bottom: .35rem; font-size: .9rem; color: var(--color-text-secondary); }
.form-field input, .form-field select, .form-field textarea {
  width: 100%; padding: .65rem .75rem; border: 1px solid var(--color-border);
  border-radius: var(--radius-md); background: var(--color-surface); color: var(--color-text-primary);
  font: inherit;
}
.error { color: var(--color-danger); font-size: .9rem; margin-bottom: .75rem; }
.muted { color: var(--color-text-muted); }

/* Subnav used inside vínculo pages */
.subnav .btn { font-size: .85rem; }

@media (max-width: 1100px) {
  .mun-switch { max-width: 240px; }
  .period-switch { max-width: 260px; }
  .page-title { font-size: 1rem; }
}
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar-toggle { display: inline-grid; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: min(300px, 88vw);
    transform: translateX(-105%);
    transition: transform .28s cubic-bezier(.22, 1, .36, 1);
    box-shadow: var(--shadow-md);
  }
  .shell.sidebar-open .sidebar { transform: translateX(0); }
  .shell.sidebar-open .sidebar-backdrop {
    display: block;
  }
  .topbar { padding: .5rem .85rem; }
  .main { padding: 1rem 1rem 2rem; }
  .auth-shell { grid-template-columns: 1fr; }
  .auth-panel { min-height: 180px; }
  .mun-meta, .period-switch .mun-meta { display: none; }
  .period-switch-icon, .mun-switch-icon { display: none; }
}
@media (max-width: 640px) {
  .page-kicker { display: none; }
  .topbar-right {
    flex: 1;
    justify-content: flex-end;
    gap: .4rem;
  }
  .period-switch, .mun-switch {
    max-width: none;
    flex: 1 1 auto;
    min-width: 0;
  }
}
