:root {
  --bg: #eef6fb; --card: #fff; --text: #0b1b2b; --muted: #5b6b7c; --line: #dbe7f0;
  --pri: #0284c7; --pri2: #0369a1; --pri-soft: #e0f2fe;
  --ok: #15803d; --ok-soft: #dcfce7; --warn: #b45309; --warn-soft: #fef3c7; --bad: #b91c1c; --bad-soft: #fee2e2;
  --r: 16px; --shadow: 0 1px 2px rgba(2,32,64,.06), 0 4px 14px rgba(2,32,64,.06);
  --tab-h: 64px; --top-h: 56px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #07131f; --card: #0f2132; --text: #e6f1fa; --muted: #8fa6ba; --line: #1d3448;
    --pri: #38bdf8; --pri2: #0284c7; --pri-soft: #0c2d44;
    --ok: #4ade80; --ok-soft: #0d2e1c; --warn: #fbbf24; --warn-soft: #33270a; --bad: #f87171; --bad-soft: #3a1414;
    --shadow: none;
  }
  .btn, .qty button { color: var(--pri) !important; }
  .btn.primary, .btn.danger { color: #fff !important; }
  .btn.ok { color: var(--ok) !important; } .btn.warnb { color: var(--warn) !important; }
  .btn.ghost { color: var(--text) !important; } .btn.danger-ghost { color: var(--bad) !important; }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); min-height: 100dvh;
  padding: calc(var(--top-h) + env(safe-area-inset-top)) 0 calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px);
  -webkit-user-select: none; user-select: none;
}
input, select, textarea { -webkit-user-select: text; user-select: text; }
body.has-cart { padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 90px); }
body.noscroll { overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h3 { margin: 0 0 10px; font-size: 16px; }
small { color: var(--muted); font-size: 13px; display: block; }
.muted { color: var(--muted); } .center { text-align: center; }

/* Barra superior */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  height: calc(var(--top-h) + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 16px 0;
  display: flex; align-items: center; justify-content: space-between;
  background: linear-gradient(135deg, #0ea5e9, #0369a1); color: #fff; box-shadow: 0 2px 10px rgba(3,105,161,.25);
}
.brand { display: flex; gap: 10px; align-items: center; font-weight: 800; font-size: 18px; }
.brand img { border-radius: 7px; }
.today { opacity: .9; font-size: 14px; }
.alert-pill {
  background: rgba(255,255,255,.18); border: 1px solid rgba(255,255,255,.4); color: #fff;
  border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 700;
}
main { max-width: 720px; margin: 0 auto; padding: 16px; }

/* Barra de pestañas */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex;
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1; height: var(--tab-h); border: 0; background: none; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px; font-size: 11.5px; font-weight: 600; color: var(--muted);
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--pri); }
.tabbar button.active svg { stroke-width: 2.3; }

/* Barra de carrito */
#cartBar { position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom)); z-index: 19; padding: 0 12px 10px; pointer-events: none; }
.cartbtn {
  pointer-events: auto; max-width: 696px; margin: 0 auto; width: 100%; display: flex; align-items: center; gap: 12px;
  border: 0; border-radius: 16px; background: var(--pri2); color: #fff; padding: 14px 16px; font-size: 17px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(3,105,161,.4); animation: up .2s ease-out;
}
.cartbtn b { margin-left: auto; font-size: 18px; }
.ccount { background: #fff; color: var(--pri2); border-radius: 999px; min-width: 28px; height: 28px; display: grid; place-items: center; font-weight: 800; padding: 0 8px; }

/* Vender */
.hero { display: flex; gap: 12px; margin-bottom: 14px; }
.hero > div { flex: 1; background: var(--card); border-radius: var(--r); padding: 12px 14px; box-shadow: var(--shadow); }
.hero strong { font-size: 20px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.pcard {
  position: relative; border: 2px solid transparent; background: var(--card); border-radius: 20px;
  padding: 16px 10px 12px; display: flex; flex-direction: column; align-items: center; text-align: center; gap: 4px;
  box-shadow: var(--shadow); min-height: 165px; transition: transform .08s;
}
.pcard:active { transform: scale(.96); }
.pcard.incart { border-color: var(--pri); }
.pemoji { font-size: 44px; line-height: 1.1; }
.pemoji.sm { font-size: 30px; }
.pname { font-weight: 700; font-size: 15px; line-height: 1.2; }
.pprice { font-weight: 800; color: var(--pri); font-size: 17px; }
.pstock { font-size: 12.5px; color: var(--muted); background: var(--bg); border-radius: 999px; padding: 2px 10px; margin-top: auto; }
.pcard.st-low .pstock { background: var(--warn-soft); color: var(--warn); }
.pcard.st-out { opacity: .55; }
.pcard.st-out .pstock { background: var(--bad-soft); color: var(--bad); }
.pbadge {
  position: absolute; top: 8px; right: 8px; background: var(--pri2); color: #fff; font-weight: 800;
  border-radius: 999px; min-width: 30px; height: 30px; display: grid; place-items: center; padding: 0 7px;
}

/* Avisos y tarjetas */
.notice { background: var(--pri-soft); border-radius: 14px; padding: 12px 14px; margin-bottom: 12px; font-size: 14.5px; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.link { background: none; border: 0; color: inherit; font-weight: 800; padding: 0; text-decoration: underline; }
.card { background: var(--card); border-radius: var(--r); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.kpis.k3 { grid-template-columns: repeat(3, 1fr); }
.kpi { background: var(--card); border-radius: 14px; padding: 12px; box-shadow: var(--shadow); min-width: 0; }
.kpi b { font-size: 18px; display: block; overflow-wrap: anywhere; }
.kpi.warn b { color: var(--warn); }
.kpi.bad b { color: var(--bad); }
.kpi.main { grid-column: 1 / -1; background: linear-gradient(135deg, #0ea5e9, #0369a1); color: #fff; }
.kpi.main small { color: rgba(255,255,255,.85); }
.kpi.main b { font-size: 28px; }
.rows > div { display: flex; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.rows > div:last-child { border: 0; }
.rows .tot { font-weight: 800; }
.rows span:first-child { min-width: 0; }

/* Listas */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.list > li { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); }
.srow-main, .mrow, .vrow, .crow { display: flex; align-items: center; gap: 12px; padding: 12px 14px; }
.srow-main > div, .mrow > div, .vrow > div:first-child, .crow > div:first-child { flex: 1; min-width: 0; }
.srow-main, .vrow, .crow { cursor: pointer; }
.srow-act { display: flex; gap: 8px; padding: 0 12px 12px; }
.srow-act .btn { flex: 1; }
.srow.inactive { opacity: .55; }
.stockval { font-size: 26px; font-weight: 800; min-width: 56px; text-align: right; }
.sv-ok { color: var(--ok); } .sv-low { color: var(--warn); } .sv-out { color: var(--bad); }
.micon { font-size: 22px; }
.mcant { font-weight: 800; font-size: 17px; white-space: nowrap; }
.pos { color: var(--ok); } .neg { color: var(--bad); }
.vr { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; white-space: nowrap; }
.vrow.anulada b { text-decoration: line-through; opacity: .6; }
.trunc { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { font-size: 11.5px; font-weight: 700; border-radius: 999px; padding: 2px 8px; background: var(--pri-soft); color: var(--pri); }
.m-efectivo { background: var(--ok-soft); color: var(--ok); }
.m-fiado { background: var(--warn-soft); color: var(--warn); }
.pill.anul { background: var(--bad-soft); color: var(--bad); }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .e-ico { font-size: 44px; }
.empty b { display: block; color: var(--text); margin: 6px 0 2px; font-size: 17px; }
.section-title { display: flex; justify-content: space-between; align-items: center; margin: 18px 2px 10px; }
.section-title h3 { margin: 0; }

/* Botones */
.btn {
  border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 700; background: var(--pri-soft); color: var(--pri2);
  min-height: 46px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
}
.btn.primary { background: var(--pri2); color: #fff; }
.btn.ghost { background: transparent; border: 1.5px solid var(--line); color: var(--text); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.danger-ghost { background: transparent; color: var(--bad); border: 1.5px solid var(--bad-soft); }
.btn.ok { background: var(--ok-soft); color: var(--ok); }
.btn.warnb { background: var(--warn-soft); color: var(--warn); }
.btn.sm { padding: 8px 8px; min-height: 40px; font-size: 14px; }
.btn.block { display: flex; width: 100%; margin: 10px 0; }
.btn.big { font-size: 17px; min-height: 54px; }
.btn:active { filter: brightness(.92); }
.btn:disabled { opacity: .5; }
.actions { display: flex; gap: 10px; margin-top: 16px; }
.actions .btn { flex: 1; }
.actions .btn.big { flex: 2; }
.actions.col { flex-direction: column; }

/* Chips y segmentos */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { position: relative; border: 1.5px solid var(--line); background: var(--card); border-radius: 999px; padding: 9px 14px; font-weight: 600; font-size: 14.5px; }
.chip.on, .chip:has(input:checked) { background: var(--pri2); border-color: var(--pri2); color: #fff; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip.emo { font-size: 22px; padding: 5px 10px; }
.chips.quick { margin-top: 8px; }
.periodos { flex-wrap: nowrap; overflow-x: auto; margin: 0 -16px 14px; padding: 0 16px 2px; scrollbar-width: none; }
.periodos .chip { white-space: nowrap; }
.seg { display: flex; background: var(--card); border-radius: 12px; padding: 4px; margin-bottom: 14px; box-shadow: var(--shadow); }
.seg button { flex: 1; border: 0; background: none; padding: 10px; border-radius: 9px; font-weight: 700; color: var(--muted); }
.seg button.on { background: var(--pri2); color: #fff; }

/* Formularios */
.field { display: block; margin: 12px 0; }
.field > span { display: block; font-size: 13.5px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .search input {
  width: 100%; border: 1.5px solid var(--line); background: var(--bg); border-radius: 12px;
  padding: 12px 14px; font-size: 16px; min-height: 48px;
}
.field input:focus, .field select:focus, .search input:focus { outline: none; border-color: var(--pri); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row2 .field { margin: 6px 0; }
.check { display: flex; align-items: center; gap: 10px; margin: 14px 0; font-weight: 600; }
.check input { width: 22px; height: 22px; accent-color: var(--pri2); flex: none; }
.search { margin: 0 0 12px; }
.hint { font-size: 14px; color: var(--muted); margin: 8px 0; }
.hint.warn { color: var(--warn); font-weight: 600; }

/* Hoja inferior (modal) */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; }
.sheet-wrap[hidden] { display: none; }
.backdrop { position: absolute; inset: 0; background: rgba(2,16,30,.5); animation: fade .2s; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; max-width: 600px; margin: 0 auto; max-height: 92dvh; overflow: auto;
  background: var(--card); border-radius: 22px 22px 0 0; padding: 8px 18px calc(20px + env(safe-area-inset-bottom));
  animation: up .22s ease-out; overscroll-behavior: contain;
}
.sheet::before { content: ""; display: block; width: 40px; height: 5px; border-radius: 9px; background: var(--line); margin: 4px auto 6px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; position: sticky; top: -8px; background: var(--card); padding: 6px 0; z-index: 1; }
.sheet-head h2 { margin: 0; font-size: 20px; }
.x { border: 0; background: var(--bg); width: 40px; height: 40px; border-radius: 50%; font-size: 16px; flex: none; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

/* Carrito */
.cart { list-style: none; padding: 0; margin: 8px 0; }
.cart li { display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; padding: 12px 0; border-bottom: 1px solid var(--line); align-items: center; }
.ci-info b { display: block; }
.ci-sub { text-align: right; font-size: 17px; }
.qty { grid-column: 1 / -1; justify-self: end; display: flex; align-items: center; gap: 6px; }
.qty button { width: 46px; height: 44px; border-radius: 12px; border: 0; background: var(--pri-soft); color: var(--pri2); font-size: 22px; font-weight: 800; }
.qty span { min-width: 42px; text-align: center; font-weight: 800; font-size: 19px; }
.qty .del { background: var(--bad-soft); color: var(--bad) !important; font-size: 18px; margin-right: auto; }
.total-row { display: flex; justify-content: space-between; align-items: baseline; padding: 14px 0 4px; font-size: 18px; }
.total-row strong { font-size: 30px; color: var(--pri); }
.vuelto { margin-top: 10px; padding: 12px 14px; border-radius: 12px; background: var(--ok-soft); color: var(--ok); font-weight: 700; font-size: 18px; }
.vuelto.neg { background: var(--bad-soft); color: var(--bad); }
.vuelto:empty { display: none; }
.done { text-align: center; padding: 6px 0 4px; }
.done .ico { font-size: 60px; }
.done h2 { margin: 4px 0; }
.done .tot { font-size: 32px; font-weight: 800; color: var(--pri); }
.saldo-big { font-size: 32px; font-weight: 800; margin: 2px 0 12px; }

/* Aviso flotante */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 96px);
  transform: translate(-50%, 20px); background: #0b1b2b; color: #fff; padding: 12px 18px; border-radius: 12px;
  font-weight: 600; opacity: 0; pointer-events: none; transition: .2s; z-index: 60; width: max-content; max-width: 90vw;
  text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: #b91c1c; } #toast.ok { background: #15803d; }

@media (min-width: 720px) { .grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); } }

/* Acceso (login / configuración inicial) */
body.auth .tabbar, body.auth #cartBar { display: none; }
body.auth { padding-bottom: 24px; }
.auth-card { max-width: 420px; margin: 24px auto; padding: 24px 20px; }
.auth-logo { display: block; margin: 0 auto 8px; border-radius: 16px; }
.auth-card h2 { margin: 6px 0 4px; }
.clickable { cursor: pointer; }
a.btn { text-decoration: none; }
