:root {
  --bg: #0f1115;
  --card: #171a21;
  --border: #2a2e38;
  --text: #eef0f3;
  --muted: #9aa1ac;
  --accent: #ff5a5f;
  --accent2: #4cc9f0;
  --ok: #43c07b;
  --danger: #e5484d;
  --radius: 10px;
}

* { box-sizing: border-box; }

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

a { color: var(--accent2); text-decoration: none; }
a:hover { text-decoration: underline; }

.wrap {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 16px 60px;
}

.wrap.wide { max-width: 1100px; }

h1 { font-size: 1.5rem; margin: 0 0 4px; }
/* Titulo "Solicitud de acceso a <nombre>": linea pequena + nombre en su propia linea,
   sin partirlo por la mitad (solo se parte entre palabras y, si una sola no cabe, se trocea). */
h1.titulo-acceso { line-height: 1.2; margin-bottom: 8px; }
h1.titulo-acceso small { display: block; font-size: .85rem; font-weight: 500; opacity: .75; margin-bottom: 2px; }
h1.titulo-acceso .nombre-comunidad { display: block; font-size: 1.6rem; text-wrap: balance; overflow-wrap: anywhere; }
h2 { font-size: 1.15rem; margin: 28px 0 10px; }
p.subtitle { color: var(--muted); margin: 0 0 24px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

.field { margin-bottom: 18px; }
.field label { display: block; font-weight: 600; margin-bottom: 6px; font-size: 0.92rem; }
.field .hint { color: var(--muted); font-size: 0.8rem; margin-top: 4px; }

input[type="text"], input[type="tel"], input[type="date"], input[type="password"],
input[type="email"], input[type="file"], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #0d0f14;
  color: var(--text);
  font-size: 1rem;
}

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

/* Textareas con texto potencialmente largo (ej. "Motivo" en fichas importadas del
   excel viejo): min-height mayor por si JS esta desactivado, y un tope maximo para
   que una ficha con un texto enorme no deje la pagina kilometrica (a partir de ahi
   se puede hacer scroll dentro de la caja, o arrastrar la esquina para agrandarla mas). */
textarea.textarea-auto { min-height: 160px; max-height: 480px; overflow-y: auto; }

.checkbox-grid, .radio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
}

.checkbox-grid label, .radio-grid label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: normal;
  background: #0d0f14;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
}

.scale-grid {
  display: flex;
  gap: 8px;
}
.scale-grid label {
  flex: 1;
  text-align: center;
  background: #0d0f14;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 0;
  cursor: pointer;
}

button, .btn {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 11px 18px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
button.secondary, .btn.secondary { background: #2a2e38; }
button.ok, .btn.ok { background: var(--ok); }
button.danger, .btn.danger { background: var(--danger); }
button:hover, .btn:hover { opacity: 0.9; text-decoration: none; }

.error-box {
  background: rgba(229,72,77,0.12);
  border: 1px solid var(--danger);
  color: #ffb3b5;
  padding: 12px 14px;
  border-radius: 8px;
  margin-bottom: 18px;
}

.topnav {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
}
.topnav a {
  color: var(--muted);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 0.9rem;
}
.topnav a.active, .topnav a:hover { background: #21252f; color: var(--text); text-decoration: none; }
.topnav .spacer { flex: 1; }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; }
tr:hover td { background: #1c202a; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge.pendiente { background: rgba(255,193,7,0.15); color: #ffc107; }
.badge.aprobado { background: rgba(67,192,123,0.15); color: var(--ok); }
.badge.rechazado { background: rgba(229,72,77,0.15); color: var(--danger); }
.badge.bloqueado { background: rgba(255,255,255,0.12); color: var(--text); }

/* Miniatura de foto en listados (Solicitudes): para identificar a alguien de un
   vistazo sin tener que abrir la ficha, sobre todo cuando el nombre no dice mucho. */
.avatar-mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  background: #1c202a;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  flex-shrink: 0;
}
.avatar-mini-vacio { border: 1px dashed var(--border); }

.photo-picker { display: flex; gap: 10px; flex-wrap: wrap; }
.photo-picker label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  background: #0d0f14;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}
.photo-picker img { width: 120px; height: 120px; object-fit: cover; border-radius: 6px; }

.photos { display: flex; gap: 10px; flex-wrap: wrap; }
.photos img { width: 150px; height: 150px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; }

.photo-item { position: relative; }
.photo-delete-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: rgba(20,22,28,0.85);
  color: var(--danger);
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}
.photo-delete-btn:hover { background: var(--danger); color: #fff; }

/* Barras clicables reutilizables en Estadisticas: años de nacimiento, poblacion... */
.bar-chart { display: flex; flex-direction: column; gap: 4px; max-height: 520px; overflow-y: auto; }
.bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
  border-radius: 6px;
  text-decoration: none;
  color: var(--text);
}
.bar-row:hover { background: #1c202a; }
.bar-label { width: 48px; flex-shrink: 0; font-variant-numeric: tabular-nums; }
.bar-label-wide { width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; background: #1c202a; border-radius: 4px; height: 14px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--accent, #6c8cff); border-radius: 4px; }
.bar-count { width: 32px; text-align: right; flex-shrink: 0; color: var(--muted); font-size: 0.85rem; }

.lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}
.lightbox.open { display: flex; }
.lightbox img {
  max-width: 92vw;
  max-height: 90vh;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.6);
}
.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}
.lightbox-close:hover { background: rgba(255,255,255,0.2); opacity: 1; }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-scroll table { min-width: 640px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.stat-box { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; text-align: center; }
.stat-box .num { font-size: 1.8rem; font-weight: 700; }
.stat-box .label { color: var(--muted); font-size: 0.82rem; }

/* Cajas de estado convertidas en botones de filtro rapido (Solicitudes) */
.stat-box-link { display: block; text-decoration: none; color: inherit; cursor: pointer; transition: border-color .15s, background .15s; }
.stat-box-link:hover { border-color: var(--accent, #6c8cff); }
.stat-box-active { border-color: var(--accent, #6c8cff); background: rgba(108,140,255,0.1); }

.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.filters select, .filters input { width: auto; }

.center { text-align: center; }
.mt { margin-top: 16px; }
form.inline { display: inline; }

/* Formularios en rejilla (Crear usuario, etc): varios campos por fila en pantallas
   anchas, uno debajo de otro en movil, sin tener que tocar el patron .field normal. */
.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}
.field-grid .field { margin-bottom: 0; }

/* Gestion de usuarios: tarjetas en vez de tabla ancha, para que se lea bien tanto
   en movil (una columna) como en escritorio (varias columnas) sin scroll lateral.
   auto-fit (no auto-fill): con pocos usuarios, las tarjetas que SI existen crecen
   para aprovechar el ancho disponible en vez de dejar columnas vacias reservadas
   (con auto-fill se quedaba un hueco enorme a la derecha si solo habia 1 o 2). */
.user-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 20px;
}
.user-card { display: flex; flex-direction: column; gap: 20px; padding: 24px; }

/* Fila superior: avatar + formulario de usuario/nombre + badge de rol y fecha, todo
   en horizontal para aprovechar el ancho de la tarjeta (que ahora puede ser grande)
   en vez de apilar cada cosa en su propia linea como antes. */
.user-card-top { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.user-avatar {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: #21252f;
  color: var(--accent2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.25rem;
  flex-shrink: 0;
}
.user-edit-form { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; flex: 1 1 320px; }
.user-edit-form .field { margin-bottom: 0; flex: 1 1 150px; min-width: 140px; }
.user-edit-form button { flex-shrink: 0; }
.user-meta { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-left: auto; }
.user-meta .fecha-alta { color: var(--muted); font-size: 0.85rem; }

.user-card-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 20px; border-top: 1px solid var(--border); padding-top: 18px; }
.user-card-actions form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.user-card-actions form:last-child { margin-left: auto; }
.user-card-actions select { width: auto; }
.user-card-actions input[type="password"] { width: 190px; }
.badge.admin { background: rgba(76,201,240,0.15); color: var(--accent2); }
.badge.gestor { background: rgba(255,255,255,0.12); color: var(--text); }

@media (max-width: 640px) {
  .wrap { padding: 18px 12px 48px; }
  h1 { font-size: 1.3rem; }
  .card { padding: 14px; }
  .stat-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
  .checkbox-grid, .radio-grid { grid-template-columns: 1fr; }
  .scale-grid { flex-wrap: wrap; }
  .scale-grid label { flex: 1 1 30%; }
  .topnav { padding: 8px 10px; gap: 2px; }
  .topnav a { padding: 6px 8px; font-size: 0.85rem; }
  .photos img { width: 110px; height: 110px; }
  table { font-size: 0.85rem; }
  th, td { padding: 7px 8px; }
  .lightbox-close { top: 10px; right: 12px; width: 38px; height: 38px; }
  .user-card { padding: 16px; }
  .user-card-actions input[type="password"] { width: 100%; }
  .user-card-actions form { width: 100%; }
  .user-card-actions form:last-child { margin-left: 0; }
  .user-meta { margin-left: 0; }
}

/* Grupos del formulario: logo grande y casilla comoda de tocar */
.grupo-opcion { gap: 14px !important; padding: 14px 16px !important; font-size: 1.1rem; }
.grupo-opcion input[type=checkbox] { width: 22px; height: 22px; flex: none; }
.grupo-logo { width: 64px; height: 64px; flex: none; object-fit: cover; border-radius: 50%; background: #fff; }
@media (max-width: 600px) {
  .grupo-logo { width: 72px; height: 72px; }
  .grupo-opcion { font-size: 1.2rem; }
}
