/* Colores en variables: cambiando estas líneas cambias el aspecto de toda la app. */
:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #1b2330;
  --muted: #6a7487;
  --accent: #1E88E5;          /* azul MonPlec */
  --accent-suave: #e3f0fc;
  --border: #dfe4ec;
  --escenario: #e6e9ee;       /* fondo neutro detrás de la imagen */
  --peligro: #e53935;
  --radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}

/* Tema oscuro: automático según el sistema, salvo que se elija "claro" con el botón. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161d; --surface: #1c222c; --text: #e7ebf2; --muted: #95a0b3;
    --accent-suave: #17324d; --border: #2c3442; --escenario: #0c0f14;
  }
}
:root[data-theme="dark"] {
  --bg: #12161d; --surface: #1c222c; --text: #e7ebf2; --muted: #95a0b3;
  --accent-suave: #17324d; --border: #2c3442; --escenario: #0c0f14;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

svg { width: 22px; height: 22px; flex: none; }

/* ---------- Botones ---------- */
button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.boton-pri { background: var(--accent); color: #fff; padding: 12px 18px; font-weight: 600; }
.boton-sec { background: var(--surface); border: 1px solid var(--border); padding: 8px 14px; }
.boton-peligro { background: var(--peligro); color: #fff; padding: 8px 14px; font-weight: 600; }
.boton-icono { width: 44px; height: 44px; }
.boton-icono:hover, .boton-sec:hover { background: var(--accent-suave); }
.boton-pri:hover { filter: brightness(1.08); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .38; cursor: default; }

/* ---------- Cabecera ---------- */
.cabecera {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.marca { display: flex; align-items: center; gap: 8px; font-size: 19px; letter-spacing: -.3px; }
.marca svg { width: 28px; height: 28px; color: var(--accent); }
.cabecera-botones { display: flex; gap: 6px; align-items: center; }

/* ---------- Pantalla de inicio ---------- */
.inicio { flex: 1; display: grid; place-items: center; padding: 16px; overflow: auto; }
.zona-soltar {
  width: min(720px, 100%);
  text-align: center;
  padding: 40px 24px;
  background: var(--surface);
  border: 2px dashed var(--border);
  border-radius: 20px;
}
.zona-icono svg { width: 72px; height: 72px; color: var(--accent); }
.zona-soltar h1 { margin: 12px 0 6px; font-size: 28px; }
.sub { margin: 0 auto 28px; color: var(--muted); max-width: 440px; line-height: 1.45; }
/* Una fila con tantas columnas como formas haya (4, o 5 con "Capturar pantalla") */
.formas { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 12px; }
.forma {
  flex-direction: column; gap: 6px;
  padding: 18px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  text-align: center;
}
.forma svg { width: 30px; height: 30px; color: var(--accent); }
.forma b { font-size: 15px; }
.forma small { color: var(--muted); font-size: 12.5px; }
.forma.principal { background: var(--accent); border-color: var(--accent); color: #fff; }
.forma.principal svg, .forma.principal small { color: #fff; }
kbd { font: 12px ui-monospace, monospace; padding: 1px 5px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface); color: var(--text); }

/* ---------- Editor: rejilla de ordenador ---------- */
.editor {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: 68px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "herr opc" "herr esc";
}
.barra-opciones {
  grid-area: opc;
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
.barra-herramientas {
  grid-area: herr;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.herr { width: 48px; height: 48px; flex: none; }
.herr:hover { background: var(--accent-suave); }
.herr.activa { background: var(--accent); color: #fff; }
.separador { width: 1px; height: 28px; background: var(--border); flex: none; margin: 0 4px; }
.barra-herramientas .separador { width: 32px; height: 1px; margin: 4px 0; }
.hueco { flex: 1; }
/* Barra de salida: sello, copiar, guardar y compartir, siempre juntos */
.salida { display: flex; gap: 2px; flex: none; padding-left: 8px; border-left: 1px solid var(--border); }

.colores { display: flex; gap: 6px; }
.color {
  width: 28px; height: 28px; flex: none;
  border-radius: 50%;
  border: 2px solid var(--border);
}
.color.activa { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }
.grosor { width: 40px; height: 40px; flex: none; }
.grosor span { display: block; width: 20px; border-radius: 4px; background: currentColor; }
.grosor.activa { background: var(--accent-suave); color: var(--accent); }
.estilo svg { width: 24px; height: 24px; }

/* Botón agrupado de líneas: flechita abajo a la derecha que indica que despliega */
.herr.grupo { position: relative; }
.marca-menu { position: absolute; right: 1px; bottom: 1px; line-height: 0; }
.marca-menu svg { width: 12px; height: 12px; }
/* Menú con flecha, flecha doble, línea y cota (se coloca desde ui.js) */
.menu-lineas {
  position: fixed; z-index: 20;
  display: grid; gap: 2px; padding: 4px; min-width: 170px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 4px 18px rgba(0,0,0,.2);
}
.menu-lineas .opcion { display: flex; align-items: center; justify-content: flex-start; gap: 10px; padding: 8px 10px; border-radius: 8px; text-align: left; font-size: 15px; color: var(--text); }
.menu-lineas .opcion svg { width: 22px; height: 22px; flex: none; }
.menu-lineas .opcion:hover { background: var(--accent-suave); }
.menu-lineas .opcion.activa { background: var(--accent); color: #fff; }

/* Panel de eliminar y afinar el fondo: IA, varita, corregir, borde y vista previa */
.panel-fondo {
  width: min(290px, calc(100vw - 16px)); padding: 10px 12px; gap: 6px; font-size: 14px;
  overflow-y: auto; overscroll-behavior: contain;   /* si no cabe (móvil), se desplaza */
}
.panel-fondo .titulo-fondo { margin-top: 6px; padding-top: 8px; border-top: 1px solid var(--border); }
.panel-fondo .titulo-fondo:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.panel-fondo p { margin: 0; color: var(--muted); line-height: 1.35; font-size: 13px; }
.panel-fondo small { color: var(--muted); }
.panel-fondo label { display: grid; gap: 2px; }
.panel-fondo .fila-valor { display: flex; justify-content: space-between; color: var(--muted); }
.panel-fondo output { color: var(--text); font-variant-numeric: tabular-nums; }
.panel-fondo input[type="range"] { width: 100%; min-width: 0; accent-color: var(--accent); }
.panel-fondo .casilla { display: flex; align-items: center; gap: 8px; }
.panel-fondo .casilla input { width: 18px; height: 18px; accent-color: var(--accent); }
.panel-fondo .boton-pri { padding: 10px 14px; }
.panel-fondo .fila-botones { display: flex; gap: 6px; }
.panel-fondo .fila-botones > * { flex: 1; }
/* Botones de modo y de vista previa: marcados cuando están activos */
.panel-fondo .modo {
  padding: 8px 6px; min-height: 40px; font-size: 13.5px; color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.panel-fondo .modo.activa { border-color: var(--accent); background: var(--accent-suave); color: var(--accent); font-weight: 600; }
.panel-fondo .modo-conservar.activa { border-color: #43A047; background: rgba(67,160,71,.15); color: #2e7d32; }
.panel-fondo .modo-borrar.activa { border-color: #E53935; background: rgba(229,57,53,.13); color: #c62828; }

/* Círculo del pincel (verde = conservar, rojo = borrar) */
.cursor-pincel {
  position: fixed; z-index: 9; pointer-events: none;
  transform: translate(-50%, -50%); border-radius: 50%;
  border: 2px solid #43A047;
  box-shadow: 0 0 0 1px rgba(0,0,0,.55), inset 0 0 0 1px rgba(255,255,255,.75);
}
.cursor-pincel[data-modo="borrar"] { border-color: #E53935; }

/* Progreso de la descarga del modelo de alta calidad */
.progreso {
  position: fixed; left: 50%; top: 64px; z-index: 30; transform: translateX(-50%);
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px 12px;
  width: max-content; max-width: calc(100% - 24px);
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); box-shadow: 0 4px 18px rgba(0,0,0,.2);
  font-size: 14px;
}
.progreso progress { width: 160px; accent-color: var(--accent); }

/* Panel de pegatinas: pestañas y cuadrícula de botones grandes */
.panel-pegatinas { width: 312px; max-height: 70vh; overflow-y: auto; }
.pestanas { display: flex; gap: 4px; margin-bottom: 4px; }
.pestana { flex: 1; padding: 8px; font-size: 14px; color: var(--muted); }
.pestana.activa { background: var(--accent-suave); color: var(--accent); font-weight: 600; }
.rejilla { display: grid; grid-template-columns: repeat(6, 1fr); gap: 2px; }
.pega { height: 48px; color: var(--text); }
.pega:hover { background: var(--accent-suave); }
.pega.activa { box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-suave); }
.pega svg { width: 30px; height: 30px; }
.emoji { font: 26px/1 "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif; }
.herr .emoji { font-size: 22px; }
.herr.activa .detalle { stroke: var(--accent); } /* que el detalle se vea sobre el botón azul */

.escenario {
  grid-area: esc;
  min-height: 0; min-width: 0;
  display: grid;
  padding: 16px;
  background: var(--escenario);
  overflow: auto;               /* con zoom aparecen barras para moverse */
}
#lienzo {
  display: block;
  margin: auto;                 /* centrado, pero sin cortarse al ampliar */
  touch-action: none;           /* el dedo dibuja, no mueve la página */
  box-shadow: 0 4px 24px rgba(0,0,0,.18);
  /* Cuadritos gris claro: se ven donde la imagen es transparente (solo en pantalla, no al guardar) */
  background: repeating-conic-gradient(#e2e2e2 0 25%, #fff 0 50%) 0 0 / 16px 16px;
}
.tam { font-weight: 700; }

/* Caja para escribir un texto, colocada encima del lienzo */
.caja-texto {
  position: fixed; z-index: 10;
  margin: 0; padding: 0;
  font-family: system-ui, sans-serif; font-weight: 700; line-height: 1.25;
  background: rgba(255,255,255,.12);
  border: 1px dashed var(--accent); border-radius: 2px;
  outline: none; resize: none; overflow: hidden; white-space: pre;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff;   /* se lee mientras escribes */
}
#lienzo.mano { cursor: move; }
#lienzo.cruz { cursor: crosshair; }
#lienzo.pincel { cursor: none; }            /* se ve el círculo del pincel */
/* Vista previa de lo transparente (solo en pantalla) */
#lienzo[data-vista="blanco"] { background: #fff; }
#lienzo[data-vista="negro"] { background: #000; }
.boton-icono.activa { background: var(--accent-suave); color: var(--accent); }

/* Botones de zoom: flotan en la esquina del escenario */
.zoom {
  grid-area: esc; align-self: end; justify-self: end;
  z-index: 5; margin: 10px;
  display: flex; gap: 2px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 2px 10px rgba(0,0,0,.15);
}

/* Ventana del sello de obra */
.dialogo h2 { margin: 0 0 14px; font-size: 19px; }
.dialogo label { display: grid; gap: 4px; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
.dialogo input {
  font: inherit; font-size: 16px; color: var(--text);
  padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.obras { display: flex; flex-wrap: wrap; gap: 6px; margin: -4px 0 12px; max-height: 96px; overflow-y: auto; }
.obra { padding: 5px 10px; border-radius: 999px; background: var(--accent-suave); color: var(--accent); font-size: 14px; }
.quitar { margin-right: auto; color: var(--peligro); }

/* Ordenador con poca altura: las herramientas, algo más pequeñas y en dos columnas (así se ven todas sin desplazar) */
@media (min-width: 721px) and (max-height: 800px) {
  .editor { grid-template-columns: 106px 1fr; }
  .barra-herramientas {
    display: grid; grid-template-columns: repeat(2, 44px);
    justify-content: center; align-content: start; gap: 4px;
    padding: 6px;
  }
  .barra-herramientas .herr { width: 44px; height: 44px; }
  .barra-herramientas .separador { grid-column: 1 / -1; width: auto; margin: 2px 4px; }
}

/* ---------- Móvil: barra de herramientas abajo ---------- */
@media (max-width: 720px) {
  .formas { grid-auto-flow: row; grid-template-columns: repeat(2, 1fr); }
  .panel-pegatinas { width: calc(100vw - 16px); max-height: 55vh; }
  .pega { height: 54px; }
  .zona-soltar { padding: 28px 16px; border-style: solid; }
  .zona-soltar h1 { font-size: 23px; }
  .editor {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas: "opc" "esc" "herr";
  }
  .barra-herramientas {
    flex-direction: row;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-right: none; border-top: 1px solid var(--border);
    overflow-x: auto; overflow-y: hidden;     /* si no caben, la barra se desliza de lado */
    scrollbar-width: thin;
    justify-content: safe center;             /* centrada si cabe; si no, empieza por la izquierda */
    gap: 2px;
  }
  .barra-herramientas .separador { width: 1px; height: 28px; margin: 0 4px; }
  .escenario { padding: 8px; }
  /* Opciones en dos filas si no caben: así copiar y compartir se ven sin desplazar */
  .barra-opciones { flex-wrap: wrap; justify-content: center; row-gap: 4px; padding: 6px 8px; }
  .barra-opciones > .separador, .hueco { display: none; }
  .salida { border-left: none; padding-left: 0; }
  .color { width: 34px; height: 34px; }
  .colores { gap: 5px; }
  .grosor { width: 44px; height: 44px; }
  .herr { width: 48px; height: 48px; }
  .solo-ordenador { display: none; }
  /* En el móvil destaca "Hacer foto" (va primero y en azul) */
  .forma.foto { order: -1; background: var(--accent); border-color: var(--accent); color: #fff; }
  .forma.foto svg, .forma.foto small { color: #fff; }
  .forma.principal { background: var(--bg); border-color: var(--border); color: inherit; }
  .forma.principal svg { color: var(--accent); }
  .forma.principal small { color: var(--muted); }
}

/* ---------- Arrastrar, ventana, avisos ---------- */
.capa-arrastre {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: rgba(30,136,229,.18);
  border: 4px dashed var(--accent);
  pointer-events: none;
  font-size: 26px; font-weight: 700; color: var(--accent);
}
.capa-arrastre div { background: var(--surface); padding: 16px 28px; border-radius: 16px; }

/* Cuenta atrás de la captura de pantalla */
.capa-cuenta { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; background: rgba(0,0,0,.55); }
.cuenta { text-align: center; color: #fff; }
.cuenta b { display: block; font-size: 140px; line-height: 1; }
.cuenta p { margin: 12px 0 18px; font-size: 18px; }
.cuenta .boton-sec { color: var(--text); }

.dialogo {
  border: none; border-radius: 16px; padding: 22px;
  width: min(380px, calc(100% - 32px));
  background: var(--surface); color: var(--text);
  box-shadow: 0 12px 40px rgba(0,0,0,.3);
}
.dialogo::backdrop { background: rgba(0,0,0,.45); }
.dialogo p { margin: 0 0 20px; line-height: 1.45; font-size: 16px; }
.dialogo-botones { display: flex; justify-content: flex-end; gap: 8px; }
/* Ventana de la webcam: más ancha, vídeo grande y botón de foto grande */
.dialogo.webcam { width: min(720px, calc(100% - 32px)); }
.webcam video { display: block; width: 100%; max-height: 70vh; margin-bottom: 14px; border-radius: 10px; background: #000; }
#webcam-disparar { font-size: 18px; padding: 14px 28px; }

.aviso {
  position: fixed; left: 50%; bottom: 96px; z-index: 30;
  transform: translate(-50%, 20px);
  background: #1b2330; color: #fff;
  padding: 10px 18px; border-radius: 20px;
  max-width: calc(100% - 32px); width: max-content; text-align: center;
  font-size: 14.5px;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.aviso.visible { opacity: 1; transform: translate(-50%, 0); }

/* En el móvil, el aviso sale justo encima de la barra de herramientas */
@media (max-width: 720px) {
  .aviso { bottom: calc(84px + env(safe-area-inset-bottom)); }
}
