:root {
  --primario: #0f766e;
  --primario-oscuro: #115e59;
  --fondo: #f3f4f6;
  --tarjeta: #ffffff;
  --texto: #111827;
  --texto-2: #6b7280;
  --borde: #e5e7eb;
  --ok: #16a34a;
  --atento: #f59e0b;
  --pasado: #dc2626;
  --peligro: #dc2626;
  --radio: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--fondo);
  color: var(--texto);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  padding-bottom: calc(96px + var(--safe-b));
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- Header ---------- */
.top {
  background: var(--tarjeta);
  padding: calc(10px + var(--safe-t)) 16px 14px;
  border-bottom: 1px solid var(--borde);
  position: sticky; top: 0; z-index: 5;
}
.top-row { display: flex; align-items: center; justify-content: space-between; }
.top h1 { margin: 0; font-size: 20px; font-weight: 700; }
.icon-btn {
  background: none; border: 0; font-size: 26px; line-height: 1;
  width: 44px; height: 44px; border-radius: 50%; color: var(--texto);
}
.icon-btn:active { background: var(--fondo); }
.presupuesto {
  display: inline-block; margin: 4px 0 10px; padding: 6px 12px;
  background: var(--fondo); border: 0; border-radius: 999px;
  font-size: 14px; color: var(--texto-2);
}
.presupuesto strong { color: var(--texto); font-variant-numeric: tabular-nums; }

.total-box {
  background: var(--primario); color: #fff; border-radius: var(--radio);
  padding: 14px 16px 16px; transition: background .25s;
}
.total-box.atento { background: #b45309; }
.total-box.pasado { background: #b91c1c; }
.total-label { font-size: 13px; opacity: .85; text-transform: uppercase; letter-spacing: .06em; }
.total { font-size: 52px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.restante { font-size: 15px; opacity: .92; margin-top: 4px; font-variant-numeric: tabular-nums; }
.barra { height: 8px; background: rgba(255,255,255,.28); border-radius: 999px; margin-top: 12px; overflow: hidden; }
.barra-fill { height: 100%; width: 0; border-radius: 999px; background: #fff; transition: width .3s; }

/* ---------- Lista ---------- */
main { padding: 12px 12px 0; }
.lista { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.item {
  background: var(--tarjeta); border-radius: 12px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px; min-height: 62px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.item:active { background: #f9fafb; }
.item .icono { font-size: 16px; opacity: .55; width: 20px; text-align: center; flex: none; }
.item .info { flex: 1; min-width: 0; }
.item .nombre { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .detalle { font-size: 13px; color: var(--texto-2); margin-top: 2px; font-variant-numeric: tabular-nums; }
.item .subtotal { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.vacio { text-align: center; color: var(--texto-2); padding: 48px 24px; line-height: 1.5; }

/* ---------- Acciones ---------- */
.acciones {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; gap: 10px; padding: 10px 12px calc(10px + var(--safe-b));
  background: rgba(243,244,246,.92); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--borde);
}
.btn {
  border: 0; border-radius: 14px; padding: 0 18px; min-height: 56px;
  font-size: 17px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center;
}
.btn:active { filter: brightness(.92); }
.btn-primary { background: var(--primario); color: #fff; flex: 1.6; }
.btn-secondary { background: var(--tarjeta); color: var(--texto); border: 1px solid var(--borde); flex: 1; }
.btn-danger { background: #fee2e2; color: var(--peligro); }
.btn[disabled] { opacity: .5; }

/* ---------- Dialogs ---------- */
dialog { border: 0; padding: 0; background: transparent; color: var(--texto); max-width: none; }
dialog::backdrop { background: rgba(17,24,39,.45); }
.sheet { position: fixed; inset: auto 0 0 0; margin: 0; width: 100%; }
.sheet > form, .sheet > .menu {
  background: var(--tarjeta); border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(16px + var(--safe-b));
  max-height: 92vh; overflow-y: auto;
}
.sheet-handle { width: 40px; height: 5px; border-radius: 999px; background: var(--borde); margin: 4px auto 12px; }
.sheet h2, .modal h2 { margin: 0 0 12px; font-size: 20px; }
.f-foto { width: 100%; max-height: 160px; object-fit: cover; border-radius: 12px; margin-bottom: 12px; background: #000; }
.aviso { background: #fef3c7; color: #92400e; padding: 10px 12px; border-radius: 10px; font-size: 14px; margin-bottom: 12px; }
.campo { display: block; margin-bottom: 12px; }
.campo > span { display: block; font-size: 13px; color: var(--texto-2); margin-bottom: 4px; }
.campo input[type="text"] {
  width: 100%; border: 1px solid var(--borde); border-radius: 12px; padding: 12px 14px;
  font-size: 17px; background: var(--fondo);
}
.campo input:focus { outline: 2px solid var(--primario); border-color: transparent; }
.con-prefijo { display: flex; align-items: center; border: 1px solid var(--borde); border-radius: 12px; background: var(--fondo); }
.con-prefijo b { padding: 0 0 0 14px; font-size: 22px; color: var(--texto-2); }
.con-prefijo input { border: 0 !important; background: transparent !important; font-size: 24px !important; font-weight: 700; font-variant-numeric: tabular-nums; }
.con-prefijo:focus-within { outline: 2px solid var(--primario); }
.con-prefijo.grande input { font-size: 32px !important; padding: 14px; width: 100%; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button {
  width: 48px; height: 48px; border-radius: 12px; border: 1px solid var(--borde);
  background: var(--fondo); font-size: 24px;
}
.stepper input { width: 70px; text-align: center; border: 1px solid var(--borde); border-radius: 12px; padding: 12px 0; font-size: 20px; font-weight: 700; background: var(--fondo); }
.subtotal-preview { text-align: right; color: var(--texto-2); font-size: 14px; margin: -4px 0 12px; font-variant-numeric: tabular-nums; }
details { margin-bottom: 12px; font-size: 14px; color: var(--texto-2); }
details pre { white-space: pre-wrap; font-size: 12px; background: var(--fondo); padding: 10px; border-radius: 10px; max-height: 180px; overflow: auto; margin: 8px 0 0; }
.sheet-botones { display: flex; gap: 10px; margin-top: 8px; }
.sheet-botones .btn { flex: 1; }
.sheet-botones .btn-danger { flex: 0 0 auto; }

.modal { position: fixed; inset: 0; margin: auto; width: min(92vw, 380px); }
.modal > form { background: var(--tarjeta); border-radius: 18px; padding: 20px 18px; }
.modal p { margin: 0 0 16px; font-size: 16px; line-height: 1.45; }

.menu { display: flex; flex-direction: column; gap: 6px; }
.menu button {
  text-align: left; background: var(--fondo); border: 0; border-radius: 12px;
  padding: 14px 16px; font-size: 17px; font-weight: 600;
}
.menu button small { display: block; font-size: 13px; font-weight: 400; color: var(--texto-2); margin-top: 2px; }
.menu button.peligro { color: var(--peligro); }
.menu button.cerrar { background: none; text-align: center; color: var(--texto-2); font-weight: 500; }

.pantalla { position: fixed; inset: 0; margin: 0; width: 100%; height: 100%; max-height: none; background: var(--fondo); }
.pantalla-top { display: flex; align-items: center; gap: 4px; padding: calc(8px + var(--safe-t)) 12px 8px; background: var(--tarjeta); border-bottom: 1px solid var(--borde); position: sticky; top: 0; }
.pantalla-top h2 { margin: 0; font-size: 20px; }
.pantalla-top .icon-btn { font-size: 34px; }
.historial { padding: 12px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; height: calc(100% - 70px); }
.historial details { background: var(--tarjeta); border-radius: 12px; padding: 0; margin: 0; color: var(--texto); }
.historial summary { list-style: none; display: flex; justify-content: space-between; align-items: center; padding: 14px; font-size: 16px; cursor: pointer; }
.historial summary::-webkit-details-marker { display: none; }
.historial summary .fecha { font-weight: 600; }
.historial summary .meta { font-size: 13px; color: var(--texto-2); }
.historial summary .tot { font-weight: 700; font-variant-numeric: tabular-nums; }
.historial ul { list-style: none; margin: 0; padding: 0 14px 14px; }
.historial li { display: flex; justify-content: space-between; padding: 6px 0; border-top: 1px solid var(--borde); font-size: 14px; }
.historial li span:last-child { font-variant-numeric: tabular-nums; }
.historial .h-presu { font-size: 13px; color: var(--texto-2); padding: 0 14px 12px; }
.historial .vacio { padding: 40px 20px; }

/* ---------- Overlay OCR ---------- */
.overlay { position: fixed; inset: 0; background: rgba(17,24,39,.6); z-index: 20; display: flex; align-items: center; justify-content: center; padding: 24px; }
.overlay-card { background: var(--tarjeta); border-radius: 18px; padding: 24px 20px; width: min(100%, 340px); text-align: center; }
.overlay-card #ocr-status { font-size: 16px; font-weight: 600; margin: 14px 0 10px; }
.spinner { width: 36px; height: 36px; border: 4px solid var(--borde); border-top-color: var(--primario); border-radius: 50%; margin: 0 auto; animation: girar .8s linear infinite; }
@keyframes girar { to { transform: rotate(360deg); } }
.ocr-barra { height: 6px; background: var(--borde); border-radius: 999px; overflow: hidden; margin-bottom: 18px; }
.ocr-barra div { height: 100%; width: 0; background: var(--primario); transition: width .2s; }
.overlay-card .btn { width: 100%; }

.toast {
  position: fixed; left: 50%; bottom: calc(110px + var(--safe-b)); transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px; z-index: 30;
  max-width: 90vw; text-align: center;
}

/* Diálogo de clave de escaneo */
.modal input#k-valor { width: 100%; box-sizing: border-box; font: inherit; font-size: 17px; padding: 12px 14px; border: 1px solid var(--borde); border-radius: 12px; background: var(--fondo); color: var(--texto); margin-bottom: 16px; }
.modal input#k-valor:focus { outline: 2px solid var(--primario); border-color: transparent; }

/* Foto del cartel a pantalla completa */
.foto-pantalla { background: #000; }
.foto-pantalla .pantalla-top { background: #111; border-color: #222; color: #fff; }
.foto-pantalla .pantalla-top .icon-btn { color: #fff; }
.foto-marco { position: absolute; top: calc(52px + var(--safe-t)); bottom: 0; left: 0; right: 0; display: flex; align-items: center; justify-content: center; overflow: auto; }
.foto-marco img { max-width: 100%; max-height: 100%; object-fit: contain; }
.f-foto[data-grande] { cursor: zoom-in; }
.historial .h-der { display: inline-flex; align-items: center; gap: 8px; }
.historial .h-foto { border: 0; background: transparent; font-size: 18px; padding: 2px 4px; cursor: pointer; line-height: 1; }
