/* ==========================================================================
   Preparación de Tarima por FFT — Mi Technologies
   Sistema industrial: blanco / negro / grises + azul institucional.
   Sin tarjetas grandes, sin sombras exageradas, sin colores llamativos.
   ========================================================================== */

:root {
  --black: #111111;
  --gray-900: #1f1f1f;
  --gray-700: #4a4a4a;
  --gray-500: #7a7a7a;
  --gray-300: #d3d6d9;
  --gray-200: #e6e8ea;
  --gray-100: #f4f5f6;
  --white: #ffffff;

  --brand-blue: #0f5f96;
  --brand-blue-dark: #0b4a76;
  --brand-blue-tint: #eaf3fa;

  --font: "Segoe UI", Arial, Helvetica, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--gray-100);
  color: var(--black);
  font-family: var(--font);
}

/* Reserva el espacio de la barra lateral colapsada (ver .app-sidebar) en
   TODAS las páginas — la barra es position:fixed, así que sin este padding
   el contenido quedaría debajo de ella. */
body { padding-left: 64px; }

h1, h2, h3 { margin: 0; }

.hidden { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------------------------------------------------------------------- */
/* BARRA LATERAL — navegación entre los módulos del sistema. Colapsada     */
/* (64px) sólo se asoman los íconos; al pasar el mouse se expande (240px)  */
/* ENCIMA del contenido (position:fixed, no empuja el layout) mostrando el */
/* nombre de cada módulo.                                                   */
/* ---------------------------------------------------------------------- */

.app-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 64px;
  background: var(--white);
  border-right: 1px solid var(--gray-300);
  z-index: 100;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 16px 0 20px;
  transition: width 160ms ease, box-shadow 160ms ease;
}

.app-sidebar:hover,
.app-sidebar:focus-within {
  width: 240px;
  box-shadow: 4px 0 20px rgba(17, 17, 17, 0.16);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  height: 28px;
  margin-bottom: 18px;
  padding: 0 20px;
  flex-shrink: 0;
}
.sidebar-brand img { height: 28px; width: 28px; flex-shrink: 0; border-radius: 6px; }

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 13px 20px;
  color: var(--gray-700);
  text-decoration: none;
  white-space: nowrap;
  border-left: 3px solid transparent;
}
.sidebar-link svg { flex-shrink: 0; width: 22px; height: 22px; }

.sidebar-link span {
  opacity: 0;
  transition: opacity 100ms ease;
  font-size: 0.85rem;
  font-weight: 700;
}
.app-sidebar:hover .sidebar-link span,
.app-sidebar:focus-within .sidebar-link span { opacity: 1; }

.sidebar-link:hover { background: var(--brand-blue-tint); color: var(--brand-blue-dark); }
.sidebar-link:focus-visible { outline: 3px solid var(--brand-blue-tint); outline-offset: -3px; }

.sidebar-link.active {
  color: var(--brand-blue-dark);
  background: var(--brand-blue-tint);
  border-left-color: var(--brand-blue);
}

@media (max-width: 480px) {
  body { padding-left: 52px; }
  .app-sidebar { width: 52px; }
  .sidebar-brand { padding: 0 15px; height: 24px; }
  .sidebar-brand img { height: 24px; width: 24px; }
  .sidebar-link { padding: 12px 15px; gap: 14px; }
}

/* ---------------------------------------------------------------------- */
/* HEADER                                                                   */
/* ---------------------------------------------------------------------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: var(--white);
  border-bottom: 3px solid var(--brand-blue);
  flex-wrap: wrap;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.header-logo { height: 38px; width: auto; flex-shrink: 0; }

.header-titles h1 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--gray-900);
}
.header-titles .subtitle {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--gray-700);
}

.header-right { display: flex; gap: 10px; flex-wrap: wrap; flex-shrink: 0; }

.btn-header {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--brand-blue);
  color: var(--white);
  border: 1.5px solid var(--brand-blue);
}
.btn-header:hover { background: var(--brand-blue-dark); border-color: var(--brand-blue-dark); }

/* ---------------------------------------------------------------------- */
/* CONTENIDO PRINCIPAL                                                      */
/* ---------------------------------------------------------------------- */

.main-content {
  max-width: 1100px;
  margin: 0 auto;
  padding: 18px 16px 32px;
}

/* Barra horizontal de captura ------------------------------------------- */

.capture-bar {
  background: var(--white);
  border: 1px solid var(--gray-300);
  border-radius: 4px;
  padding: 16px 18px;
  margin-bottom: 20px;
}

.capture-form {
  display: flex;
  align-items: end;
  gap: 14px;
  flex-wrap: wrap;
}

.field { display: flex; flex-direction: column; gap: 5px; }
.field-order { flex: 1 1 260px; min-width: 220px; }
.field-date { flex: 0 0 150px; }

.field label {
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--gray-700);
}

.field input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  font-size: 1rem;
  border: 1.5px solid var(--gray-500);
  border-radius: 3px;
  background: var(--white);
  color: var(--black);
  font-family: var(--font);
}

.field input[type="text"]:focus-visible {
  outline: 3px solid var(--brand-blue-tint);
  outline-offset: 0;
  border-color: var(--brand-blue);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.btn {
  appearance: none;
  border: 1.5px solid var(--black);
  background: var(--white);
  color: var(--black);
  padding: 10px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 3px;
  cursor: pointer;
  min-height: 44px;
  font-family: var(--font);
  text-decoration: none;
  box-sizing: border-box;
}

.btn:hover { background: var(--gray-100); }
.btn:active { background: var(--gray-200); }
.btn:focus-visible { outline: 3px solid var(--brand-blue-tint); outline-offset: 1px; border-color: var(--brand-blue); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--black);
  color: var(--white);
  border-color: var(--black);
}
.btn-primary:hover { background: var(--gray-900); }

.btn-ghost {
  border-color: var(--gray-500);
  color: var(--gray-700);
}

.btn-small {
  padding: 8px 12px;
  font-size: 0.8rem;
  min-height: 38px;
}

.form-msg {
  margin: 12px 0 0;
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 1.2em;
}
.form-msg.ok { color: #1a6b1a; }
.form-msg.error { color: #a01313; }

.hint {
  margin: 8px 0 0;
  font-size: 0.75rem;
  color: var(--gray-700);
}

/* Mensaje informativo de impresión --------------------------------------- */

.print-info {
  text-align: center;
  font-size: 0.85rem;
  color: var(--gray-700);
  background: var(--brand-blue-tint);
  border: 1px solid #cfe3f0;
  border-radius: 4px;
  padding: 10px 14px;
  margin: 18px auto 0;
  max-width: 640px;
}

/* Footer ------------------------------------------------------------------ */

.app-footer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--gray-500);
  padding: 16px;
  border-top: 1px solid var(--gray-300);
}

/* ---------------------------------------------------------------------- */
/* Botonera flotante de "Vista previa"                                     */
/* ---------------------------------------------------------------------- */

.preview-toolbar {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 60;
  display: none;
  gap: 10px;
  background: var(--white);
  border: 1.5px solid var(--black);
  border-radius: 4px;
  padding: 10px;
}

/* Modo "Vista previa": oculta el resto de la página y agranda la hoja sobre
   un fondo tipo mesa de trabajo, con la botonera flotante Imprimir/Cerrar. */
body.preview-mode .app-header,
body.preview-mode .capture-bar,
body.preview-mode .print-info,
body.preview-mode .app-footer {
  display: none;
}

body.preview-mode .preview-toolbar { display: flex; }

body.preview-mode .sheet-section {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: #58595c;
  overflow: auto;
  padding: 70px 16px 40px;
}

body.preview-mode .preview-wrap {
  width: min(880px, 92vw);
  margin: auto;
}

body.preview-mode .print-area {
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  border-color: transparent;
}

/* ==========================================================================
   HOJA DE ETIQUETA — proporción de Carta HORIZONTAL (11in x 8.5in)
   preservada en pantalla mediante container queries; en impresión usa
   unidades físicas absolutas exactas a la página.
   ========================================================================== */

.sheet-section {
  display: flex;
  justify-content: center;
}

.preview-wrap {
  container-type: inline-size;
  width: min(880px, 100%);
  aspect-ratio: 11 / 8.5;
  margin: 0 auto;
}

.print-area {
  width: 100%;
  height: 100%;
  background: var(--white);
  border: 1px solid var(--gray-300);
  padding: 2.7cqw 3.6cqw 6cqw;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: var(--font);
  color: var(--black);
  overflow: hidden;
}

.print-inner {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.label-logo {
  align-self: flex-start; /* esquina superior IZQUIERDA, no centrado */
  width: 15cqw;
  height: auto; /* alto automático: conserva la proporción real, no deforma */
  margin-top: 1.6cqw; /* baja el logo un poco, casi no se veía pegado al borde */
  margin-bottom: 8cqw; /* logo se queda fijo; el resto sube reduciendo este espacio */
}

.label-title {
  width: 100%;
  font-size: 8.2cqw;
  font-weight: 800;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  line-height: 1.05;
  margin: 0 0 4.8cqw; /* más separación respecto al valor de la orden */
}

.order-block {
  width: 100%;
  text-align: center;
  margin-bottom: 3cqw;
}

/* Sin línea inferior: el valor grande y en negritas es suficiente indicador. */
.order-value {
  font-size: 5.5cqw;
  font-weight: 800;
  letter-spacing: 0.03em;
  min-height: 6.5cqw;
  display: flex;
  align-items: center;
  justify-content: center;
  word-break: break-all;
}

/* grid-template-columns: 1fr auto 1fr — el QR (columna central, ancho de
   contenido) queda matemáticamente centrado en TODA la hoja porque las dos
   columnas laterales son iguales (1fr), sin importar cuánto ocupen FECHA
   o CALIDAD a los lados. */
.label-bottom {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  gap: 2.5cqw;
  margin-top: auto;
}

/* align-self:end alinea el borde INFERIOR de fecha-block con el del QR
   (la columna más alta, que define el alto de la fila) — así la fecha
   queda a la misma altura "baja" que el QR y CALIDAD, en vez de flotar
   arriba por ser un bloque de contenido más corto. */
.fecha-block {
  text-align: center;
  justify-self: start;
  align-self: end;
  margin-left: 3.6cqw;
}

/* Orden pedido: VALOR arriba, línea, y la palabra FECHA debajo de la línea. */
.fecha-value {
  font-size: 2.9cqw;
  font-weight: 600;
}

.fecha-line {
  width: 19cqw;
  margin: 0.6cqw auto 0;
  border-bottom: 1.5px solid var(--black);
}

.fecha-caption {
  font-size: 3.4cqw;
  font-weight: 800;
  letter-spacing: 0.05em;
  margin-top: 0.8cqw;
}

.qr-block {
  display: flex;
  justify-content: center;
  justify-self: center;
  align-self: center; /* el QR es la columna más alta: define el alto de toda la fila */
  position: relative;
  right: 1.8cqw; /* ligeramente a la izquierda del centro exacto, según lo pedido */
}

.qr-canvas {
  width: 26cqw;
  height: 26cqw;
  display: flex;
  align-items: center;
  justify-content: center;
}

.qr-canvas img,
.qr-canvas canvas {
  width: 100% !important;
  height: 100% !important;
  image-rendering: pixelated;
}

.calidad-block {
  text-align: center;
  justify-self: end;
  align-self: end; /* mismo borde inferior que el QR y la fecha */
  margin-right: 3.6cqw; /* un poco más a la izquierda respecto al borde derecho */
}

/* Orden pedido: primero el cuadro, la palabra CALIDAD debajo. */
/* Cuadro para sello/firma física del personal de calidad — RECTANGULAR ANCHO (más ancho que alto). */
.calidad-box {
  width: 23.6cqw;
  height: 19.8cqw;
  margin: 0 auto;
  border: 1.75px solid var(--black);
}

.calidad-caption {
  font-size: 3.2cqw;
  font-weight: 800;
  letter-spacing: 0.05em;
  margin-top: 2.1cqw;
}

/* ---------------------------------------------------------------------- */
/* MODAL: Historial de etiquetas                                          */
/* ---------------------------------------------------------------------- */

/* Bloquea el scroll de la página de atrás mientras el historial está
   abierto (Req 1). Fijar el <body> (en vez de sólo overflow:hidden) es lo
   que impide de verdad la rueda del mouse, Page Up/Down, flechas, Space y
   los gestos táctiles en iOS/Safari, que ignoran overflow:hidden en el body.
   `top` negativo (puesto en JS al abrir) conserva la posición visual exacta
   mientras el body está fijo; overflow-y:scroll conserva el ancho del canal
   de la barra de desplazamiento para que no haya salto horizontal al
   aparecer/desaparecer. Sólo actúa mientras esta clase está presente — no
   deja overflow:hidden permanente y no afecta el modo "Vista previa" ni
   ningún otro modal. */
body.modal-open {
  position: fixed;
  left: 0;
  right: 0;
  width: 100%;
  overflow-y: scroll;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 17, 17, 0.55);
  z-index: 90;
  animation: fadeIn 160ms ease-out;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 91;
  width: min(920px, 94vw);
  max-height: 86vh;
  background: var(--white);
  border-radius: 6px;
  border: 1px solid var(--gray-300);
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
  animation: modalIn 180ms ease-out;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes modalIn {
  from { opacity: 0; transform: translate(-50%, -50%) scale(0.97); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 2px solid var(--black);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1.05rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.modal-close {
  appearance: none;
  border: none;
  background: none;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  color: var(--gray-700);
  padding: 6px 8px;
  border-radius: 3px;
}
.modal-close:hover { background: var(--gray-100); color: var(--black); }
.modal-close:focus-visible { outline: 3px solid var(--brand-blue-tint); border-color: var(--brand-blue); }

.modal-body {
  padding: 16px 20px 20px;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* el scroll vive sólo dentro de .table-wrap, no aquí */
}

/* Historial dentro del modal --------------------------------------------- */

.history-toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
  flex-shrink: 0; /* el buscador siempre visible, no se desplaza con la tabla */
}

.history-toolbar input[type="text"] {
  flex: 1;
  min-width: 180px;
  padding: 9px 12px;
  border: 1.5px solid var(--gray-500);
  border-radius: 3px;
  font-size: 0.9rem;
  font-family: var(--font);
}
.history-toolbar input[type="text"]:focus-visible {
  outline: 3px solid var(--brand-blue-tint);
  border-color: var(--brand-blue);
}

/* Única zona con scroll vertical dentro del modal: sólo las filas se
   desplazan; encabezado del modal y buscador quedan fijos arriba (Req 1.3).
   overscroll-behavior:contain evita que, al llegar al inicio/final de la
   tabla, el scroll siga "de rebote" hacia la página de atrás. */
.table-wrap {
  overflow-x: auto;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  overscroll-behavior: contain;
}

#history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

#history-table th,
#history-table td {
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--gray-200);
  white-space: nowrap;
}

#history-table th {
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.03em;
  color: var(--gray-700);
  border-bottom: 2px solid var(--black);
  position: sticky;
  top: 0;
  background: var(--white);
  z-index: 1;
}

#history-table tbody tr:hover { background: var(--gray-100); }

.row-actions { display: flex; gap: 6px; }

.link-btn {
  border: none;
  background: none;
  color: var(--brand-blue-dark);
  font-weight: 600;
  font-size: 0.8rem;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 3px;
  text-decoration: underline;
}
.link-btn:hover { background: var(--gray-200); }
.link-btn:focus-visible { outline: 3px solid var(--brand-blue-tint); }
.link-btn.danger { color: #a01313; }

/* Marca registros previos a este cambio, que sólo tienen created_at (no una
   impresión confirmada) — ver renderHistory() en app.js. */
.legacy-tag {
  color: var(--gray-500);
  font-size: 0.75rem;
  font-weight: 400;
}

.empty-msg {
  text-align: center;
  color: var(--gray-700);
  font-size: 0.9rem;
  padding: 18px 0 4px;
}

/* ---------------------------------------------------------------------- */
/* Responsive                                                              */
/* ---------------------------------------------------------------------- */

@media (max-width: 780px) {
  .capture-form { align-items: stretch; }
  .field-order, .field-date { flex: 1 1 100%; }
  .actions { width: 100%; }
  .actions .btn { flex: 1 1 calc(50% - 5px); }
}

@media (max-width: 480px) {
  .header-titles h1 { font-size: 1rem; }
  .btn-header span { display: none; }
  .actions .btn { flex: 1 1 100%; }
}

/* ==========================================================================
   IMPRESIÓN — garantiza EXACTAMENTE una hoja Carta HORIZONTAL
   (11in x 8.5in), sólo la etiqueta, sin importar el layout de pantalla.
   ========================================================================== */

@page {
  size: letter landscape;
  margin: 0;
}

@media print {
  html, body {
    width: 11in;
    height: 8.5in;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    background: #ffffff !important;
  }

  .no-print { display: none !important; }
  .app-sidebar { display: none !important; }

  body * { visibility: hidden; }

  #preview-wrap,
  #preview-wrap *,
  #print-area,
  #print-area * {
    visibility: visible !important;
  }

  #preview-wrap {
    container-type: normal;
    position: fixed;
    inset: 0;
    width: 11in;
    height: 8.5in;
    aspect-ratio: auto;
    margin: 0;
    padding: 0;
    overflow: hidden;
  }

  #print-area {
    position: fixed;
    inset: 0;
    width: 11in;
    height: 8.5in;
    margin: 0;
    padding: 0.3in 0.42in 0.65in;
    border: none;
    overflow: hidden;
    box-sizing: border-box;
    page-break-before: avoid;
    page-break-after: avoid;
    page-break-inside: avoid;
    break-before: avoid-page;
    break-after: avoid-page;
    break-inside: avoid-page;
  }

  .label-logo { width: 1.65in; height: auto; margin-top: 0.18in; margin-bottom: 0.75in; }
  .label-title { font-size: 48pt; margin: 0 0 0.45in; }
  .order-block { margin-bottom: 0.26in; }
  .order-value { font-size: 32pt; min-height: 0.5in; }
  .label-bottom { grid-template-columns: 1fr auto 1fr; gap: 0.3in; }
  .fecha-block { margin-left: 0.4in; }
  .fecha-value { font-size: 17pt; }
  .fecha-line { width: 2.1in; margin: 0.06in auto 0; }
  .fecha-caption { font-size: 20pt; margin-top: 0.08in; }
  .qr-block { right: 0.2in; }
  .qr-canvas { width: 2.8in; height: 2.8in; }
  .calidad-block { margin-right: 0.4in; }
  .calidad-box { width: 2.6in; height: 2.2in; margin: 0 auto; }
  .calidad-caption { font-size: 18pt; margin-top: 0.14in; }
}
