/* =========================================================
   Stockpan · Mobile-first · Crema & Negro · Claro / Oscuro
   ========================================================= */
:root {
  --bg: #F4EDE0;          /* crema */
  --bg-2: #EBE2D0;
  --surface: #FBF7EF;
  --surface-2: #F1E9DA;
  --ink: #141210;         /* negro cálido */
  --ink-2: #4A443B;
  --ink-3: #8A8173;
  --line: #DDD2BE;
  --accent: #141210;      /* botón primario negro */
  --on-accent: #F4EDE0;
  --gold: #B8893E;
  --ok: #2F7D4F;   --ok-bg: #DCEBDD;
  --warn: #9A6B00; --warn-bg: #F6E7B8;
  --crit: #B23A2B; --crit-bg: #F5D5CE;
  --info: #2D5D8A; --info-bg: #DCE6F1;
  --shadow: 0 1px 2px rgba(20,18,16,.06), 0 8px 24px rgba(20,18,16,.08);
  --radius: 20px;
  --radius-sm: 14px;
  --pill: 999px;
  --nav-h: 68px;
  --top-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: 'DM Sans', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --display: 'Fraunces', Georgia, serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11100E; --bg-2: #181613; --surface: #1C1A17; --surface-2: #24211D;
    --ink: #F4EDE0; --ink-2: #CFC6B6; --ink-3: #8F8677; --line: #2F2B26;
    --accent: #F4EDE0; --on-accent: #141210; --gold: #D9AE68;
    --ok: #7FC79A; --ok-bg: #1D2E23; --warn: #E7C25E; --warn-bg: #33291A;
    --crit: #F08E7E; --crit-bg: #3A1F1B; --info: #8FB6E0; --info-bg: #1B2633;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 30px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11100E; --bg-2: #181613; --surface: #1C1A17; --surface-2: #24211D;
  --ink: #F4EDE0; --ink-2: #CFC6B6; --ink-3: #8F8677; --line: #2F2B26;
  --accent: #F4EDE0; --on-accent: #141210; --gold: #D9AE68;
  --ok: #7FC79A; --ok-bg: #1D2E23; --warn: #E7C25E; --warn-bg: #33291A;
  --crit: #F08E7E; --crit-bg: #3A1F1B; --info: #8FB6E0; --info-bg: #1B2633;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 30px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 16px; line-height: 1.45; }
body { min-height: 100dvh; overscroll-behavior-y: none; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.15; }
h1 { font-family: var(--display); font-size: 1.7rem; letter-spacing: -.01em; }
h2 { font-family: var(--display); font-size: 1.25rem; }
h3 { font-size: 1rem; font-weight: 700; }
.muted { color: var(--ink-3); }
.small { font-size: .82rem; }
.mono { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 40; height: calc(var(--top-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 16px 0; display: flex; align-items: center; gap: 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--display); font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.brand img { width: 30px; height: 30px; border-radius: 9px; }
.brand span { display: none; }
.search-trigger {
  flex: 1; min-width: 0; height: 42px; display: flex; align-items: center; gap: 8px; padding: 0 14px;
  border-radius: var(--pill); border: 1px solid var(--line); background: var(--surface); color: var(--ink-3);
  cursor: text; text-align: left;
}
.search-trigger span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-trigger kbd { margin-left: auto; display: none; }
.icon-btn {
  width: 42px; height: 42px; flex: 0 0 42px; display: grid; place-items: center; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.icon-btn:active { transform: scale(.94); }
svg.i { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

main {
  padding: calc(var(--top-h) + env(safe-area-inset-top, 0px) + 16px) 16px calc(var(--nav-h) + var(--safe-b) + 28px);
  max-width: 1180px; margin: 0 auto;
}

/* Barra de menú fija inferior (móvil) */
.navbar {
  position: fixed; inset: auto 0 0 0; z-index: 40; height: calc(var(--nav-h) + var(--safe-b)); padding: 8px 10px calc(8px + var(--safe-b));
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px;
  background: var(--ink); color: var(--bg); border-top-left-radius: 24px; border-top-right-radius: 24px;
  box-shadow: 0 -6px 24px rgba(20,18,16,.18);
}
:root[data-theme="dark"] .navbar { background: var(--surface); color: var(--ink); border-top: 1px solid var(--line); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .navbar { background: var(--surface); color: var(--ink); border-top: 1px solid var(--line); } }
.navbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border-radius: 18px;
  text-decoration: none; font-size: .7rem; font-weight: 600; opacity: .62; position: relative;
}
.navbar a.active { opacity: 1; background: color-mix(in srgb, currentColor 13%, transparent); }
.navbar a .badge { position: absolute; top: 4px; right: calc(50% - 22px); }

/* ---------- Componentes ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 46px; padding: 0 20px;
  border-radius: var(--pill); border: 1px solid transparent; background: var(--accent); color: var(--on-accent);
  font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap; transition: transform .08s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.soft { background: var(--surface-2); color: var(--ink); }
.btn.danger { background: var(--crit); color: #fff; }
.btn.ok { background: var(--ok); color: #fff; }
.btn.wa { background: #1F8F4E; color: #fff; }
.btn.sm { min-height: 36px; padding: 0 14px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn svg.i { width: 18px; height: 18px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 12px; }
.card.flat { box-shadow: none; }
.card.tap { cursor: pointer; transition: transform .08s; }
.card.tap:active { transform: scale(.99); }
.stack { display: grid; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.section-head { display: flex; align-items: end; justify-content: space-between; gap: 12px; margin: 26px 0 12px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.page-head p { margin: 4px 0 0; }
.grid-2 { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.list { display: grid; gap: 10px; }
.divider { height: 1px; background: var(--line); margin: 14px 0; }

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.kpi b { display: block; font-family: var(--display); font-size: 1.9rem; line-height: 1; margin-top: 6px; }
.kpi.dark { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.kpi.dark .muted { color: color-mix(in srgb, var(--bg) 65%, transparent); }

.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: var(--pill); font-size: .75rem; font-weight: 700; white-space: nowrap; background: var(--surface-2); color: var(--ink-2); }
.pill::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: .9; }
.pill.normal, .pill.conectada, .pill.enviado, .pill.recibido, .pill.finalizada, .pill.confirmado, .pill.procesado { background: var(--ok-bg); color: var(--ok); }
.pill.bajo, .pill.pendiente, .pill.qr, .pill.conectando, .pill.enviado-r, .pill.planificada, .pill.despachado, .pill.procesando { background: var(--warn-bg); color: var(--warn); }
.pill.critico, .pill.fallido, .pill.desconectada, .pill.error, .pill.rechazado, .pill.urgente, .pill.anulada { background: var(--crit-bg); color: var(--crit); }
.pill.info, .pill.creada, .pill.manual, .pill.respuesta { background: var(--info-bg); color: var(--info); }
.pill.plain::before { display: none; }
.badge { min-width: 18px; height: 18px; padding: 0 5px; border-radius: var(--pill); background: var(--crit); color: #fff; font-size: .68rem; font-weight: 700; display: inline-grid; place-items: center; }

/* Chips de filtro: scroll horizontal en móvil */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 16px 6px; margin: 0 -16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; height: 38px; padding: 0 16px; border-radius: var(--pill); border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; font-size: .88rem; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }

/* Segmentos (tabs internas) */
.seg { display: flex; gap: 4px; padding: 4px; border-radius: var(--pill); background: var(--surface-2); overflow-x: auto; scrollbar-width: none; margin-bottom: 16px; }
.seg button { flex: 1 0 auto; height: 38px; padding: 0 14px; border: 0; border-radius: var(--pill); background: transparent; font-weight: 600; font-size: .9rem; color: var(--ink-2); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

/* Formularios */
label.f { display: grid; gap: 6px; font-size: .85rem; font-weight: 600; color: var(--ink-2); }
.input, select.input, textarea.input {
  width: 100%; min-height: 48px; padding: 12px 16px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); outline: none; font-size: 16px; /* 16px evita zoom en iOS */
}
textarea.input { min-height: 110px; resize: vertical; border-radius: var(--radius-sm); }
.input:focus { border-color: var(--ink); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ink) 12%, transparent); }
.input.round { border-radius: var(--pill); }
.check { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: .92rem; }
.check input { width: 22px; height: 22px; accent-color: var(--ink); }

/* Ítems de stock */
.item-card { display: grid; gap: 10px; }
.item-card .top { display: flex; gap: 12px; align-items: flex-start; }
.cat-dot { width: 40px; height: 40px; flex: 0 0 40px; border-radius: 14px; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: .8rem; }
.item-card .name { font-weight: 700; line-height: 1.25; }
.item-card .qty { font-family: var(--display); font-size: 1.35rem; text-align: right; white-space: nowrap; }
.wh-rows { display: grid; gap: 6px; }
.wh-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; font-size: .86rem; }
.bar { height: 6px; border-radius: 6px; background: var(--surface-2); overflow: hidden; grid-column: 1 / -1; }
.bar > i { display: block; height: 100%; border-radius: 6px; background: var(--ok); }
.bar > i.bajo { background: var(--warn); } .bar > i.critico { background: var(--crit); }

table.t { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.t th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-3); font-weight: 700; padding: 8px 6px; border-bottom: 1px solid var(--line); }
table.t td { padding: 10px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table-wrap { overflow-x: auto; margin: 0 -4px; }

/* Bottom sheet / modal */
.sheet-backdrop { position: fixed; inset: 0; z-index: 60; background: rgba(10,9,8,.45); backdrop-filter: blur(2px); animation: fade .15s; }
.sheet {
  position: fixed; inset: auto 0 0 0; z-index: 61; max-height: 92dvh; overflow-y: auto; background: var(--bg);
  border-radius: 28px 28px 0 0; padding: 10px 16px calc(24px + var(--safe-b)); animation: up .22s cubic-bezier(.2,.8,.2,1);
}
.sheet .grab { width: 44px; height: 5px; border-radius: 5px; background: var(--line); margin: 0 auto 12px; }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; position: sticky; top: -10px; background: var(--bg); padding: 6px 0; z-index: 1; }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }

/* Buscador global */
.search-overlay { position: fixed; inset: 0; z-index: 70; background: var(--bg); display: flex; flex-direction: column; animation: fade .12s; }
.search-overlay .bar-top { display: flex; gap: 10px; align-items: center; padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; border-bottom: 1px solid var(--line); }
.search-overlay .results { flex: 1; overflow-y: auto; padding: 8px 16px 40px; }
.search-overlay .filters { padding: 10px 16px 4px; }
.result { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: var(--radius-sm); cursor: pointer; }
.result:hover, .result.sel { background: var(--surface); }
.result .meta { font-size: .8rem; color: var(--ink-3); }
.group-title { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-3); font-weight: 700; margin: 16px 12px 4px; }
mark { background: color-mix(in srgb, var(--gold) 35%, transparent); color: inherit; border-radius: 4px; padding: 0 1px; }

/* WhatsApp */
.wa-bubble { background: #DCF8C6; color: #111; border-radius: 16px 16px 4px 16px; padding: 10px 12px; white-space: pre-wrap; font-size: .9rem; box-shadow: var(--shadow); }
:root[data-theme="dark"] .wa-bubble { background: #144D37; color: #E9F5EC; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .wa-bubble { background: #144D37; color: #E9F5EC; } }
.qr-box { display: grid; place-items: center; background: #fff; border-radius: var(--radius); padding: 14px; }
.qr-box img { width: min(280px, 70vw); height: auto; image-rendering: pixelated; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--ink-3); flex: 0 0 10px; }
.status-dot.conectada { background: var(--ok); box-shadow: 0 0 0 4px var(--ok-bg); }
.status-dot.qr, .status-dot.conectando { background: var(--warn); }
.status-dot.desconectada, .status-dot.error { background: var(--crit); }

/* Toast */
#toast-root { position: fixed; z-index: 90; left: 16px; right: 16px; bottom: calc(var(--nav-h) + var(--safe-b) + 14px); display: grid; gap: 8px; pointer-events: none; }
.toast { pointer-events: auto; background: var(--ink); color: var(--bg); padding: 12px 16px; border-radius: var(--pill); font-weight: 600; box-shadow: var(--shadow); animation: up .2s; text-align: center; }
.toast.err { background: var(--crit); color: #fff; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 400px; padding: 28px 22px; }
.login .logo { width: 64px; height: 64px; border-radius: 20px; margin-bottom: 18px; }

.empty { text-align: center; padding: 36px 16px; color: var(--ink-3); }
.empty svg.i { width: 36px; height: 36px; margin-bottom: 8px; }
.skeleton { height: 86px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }
.fab { position: fixed; right: 16px; bottom: calc(var(--nav-h) + var(--safe-b) + 16px); z-index: 35; width: 58px; height: 58px; border-radius: 50%; box-shadow: var(--shadow); padding: 0; }

/* ---------- Tablet / escritorio: la barra fija pasa a lateral ---------- */
@media (min-width: 560px) {
  .brand span { display: inline; }
  .kpis { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 900px) {
  :root { --nav-h: 0px; }
  .topbar { left: 232px; }
  .navbar {
    inset: 0 auto 0 0; width: 232px; height: 100dvh; padding: 20px 14px; border-radius: 0 28px 28px 0;
    grid-template-columns: 1fr; grid-auto-rows: 52px; align-content: start; gap: 6px;
  }
  .navbar::before { content: 'Stockpan'; font-family: var(--display); font-size: 1.5rem; font-weight: 700; padding: 4px 14px 22px; }
  .navbar a { flex-direction: row; justify-content: flex-start; gap: 12px; padding: 0 16px; font-size: .95rem; }
  .navbar a .badge { position: static; margin-left: auto; }
  main { margin-left: 232px; padding-bottom: 40px; }
  .brand { display: none; }
  .search-trigger { max-width: 560px; }
  .search-trigger kbd { display: inline; font-size: .75rem; border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }
  .sheet { inset: 0 0 0 auto; width: 480px; max-height: none; border-radius: 28px 0 0 28px; }
  .sheet .grab { display: none; }
  .search-overlay { inset: 8vh 0 auto 0; margin: 0 auto; width: min(720px, 92vw); max-height: 80vh; border-radius: 24px; border: 1px solid var(--line); box-shadow: var(--shadow); }
  .search-overlay::before { content: ''; position: fixed; inset: 0; z-index: -1; background: rgba(10,9,8,.45); }
  #toast-root { left: auto; right: 24px; bottom: 24px; width: 360px; }
  .fab { bottom: 28px; right: 28px; }
  .cols-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; align-items: start; }
  .cards-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
  .cards-grid .card + .card { margin-top: 0; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
