:root {
  --primary: #1e3a5f;
  --primary-dark: #142849;
  --accent: #c8a45c;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1d2230;
  --muted: #6b7280;
  --border: #e5e7eb;
  --success: #22c55e;
  --danger: #dc2626;
  --warning: #f59e0b;
  --shadow: 0 2px 8px rgba(0,0,0,0.06);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color: var(--text); background: var(--bg);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.screen { min-height: 100vh; min-height: 100dvh; }
.muted { color: var(--muted); font-weight: normal; font-size: 0.9em; }

/* ===== LOGIN ===== */
#login-screen {
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1e3a5f 0%, #2d5a8d 100%);
  padding: 1rem;
}
.login-box {
  background: var(--surface);
  padding: 1.75rem 1.5rem 1.5rem;
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  width: 100%; max-width: 420px;
}
.logo { text-align: center; margin-bottom: 1.25rem; }
.logo-img { width: 92px; height: 92px; display: inline-block; }
.logo h1 { margin: 0.75rem 0 0.25rem; color: var(--primary); font-size: 1.35rem; line-height: 1.2; }
.subtitle { color: var(--muted); margin: 0; font-size: 0.9rem; }

#login-form label { display: block; font-size: 0.85rem; font-weight: 600; margin: 1rem 0 0.35rem; color: var(--primary); }
#login-form input,
#login-form select {
  width: 100%; padding: 0.75rem;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 1rem; background: white; color: var(--text);
  appearance: none; -webkit-appearance: none;
}
#login-form select {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%231e3a5f' d='M2 4l4 4 4-4z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}
#login-form button {
  width: 100%; padding: 0.85rem;
  margin-top: 1.5rem;
  background: var(--primary); color: white;
  border: none; border-radius: 8px;
  font-size: 1rem; font-weight: 600;
  cursor: pointer; min-height: 48px;
}
#login-form button:hover { background: var(--primary-dark); }
.error-msg { color: var(--danger); margin-top: 0.75rem; font-size: 0.9rem; min-height: 1.2em; }
.hint { font-size: 0.8rem; color: var(--muted); text-align: center; margin-top: 1.25rem; line-height: 1.4; }

/* ===== APP HEADER ===== */
.app-header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 1rem;
  background: var(--primary);
  color: white;
  padding: 0.75rem 1.25rem;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 0.65rem; min-width: 0; }
.brand-icon { width: 38px; height: 38px; flex-shrink: 0; border-radius: 6px; background: rgba(255,255,255,0.08); padding: 2px; }
.brand-text { min-width: 0; }
.brand strong { display: block; font-size: 0.95rem; line-height: 1.1; }
.brand small { display: block; opacity: 0.75; font-size: 0.72rem; margin-top: 2px; }

.tabs {
  display: flex; gap: 0.25rem;
  justify-content: center;
  flex-wrap: wrap;
}
.tab-btn {
  background: transparent; color: rgba(255,255,255,0.85);
  border: none; padding: 0.55rem 0.85rem;
  border-radius: 8px; cursor: pointer;
  font-size: 0.88rem; min-height: 40px;
  display: inline-flex; align-items: center; gap: 0.3rem;
  white-space: nowrap;
}
.tab-btn:hover { background: rgba(255,255,255,0.1); }
.tab-btn.active { background: var(--accent); color: var(--primary-dark); font-weight: 600; }

.user-info { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; }
#user-label { white-space: nowrap; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.btn-ghost {
  background: transparent; border: 1px solid rgba(255,255,255,0.3);
  color: white; padding: 0.5rem 0.85rem; border-radius: 8px;
  cursor: pointer; min-height: 38px;
}
.btn-ghost:hover { background: rgba(255,255,255,0.1); }

.app-main { padding: 1.25rem; max-width: 1400px; margin: 0 auto; }
.app-footer { text-align: center; padding: 1rem; color: var(--muted); font-size: 0.8rem; border-top: 1px solid var(--border); margin-top: 2rem; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
h2 { color: var(--primary); margin-top: 0; }
h3 { color: var(--primary); font-size: 1rem; margin: 0 0 0.75rem; }

/* ===== FILTERS ===== */
.filters { display: flex; gap: 0.75rem; align-items: flex-end; flex-wrap: wrap; }
.filters label { display: flex; flex-direction: column; font-size: 0.8rem; font-weight: 600; color: var(--primary); gap: 0.25rem; }
.filters select {
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.9rem; min-width: 180px; background: white;
  min-height: 42px;
}
.filters button { align-self: flex-end; }

.estaca-banner {
  background: linear-gradient(135deg, #1e3a5f 0%, #2d5a8d 50%, #3b6da5 100%);
  color: white;
  padding: 1rem 1.5rem;
  border-radius: 14px;
  margin-bottom: 1.5rem;
  font-size: 0.95rem;
  box-shadow: 0 4px 18px rgba(30,58,95,0.20);
  position: relative;
  overflow: hidden;
}
.estaca-banner::before {
  content: ''; position: absolute; top: -30%; right: -5%;
  width: 220px; height: 220px;
  background: radial-gradient(circle, rgba(200,164,92,0.20) 0%, transparent 70%);
  pointer-events: none;
}
.estaca-banner strong { font-size: 1.1em; font-weight: 700; }
.estaca-banner .muted { color: rgba(255,255,255,0.78); }
.estaca-tag {
  background: var(--accent); color: var(--primary-dark);
  padding: 0.22rem 0.8rem; border-radius: 100px;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.08em;
  margin-right: 0.6rem; display: inline-block; vertical-align: middle;
  box-shadow: 0 2px 6px rgba(200,164,92,0.3);
}

/* ===== DASHBOARD - KPI CARDS ===== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 1rem;
  margin-bottom: 1.75rem;
}
.kpi {
  background: linear-gradient(135deg, #ffffff 0%, #fafbfc 100%);
  padding: 1.05rem 1.2rem;
  border-radius: 14px;
  border: 1px solid rgba(20,40,73,0.06);
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 4px 16px rgba(20,40,73,0.06);
  display: flex;
  align-items: flex-start;
  gap: 0.9rem;
  position: relative;
  overflow: hidden;
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}
.kpi::before {
  content: '';
  position: absolute; top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--kpi-color, var(--accent)) 0%, transparent 70%);
}
.kpi:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0,0,0,0.06), 0 14px 32px rgba(20,40,73,0.14);
  border-color: rgba(20,40,73,0.12);
}
.kpi-icon {
  flex-shrink: 0;
  width: 42px; height: 42px;
  border-radius: 11px;
  display: flex; align-items: center; justify-content: center;
  background: var(--kpi-bg, rgba(30,58,95,0.08));
  color: var(--kpi-color, var(--primary));
}
.kpi-icon svg { width: 22px; height: 22px; }
.kpi-content { min-width: 0; flex: 1; }
.kpi-label {
  font-size: 0.7rem; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em;
  font-weight: 600; line-height: 1.25;
  white-space: normal; overflow: visible; text-overflow: clip;
  word-break: normal;
  /* asegura espacio para 2 líneas y evita que se recorte */
  display: block; min-height: 2em;
}
.kpi-value {
  font-size: 1.9rem;
  font-weight: 700; color: var(--primary);
  line-height: 1.05; margin-top: 0.32rem;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.kpi-trend {
  font-size: 0.7rem; margin-top: 0.3rem;
  color: var(--muted); line-height: 1.25;
  white-space: normal; overflow: visible; text-overflow: clip;
}

/* Variantes de color */
.kpi-primary    { --kpi-color: #1e3a5f; --kpi-bg: rgba(30,58,95,0.10); }
.kpi-accent     { --kpi-color: #c8a45c; --kpi-bg: rgba(200,164,92,0.16); }
.kpi-success    { --kpi-color: #16a34a; --kpi-bg: rgba(34,197,94,0.13); }
.kpi-info       { --kpi-color: #2563eb; --kpi-bg: rgba(37,99,235,0.11); }
.kpi-temple     { --kpi-color: #7c3aed; --kpi-bg: rgba(124,58,237,0.11); }
.kpi-warm       { --kpi-color: #db2777; --kpi-bg: rgba(219,39,119,0.11); }
.kpi-purple     { --kpi-color: #5340b5; --kpi-bg: rgba(83,64,181,0.11); }
.kpi-recent     { --kpi-color: #0891b2; --kpi-bg: rgba(8,145,178,0.11); }
.kpi-priesthood { --kpi-color: #1e3a5f; --kpi-bg: rgba(30,58,95,0.10); }

/* Alerta — sin ministrantes */
.kpi-alert {
  --kpi-color: #dc2626; --kpi-bg: rgba(220,38,38,0.12);
  background: linear-gradient(135deg, #fff 0%, #fef5f5 100%);
  border-color: rgba(220,38,38,0.20);
}
.kpi-alert .kpi-value { color: #dc2626; }

/* Índice Senda — destacado */
.kpi-index {
  background: linear-gradient(135deg, #1e3a5f 0%, #2d5a8d 100%);
  color: #fff;
  border-color: rgba(255,255,255,0.18);
  box-shadow: 0 4px 18px rgba(30,58,95,0.25);
}
.kpi-index::before { background: linear-gradient(90deg, #c8a45c 0%, transparent 70%); height: 4px; }
.kpi-index .kpi-label { color: rgba(255,255,255,0.85); }
.kpi-index .kpi-value { color: #fff; }
.kpi-index .kpi-trend { color: rgba(255,255,255,0.7); }
.kpi-index .kpi-icon { background: rgba(255,217,163,0.20); color: #ffd9a3; }
.kpi-index:hover { box-shadow: 0 6px 14px rgba(0,0,0,0.08), 0 18px 40px rgba(30,58,95,0.35); }

/* ===== CHARTS ===== */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); gap: 1.1rem; }
.chart-card {
  background: #ffffff;
  padding: 1.25rem 1.25rem 1rem;
  border-radius: 14px;
  border: 1px solid rgba(20,40,73,0.06);
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 4px 16px rgba(20,40,73,0.06);
  transition: box-shadow 0.18s ease;
}
.chart-card:hover { box-shadow: 0 4px 10px rgba(0,0,0,0.06), 0 14px 32px rgba(20,40,73,0.12); }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; margin-bottom: 0.8rem; flex-wrap: wrap; }
.chart-head h3 { margin: 0; font-size: 0.98rem; }
.chart-hint { font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 500; }
.chart-box { position: relative; width: 100%; height: 280px; }
.chart-box-donut { display: flex; align-items: center; justify-content: center; }
.donut-center {
  position: absolute;
  top: 42%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
}
.donut-num { font-size: 1.85rem; font-weight: 700; color: var(--primary); line-height: 1; letter-spacing: -0.02em; }
.donut-lbl { font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; margin-top: 4px; font-weight: 600; }
.chart-card.wide { grid-column: 1 / -1; }
.chart-card.wide .chart-box { height: 320px; }

/* Animación de entrada del dashboard */
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
#tab-dashboard.active .kpi { animation: fadeUp 0.4s ease both; }
#tab-dashboard.active .kpi:nth-child(1){animation-delay:0.00s}
#tab-dashboard.active .kpi:nth-child(2){animation-delay:0.04s}
#tab-dashboard.active .kpi:nth-child(3){animation-delay:0.08s}
#tab-dashboard.active .kpi:nth-child(4){animation-delay:0.12s}
#tab-dashboard.active .kpi:nth-child(5){animation-delay:0.16s}
#tab-dashboard.active .kpi:nth-child(6){animation-delay:0.20s}
#tab-dashboard.active .kpi:nth-child(7){animation-delay:0.24s}
#tab-dashboard.active .kpi:nth-child(8){animation-delay:0.28s}
#tab-dashboard.active .kpi:nth-child(9){animation-delay:0.32s}
#tab-dashboard.active .kpi:nth-child(10){animation-delay:0.36s}
#tab-dashboard.active .kpi:nth-child(11){animation-delay:0.40s}
#tab-dashboard.active .chart-card { animation: fadeUp 0.45s ease both; animation-delay: 0.35s; }

/* ===== FORM: helpers nuevos (cédula obligatoria nuevo, sacerdocio bloqueado) ===== */
.form-grid small.muted { font-size: 0.72rem; font-weight: normal; margin-top: 2px; line-height: 1.3; }
.form-grid .req-new {
  background: var(--accent);
  color: var(--primary-dark);
  font-size: 0.6rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 8px;
  letter-spacing: 0.05em;
  margin-left: 4px;
  vertical-align: middle;
  text-transform: uppercase;
}
.form-grid select.disabled-na,
.form-grid select:disabled {
  background-color: #f3f4f6;
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.85;
}
#f-cedula {
  font-family: 'Menlo','Consolas','Courier New',monospace;
  letter-spacing: 0.04em;
}

/* ===== KPI clickeables (drill-down) ===== */
.kpi[data-drill] {
  cursor: pointer;
  position: relative;
  user-select: none;
}
.kpi[data-drill]:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 14px rgba(0,0,0,0.08), 0 18px 36px rgba(20,40,73,0.16);
}
.kpi[data-drill]:active { transform: translateY(-1px); }
.kpi[data-drill]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.kpi[data-drill]::after {
  content: '→';
  position: absolute;
  top: 8px; right: 10px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
  opacity: 0.4;
  transition: opacity 0.18s ease, transform 0.18s ease;
}
.kpi[data-drill]:hover::after { opacity: 1; transform: translateX(3px); color: var(--kpi-color, var(--accent)); }

/* ===== Drill banner (en pestaña Registros) ===== */
.drill-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: linear-gradient(135deg, #fef9e7 0%, #fdf3d0 100%);
  border: 1px solid #f1d678;
  border-left: 4px solid var(--accent);
  padding: 0.8rem 1.1rem;
  border-radius: 10px;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  animation: fadeUp 0.3s ease;
}
.drill-banner.hidden { display: none !important; }
.drill-content { display: flex; align-items: center; gap: 0.6rem; flex: 1; min-width: 0; }
.drill-tag {
  background: var(--accent);
  color: var(--primary-dark);
  padding: 0.2rem 0.7rem;
  border-radius: 100px;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  white-space: nowrap;
  text-transform: uppercase;
}
.drill-label { color: var(--primary-dark); font-weight: 600; font-size: 0.92rem; line-height: 1.3; }
.drill-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.btn-small {
  padding: 0.4rem 0.75rem;
  font-size: 0.82rem;
  min-height: 36px;
}
@media (max-width: 600px) {
  .drill-banner { flex-direction: column; align-items: stretch; }
  .drill-content { flex-direction: column; align-items: flex-start; gap: 0.4rem; }
  .drill-actions { width: 100%; }
  .btn-small { flex: 1; }
}

/* ===== CONFIGURACIÓN ===== */
.config-section {
  background: var(--surface);
  padding: 1.25rem 1.4rem;
  border-radius: 12px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(20,40,73,0.06);
  margin-bottom: 1.25rem;
}
.config-section h3 { margin-top: 0; color: var(--primary); font-size: 1.05rem; }
.config-add-row {
  display: flex; gap: 0.5rem; flex-wrap: wrap;
  margin-bottom: 0.85rem; align-items: stretch;
}
.config-add-row input[type="text"],
.config-add-row input[type="number"] {
  flex: 1; min-width: 200px;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.95rem; background: white;
  min-height: 42px;
}
.config-add-row .filter-select { min-width: 180px; }
.config-add-row .btn-primary { padding: 0.55rem 1rem; font-size: 0.9rem; min-height: 42px; }
.config-chips {
  display: flex; gap: 0.5rem; flex-wrap: wrap;
}
.config-chip {
  display: inline-flex; align-items: center; gap: 0.4rem;
  background: var(--bg);
  color: var(--text);
  padding: 0.4rem 0.65rem 0.4rem 0.85rem;
  border-radius: 100px;
  font-size: 0.92rem; font-weight: 600;
  border: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.config-chip button {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0 2px;
  line-height: 1;
  border-radius: 50%;
  width: 20px; height: 20px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}
.config-chip button:hover { background: var(--danger); color: #fff; }
.config-list {
  display: flex; flex-direction: column; gap: 0.5rem;
}
.config-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.7rem 1rem;
}
.config-item-head { display: flex; align-items: center; gap: 0.75rem; }
.config-item-head strong { color: var(--primary); font-size: 0.98rem; }
.config-item-head .btn-mini-del { margin-left: auto; }
.config-item-areas {
  margin-top: 0.45rem;
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.5;
}
.config-area-row {
  display: flex; align-items: center; gap: 0.75rem;
}
.config-area-row strong { color: var(--primary); font-weight: 600; }
.config-area-row .muted { font-size: 0.85rem; }
.config-area-row .btn-mini-del { margin-left: auto; }
.btn-mini-del {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 28px; height: 28px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.btn-mini-del:hover {
  background: var(--danger); color: #fff; border-color: var(--danger);
}

@media (max-width: 640px) {
  .config-add-row { flex-direction: column; align-items: stretch; }
  .config-add-row input[type="text"],
  .config-add-row input[type="number"],
  .config-add-row .filter-select,
  .config-add-row .btn-primary { min-width: 0; width: 100%; }
  .config-area-row { flex-wrap: wrap; }
  .config-area-row .btn-mini-del { margin-left: auto; }
}

/* ===== TOOLBAR ===== */
.toolbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem; margin-bottom: 1rem; }
.toolbar-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.toolbar input[type=search] {
  padding: 0.55rem 0.75rem; border: 1px solid var(--border);
  border-radius: 8px; min-width: 220px; min-height: 42px;
  font-size: 0.95rem; background: white;
}
.toolbar input[type=search]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.filter-select {
  padding: 0.55rem 2rem 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: white; min-height: 42px;
  font-size: 0.9rem; color: var(--text);
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%231e3a5f' d='M2 4l4 4 4-4z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  min-width: 180px;
  cursor: pointer;
}
.filter-select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.filter-select:disabled { background-color: var(--bg); cursor: not-allowed; opacity: 0.7; }

.record-count {
  font-size: 0.85rem;
  white-space: nowrap;
  font-weight: 500;
}

/* ===== FORM TOOLBAR (con navegación) ===== */
.form-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 1rem; margin-bottom: 1rem;
}
.form-toolbar h2 { margin: 0; }
.form-nav { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.nav-controls { display: flex; align-items: center; gap: 0.4rem; }
.btn-nav {
  background: white;
  color: var(--primary);
  border: 1px solid var(--border);
  padding: 0.55rem 0.85rem;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 0.35rem;
  min-height: 42px;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.btn-nav svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-nav:hover:not(:disabled) {
  background: var(--bg);
  border-color: rgba(30,58,95,0.20);
}
.btn-nav:active:not(:disabled) { transform: translateY(1px); }
.btn-nav:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-nav-new {
  background: var(--accent);
  color: var(--primary-dark);
  border-color: var(--accent);
}
.btn-nav-new:hover { background: #d4b270; border-color: #d4b270; }
.nav-pos {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  min-width: 60px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  padding: 0 0.35rem;
}

/* ===== DIRTY WARNING ===== */
.dirty-warning {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  border: 1px solid #fcd34d;
  color: #78350f;
  padding: 0.7rem 1rem;
  border-radius: 10px;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.9rem;
  font-weight: 500;
  animation: pulseWarn 0.3s ease;
}
.dirty-warning svg {
  width: 18px; height: 18px;
  color: #d97706;
  flex-shrink: 0;
}
@keyframes pulseWarn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
#form-title { transition: color 0.15s ease; }

/* ===== TABLE ===== */
.table-wrap { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { padding: 0.65rem 0.75rem; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: #fafbfc; color: var(--primary); font-weight: 600; position: sticky; top: 0; }
tbody tr:hover { background: #f9fafb; }
.tbl-actions { white-space: nowrap; }
.tbl-actions button {
  background: transparent; border: 1px solid var(--border);
  padding: 0.4rem 0.6rem; border-radius: 6px; cursor: pointer;
  margin-right: 0.25rem; font-size: 0.95rem;
  min-width: 36px; min-height: 36px;
}
.tbl-actions button:hover { background: var(--bg); }
.tbl-actions .del { color: var(--danger); border-color: #fecaca; }

/* ===== FORM ===== */
.record-form { background: var(--surface); padding: 1.25rem; border-radius: 10px; box-shadow: var(--shadow); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; }
.form-grid label { display: flex; flex-direction: column; font-size: 0.85rem; font-weight: 600; color: var(--primary); gap: 0.35rem; }
.form-grid input, .form-grid select {
  padding: 0.65rem; border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.95rem; font-weight: normal; color: var(--text);
  min-height: 44px; background: white;
}
.form-grid input:focus, .form-grid select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-actions { margin-top: 1.5rem; display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }

.btn-primary, .btn-secondary, .btn-danger {
  padding: 0.75rem 1.25rem; border-radius: 8px; border: none;
  font-size: 0.95rem; font-weight: 600; cursor: pointer;
  min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 0.3rem;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: white; color: var(--primary); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #b91c1c; }
.file-btn { display: inline-flex; align-items: center; cursor: pointer; }

/* ===== SYNC ===== */
.sync-actions { display: flex; gap: 0.75rem; flex-wrap: wrap; margin: 0.5rem 0 1rem; }
#sync-msg { margin-bottom: 1rem; }
.sync-mode-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem 1rem;
  margin-bottom: 0.5rem;
}
.radio-row {
  display: flex; align-items: flex-start; gap: 0.6rem;
  padding: 0.5rem 0; cursor: pointer;
  font-size: 0.92rem; line-height: 1.4;
}
.radio-row input[type=radio] { margin-top: 3px; flex-shrink: 0; transform: scale(1.15); }

/* ===== USERS MGMT ===== */
#users-table code {
  background: var(--bg); padding: 2px 6px; border-radius: 4px;
  font-family: 'Menlo', 'Consolas', monospace; font-size: 0.85em;
}

/* ============================================================
   RESPONSIVE — mobile-first refinements
   ============================================================ */

/* Tablets pequeñas y phones */
@media (max-width: 900px) {
  .app-header {
    grid-template-columns: 1fr;
    gap: 0.6rem;
    padding: 0.75rem 1rem;
    text-align: center;
  }
  .brand { justify-content: center; }
  .tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    justify-content: flex-start;
    margin: 0 -1rem;
    padding: 0 1rem 0.25rem;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }
  .tabs::-webkit-scrollbar { height: 4px; }
  .tabs::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.3); border-radius: 2px; }
  .user-info { justify-content: center; }
  .chart-grid { grid-template-columns: 1fr; }
}

/* Phones */
@media (max-width: 640px) {
  .app-main { padding: 1rem 0.85rem; }
  h2 { font-size: 1.15rem; }
  .toolbar { gap: 0.6rem; }
  .toolbar h2 { width: 100%; }
  .toolbar input[type=search] { min-width: 0; width: 100%; }
  .toolbar-actions { width: 100%; flex-direction: column; align-items: stretch; }
  .toolbar-actions .filter-select { width: 100%; }
  .toolbar-actions .btn-secondary { width: 100%; }
  .filters select { min-width: 0; width: 100%; }
  .filters label { width: 100%; }

  .form-toolbar { gap: 0.7rem; }
  .form-toolbar h2 { width: 100%; }
  .form-nav { width: 100%; flex-direction: column; align-items: stretch; gap: 0.6rem; }
  .form-nav .filter-select { width: 100%; }
  .nav-controls { width: 100%; justify-content: space-between; }
  .btn-nav { flex: 1; min-width: 0; justify-content: center; padding: 0.55rem 0.5rem; }
  .btn-nav span { display: inline; }
  .btn-nav-new { flex: 1.2; }
  .nav-pos { flex: 0 0 auto; min-width: 70px; }
  .record-count { width: 100%; text-align: center; padding: 0.2rem 0; }

  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 0.7rem; }
  .kpi { padding: 0.85rem 0.9rem; gap: 0.7rem; }
  .kpi-icon { width: 36px; height: 36px; border-radius: 9px; }
  .kpi-icon svg { width: 18px; height: 18px; }
  .kpi-value { font-size: 1.45rem; }
  .kpi-label { font-size: 0.68rem; }
  .kpi-trend { font-size: 0.66rem; }

  .chart-card { padding: 1rem 0.95rem 0.85rem; }
  .chart-head h3 { font-size: 0.92rem; }
  .chart-hint { display: none; }
  .chart-box { height: 240px; }
  .chart-card.wide .chart-box { height: 260px; }
  .donut-num { font-size: 1.5rem; }

  /* Tablas → tarjetas apiladas (cada fila se vuelve un bloque) */
  .table-wrap { background: transparent; box-shadow: none; overflow: visible; }
  table { font-size: 0.85rem; }
  thead { display: none; }
  tbody, tr, td { display: block; width: 100%; }
  tr {
    background: var(--surface);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 0.65rem 0.9rem;
    margin-bottom: 0.65rem;
    border: 1px solid var(--border);
  }
  td {
    border: none;
    padding: 0.35rem 0;
    white-space: normal;
    display: flex; justify-content: space-between;
    align-items: flex-start; gap: 1rem;
    font-size: 0.88rem;
  }
  td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    min-width: 100px;
    flex-shrink: 0;
  }
  td:empty, td:not([data-label]) { display: none; }
  td.tbl-actions { justify-content: flex-end; padding-top: 0.5rem; border-top: 1px solid var(--border); margin-top: 0.35rem; }
  td.tbl-actions::before { display: none; }
  tbody tr:hover { background: var(--surface); }

  .form-grid { grid-template-columns: 1fr; gap: 0.85rem; }
  .form-actions { flex-direction: column; align-items: stretch; }
  .form-actions .btn-primary,
  .form-actions .btn-secondary,
  .form-actions .btn-danger { width: 100%; }

  .sync-actions { flex-direction: column; align-items: stretch; }
  .sync-actions .btn-primary,
  .sync-actions .btn-secondary,
  .sync-actions .btn-danger,
  .sync-actions .file-btn { width: 100%; }

  .estaca-banner { font-size: 0.88rem; padding: 0.75rem 1rem; }
  .login-box { padding: 1.4rem 1.15rem 1.3rem; }
  .logo-img { width: 78px; height: 78px; }
  .logo h1 { font-size: 1.2rem; }
}

/* Phones muy pequeñas */
@media (max-width: 400px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .tab-label { display: none; }
  .tab-btn { font-size: 1.1rem; padding: 0.5rem 0.7rem; }
  .brand strong { font-size: 0.85rem; }
  .user-info { gap: 0.5rem; flex-direction: column; }
  #user-label { font-size: 0.8rem; max-width: 100%; }
  .btn-nav span { display: none; }
  .btn-nav { padding: 0.55rem 0.7rem; }
  .btn-nav-new { font-size: 0.78rem; }
}

/* ============================================================
   MEJORAS DE RESPONSIVIDAD MÓVIL (refinamientos finales)
   ============================================================ */

/* Tablet y móvil — toolbar de Registros con 3 filtros + búsqueda */
@media (max-width: 900px) {
  .toolbar { gap: 0.65rem; }
  .toolbar-actions { gap: 0.45rem; }
  .filter-select { min-width: 0; flex: 1 1 160px; }
  .toolbar input[type=search] { flex: 2 1 220px; min-width: 180px; }
  .record-count { flex: 0 0 auto; }
  /* Botones de acción del toolbar al final con ancho completo si no caben */
  .toolbar-actions .btn-secondary { flex: 0 0 auto; }
}

/* Móviles (<=640px): stacking total + simplificación visual */
@media (max-width: 640px) {
  /* Body type tweaks */
  html, body { font-size: 15px; }

  /* App main: menos padding lateral, scroll suave */
  .app-main { padding: 0.85rem 0.7rem 1.5rem; }
  h2 { font-size: 1.1rem; }
  h3 { font-size: 0.95rem; }

  /* Header: el brand-text con texto largo se trunca elegantemente */
  .app-header { padding: 0.7rem 0.85rem; }
  .brand-text strong { font-size: 0.88rem; }
  .brand-text small { font-size: 0.66rem; }

  /* KPIs: ajustar tamaño de íconos y arrow drill */
  .kpi { padding: 0.8rem 0.85rem; gap: 0.55rem; }
  .kpi-icon { width: 34px; height: 34px; border-radius: 8px; }
  .kpi-icon svg { width: 17px; height: 17px; }
  .kpi-value { font-size: 1.4rem; }
  .kpi-label { font-size: 0.65rem; }
  .kpi[data-drill]::after { top: 6px; right: 8px; font-size: 0.7rem; }

  /* Toolbars: stacking total con ancho consistente */
  .toolbar-actions { flex-direction: column; align-items: stretch; }
  .toolbar-actions .filter-select,
  .toolbar-actions input[type=search],
  .toolbar-actions .btn-secondary { width: 100%; flex: none; }
  .toolbar-actions .record-count { width: 100%; text-align: center; padding: 0.25rem 0; }

  /* Drill banner: stack vertical pulcro */
  .drill-banner {
    padding: 0.7rem 0.9rem;
    flex-direction: column; align-items: stretch;
    gap: 0.6rem;
  }
  .drill-content { flex-direction: column; align-items: flex-start; gap: 0.4rem; width: 100%; }
  .drill-actions { width: 100%; display: flex; gap: 0.4rem; }
  .drill-actions .btn-small { flex: 1; min-width: 0; }

  /* Form-toolbar y form-nav: stacking simétrico */
  .form-toolbar { flex-direction: column; align-items: stretch; gap: 0.6rem; }
  .form-toolbar h2 { font-size: 1.05rem; }
  .form-nav { width: 100%; flex-direction: column; gap: 0.5rem; }
  .form-nav .filter-select { width: 100%; min-width: 0; }
  .nav-controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 0.4rem; width: 100%; }
  .nav-controls .btn-nav { width: 100%; padding: 0.55rem 0.5rem; min-width: 0; justify-content: center; }
  .nav-controls .btn-nav-new { grid-column: 1 / -1; width: 100%; margin-top: 0.2rem; }
  .nav-pos { min-width: 0; padding: 0 0.3rem; }

  /* Configuración: padding interno más chico */
  .config-section { padding: 1rem 0.95rem; }
  .config-section h3 { font-size: 0.95rem; }
  .config-add-row { flex-direction: column; align-items: stretch; }
  .config-add-row input,
  .config-add-row .filter-select,
  .config-add-row .btn-primary { width: 100%; min-width: 0; flex: none; }
  .config-item { padding: 0.65rem 0.85rem; }
  .config-item-head { flex-wrap: wrap; gap: 0.4rem; }
  .config-item-head .btn-mini-del { margin-left: auto; }
  .config-area-row { flex-wrap: wrap; }

  /* Sync-mode: radios con texto compacto */
  .sync-mode-group { padding: 0.6rem 0.8rem; }
  .radio-row { font-size: 0.86rem; padding: 0.35rem 0; }
  .radio-row input[type=radio] { transform: scale(1.05); }

  /* Estaca banner: legible en pantallas pequeñas */
  .estaca-banner { font-size: 0.82rem; padding: 0.7rem 0.9rem; line-height: 1.5; }
  .estaca-banner strong { font-size: 1em; }
  .estaca-tag { font-size: 0.62rem; padding: 0.15rem 0.55rem; }

  /* Tarjetas (form, user-form): padding más eficiente */
  .record-form { padding: 1rem 0.9rem; }

  /* Login: padding más compacto */
  .login-box { padding: 1.3rem 1rem 1.2rem; max-width: 360px; }
  .logo h1 { font-size: 1.15rem; line-height: 1.2; }

  /* Tabs móviles: hacer hint visible */
  .tabs { gap: 0.1rem; }
  .tab-btn { padding: 0.5rem 0.7rem; font-size: 0.82rem; }
}

/* Phones muy pequeñas (<=380px) — apretar más */
@media (max-width: 380px) {
  .app-main { padding: 0.75rem 0.55rem 1.25rem; }
  .kpi-grid { gap: 0.55rem; }
  .kpi { padding: 0.7rem 0.75rem; gap: 0.5rem; }
  .kpi-value { font-size: 1.3rem; }
  .estaca-banner { font-size: 0.78rem; }
  .drill-tag { font-size: 0.6rem; padding: 0.15rem 0.5rem; }
  .drill-label { font-size: 0.84rem; }
}

/* Print */
@media print {
  .app-header, .toolbar-actions, .filters, .tbl-actions, .form-actions, .sync-actions, .estaca-back-nav, .drill-banner, .drill-actions { display: none !important; }
  .app-main { padding: 0; }
  .chart-card, .kpi { break-inside: avoid; }
}
