/*
 * Estilo de la APLICACIÓN (el taller donde se escribe).
 *
 * Aquí NO vive el estilo del documento: ese es uno solo y está en
 * `src/domain/document-css.ts`, y llega a la pantalla dentro del iframe de la
 * vista previa, que es el mismo HTML que se imprime. Si algo de aquí tocara la
 * tipografía o los márgenes del documento, la vista previa dejaría de ser fiel.
 *
 * Mobile-first de verdad: todo lo de abajo está pensado a 390 px y las únicas
 * dos consultas de medios (720 px, 1024 px) reparten el espacio que sobra.
 */

:root {
  --tinta: #12141a;
  --tinta-suave: #39404d;
  --apagado: #6b7480;
  --linea: #e2e5ec;
  --linea-suave: #eef0f4;
  --acento: #4361ee;
  --acento-hondo: #2b3ec2;
  --acento-suave: #eef1fe;
  --lienzo: #f1f2f6;
  --papel: #ffffff;
  --peligro: #b42318;

  --radio: 16px;
  --radio-chico: 10px;
  --sombra-suave: 0 1px 2px rgba(18, 20, 26, 0.04), 0 12px 28px -20px rgba(18, 20, 26, 0.35);
  --sombra-alta: 0 2px 6px rgba(18, 20, 26, 0.06), 0 24px 48px -28px rgba(18, 20, 26, 0.45);

  --salida: cubic-bezier(0.23, 1, 0.32, 1);
  --entrada: cubic-bezier(0.4, 0, 1, 1);

  --alto-barra: 60px;
  --alto-acciones: 76px;

  --texto: 'Plus Jakarta Sans', 'Helvetica Neue', Arial, sans-serif;
  --mono: 'JetBrains Mono', 'SFMono-Regular', Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  padding-bottom: calc(var(--alto-acciones) + env(safe-area-inset-bottom, 0px));
  background-color: var(--lienzo);
  /* Textura de taller: retícula finísima, no un color plano. */
  background-image:
    radial-gradient(circle at 1px 1px, rgba(18, 20, 26, 0.055) 1px, transparent 0);
  background-size: 22px 22px;
  color: var(--tinta);
  font-family: var(--texto);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ---------- Utilidades ---------- */

.oculto {
  display: none;
}

.oculto-visual {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.saltar {
  position: absolute;
  top: -100px;
  left: 12px;
  z-index: 60;
  padding: 10px 16px;
  border-radius: 0 0 var(--radio-chico) var(--radio-chico);
  background: var(--tinta);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  transition: top 180ms var(--salida);
}

.saltar:focus {
  top: 0;
}

:where(a, button, input, textarea, select, [tabindex]):focus-visible {
  outline: 2px solid var(--acento);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ---------- Barra superior ---------- */

.barra {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: var(--alto-barra);
  padding: 10px 16px;
  border-bottom: 1px solid var(--linea);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: saturate(180%) blur(12px);
}

.barra__marca {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.barra__logo {
  width: 28px;
  height: 28px;
  flex: none;
}

.barra__nombre {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

/* La pastilla «MD» hereda el acento del documento: es la misma marca. */
.barra__nombre-md {
  margin-left: 6px;
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--acento-suave);
  color: var(--acento-hondo);
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.barra__lema {
  display: none;
  margin: 0;
  color: var(--apagado);
  font-size: 13px;
}

/* ---------- Taller (editor + vista previa) ---------- */

/*
 * En móvil manda la secuencia de trabajo: escribo, veo cómo queda, y hasta el
 * final ajusto. En escritorio la vista previa sube a su propia columna.
 */
.taller {
  display: grid;
  gap: 14px;
  padding: 14px;
  align-content: start;
}

.panel--editor {
  order: 1;
}

.panel--previa {
  order: 2;
}

.panel--ajustes {
  order: 3;
}

.panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  background: var(--papel);
  box-shadow: var(--sombra-suave);
  overflow: hidden;
}

.panel__cabeza {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--linea-suave);
}

.panel__titulo {
  position: relative;
  margin: 0;
  padding-left: 14px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--tinta-suave);
}

/* Guiño a la regla de acento de la portada del PDF. */
.panel__titulo::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  width: 7px;
  height: 3px;
  border-radius: 2px;
  background: var(--acento);
  transform: translateY(-50%);
}

.panel__meta {
  margin: 0 auto 0 0;
  color: var(--apagado);
  font-family: var(--mono);
  font-size: 11.5px;
  letter-spacing: 0.01em;
  font-variant-numeric: tabular-nums;
}

/* El documento pasó el tope del servicio: se ve antes de darle a descargar. */
.panel__meta--alerta {
  color: var(--peligro);
  font-weight: 700;
}

.panel__pie {
  margin: 0;
  padding: 10px 14px;
  border-top: 1px solid var(--linea-suave);
  color: var(--apagado);
  font-size: 12px;
}

/* ---------- Editor ---------- */

.zona {
  position: relative;
  display: flex;
  min-height: 0;
}

.editor {
  width: 100%;
  min-height: 220px;
  padding: 16px 14px;
  border: 0;
  background: transparent;
  color: var(--tinta);
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.7;
  resize: vertical;
  tab-size: 2;
}

.editor:focus {
  outline: none;
  box-shadow: inset 3px 0 0 var(--acento);
}

.editor::placeholder {
  color: #a6adb9;
}

.zona__aviso {
  position: absolute;
  inset: 6px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--acento);
  border-radius: var(--radio-chico);
  background: rgba(238, 241, 254, 0.94);
  opacity: 0;
  visibility: hidden;
  transition: opacity 160ms var(--salida);
}

.zona--soltando .zona__aviso {
  opacity: 1;
  visibility: visible;
}

.zona__pastilla {
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--acento);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--sombra-alta);
}

/* ---------- Ajustes ---------- */

.ajustes {
  display: grid;
  gap: 16px;
  padding: 14px;
  background: #fbfbfd;
}

.campo {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.campo__etiqueta {
  display: block;
  margin-bottom: 6px;
  padding: 0;
  color: var(--tinta-suave);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.campo__entrada {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--linea);
  border-radius: var(--radio-chico);
  background: var(--papel);
  color: var(--tinta);
  font-family: var(--texto);
  font-size: 15px;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

.campo__entrada:hover {
  border-color: #cdd3de;
}

.campo__entrada:focus {
  outline: none;
  border-color: var(--acento);
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.16);
}

.campo__pista {
  margin: 6px 0 0;
  color: var(--apagado);
  font-size: 12px;
}

.segmentado {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--linea);
  border-radius: 999px;
  background: var(--papel);
}

.segmentado input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.segmentado label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 76px;
  min-height: 38px;
  padding: 0 16px;
  border-radius: 999px;
  color: var(--tinta-suave);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 180ms var(--salida), color 180ms var(--salida);
}

.segmentado label:hover {
  color: var(--tinta);
}

.segmentado input:checked + label {
  background: var(--tinta);
  color: #fff;
}

.segmentado input:focus-visible + label {
  outline: 2px solid var(--acento);
  outline-offset: 2px;
}

.interruptor {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  cursor: pointer;
}

.interruptor input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.interruptor__riel {
  position: relative;
  width: 46px;
  height: 27px;
  flex: none;
  border-radius: 999px;
  background: #ced3dd;
  transition: background-color 200ms var(--salida);
}

.interruptor__bolita {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(18, 20, 26, 0.3);
  transition: transform 220ms var(--salida);
}

.interruptor input:checked + .interruptor__riel {
  background: var(--acento);
}

.interruptor input:checked + .interruptor__riel .interruptor__bolita {
  transform: translateX(19px);
}

.interruptor input:focus-visible + .interruptor__riel {
  outline: 2px solid var(--acento);
  outline-offset: 2px;
}

.interruptor__texto {
  font-size: 14px;
  color: var(--tinta-suave);
}

/* ---------- Vista previa ---------- */

.hoja {
  --escala: 0.42;
  --ancho-hoja: 816px;
  --alto-marco: 900px;

  position: relative;
  overflow: hidden;
  height: 58vh;
  min-height: 320px;
  background:
    linear-gradient(180deg, #eceef3 0%, #e7e9ef 100%);
  border-top: 1px solid var(--linea-suave);
}

.hoja__marco {
  display: block;
  width: var(--ancho-hoja);
  height: var(--alto-marco);
  /* Centra la hoja escalada dentro de la columna (el JS calcula el desfase). */
  margin: 12px 0 0 var(--desfase, 0px);
  border: 0;
  background: #fff;
  transform: scale(var(--escala));
  transform-origin: top left;
  box-shadow: var(--sombra-alta);
}

.hoja--cargando .hoja__marco {
  opacity: 0.55;
  transition: opacity 200ms var(--entrada);
}

.hoja__estado {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(236, 238, 243, 0.95);
  color: var(--tinta-suave);
  font-size: 14px;
  text-align: center;
}

.hoja__estado[hidden] {
  display: none;
}

/* ---------- Botones y barra de acción ---------- */

.boton {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: var(--radio-chico);
  font-family: var(--texto);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 120ms ease-out, background-color 160ms ease, box-shadow 160ms ease;
}

.boton:active {
  transform: scale(0.985);
}

.boton--suave {
  min-height: 38px;
  padding: 0 12px;
  border-color: var(--linea);
  background: var(--papel);
  color: var(--tinta-suave);
  font-size: 13px;
}

.boton--suave:hover {
  border-color: #cdd3de;
  color: var(--tinta);
}

.boton--principal {
  width: 100%;
  background: var(--acento);
  color: #fff;
  box-shadow: 0 1px 2px rgba(18, 20, 26, 0.1), 0 10px 24px -14px rgba(67, 97, 238, 0.9);
}

.boton--principal:hover:not(:disabled) {
  background: var(--acento-hondo);
}

.boton:disabled {
  cursor: progress;
  opacity: 0.72;
}

/* Indicador de carga: aro que gira solo mientras se imprime. */
.boton__hilera {
  display: none;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: girar 700ms linear infinite;
}

.boton--trabajando .boton__hilera {
  display: block;
}

@keyframes girar {
  to {
    transform: rotate(360deg);
  }
}

.acciones {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 45;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--linea);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: saturate(180%) blur(12px);
}

/* ---------- Avisos (toasts) ---------- */

.avisos {
  position: fixed;
  top: calc(var(--alto-barra) + 10px);
  left: 12px;
  right: 12px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.aviso {
  padding: 12px 14px;
  border: 1px solid var(--linea);
  border-left: 3px solid var(--acento);
  border-radius: var(--radio-chico);
  background: rgba(255, 255, 255, 0.97);
  box-shadow: var(--sombra-alta);
  color: var(--tinta);
  font-size: 14px;
  line-height: 1.45;
  animation: entrar-aviso 240ms var(--salida) both;
}

.aviso--error {
  border-left-color: var(--peligro);
}

.aviso--exito {
  border-left-color: #128a52;
}

.aviso--saliendo {
  animation: salir-aviso 200ms var(--entrada) both;
}

@keyframes entrar-aviso {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
}

@keyframes salir-aviso {
  to {
    opacity: 0;
    transform: translateY(-6px);
  }
}

/* ---------- Medalla «by Integra» ---------- */

.medalla {
  position: fixed;
  right: 12px;
  bottom: calc(var(--alto-acciones) + 10px + env(safe-area-inset-bottom, 0px));
  z-index: 30;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  border: 1px solid rgba(18, 20, 26, 0.1);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(8px);
  box-shadow: 0 1px 2px rgba(18, 20, 26, 0.06);
  color: var(--tinta-suave);
  font-size: 12px;
  font-weight: 500;
  text-decoration: none;
  transition: color 160ms ease, transform 160ms var(--salida);
}

.medalla:hover {
  color: var(--tinta);
  transform: translateY(-1px);
}

/* Sobre la pastilla clara va el isotipo OSCURO (integraIconDark.svg) tal cual,
   sin filtros: invertir el claro dependía de un archivo cuyo color puede
   cambiar, y bastaba cambiar el src para dejar la medalla invisible. */
.medalla__logo {
  width: 18px;
  height: 18px;
}

.medalla__flecha {
  color: #9aa2ae;
  transition: transform 160ms var(--salida);
}

.medalla:hover .medalla__flecha {
  transform: translateX(2px);
}

/* ---------- 720 px: ya cabe el lema y la hoja crece ---------- */

@media (min-width: 720px) {
  .barra {
    padding: 10px 20px;
  }

  .barra__lema {
    display: block;
  }

  .taller {
    padding: 18px 20px;
    gap: 18px;
  }

  .hoja {
    height: 64vh;
  }
}

/* ---------- 1024 px: editor y vista previa lado a lado ---------- */

@media (min-width: 1024px) {
  :root {
    --alto-barra: 64px;
    --alto-acciones: 0px;
  }

  body {
    height: 100dvh;
    padding-bottom: 0;
    overflow: hidden;
  }

  .barra {
    gap: 20px;
    /* Deja libre la esquina donde queda la barra de acción. */
    padding: 10px 200px 10px 24px;
  }

  .barra__lema {
    margin-right: auto;
  }

  /* En escritorio la acción principal vive en la esquina del encabezado. */
  .acciones {
    top: 10px;
    right: 24px;
    bottom: auto;
    left: auto;
    z-index: 41;
    padding: 0;
    border: 0;
    background: none;
    backdrop-filter: none;
  }

  .boton--principal {
    width: auto;
  }

  .taller {
    grid-template-columns: minmax(380px, 5fr) minmax(0, 6fr);
    grid-template-rows: minmax(0, 1fr) auto;
    align-content: stretch;
    height: calc(100dvh - var(--alto-barra));
    padding: 20px 24px 24px;
    gap: 20px;
  }

  .panel {
    min-height: 0;
  }

  .panel--editor {
    grid-area: 1 / 1;
  }

  .panel--ajustes {
    grid-area: 2 / 1;
  }

  .panel--previa {
    grid-area: 1 / 2 / span 2;
  }

  .panel--editor .zona {
    flex: 1 1 auto;
    min-height: 140px;
  }

  .editor {
    height: 100%;
    min-height: 0;
    resize: none;
  }

  .hoja {
    flex: 1 1 auto;
    height: auto;
    min-height: 0;
  }

  .avisos {
    top: auto;
    bottom: 18px;
    left: 24px;
    right: auto;
    width: min(380px, 40vw);
  }

  .medalla {
    right: 20px;
    bottom: 18px;
  }
}

/* ---------- Movimiento reducido ---------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }

  .boton:active,
  .medalla:hover {
    transform: none;
  }
}
