/* General */
body {
  font-family: Georgia, serif;
  margin: 0;
  padding: 0;
  background: url('img/portadaWEB.jpg') no-repeat center center fixed;
  background-size: cover;
  color: #111;
}

.titulo-principal {
  text-align: center;
  font-size: 36px;
  font-weight: bold;
  margin: 2em auto 1em auto;
  color: #fff;
}

/* Bloques de contenido */
.bloque {
  background-color: rgba(240,240,240,0.9);
  margin: 1.2em auto;
  padding: 1.2em;
  border-radius: 12px;
  max-width: 600px;
}

.bloque p {
  margin: 0;
  padding: 0;
}

/* Formularios */
.formulario {
  background-color: rgba(240,240,240,0.9);
  margin: 1.5em auto;
  padding: 1em;
  border-radius: 12px;
  max-width: 600px;
}

.formulario input,
.formulario select,
textarea {
  width: 100%;
  padding: 12px;
  box-sizing: border-box;
  background-color: #fff;
  border-radius: 12px;
  border: 1px solid #aaa;
  box-shadow:
    4px 4px 8px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.3);
  font-size: 16px;
  transition: all 0.15s ease;
  background-clip: padding-box;
}

textarea {
  height: 220px;
  margin: 10px 0 5px 0;
  resize: none;
}

/* Botones de formulario */
.boton-formulario {
  width: 100%;
  padding: 12px;
  font-size: 18px;
  margin-top: 8px;
  cursor: pointer;
  background: linear-gradient(to bottom, #ffffff, #e4e4e4);
  border-radius: 14px;
  border: 1px solid #aaa;
  height: 70px;
  box-shadow:
    6px 6px 10px rgba(0,0,0,0.45),
    0 0 8px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.3);
  transition: all 0.15s ease;
}

.boton-formulario:active {
  transform: translate(4px,4px);
}

.direccion-envio {
  display: none;
}

/* Contenedor QR */
.boton-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 1em auto;
  background-color: rgba(200,200,200,0.9);
  border-radius: 14px;
  width: 260px;
  height: 260px;
  position: relative;
  padding: 18px;
  transition: padding-bottom 0.3s ease;
}

/* QR Botón */
.qr-boton {
  width: 235px;
  height: 235px;
  background: linear-gradient(to bottom, #ffffff, #e4e4e4);
  padding: 8px;
  border-radius: 14px;
  border: 1px solid #aaa;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow:
    6px 6px 10px rgba(0,0,0,0.45),
    0 0 8px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.3);
  transition: all 0.15s ease;
}

.qr-boton img {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

.qr-boton:active {
  transform: translate(4px,4px);
}

/* Mensaje dirección copiada */
#feedbackQR {
  position: absolute;
  bottom: 10px;
  width: 100%;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: green;
  opacity: 0;
  transition: opacity 0.3s ease;
}

#feedbackQR.visible {
  opacity: 1;
}

/* Campo de selección de formato */
#formato {
  width: 100%;
  max-width: 100%;
  padding: 12px;
  font-size: 16px;
  border-radius: 12px;
  border: 1px solid #aaa;
  box-shadow:
    4px 4px 8px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.3);
  background-color: #fff;
  box-sizing: border-box;
  transition: all 0.15s ease;
  appearance: none;
}

/* ──────────────────────────────── */
/* Soporte autofill para mantener colores por formato */
/* ──────────────────────────────── */
.formulario.formato-ebook input:-webkit-autofill,
.formulario.formato-ebook select:-webkit-autofill,
.formulario.formato-ebook textarea:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px #b7e4b7 inset !important;
  -webkit-text-fill-color: #111 !important;
  transition: background-color 5000s ease-in-out 0s;
}

.formulario.formato-blanda input:-webkit-autofill,
.formulario.formato-blanda select:-webkit-autofill,
.formulario.formato-blanda textarea:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px #b3c7ff inset !important;
  -webkit-text-fill-color: #111 !important;
  transition: background-color 5000s ease-in-out 0s;
}

.formulario.formato-dura input:-webkit-autofill,
.formulario.formato-dura select:-webkit-autofill,
.formulario.formato-dura textarea:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px #ffe599 inset !important;
  -webkit-text-fill-color: #111 !important;
  transition: background-color 5000s ease-in-out 0s;
}

/* ──────────────────────────────── */
/* Botón verde estilo 3D reutilizable */
/* ──────────────────────────────── */
.boton-verde {
  display: inline-block;
  padding: 12px 22px;
  font-size: 18px;
  cursor: pointer;
  text-decoration: none;
  color: #111;
  background: linear-gradient(to bottom, #c9f7c9, #8fd88f);
  border-radius: 14px;
  border: 1px solid #7abf7a;
  box-shadow:
    6px 6px 10px rgba(0,0,0,0.45),
    0 0 8px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.4);
  transition: all 0.15s ease;
}

.boton-verde:active {
  transform: translate(4px,4px);
  box-shadow:
    2px 2px 4px rgba(0,0,0,0.4),
    inset 0 1px 0 rgba(255,255,255,0.3);
}

// Dirección BCH
const BCH_ADDRESS = "bitcoincash:qzzvgukpd9f5pas6hvr98vsnpwqnak7rxqt65yuwa5";

// Colores según formato
const COLORES_FORMATO = {
  ebook: "#b7e4b7",
  blanda: "#b3c7ff",
  dura: "#ffe599"
};

// ────────────────────────────────
// Copiar dirección BCH
// ────────────────────────────────
function copiarDireccion() {
  const feedback = document.getElementById("feedbackQR");
  const contenedor = document.querySelector(".boton-wrapper");

  contenedor.style.paddingBottom = "40px";
  feedback.classList.add("visible");

  navigator.clipboard.writeText(BCH_ADDRESS);

  setTimeout(() => {
    feedback.classList.remove("visible");
    contenedor.style.paddingBottom = "18px";
  }, 2000);
}

// ────────────────────────────────
// Mostrar/ocultar dirección + required dinámico
// ────────────────────────────────
function actualizarCamposEnvio() {
  const formato = document.getElementById("formato").value;
  const envio = document.getElementById("direccionEnvio");
  const formulario = document.querySelector(".formulario");

  const camposEnvio = document.querySelectorAll("#direccionEnvio input");
  const esFisico = (formato === "blanda" || formato === "dura");

  envio.style.display = esFisico ? "block" : "none";

  camposEnvio.forEach(campo => {
    campo.required = esFisico;
  });

  aplicarColorCampos(formato);

  formulario.classList.remove("formato-ebook", "formato-blanda", "formato-dura");
  if (formato in COLORES_FORMATO) {
    formulario.classList.add("formato-" + formato);
  }
}

// ────────────────────────────────
// Aplicar colores
// ────────────────────────────────
function aplicarColorCampos(formato) {
  const color = COLORES_FORMATO[formato] || "#ffffff";

  const campos = document.querySelectorAll(".formulario input, .formulario textarea, .formulario select");

  campos.forEach(campo => {
    campo.style.setProperty("background-color", color, "important");
  });

  document.getElementById("formato")
    .style.setProperty("background-color", color, "important");
}

// ────────────────────────────────
// Validación + envío
// ────────────────────────────────
function prepararEnvio() {
  const form = document.querySelector(".formulario");

  // Validación personalizada visual
  if (!form.checkValidity()) {
    mostrarErrores(form);
    return false;
  }

  const formatoSelect = document.getElementById("formato");
  const formatoTexto = formatoSelect.options[formatoSelect.selectedIndex].text;
  document.getElementById("formatoPedido").value = formatoTexto;

  const idPedido = 'PED-' + Math.random().toString(36).substr(2, 8).toUpperCase();
  document.getElementById("pedidoInput").value = idPedido;

  document.getElementById("fechaPedido").value = new Date().toLocaleString();

  return true;
}

// ────────────────────────────────
// Mostrar errores en rojo
// ────────────────────────────────
function mostrarErrores(form) {
  const campos = form.querySelectorAll("input, select");

  campos.forEach(campo => {
    if (!campo.checkValidity()) {
      campo.classList.add("error");

      // Mensaje personalizado
      if (campo.validity.valueMissing) {
        campo.setCustomValidity("Este campo es obligatorio");
      } else if (campo.type === "email") {
        campo.setCustomValidity("Introduzca un email válido");
      }

      campo.reportValidity();
    } else {
      campo.classList.remove("error");
      campo.setCustomValidity("");
    }
  });
}

// ────────────────────────────────
// Inicialización
// ────────────────────────────────
document.addEventListener("DOMContentLoaded", function() {
  actualizarCamposEnvio();
});
