/* Paleta: fondo blanco con tonos morados */
:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f7f3fd;     /* morado muy claro: hover, encabezados */
  --border: #e6dcf5;
  --text: #2a1f3d;
  --muted: #6e6283;
  --accent: #6d28d9;        /* morado principal */
  --accent-strong: #4c1d95; /* morado oscuro: barra superior */
  --accent-contrast: #ffffff;
  --accent-soft: #ede4fc;
  --danger: #c0392b;
  --mark: #e4d4ff;
  --group-bg: #f1e9fd;
  --shadow: 0 8px 30px rgba(76, 29, 149, .15);
  --radius: 10px;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px;
  background: linear-gradient(90deg, var(--accent-strong), var(--accent));
  color: #fff;
}
.brand h1 { margin: 0; font-size: 18px; }
.meta { margin: 2px 0 0; color: rgba(255, 255, 255, .8); font-size: 12px; }
.topbar .btn { background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .35); color: #fff; }
.topbar .btn:hover { background: rgba(255, 255, 255, .22); }
.topbar .btn-primary { background: #fff; color: var(--accent-strong); border-color: #fff; }
.topbar .btn-primary:hover { background: var(--accent-soft); filter: none; }
.topbar .badge { background: var(--accent); color: #fff; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); font-weight: 600; }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-danger { color: var(--danger); }
.badge {
  min-width: 20px; padding: 0 6px; border-radius: 10px; text-align: center;
  background: var(--accent-contrast); color: var(--accent); font-size: 12px; line-height: 20px;
}
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 16px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Filters ---------- */
.filters {
  position: sticky; top: 0; z-index: 5;
  padding: 12px 20px; background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.search-wrap { position: relative; }
.search-wrap input {
  width: 100%; padding: 12px 44px 12px 42px; font-size: 16px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); outline: none;
}
.search-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-icon {
  position: absolute; left: 14px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%);
  fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
}
.hint {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  border: 1px solid var(--border); border-radius: 4px; padding: 0 6px; color: var(--muted); font-size: 12px;
}
.filter-row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 14px; margin-top: 10px; }
.field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
.field select, .field input {
  font: inherit; font-size: 14px; color: var(--text);
  padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  min-width: 180px; max-width: 280px;
}
.field-sm input { min-width: 0; width: 120px; }
.field-inline { flex-direction: row; align-items: center; gap: 8px; }
.field-inline select { min-width: 0; }
.check { display: inline-flex; align-items: center; gap: 6px; padding-bottom: 7px; cursor: pointer; user-select: none; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 16px; padding: 16px 20px 40px; }
.sidebar {
  position: sticky; top: var(--filters-h, 130px); align-self: start;
  max-height: calc(100vh - var(--filters-h, 130px) - 16px);
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.sidebar-head { padding: 10px; border-bottom: 1px solid var(--border); display: grid; gap: 6px; }
.sidebar-head input {
  font: inherit; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface-2); color: var(--text);
}
.cat-list { list-style: none; margin: 0; padding: 4px; overflow: auto; }
.cat-list button {
  width: 100%; display: flex; justify-content: space-between; gap: 8px;
  padding: 5px 8px; border: 0; border-radius: 6px; background: none; color: var(--text);
  font: inherit; font-size: 13px; text-align: left; cursor: pointer;
}
.cat-list button:hover { background: var(--surface-2); }
.cat-list button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.cat-list .n { color: var(--muted); font-variant-numeric: tabular-nums; }
.cat-list li.zero { display: none; }

.results-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; }
.results-head p { margin: 0; color: var(--muted); }

/* ---------- Table ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: clip; }
.grid { width: 100%; border-collapse: collapse; }
.grid th, .grid td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.grid thead th {
  position: sticky; top: var(--filters-h, 130px); z-index: 2;
  background: var(--surface-2); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--accent-strong);
  cursor: pointer; user-select: none; white-space: nowrap;
}
.grid thead th.act { cursor: default; }
.grid thead th[aria-sort="ascending"]::after { content: " ▲"; }
.grid thead th[aria-sort="descending"]::after { content: " ▼"; }
.grid tbody tr:hover td { background: var(--surface-2); }
.grid .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.grid td.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; white-space: nowrap; }
.grid td.code button {
  font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: copy;
  border-bottom: 1px dashed var(--border);
}
.grid td.code button:hover { color: var(--accent); border-color: var(--accent); }
.grid td.brand { white-space: nowrap; color: var(--muted); }
.grid td.price { font-weight: 600; }
.grid td.price .tag { font-weight: 600; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); }
.grid td.noprice { color: var(--muted); font-weight: 400; }
.grid td.act { width: 1%; text-align: right; }
.add-btn {
  width: 30px; height: 28px; border-radius: 7px; border: 1px solid var(--border);
  background: var(--surface); color: var(--accent); font-size: 17px; line-height: 1; cursor: pointer;
}
.add-btn:hover { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.add-btn.in { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); font-size: 12px; font-weight: 700; }
.grid tr.group td {
  background: var(--group-bg); font-weight: 700; font-size: 12.5px; letter-spacing: .02em;
  color: var(--accent); padding: 6px 10px;
}
.grid tr.group:hover td { background: var(--group-bg); }
.grid tr.group .n { color: var(--muted); font-weight: 400; margin-left: 6px; }
mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }
.sentinel { height: 1px; }
.empty { padding: 24px; text-align: center; color: var(--muted); margin: 0; }

/* ---------- Cart ---------- */
.cart {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 20;
  width: min(460px, 100vw); display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow);
}
.cart-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.cart-head h2 { margin: 0; font-size: 17px; }
.cart-body { flex: 1; overflow: auto; }
.cart-table { width: 100%; border-collapse: collapse; }
.cart-table th, .cart-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; text-align: left; }
.cart-table th { font-size: 12px; color: var(--muted); text-transform: uppercase; }
.cart-table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.cart-table .sub { color: var(--muted); font-size: 12px; }
.cart-table input { width: 58px; padding: 4px 6px; font: inherit; text-align: right; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }
.cart-foot { border-top: 1px solid var(--border); padding: 12px 16px; display: grid; gap: 10px; }
.total { display: flex; justify-content: space-between; align-items: baseline; font-size: 16px; }
.total strong { font-size: 20px; }
.cart-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.backdrop { position: fixed; inset: 0; z-index: 15; background: rgba(0, 0, 0, .35); }

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 30; transform: translate(-50%, 20px);
  padding: 9px 16px; border-radius: 8px; background: var(--text); color: var(--bg);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px 40px; }
  .sidebar { display: none; }
}
@media (max-width: 720px) {
  .topbar, .filters { padding-left: 16px; padding-right: 16px; }
  .topbar { flex-wrap: wrap; }
  .filters { position: static; }
  .hint { display: none; }
  .field { flex: 1 1 140px; }
  .field select, .field input, .field-sm input { min-width: 0; width: 100%; max-width: none; }
  .results-head { flex-wrap: wrap; }

  .grid thead { display: none; }
  .grid, .grid tbody, .grid tr, .grid td { display: block; width: 100%; }
  .grid tr.item {
    display: grid; grid-template-columns: 1fr auto; gap: 2px 10px;
    padding: 10px 12px; border-bottom: 1px solid var(--border);
  }
  .grid tr.item td { border: 0; padding: 0; background: none !important; }
  .grid tr.item td.desc { grid-column: 1; font-weight: 600; }
  .grid tr.item td.price { grid-column: 2; grid-row: 1; text-align: right; }
  .grid tr.item td.app { grid-column: 1; }
  .grid tr.item td.code, .grid tr.item td.brand { grid-column: 1; font-size: 12px; color: var(--muted); }
  .grid tr.item td.act { grid-column: 2; grid-row: 2 / span 3; align-self: end; width: auto; }
  .grid tr.item td.code::before { content: "Cód. "; font-family: system-ui, sans-serif; }
  .grid tr.item td.brand::before { content: "Marca: "; }
  .grid tr.item td:empty { display: none; }
}

/* ---------- Print (solo el pedido) ---------- */
@media print {
  body > *:not(.cart) { display: none !important; }
  .cart { position: static; width: auto; box-shadow: none; border: 0; display: flex !important; }
  .cart-actions, #closeCart, .cart-table td:last-child, .cart-table th:last-child { display: none; }
  .cart-table input { border: 0; width: auto; }
}
