:root {
  --papel: #f7f4ee;
  --superficie: #ffffff;
  --tinta: #191512;
  --tinta-2: #6b6259;
  --tinta-3: #9a9086;
  --linea: #e6dfd4;
  --vino: #7a1f2b;
  --vino-tenue: #f4e6e4;
  --enlace: #7a1f2b;
  --ok: #1d7a4c;
  --ok-tenue: #dcefe4;
  --cerca: #a05f00;
  --cerca-tenue: #fbf0db;
  --fuera: #b3241c;
  --fuera-tenue: #fbe6e3;
  --sombra: 0 1px 2px rgba(25, 21, 18, .06), 0 6px 18px rgba(25, 21, 18, .05);
  --r: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--papel);
  color: var(--tinta);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100dvh; }

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 18px) 18px calc(env(safe-area-inset-bottom) + 28px);
}

/* ---------- tipografía ---------- */
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; }
.sutil { color: var(--tinta-2); font-size: .85rem; margin: 0; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- barra superior ---------- */
.barra {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
  min-height: 34px;
}
.barra__texto { flex: 1; min-width: 0; }
.barra__texto p { margin: 1px 0 0; font-size: .82rem; color: var(--tinta-2); }

.btn-icono {
  width: 34px; height: 34px;
  border: 1px solid var(--linea);
  background: var(--superficie);
  border-radius: 10px;
  color: var(--tinta-2);
  font-size: .95rem;
  cursor: pointer;
  display: grid; place-items: center;
  flex: none;
  padding: 0;
}
.btn-icono:active { background: #f2ede4; }

/* ---------- botones ---------- */
.btn {
  width: 100%;
  min-height: 54px;
  border-radius: var(--r);
  border: 1px solid transparent;
  font-size: 1rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0 18px;
  transition: transform .06s ease, filter .15s ease;
}
.btn:active { transform: scale(.985); }
.btn--primario { background: var(--vino); color: #fff; box-shadow: var(--sombra); }
.btn--secundario { background: var(--superficie); color: var(--tinta); border-color: var(--linea); }
.btn--compacto { min-height: 42px; width: auto; font-size: .88rem; padding: 0 16px; }
.btn[disabled] { opacity: .55; pointer-events: none; }

/* ---------- inicio ---------- */
.hero {
  background: linear-gradient(160deg, #2c1a17 0%, #7a1f2b 100%);
  color: #fff;
  border-radius: 20px;
  padding: 24px 20px 20px;
  margin-bottom: 16px;
  box-shadow: var(--sombra);
}
.hero h1 { font-size: 1.55rem; }
.hero p { margin: 6px 0 18px; color: #f0d9d6; font-size: .88rem; }
.hero .btn--primario {
  background: #fff;
  color: #6b1a25;
  box-shadow: none;
}
.hero__acciones { display: grid; gap: 10px; }

.indicadores {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 22px;
}
.indicador {
  background: var(--superficie);
  border: 1px solid var(--linea);
  border-radius: var(--r);
  padding: 12px 12px 10px;
  display: flex;
  flex-direction: column;
  min-height: 76px;
}
.indicador__valor { font-size: 1.5rem; font-weight: 680; line-height: 1.1; letter-spacing: -.02em; }
.indicador__rotulo {
  font-size: .67rem; color: var(--tinta-2); text-transform: uppercase;
  letter-spacing: .02em; margin-top: auto; padding-top: 4px; line-height: 1.25;
}
.indicador--alerta .indicador__valor { color: var(--fuera); }

.seccion-titulo {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 0 2px 10px;
}
.seccion-titulo a, .vinculo {
  font-size: .82rem;
  color: var(--enlace);
  text-decoration: none;
  font-weight: 600;
}

/* ---------- lista de cubas ---------- */
.lista { display: grid; gap: 8px; }

.cuba-fila {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: var(--superficie);
  border: 1px solid var(--linea);
  border-radius: var(--r);
  padding: 13px 14px;
  font-family: inherit;
  font-size: 1rem;
  color: inherit;
  cursor: pointer;
}
.cuba-fila:active { background: #fbf8f2; }
.cuba-fila__id { flex: 1; min-width: 0; }
.cuba-fila__codigo { display: block; font-weight: 660; letter-spacing: -.01em; }
.cuba-fila__meta { display: block; font-size: .78rem; color: var(--tinta-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cuba-fila__temp { text-align: right; flex: none; }
.cuba-fila__valor { display: block; font-size: 1.15rem; font-weight: 650; line-height: 1.2; }
.cuba-fila__estado { display: block; font-size: .72rem; font-weight: 620; }

.punto {
  width: 9px; height: 9px; border-radius: 50%;
  flex: none;
  background: var(--tinta-3);
}
.punto--ok { background: var(--ok); }
.punto--cerca { background: var(--cerca); }
.punto--fuera { background: var(--fuera); }

.txt-ok { color: var(--ok); }
.txt-cerca { color: var(--cerca); }
.txt-fuera { color: var(--fuera); }
.txt-neutro { color: var(--tinta-3); }

/* ---------- ficha ---------- */
.identidad { margin-bottom: 16px; }
.identidad__codigo {
  font-size: 2.6rem;
  font-weight: 700;
  letter-spacing: -.03em;
  line-height: 1;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  font-size: .76rem;
  color: var(--tinta-2);
  background: var(--superficie);
  border: 1px solid var(--linea);
  border-radius: 999px;
  padding: 4px 10px;
}

.tarjeta {
  background: var(--superficie);
  border: 1px solid var(--linea);
  border-radius: var(--r);
  padding: 16px;
  margin-bottom: 12px;
}
.tarjeta--estado { border-left: 4px solid var(--tinta-3); }
.tarjeta--ok { border-left-color: var(--ok); background: linear-gradient(100deg, var(--ok-tenue), var(--superficie) 55%); }
.tarjeta--cerca { border-left-color: var(--cerca); background: linear-gradient(100deg, var(--cerca-tenue), var(--superficie) 55%); }
.tarjeta--fuera { border-left-color: var(--fuera); background: linear-gradient(100deg, var(--fuera-tenue), var(--superficie) 55%); }

.lectura { display: flex; align-items: flex-end; gap: 6px; }
.lectura__valor { font-size: 2.7rem; font-weight: 700; line-height: 1; letter-spacing: -.03em; }
.lectura__unidad { font-size: 1.05rem; font-weight: 600; color: var(--tinta-2); padding-bottom: 5px; }
.lectura__nota { font-size: .85rem; color: var(--tinta-2); padding-bottom: 5px; margin-left: auto; text-align: right; }

.estado-etiqueta {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: .8rem; font-weight: 660;
  margin-bottom: 8px;
}

.ficha-pie {
  position: sticky;
  bottom: 0;
  padding: 12px 0 calc(env(safe-area-inset-bottom) + 4px);
  background: linear-gradient(transparent, var(--papel) 30%);
}

/* ---------- gráfico ---------- */
.grafico-cabecera {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 12px;
}
.segmentado {
  display: inline-flex;
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: 10px;
  padding: 2px;
}
.segmentado button {
  border: 0; background: transparent; font-family: inherit;
  font-size: .78rem; font-weight: 600; color: var(--tinta-2);
  padding: 5px 11px; border-radius: 8px; cursor: pointer;
}
.segmentado button[aria-pressed="true"] {
  background: var(--superficie); color: var(--tinta); box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
.grafico { width: 100%; height: auto; display: block; margin-top: 4px; }
.eje-texto { font-size: 10px; fill: var(--tinta-2); font-family: inherit; }
.leyenda-grafico {
  display: flex; gap: 14px; flex-wrap: wrap;
  margin-top: 10px; font-size: .72rem; color: var(--tinta-2);
}
.leyenda-grafico span { display: inline-flex; align-items: center; gap: 5px; }
.leyenda-grafico i {
  width: 8px; height: 8px; border-radius: 50%; display: inline-block;
  background: var(--tinta-3);
}
.leyenda-grafico .l-ok { background: var(--ok); }
.leyenda-grafico .l-cerca { background: var(--cerca); }
.leyenda-grafico .l-fuera { background: var(--fuera); }

/* ---------- historial ---------- */
.registro {
  display: flex; align-items: baseline; gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid var(--linea);
}
.registro:last-child { border-bottom: 0; }
.registro__fecha { font-size: .78rem; color: var(--tinta-2); width: 82px; flex: none; }
.registro__valores { flex: 1; min-width: 0; font-size: .88rem; }
.registro__obs { font-size: .76rem; color: var(--tinta-3); }
.registro__temp { font-weight: 640; }

.vacio {
  text-align: center;
  color: var(--tinta-2);
  font-size: .88rem;
  padding: 26px 10px;
}

/* ---------- formulario ---------- */
.campo-temp {
  text-align: center;
  padding: 10px 0 4px;
}
.campo-temp label { display: block; font-size: .82rem; color: var(--tinta-2); margin-bottom: 6px; }
.campo-temp__caja {
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.campo-temp input {
  width: 190px;
  font-family: inherit;
  font-size: 3.2rem;
  font-weight: 700;
  letter-spacing: -.03em;
  text-align: center;
  border: 0;
  border-bottom: 2px solid var(--linea);
  background: transparent;
  color: var(--tinta);
  padding: 2px 0 6px;
  -moz-appearance: textfield;
  font-variant-numeric: tabular-nums;
}
.campo-temp input::-webkit-outer-spin-button,
.campo-temp input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.campo-temp input:focus { outline: none; border-bottom-color: var(--vino); }
.campo-temp__unidad { font-size: 1.4rem; font-weight: 600; color: var(--tinta-2); }

.opcional { border-top: 1px solid var(--linea); margin-top: 18px; padding-top: 14px; }
.opcional summary {
  cursor: pointer; font-size: .85rem; font-weight: 600; color: var(--enlace);
  list-style: none;
}
.opcional summary::-webkit-details-marker { display: none; }
.fila-campos { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.campo label { display: block; font-size: .75rem; color: var(--tinta-2); margin-bottom: 4px; }
.campo input, .campo textarea {
  width: 100%;
  font-family: inherit;
  font-size: 1rem;
  padding: 10px 12px;
  border: 1px solid var(--linea);
  border-radius: 10px;
  background: var(--superficie);
  color: var(--tinta);
}
.campo textarea { resize: vertical; min-height: 60px; }
.campo input:focus, .campo textarea:focus { outline: 2px solid var(--vino-tenue); border-color: var(--vino); }
.campo--ancho { grid-column: 1 / -1; }

.aviso-rango {
  font-size: .82rem;
  color: var(--tinta-2);
  text-align: center;
  margin: 14px 0 0;
}

/* ---------- escáner ---------- */
.escaner {
  position: fixed; inset: 0; z-index: 40;
  background: #120e0c;
  display: flex; align-items: flex-start; justify-content: center;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.escaner[hidden] { display: none; }
.escaner__marco {
  width: 100%; max-width: 560px; color: #fff;
  margin: auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.escaner__barra {
  display: flex; align-items: center; justify-content: space-between;
  font-size: .85rem; color: #cfc6bd; margin-bottom: 12px;
}
.escaner__barra .btn-icono { background: rgba(255,255,255,.1); border-color: rgba(255,255,255,.18); color: #fff; }
/* La cámara manda el ancho, pero nunca tanto como para empujar el pie
   (el código manual) fuera de la pantalla: en teléfonos pequeños el video
   se achica y el bloque de abajo siempre queda visible. */
.escaner__camara {
  position: relative; border-radius: 16px; overflow: hidden; background: #000;
}
#video {
  width: 100%; height: auto; max-height: 56dvh;
  aspect-ratio: 3 / 4; object-fit: cover;
  display: block;
}
.escaner__guia {
  position: absolute;
  width: min(62vw, 260px); aspect-ratio: 1;
  border: 2px solid rgba(255,255,255,.75);
  border-radius: 18px;
  pointer-events: none;
  top: 50%; left: 50%; transform: translate(-50%, -50%);
  box-shadow: 0 0 0 100vmax rgba(0,0,0,.28);
}
.escaner__pie { margin-top: 14px; }
.escaner__pie p { font-size: .82rem; color: #cfc6bd; margin: 0 0 14px; text-align: center; }
/* El bloque manual va acotado y centrado: no necesita todo el ancho de la
   pantalla y así nunca empuja el diseño a un scroll horizontal. */
.escaner__manual { max-width: 260px; margin: 0 auto; }
.escaner__manual label { display: block; font-size: .76rem; color: #9d948b; text-align: center; }
.escaner__fila { display: flex; gap: 8px; margin-top: 6px; }
.escaner__fila input {
  flex: 1 1 auto; min-width: 0;
  font-family: inherit; font-size: 1rem; text-transform: uppercase;
  padding: 11px 12px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,.2); background: rgba(255,255,255,.08); color: #fff;
}
.escaner__fila input::placeholder { color: #7d746c; }
.escaner__fila .btn { background: #fff; color: #6b1a25; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: calc(env(safe-area-inset-bottom) + 22px);
  transform: translateX(-50%);
  background: #191512;
  color: #fff;
  font-size: .87rem;
  padding: 12px 18px;
  border-radius: 12px;
  z-index: 60;
  max-width: min(90vw, 420px);
  box-shadow: 0 8px 26px rgba(0,0,0,.28);
  text-align: center;
}
.toast[hidden] { display: none; }
.toast--error { background: #8f1c15; }

@media (prefers-color-scheme: dark) {
  :root {
    --papel: #141110;
    --superficie: #1e1a18;
    --tinta: #f3eee7;
    --tinta-2: #a89e94;
    --tinta-3: #7a716a;
    --linea: #332c28;
    --vino-tenue: #2b1a1c;
    --enlace: #e59aa2;
    --ok-tenue: #16281f;
    --cerca-tenue: #2a2113;
    --fuera-tenue: #2b1815;
    --sombra: 0 1px 2px rgba(0,0,0,.4);
  }
  .hero { background: linear-gradient(160deg, #2a1715 0%, #5e1720 100%); }
  .segmentado { background: #171311; }
  .cuba-fila:active { background: #241f1c; }
}
