:root {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --text: #1a1d23;
  --muted: #6b7280;
  --border: transparent;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-contrast: #ffffff;
  --primary-shadow: rgba(37, 99, 235, .3);
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #d97706;
  --warning-bg: #fef3c7;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow: 0 4px 16px rgba(16, 24, 40, .10), 0 1px 3px rgba(16, 24, 40, .06);
  --bg-overlay: rgba(255, 255, 255, .5);
  --bg-gradient: linear-gradient(135deg, rgba(37, 99, 235, .4) 0%, rgba(147, 51, 234, .22) 35%, rgba(255, 255, 255, .35) 68%);
  --logo-glow: 0 0 0 3px rgba(37, 99, 235, .12);
  --logo-shadow-color: rgba(15, 23, 42, .3);
  --glass-bg: rgba(255, 255, 255, .34);
  --glass-bg-strong: rgba(255, 255, 255, .34);
  --glass-border: transparent;
  --glass-highlight: inset 0 1px 0 rgba(255, 255, 255, .8);
  --glass-blur: 20px;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1d212a;
  --text: #e5e7eb;
  --muted: #9aa1ac;
  --border: transparent;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-contrast: #0b1220;
  --primary-shadow: rgba(59, 130, 246, .38);
  --danger: #f87171;
  --danger-bg: #3f1d1d;
  --warning: #fbbf24;
  --warning-bg: #3f2f10;
  --success: #4ade80;
  --success-bg: #133a22;
  --shadow: 0 1px 3px rgba(0, 0, 0, .5);
  --bg-overlay: rgba(10, 12, 16, .5);
  --bg-gradient: linear-gradient(135deg, rgba(59, 130, 246, .3) 0%, rgba(10, 12, 16, .78) 55%);
  --logo-glow: 0 0 0 3px rgba(59, 130, 246, .28), 0 0 22px rgba(59, 130, 246, .18);
  --logo-shadow-color: rgba(0, 0, 0, .55);
  --glass-bg: rgba(23, 26, 33, .38);
  --glass-bg-strong: rgba(23, 26, 33, .38);
  --glass-border: transparent;
  --glass-highlight: inset 0 1px 0 rgba(255, 255, 255, .08);
  --glass-blur: 20px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1d212a;
    --text: #e5e7eb;
    --muted: #9aa1ac;
    --border: transparent;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-contrast: #0b1220;
  --primary-shadow: rgba(59, 130, 246, .38);
    --danger: #f87171;
    --danger-bg: #3f1d1d;
    --warning: #fbbf24;
    --warning-bg: #3f2f10;
    --success: #4ade80;
    --success-bg: #133a22;
    --shadow: 0 1px 3px rgba(0, 0, 0, .5);
    --bg-overlay: rgba(10, 12, 16, .5);
    --bg-gradient: linear-gradient(135deg, rgba(59, 130, 246, .3) 0%, rgba(10, 12, 16, .78) 55%);
    --logo-glow: 0 0 0 3px rgba(59, 130, 246, .28), 0 0 22px rgba(59, 130, 246, .18);
    --logo-shadow-color: rgba(0, 0, 0, .55);
    --glass-bg: rgba(23, 26, 33, .38);
    --glass-bg-strong: rgba(23, 26, 33, .38);
    --glass-border: transparent;
    --glass-highlight: inset 0 1px 0 rgba(255, 255, 255, .08);
    --glass-blur: 20px;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
}

body.bg-login {
  background-image: var(--bg-gradient), url("/static/001.jpg");
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  background-repeat: no-repeat;
}

body.bg-dashboard {
  background-image: var(--bg-gradient), url("/static/mc00.jpg");
  background-size: cover;
  background-position: 80% center;
  background-attachment: fixed;
  background-repeat: no-repeat;
}

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 16px;
  margin-bottom: 20px;
}
.topbar .brand { min-width: 220px; }

.logo-float {
  position: relative;
  display: inline-block;
  animation: logo-bob 4.5s ease-in-out infinite;
}
.logo-float::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -8px;
  width: 60%;
  height: 10px;
  background: var(--logo-shadow-color);
  border-radius: 50%;
  filter: blur(6px);
  transform: translateX(-50%);
  animation: logo-shadow-pulse 4.5s ease-in-out infinite;
  z-index: -1;
}

.topbar img.logo {
  display: block;
  height: 88px;
  width: 88px;
  object-fit: cover;
  border-radius: 50%;
  box-shadow: var(--shadow), var(--logo-glow);
  border: 2px solid var(--surface);
}

@keyframes logo-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-7px); }
}
@keyframes logo-shadow-pulse {
  0%, 100% { transform: translateX(-50%) scale(1); opacity: .55; }
  50% { transform: translateX(-50%) scale(.72); opacity: .28; }
}
@media (prefers-reduced-motion: reduce) {
  .logo-float, .logo-float::after { animation: none; }
}

.topbar .brand { flex: 1; }
.topbar .brand h1 { margin: 0; font-size: 1.25rem; }
.topbar .brand p { margin: 0; color: var(--muted); font-size: .85rem; }

.icon-btn {
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  box-shadow: var(--glass-highlight);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 12px;
  cursor: pointer;
  font-size: .95rem;
  text-decoration: none;
}
.icon-btn:hover { border-color: var(--primary); }

.card {
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow), var(--glass-highlight);
  padding: 18px;
  margin-bottom: 18px;
}

input, select {
  background: var(--surface-2);
  border: none;
  color: var(--text);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: .95rem;
  width: 100%;
  box-shadow: var(--glass-highlight), inset 0 0 0 1px rgba(127, 127, 127, .08);
  transition: box-shadow .15s ease, background .15s ease;
}
input::placeholder { color: var(--muted); }
input:hover, select:hover { background: var(--surface); }
input:focus, select:focus {
  outline: none;
  background: var(--surface);
  box-shadow: var(--logo-glow), var(--glass-highlight);
}

button.primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-hover));
  color: var(--primary-contrast);
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 14px var(--primary-shadow);
  transition: transform .12s ease, box-shadow .12s ease, filter .12s ease;
}
button.primary:hover { filter: brightness(1.06); box-shadow: 0 6px 18px var(--primary-shadow); transform: translateY(-1px); }
button.primary:active { transform: translateY(0); filter: brightness(.98); }

button.ghost {
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  color: var(--text);
  border: none;
  border-radius: var(--radius);
  padding: 10px 16px;
  cursor: pointer;
  box-shadow: var(--glass-highlight);
  transition: box-shadow .15s ease, transform .12s ease;
}
button.ghost:hover { box-shadow: var(--logo-glow), var(--glass-highlight); }
button.ghost:active { transform: translateY(1px); }
button.ghost:disabled { opacity: .45; cursor: default; }
button.ghost:disabled:hover { box-shadow: var(--glass-highlight); }

.quick-search {
  display: flex;
  gap: 10px;
}
.quick-search input { flex: 1; }

.extra-filters {
  margin-top: 14px;
}
.extra-filters summary {
  cursor: pointer;
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--muted);
  -webkit-tap-highlight-color: transparent;
}
.extra-filters summary::-webkit-details-marker { display: none; }
.extra-filters summary:hover { color: var(--primary); }
.extra-filters summary .chevron {
  transition: transform .2s ease;
  display: inline-block;
}
.extra-filters[open] summary .chevron { transform: rotate(180deg); }
.extra-filters[open] summary { margin-bottom: 12px; }

.filters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}
.filters-grid label {
  display: block;
  font-size: .8rem;
  color: var(--muted);
  margin-bottom: 4px;
}
.range-pair { display: flex; gap: 6px; align-items: center; }

.filters-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.results-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: .85rem;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  box-shadow: var(--shadow), var(--glass-highlight);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  text-align: left;
  padding: 12px;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: .9rem;
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
tr.doc-row { cursor: pointer; }

td.num, th.num { text-align: right; }

.co-art { font-weight: 600; color: var(--primary); }
.art-des { white-space: normal; min-width: 220px; }
.precio { font-weight: 700; color: var(--danger); }

tr.dia-header td {
  background: var(--surface-2);
  font-weight: 700;
  color: var(--primary);
}
tr.dia-subtotal td {
  font-weight: 700;
  border-top: 1px solid var(--border);
}
tr.gran-total td {
  font-weight: 800;
  font-size: 1rem;
  border-top: 2px solid var(--primary);
}

.badge {
  display: inline-block;
  min-width: 28px;
  padding: 3px 8px;
  border-radius: 999px;
  font-weight: 600;
  font-size: .82rem;
  text-align: center;
}
.badge.stock-none { background: var(--danger-bg); color: var(--danger); }
.badge.stock-low { background: var(--warning-bg); color: var(--warning); }
.badge.stock-ok { background: var(--success-bg); color: var(--success); }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}
.pagination span { color: var(--muted); font-size: .85rem; }

.loading-bar {
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--primary), transparent);
  background-size: 200% 100%;
  border-radius: 3px;
  opacity: 0;
  animation: none;
}
.loading-bar.active {
  opacity: 1;
  animation: shimmer 1.1s linear infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---- Login ---- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(160%);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow), var(--glass-highlight);
  padding: 32px 28px;
  text-align: center;
}
.login-card .logo-float { margin-bottom: 12px; }
.login-card img {
  display: block;
  height: 144px;
  width: 144px;
  object-fit: cover;
  border-radius: 50%;
  box-shadow: var(--shadow), var(--logo-glow);
  border: 3px solid var(--surface);
}
.login-card h1 { font-size: 1.3rem; margin: 0 0 4px; }
.login-card p.subtitle { color: var(--muted); margin: 0 0 24px; font-size: .85rem; }
.login-card form { display: flex; flex-direction: column; gap: 12px; text-align: left; }
.login-card .field label { font-size: .8rem; color: var(--muted); display: block; margin-bottom: 4px; }
.login-card button.primary { margin-top: 8px; width: 100%; }
.login-error {
  background: var(--danger-bg);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 8px 12px;
  font-size: .85rem;
  margin-bottom: 4px;
}

/* ---- Impresión ---- */
@media print {
  @page { size: portrait; margin: 10mm; }
  .no-print { display: none !important; }
  body { background: #fff !important; background-image: none !important; color: #000; }
  .page { max-width: 100%; padding: 0; }
  .card, .table-wrap {
    background: #fff !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }
  .table-wrap { border: none; }
  table {
    min-width: 0;
    width: 100%;
    table-layout: fixed;
    font-size: 10px;
    line-height: 1.4;
    border-collapse: separate;
    border-spacing: 0;
  }
  th, td {
    padding: 4px 5px;
    white-space: normal;
    word-break: break-word;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  tr { break-inside: avoid; page-break-inside: avoid; }
  thead { display: table-header-group; }
  thead th {
    position: static;
    background: #eee !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    font-size: 8px;
    line-height: 1.3;
    padding: 3px 4px;
  }
  td.co-art, td.art-des { font-size: 9.5px; }
  .badge { background: none !important; color: #000 !important; padding: 0; }

  tbody tr:not(.dia-header):not(.dia-subtotal):not(.gran-total):nth-child(even) td {
    background: #f0f0f0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  tr.dia-header td {
    background: #ddd !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    font-weight: 700;
  }
  tr.dia-subtotal td, tr.gran-total td {
    font-weight: 700;
    border-top: 1px solid #000;
  }

  #print-header {
    font-size: 11px;
    line-height: 1.3;
    margin-bottom: 6px;
    padding-bottom: 4px;
    border-bottom: 1px solid #000;
  }
}

@media (max-width: 640px) {
  .page { padding: 14px 12px 40px; }

  /* Encabezado: logo más chico, texto compacto, botones a tamaño táctil */
  .topbar {
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 14px;
  }
  .topbar img.logo { height: 52px; width: 52px; }
  .topbar .brand { flex-basis: 100%; order: 1; }
  .topbar .brand h1 { font-size: 1.05rem; }
  .topbar .brand p { font-size: .78rem; }
  .topbar .icon-btn { order: 2; }
  .icon-btn {
    min-height: 42px;
    padding: 8px 10px;
    font-size: .85rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Objetivos táctiles más grandes y sin zoom automático de iOS (min 16px) */
  input, select {
    font-size: 16px;
    min-height: 44px;
  }
  button.primary, button.ghost {
    min-height: 44px;
    font-size: .95rem;
  }

  .card { padding: 14px; }
  .quick-search { gap: 8px; }
  .quick-search button.primary { padding: 0 16px; white-space: nowrap; }

  .filters-grid { grid-template-columns: 1fr; gap: 14px; }
  .filters-actions button { flex: 1; }

  .results-meta { margin-bottom: 8px; font-size: .8rem; }

  /* Tabla -> lista de tarjetas, mucho más legible que hacer scroll lateral */
  .table-wrap {
    border: none;
    background: transparent;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    box-shadow: none;
    overflow: visible;
  }
  /* display:block en toda la cadena tabla->tbody->tr: si <table> queda como
     display:table, sus hijos se encogen al contenido en vez de llenar el ancho */
  table { display: block; min-width: 0; width: 100%; }
  thead { display: none; }
  tbody { display: block; }
  /* La fila se vuelve una grilla real: los <td> auto-colocados dentro de un
     grid se comportan de forma mucho más predecible que flex/inline-flex sobre
     celdas de tabla (Chrome insiste en tratarlas como bloques en ese caso) */
  tbody tr {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    align-items: center;
    gap: 6px 8px;
    background: var(--glass-bg);
    backdrop-filter: blur(var(--glass-blur)) saturate(160%);
    -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(160%);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow), var(--glass-highlight);
    padding: 12px 14px;
    margin-bottom: 10px;
  }
  tbody td {
    border: none;
    padding: 0;
    white-space: normal;
    font-size: .88rem;
  }
  tbody td.co-art {
    grid-column: 1 / span 2;
    grid-row: 1;
    font-size: 1rem;
  }
  tbody td.precio {
    grid-column: 3;
    grid-row: 1;
    text-align: right;
    font-size: 1.05rem;
  }
  tbody td.art-des {
    grid-column: 1 / -1;
    grid-row: 2;
    padding-bottom: 4px;
    min-width: 0;
  }
  /* Los 6 <td> de stock (Total + 5 almacenes) se auto-colocan de a 3 por fila */
  tbody td.num:not(.precio) {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    justify-self: stretch;
    text-align: center;
    gap: 3px;
  }
  tbody td.num:not(.precio)::before {
    content: attr(data-label);
    font-size: .62rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .02em;
    white-space: nowrap;
  }

  .pagination {
    gap: 8px;
  }
  .pagination button.ghost {
    flex: 1;
    min-height: 44px;
  }
  .pagination span { font-size: .8rem; white-space: nowrap; }

  .login-card { padding: 26px 20px; }
  .login-card img { height: 96px; width: 96px; }
}
