errserve 0.8.0

Servidor de páginas de error para Traefik que devuelve el código de error real
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="robots" content="noindex, nofollow" />
    <title>{{code}} — {{reason}}</title>
    <!--
      Página del tema `matrix`: un documento propio, no una piel de las sobrias.

      No pide nada por la red: su estilo vive en el `<style>` de aquí abajo y su
      script en un `<script>` en línea; no carga hojas, ni fuentes, ni imágenes
      remotas, y su script no hace ninguna llamada de red. La lluvia se dibuja en
      un `<canvas>` y el resto es CSS.

      Anima —la lluvia en el canvas, el glitch del código, el parpadeo del cursor
      y las transiciones de las pastillas y del aviso— y **puede apagarse**: hay un
      bloque `@media (prefers-reduced-motion: reduce)` que neutraliza cada familia;
      el script consulta la preferencia con `matchMedia('(prefers-reduced-motion:
      reduce)')`, la escucha con un oyente de `change` y, si el usuario pide
      movimiento reducido, **para** de verdad la lluvia —deja de pedir fotogramas y
      el bucle puede volver a entrar— y no arranca el glitch. También pausa el
      dibujo con `visibilitychange` cuando la pestaña se oculta. Límite escrito: la
      guarda del proyecto comprueba la mención de la preferencia, no que la rama sea
      correcta; la revisión humana es la que mira si apaga de verdad.

      Accesibilidad: el resultado del juego lo anuncia una sola región viva,
      `#game-msg`; el aviso (toast) es refuerzo visual y va `aria-hidden`, para no
      locutar el mismo texto dos veces, y las capas del glitch sólo se generan con
      la clase `glitch-on`, para no duplicar el código en el árbol de accesibilidad.

      Contraste medido a mano (objetivo AA, fondo #000000): texto #d9ffe2 19,35;
      código #39ff14 15,49; cian #3fe0ff 13,32; tenue #7ddc9f 12,61; salida del
      terminal #39ff14 sobre el panel #04140a 13,96 y su texto #d9ffe2 17,44.
      Panel del juego #0b2214: cabecera y mensaje #7ddc9f 10,06, código y acierto
      #39ff14 12,36, fallo #ffe9ec 14,46; la tecla #04140a sobre ese panel mide
      1,13 (separación de superficie, no texto, como el `<hr>` decorativo). El
      texto de `.boot` va sobre la lluvia, sin fondo: lleva un halo oscuro para
      que un glifo brillante detrás no lo apague. Pastilla roja: #ffe9ec sobre
      #b3121f 5,99. Pastilla azul: #e8f1ff sobre #1a4fd6 5,89.
    -->
    <style>
      /* Estilo del tema `matrix`, autocontenido: vive dentro de la página, no se
       * carga de ningún fichero ni se incrusta al servir. No referencia recursos
       * externos —el contrato de la página es no pedir nada por la red— y todo lo
       * que anima está neutralizado en `@media (prefers-reduced-motion: reduce)`,
       * con la forma canónica de cada familia. Paleta y contraste: ver la
       * cabecera del documento. */

      :root {
        color-scheme: dark;
        --bg: #000000;
        --fg: #d9ffe2;
        --verde: #39ff14;
        --cian: #3fe0ff;
        --apagado: #7ddc9f;
        --rojo: #b3121f;
        --rojo-texto: #ffe9ec;
        --azul: #1a4fd6;
        --azul-texto: #e8f1ff;
        --panel: #04140a;
        --panel-2: #0b2214;
        --linea: #123a1d;
      }

      * {
        box-sizing: border-box;
      }

      html,
      body {
        min-height: 100%;
      }

      body {
        margin: 0;
        min-height: 100vh;
        background: var(--bg);
        color: var(--fg);
        font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
          monospace;
        line-height: 1.5;
        overflow-x: hidden;
      }

      /* La lluvia digital: un canvas a pantalla completa, dibujado por el script.
       * Nunca pide una imagen. */
      #rain {
        position: fixed;
        inset: 0;
        width: 100%;
        height: 100%;
        z-index: 0;
        opacity: 0.5;
        pointer-events: none;
      }

      /* Scanlines: la rejilla de un monitor de fósforo, en CSS puro y estática. */
      body::before {
        content: "";
        position: fixed;
        inset: 0;
        z-index: 1;
        pointer-events: none;
        background: repeating-linear-gradient(
          to bottom,
          rgba(0, 255, 65, 0.07) 0,
          rgba(0, 255, 65, 0.07) 1px,
          transparent 1px,
          transparent 3px
        );
      }

      /* Viñeta del CRT: oscurece los bordes para que el texto respire. */
      body::after {
        content: "";
        position: fixed;
        inset: 0;
        z-index: 2;
        pointer-events: none;
        background: radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.7) 100%);
      }

      main {
        position: relative;
        z-index: 3;
        max-width: 46rem;
        margin: 0 auto;
        padding: 4rem 1.25rem 3rem;
        display: flex;
        flex-direction: column;
        gap: 1rem;
      }

      .boot {
        margin: 0;
        color: var(--apagado);
        font-size: 0.85rem;
        letter-spacing: 0.04em;
        /* Halo oscuro para que un glifo brillante de la lluvia que pase por
         * detrás no apague la letra: la lluvia sigue siendo la protagonista del
         * margen, pero el texto de arranque se lee sobre ella. */
        text-shadow:
          0 1px 2px #000000,
          0 -1px 2px #000000,
          1px 0 2px #000000,
          -1px 0 2px #000000;
      }

      .glitch {
        position: relative;
        margin: 0;
        font-size: clamp(3rem, 16vw, 7rem);
        font-weight: 700;
        letter-spacing: 0.06em;
        color: var(--verde);
        text-shadow: 0 0 8px rgba(0, 255, 65, 0.55);
      }

      /* Las dos capas de color del glitch: copian el código con `attr(data-text)`
       * y se desplazan con clip-path. Existen y animan **sólo** con la clase
       * `glitch-on` que el script añade —y que no añade si el usuario pide
       * movimiento reducido—: sin ella no hay capas, así que el código no se
       * duplica en el árbol de accesibilidad. */
      .glitch-on::before,
      .glitch-on::after {
        content: attr(data-text);
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        overflow: hidden;
        clip-path: inset(0 0 0 0);
      }

      .glitch-on::before {
        color: var(--rojo);
        animation: glitch-x 1.6s steps(2, end) infinite;
      }

      .glitch-on::after {
        color: var(--cian);
        animation: glitch-y 2.2s steps(2, end) infinite;
      }

      .terminal {
        margin: 0;
        padding: 0.85rem 1rem;
        border: 1px solid var(--linea);
        border-radius: 6px;
        background: var(--panel);
        font-size: 0.95rem;
      }

      .line {
        margin: 0;
        white-space: pre-wrap;
        word-break: break-word;
      }

      .prompt {
        color: var(--verde);
      }

      .prompt::before {
        content: "errserve:~$ ";
        color: var(--apagado);
      }

      .out {
        color: var(--fg);
      }

      .caret {
        color: var(--verde);
        animation: parpadeo 1s step-end infinite;
      }

      .controls {
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
      }

      .pill {
        font: inherit;
        font-weight: 700;
        letter-spacing: 0.08em;
        padding: 0.6rem 1.4rem;
        border: 0;
        border-radius: 999px;
        cursor: pointer;
        transition: transform 120ms ease, box-shadow 120ms ease;
      }

      .pill:hover,
      .pill:focus-visible {
        transform: translateY(-2px);
      }

      .pill:focus-visible {
        outline: 2px solid var(--fg);
        outline-offset: 2px;
      }

      .pill-roja {
        background: var(--rojo);
        color: var(--rojo-texto);
        box-shadow:
          0 0 0 1px rgba(255, 59, 78, 0.45),
          0 0 12px rgba(255, 59, 78, 0.35);
      }

      .pill-azul {
        background: var(--azul);
        color: var(--azul-texto);
        box-shadow:
          0 0 0 1px rgba(77, 157, 255, 0.45),
          0 0 12px rgba(77, 157, 255, 0.35);
      }

      .game {
        margin-top: 0.5rem;
        padding: 1rem;
        border: 1px solid var(--linea);
        border-radius: 6px;
        /* Panel **opaco**: la caja del minijuego no puede dejar pasar la lluvia
         * —el texto de aquí hay que leerlo—. Es un tono distinto de `--panel`
         * (un pelo más claro) para que `.key` siga leyéndose como su propia
         * superficie sobre él. */
        background: var(--panel-2);
      }

      .game h2 {
        margin: 0 0 0.5rem;
        font-size: 0.8rem;
        font-weight: 600;
        letter-spacing: 0.14em;
        text-transform: uppercase;
        color: var(--apagado);
      }

      .game-code {
        margin: 0 0 0.75rem;
        font-size: 2rem;
        letter-spacing: 0.5em;
        color: var(--verde);
      }

      .keypad {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
      }

      .key {
        font: inherit;
        width: 2.6rem;
        height: 2.6rem;
        border: 1px solid var(--linea);
        border-radius: 6px;
        background: var(--panel);
        color: var(--fg);
        cursor: pointer;
      }

      .key:hover,
      .key:focus-visible {
        border-color: var(--verde);
        color: var(--verde);
      }

      .key:focus-visible {
        outline: 2px solid var(--verde);
        outline-offset: 2px;
      }

      .game-msg {
        margin: 0.75rem 0 0;
        min-height: 1.5em;
        color: var(--apagado);
      }

      .game-msg.gana {
        color: var(--verde);
      }

      .game-msg.pierde {
        color: var(--rojo-texto);
      }

      .toast {
        position: fixed;
        left: 50%;
        bottom: 1.5rem;
        z-index: 4;
        transform: translate(-50%, 12px);
        opacity: 0;
        max-width: 90vw;
        padding: 0.6rem 1rem;
        border: 1px solid var(--linea);
        border-radius: 6px;
        background: var(--panel);
        color: var(--verde);
        font-size: 0.9rem;
        letter-spacing: 0.04em;
        pointer-events: none;
        transition: opacity 300ms ease, transform 300ms ease;
      }

      .toast.show {
        opacity: 1;
        transform: translate(-50%, 0);
      }

      @keyframes glitch-x {
        0%,
        100% {
          clip-path: inset(0 0 0 0);
          transform: translateX(0);
        }
        20% {
          clip-path: inset(20% 0 60% 0);
          transform: translateX(-4px);
        }
        40% {
          clip-path: inset(60% 0 20% 0);
          transform: translateX(4px);
        }
        60% {
          clip-path: inset(35% 0 45% 0);
          transform: translateX(-2px);
        }
        80% {
          clip-path: inset(10% 0 70% 0);
          transform: translateX(2px);
        }
      }

      @keyframes glitch-y {
        0%,
        100% {
          clip-path: inset(0 0 0 0);
          transform: translateX(0);
        }
        25% {
          clip-path: inset(50% 0 30% 0);
          transform: translateX(3px);
        }
        50% {
          clip-path: inset(15% 0 65% 0);
          transform: translateX(-3px);
        }
        75% {
          clip-path: inset(70% 0 10% 0);
          transform: translateX(2px);
        }
      }

      @keyframes parpadeo {
        0%,
        50% {
          opacity: 1;
        }
        50.01%,
        100% {
          opacity: 0;
        }
      }

      /* La puerta que el usuario cierra: neutraliza cada familia por su forma. */
      @media (prefers-reduced-motion: reduce) {
        .glitch-on::before,
        .glitch-on::after,
        .caret {
          animation: none;
        }

        .pill,
        .toast {
          transition: none;
        }
      }
    </style>
  </head>
  <body class="c{{code}}">
    <canvas id="rain" aria-hidden="true"></canvas>
    <main>
      <p class="boot">// errserve — terminal de errores</p>
      <h1 class="glitch" id="code" data-text="{{code}}">{{code}}</h1>
      <div class="terminal">
        <p class="line"><span class="prompt">status</span> <span class="out">{{reason}}</span></p>
        <p class="line"><span class="prompt">detail</span> <span class="out">{{description}}</span></p>
        <p class="line">
          <span class="prompt">system</span> <span class="caret" aria-hidden="true">█</span>
        </p>
      </div>
      <div class="controls">
        <button type="button" class="pill pill-roja" id="reboot">REBOOT</button>
        <button type="button" class="pill pill-azul" id="awake">AWAKE</button>
      </div>
      <section class="game" aria-labelledby="game-title">
        <h2 id="game-title">Acceso — teclea el código de 3 dígitos</h2>
        <p class="game-code" id="game-code" aria-live="polite">_ _ _</p>
        <div class="keypad" id="keypad" role="group" aria-label="Teclado numérico"></div>
        <p class="game-msg" id="game-msg" role="status" aria-live="polite"></p>
      </section>
    </main>
    <div class="toast" id="toast" aria-hidden="true"></div>
    <script>
      (function () {
        'use strict';

        var reducido = window.matchMedia('(prefers-reduced-motion: reduce)');

        function pideQuieto() {
          return reducido.matches;
        }

        // --- Aviso breve (toast) ---
        // Refuerzo **visual**: no es región viva. El resultado del juego lo
        // anuncia `#game-msg`, la única región viva del juego —si el aviso también
        // lo anunciara, el mismo texto se locutaría dos veces en el lector—.
        var toastEl = document.getElementById('toast');
        var toastTemporizador = null;

        function toast(mensaje) {
          toastEl.textContent = mensaje;
          toastEl.classList.add('show');
          if (toastTemporizador) {
            clearTimeout(toastTemporizador);
          }
          toastTemporizador = setTimeout(function () {
            toastEl.classList.remove('show');
          }, 2600);
        }

        // --- Glitch del código ---
        var codigo = document.getElementById('code');

        function ponGlitch(activo) {
          if (activo && !pideQuieto()) {
            codigo.classList.add('glitch-on');
          } else {
            codigo.classList.remove('glitch-on');
          }
        }

        function reiniciaGlitch() {
          ponGlitch(false);
          if (pideQuieto()) {
            return;
          }
          void codigo.offsetWidth;
          ponGlitch(true);
        }

        // --- Lluvia digital ---
        // `getContext('2d')` puede devolver null: si pasa, la lluvia no existe,
        // pero el script **no** aborta —el minijuego, las pastillas y el aviso
        // siguen funcionando y la página sigue siendo legible—.
        var canvas = document.getElementById('rain');
        var ctx =
          canvas && typeof canvas.getContext === 'function'
            ? canvas.getContext('2d')
            : null;
        var GLIFOS = 'アイウエオカキクケコサシスセソタチツテト0123456789ABCDEF';
        var TAM = 16;
        // Tope de área del backing store. Por encima del máximo del navegador el
        // canvas deja de rasterizar **en silencio** y solo se ve negro, así que
        // se limita el DPR usado —nunca por encima del real— y, si hace falta, se
        // dibuja en un bitmap más pequeño que el CSS estira a pantalla completa.
        var MAX_PIXELES = 12000000;
        var gotas = [];
        var rafId = null;
        var enMarcha = false;
        var midePendiente = null;

        function dprUtil() {
          var dpr = window.devicePixelRatio || 1;
          var area = Math.max(1, window.innerWidth * window.innerHeight);
          var tope = Math.sqrt(MAX_PIXELES / area);
          return Math.max(0.5, Math.min(dpr, tope));
        }

        function mide() {
          if (!ctx) {
            return;
          }
          var ancho = window.innerWidth;
          var alto = window.innerHeight;
          var dpr = dprUtil();
          canvas.width = Math.max(1, Math.floor(ancho * dpr));
          canvas.height = Math.max(1, Math.floor(alto * dpr));
          ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
          var columnas = Math.ceil(ancho / TAM);
          gotas = [];
          for (var i = 0; i < columnas; i = i + 1) {
            gotas[i] = Math.floor(Math.random() * (alto / TAM));
          }
        }

        // `resize` se agrupa: una medida por ráfaga, no una por evento.
        function programaMide() {
          if (midePendiente) {
            clearTimeout(midePendiente);
          }
          midePendiente = setTimeout(function () {
            midePendiente = null;
            mide();
          }, 150);
        }

        function fotograma() {
          rafId = null;
          if (!enMarcha || !ctx) {
            return;
          }
          ctx.fillStyle = 'rgba(0, 0, 0, 0.08)';
          ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
          ctx.fillStyle = '#39ff14';
          ctx.font = TAM + 'px monospace';
          for (var i = 0; i < gotas.length; i = i + 1) {
            var glifo = GLIFOS.charAt(Math.floor(Math.random() * GLIFOS.length));
            ctx.fillText(glifo, i * TAM, gotas[i] * TAM);
            if (gotas[i] * TAM > window.innerHeight && Math.random() > 0.975) {
              gotas[i] = 0;
            } else {
              gotas[i] = gotas[i] + 1;
            }
          }
          if (enMarcha) {
            rafId = requestAnimationFrame(fotograma);
          }
        }

        // Parar es **dejar de pedir fotogramas**: se cancela el que esté pendiente
        // y se baja la bandera, así que el bucle sale de verdad y puede volver a
        // entrar —no es solo «no dibujar»—.
        function paraLluvia() {
          enMarcha = false;
          if (rafId !== null) {
            cancelAnimationFrame(rafId);
            rafId = null;
          }
        }

        function arrancaLluvia() {
          if (!ctx || enMarcha || document.hidden || pideQuieto()) {
            return;
          }
          enMarcha = true;
          rafId = requestAnimationFrame(fotograma);
        }

        // La preferencia se puede cambiar con la página abierta: hay que escuchar
        // el `change` de la media query y apagar la lluvia sin recargar.
        function aplicaPreferencia() {
          ponGlitch(!pideQuieto());
          if (pideQuieto()) {
            paraLluvia();
          } else {
            arrancaLluvia();
          }
        }

        if (typeof reducido.addEventListener === 'function') {
          reducido.addEventListener('change', aplicaPreferencia);
        } else if (typeof reducido.addListener === 'function') {
          reducido.addListener(aplicaPreferencia);
        }

        // Pestaña oculta: se pausa el dibujo; al volver, se reanuda.
        document.addEventListener('visibilitychange', function () {
          if (document.hidden) {
            paraLluvia();
          } else {
            arrancaLluvia();
          }
        });

        // --- Terminal: la pastilla azul la «despierta» escribiendo la salida ---
        var lineas = Array.prototype.slice.call(document.querySelectorAll('.out'));
        var originales = lineas.map(function (el) {
          return el.textContent;
        });
        var terminalEscribiendo = false;

        function escribe(el, texto, hecho) {
          // Reentrante: se cancela cualquier escritura anterior sobre el mismo
          // elemento antes de reprogramar, como `toast` con su temporizador.
          el._escribiendo = false;
          if (el._temporizador) {
            clearTimeout(el._temporizador);
            el._temporizador = null;
          }
          if (pideQuieto()) {
            el.textContent = texto;
            hecho();
            return;
          }
          el._escribiendo = true;
          el.textContent = '';
          var i = 0;
          function paso() {
            if (!el._escribiendo) {
              return;
            }
            el.textContent = texto.slice(0, i);
            i = i + 1;
            if (i <= texto.length) {
              el._temporizador = setTimeout(paso, 22);
            } else {
              el._temporizador = null;
              el._escribiendo = false;
              hecho();
            }
          }
          paso();
        }

        function despiertaTerminal() {
          if (terminalEscribiendo) {
            return; // ya se está escribiendo: una sola cadena por vez
          }
          terminalEscribiendo = true;
          var pendientes = lineas.length;
          lineas.forEach(function (el, indice) {
            escribe(el, originales[indice], function () {
              pendientes = pendientes - 1;
              if (pendientes === 0) {
                terminalEscribiendo = false;
                toast('TERMINAL DESPIERTA');
              }
            });
          });
        }

        // --- Minijuego: acertar el código de tres dígitos ---
        var juego = document.querySelector('.game');
        var teclado = document.getElementById('keypad');
        var pantalla = document.getElementById('game-code');
        var mensaje = document.getElementById('game-msg');
        var secreto = [];
        var intento = [];
        var fallos = 0;
        var resuelto = false;
        var reinicioTemporizador = null;

        function sortea() {
          secreto = String(100 + Math.floor(Math.random() * 900)).split('');
        }

        function pintaIntento() {
          var celdas = ['_', '_', '_'];
          for (var i = 0; i < intento.length; i = i + 1) {
            celdas[i] = intento[i];
          }
          pantalla.textContent = celdas.join(' ');
        }

        // El reinicio diferido del intento se cancela en `reiniciaJuego`: si
        // sobreviviera a un REBOOT, escribiría sobre un estado que ya no es el suyo.
        function cancelaReinicioIntento() {
          if (reinicioTemporizador) {
            clearTimeout(reinicioTemporizador);
            reinicioTemporizador = null;
          }
        }

        function termina(acierto) {
          if (acierto) {
            resuelto = true;
            mensaje.textContent = 'ACCESO CONCEDIDO — la simulación se rinde.';
            mensaje.className = 'game-msg gana';
            toast('ACCESO CONCEDIDO');
            return;
          }
          fallos = fallos + 1;
          mensaje.className = 'game-msg pierde';
          if (fallos >= 3) {
            mensaje.textContent = 'ACCESO DENEGADO — pista: empieza por ' + secreto[0];
          } else {
            mensaje.textContent = 'ACCESO DENEGADO (' + fallos + ')';
          }
          toast('ACCESO DENEGADO');
        }

        function introduce(digito) {
          if (resuelto || intento.length >= 3) {
            return;
          }
          intento.push(digito);
          pintaIntento();
          if (intento.length === 3) {
            var acierto = intento.join('') === secreto.join('');
            termina(acierto);
            if (!acierto) {
              cancelaReinicioIntento();
              reinicioTemporizador = setTimeout(function () {
                reinicioTemporizador = null;
                if (!resuelto) {
                  intento = [];
                  pintaIntento();
                }
              }, 700);
            }
          }
        }

        function reiniciaJuego() {
          cancelaReinicioIntento();
          sortea();
          intento = [];
          fallos = 0;
          resuelto = false;
          mensaje.textContent = '';
          mensaje.className = 'game-msg';
          pintaIntento();
        }

        for (var d = 0; d <= 9; d = d + 1) {
          (function (digito) {
            var boton = document.createElement('button');
            boton.type = 'button';
            boton.className = 'key';
            boton.textContent = String(digito);
            boton.addEventListener('click', function () {
              introduce(String(digito));
            });
            teclado.appendChild(boton);
          })(d);
        }

        // El teclado sólo alimenta el juego cuando el foco está **dentro** de la
        // sección: así el día que una página del operador traiga un `<input>`,
        // escribir en él no queda secuestrado. `Tab`, `Esc` y las flechas no se
        // tocan, y Backspace sólo se cancela si el juego lo ha consumido.
        document.addEventListener('keydown', function (evento) {
          if (!juego || !juego.contains(evento.target)) {
            return;
          }
          if (evento.key >= '0' && evento.key <= '9') {
            introduce(evento.key);
          } else if (evento.key === 'Backspace' && intento.length > 0) {
            intento = intento.slice(0, -1);
            pintaIntento();
            evento.preventDefault();
          }
        });

        // --- Pastillas ---
        document.getElementById('reboot').addEventListener('click', function () {
          reiniciaGlitch();
          reiniciaJuego();
          toast('REBOOT — glitch reiniciado');
        });

        document.getElementById('awake').addEventListener('click', despiertaTerminal);

        // --- Arranque ---
        mide();
        window.addEventListener('resize', programaMide);
        ponGlitch(!pideQuieto());
        arrancaLluvia();
        sortea();
        pintaIntento();
        toast(pideQuieto() ? 'MODO QUIETO — respeto tu preferencia' : 'SISTEMA EN LÍNEA — errserve');
      })();
    </script>
  </body>
</html>