/* ------------------------------------------------------------------
   Page-specific styles: Ventas, Etapas, Usuarios, Existencias, Reportes
   (matrices), Pasteles personalizados, Kardex, Facturación, Ajustes.
   ------------------------------------------------------------------ */

/* ---------- Content blocks ---------- */
.etapa-block,
.kardex-action,
.sales-list-column {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  margin-bottom: 16px;
  overflow-x: auto;
}

.etapa-block h3 { margin: 0 0 0.35rem; }
.etapa-block > p.muted { margin-top: 0; }

/* Bare tables rendered straight into a panel get the card look too. */
.panel > div[id$="-table"]:not(:empty),
.panel > .sheet-frame > div[id$="-table"]:not(:empty),
#suggested-shipments-grid:not(:empty) {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow-x: auto;
}

.etapa-block div[id$="-table"],
.sales-list-column div[id$="-table"] {
  border: 0;
  border-radius: 0;
  background: transparent;
}

/* ---------- Ventas ---------- */
.sales-dashboard {
  display: grid;
  grid-template-columns: minmax(240px, 34%) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.sales-summary-column {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.store-income { grid-column: 1 / -1; }

.store-income-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-soft);
}

.store-income-row:last-child { border-bottom: 0; }
.store-income-row span { font-size: 0.9rem; color: var(--text-2); }
.store-income-row strong { font-size: 0.95rem; }

.sales-list-column h2 { margin: 0; }
.sales-list-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-bottom: 16px;
}

/* ---------- Loading indicators ---------- */
.loading-spinner {
  display: inline-block;
  flex: none;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: admin-spin 0.8s linear infinite;
}

.loading-inline {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--text-2);
}

.loading-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 48px 16px;
  color: var(--text-2);
}

.loading-block .loading-spinner {
  width: 32px;
  height: 32px;
  border-width: 3px;
}

@keyframes admin-spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .loading-spinner { animation-duration: 2.4s; }
}
#sales-table { overflow-x: auto; }

.simulation-toolbar { justify-content: flex-end; }

/* ---------- Simulación en vivo ---------- */
.sim-live { margin-bottom: 28px; }
.sim-live h5 { margin: 18px 0 8px; font-size: 0.95rem; }
.sim-speeds { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--radius); overflow: hidden; }
.sim-speeds button { border: 0; border-radius: 0; background: var(--surface); color: var(--text); padding: 7px 12px; }
.sim-speeds button + button { border-left: 1px solid var(--border); }
.sim-speeds button.active { background: var(--brand); color: #fff; }
.sim-clock { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; margin: 16px 0 12px; }
.sim-clock strong { font-size: 2.2rem; font-variant-numeric: tabular-nums; letter-spacing: -1px; }
.sim-metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.sim-metrics .metric { padding: 14px 16px; }
.sim-metrics .metric strong { font-size: 1.2rem; letter-spacing: -0.2px; }
.sim-metrics .metric.alert strong { color: var(--danger); }
.sim-trucks { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.sim-truck { padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.sim-truck-head { display: flex; justify-content: space-between; gap: 8px; font-size: 0.88rem; }
.sim-truck-meta { font-size: 0.8rem; color: var(--muted); margin-top: 2px; }
.sim-track { height: 6px; border-radius: var(--radius-pill); background: var(--surface-muted); margin-top: 8px; overflow: hidden; }
.sim-track span { display: block; height: 100%; background: var(--info); border-radius: inherit; transition: width 0.4s linear; }
.sim-track.returning span { background: var(--faint); }
.sim-log-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px; }
.sim-log-head h5 { margin-right: auto; }
.sim-log-head label { font-size: 0.85rem; display: inline-flex; gap: 6px; align-items: center; }
.sim-log { list-style: none; margin: 0; padding: 0; max-height: 420px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.sim-log li { display: grid; grid-template-columns: 92px 150px minmax(0, 1fr); gap: 10px; padding: 6px 12px; border-bottom: 1px solid var(--border-soft); font-size: 0.85rem; }
.sim-log li:last-child { border-bottom: 0; }
.sim-log time { color: var(--muted); font-variant-numeric: tabular-nums; }
.sim-log .actor { color: var(--text-2); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sim-log li.warn { background: var(--warning-soft); }
.sim-log li.warn .msg { color: var(--warning-ink); }
.sim-log li.error { background: var(--danger-soft); }
.sim-log li.error .msg { color: var(--danger); font-weight: 600; }
.sim-log li.ok .msg { color: var(--success-ink); }
.sim-log li.sale .msg { color: var(--text-2); }
@media (max-width: 900px) {
  .sim-log li { grid-template-columns: 1fr; gap: 2px; }
}

/* ---------- Usuarios ---------- */
.user-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.user-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  min-width: 0;
}

.user-card h3 { margin: 0 0 0.35rem; }
.user-card p { margin: 0 0 0.75rem; }

.user-meta,
.card-actions,
.table-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.card-actions { margin-top: 16px; }
.table-actions { min-width: 150px; }

/* "Exportar a Excel / PDF" bar that renderTable adds above an exportable table */
.table-export {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
  margin: 0 0 8px;
}

/* Usuarios › Modo ahorro keys */
.lite-keys { margin-top: 32px; }

.lite-key-result {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 16px;
  padding: 12px 16px;
  border: 1px dashed var(--brand);
  border-radius: var(--radius);
  background: var(--brand-tint);
}

.lite-key-result strong {
  font-family: ui-monospace, 'Courier New', monospace;
  font-size: 1.6rem;
  letter-spacing: 0.12em;
  user-select: all;
}

/* ---------- Envíos (shipment report modal) ---------- */
.shipment-report-card,
.stock-units-card { width: min(760px, 100%); }

.shipment-report-sheet { width: 100%; border-collapse: collapse; margin-top: 12px; }

.shipment-report-sheet th,
.shipment-report-sheet td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
}

.shipment-report-sheet thead th { background: var(--surface-2); }
.shipment-report-sheet tfoot td { font-weight: 700; background: var(--surface-2); }
.shipment-report-sheet tbody tr.shipment-row-complete td { background: var(--success-soft); }

/* ---------- Etapas ---------- */
.etapa-mode { display: flex; align-items: center; gap: 6px; }
.etapa-mode select { width: auto; }
.etapa-mode input { width: 5.5rem; }

/* ---------- Existencias / lotes ---------- */
#stock-table { overflow-x: auto; }
#stock-table table { min-width: 980px; }

.unit-dropdown { min-width: 260px; }

.unit-dropdown summary {
  cursor: pointer;
  color: var(--brand);
  font-weight: 700;
}

.unit-dropdown-body {
  margin-top: 12px;
  max-height: 340px;
  overflow: auto;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.unit-dropdown-body table { min-width: 620px; }

.unit-barcode {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--text);
  font-size: 0.76rem;
}

.unit-barcode svg { background: #fff; max-width: 160px; }

/* ---------- Productos ---------- */
.product-image-field { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.product-image-label { display: flex; flex-direction: column; gap: 4px; }
.product-image-label input { width: auto; }
.product-image-preview { width: 72px; height: 54px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.product-thumb { width: 48px; height: 36px; object-fit: cover; border-radius: 6px; display: block; }

/* Product / user forms: the long filter row becomes a form card. */
#product-form,
#user-form,
#lite-key-form {
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

/* ---------- Descuentos y precios ---------- */
.promo-card { margin-bottom: 24px; }
.promo-row { margin-bottom: 12px; }
.promo-name-input { flex: 2; min-width: 250px; }
.promo-type-select { flex: 1; min-width: 180px; }
.promo-val-input { width: 120px; }
.promo-qty-input { width: 70px; }
.promo-desc-input { flex: 1; min-width: 200px; }
.promo-check-label { display: flex; align-items: center; gap: 6px; }
.promo-actions-row { display: flex; gap: 8px; align-items: center; }

.checkbox-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 6px 16px;
  margin-bottom: 16px;
  font-size: 0.85rem;
}

.checkbox-grid label { display: flex; align-items: center; gap: 6px; }

/* ---------- Reportes: grouped report menu on the left, the report on the right ---------- */
.report-layout {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}

.report-menu {
  position: sticky;
  top: 84px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  overflow: visible;
}

.report-menu-group {
  margin: 10px 10px 4px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.report-menu-group:first-child { margin-top: 4px; }

.report-menu .subtab {
  justify-content: flex-start;
  padding: 9px 10px;
  border: 0;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  text-align: left;
  white-space: normal;
}

.report-menu .subtab:hover { background: var(--surface-muted); }

.report-menu .subtab.active {
  background: var(--brand-soft);
  color: var(--brand-ink);
}

.report-views { min-width: 0; }

.report-title { margin: 0 0 0.25rem; }
.report-meta { margin: 0 0 1rem; }
.report-title-input { flex: 1; min-width: 240px; }
.matrix-filter-row { margin-top: 16px; }
.matrix-search-input { width: 200px; }
.report-group-filter { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.report-group-filter > span { display: inline-flex; gap: 10px; flex-wrap: wrap; }
.report-group-heading { margin: 20px 0 8px; font-size: 1rem; letter-spacing: 0.04em; }

/* Spreadsheet-style matrices. The cell colors carry meaning (stock ok / short / total). */
.matrix-container {
  overflow-x: auto;
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
}

.matrix-header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
  padding: 12px 16px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.matrix-title-banner {
  background: var(--brand-soft);
  color: var(--brand-ink);
  font-weight: 700;
  padding: 6px 14px;
  border-radius: var(--radius-pill);
  letter-spacing: 0.04em;
  font-size: 0.85rem;
  text-transform: uppercase;
}

.matrix-legend { display: flex; align-items: center; gap: 8px; font-size: 0.78rem; font-weight: 600; }

.matrix-pill-green,
.matrix-pill-red,
.matrix-pill-purple {
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  color: #fff;
}

.matrix-pill-green { background: var(--success); }
.matrix-pill-red { background: var(--brand); }
.matrix-pill-purple { background: #7e22ce; }

.matrix-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; min-width: 850px; }

.matrix-table th,
.matrix-table td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: center;
  text-transform: none;
  letter-spacing: 0;
}

.matrix-table tbody tr:hover > td { background: inherit; }

.matrix-th-product {
  background: #fdf3d0 !important;
  color: var(--text) !important;
  font-weight: 700;
  text-align: left !important;
  position: sticky;
  left: 0;
  z-index: 2;
  min-width: 190px;
}

.matrix-th-store { background: var(--surface-muted) !important; color: var(--text) !important; font-weight: 700; min-width: 55px; }

.matrix-th-day,
.matrix-th-reason {
  background: #e7e4e1 !important;
  color: var(--text) !important;
  font-weight: 700;
  min-width: 34px;
  font-size: 0.72rem;
}

.matrix-th-total,
.matrix-th-price { background: #fdf3d0 !important; color: var(--text) !important; font-weight: 700; min-width: 75px; }

.matrix-td-product {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  text-align: left !important;
  position: sticky;
  left: 0;
  z-index: 1;
  border-right: 2px solid var(--border-strong);
}

.matrix-td-product-tpl { background: #fffaea; color: var(--text); }
.matrix-cell-green { background: var(--success) !important; color: #fff !important; font-weight: 700; }
.matrix-cell-red { background: #d9364e !important; color: #fff !important; font-weight: 700; }
.matrix-cell-zero { background: var(--brand-ink) !important; color: #fff !important; font-weight: 700; }
.matrix-cell-total { background: #fdf3d0 !important; color: #7a4a05 !important; font-weight: 700; }
.matrix-cell-price { background: var(--surface); color: var(--text); text-align: right !important; font-weight: 600; }
.matrix-table .matrix-store-start { border-left: 2px solid var(--text-2); }

.matrix-tr-total td,
.matrix-tr-total td.matrix-td-product {
  background: #fbe6a6 !important;
  color: var(--text) !important;
  font-weight: 700;
  border-top: 2px solid var(--text-2);
}

/* Ventas por producto / por forma de pago: plain list reports on the matrix styling. */
.sales-report-table { min-width: 0; }
.sales-report-table .matrix-cell-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sales-report-table .matrix-cell-text { text-align: left; }

/* Venta especializada: summary cards, then the three top-5 lists side by side. */
.specialized-cards { margin: 12px 0 16px; padding: 0; }
.specialized-section { margin-bottom: 20px; }
.specialized-section h4 { margin: 0 0 8px; font-size: 0.9rem; letter-spacing: 0.02em; }
.specialized-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }

/* ---------- Pasteles personalizados: one price table per option category ---------- */
.cake-options-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; margin-bottom: 24px; }
.cake-card h4 { margin: 0 0 4px; }
.cake-card table input[type="text"] { width: 100%; min-width: 120px; }
.cake-card .cake-price-input { width: 100px; }
.cake-card .cake-add-row td { padding-top: 10px; }
.cake-card-wide { grid-column: 1 / -1; }
.cake-combo-row > td { background: var(--surface-muted); }
.cake-combos { padding: 8px 0 12px 16px; }
.cake-combos table { width: auto; margin-bottom: 8px; }
.cake-combos select { max-width: 320px; }

.cake-image-picker { display: flex; flex-direction: column; gap: 4px; width: 130px; font-size: 0.75rem; }
.cake-image-picker img { width: 96px; height: 96px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.cake-image-picker input[type="file"] { width: 130px; font-size: 0.72rem; }

/* ---------- Kardex ---------- */
.kardex-metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }

.kardex-table-wrap {
  overflow: auto;
  max-height: 60vh;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  margin-bottom: 16px;
}

.kardex-table-wrap table { margin: 0; }
.kardex-table-wrap thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.kardex-num { text-align: right; font-variant-numeric: tabular-nums; }
.kardex-in { color: var(--success-ink); }
.kardex-out { color: var(--brand-ink); }
.kardex-diff { background: var(--brand-soft); color: var(--brand-ink); font-weight: 600; }
.kardex-row-corte td { background: var(--info-soft); }
.kardex-row-flag td { background: var(--warning-soft); }

.kardex-link,
.kardex-cell-btn {
  min-height: 0;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  cursor: pointer;
}

.kardex-link { color: var(--brand); text-decoration: underline; }
.kardex-cell-btn { color: inherit; text-decoration: underline dotted; }
.kardex-link:hover, .kardex-cell-btn:hover { background: none; color: var(--brand-hover); }

.kardex-action h4 { margin: 0 0 8px; }
.kardex-action .filters { margin-bottom: 4px; }

.kardex-status { font-weight: 600; white-space: nowrap; }
.kardex-status.pending, .kardex-status.picked_up, .kardex-status.received { color: var(--warning-ink); }
.kardex-status.completed { color: var(--success-ink); }
.kardex-status.cancelled { color: var(--faint); text-decoration: line-through; }

/* ---------- Facturación (CFDI) settings groups ---------- */
.cfdi-fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px 4px; margin: 0 0 16px; }
.cfdi-fieldset legend { padding: 0 6px; font-weight: 600; font-size: 0.9rem; }

/* ---------- Ajustes ---------- */
.config-logo { align-items: center; }

#config-logo-preview {
  height: 80px;
  width: auto;
  max-width: 240px;
  object-fit: contain;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
}

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .sales-dashboard,
  .user-grid { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .report-layout { grid-template-columns: 1fr; gap: 16px; }

  /* The report menu becomes one row of chips that scrolls sideways. */
  .report-menu {
    position: static;
    flex-direction: row;
    gap: 6px;
    padding: 0;
    border: 0;
    background: transparent;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .report-menu::-webkit-scrollbar { display: none; }
  .report-menu-group { display: none; }

  .report-menu .subtab {
    flex-shrink: 0;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--surface);
    white-space: nowrap;
  }

  .report-menu .subtab.active { background: var(--brand); border-color: var(--brand); color: #fff; }
}

@media (max-width: 640px) {
  .sales-summary-column { grid-template-columns: 1fr; }
  .cake-options-grid { grid-template-columns: 1fr; }
}

/* ---------- Full-screen sheets (js/fullscreen-sheets.js) ---------- */
.sheet-frame-bar { display: flex; justify-content: flex-end; margin: 8px 0 6px; }
.sheet-frame-toggle { min-height: 0; padding: 4px 12px; font-size: 0.8rem; }
.sheet-frame > .matrix-container { margin-top: 0; }

.sheet-frame.is-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 150;
  display: flex;
  flex-direction: column;
  padding: 12px 16px 16px;
  background: var(--bg, var(--surface));
}

.sheet-frame.is-fullscreen > .sheet-frame-bar { margin-top: 0; }

.sheet-frame.is-fullscreen > .sheet-frame-body {
  flex: 1;
  min-height: 0;
  max-height: none;
  margin: 0;
  overflow: auto;
}

body.has-fullscreen-sheet { overflow: hidden; }

/* Keep the matrix header row visible while scrolling down in full screen. */
.sheet-frame.is-fullscreen .matrix-table thead tr:first-child th { position: sticky; top: 0; z-index: 3; }
.sheet-frame.is-fullscreen .matrix-table thead tr:first-child th.matrix-th-product { z-index: 4; }

/* Plain tables (Existencias, Ventas del sistema) keep their header row in full screen too. */
.sheet-frame.is-fullscreen > .sheet-frame-body > table thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.sheet-frame.is-fullscreen > #sales-table { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); }
