:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e6ec;
  --text: #1c2430;
  --muted: #6b7686;
  --primary: #1f6feb;
  --primary-dark: #1859c0;
  --ok: #1a8a4a;
  --warn: #c27c0e;
  --danger: #c73434;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20, 30, 50, .08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

h1, h2 { margin: 0; }
h1 { font-size: 1.15rem; }
h2 { font-size: 1.05rem; margin-bottom: 14px; display: flex; align-items: center; gap: 8px; }
small { color: var(--muted); font-weight: 400; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px;
  padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 38px; height: 38px; border-radius: 9px; background: var(--primary); color: #fff;
  display: grid; place-items: center; font-weight: 700;
}
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 10px; }
.tab {
  border: 0; background: transparent; padding: 8px 16px; border-radius: 7px; cursor: pointer;
  font: inherit; color: var(--muted);
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }
.view { display: none; }
.view.active { display: block; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat span { display: block; color: var(--muted); font-size: .82rem; }
.stat strong { font-size: 1.4rem; }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar input { flex: 1; min-width: 220px; }

input, select {
  font: inherit; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); width: 100%;
}
.toolbar select { width: auto; }
input:focus, select:focus { outline: 2px solid rgba(31, 111, 235, .25); border-color: var(--primary); }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 12px; }
label input, label select { margin-top: 5px; }

/* ---------- Buttons ---------- */
.btn {
  font: inherit; border: 1px solid transparent; border-radius: 8px; padding: 9px 16px;
  cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { padding: 5px 10px; font-size: .82rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.row { display: flex; gap: 10px; }
.row.end { justify-content: flex-end; margin-top: 16px; }

/* ---------- Table ---------- */
.table-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow-x: auto; box-shadow: var(--shadow);
}
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #fafbfc; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbfd; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sub { display: block; font-size: .8rem; color: var(--muted); }
.code { font-family: ui-monospace, Consolas, monospace; font-size: .85rem; }
.actions { display: flex; gap: 6px; justify-content: flex-end; }
.empty { text-align: center; color: var(--muted); padding: 30px; margin: 0; }

.pill { white-space: nowrap; display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .75rem; font-weight: 600; }
.pill.ok { background: #e3f4ea; color: var(--ok); }
.pill.warn { background: #fdf1dc; color: var(--warn); }
.pill.danger { background: #fbe5e5; color: var(--danger); }
.pill.mayor { background: #e6efff; color: var(--primary); }
.pill.menor { background: #eef0f3; color: var(--muted); }
.pill.mixta { background: #f1e8fd; color: #7343c4; }
.save { color: var(--ok); font-size: .78rem; }

/* ---------- POS ---------- */
.pos { display: grid; grid-template-columns: 1fr 1.2fr; gap: 18px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.pos-info { background: var(--bg); border-radius: 8px; padding: 10px 12px; font-size: .88rem; margin-bottom: 14px; line-height: 1.6; }
/* ---------- Autocompletado ---------- */
.field { margin-bottom: 12px; }
.field-label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 5px; }
.ac { position: relative; }
.ac-list {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 10;
  list-style: none; margin: 0; padding: 4px; max-height: 300px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 9px;
  box-shadow: 0 10px 28px rgba(20, 30, 50, .16);
}
.ac-list li { padding: 8px 10px; border-radius: 7px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.ac-list li.activo, .ac-list li:hover { background: #eef4ff; }
.ac-list li.off { opacity: .45; cursor: not-allowed; }
.ac-list li.off:hover { background: none; }
.ac-list .ac-precio { text-align: right; font-size: .8rem; color: var(--muted); white-space: nowrap; }
.ac-list mark { background: #fff1b8; color: inherit; border-radius: 2px; }
.ac-list .ac-vacio { color: var(--muted); cursor: default; justify-content: center; }
.ac-list .ac-vacio:hover { background: none; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 14px; }
.segmented button {
  font: inherit; padding: 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); cursor: pointer; font-weight: 600;
}
.segmented button small { display: block; font-size: .72rem; }
.segmented button.active { border-color: var(--primary); background: #eef4ff; color: var(--primary); }
.hint { font-size: .85rem; color: var(--muted); margin: 0 0 12px; }

.cart { list-style: none; padding: 0; margin: 0 0 14px; min-height: 60px; }
.cart li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px dashed var(--border); }
.cart li:last-child { border-bottom: 0; }
.cart .empty { padding: 20px; }
.cart-x { border: 0; background: none; color: var(--danger); cursor: pointer; font-size: 1.1rem; }
.total { display: flex; justify-content: space-between; align-items: baseline; padding: 14px 0; border-top: 2px solid var(--border); margin-bottom: 12px; }
.total strong { font-size: 1.5rem; }
.badge { font-size: .72rem; padding: 2px 9px; border-radius: 99px; background: var(--bg); color: var(--muted); }

/* ---------- Modal ---------- */
dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(560px, 94vw);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(15, 20, 30, .45); }
dialog form { padding: 22px 24px; max-height: 90vh; overflow-y: auto; }
fieldset { border: 1px solid var(--border); border-radius: 9px; padding: 12px 14px 2px; margin: 0 0 12px; }
legend { font-size: .8rem; font-weight: 600; padding: 0 6px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid .full { grid-column: 1 / -1; }
dialog.wide { width: min(860px, 96vw); }
.dialog-body { padding: 22px 24px; max-height: 90vh; overflow-y: auto; }
.row.between { justify-content: space-between; align-items: center; }
.row.between h2 { margin: 0; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; text-align: left; }
.link:hover { text-decoration: underline; }
.error { color: var(--danger); font-size: .88rem; min-height: 1em; margin: 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: #fff; padding: 11px 20px; border-radius: 9px;
  opacity: 0; pointer-events: none; transition: .25s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Inventario ---------- */
.toolbar input[type=date] { width: auto; flex: 0 0 auto; min-width: 0; }
.toolbar #invProducto { flex: 1; min-width: 200px; width: auto; }
.mov-preview { background: var(--bg); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; font-size: .92rem; }
.mov-preview:empty { display: none; }
.mas { color: var(--ok); font-weight: 700; }
.menos { color: var(--danger); font-weight: 700; }
.pill.ingreso { background: #e3f4ea; color: var(--ok); }
.pill.venta { background: #e6efff; color: var(--primary); }
.pill.ajuste { background: #fdf1dc; color: var(--warn); }
.pill.inicial { background: #eef0f3; color: var(--muted); }
input:disabled { background: var(--bg); color: var(--muted); }

/* ---------- Nota de venta ---------- */
.nota-acciones { flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.nota-acciones select { width: auto; }
.nota-preview { background: #e9ecf1; border-radius: 10px; padding: 20px; display: flex; justify-content: center; overflow-x: auto; }
.nota {
  background: #fff; color: #000; font-family: "Courier New", ui-monospace, monospace;
  font-size: 12px; line-height: 1.35; box-shadow: 0 2px 10px rgba(0, 0, 0, .12);
}
.nota.ticket { width: 80mm; padding: 5mm 4mm; }
.nota.a4 { width: 190mm; min-height: 120mm; padding: 12mm; font-family: system-ui, "Segoe UI", sans-serif; font-size: 13px; }
.nota .n-center { text-align: center; }
.nota .n-empresa { font-size: 1.25em; font-weight: 700; }
.nota .n-doc { border: 1.5px solid #000; padding: 5px; margin: 8px 0; text-align: center; font-weight: 700; }
.nota.a4 .n-cab { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.nota.a4 .n-cab .n-doc { min-width: 220px; margin: 0; padding: 12px; font-size: 1.1em; }
.nota hr { border: 0; border-top: 1px dashed #000; margin: 6px 0; }
.nota table { width: 100%; border-collapse: collapse; }
.nota th, .nota td { padding: 2px 0; border: 0; font-size: inherit; text-transform: none; letter-spacing: 0; color: #000; background: none; vertical-align: top; }
.nota th { border-bottom: 1px dashed #000; text-align: left; }
.nota.ticket th, .nota.ticket td { padding-right: 6px; }
.nota.ticket th:last-child, .nota.ticket td:last-child { padding-right: 0; }
.nota .r { text-align: right; white-space: nowrap; }
.nota.a4 th, .nota.a4 td { padding: 6px 8px; border-bottom: 1px solid #ccc; }
.nota.a4 th { background: #f1f1f1; border-bottom: 1px solid #000; }
.nota .n-total { font-size: 1.2em; font-weight: 700; }
.nota .n-dato { display: flex; gap: 6px; }
.nota .n-dato b { min-width: 64px; }
.nota .n-pie { margin-top: 10px; font-size: .9em; }

#areaImpresion { display: none; }

@media print {
  @page { margin: 0; }
  body { background: #fff; }
  body > *:not(#areaImpresion) { display: none !important; }
  #areaImpresion { display: block !important; }
  #areaImpresion .nota { box-shadow: none; margin: 0; }
}

@media (max-width: 800px) {
  main { padding: 16px; }
  .topbar { padding: 12px 16px; }
  .pos { grid-template-columns: 1fr; }
  .grid { grid-template-columns: 1fr; }
}
