:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f0;
  --border: #dbe2df;
  --text: #17201d;
  --muted: #5d6b66;
  --primary: #0f5c4d;
  --primary-hover: #0b4a3e;
  --primary-soft: #e1f0eb;
  --on-primary: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #9a6100;
  --warn-soft: #fff4dc;
  --ok: #157f3d;
  --ok-soft: #e3f5e9;
  --info: #1d5fa8;
  --info-soft: #e5effa;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 20, .06), 0 4px 16px rgba(16, 24, 20, .06);
  --nav-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101513;
    --surface: #171e1b;
    --surface-2: #1f2824;
    --border: #2c3833;
    --text: #e6ece9;
    --muted: #96a5a0;
    --primary: #3fb59a;
    --primary-hover: #57c6ab;
    --primary-soft: #183a32;
    --on-primary: #06231c;
    --danger: #f07167;
    --danger-soft: #3a1c1a;
    --warn: #e9b44c;
    --warn-soft: #3a2e14;
    --ok: #5ccf86;
    --ok-soft: #173322;
    --info: #74a9e8;
    --info-soft: #172a40;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 16px 10px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; font-weight: 700; }
.brand img { width: 30px; height: 30px; }
.empresa-actual { margin: 0 8px 14px; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; }
.empresa-actual .nombre { font-weight: 700; font-size: .88rem; line-height: 1.25; margin: 2px 0 4px; overflow-wrap: anywhere; }
.empresa-movil { font-size: .8rem; font-weight: 700; color: var(--muted); margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 860px) { .empresa-actual { display: none; } }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px;
  color: var(--text); text-decoration: none; font-weight: 500; margin-bottom: 2px;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.nav svg { width: 20px; height: 20px; flex-shrink: 0; }
.sidebar .user { margin-top: auto; padding: 10px 12px; font-size: .85rem; color: var(--muted); }
.main { flex: 1; min-width: 0; padding: 20px 24px 40px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.ambiente-badge { font-size: .75rem; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.ambiente-badge.pruebas { background: var(--warn-soft); color: var(--warn); }
.ambiente-badge.produccion { background: var(--ok-soft); color: var(--ok); }

@media (max-width: 860px) {
  .sidebar {
    position: fixed; bottom: 0; top: auto; left: 0; right: 0; width: auto; height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    flex-direction: row; padding: 0 0 env(safe-area-inset-bottom); border-right: 0; border-top: 1px solid var(--border); z-index: 20;
  }
  .brand, .sidebar .user { display: none; }
  .nav { display: flex; width: 100%; }
  .nav a {
    flex: 1; flex-direction: column; gap: 2px; font-size: .68rem; padding: 8px 2px; margin: 0; border-radius: 0; justify-content: center;
  }
  .nav a.active { background: transparent; }
  .main { padding: 14px 16px calc(var(--nav-h) + 90px + env(safe-area-inset-bottom)); }
}

/* ---------- Componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 14px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 9px 14px; border-radius: 8px; font: inherit; font-weight: 600;
  cursor: pointer; min-height: 40px; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { color: var(--danger); }
.btn.big { min-height: 52px; font-size: 1.05rem; width: 100%; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.btn.icon { padding: 0; width: 36px; min-height: 36px; }
.btn svg { width: 18px; height: 18px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

label { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; min-height: 42px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
input[type=checkbox] { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--text); font-size: .92rem; }
.field { margin-bottom: 12px; }
.grid { display: grid; gap: 12px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 600px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }
.hint { font-size: .8rem; color: var(--muted); margin-top: 4px; }

.badge { display: inline-block; font-size: .72rem; font-weight: 700; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--info); }

.alert { padding: 10px 12px; border-radius: 8px; font-size: .9rem; margin-bottom: 12px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.err { background: var(--danger-soft); color: var(--danger); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.info { background: var(--info-soft); color: var(--info); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }

/* Lista tipo tarjeta para móviles */
.list-item {
  display: flex; justify-content: space-between; gap: 10px; padding: 12px 4px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.list-item:last-child { border-bottom: 0; }
.list-item .title { font-weight: 600; }
@media (max-width: 860px) { .only-desktop { display: none !important; } }
@media (min-width: 861px) { .only-mobile { display: none !important; } }

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 14px; }
.stat .label { font-size: .8rem; color: var(--muted); font-weight: 600; }
.stat .value { font-size: 1.4rem; font-weight: 700; margin-top: 2px; }
@media (max-width: 860px) { .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* ---------- Punto de venta ---------- */
.pos { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 16px; align-items: start; }
@media (max-width: 1100px) { .pos { grid-template-columns: minmax(0, 1fr) 360px; } }
@media (max-width: 860px) { .pos { grid-template-columns: 1fr; } }
.search-box { position: relative; }
.search-box input { padding-left: 38px; min-height: 48px; font-size: 1rem; }
.search-box svg { position: absolute; left: 11px; top: 14px; width: 20px; height: 20px; color: var(--muted); }
.products { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: 12px; }
.product {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; cursor: pointer;
  text-align: left; font: inherit; color: inherit; display: flex; flex-direction: column; gap: 6px; min-height: 92px;
}
.product:hover { border-color: var(--primary); }
.product:active { transform: scale(.98); }
.product .name { font-weight: 600; font-size: .92rem; line-height: 1.25; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.product .price { margin-top: auto; font-weight: 700; color: var(--primary); }
.product .code { font-size: .75rem; color: var(--muted); }

.cart-line { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.cart-line .desc { font-weight: 600; font-size: .92rem; cursor: pointer; }
.cart-line .meta { font-size: .8rem; color: var(--muted); }
.cart-line .total { font-weight: 700; text-align: right; }
.qty { display: flex; align-items: center; gap: 4px; }
.qty button { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 1.1rem; cursor: pointer; }
.qty input { width: 58px; min-height: 32px; padding: 4px; text-align: center; }
.totals-row { display: flex; justify-content: space-between; padding: 3px 0; font-size: .92rem; }
.totals-row.grand { font-size: 1.35rem; font-weight: 800; padding-top: 8px; margin-top: 6px; border-top: 1px solid var(--border); }
.segmented { display: flex; background: var(--surface-2); border-radius: 8px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.segmented button {
  flex: 1; border: 0; background: transparent; color: var(--muted); padding: 8px; border-radius: 6px; font: inherit; font-weight: 600; cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.checkout { position: sticky; top: 16px; }
.mobile-bar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); background: var(--surface);
  border-top: 1px solid var(--border); padding: 10px 16px; display: flex; align-items: center; gap: 12px; z-index: 15;
}
.mobile-bar .t { font-size: 1.25rem; font-weight: 800; }
@media (min-width: 861px) { .mobile-bar { display: none; } }
.empty { text-align: center; padding: 28px 10px; color: var(--muted); }

/* ---------- Modal y toast ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 16, 14, .5); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px; max-height: calc(100vh - 32px); overflow: auto;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .3); display: flex; flex-direction: column;
}
.modal.wide { max-width: 900px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.modal-body { padding: 16px; }
.modal-foot { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; position: sticky; bottom: 0; background: var(--surface); }
@media (max-width: 600px) {
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal { border-radius: 14px 14px 0 0; max-height: 92vh; }
  .modal-foot .btn { flex: 1; }
}
#toast-root { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 420px); }
.toast { padding: 12px 14px; border-radius: 10px; box-shadow: var(--shadow); font-weight: 600; background: var(--surface); border: 1px solid var(--border); }
.toast.err { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.toast.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login .card { width: 100%; max-width: 380px; padding: 24px; }
.login .brand { justify-content: center; font-size: 1.15rem; padding-bottom: 6px; }
.login .brand img { width: 40px; height: 40px; }

.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

.sri-msg { font-size: .85rem; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; margin-top: 6px; }
.kv { display: grid; grid-template-columns: 140px minmax(0, 1fr); gap: 4px 12px; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

/* ---------- Impresión ---------- */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block !important; color: #000; background: #fff; }
  body { background: #fff; }
}

/* Ticket térmico */
.ticket { font-family: "Courier New", monospace; font-size: 11px; line-height: 1.3; color: #000; width: 100%; }
.ticket .c { text-align: center; }
.ticket .b { font-weight: 700; }
.ticket .big { font-size: 15px; font-weight: 700; }
.ticket .sep { border-top: 1px dashed #000; margin: 4px 0; }
.ticket .row { display: flex; justify-content: space-between; gap: 6px; }
.ticket .row span:last-child { white-space: nowrap; }
.ticket .clave { word-break: break-all; font-size: 10px; }
.ticket svg { width: 100%; height: auto; }

/* RIDE (A4) */
.ride { font-family: Arial, Helvetica, sans-serif; font-size: 10.5px; color: #000; width: 100%; }
.ride .top { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ride .box { border: 1px solid #000; border-radius: 8px; padding: 8px 10px; }
.ride .emisor-nombre { font-size: 15px; font-weight: 700; margin: 30px 0 8px; }
.ride .doc-tipo { font-size: 16px; font-weight: 700; margin: 4px 0; }
.ride p { margin: 2px 0; }
.ride table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.ride th, .ride td { border: 1px solid #000; padding: 3px 5px; font-size: 10px; color: #000; text-transform: none; letter-spacing: 0; }
.ride th { background: #eee; font-weight: 700; }
.ride .bottom { display: grid; grid-template-columns: 1fr 260px; gap: 10px; margin-top: 8px; align-items: start; }
.ride .bottom table { margin-top: 0; }
.ride .barcode svg { width: 100%; height: 50px; }
.ride-preview { background: #fff; padding: 16px; border-radius: 8px; border: 1px solid var(--border); }
.ticket-preview { background: #fff; padding: 12px; border-radius: 8px; border: 1px solid var(--border); max-width: 320px; margin: 0 auto; }
