:root {
  --fondo: #f4f6f8;
  --superficie: #ffffff;
  --texto: #1a2330;
  --apagado: #556070;
  --borde: #d9dfe6;
  --primario: #0a6c8f;
  --primario-texto: #ffffff;
  --primario-suave: #e3f2f8;
  --ok: #17703f;
  --ok-fondo: #e3f4ea;
  --atencion: #8a5200;
  --atencion-fondo: #fff1d3;
  --hecho: #3d4959;
  --hecho-fondo: #e8ecf1;
  --peligro: #a8281f;
  --peligro-fondo: #fde8e6;
  --sombra: 0 1px 2px rgba(20, 30, 45, 0.08), 0 2px 8px rgba(20, 30, 45, 0.05);
  --radio: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fondo: #12171d;
    --superficie: #1b222b;
    --texto: #e8edf2;
    --apagado: #a3afbd;
    --borde: #2e3944;
    --primario: #4cb6d8;
    --primario-texto: #06222c;
    --primario-suave: #17323d;
    --ok: #6fd69b;
    --ok-fondo: #173226;
    --atencion: #f2c264;
    --atencion-fondo: #3a2e12;
    --hecho: #b9c4d0;
    --hecho-fondo: #28323d;
    --peligro: #ff9a92;
    --peligro-fondo: #3d1d1a;
    --sombra: none;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--fondo);
  color: var(--texto);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.45;
}

h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; text-transform: none; }
p { margin: 0; }
a { color: var(--primario); }
button, input, select, textarea { font: inherit; color: inherit; }

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

.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------------------------------------------------------------- encabezado y navegación */

.encabezado {
  background: var(--superficie);
  border-bottom: 1px solid var(--borde);
  position: sticky; top: 0; z-index: 20;
}
.encabezado-interior {
  max-width: 880px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px;
}
.marca { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.marca img { height: 40px; width: auto; max-width: 120px; object-fit: contain; border-radius: 6px; }
.marca-nombre { font-size: 1.15rem; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.usuario { display: flex; align-items: center; gap: 10px; color: var(--apagado); font-size: 0.9rem; }
.usuario-nombre { display: none; }

.navegacion { max-width: 880px; margin: 0 auto; padding: 0 16px; display: flex; gap: 4px; }
.navegacion a {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; text-align: center; text-decoration: none; color: var(--apagado);
  padding: 12px 6px; border-bottom: 4px solid transparent; font-weight: 600; min-height: 48px;
}
.navegacion a[aria-current="page"] { color: var(--primario); border-bottom-color: var(--primario); }
.cuenta {
  display: inline-block; min-width: 1.5em; padding: 0 0.45em; border-radius: 99px;
  background: var(--hecho-fondo); color: var(--hecho); font-size: 0.8rem; line-height: 1.5;
}
.cuenta.atencion { background: var(--atencion-fondo); color: var(--atencion); }

main { max-width: 880px; margin: 0 auto; padding: 16px 16px 104px; }
.actualizado { color: var(--apagado); font-size: 0.8rem; text-align: right; margin: 0 0 8px; min-height: 1.2em; }

/* ---------------------------------------------------------------- botones */

.boton {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 10px 20px; border-radius: 12px; border: 2px solid var(--primario);
  background: var(--superficie); color: var(--primario); font-weight: 700; cursor: pointer;
  text-decoration: none; text-align: center;
}
.boton:hover { background: var(--primario-suave); }
.boton.principal { background: var(--primario); color: var(--primario-texto); }
.boton.principal:hover { filter: brightness(1.08); background: var(--primario); }
.boton.discreto { border-color: transparent; background: transparent; color: var(--apagado); font-weight: 600; }
.boton.discreto:hover { background: var(--hecho-fondo); }
.boton.chico { min-height: 44px; padding: 6px 14px; font-size: 0.9rem; }
.boton[disabled] { opacity: 0.55; cursor: progress; }

/* ---------------------------------------------------------------- resumen de hoy */

.resumen { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 18px; }
.dato-resumen {
  background: var(--superficie); border: 1px solid var(--borde); border-radius: var(--radio);
  padding: 12px 10px; text-align: center; text-decoration: none; color: var(--texto); box-shadow: var(--sombra);
}
.dato-resumen strong { display: block; font-size: 1.8rem; line-height: 1.1; }
.dato-resumen span { color: var(--texto); font-size: 0.9rem; font-weight: 600; }
.dato-resumen small { display: block; color: var(--apagado); font-size: 0.75rem; line-height: 1.2; }
.nota-ajenos { color: var(--apagado); font-size: 0.9rem; text-align: center; margin: 18px 0 0; }
.dato-resumen.atencion strong { color: var(--atencion); }

/* ---------------------------------------------------------------- listas */

.dia { font-size: 0.95rem; color: var(--apagado); font-weight: 700; margin: 22px 0 8px; }
.dia:first-child { margin-top: 0; }
.lista { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }

.tarjeta {
  position: relative; background: var(--superficie); border: 1px solid var(--borde);
  border-left-width: 6px; border-radius: var(--radio); padding: 14px 16px; box-shadow: var(--sombra);
}
.tarjeta.e-PENDIENTE { border-left-color: var(--primario); }
.tarjeta.e-REVISION { border-left-color: var(--atencion); }
.tarjeta.e-COMPLETADO { border-left-color: var(--ok); }
.tarjeta.e-NO_PEDIDO { border-left-color: var(--borde); }
.tarjeta.e-COMPLETADO .tarjeta-productos { color: var(--apagado); }
.tarjeta.e-PROCESANDO { border-left-color: var(--hecho); }
.tarjeta.agregado { margin-left: 22px; border-style: dashed; border-left-style: solid; }
.tarjeta.agregado::before {
  content: ""; position: absolute; left: -22px; top: -12px; width: 16px; height: 36px;
  border-left: 3px solid var(--borde); border-bottom: 3px solid var(--borde); border-bottom-left-radius: 10px;
}

.tarjeta-fila { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.tarjeta-titulo.sin-declarar { font-weight: 600; }
.tarjeta-titulo { font-weight: 700; font-size: 1.05rem; margin: 0; overflow-wrap: anywhere; }
.tarjeta-titulo a { color: inherit; text-decoration: none; }
.tarjeta-titulo a::after { content: ""; position: absolute; inset: 0; border-radius: var(--radio); }
.tarjeta-meta { color: var(--apagado); font-size: 0.85rem; margin-top: 2px; }
.tarjeta-productos { margin-top: 8px; overflow-wrap: anywhere; }
.tarjeta-pie { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.tarjeta-pie .boton { position: relative; z-index: 2; }
.vinculo-agregado {
  display: inline-flex; align-items: center; padding: 11px 0; margin: -11px 0; /* 44 px de toque sin agrandar la tarjeta */
  font-size: 0.85rem; color: var(--primario); font-weight: 600;
}

.etiqueta {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: 99px;
  font-size: 0.8rem; font-weight: 700; white-space: nowrap;
}
.etiqueta.PENDIENTE { background: var(--primario-suave); color: var(--primario); }
.etiqueta.REVISION { background: var(--atencion-fondo); color: var(--atencion); }
.etiqueta.COMPLETADO { background: var(--ok-fondo); color: var(--ok); }
.etiqueta.PROCESANDO, .etiqueta.NO_PEDIDO { background: var(--hecho-fondo); color: var(--hecho); }
.etiqueta.neutra { background: var(--hecho-fondo); color: var(--hecho); font-weight: 600; }
.etiqueta.agregado { background: transparent; color: var(--primario); border: 1px solid var(--primario); }

.vacio {
  text-align: center; color: var(--apagado); padding: 40px 16px; background: var(--superficie);
  border: 1px dashed var(--borde); border-radius: var(--radio);
}
.vacio strong { display: block; color: var(--texto); font-size: 1.1rem; margin-bottom: 4px; }
.mas { text-align: center; margin-top: 16px; }

/* ---------------------------------------------------------------- historial: filtros */

.filtros { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.campo-busqueda { flex: 1 1 240px; }
input[type="search"], input[type="text"], input[type="password"], input[type="date"], input[type="number"],
select, textarea {
  width: 100%; min-height: 48px; padding: 10px 14px; border-radius: 12px; border: 2px solid var(--borde);
  background: var(--superficie);
}
input:focus, select:focus, textarea:focus { border-color: var(--primario); outline: none; box-shadow: 0 0 0 3px var(--primario-suave); }
textarea { min-height: 96px; resize: vertical; }
.filtros select { flex: 0 1 200px; }

/* ---------------------------------------------------------------- detalle */

.volver { display: inline-block; margin-bottom: 12px; text-decoration: none; font-weight: 600; padding: 8px 0; min-height: 44px; }
.cabecera-pedido { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 4px; }
.sub { color: var(--apagado); margin-bottom: 16px; }

.bloque {
  background: var(--superficie); border: 1px solid var(--borde); border-radius: var(--radio);
  padding: 16px; margin-bottom: 18px; box-shadow: var(--sombra);
}
.bloque > h3 { margin-bottom: 10px; color: var(--apagado); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.bloque.aviso { background: var(--atencion-fondo); border-color: transparent; color: var(--atencion); }
.bloque.aviso > h3 { color: inherit; }
.bloque.aviso ul { margin: 8px 0 0; padding-left: 20px; }
.bloque.vinculo { background: var(--primario-suave); border-color: transparent; }
.bloque.neutro { background: var(--hecho-fondo); border-color: transparent; color: var(--hecho); }
.bloque.neutro > h3 { color: inherit; }
.bloque.neutro .nota { color: inherit; }
.bloque.error { background: var(--peligro-fondo); border-color: transparent; color: var(--peligro); }

.datos { display: grid; grid-template-columns: 1fr; gap: 10px 20px; margin: 0; }
.datos div { min-width: 0; }
.datos dt { color: var(--apagado); font-size: 0.8rem; }
.datos dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.datos dd.sin-dato { color: var(--apagado); font-weight: 400; font-style: italic; }
.bloque > h3.tras-datos { margin-top: 26px; padding-top: 18px; border-top: 1px solid var(--borde); }
.origen-datos { color: var(--apagado); font-size: 0.9rem; margin: -10px 0 16px; }
.nota.guia { margin: 0 0 14px; }

.productos { width: 100%; border-collapse: collapse; margin-top: 4px; }
.productos th { text-align: left; color: var(--apagado); font-size: 0.8rem; font-weight: 600; padding: 4px 8px 4px 0; }
.productos td { padding: 8px 8px 8px 0; border-top: 1px solid var(--borde); overflow-wrap: anywhere; }
.productos td.num { white-space: nowrap; font-weight: 700; }

.original {
  white-space: pre-wrap; overflow-wrap: anywhere; background: var(--fondo); border-radius: 10px;
  padding: 12px 14px; margin: 0; font-family: inherit; border: 1px solid var(--borde);
}
.nota { color: var(--apagado); font-size: 0.85rem; margin-top: 8px; }

.acciones {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; background: var(--superficie);
  border-top: 1px solid var(--borde); padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
}
.acciones-interior { max-width: 880px; margin: 0 auto; display: flex; gap: 8px; }
.acciones-interior .boton { flex: 1 1 0; min-width: 0; min-height: 48px; padding: 8px 10px; }
.acciones-interior .estado-completado { flex: 2 1 0; align-self: center; margin: 0; color: var(--hecho); font-weight: 600; font-size: 0.95rem; }

details.historial summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; }
details.historial ul { margin: 6px 0 0; padding-left: 20px; color: var(--apagado); }

/* ---------------------------------------------------------------- formulario de corrección */

.formulario .campo { margin-bottom: 14px; }
.formulario label { display: block; font-weight: 600; margin-bottom: 4px; }
.fila-producto {
  display: grid; grid-template-columns: 90px 1fr; gap: 8px; padding: 12px; margin-bottom: 10px;
  border: 1px solid var(--borde); border-radius: 12px; background: var(--fondo);
}
.fila-producto .ancho { grid-column: 1 / -1; }
.fila-producto label { font-size: 0.8rem; color: var(--apagado); font-weight: 600; }
.errores { margin: 0 0 14px; padding: 12px 14px 12px 32px; border-radius: 12px; background: var(--peligro-fondo); color: var(--peligro); }

/* ---------------------------------------------------------------- ingreso */

.ingreso { max-width: 420px; margin: 0 auto; padding: 56px 16px 24px; }
.ingreso-marca { display: flex; flex-direction: column; align-items: center; gap: 12px; margin-bottom: 28px; text-align: center; }
.ingreso-marca img { max-height: 84px; max-width: 220px; object-fit: contain; border-radius: 8px; }
.ingreso .bloque { padding: 22px; }
.ingreso .campo { margin-bottom: 16px; }
.ingreso label { display: block; font-weight: 600; margin-bottom: 4px; }
.ingreso .boton { width: 100%; }
.mensaje-error { color: var(--peligro); font-weight: 600; margin-bottom: 14px; }

/* ---------------------------------------------------------------- aviso emergente y diálogo */

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 96px; z-index: 40;
  background: var(--texto); color: var(--superficie); padding: 12px 18px; border-radius: 12px;
  display: flex; align-items: center; gap: 14px; max-width: calc(100vw - 32px); box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3);
}
.toast button { background: none; border: 0; color: inherit; font-weight: 700; text-decoration: underline; cursor: pointer; padding: 6px; min-height: 40px; }

dialog { border: 0; border-radius: var(--radio); padding: 22px; max-width: min(92vw, 440px); background: var(--superficie); color: var(--texto); }
dialog::backdrop { background: rgba(10, 15, 22, 0.55); }
dialog h2 { margin-bottom: 8px; }
dialog .botones { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
dialog .botones .boton { flex: 1 1 140px; }

.cargando { text-align: center; color: var(--apagado); padding: 40px 0; }

/* ---------------------------------------------------------------- pantallas más anchas */

@media (min-width: 640px) {
  .usuario-nombre { display: inline; }
  .datos { grid-template-columns: 1fr 1fr; }
  .fila-producto { grid-template-columns: 110px 1fr 1.4fr; }
  .fila-producto .ancho { grid-column: auto; }
  .fila-producto .quitar { grid-column: 1 / -1; justify-self: end; }
}

@media (max-width: 480px) {
  .navegacion { padding: 0 8px; gap: 0; }
  .navegacion a { padding: 12px 2px; font-size: 0.85rem; gap: 4px; }
  .cuenta { font-size: 0.75rem; padding: 0 0.4em; }
  .resumen { gap: 8px; }
  .dato-resumen { padding: 10px 6px; }
}

@media (prefers-reduced-motion: no-preference) {
  .boton { transition: background-color 0.15s, filter 0.15s; }
}
