:root {
  --bg: #f5f5f2;
  --surface: #ffffff;
  --surface-2: #f0efeb;
  --text: #1d1d1b;
  --muted: #6b6a66;
  --line: #e2e0da;
  --accent: #d9480f;
  --accent-ink: #ffffff;
  --accent-soft: #fdebe1;
  --ok: #2b8a3e;
  --ok-soft: #e3f4e6;
  --warn: #b35c00;
  --warn-soft: #fff1d6;
  --bad: #c92a2a;
  --bad-soft: #fde4e4;
  --info: #1864ab;
  --info-soft: #e1ecf8;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151514;
    --surface: #1f1f1d;
    --surface-2: #2a2a27;
    --text: #efeee9;
    --muted: #a4a29b;
    --line: #34332f;
    --accent: #ff7a3d;
    --accent-ink: #1a0b03;
    --accent-soft: #3b2216;
    --ok: #69db7c;
    --ok-soft: #1f3324;
    --warn: #ffc163;
    --warn-soft: #3a2d14;
    --bad: #ff8787;
    --bad-soft: #3d1f1f;
    --info: #74b4f5;
    --info-soft: #182a3d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }

/* ---------- barra superior */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: .75rem;
  padding: .6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; text-decoration: none; color: var(--text); }
.brand img { width: 28px; height: 28px; border-radius: 7px; }
.topbar .spacer { flex: 1; }
.chip-sync { font-size: .8rem; padding: .2rem .55rem; border-radius: 999px; background: var(--warn-soft); color: var(--warn); border: 0; cursor: pointer; }
.chip-off { background: var(--bad-soft); color: var(--bad); }
.user-btn { background: none; border: 1px solid var(--line); border-radius: 999px; padding: .3rem .75rem; cursor: pointer; font-size: .9rem; }

.tabs {
  display: flex; gap: .25rem; overflow-x: auto; scrollbar-width: none;
  padding: .5rem 1rem 0; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 53px; z-index: 19;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: none; padding: .55rem .85rem; text-decoration: none; color: var(--muted);
  border-bottom: 2px solid transparent; font-weight: 600; font-size: .95rem;
}
.tabs a.on { color: var(--text); border-color: var(--accent); }

main { max-width: 1180px; margin: 0 auto; padding: 1rem; }
main.movil { max-width: 560px; }

/* ---------- tarjetas y grillas */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); }
.card + .card { margin-top: 1rem; }
.grid { display: grid; gap: 1rem; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 1rem; }
.two { grid-template-columns: 1fr; }
@media (min-width: 900px) { .two { grid-template-columns: 1fr 1fr; } }
.kpi .label { color: var(--muted); font-size: .85rem; }
.kpi .value { font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: .15rem; }
.kpi .sub { color: var(--muted); font-size: .8rem; margin-top: .15rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: .75rem; }

/* ---------- botones */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: .55rem .95rem; cursor: pointer; font-weight: 600; text-decoration: none; color: var(--text);
  min-height: 42px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--bad); }
.btn.sm { min-height: 32px; padding: .25rem .6rem; font-size: .85rem; border-radius: 8px; }
.btn.block { width: 100%; }
.btn.xl { min-height: 64px; font-size: 1.2rem; border-radius: 14px; }
.btn[disabled] { opacity: .55; cursor: default; }
.linkbtn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font-weight: 600; }

/* ---------- formularios */
label.f { display: block; }
label.f > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .25rem; font-weight: 600; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; padding: .6rem .7rem; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  min-height: 44px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.filters { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; margin-bottom: 1rem; }
.check { display: flex; gap: .5rem; align-items: center; }
.check input { width: 20px; height: 20px; }
.form-error { color: var(--bad); font-size: .9rem; min-height: 1.2em; }

/* ---------- tablas */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { padding: .55rem .7rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--surface-2); position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
td.pos { color: var(--ok); }
td.neg { color: var(--bad); }

/* ---------- etiquetas de estado */
.badge { display: inline-block; padding: .12rem .55rem; border-radius: 999px; font-size: .78rem; font-weight: 700; white-space: nowrap; }
.b-ok { background: var(--ok-soft); color: var(--ok); }
.b-reponer { background: var(--warn-soft); color: var(--warn); }
.b-bajo, .b-neg { background: var(--bad-soft); color: var(--bad); }
.b-sin { background: var(--surface-2); color: var(--muted); }
.b-info { background: var(--info-soft); color: var(--info); }

/* ---------- login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login { width: 100%; max-width: 380px; }
.login .logo { width: 56px; height: 56px; border-radius: 14px; display: block; margin-bottom: .75rem; }

/* ---------- app móvil */
.home-grid { display: grid; gap: .75rem; }
.tile {
  display: flex; flex-direction: column; justify-content: center; gap: .15rem;
  min-height: 84px; padding: 1rem 1.1rem; border-radius: 16px; border: 1px solid var(--line);
  background: var(--surface); text-decoration: none; color: var(--text); cursor: pointer; text-align: left;
  box-shadow: var(--shadow); width: 100%;
}
.tile strong { font-size: 1.2rem; }
.tile span { color: var(--muted); font-size: .9rem; }
.tile.main { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tile.main span { color: var(--accent-ink); opacity: .85; }

.tipos { display: grid; grid-template-columns: 1fr 1fr; gap: .65rem; }
.tipo {
  min-height: 92px; border-radius: 16px; border: 1px solid var(--line); background: var(--surface); cursor: pointer;
  font-weight: 700; font-size: 1.05rem; padding: .8rem; text-align: left; display: flex; flex-direction: column; justify-content: space-between;
  border-left: 6px solid var(--muted);
}
.tipo small { font-weight: 500; color: var(--muted); font-size: .8rem; }
.tipo.t-in { border-left-color: var(--ok); }
.tipo.t-out { border-left-color: var(--accent); }
.tipo.t-ruta { border-left-color: var(--info); }
.tipo.t-merma { border-left-color: var(--bad); }
.tipo.t-adj { border-left-color: var(--warn); }

.step-head { display: flex; align-items: center; gap: .5rem; margin-bottom: .75rem; }
.step-head .back { border: 0; background: var(--surface-2); border-radius: 10px; min-width: 44px; min-height: 44px; cursor: pointer; font-size: 1.2rem; }
.step-head .crumb { font-size: .85rem; color: var(--muted); }
.step-head .title { font-weight: 700; font-size: 1.1rem; }

.search-row { display: flex; gap: .5rem; margin-bottom: .6rem; position: sticky; top: 60px; z-index: 5; background: var(--bg); padding: .25rem 0; }
.search-row input { min-height: 52px; font-size: 1.05rem; }
.search-row .btn { min-height: 52px; }
.chips { display: flex; gap: .4rem; overflow-x: auto; margin-bottom: .6rem; scrollbar-width: none; }
.chips button { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: .35rem .8rem; cursor: pointer; font-size: .9rem; }
.chips button.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.items { display: grid; gap: .5rem; }
.item {
  display: flex; justify-content: space-between; align-items: center; gap: .75rem;
  min-height: 62px; padding: .7rem .9rem; border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; text-align: left; width: 100%;
}
.item .name { font-weight: 650; }
.item .meta { color: var(--muted); font-size: .82rem; }
.item .qty { font-weight: 700; font-size: 1.15rem; font-variant-numeric: tabular-nums; text-align: right; }

.qty-box { display: grid; grid-template-columns: 64px 1fr 64px; gap: .5rem; align-items: stretch; }
.qty-box button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 14px; font-size: 1.8rem; cursor: pointer; }
.qty-box input { text-align: center; font-size: 2rem; font-weight: 700; min-height: 72px; border-radius: 14px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); min-height: 48px; cursor: pointer; font-weight: 700; }
.seg button.on { background: var(--text); color: var(--bg); }
.photo-prev { max-width: 100%; max-height: 220px; border-radius: 12px; display: block; margin-top: .5rem; }
.warn-box { background: var(--warn-soft); color: var(--warn); padding: .6rem .8rem; border-radius: 10px; font-size: .9rem; }
.info-box { background: var(--info-soft); color: var(--info); padding: .6rem .8rem; border-radius: 10px; font-size: .9rem; }

/* ---------- escáner */
.scanner { position: fixed; inset: 0; background: #000; z-index: 50; display: flex; flex-direction: column; }
.scanner video { flex: 1; width: 100%; object-fit: cover; }
.scanner .bar { padding: 1rem; display: flex; justify-content: space-between; align-items: center; color: #fff; background: rgba(0,0,0,.6); }

/* ---------- toast y modal */
.toast {
  position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: .75rem 1.1rem; border-radius: 12px; font-weight: 600;
  box-shadow: 0 8px 30px rgba(0,0,0,.25); max-width: calc(100% - 2rem);
}
.toast.err { background: var(--bad); color: #fff; }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: grid; place-items: center; padding: 1rem; }
.modal { background: var(--surface); border-radius: var(--radius); width: 100%; max-width: 520px; max-height: calc(100vh - 2rem); overflow: auto; padding: 1.25rem; }
.modal h2 { margin-bottom: 1rem; }
.modal .acts { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }
.secret { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 1.3rem; padding: .7rem; background: var(--surface-2); border-radius: 10px; text-align: center; user-select: all; letter-spacing: .05em; }

.list-plain { list-style: none; padding: 0; margin: 0; }
.list-plain li { padding: .55rem 0; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: .75rem; }
.list-plain li:last-child { border-bottom: 0; }
.empty { color: var(--muted); padding: 1rem 0; text-align: center; }
.pager { display: flex; gap: .5rem; align-items: center; justify-content: flex-end; margin-top: .75rem; }

@media (max-width: 640px) {
  main { padding: .75rem; }
  .hide-sm { display: none; }
  th, td { padding: .5rem; }
}

/* ---------- etapa 1b: bodegas, ubicaciones y rutas */
.chips.wrap { flex-wrap: wrap; overflow: visible; }
.bodega-sel { margin-bottom: .75rem; }
.bodega-sel .chips button { font-weight: 700; padding: .5rem .9rem; }
