/* =========================================================================
   Residencial - Sistema de Control de Accesos
   Diseño profesional - Verde claro y blanco
   ========================================================================= */

:root {
  /* Verde - color primario */
  --primary:        #4CAF50;
  --primary-dark:   #388E3C;
  --primary-light:  #C8E6C9;
  --primary-soft:   #E8F5E9;
  --primary-pale:   #F1F8F4;
  --accent:         #66BB6A;

  /* Morado - color de acento (Real del Campo) */
  --purple:         #7E22CE;
  --purple-dark:    #6B21A8;
  --purple-light:   #A855F7;
  --purple-soft:    #F3E8FF;
  --purple-pale:    #FAF5FF;

  --text:           #1F2937;
  --text-muted:     #6B7280;
  --text-light:     #9CA3AF;
  --bg:             #F8FAF9;
  --surface:        #FFFFFF;
  --border:         #E5E7EB;
  --border-light:   #F0F4F1;

  --success:        #10B981;
  --warning:        #F59E0B;
  --danger:         #EF4444;
  --info:           #3B82F6;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow:    0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-lg: 0 10px 25px rgba(16, 24, 40, 0.08), 0 4px 10px rgba(16, 24, 40, 0.04);

  --radius-sm: 6px;
  --radius:    10px;
  --radius-lg: 14px;

  --sidebar-w: 260px;
  --header-h:  64px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

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

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

h1, h2, h3, h4 { color: var(--text); font-weight: 600; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; }

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

.sidebar {
  width: var(--sidebar-w);
  background: linear-gradient(180deg, #FFFFFF 0%, var(--primary-pale) 60%, var(--purple-pale) 100%);
  border-right: 1px solid var(--border);
  position: fixed;
  inset: 0 auto 0 0;
  display: flex;
  flex-direction: column;
  z-index: 50;
  transition: transform .25s ease;
}

.sidebar-brand {
  padding: 18px 22px 14px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ====== LOGO ====== */
/* Wrapper: contiene fallback (atrás) y la imagen (al frente). Si la imagen
   carga, cubre el fallback; si no, queda visible el "RC". */
.logo-wrap {
  position: relative;
  display: inline-block;
  overflow: hidden;
  background: #fff;
  flex-shrink: 0;
  box-sizing: border-box;
  vertical-align: middle;
}
.logo-wrap > .logo-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--purple) 0%, var(--primary) 100%);
  color: #fff;
  font-weight: 700;
  font-family: -apple-system, Arial, sans-serif;
  z-index: 1;
  border-radius: inherit;
}
.logo-wrap > img {
  position: relative;
  display: block;
  z-index: 2;
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: inherit;
}

/* Logo del sidebar */
.sidebar-brand .logo {
  width: 48px;
  height: 48px;
  min-width: 48px;
  border-radius: 50%;
  padding: 2px;
  box-shadow: 0 2px 10px rgba(126,34,206,0.18), 0 0 0 1px var(--purple-soft);
}
.sidebar-brand .logo .logo-fallback { font-size: 15px; }

.sidebar-brand .name {
  font-weight: 600; font-size: 14px; line-height: 1.25; color: var(--purple-dark);
}
.sidebar-brand .name small {
  display: block; color: var(--text-muted); font-weight: 400; font-size: 11px; margin-top: 2px;
}

.sidebar-nav {
  flex: 1;
  padding: 14px 12px;
  overflow-y: auto;
}

.sidebar-section {
  font-size: 11px; text-transform: uppercase; letter-spacing: .8px;
  color: var(--text-light); padding: 14px 12px 6px; font-weight: 600;
}

.sidebar-nav a {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px;
  margin: 2px 0;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-weight: 500;
  font-size: 14px;
  text-decoration: none;
  transition: all .15s ease;
}

.sidebar-nav a:hover {
  background: var(--primary-soft);
  color: var(--primary-dark);
  text-decoration: none;
}

.sidebar-nav a.active {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  box-shadow: 0 2px 6px rgba(76,175,80,0.3);
}
.sidebar-section {
  color: var(--purple-dark);
  opacity: .85;
}

.sidebar-nav a .icon { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-footer {
  padding: 14px 18px;
  border-top: 1px solid var(--border-light);
  font-size: 12px; color: var(--text-muted);
}

.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 2px solid transparent;
  border-image: linear-gradient(90deg, var(--primary) 0%, var(--purple) 100%) 1;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  position: sticky; top: 0; z-index: 40;
}

.topbar h1 { margin: 0; font-size: 1.05rem; font-weight: 600; }
.topbar .breadcrumb { color: var(--text-muted); font-size: 13px; }
.topbar .breadcrumb b { color: var(--text); font-weight: 600; }

.topbar-user {
  display: flex; align-items: center; gap: 14px;
}
.topbar-user .avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600;
}
.topbar-user .info { font-size: 13px; line-height: 1.2; text-align: right; }
.topbar-user .info b { display: block; }
.topbar-user .info span { color: var(--text-muted); font-size: 12px; }

.menu-toggle { display: none; background: none; border: 0; padding: 8px; cursor: pointer; }
.menu-toggle svg { width: 24px; height: 24px; }

.content { padding: 26px 32px 50px; flex: 1; }

/* =========== ALERTS =========== */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  display: flex; justify-content: space-between; align-items: center;
  border: 1px solid transparent;
  font-size: 14px;
}
.alert button { background: none; border: 0; font-size: 20px; cursor: pointer; color: inherit; opacity: .6; }
.alert button:hover { opacity: 1; }
.alert-success { background: #ECFDF5; border-color: #A7F3D0; color: #047857; }
.alert-error   { background: #FEF2F2; border-color: #FECACA; color: #B91C1C; }
.alert-warning { background: #FFFBEB; border-color: #FDE68A; color: #B45309; }
.alert-info    { background: #EFF6FF; border-color: #BFDBFE; color: #1E40AF; }

/* =========== CARDS =========== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 22px;
  margin-bottom: 18px;
}

.card-header {
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--border-light);
  padding: 0 0 14px;
  margin-bottom: 18px;
}
.card-header h2 { margin: 0; font-size: 1.05rem; color: var(--purple-dark); }
.card-title-sub { color: var(--text-muted); font-size: 13px; margin: 0; }

/* =========== STAT CARDS =========== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  display: flex; align-items: center; gap: 16px;
  box-shadow: var(--shadow-sm);
  transition: transform .15s ease, box-shadow .15s ease;
}
.stat:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
.stat .icon-box {
  width: 48px; height: 48px;
  border-radius: 12px;
  background: var(--primary-soft);
  color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
}
.stat.green   .icon-box { background: #DCFCE7; color: #16A34A; }
.stat.blue    .icon-box { background: #DBEAFE; color: #2563EB; }
.stat.orange  .icon-box { background: #FEF3C7; color: #D97706; }
.stat.red     .icon-box { background: #FEE2E2; color: #DC2626; }
.stat .num { font-size: 1.6rem; font-weight: 700; line-height: 1.1; }
.stat .lbl { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

/* =========== TABLES =========== */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
table.data thead {
  background: linear-gradient(90deg, var(--primary-pale) 0%, var(--purple-pale) 100%);
}
table.data th {
  text-align: left;
  padding: 12px 14px;
  font-weight: 600;
  color: var(--purple-dark);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}
table.data tbody tr:hover { background: var(--primary-pale); }
table.data tbody tr:last-child td { border-bottom: none; }
.empty-row {
  text-align: center;
  color: var(--text-muted);
  padding: 32px !important;
  font-style: italic;
}

/* =========== BADGES =========== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .2px;
}
.badge-success   { background: #DCFCE7; color: #15803D; }
.badge-info      { background: var(--purple-soft); color: var(--purple-dark); }
.badge-warning   { background: #FEF3C7; color: #B45309; }
.badge-danger    { background: #FEE2E2; color: #B91C1C; }
.badge-secondary { background: #F3F4F6; color: #4B5563; }

/* =========== FORMS =========== */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group.full { grid-column: 1 / -1; }
.form-group label {
  font-weight: 500; font-size: 13px; color: var(--text);
}
.form-group label .req { color: var(--danger); }
.form-group .hint { font-size: 12px; color: var(--text-muted); }

input[type="text"], input[type="email"], input[type="password"],
input[type="tel"], input[type="number"], input[type="date"],
input[type="time"], input[type="datetime-local"],
select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  font-size: 16px;          /* 16px previene zoom automático en iOS */
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%236B7280' d='M6 8L0 0h12z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(76,175,80,0.18);
}

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

/* =========== BUTTONS =========== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px;
  min-height: 42px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 14px; font-weight: 500;
  cursor: pointer;
  transition: all .15s ease;
  text-decoration: none;
  font-family: inherit;
  line-height: 1.2;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.btn:hover { text-decoration: none; }
.btn-primary {
  background: var(--primary); color: #fff;
  box-shadow: 0 1px 2px rgba(76,175,80,0.25);
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary {
  background: var(--surface); color: var(--text); border-color: var(--border);
}
.btn-secondary:hover { background: var(--primary-pale); border-color: var(--primary-light); }
.btn-danger {
  background: var(--danger); color: #fff;
}
.btn-danger:hover { background: #DC2626; }
.btn-ghost {
  background: transparent; color: var(--primary-dark);
}
.btn-ghost:hover { background: var(--primary-soft); }
.btn-sm { padding: 7px 12px; font-size: 13px; min-height: 36px; }
.btn-lg { padding: 14px 22px; font-size: 16px; min-height: 50px; }
.btn-block { width: 100%; }

.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }

/* =========== LOGIN =========== */
.auth-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, var(--purple-pale) 0%, #FFFFFF 50%, var(--primary-pale) 100%);
  padding: 20px;
}
.auth-card {
  width: 100%; max-width: 420px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 36px 32px;
  border-top: 4px solid transparent;
  border-image: linear-gradient(90deg, var(--primary) 0%, var(--purple) 100%) 1;
}
.auth-card .brand {
  text-align: center; margin-bottom: 26px;
}
.auth-card .brand .logo-big {
  width: 110px;
  height: 110px;
  margin: 0 auto 14px;
  border-radius: 50%;
  padding: 4px;
  box-shadow: 0 8px 24px rgba(126,34,206,0.2), 0 0 0 1px var(--purple-soft);
  display: block;
}
.auth-card .brand .logo-big .logo-fallback { font-size: 32px; }
.auth-card h1 {
  color: var(--purple-dark);
}
.auth-card h1 { font-size: 1.3rem; margin-bottom: 4px; }
.auth-card .sub { color: var(--text-muted); font-size: 13.5px; }

/* =========== QR =========== */
.qr-box {
  display: flex; flex-direction: column; align-items: center;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
}
.qr-box canvas, .qr-box img { max-width: 240px; height: auto; }
.qr-box .meta { margin-top: 14px; font-size: 13px; color: var(--text-muted); }
.qr-token {
  background: var(--primary-pale);
  padding: 6px 12px; border-radius: var(--radius-sm);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; color: var(--primary-dark);
  word-break: break-all;
  margin-top: 8px;
}

/* =========== SCANNER =========== */
.scanner-wrap {
  max-width: 460px; margin: 0 auto;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}
#qr-reader { width: 100%; }

.toolbar {
  display: flex; flex-wrap: wrap; gap: 10px;
  align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.toolbar .filters { display: flex; flex-wrap: wrap; gap: 8px; }

.kv {
  display: grid; grid-template-columns: 160px 1fr;
  gap: 8px 16px;
  font-size: 14px;
}
.kv dt { color: var(--text-muted); font-weight: 500; }
.kv dd { margin: 0; color: var(--text); }

/* =========== UTILIDADES DE LAYOUT =========== */
.row-2col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  margin-bottom: 18px;
}
.row-3col {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 18px;
  margin-bottom: 18px;
}
.qr-detail-grid {
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
  gap: 30px;
  align-items: start;
}

/* =========== RESPONSIVE - TABLETS Y MÓVILES =========== */
@media (max-width: 900px) {
  .sidebar {
    width: 86%;
    max-width: 320px;
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }
  .sidebar-backdrop {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 45;
  }
  .sidebar.open ~ .sidebar-backdrop,
  body.sidebar-open .sidebar-backdrop { display: block; }

  .main { margin-left: 0; }
  .menu-toggle { display: inline-flex; align-items: center; }
  .content { padding: 16px 14px 60px; }
  .topbar { padding: 0 14px; height: 56px; }
  .topbar h1 { font-size: .98rem; }
  .topbar .breadcrumb { font-size: 11.5px; }
  .topbar-user .info { display: none; }       /* solo avatar en móvil */

  .row-2col, .row-3col { grid-template-columns: 1fr; gap: 14px; margin-bottom: 14px; }
  .qr-detail-grid { grid-template-columns: 1fr; gap: 18px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }

  .card { padding: 16px 14px; margin-bottom: 14px; border-radius: 10px; }
  .card-header { flex-wrap: wrap; gap: 10px; }
  .card-header .btn { width: 100%; }

  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv dt { margin-top: 8px; font-size: 12px; }
  .kv dd { font-size: 14px; }

  .stat { padding: 14px; gap: 12px; }
  .stat .num { font-size: 1.35rem; }
  .stat .icon-box { width: 40px; height: 40px; font-size: 18px; }

  /* Tablas: hacer scroll horizontal pero mantener el primer col visible */
  .table-wrap { border-radius: 8px; margin: 0 -4px; }
  table.data th, table.data td { padding: 10px 12px; font-size: 13px; }
  table.data th { white-space: nowrap; }

  .toolbar { flex-direction: column; align-items: stretch; gap: 8px; }
  .toolbar input, .toolbar select { width: 100%; }
  .toolbar form { width: 100%; }
  .toolbar .filters { flex-wrap: wrap; }
  .toolbar .filters .btn { flex: 1 1 calc(50% - 4px); min-width: 0; }

  .btn-row { gap: 8px; }
  .btn-row .btn { flex: 1 1 auto; min-width: 0; }

  h1 { font-size: 1.35rem; }
  h2 { font-size: 1.15rem; }
  h3 { font-size: 1.0rem; }

  .form-grid { grid-template-columns: 1fr; gap: 12px; }
}

/* Móviles pequeños */
@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .stat .num { font-size: 1.3rem; }
  .content { padding: 14px 10px 60px; }
  .auth-card { padding: 26px 20px; }
  .auth-card .brand .logo-big { width: 90px; height: 90px; }
  .auth-card .brand .logo-big .logo-fallback { font-size: 26px; }
  .auth-card h1 { font-size: 1.15rem; }

  /* En móvil, tablas con acciones a la derecha quedan apretadas */
  table.data td.text-right .btn { display: block; width: 100%; margin-top: 4px; }
  table.data td.text-right .btn + .btn { margin-left: 0; }

  .btn { padding: 12px 16px; }
  .btn-sm { padding: 8px 12px; }
}

/* Modo paisaje en móvil pequeño */
@media (max-height: 480px) and (max-width: 900px) {
  .topbar { height: 50px; }
  .stat { padding: 10px 12px; }
}

/* Ocultar scrollbar feo en tablas */
.table-wrap::-webkit-scrollbar { height: 8px; }
.table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* Util */
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.mt-0 { margin-top: 0 !important; }
.mt-2 { margin-top: 16px; }
.mb-0 { margin-bottom: 0 !important; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.hidden { display: none !important; }

@media print {
  .sidebar, .topbar, .btn-row, .toolbar, .no-print { display: none !important; }
  .main { margin-left: 0 !important; }
  body { background: #fff; }
  .card { border: 1px solid #ccc; box-shadow: none; }
}
