:root {
  --azul: #0f3460;
  --azul-claro: #3884ff;
  --bg: #f4f6fb;
  --card: #ffffff;
  --texto: #1c2333;
  --muted: #6b7280;
  --borde: #e3e8f0;
  --verde: #1f9d55;
  --rojo: #d64545;
  --rojo-bg: #fdecec;
  --radius: 14px;
  --sombra: 0 2px 10px rgba(15, 52, 96, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--texto);
  -webkit-tap-highlight-color: transparent;
}

.muted { color: var(--muted); }
.small { font-size: .82rem; }
.center { text-align: center; }
.error { color: var(--rojo); background: var(--rojo-bg); padding: .6rem .8rem; border-radius: 10px; font-size: .9rem; }

/* ---------- Login ---------- */
.vista-login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.2rem;
  background: linear-gradient(160deg, var(--azul), #08203f);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border-radius: 18px;
  padding: 1.6rem;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.brand { text-align: center; margin-bottom: 1.2rem; }
.brand-badge {
  width: 64px; height: 64px; margin: 0 auto .6rem;
  background: var(--azul); color: #fff; font-weight: 800; letter-spacing: 1px;
  display: grid; place-items: center; border-radius: 16px; font-size: 1.1rem;
}
.brand h1 { margin: .2rem 0 0; font-size: 1.3rem; }

form { display: flex; flex-direction: column; gap: .35rem; }
label { font-size: .85rem; font-weight: 600; margin-top: .5rem; }
input {
  font-size: 1rem; padding: .7rem .8rem; border: 1px solid var(--borde);
  border-radius: 10px; outline: none; background: #fff; color: var(--texto);
}
input:focus { border-color: var(--azul-claro); box-shadow: 0 0 0 3px rgba(56,132,255,.15); }

.btn-primary {
  margin-top: 1rem; background: var(--azul); color: #fff; border: 0;
  padding: .8rem; border-radius: 10px; font-size: 1rem; font-weight: 600; cursor: pointer;
}
.btn-primary:active { transform: translateY(1px); }
.btn-ghost { background: transparent; border: 1px solid rgba(255,255,255,.4); color: #fff; padding: .4rem .8rem; border-radius: 8px; cursor: pointer; }
.topbar-acciones { display: flex; align-items: center; gap: .5rem; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; padding: .4rem; }
.icon-btn svg { width: 18px; height: 18px; }

/* Modal Acerca de */
.modal { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 1.2rem; background: rgba(8,16,32,.55); }
.modal[hidden] { display: none; }
.modal-card { background: #fff; border-radius: 18px; padding: 1.6rem; max-width: 340px; width: 100%; text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal-card .brand-badge { margin: 0 auto .6rem; }
.modal-card h2 { margin: .2rem 0 0; }
.acerca-dev { margin: 1rem 0 .4rem; font-size: 1rem; }
.acerca-dev b { color: var(--azul); font-size: 1.1rem; }
.modal-card .btn-primary { margin-top: 1.2rem; }

/* ---------- App ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--azul); color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  padding: .7rem 1rem; padding-top: max(.7rem, env(safe-area-inset-top));
}
.topbar-info { display: flex; flex-direction: column; gap: .15rem; line-height: 1.2; }
.topbar-info .muted { color: rgba(255,255,255,.7); font-size: .78rem; }
.chip {
  align-self: flex-start; background: rgba(255,255,255,.18);
  padding: .05rem .5rem; border-radius: 999px; font-size: .72rem;
}

.buscador {
  position: sticky; top: 56px; z-index: 4;
  display: flex; gap: .5rem; padding: .7rem 1rem; background: var(--bg);
  border-bottom: 1px solid var(--borde);
}
.buscador-campo { position: relative; flex: 1; }
.buscador-campo input { width: 100%; text-transform: uppercase; }
.buscador .btn-primary { margin: 0; padding: .7rem 1rem; }

/* Desplegable de sugerencias (autocompletado) */
.sugerencias {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  list-style: none; margin: 0; padding: .25rem; max-height: 60vh; overflow-y: auto;
  background: #fff; border: 1px solid var(--borde); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(15,52,96,.18);
}
.sugerencia {
  display: flex; align-items: center; gap: .6rem; padding: .5rem; border-radius: 10px; cursor: pointer;
}
.sugerencia:hover, .sugerencia.activa { background: #eef3fb; }
.sug-img {
  width: 42px; height: 42px; flex: none; object-fit: contain;
  background: #f0f3f9; border: 1px solid var(--borde); border-radius: 8px;
}
.sug-info { min-width: 0; }
.sug-alterno { font-weight: 700; font-size: .9rem; text-transform: uppercase; }
.sug-nombre { font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sug-vacio { padding: .6rem .8rem; color: var(--muted); font-size: .85rem; }

.resultado { padding: 1rem; padding-bottom: 5rem; max-width: 720px; margin: 0 auto; }

/* Menú inferior por módulos */
.navbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; background: #fff; border-top: 1px solid var(--borde);
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -2px 10px rgba(15,52,96,.06);
}
.navbar[hidden] { display: none; }
.creditos { text-align: center; font-size: .72rem; color: var(--muted); margin: 1rem 0 0; }
.creditos b { color: var(--azul); }
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: .5rem; background: none; border: 0; color: var(--muted);
  font-size: .7rem; cursor: pointer;
}
.nav-btn.activo { color: var(--azul); }
.nav-btn svg { width: 22px; height: 22px; }

.card {
  background: var(--card); border: 1px solid var(--borde);
  border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; box-shadow: var(--sombra);
}
.card h3 { margin: 0 0 .6rem; font-size: 1rem; }
.card-title-row { display: flex; align-items: center; justify-content: space-between; gap: .5rem; flex-wrap: wrap; }
.toggle { font-size: .78rem; color: var(--muted); display: flex; align-items: center; gap: .3rem; }

/* Detalle */
.detalle-top { display: flex; gap: 1rem; align-items: flex-start; }
.prod-img {
  width: 96px; height: 96px; object-fit: contain; border-radius: 12px;
  background: #f0f3f9; border: 1px solid var(--borde); flex: none;
}
.detalle-head h2 { margin: 0 0 .2rem; font-size: 1.15rem; }
.detalle-head .sku { margin: .3rem 0 0; font-size: .85rem; }
.saldo { margin: .4rem 0 0; color: var(--rojo); font-weight: 700; font-size: .85rem; }

.precios { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1rem 0 .4rem; }
.precio-box { flex: 1 1 30%; background: #f0f4fb; border-radius: 10px; padding: .5rem .6rem; min-width: 90px; }
.precio-box .lbl { font-size: .72rem; color: var(--muted); display: block; }
.precio-box .val { font-size: 1.05rem; font-weight: 700; }
.precio-box.desc .val { color: var(--verde); }

.datos-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem .8rem; margin-top: .6rem; }
.dato { font-size: .85rem; }
.dato .lbl { color: var(--muted); display: block; font-size: .72rem; }
.dato .val { font-weight: 600; }

.costos { margin-top: .8rem; padding-top: .8rem; border-top: 1px dashed var(--borde); }

/* Tabla existencias con columnas congeladas (Barra, Talla, Global) */
.tabla-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tabla { border-collapse: separate; border-spacing: 0; width: 100%; font-size: .82rem; }
.tabla th, .tabla td {
  padding: .4rem .45rem; text-align: center; white-space: nowrap;
  border-bottom: 1px solid var(--borde); border-right: 1px solid var(--borde);
  background: #fff;
}
.tabla thead th { background: var(--azul); color: #fff; position: sticky; top: 0; z-index: 2; }
.tabla td.cero { color: #c2c8d2; }
.tabla td.tiene { font-weight: 700; color: var(--azul); }
.tabla tr.totales td { background: #e9f7ef; font-weight: 800; border-top: 2px solid var(--verde); }

/* Columnas congeladas (anchos fijos para que los left calcen exacto) */
.tabla .c-barra  { position: sticky; left: 0;     width: 92px;  min-width: 92px;  max-width: 92px; text-align: left; font-size: .72rem; }
.tabla .c-talla  { position: sticky; left: 92px;  width: 50px;  min-width: 50px;  max-width: 50px; font-weight: 700; }
.tabla .c-global { position: sticky; left: 142px; width: 54px;  min-width: 54px;  max-width: 54px; font-weight: 700; }

/* Fondos opacos para que las tiendas pasen por debajo al hacer scroll */
.tabla tbody .c-barra, .tabla tbody .c-talla { background: #eef2f9; z-index: 1; }
.tabla tbody .c-global { background: #f6f8fc; z-index: 1; }
.tabla tr.totales .c-barra, .tabla tr.totales .c-talla, .tabla tr.totales .c-global { background: #e0f3e8; z-index: 1; }
/* Esquinas (encabezado + congelada) por encima de todo */
.tabla thead .c-barra, .tabla thead .c-talla, .tabla thead .c-global { z-index: 3; background: var(--azul); }
.tabla .c-global { box-shadow: 2px 0 4px rgba(0,0,0,.06); }

/* Ranking prioridades */
.ranking { list-style: none; margin: 0; padding: 0; }
.ranking li { display: flex; justify-content: space-between; padding: .45rem .2rem; border-bottom: 1px solid var(--borde); font-size: .9rem; }
.ranking li:last-child { border-bottom: 0; }
.ranking .tienda { font-weight: 600; }
.ranking .ventas { color: var(--azul); font-weight: 700; }
.prioridades { display: grid; gap: 1rem; }
@media (min-width: 640px) { .prioridades { grid-template-columns: 1fr 1fr; } }

/* Módulo Seguridad / permisos */
.lbl-rol { display: block; font-size: .8rem; font-weight: 600; margin: .6rem 0 .25rem; }
.select-rol { width: 100%; font-size: 1rem; padding: .6rem .7rem; border: 1px solid var(--borde); border-radius: 10px; background: #fff; }
.perm-lista { margin-top: .8rem; display: flex; flex-direction: column; gap: .9rem; }
.perm-grupo h4 { margin: 0 0 .4rem; font-size: .82rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.perm-item { display: flex; gap: .6rem; align-items: flex-start; padding: .5rem; border: 1px solid var(--borde); border-radius: 10px; margin-bottom: .4rem; cursor: pointer; }
.perm-item input { width: 18px; height: 18px; margin-top: .15rem; flex: none; }
.perm-txt { display: flex; flex-direction: column; }
.perm-txt b { font-size: .85rem; }
.perm-acciones { display: flex; align-items: center; gap: .8rem; margin-top: 1rem; }
.perm-acciones .btn-primary { margin: 0; }
.perm-acciones .btn-primary:disabled { opacity: .5; }

/* Dashboard de ventas (KPIs) */
.kpi-fechas { display: flex; flex-wrap: wrap; gap: .6rem; align-items: end; margin-bottom: 1rem; }
.kpi-fechas label { display: flex; flex-direction: column; font-size: .75rem; font-weight: 600; color: var(--muted); gap: .2rem; }
.kpi-fechas input { font-size: .95rem; padding: .5rem; border: 1px solid var(--borde); border-radius: 10px; }
.kpi-fechas .btn-primary { margin: 0; padding: .55rem 1.1rem; }
.kpi-cards { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin-bottom: 1rem; }
.kpi-card { position: relative; background: var(--card); border: 1px solid var(--borde); border-radius: var(--radius); padding: .8rem; box-shadow: var(--sombra); }
.kpi-card .lbl { display: block; font-size: .72rem; color: var(--muted); }
.kpi-card .val { font-size: 1.2rem; font-weight: 800; color: var(--azul); }

/* Badges de variación / estado */
.badge { display: inline-flex; align-items: center; gap: .2rem; font-size: .72rem; font-weight: 700; padding: .12rem .45rem; border-radius: 999px; white-space: nowrap; }
.badge-up   { background: #e7f7ee; color: #157347; }
.badge-down { background: #fdecec; color: #c0392b; }
.badge-warn { background: #fff5e6; color: #b8730a; }
.kpi-card .badge { position: absolute; top: .7rem; right: .7rem; }

.meta-head { display: flex; align-items: flex-end; justify-content: space-between; gap: .5rem; }
.meta-real { display: block; font-size: 1.5rem; font-weight: 800; color: var(--azul); }
.meta-meta { text-align: right; }
.meta-meta b { font-size: 1.05rem; }
.meta-bar { position: relative; height: 14px; background: #eef2f9; border-radius: 999px; overflow: hidden; margin: .6rem 0 .3rem; }
.meta-bar-fill { height: 100%; background: linear-gradient(90deg, var(--azul-claro), var(--verde)); border-radius: 999px; }
.meta-bar-ideal { position: absolute; top: -2px; width: 2px; height: 18px; background: #1c2333; opacity: .55; }
.meta-pct { font-size: .8rem; color: var(--muted); margin-bottom: .7rem; }

.badge-row { display: flex; flex-wrap: wrap; gap: .6rem; margin-bottom: .8rem; }
.badge-item { display: flex; flex-direction: column; gap: .2rem; }
.badge-lbl { font-size: .68rem; color: var(--muted); }

/* Cumplimiento por tienda */
.tiendas-cumpl { display: flex; flex-direction: column; gap: .9rem; margin-top: .6rem; }
.cumpl-row { border-bottom: 1px solid var(--borde); padding-bottom: .7rem; }
.cumpl-row:last-child { border-bottom: 0; }
.cumpl-top { display: flex; justify-content: space-between; align-items: center; }
.cumpl-nom { font-weight: 700; font-size: .92rem; }
.cumpl-sub { margin-top: .3rem; }
.txt-up { color: #157347; font-weight: 700; }
.txt-down { color: #c0392b; font-weight: 700; }

/* Loader */
.loader { position: fixed; inset: 0; background: rgba(255,255,255,.6); display: grid; place-items: center; z-index: 50; }
.spinner { width: 42px; height: 42px; border: 4px solid var(--borde); border-top-color: var(--azul); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.vista[hidden], [hidden] { display: none !important; }
