/* ============================================================
   RSD ASISTENCIA - Plantilla de presupuesto A4 imprimible
   Diseño profesional adaptado para pantalla y print
   ============================================================ */

:root {
  --rsd-primary: #1e3a8a;       /* azul marino del logo */
  --rsd-secondary: #3b82f6;     /* azul cielo del logo */
  --rsd-accent: #f59e0b;        /* amarillo cálido para totales */
  --rsd-dark: #0f172a;
  --rsd-text: #1f2937;
  --rsd-muted: #4b5563;         /* gris oscuro (zinc-600) - mejor contraste en print */
  --rsd-muted-soft: #6b7280;    /* gris suave para etiquetas decorativas */
  --rsd-border: #e5e7eb;
  --rsd-bg-soft: #f8fafc;
  --rsd-bg-row: #f1f5f9;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: var(--rsd-text);
  font-size: 10.5pt;
  line-height: 1.45;
  background: #d1d5db;
}

/* Cada hoja A4 en pantalla: tamaño exacto y sombra */
.sheet {
  width: 210mm;
  min-height: 297mm;
  margin: 12mm auto;
  padding: 18mm 16mm 22mm 16mm;
  background: #fff;
  box-shadow: 0 6px 24px rgba(0,0,0,.18);
  position: relative;
  page-break-after: always;
}
.sheet:last-child { page-break-after: auto; }

/* ============================================================
   PORTADA
   ============================================================ */

.cover {
  display: flex;
  flex-direction: column;
}

.cover-header {
  background: linear-gradient(135deg, var(--rsd-primary) 0%, var(--rsd-secondary) 100%);
  color: #fff;
  padding: 22mm 16mm 14mm 16mm;
  margin: -18mm -16mm 0 -16mm;
  position: relative;
  overflow: hidden;
}
.cover-header::after {
  content: "";
  position: absolute;
  right: -40mm;
  top: -40mm;
  width: 90mm;
  height: 90mm;
  background: rgba(255,255,255,.07);
  border-radius: 50%;
}
.cover-header::before {
  content: "";
  position: absolute;
  left: -30mm;
  bottom: -30mm;
  width: 70mm;
  height: 70mm;
  background: rgba(255,255,255,.06);
  border-radius: 50%;
}

.cover-logo {
  background: #fff;
  padding: 6mm 8mm;
  border-radius: 6px;
  display: inline-block;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  position: relative;
  z-index: 2;
}
.cover-logo img { height: 18mm; display: block; }

.cover-title {
  margin-top: 10mm;
  font-size: 26pt;
  font-weight: 800;
  letter-spacing: -0.5px;
  position: relative;
  z-index: 2;
}
.cover-subtitle {
  font-size: 12pt;
  font-weight: 500;
  opacity: 1;
  margin-top: 2mm;
  position: relative;
  z-index: 2;
}

.cover-numero {
  position: absolute;
  right: 16mm;
  top: 22mm;
  background: rgba(255,255,255,.95);
  color: var(--rsd-primary);
  padding: 4mm 7mm;
  border-radius: 4px;
  font-weight: 700;
  font-size: 12pt;
  z-index: 3;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
}

.cover-meta-row {
  display: flex;
  gap: 8mm;
  margin-top: 14mm;
  padding: 0;
}

.cover-card {
  flex: 1;
  border: 1px solid var(--rsd-border);
  border-radius: 6px;
  padding: 6mm 7mm;
  background: var(--rsd-bg-soft);
}
.cover-card h3 {
  margin: 0 0 3mm 0;
  font-size: 8pt;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--rsd-secondary);
  font-weight: 700;
  border-bottom: 2px solid var(--rsd-secondary);
  padding-bottom: 1.5mm;
  display: inline-block;
}
.cover-card .nombre {
  font-size: 13pt;
  font-weight: 700;
  color: var(--rsd-dark);
  margin-bottom: 1.5mm;
}
.cover-card .linea {
  font-size: 10pt;
  color: var(--rsd-text);
  margin: 0.7mm 0;
}
.cover-card .linea strong { color: var(--rsd-dark); }

.cover-obra {
  margin-top: 8mm;
  border: 2px solid var(--rsd-primary);
  border-radius: 6px;
  padding: 6mm 8mm;
}
.cover-obra h3 {
  margin: 0 0 4mm 0;
  font-size: 9pt;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--rsd-primary);
  font-weight: 700;
}
.cover-obra-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4mm 10mm;
}
.cover-obra-grid .item .lab {
  font-size: 8pt;
  color: var(--rsd-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 1mm;
}
.cover-obra-grid .item .val {
  font-size: 11pt;
  color: var(--rsd-dark);
  font-weight: 600;
}

.cover-footer-nota {
  margin-top: auto;
  padding: 8mm 0 0 0;
  border-top: 1px dashed var(--rsd-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8mm;
  font-size: 9pt;
  color: var(--rsd-muted);
  flex-wrap: wrap;
}
.cover-footer-nota > div { min-width: 0; }
.cover-footer-nota .numero-pagina-portada {
  font-weight: 700;
  color: var(--rsd-primary);
  white-space: nowrap;
  text-align: right;
  flex-shrink: 0;
}

/* ============================================================
   PÁGINAS DE CAPÍTULOS
   ============================================================ */

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid var(--rsd-primary);
  padding-bottom: 4mm;
  margin-bottom: 6mm;
}
.page-header .left { display: flex; align-items: center; gap: 4mm; }
.page-header img { height: 10mm; }
.page-header .titulo {
  font-weight: 700;
  color: var(--rsd-primary);
  font-size: 11pt;
  letter-spacing: -0.3px;
}
.page-header .right {
  font-size: 9pt;
  color: var(--rsd-text);
  text-align: right;
  line-height: 1.3;
  font-weight: 500;
}
.page-header .right strong { color: var(--rsd-dark); }

.capitulo-cinta {
  display: flex;
  align-items: stretch;
  margin: 3mm 0 5mm 0;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0,0,0,.05);
  page-break-inside: avoid;
  page-break-after: avoid;
}
.capitulo-cinta .codigo {
  background: var(--rsd-primary);
  color: #fff;
  font-size: 22pt;
  font-weight: 800;
  padding: 4mm 7mm;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 18mm;
}
.capitulo-cinta .nombre {
  background: linear-gradient(90deg, var(--rsd-secondary) 0%, #60a5fa 100%);
  color: #fff;
  flex: 1;
  padding: 4mm 7mm;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.capitulo-cinta .nombre .lab {
  font-size: 8.5pt;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  opacity: 1;
  font-weight: 600;
}
.capitulo-cinta .nombre .titulo {
  font-size: 14pt;
  font-weight: 700;
  letter-spacing: -0.3px;
  margin-top: 0.5mm;
}

table.partidas {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 3mm;
  font-size: 9.5pt;
}
table.partidas thead {
  display: table-header-group;
}
table.partidas thead th {
  background: var(--rsd-bg-row);
  color: var(--rsd-dark);
  font-weight: 700;
  font-size: 8.5pt;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2.5mm 3mm;
  text-align: left;
  border-bottom: 2px solid var(--rsd-primary);
}
table.partidas thead th.num { text-align: right; }
table.partidas thead th.center { text-align: center; }

table.partidas .partida-row td {
  padding: 3mm 3mm 1.5mm 3mm;
  vertical-align: top;
  border-top: 1px solid var(--rsd-border);
}
table.partidas .partida-row .codigo {
  font-weight: 700;
  color: var(--rsd-primary);
  white-space: nowrap;
  width: 14mm;
}
.origen-tag-print {
  display: block;
  margin-top: 1mm;
  padding: 0.5mm 1.5mm;
  border-radius: 1mm;
  font-size: 6.5pt;
  font-weight: 800;
  letter-spacing: 0.4px;
  text-align: center;
  text-transform: uppercase;
}
.origen-tag-print.pro  { background: #d1fae5; color: #065f46; }
.origen-tag-print.rsd  { background: #fef3c7; color: #92400e; }
.origen-tag-print.ayto { background: #dbeafe; color: #1e3a8a; }
.origen-tag-print.ia   { background: #ede9fe; color: #5b21b6; }
.origen-tag-print.man  { background: #e5e7eb; color: #4b5563; }
.origen-tag-print.competidor { background: #fed7aa; color: #9a3412; }
.origen-tag-print.internet { background: #d1fae5; color: #065f46; }

/* Resaltado por color de la partida en la versión imprimible */
table.partidas tr.partida-row.resaltada-amarillo td,
table.partidas tr.descripcion-row.resaltada-amarillo td { background: #fef3c7; }
table.partidas tr.partida-row.resaltada-verde td,
table.partidas tr.descripcion-row.resaltada-verde td    { background: #d1fae5; }
table.partidas tr.partida-row.resaltada-azul td,
table.partidas tr.descripcion-row.resaltada-azul td     { background: #dbeafe; }
table.partidas tr.partida-row.resaltada-rosa td,
table.partidas tr.descripcion-row.resaltada-rosa td     { background: #fce7f3; }
table.partidas tr.partida-row.resaltada-naranja td,
table.partidas tr.descripcion-row.resaltada-naranja td  { background: #fed7aa; }

/* Nota al cliente debajo de la partida en versión imprimible */
table.partidas tr.nota-cliente-row > td { padding: 0 0 2mm 0 !important; }
.nota-cliente-print {
  background: linear-gradient(90deg, #fef3c7, #fffbeb);
  border-left: 3px solid #f59e0b;
  padding: 2mm 3mm;
  font-size: 8.5pt;
  color: #78350f;
  border-radius: 0 1.5mm 1.5mm 0;
  line-height: 1.45;
  margin: 0 1mm 1mm 8mm;
}
.nota-cliente-print strong { color: #92400e; }
table.partidas .partida-row .titulo {
  font-weight: 600;
  color: var(--rsd-dark);
  font-size: 10pt;
  margin-bottom: 1mm;
}
table.partidas .partida-row .ud { width: 12mm; text-align: center; }
table.partidas .partida-row .cant { width: 18mm; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.partidas .partida-row .precio { width: 20mm; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.partidas .partida-row .dto { width: 14mm; text-align: center; font-variant-numeric: tabular-nums; color: var(--rsd-muted); font-size: 9pt; }
table.partidas .partida-row .dto .dto-badge {
  background: #fef3c7;
  color: #92400e;
  padding: 0.5mm 1.5mm;
  border-radius: 1mm;
  font-weight: 700;
  font-size: 8.5pt;
}
table.partidas .partida-row .importe { width: 24mm; text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; white-space: nowrap; }

table.partidas .descripcion-row td {
  padding: 0 3mm 3mm 17mm;
  font-size: 9pt;
  color: var(--rsd-text);
  line-height: 1.45;
  border-top: none;
  white-space: pre-line;
}

/* Materiales asociados — sub-fila indentada bajo la partida */
table.partidas .material-row-print td {
  padding: 1mm 3mm 1mm 3mm;
  font-size: 8.8pt;
  border-top: none;
  background: #fafbff;
  color: #475569;
}
table.partidas .material-row-print .titulo {
  font-weight: 500;
  padding-left: 14mm;
  font-size: 9pt;
}
table.partidas .material-row-print .importe {
  font-weight: 600;
  color: #1e3a8a;
}
.mat-ref {
  font-family: 'Menlo', 'Consolas', monospace;
  font-size: 8pt;
  color: var(--rsd-muted);
  font-weight: 400;
}

table.partidas tr.partida-row { page-break-inside: avoid; }

.subtotal-capitulo {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 6mm;
  padding: 3mm 4mm;
  background: var(--rsd-bg-soft);
  border: 1px solid var(--rsd-border);
  border-left: 4px solid var(--rsd-primary);
  border-radius: 3px;
  margin-top: 3mm;
  page-break-inside: avoid;
}
.subtotal-capitulo .label {
  font-weight: 700;
  color: var(--rsd-dark);
  font-size: 10pt;
  letter-spacing: 0.5px;
}
.subtotal-capitulo .valor {
  font-weight: 800;
  color: var(--rsd-primary);
  font-size: 13pt;
  font-variant-numeric: tabular-nums;
  min-width: 28mm;
  text-align: right;
}

/* ============================================================
   RESUMEN
   ============================================================ */

.seccion-titulo {
  font-size: 16pt;
  font-weight: 800;
  color: var(--rsd-primary);
  border-bottom: 3px solid var(--rsd-primary);
  padding-bottom: 2mm;
  margin: 0 0 6mm 0;
  letter-spacing: -0.3px;
}

table.resumen {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 6mm;
}
table.resumen th {
  background: var(--rsd-primary);
  color: #fff;
  padding: 3mm 4mm;
  text-align: left;
  font-size: 9pt;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}
table.resumen th.num { text-align: right; }
table.resumen td {
  padding: 2.8mm 4mm;
  border-bottom: 1px solid var(--rsd-border);
  font-size: 10pt;
}
table.resumen td.codigo {
  font-weight: 700;
  color: var(--rsd-primary);
  width: 18mm;
}
table.resumen td.importe {
  text-align: right;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  width: 32mm;
}
table.resumen tr:nth-child(even) td { background: var(--rsd-bg-soft); }

.totales-finales {
  margin-top: 4mm;
  padding: 6mm 8mm;
  background: var(--rsd-bg-soft);
  border-radius: 6px;
  border: 1px solid var(--rsd-border);
}
.totales-finales .fila {
  display: flex;
  justify-content: space-between;
  padding: 2mm 0;
  font-size: 11pt;
}
.totales-finales .fila.subtotal { border-bottom: 1px solid var(--rsd-border); }
.totales-finales .fila.iva { color: var(--rsd-muted); font-size: 10pt; }
.totales-finales .fila.total {
  margin-top: 4mm;
  padding: 4mm 5mm;
  background: var(--rsd-dark);
  color: #fff;
  border-radius: 4px;
  font-size: 14pt;
  font-weight: 800;
}
.totales-finales .fila .valor { font-variant-numeric: tabular-nums; }

/* ============================================================
   CONDICIONES
   ============================================================ */

.condiciones-lista {
  margin: 0 0 5mm 0;
  padding-left: 6mm;
}
.condiciones-lista li {
  margin-bottom: 2.5mm;
  font-size: 10pt;
  line-height: 1.5;
}

.condiciones-intro {
  font-size: 9.5pt;
  color: var(--rsd-text);
  font-style: italic;
  margin: 0 0 4mm 0;
  padding: 3mm 4mm;
  background: var(--rsd-bg-soft);
  border-left: 3px solid var(--rsd-secondary);
  border-radius: 2px;
}

.condiciones-bloque {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3mm 6mm;
}
.condicion-seccion {
  page-break-inside: avoid;
  break-inside: avoid;
}
.condicion-titulo {
  margin: 0 0 1.5mm 0;
  font-size: 9pt;
  font-weight: 800;
  color: var(--rsd-primary);
  letter-spacing: 0.4px;
  text-transform: uppercase;
  border-bottom: 1px solid var(--rsd-border);
  padding-bottom: 1mm;
}
.condicion-puntos {
  margin: 0 0 3mm 0;
  padding-left: 4mm;
  list-style: disc;
}
.condicion-puntos li {
  font-size: 8.2pt;
  line-height: 1.35;
  margin-bottom: 1.2mm;
  text-align: justify;
  color: var(--rsd-text);
}

.acta-conformidad {
  margin-top: 6mm;
  padding: 4mm 5mm;
  background: var(--rsd-bg-soft);
  border: 1px solid var(--rsd-border);
  border-radius: 4px;
  font-size: 9pt;
  line-height: 1.5;
  text-align: center;
}

.firma .linea-sub {
  border-top: none;
  font-size: 8pt;
  color: var(--rsd-muted);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  margin-top: 1mm;
  padding-top: 0;
}

.bloque-pago {
  border: 2px solid var(--rsd-primary);
  border-radius: 6px;
  overflow: hidden;
  margin: 5mm 0;
  page-break-inside: avoid;
}
.bloque-pago h4 {
  background: var(--rsd-primary);
  color: #fff;
  margin: 0;
  padding: 3mm 5mm;
  font-size: 10pt;
  letter-spacing: 0.6px;
  text-transform: uppercase;
}
.bloque-pago .filas {
  display: flex;
}
.bloque-pago .fila {
  flex: 1;
  text-align: center;
  padding: 4mm 3mm;
  border-right: 1px solid var(--rsd-border);
}
.bloque-pago .fila:last-child { border-right: none; }
.bloque-pago .fila .pct {
  font-size: 22pt;
  font-weight: 800;
  color: var(--rsd-primary);
  line-height: 1;
}
.bloque-pago .fila .txt {
  font-size: 9pt;
  color: var(--rsd-muted);
  margin-top: 1.5mm;
}

.parrafo-legal {
  font-size: 8pt;
  color: var(--rsd-muted);
  text-align: justify;
  line-height: 1.4;
  margin: 3mm 0;
}

.firmas {
  display: flex;
  justify-content: space-between;
  margin-top: 18mm;
  page-break-inside: avoid;
}
.firma {
  text-align: center;
  width: 75mm;
}
.firma .linea {
  border-top: 1px solid var(--rsd-dark);
  padding-top: 2mm;
  font-size: 9pt;
  font-weight: 700;
  color: var(--rsd-dark);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

/* ============================================================
   IMPRESIÓN
   ============================================================ */

@media print {
  /* CRÍTICO: por defecto los navegadores no imprimen fondos de color
     ni gradientes (para ahorrar tinta). Esto fuerza a Edge/Chrome a
     imprimir EXACTAMENTE los colores de fondo definidos en CSS,
     incluido el degradado azul de la portada y las cintas de capítulo. */
  *, *::before, *::after {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    color-adjust: exact !important;
  }

  /* Forzar TODO a blanco en impresión: html, body, sheet, sections.
     Si no, en algunos navegadores el body gris (de la vista en pantalla)
     se cuela por debajo de la última hoja cuando el contenido no llega
     hasta el borde inferior. */
  html, body { background: #fff !important; }
  .sheet {
    box-shadow: none;
    margin: 0;
    width: auto;
    min-height: auto;
    padding: 0;
    background: #fff !important;
  }
  /* La última hoja (firma) no tiene page-break-after: always, así que
     el navegador puede dejar espacio en blanco debajo del .firmas.
     Con esto fijo nada que tenga fondo se cuela. */
  .sheet:last-child { background: #fff !important; }
  section.sheet > * { background-clip: padding-box; }
  .no-print { display: none !important; }
  .cover-header { margin: 0 -14mm; padding: 18mm 14mm 12mm 14mm; }

  /* La portada usa @page :first con margin 0 (gradiente full-bleed).
     El resto del contenido necesita su propio padding lateral para no
     pegarse al borde físico del papel (las impresoras dejan ~3-5mm). */
  .sheet.cover {
    padding: 0 14mm;
    min-height: 277mm; /* 297mm A4 - 18mm bottom margin - 2mm de seguridad */
    box-sizing: border-box;
  }
  .cover-footer-nota {
    padding-bottom: 4mm;
  }

  @page {
    size: A4;
    margin: 14mm 14mm 18mm 14mm;
    @bottom-center {
      content: "Página " counter(page) " de " counter(pages);
      font-family: 'Inter', system-ui, sans-serif;
      font-size: 8.5pt;
      color: #6b7280;
      padding-top: 5mm;
    }
    @bottom-left {
      content: "RSD ASISTENCIA, S.L. · CIF B86439155";
      font-family: 'Inter', system-ui, sans-serif;
      font-size: 7.5pt;
      color: #9ca3af;
      padding-top: 5mm;
    }
    @bottom-right {
      content: "reformas@rsdasistencia.com";
      font-family: 'Inter', system-ui, sans-serif;
      font-size: 7.5pt;
      color: #9ca3af;
      padding-top: 5mm;
    }
  }
  @page :first {
    margin: 0 0 18mm 0;
    @bottom-center { content: ""; }
    @bottom-left { content: ""; }
    @bottom-right { content: ""; }
  }
}

/* Toolbar superior (no se imprime) */
.toolbar {
  position: sticky;
  top: 0;
  background: var(--rsd-dark);
  color: #fff;
  padding: 3mm 6mm;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.toolbar .acciones { display: flex; gap: 3mm; }
.toolbar button, .toolbar a.btn {
  background: var(--rsd-secondary);
  color: #fff;
  border: none;
  padding: 2mm 5mm;
  border-radius: 4px;
  font-size: 10pt;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.toolbar button:hover, .toolbar a.btn:hover { background: #2563eb; }
.toolbar button.secondary, .toolbar a.btn.secondary {
  background: transparent;
  border: 1px solid rgba(255,255,255,.3);
}

/* ============================================================
   RESPONSIVE — móvil (≤ 700 px). Solo afecta a la VISTA en pantalla
   (`@media screen`), NO a la impresión. La impresión sigue como
   estaba. Aquí escalamos la hoja para que quepa en el ancho del
   móvil y reorganizamos la toolbar para que sea cómoda en táctil.
   ============================================================ */
@media screen and (max-width: 700px) {
  html, body { background: #f3f4f6; }
  .toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 12px;
  }
  .toolbar .titulo { font-size: 14px !important; text-align: center; }
  .toolbar .acciones {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }
  .toolbar button, .toolbar a.btn {
    padding: 10px 6px;
    font-size: 13px;
    text-align: center;
    border-radius: 6px;
  }
  /* Hoja A4 (210 mm) escalada para que entre en pantalla móvil sin
     tener que hacer zoom out. zoom solo afecta a la vista, no al
     resto de cálculos del navegador, ni a `@media print`. */
  .sheet { zoom: 0.5; margin: 6px auto !important; }
}
@media screen and (min-width: 480px) and (max-width: 700px) {
  .sheet { zoom: 0.55; }
}
