:root {
  --bg: #f6f4ee;
  --surface: #ffffff;
  --ink: #1e2019;
  --ink-soft: #78766a;
  --placeholder: #a9a694;
  --line: #e4e0d2;
  --tint: #efebde;
  --accent: #b9762e;
  --accent-hover: #96611f;
  --accent-soft: #f5e9d8;
  --ok: #3f7d5c;
  --ok-bg: #e6f0e8;
  --warn: #b9792a;
  --warn-bg: #fbf1de;
  --bad: #b04632;
  --bad-bg: #f8e6e1;
  --radius: 14px;
  --radius-sm: 11px;
  --font: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --header-h: 64px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
body { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
body.focus-mode { padding-bottom: env(safe-area-inset-bottom); }

a { color: var(--accent); }
a:hover { color: var(--accent-hover); }
button, input, select, textarea { font: inherit; color: inherit; }
::placeholder { color: var(--placeholder); }

.hidden { display: none !important; }
.muted { color: var(--ink-soft); font-size: 0.85rem; }
.text-bad { color: var(--bad); font-size: 0.84rem; font-weight: 600; }
.code { font-size: 0.76rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.name { font-size: 0.9rem; font-weight: 700; line-height: 1.25; }

/* ─── Cabecera ───────────────────────────────────────────────────────── */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: calc(14px + env(safe-area-inset-top)) 20px 12px;
}
.hdr-row { display: flex; align-items: center; gap: 10px; min-height: 36px; }
.hdr-back {
  border: none;
  background: none;
  padding: 6px;
  margin-left: -8px;
  border-radius: 10px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.hdr-back svg { width: 22px; height: 22px; }
.hdr-titles { flex: 1; min-width: 0; }
.hdr-title { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hdr-sub { font-size: 12px; color: var(--ink-soft); }
.hdr-sub:empty { display: none; }
#hdr-extra:not(:empty) { margin-top: 12px; }

.avatar {
  width: 34px; height: 34px;
  border-radius: 999px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.user-menu {
  position: fixed;
  top: calc(56px + env(safe-area-inset-top));
  right: 16px;
  z-index: 40;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(30, 32, 25, 0.14);
  padding: 14px;
  min-width: 220px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  border: 1px solid var(--line);
}
.search svg { width: 16px; height: 16px; flex: none; }
.search input { border: none; background: transparent; padding: 10px 0; min-height: 42px; font-size: 15px; }
.search input:focus { outline: none; }
.search:focus-within { border-color: var(--accent); }

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 -20px;
  padding: 2px 20px;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ─── Layout ─────────────────────────────────────────────────────────── */

.container { max-width: 720px; margin: 0 auto; padding: 14px 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.card h2 { margin: 0; font-size: 1.05rem; letter-spacing: -0.01em; }
.card h3 { margin: 0; font-size: 0.95rem; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.stack.tight { gap: 6px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 140px; min-width: 0; }
.row > .grow2 { flex: 2 1 180px; }
.row > .min0 { flex: 0 1 110px; }
.row.between { justify-content: space-between; align-items: center; }
.row.between > * { flex: 0 1 auto; }
.grow { flex: 1; min-width: 0; }
.pad-top { padding-top: 12px; }

/* ─── Formularios ────────────────────────────────────────────────────── */

label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 6px;
}
label.check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  min-height: 40px;
}
label.check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  background: var(--surface);
  min-height: 46px;
  font-size: 15px;
}
input:focus, select:focus, textarea:focus { outline: 3px solid var(--accent-soft); border-color: var(--accent); }
input[readonly] { background: var(--bg); color: var(--ink-soft); }
input[type="file"] { padding: 8px; font-size: 13px; color: var(--ink-soft); }
input[type="file"]::file-selector-button {
  border: none;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-hover);
  font: inherit;
  font-weight: 700;
  padding: 8px 12px;
  margin-right: 10px;
  cursor: pointer;
}

.btn {
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  min-height: 46px;
  cursor: pointer;
  font-weight: 700;
  font-size: 14px;
  background: var(--accent-soft);
  color: var(--accent-hover);
  transition: background 0.15s, transform 0.05s;
}
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-danger { background: var(--bad-bg); color: var(--bad); }
.btn-ghost { background: var(--surface); border-color: var(--line); color: var(--ink); }
.btn-sm { padding: 8px 12px; min-height: 36px; font-size: 13px; }
.btn-block { width: 100%; }

details.soft > summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 0.9rem; padding: 4px 0; }

/* ─── Indicadores ────────────────────────────────────────────────────── */

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 11.5px;
  font-weight: 700;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-low { background: var(--warn-bg); color: var(--warn); }
.badge-out { background: var(--bad-bg); color: var(--bad); }

.tag {
  display: inline-block;
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 0.7rem;
  font-weight: 600;
  background: var(--tint);
  color: var(--ink-soft);
  vertical-align: middle;
}
.tag-ok { background: var(--ok-bg); color: var(--ok); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.stat span { display: block; font-size: 0.74rem; color: var(--ink-soft); font-weight: 600; }
.stat strong { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stat-warn strong { color: var(--warn); }
.stat-out strong { color: var(--bad); }

.alert { padding: 11px 13px; border-radius: var(--radius-sm); font-size: 0.86rem; line-height: 1.4; }
.alert ul { margin: 6px 0 0; padding-left: 18px; }
.alert-error { background: var(--bad-bg); color: var(--bad); }
.alert-ok { background: var(--ok-bg); color: var(--ok); }
.alert-warn { background: var(--warn-bg); color: #8a5a1c; }

/* ─── Inventario: tarjetas ───────────────────────────────────────────── */

.pgrid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.pcard {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 112px;
}
.pcard-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.pcard .name { font-size: 13px; flex: 1; }
.pcard-foot { font-size: 12px; color: var(--ink-soft); }
.pcard.is-inactive { opacity: 0.6; }
.empty { text-align: center; padding: 32px 12px; color: var(--ink-soft); font-size: 0.9rem; }

/* ─── Listas ─────────────────────────────────────────────────────────── */

.product-line, .count-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.product-line:last-child, .count-line:last-child { border-bottom: none; }
.product-line.as-button { width: 100%; background: none; border: none; border-bottom: 1px solid var(--line); text-align: left; cursor: pointer; }
.is-inactive .name { color: var(--ink-soft); }
.hit {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  border: 1px solid var(--line);
}

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--ink-soft); font-weight: 600; font-size: 0.74rem; }
.table tfoot th { color: var(--ink); border-bottom: none; }
.table td:nth-child(n+2), .table th:nth-child(n+2) { text-align: right; font-variant-numeric: tabular-nums; }

.more-menu { display: grid; gap: 8px; margin-top: 12px; }
.more-menu button {
  text-align: left;
  padding: 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-weight: 700;
}
.more-menu button span { font-size: 0.8rem; color: var(--ink-soft); font-weight: 500; }

.steps { margin: 0; padding-left: 20px; line-height: 1.6; font-size: 0.9rem; }

/* ─── Conteo ─────────────────────────────────────────────────────────── */

.grid-ubicaciones { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.ubicacion-tile {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  cursor: pointer;
  overflow: hidden;
  text-align: left;
  display: flex;
  flex-direction: column;
}
.ubicacion-tile.has-count { border-color: var(--accent); }
.tile-thumb { aspect-ratio: 1.3; background: var(--tint); display: grid; place-items: center; overflow: hidden; }
.tile-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile-thumb span { font-size: 1.5rem; font-weight: 700; color: var(--placeholder); }
.tile-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 2px; }
.tile-body strong { font-size: 13px; }
.tile-body span { font-size: 11.5px; color: var(--ink-soft); }
.tile-body .tile-count { color: var(--accent); font-weight: 700; }

.bleed { margin: -14px -16px 0; }
.zone-stage {
  position: relative;
  width: 100%;
  overflow: hidden;
  background: #2a2a24;
  user-select: none;
  -webkit-user-select: none;
}
.card .zone-stage, .zone-stage.rounded { border-radius: var(--radius-sm); }
.zone-stage.drawing { touch-action: none; cursor: crosshair; }
.zone-stage img { display: block; width: 100%; height: auto; pointer-events: none; }
.zone-rect {
  position: absolute;
  border: 2px dashed rgba(255, 255, 255, 0.85);
  background: rgba(30, 32, 25, 0.08);
  border-radius: 8px;
  padding: 6px;
  margin: 0;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  font: inherit;
  box-shadow: inset 0 0 0 1px rgba(30, 32, 25, 0.25);
}
.zone-rect.selected { border: 2px solid var(--accent); background: rgba(185, 118, 46, 0.22); box-shadow: none; }
.zone-rect.static { pointer-events: none; border-style: solid; border-color: var(--accent); background: rgba(185, 118, 46, 0.18); }
.zone-draft { border-style: dashed; background: rgba(255, 255, 255, 0.25); border-color: #fff; pointer-events: none; }
.zone-label {
  background: rgba(255, 255, 255, 0.94);
  color: var(--ink);
  font-size: 10.5px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.zone-rect.selected .zone-label, .zone-rect.static .zone-label { background: var(--accent); color: #fff; }

.sheet {
  position: relative;
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  margin: -14px -16px 0;
  padding: 18px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 40vh;
}
.sheet.flat { margin-top: -14px; }
.sheet-title { font-size: 14px; font-weight: 700; }

.zone-list button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 14px 0;
  border: none;
  border-bottom: 1px solid var(--line);
  background: none;
  cursor: pointer;
  font-weight: 700;
  text-align: left;
}
.zone-list button:last-child { border-bottom: none; }
.zone-item { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 6px 0; }
.zone-item input { flex: 1 1 160px; min-height: 40px; padding: 8px 12px; }
.list-sep { padding: 16px 0 4px; font-size: 12.5px; font-weight: 700; color: var(--ink-soft); border-bottom: 1px solid var(--line); }

.count-line .name { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.count-line.counted .qty-val { color: var(--accent-hover); }
.qty-control {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
  flex: none;
}
.qty-control button {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.qty-control [data-op="-1"] { color: var(--ink-soft); }
.qty-control [data-op="1"] { color: var(--accent); }
.qty-control button:active { background: var(--accent-soft); }
.qty-control .qty-val { width: auto; min-width: 34px; border-radius: 10px; font-size: 14px; color: var(--ink); font-variant-numeric: tabular-nums; }

/* ─── Resultado ──────────────────────────────────────────────────────── */

details.res-row { border-bottom: 1px solid var(--line); }
details.res-row:last-child { border-bottom: none; }
details.res-row > summary { list-style: none; display: flex; align-items: center; gap: 10px; padding: 12px 0; cursor: pointer; }
details.res-row > summary::-webkit-details-marker { display: none; }
details.res-row[open] > summary .name { color: var(--accent); }
.res-nums { display: flex; gap: 12px; flex: none; text-align: right; }
.res-nums span { display: block; font-size: 0.68rem; color: var(--ink-soft); }
.res-nums strong { font-variant-numeric: tabular-nums; }
.res-body { padding: 0 0 14px; display: flex; flex-direction: column; gap: 10px; }
.diff-pos strong, .diff-pos { color: var(--ok); }
.diff-neg strong, .diff-neg { color: var(--bad); }

/* ─── Navegación inferior ────────────────────────────────────────────── */

.nav-bottom {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  background: var(--surface);
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  padding: 10px 6px calc(10px + env(safe-area-inset-bottom));
}
.focus-mode .nav-bottom { display: none; }
.nav-bottom button {
  border: none;
  background: transparent;
  color: var(--ink-soft);
  font-size: 10.5px;
  font-weight: 600;
  padding: 2px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.nav-bottom svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-bottom button.active { color: var(--accent); font-weight: 700; }

/* ─── Ingreso ────────────────────────────────────────────────────────── */

.login-wrap { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; max-width: 420px; margin: 0 auto; }
.login-main { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 32px; }
.login-brand { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 40px; text-align: center; }
.login-brand h1 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }
.login-brand p { margin: 0; font-size: 13px; color: var(--ink-soft); }
.brand-logo { width: 56px; height: 56px; border-radius: 16px; background: var(--accent); display: grid; place-items: center; }
.brand-logo svg { width: 28px; height: 28px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form .btn { margin-top: 10px; padding: 14px; font-size: 15px; }
.login-alt { text-align: center; margin-top: 18px; font-size: 13px; color: var(--ink-soft); }
.login-alt button { border: none; background: none; padding: 0; color: var(--accent); cursor: pointer; font-weight: 600; font-size: 13px; }
.login-foot { padding: 20px 32px 28px; text-align: center; font-size: 11.5px; color: var(--ink-soft); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 11px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.86rem;
  z-index: 50;
  width: max-content;
  max-width: 92%;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.focus-mode .toast { bottom: calc(24px + env(safe-area-inset-bottom)); }
.toast-error { background: var(--bad); }

@media (min-width: 720px) {
  .pgrid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-ubicaciones { grid-template-columns: repeat(3, 1fr); }
  .app-header { padding-left: max(20px, calc(50% - 340px)); padding-right: max(20px, calc(50% - 340px)); }
  .nav-bottom { padding-left: calc(50% - 360px); padding-right: calc(50% - 360px); }
  .bleed, .sheet { margin-left: 0; margin-right: 0; }
  .bleed .zone-stage { border-radius: var(--radius); }
  .chips { margin: 0; padding: 2px 0; }
}
