/* Tokens de PIAF_Web (Tailwind slate + primario índigo), solo tema claro */
:root {
  --primary: oklch(0.518 0.143 272.314);
  --primary-hover: oklch(0.457 0.139 272.314);
  --primary-soft: oklch(0.518 0.143 272.314 / 0.2);
  --indigo-400: #818cf8;
  --slate-50: #f8fafc;
  --slate-100: #f1f5f9;
  --slate-200: #e2e8f0;
  --slate-300: #cbd5e1;
  --slate-400: #94a3b8;
  --slate-500: #64748b;
  --slate-600: #475569;
  --slate-700: #334155;
  --slate-800: #1e293b;
  --slate-900: #0f172a;
  --green-100: #dcfce7;
  --green-700: #15803d;
  --red-100: #fee2e2;
  --red-700: #b91c1c;
  --amber-100: #fef3c7;
  --amber-700: #b45309;
  --radius: 12px;
  --radius-lg: 16px;
  --radius-pill: 999px;
  --sidebar-w: 256px;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.04), 0 4px 16px rgb(15 23 42 / 0.06);
  --shadow-lg: 0 8px 32px rgb(15 23 42 / 0.12), 0 2px 8px rgb(15 23 42 / 0.06);
  --borde: rgb(15 23 42 / 0.08);
  --primary-ring: oklch(0.518 0.143 272.314 / 0.35);
  --primary-tenue: oklch(0.518 0.143 272.314 / 0.1);
  --slate-50-alfa: rgb(248 250 252 / 0.72);
  --blanco-alfa: rgb(255 255 255 / 0.72);
  --blur: saturate(180%) blur(20px);
  --fuente: -apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif;
  --tactil: 44px;
  --dur: 0.2s;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  font-family: var(--fuente);
  font-size: 15px;
  line-height: 1.4;
  color: var(--slate-900);
  background: var(--slate-100);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { letter-spacing: -0.02em; }

:focus-visible { outline: 3px solid var(--primary-ring); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

a { color: var(--primary); }

.material-symbols-rounded { font-size: 22px; line-height: 1; vertical-align: middle; }

/* Layout */
.app { min-height: 100vh; }

.sidebar { display: none; }

.marca { display: flex; align-items: center; gap: 10px; padding: 8px 12px 20px; font-weight: 700; font-size: 17px; letter-spacing: -0.02em; }
.marca .material-symbols-rounded { color: var(--primary); font-size: 28px; }

.sidebar nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 8px 12px;
  border-radius: var(--radius);
  color: var(--slate-700);
  text-decoration: none;
  font-weight: 500;
  transition: background var(--dur) ease-out, color var(--dur) ease-out;
}
.sidebar nav a .material-symbols-rounded { color: var(--primary); font-size: 20px; }
.sidebar nav a:hover { background: var(--primary-tenue); }
.sidebar nav a.activo { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sidebar nav a.activo .material-symbols-rounded { font-variation-settings: 'FILL' 1; }

.header {
  position: sticky;
  top: 0;
  z-index: 30;
  padding: 20px 24px 12px;
  background: var(--slate-50-alfa);
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
  border-bottom: 1px solid var(--borde);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.header h1 { margin: 0; font-size: 28px; font-weight: 700; line-height: 1.15; }
.header .fecha { margin: 2px 0 0; font-size: 13px; color: var(--slate-500); }
.header .fecha::first-letter { text-transform: uppercase; }

.contenido { padding: 24px; display: flex; flex-direction: column; gap: 24px; }

/* Tab bar tipo iOS (móvil y tablet) */
.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 50;
  display: flex;
  padding: 4px 4px env(safe-area-inset-bottom);
  background: var(--blanco-alfa);
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
  border-top: 1px solid var(--borde);
}
.tabbar .tab {
  flex: 1;
  min-width: 0;
  min-height: 50px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: var(--radius);
  color: var(--slate-500);
  text-decoration: none;
  font-size: 10px;
  font-weight: 500;
  transition: color var(--dur) ease-out;
}
.tabbar .tab span:last-child { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabbar .tab .material-symbols-rounded { font-size: 24px; }
.tabbar .tab.activo { color: var(--primary); }
.tabbar .tab.activo .material-symbols-rounded { font-variation-settings: 'FILL' 1; }

/* espacio para que la tab bar no tape el final */
.principal { padding-bottom: calc(58px + env(safe-area-inset-bottom)); }

@media (min-width: 1024px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: var(--sidebar-w);
    display: flex;
    flex-direction: column;
    padding: 20px 12px;
    background: var(--slate-50-alfa);
    -webkit-backdrop-filter: var(--blur);
    backdrop-filter: var(--blur);
    border-right: 1px solid var(--borde);
  }
  .sidebar nav { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
  .tabbar { display: none; }
  .principal { margin-left: var(--sidebar-w); padding-bottom: 0; }
}

@media (max-width: 640px) {
  .header { padding: 16px 16px 10px; }
  .header h1 { font-size: 26px; }
  .contenido { padding: 16px; gap: 16px; }
}

/* Componentes */
.card {
  background: #fff;
  border: 1px solid var(--borde);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card h2 { margin: 0 0 16px; font-size: 19px; font-weight: 600; line-height: 1.25; }

.vacio { text-align: center; color: var(--slate-500); padding: 48px 20px; }
.vacio .material-symbols-rounded { font-size: 48px; color: var(--slate-300); display: block; margin-bottom: 8px; }
/* carga: contenedor aún vacío muestra texto atenuado; al recargar, los datos viejos se atenúan (aria-busy lo ponen las páginas) */
.cargando, .tabla-wrap:empty::before, #totales:empty::before { content: "Cargando…"; display: block; text-align: center; color: var(--slate-400); padding: 48px 20px; }
main[aria-busy="true"] :is(.tabla-wrap, .grafica, #totales) { opacity: 0.5; transition: opacity var(--dur) ease-out; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tactil);
  padding: 10px 20px;
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  text-decoration: none;
  transition: background var(--dur) ease-out, transform var(--dur) ease-out;
}
.btn:hover { background: var(--primary-hover); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-sec { background: var(--primary-tenue); color: var(--primary); }
.btn-sec:hover { background: var(--primary-soft); }
.btn-icono { min-width: var(--tactil); min-height: var(--tactil); background: none; border: 0; padding: 8px; color: var(--slate-600); cursor: pointer; border-radius: var(--radius-pill); transition: background var(--dur) ease-out; }
.btn-icono:hover { color: var(--slate-900); background: var(--slate-100); }

.input, select.input {
  min-height: var(--tactil);
  padding: 10px 14px;
  border: 1px solid var(--slate-200);
  border-radius: var(--radius);
  font: inherit;
  background: #fff;
  color: var(--slate-900);
  min-width: 0;
  transition: border-color var(--dur) ease-out, box-shadow var(--dur) ease-out;
}
.input:focus, .input:focus-visible { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-ring); }

.campo { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 500; color: var(--slate-600); }

.filtros { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.filtros .campo { flex: 0 1 180px; }
.filtros .input { width: 100%; }
@media (max-width: 640px) {
  .filtros .campo { flex: 1 1 calc(50% - 6px); }
  .filtros .btn { flex: 1 1 100%; }
}

.tabla-wrap { overflow-x: auto; }
.tabla { width: 100%; border-collapse: collapse; }
.tabla th, .tabla td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--slate-200); white-space: nowrap; }
.tabla th { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; color: var(--slate-500); background: var(--slate-50); }
.tabla tbody tr:hover { background: var(--slate-50); }
.tabla .num { text-align: right; font-variant-numeric: tabular-nums; }
.tabla td { border-bottom-color: var(--slate-100); }
@media (min-width: 641px) { .tabla tbody tr:last-child td { border-bottom: 0; } }
/* tablas anchas (Cortes, Retiros): caben en 1280 con sidebar sin scroll horizontal */
@media (min-width: 641px) {
  .tabla-densa th, .tabla-densa td { padding: 10px 8px; }
  .tabla-densa th { white-space: normal; }
  .tabla-densa .envuelve { white-space: normal; overflow-wrap: anywhere; min-width: 110px; }
  .tabla-densa select.input { max-width: 160px; }
  .tabla-densa td small { max-width: 140px; }
}
/* móvil: cada fila es tarjeta tipo lista iOS, "etiqueta · valor" (data-label lo pone layout.js) */
@media (max-width: 640px) {
  .tabla thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .tabla, .tabla tbody, .tabla tr, .tabla td { display: block; }
  .tabla tr { margin-bottom: 12px; border: 1px solid var(--slate-200); border-radius: var(--radius-lg); background: #fff; overflow: hidden; }
  .tabla td { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 14px; text-align: right; white-space: normal; overflow-wrap: anywhere; border-bottom: 1px solid var(--slate-100); }
  .tabla td:last-child { border-bottom: 0; }
  .tabla td:empty { display: none; }
  .tabla td[data-label]::before { content: attr(data-label); flex-shrink: 0; max-width: 50%; text-align: left; font-size: 13px; color: var(--slate-500); }
  /* controles (Retiros, Conceptos) a todo el ancho, etiqueta arriba */
  .tabla td:has(select, input:not([type="checkbox"])) { flex-direction: column; align-items: stretch; gap: 6px; text-align: left; }
  .tabla td:has(select, input:not([type="checkbox"]))::before { max-width: none; }
  .tabla td:has(.btn) .btn { flex: 1; justify-content: center; }
  .tabla td:not([data-label]) { justify-content: flex-end; }
  .tabla small { max-width: 100%; }
}

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: var(--radius-pill); font-size: 12px; font-weight: 600; }
.badge-ok { background: var(--green-100); color: var(--green-700); }
.badge-error { background: var(--red-100); color: var(--red-700); }
.badge-pendiente { background: var(--amber-100); color: var(--amber-700); }

.modal-fondo { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgb(15 23 42 / 0.4); animation: aparece var(--dur) ease-out; }
.modal { background: #fff; border-radius: var(--radius-lg); width: min(900px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; overscroll-behavior: contain; padding: 24px; box-shadow: var(--shadow-lg); animation: crece 0.25s ease-out; }
@keyframes aparece { from { opacity: 0; } }
@keyframes crece { from { opacity: 0; transform: scale(0.96); } }
/* hoja inferior tipo iOS con asa */
@media (max-width: 640px) {
  .modal-fondo { align-items: flex-end; padding: 0; }
  .modal { width: 100%; max-height: 92vh; border-radius: var(--radius-lg) var(--radius-lg) 0 0; padding: 8px 16px calc(24px + env(safe-area-inset-bottom)); animation: sube 0.25s ease-out; }
  .modal::before { content: ""; display: block; width: 36px; height: 5px; margin: 0 auto 12px; border-radius: var(--radius-pill); background: var(--slate-300); }
}
@keyframes sube { from { transform: translateY(100%); } }

/* móvil/tablet: arriba al centro (la tab bar ocupa abajo); escritorio: abajo a la derecha */
.toasts { position: fixed; top: calc(12px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 70; display: flex; flex-direction: column; align-items: center; gap: 8px; width: max-content; max-width: calc(100vw - 32px); pointer-events: none; }
.toast { display: flex; align-items: center; gap: 8px; padding: 10px 18px 10px 14px; border-radius: var(--radius-pill); background: var(--slate-900); color: #fff; font-weight: 500; box-shadow: var(--shadow-lg); animation: baja var(--dur) ease-out; pointer-events: auto; }
.toast-ok { background: var(--green-700); }
.toast-error { background: var(--red-700); }
@keyframes baja { from { opacity: 0; transform: translateY(-8px); } }
@keyframes entra { from { opacity: 0; transform: translateY(8px); } }
@media (min-width: 1024px) {
  .toasts { top: auto; left: auto; right: 24px; bottom: 24px; transform: none; align-items: flex-end; }
  .toast { animation-name: entra; }
}

.estado-api { display: flex; align-items: center; gap: 12px; }
.punto { width: 12px; height: 12px; border-radius: 50%; background: var(--slate-300); flex-shrink: 0; }
.punto-ok { background: #22c55e; }
.punto-error { background: #ef4444; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
.kpi { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; }
.kpi > .material-symbols-rounded { display: grid; place-items: center; width: 40px; height: 40px; font-size: 22px; color: var(--primary); background: var(--primary-tenue); border-radius: 50%; }
.kpi-label { margin: 0; font-size: 13px; font-weight: 500; color: var(--slate-500); }
.kpi-valor { margin: 2px 0 0; letter-spacing: -0.02em; font-size: 24px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
@media (max-width: 640px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .kpi { padding: 16px; }
  .kpi-valor { font-size: 22px; }
}

.grafica { position: relative; height: 300px; }
.grafica + .tabla-wrap { margin-top: 16px; }

.aviso { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-radius: var(--radius-lg); font-weight: 500; text-decoration: none; }
.aviso[hidden] { display: none; }
.aviso-error { background: var(--red-100); color: var(--red-700); }
.aviso-error:hover { text-decoration: underline; }
.aviso-ok { background: var(--green-100); color: var(--green-700); margin-bottom: 16px; }

.card-cab { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.card-cab h2 { margin: 0; align-self: center; }
.card-cab .filtros { flex: 1 1 320px; justify-content: flex-end; }
.card-cab .filtros .campo { flex: 0 1 220px; }
@media (max-width: 640px) { .card-cab .filtros .campo { flex: 1 1 calc(50% - 6px); } }
.modal-fondo[hidden] { display: none; }
.modal-cab { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.modal-cab h2 { margin: 0; font-size: 20px; font-weight: 600; }
.modal-cab p { margin: 4px 0 0; color: var(--slate-500); font-size: 13px; }
.modal h3 { margin: 24px 0 8px; font-size: 15px; font-weight: 600; }
.acciones { display: flex; flex-wrap: wrap; gap: 8px; }
.datos { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px 24px; margin: 0; }
.datos dt { font-size: 12px; color: var(--slate-500); }
.datos dd { margin: 2px 0 0; font-weight: 500; font-variant-numeric: tabular-nums; }
.tabla small { display: inline-block; max-width: 220px; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; color: var(--red-700); }

/* Login: tarjeta centrada, sin sidebar ni tab bar */
body.login main { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 16px; }
.login-card .marca { padding: 0 0 4px; justify-content: center; }
