/* =====================================================
   apSales — CSS moderno (Grid/Flexbox), mobile-first PWA
   ===================================================== */
:root {
  --brand: #0e7490;
  --brand-2: #0891b2;
  --brand-3: #22d3ee;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --ink: #0f172a;
  --mut: #64748b;
  --line: #e2e8f0;
  --ok: #059669;
  --warn: #d97706;
  --bad: #dc2626;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(15, 23, 42, .08);
  --sidebar-w: 232px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg); color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.mut { color: var(--mut); }
.small { font-size: .8rem; }

/* ---------- Login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center;
  background: linear-gradient(160deg, #0f172a, #164e63 70%, #0e7490); padding: 1rem; }
.login-card { background: var(--surface); border-radius: 20px; padding: 2rem;
  width: min(400px, 100%); text-align: center; box-shadow: 0 25px 60px rgba(0,0,0,.45); }
.login-card h1 { margin: .6rem 0 .2rem; color: var(--brand); }
.login-card form { text-align: left; margin-top: 1.2rem; }
.login-card label { display: block; font-size: .85rem; color: var(--mut); margin-bottom: .8rem; }
.login-card input { width: 100%; margin-top: .3rem; padding: .7rem .8rem;
  border: 1px solid var(--line); border-radius: 10px; font-size: 1rem; }
.login-msg { min-height: 1.2em; color: var(--bad); font-size: .9rem; margin: .4rem 0 0; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100dvh; }
.sidebar { background: #0f172a; color: #e2e8f0; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100dvh; }
.brand { display: flex; align-items: center; gap: .6rem; padding: 1.1rem 1.2rem;
  font-size: 1.25rem; color: #fff; border-bottom: 1px solid #1e293b; }
.brand b { color: var(--brand-3); }
#nav { display: flex; flex-direction: column; padding: .6rem; gap: .15rem; flex: 1; overflow-y: auto; }
#nav a { display: flex; gap: .6rem; align-items: center; color: #cbd5e1; text-decoration: none;
  padding: .65rem .9rem; border-radius: 10px; font-size: .95rem; }
#nav a:hover { background: #1e293b; color: #fff; }
#nav a.active { background: var(--brand); color: #fff; font-weight: 600; }
.side-foot { padding: 1rem 1.2rem; border-top: 1px solid #1e293b; }
.side-foot .mut { color: #64748b; }

.main-wrap { display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: .8rem; padding: .7rem 1.2rem;
  background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 30; }
.topbar h2 { margin: 0; font-size: 1.05rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
#burger { display: none; }

.user-chip { display: flex; align-items: center; gap: .4rem; background: var(--bg);
  padding: .3rem .5rem .3rem .8rem; border-radius: 999px; font-size: .85rem; font-weight: 600; }

.badge { font-size: .78rem; padding: .3rem .6rem; border-radius: 999px; background: #e2e8f0; color: var(--mut); white-space: nowrap; }
.badge.on { background: #d1fae5; color: var(--ok); }
.badge.off { background: #fee2e2; color: var(--bad); }
.badge.pending { background: #fef3c7; color: var(--warn); font-weight: 700; }

.view { padding: 1.25rem; flex: 1; }

/* ---------- Componentes ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .9rem; margin-bottom: 1.25rem; }
.card { background: var(--surface); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.card .kpi { font-size: 1.5rem; font-weight: 800; margin: .15rem 0; }
.card .lbl { font-size: .78rem; color: var(--mut); text-transform: uppercase; letter-spacing: .04em; }
.card.ok .kpi { color: var(--ok); } .card.warn .kpi { color: var(--warn); } .card.bad .kpi { color: var(--bad); }

.toolbar { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.toolbar input[type=search], .toolbar input[type=date], .toolbar select {
  padding: .55rem .8rem; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); font-size: .95rem; }
.toolbar .grow { flex: 1; min-width: 160px; }

.btn { border: 0; border-radius: 10px; padding: .6rem 1rem; font-weight: 600; font-size: .92rem;
  background: var(--brand); color: #fff; cursor: pointer; }
.btn:hover { background: var(--brand-2); }
.btn.ghost { background: var(--surface); color: var(--brand); border: 1px solid var(--line); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { background: var(--bad); } .btn.ok { background: var(--ok); }
.btn.small { padding: .4rem .7rem; font-size: .82rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; font-size: 1.15rem; cursor: pointer; padding: .35rem .5rem; border-radius: 8px; }
.icon-btn:hover { background: var(--bg); }

.table-wrap { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { padding: .65rem .9rem; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--mut); background: #f8fafc; position: sticky; top: 0; }
tbody tr:hover { background: #f8fafc; cursor: pointer; }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.tag { display: inline-block; padding: .2rem .55rem; border-radius: 999px; font-size: .75rem; font-weight: 700; }
.tag.pagada    { background: #d1fae5; color: var(--ok); }
.tag.pendiente { background: #fef3c7; color: var(--warn); }
.tag.anulada   { background: #fee2e2; color: var(--bad); }
.tag.low       { background: #fee2e2; color: var(--bad); }
.tag.ok-stock  { background: #d1fae5; color: var(--ok); }
.tag.nuevo     { background: #dbeafe; color: #1d4ed8; }
.tag.frecuente { background: #e0e7ff; color: #4338ca; }
.tag.corporativo { background: #fce7f3; color: #be185d; }
.dot { display: inline-block; width: .65rem; height: .65rem; border-radius: 50%; margin-right: .35rem; }

.empty { text-align: center; padding: 2.5rem 1rem; color: var(--mut); }

/* ---------- POS ---------- */
.pos { display: grid; grid-template-columns: 1.2fr .8fr; gap: 1rem; align-items: start; }
.pos-list { max-height: 62vh; overflow-y: auto; }
.prod-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .7rem; }
.prod-card { background: var(--surface); border-radius: 12px; padding: .8rem; box-shadow: var(--shadow);
  cursor: pointer; border: 2px solid transparent; transition: transform .08s ease; }
.prod-card:hover { border-color: var(--brand-3); transform: translateY(-2px); }
.prod-card .pname { font-weight: 600; font-size: .88rem; min-height: 2.4em; }
.prod-card .pprice { color: var(--brand); font-weight: 800; margin-top: .3rem; }
.prod-card .pstock { font-size: .75rem; color: var(--mut); }
.cart { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; position: sticky; top: 70px; }
.cart h3 { margin: 0 0 .6rem; }
.cart-row { display: grid; grid-template-columns: 1fr auto auto; gap: .5rem; align-items: center;
  padding: .45rem 0; border-bottom: 1px solid var(--line); font-size: .88rem; }
.qty { display: flex; align-items: center; gap: .3rem; }
.qty button { width: 1.7rem; height: 1.7rem; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); cursor: pointer; font-weight: 700; }
.qty input { width: 3.2rem; text-align: center; border: 1px solid var(--line); border-radius: 8px; padding: .25rem; }
.cart-totals { margin-top: .7rem; font-size: .9rem; }
.cart-totals div { display: flex; justify-content: space-between; padding: .2rem 0; }
.cart-totals .grand { font-size: 1.2rem; font-weight: 800; color: var(--brand); border-top: 2px solid var(--line); margin-top: .4rem; padding-top: .5rem; }
.pay-opts { display: flex; gap: .5rem; margin: .6rem 0; }
.pay-opts .btn { flex: 1; }
.pay-opts .btn.sel { outline: 3px solid var(--brand-3); }

/* ---------- Formularios ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: .8rem; color: var(--mut); margin-bottom: .25rem; }
.field input, .field select, .field textarea { width: 100%; padding: .55rem .7rem;
  border: 1px solid var(--line); border-radius: 10px; font-size: .95rem; background: var(--surface); }
.field textarea { min-height: 70px; resize: vertical; }

/* ---------- Modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.55); display: grid; place-items: center;
  z-index: 100; padding: 1rem; animation: fade .15s ease; }
.modal { background: var(--surface); border-radius: 16px; width: min(640px, 100%); max-height: 92dvh;
  overflow-y: auto; padding: 1.4rem; box-shadow: 0 25px 70px rgba(0,0,0,.4); }
.modal h3 { margin: 0 0 1rem; }
.modal-actions { display: flex; gap: .6rem; justify-content: flex-end; margin-top: 1.2rem; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Toasts ---------- */
#toasts { position: fixed; bottom: calc(76px + var(--safe-b)); right: 1rem; display: flex; flex-direction: column; gap: .5rem; z-index: 200; }
.toast { background: #0f172a; color: #fff; padding: .7rem 1rem; border-radius: 12px; font-size: .9rem;
  box-shadow: 0 10px 30px rgba(0,0,0,.35); animation: slide .2s ease; max-width: 340px; }
.toast.ok { background: var(--ok); } .toast.bad { background: var(--bad); } .toast.warn { background: var(--warn); }
@keyframes slide { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Sync ---------- */
.sync-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 1rem; }
.sync-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.2rem; }
.sync-card h4 { margin: 0 0 .4rem; }
.sync-card ul { margin: .4rem 0 0; padding-left: 1.1rem; color: var(--mut); font-size: .85rem; }

/* ---------- Chart ---------- */
.chart-wrap { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; margin-bottom: 1.25rem; }
canvas#salesChart { width: 100%; height: 220px; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: var(--sidebar-w); z-index: 60;
    transform: translateX(-100%); transition: transform .2s ease; }
  .sidebar.open { transform: none; box-shadow: 10px 0 40px rgba(0,0,0,.4); }
  #burger { display: inline-block; }
  .pos { grid-template-columns: 1fr; }
  .cart { position: static; }
  .pos-list { max-height: 42vh; }
  .view { padding: 1rem 1rem calc(84px + var(--safe-b)); }
  .bottom-nav { display: flex; }
}
.bottom-nav { display: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: .35rem 0 calc(.35rem + var(--safe-b)); }
.bottom-nav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .1rem;
  text-decoration: none; color: var(--mut); font-size: 1.15rem; }
.bottom-nav a span { font-size: .68rem; font-weight: 600; }
.bottom-nav a.active { color: var(--brand); }
