:root {
  --fondo: #f5f1ea;
  --tarjeta: #ffffff;
  --tinta: #1b2231;
  --suave: #5a6375;
  --borde: #e3dcd0;
  --primario: #2f4ba8;
  --sobre-primario: #ffffff;
  --primario-suave: #e6ebfa;
  --bien: #17734b;
  --bien-suave: #def2e8;
  --alerta: #96560a;
  --alerta-suave: #fbeed6;
  --mal: #b23527;
  --mal-suave: #fbe4e0;
  --radio: 16px;
  --sombra: 0 1px 2px rgb(27 34 49 / 0.06), 0 4px 14px rgb(27 34 49 / 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fondo: #11131a;
    --tarjeta: #1b1f2b;
    --tinta: #eceef4;
    --suave: #a3aabb;
    --borde: #2c3242;
    --primario: #92a6f4;
    --sobre-primario: #0d1226;
    --primario-suave: #242c4a;
    --bien: #62cf9c;
    --bien-suave: #173327;
    --alerta: #e8b665;
    --alerta-suave: #3a2e17;
    --mal: #f29385;
    --mal-suave: #3d1f1b;
    --sombra: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0 auto;
  max-width: 560px;
  min-height: 100dvh;
  background: var(--fondo);
  color: var(--tinta);
  font: 16px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

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

/* ---------- Barra superior ---------- */

.barra {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--fondo);
  border-bottom: 1px solid var(--borde);
}

.marca-fila { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 28px; }
.engranaje { display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; margin: -8px -10px -8px 0; border-radius: 50%; color: var(--suave); }
.engranaje svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.subtitulo { font-size: 1.1rem; }
.marca { font-size: 0.8rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--suave); }
.titulo { font-size: 1.5rem; line-height: 1.2; margin-top: 2px; }

.mes { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 4px; }
.mes h1 { font-size: 1.35rem; line-height: 1.2; text-align: center; flex: 1; }

.redondo {
  width: 44px; height: 44px; flex: none;
  border: 1px solid var(--borde); border-radius: 50%;
  background: var(--tarjeta);
  font-size: 1.5rem; line-height: 1;
}

/* ---------- Contenido ---------- */

.vista { padding: 14px 16px calc(96px + env(safe-area-inset-bottom)); display: grid; gap: 14px; }

.tarjeta {
  background: var(--tarjeta);
  border: 1px solid var(--borde);
  border-radius: var(--radio);
  padding: 16px;
  box-shadow: var(--sombra);
}
.tarjeta h2 { font-size: 1.05rem; margin-bottom: 10px; }

.nota { color: var(--suave); font-size: 0.875rem; }
.nota + .nota { margin-top: 6px; }

.heroe { background: var(--primario); color: var(--sobre-primario); border-color: transparent; }
.heroe.favor { background: var(--bien); }
.heroe.neutro { background: var(--tarjeta); color: var(--tinta); border-color: var(--borde); }
.heroe .etiqueta { font-size: 0.95rem; opacity: 0.92; }
.heroe .cifra { font-size: 2.6rem; font-weight: 750; line-height: 1.1; letter-spacing: -0.02em; margin: 4px 0 6px; font-variant-numeric: tabular-nums; }
.heroe .detalle { font-size: 0.95rem; opacity: 0.92; }
.heroe .btn { margin-top: 12px; background: transparent; border-color: currentColor; color: inherit; }

.banner { background: var(--alerta-suave); border-color: transparent; }
.banner.mal { background: var(--mal-suave); }
.banner strong { display: block; margin-bottom: 2px; }
.banner .acciones { margin-top: 12px; }

/* Barras de ventas contra compras */
.barras { display: grid; gap: 12px; margin-bottom: 12px; }
.barra-fila .rotulo { display: flex; justify-content: space-between; gap: 10px; font-size: 0.95rem; margin-bottom: 5px; }
.barra-fila .rotulo b { font-variant-numeric: tabular-nums; white-space: nowrap; }
.riel { display: flex; height: 14px; border-radius: 7px; background: var(--fondo); overflow: hidden; }
.riel span { display: block; height: 100%; min-width: 0; }
.riel .ventas { background: var(--primario); }
.riel .compras { background: var(--bien); }
.riel .remanente { background: var(--bien); opacity: 0.45; }

/* Listas de etiqueta y monto */
.cuenta { margin: 0; padding: 0; list-style: none; }
.cuenta li { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 9px 0; border-top: 1px solid var(--borde); }
.cuenta li:first-child { border-top: 0; padding-top: 0; }
.cuenta li > span:last-child, .cuenta li > b:last-child { font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.cuenta .total { font-weight: 700; border-top: 2px solid var(--tinta); }
.cuenta small { display: block; color: var(--suave); font-size: 0.82rem; }

.enlace { background: none; border: 0; padding: 6px 0; color: var(--primario); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; min-height: 32px; }

.par { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dato { background: var(--tarjeta); border: 1px solid var(--borde); border-radius: var(--radio); padding: 12px 14px; }
.dato span { display: block; color: var(--suave); font-size: 0.85rem; }
.dato b { font-size: 1.2rem; font-variant-numeric: tabular-nums; }

/* ---------- Botones ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 10px 16px;
  border: 1px solid var(--borde); border-radius: 12px;
  background: var(--tarjeta); color: var(--tinta);
  font-weight: 600; text-align: center;
}
.btn.primario { background: var(--primario); border-color: var(--primario); color: var(--sobre-primario); }
.btn.peligro { background: var(--mal); border-color: var(--mal); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.peligro { color: #2a0d09; } }
.btn.chico { min-height: 40px; padding: 6px 12px; font-size: 0.92rem; }
.btn.ancho { width: 100%; }
.btn:active { transform: translateY(1px); }

.acciones { display: flex; flex-wrap: wrap; gap: 10px; }
.acciones > .btn { flex: 1 1 auto; }
.elegir-mes { display: grid; grid-template-columns: 48px 1fr 48px; gap: 10px; margin-top: 12px; }
.elegir-mes .btn { padding-inline: 8px; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none; min-height: 40px; padding: 6px 14px;
  border: 1px solid var(--borde); border-radius: 20px;
  background: var(--tarjeta); font-weight: 600; font-size: 0.92rem;
}
.chips button[aria-pressed='true'] { background: var(--tinta); border-color: var(--tinta); color: var(--fondo); }

/* ---------- Formularios ---------- */

label { display: block; font-weight: 600; font-size: 0.95rem; }
label + label, label + .fila2, .fila2 + label, .campo + .campo, form > * + * { margin-top: 14px; }

input:not([type='checkbox']):not([type='radio']):not([type='file']), select {
  display: block; width: 100%; min-height: 48px; margin-top: 6px; padding: 10px 12px;
  border: 1px solid var(--borde); border-radius: 12px;
  background: var(--fondo); color: var(--tinta);
  font-size: 1rem; font-weight: 400;
}
input.dinero { font-variant-numeric: tabular-nums; font-size: 1.15rem; font-weight: 600; }
input[type='search'] { background: var(--tarjeta); margin-top: 0; }

.fila2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.fila2 > label { margin-top: 0; }

.check { display: flex; align-items: center; gap: 12px; min-height: 44px; font-weight: 500; }
.check input { width: 24px; height: 24px; flex: none; accent-color: var(--primario); }

.opciones { margin: 14px 0 0; padding: 0; border: 0; min-width: 0; }
.opciones legend { padding: 0; margin-bottom: 2px; font-weight: 600; font-size: 0.95rem; }
.opciones .check { margin-top: 0; }
.ayuda { color: var(--suave); font-size: 0.875rem; font-weight: 400; margin-top: 6px; }
.error { color: var(--mal); font-weight: 600; }
.error:empty { display: none; }

details { border: 1px solid var(--borde); border-radius: 12px; padding: 0 12px; }
details summary { min-height: 46px; display: flex; align-items: center; font-weight: 600; cursor: pointer; color: var(--primario); }
details[open] { padding-bottom: 14px; }

/* Botón que abre el selector de archivos: el campo real lo cubre entero y es invisible. */
.archivo { position: relative; cursor: pointer; }
.archivo input[type='file'] { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; font-size: 0; }
.archivo:focus-within { outline: 3px solid var(--primario); outline-offset: 2px; }

/* ---------- Fotos adjuntas ---------- */

.rotulo-fotos { font-weight: 600; font-size: 0.95rem; }
.campo-fotos .ayuda { margin-top: 2px; }
.fotos { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; min-height: 76px; }
.fotos.ocupada { opacity: 0.55; }
.mini {
  display: flex; align-items: center; justify-content: center;
  width: 76px; height: 76px; margin: 0; padding: 0; overflow: hidden;
  border: 1px solid var(--borde); border-radius: 12px; background: var(--fondo);
  color: var(--suave); font-size: 0.8rem; font-weight: 600; text-align: center;
}
.mini img { display: block; width: 100%; height: 100%; object-fit: cover; }
.mini.agregar { border-style: dashed; border-width: 2px; color: var(--primario); font-size: 0.9rem; }

dialog.visor { width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0; padding: 0; border: 0; background: #0b0d14; color: #fff; }
dialog.visor[open] { display: flex; flex-direction: column; }
dialog.visor::backdrop { background: #0b0d14; }
.visor-imagen { flex: 1; min-height: 0; display: flex; overflow: auto; cursor: zoom-in; }
.visor-imagen img { margin: auto; max-width: 100%; max-height: 100%; object-fit: contain; }
.visor-imagen p { margin: auto; padding: 24px; text-align: center; }
.visor-imagen.grande { cursor: zoom-out; }
.visor-imagen.grande img { max-width: none; max-height: none; width: 250%; }
.visor .acciones { padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); }
.visor .btn { background: #1b1f2b; border-color: #2c3242; color: #fff; }
.visor .btn.primario { background: #fff; border-color: #fff; color: #0b0d14; }

/* ---------- Documentos y productos ---------- */

.lista { display: grid; gap: 10px; }

.doc { background: var(--tarjeta); border: 1px solid var(--borde); border-radius: var(--radio); overflow: hidden; box-shadow: var(--sombra); }
.doc-cuerpo { display: block; width: 100%; padding: 13px 14px; background: none; border: 0; text-align: left; }
.linea { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.linea strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.linea .monto { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.linea.dos { color: var(--suave); font-size: 0.875rem; margin-top: 2px; }
.linea.dos span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.linea.dos span:last-child { white-space: nowrap; }
.doc-pie { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 14px 10px; border-top: 1px solid var(--borde); }

.estado { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 0.82rem; font-weight: 700; white-space: nowrap; }
.estado.bien { background: var(--bien-suave); color: var(--bien); }
.estado.alerta { background: var(--alerta-suave); color: var(--alerta); }
.estado.mal { background: var(--mal-suave); color: var(--mal); }

.stock { display: flex; align-items: center; gap: 10px; }
.stock output { min-width: 2.5ch; text-align: center; font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stock .redondo { width: 40px; height: 40px; font-size: 1.3rem; }

.vacio { text-align: center; color: var(--suave); padding: 28px 12px; border: 1px dashed var(--borde); border-radius: var(--radio); }

.pasos { margin: 0 0 14px; padding-left: 22px; }
.pasos li + li { margin-top: 6px; }

/* ---------- Navegación inferior ---------- */

.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  max-width: 560px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  background: var(--tarjeta); border-top: 1px solid var(--borde);
}
.nav a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-height: 52px; padding: 6px 2px; border-radius: 12px;
  color: var(--suave); text-decoration: none; font-size: 0.72rem; font-weight: 600;
}
.nav a[aria-current='page'] { color: var(--primario); background: var(--primario-suave); }
.nav svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Hojas (formularios) y avisos ---------- */

dialog.hoja {
  width: 100%; max-width: 560px; max-height: 92dvh;
  margin: auto auto 0; padding: 0; border: 0;
  border-radius: 22px 22px 0 0;
  background: var(--tarjeta); color: var(--tinta);
  overflow: auto;
}
dialog.hoja::backdrop { background: rgb(8 10 18 / 0.55); }
.hoja-cuerpo { padding: 20px 16px calc(22px + env(safe-area-inset-bottom)); }
.hoja-cuerpo h2 { font-size: 1.3rem; margin-bottom: 14px; }
.hoja-cuerpo .acciones { margin-top: 20px; }
.hoja-cuerpo > p + p { margin-top: 10px; }

dialog.confirmar { margin: auto; max-width: min(360px, calc(100% - 32px)); border-radius: 22px; }

.aviso {
  position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); z-index: 20;
  transform: translate(-50%, 12px);
  max-width: calc(100% - 32px); padding: 11px 18px; border-radius: 24px;
  background: var(--tinta); color: var(--fondo); font-weight: 600; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
}
.aviso.visible { opacity: 1; transform: translate(-50%, 0); }

.sin-js { padding: 24px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
