/* Beamlet — "cipher terminal" HUD.
 *
 * Two surfaces are photographed by the other device and must stay plain
 * black-on-white with nothing drawn over them: the receiver's key code
 * (#qr, inside .plate) and the sender's frame (#stage). All decoration —
 * grid, scanlines, glow — lives in body's background or around those
 * surfaces, never in a layer stacked above them.
 *
 * The pages' CSP (default-src 'self') rules out web fonts, inline <style>
 * and style attributes; dynamic values arrive as custom properties set
 * through the CSSOM (element.style.setProperty).
 */

/* Author display rules (e.g. #stage's grid) would otherwise override the
   hidden attribute and leave hidden overlays covering the page. */
[hidden] { display: none !important; }

:root {
  --bg: #05070d;
  --panel: rgba(12, 20, 32, 0.72);
  --line: #1d3a4a;
  --text: #d7f7ff;
  --muted: #8aa4b4;
  --cyan: #3ef2ff;
  --magenta: #ff3ec8;
  --ok: #5dff9a;
  --warn: #ffb13e;
  --err: #ff4d6d;
  --glow: 0 0 12px rgba(62, 242, 255, 0.45);
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  /* Motion: feedback on hover, focus and finished steps is fast; a status
     pulse that should be noticed once is slow. One curve for both. */
  --dur-fast: 150ms;
  --dur-status: 900ms;
  --ease-out: ease-out;
  --sans: system-ui, -apple-system, "Hiragino Sans", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font: 16px/1.6 var(--sans);
  color: var(--text);
  background-color: var(--bg);
  /* Scanlines, grid and vignette are background layers: they sit beneath
     every element, so the white plate and the stage cover them fully. */
  background-image:
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.025) 0 1px, transparent 1px 3px),
    linear-gradient(rgba(62, 242, 255, 0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(62, 242, 255, 0.06) 1px, transparent 1px),
    radial-gradient(ellipse at 50% -10%, rgba(255, 62, 200, 0.18), transparent 60%),
    radial-gradient(ellipse at 50% 110%, rgba(62, 242, 255, 0.12), transparent 60%);
  background-size: auto, 32px 32px, 32px 32px, auto, auto;
  background-attachment: fixed;
}

main { max-width: 680px; margin: 0 auto; padding: 24px 16px 48px; }

/* Wide tracking suits the Latin logotype; Japanese headings take the text
   face and near-default tracking, or the kana drift apart. */
h1 {
  margin: 8px 0 16px;
  font: 700 1.6rem/1.2 var(--sans);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
  text-shadow: 0 0 8px rgba(62, 242, 255, 0.6), 2px 0 rgba(255, 62, 200, 0.5);
}

h2 {
  margin: 0 0 12px;
  font: 700 1rem/1.4 var(--sans);
  letter-spacing: 0.02em;
  color: var(--cyan);
}
h2::before { content: "// "; font-family: var(--mono); color: var(--magenta); }

p { margin: 8px 0; }
/* Break Japanese at phrase boundaries and avoid one-character last lines
   where supported; elsewhere wrapping stays as before. */
p, li, .tile span { word-break: auto-phrase; text-wrap: pretty; }
a { color: var(--cyan); text-underline-offset: 3px; }
a:hover { text-shadow: var(--glow); }
label {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px 8px;
  align-items: center;
  max-width: 100%;
  margin: 4px 12px 4px 0;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}
label select { min-width: 0; max-width: 100%; }

/* ---- panels with corner brackets ---- */

section, .panel, .tile {
  position: relative;
  margin: 20px 0;
  padding: 18px 18px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
}
section::before, section::after, .panel::before, .panel::after, .tile::before, .tile::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid var(--cyan);
  pointer-events: none;
}
section::before, .panel::before, .tile::before { top: -2px; left: -2px; border-right: 0; border-bottom: 0; }
section::after, .panel::after, .tile::after { right: -2px; bottom: -2px; border-left: 0; border-top: 0; }

.step { transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out); }
.step::before, .step::after { transition: border-color var(--dur-fast) var(--ease-out); }
.step[data-state="done"] { border-color: rgba(93, 255, 154, 0.45); }
.step[data-state="done"]::before, .step[data-state="done"]::after { border-color: var(--ok); }
.step[data-state="done"] h2::after { content: "  ✓ OK"; color: var(--ok); }
.step[data-state="active"] { border-color: var(--cyan); box-shadow: var(--glow); }

/* ---- controls ---- */

button, ::file-selector-button {
  font: 600 0.95rem var(--mono);
  letter-spacing: 0.06em;
  color: var(--cyan);
  background: rgba(62, 242, 255, 0.06);
  border: 1px solid var(--cyan);
  padding: 10px 18px;
  cursor: pointer;
  transition: box-shadow var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}
button:hover:not(:disabled), button:focus-visible, ::file-selector-button:hover {
  background: rgba(62, 242, 255, 0.14);
  box-shadow: var(--glow);
}
button:disabled {
  color: var(--muted);
  background: transparent;
  border-style: dashed;
  border-color: var(--line);
  cursor: not-allowed;
}
#send:not(:disabled), #start {
  color: var(--magenta);
  border-color: var(--magenta);
  background: rgba(255, 62, 200, 0.08);
}
#send:not(:disabled):hover, #start:hover { box-shadow: 0 0 14px rgba(255, 62, 200, 0.5); }

select, input[type="file"] {
  font: 0.9rem var(--mono);
  color: var(--text);
  background: #0a121c;
  border: 1px solid var(--line);
  padding: 6px 8px;
  max-width: 100%;
}
::file-selector-button { margin-right: 10px; padding: 6px 12px; }

:focus-visible { outline: 2px solid var(--magenta); outline-offset: 2px; }

/* ---- text roles ---- */

.note { font-size: 0.85rem; color: var(--muted); }
ul.note { padding-left: 1.2em; }
.status { font: 0.88rem/1.5 var(--mono); color: var(--muted); }
.status:not(:empty)::before { content: "> "; color: var(--cyan); }
.error { color: var(--err); }

/* The fingerprint is compared by eye across two screens; it must read at
   full contrast even with the glow ignored. */
.fingerprint { font-family: var(--mono); color: var(--muted); }
.fingerprint strong {
  display: inline-block;
  margin: 6px 6px;
  padding: 6px 14px;
  font: 700 2rem/1 var(--mono);
  letter-spacing: 0.28em;
  color: #ffffff;
  background: #071019;
  border: 1px solid var(--cyan);
  text-shadow: 0 0 10px rgba(62, 242, 255, 0.7);
}

/* ---- key code plate: white, unfiltered, nothing on top ---- */

.plate {
  display: inline-block;
  max-width: 100%;
  padding: 14px;
  margin: 8px 0;
  background: #fff;
  box-shadow: 0 0 0 1px var(--cyan), 0 0 24px rgba(62, 242, 255, 0.35);
}
.plate canvas { display: block; }

/* ---- receiver viewfinder: decoration around the preview only; the
   decoder reads the camera stream, not these pixels ---- */

.viewfinder { position: relative; margin: 8px 0; border: 1px solid var(--line); background: #000; }
.viewfinder video { display: block; width: 100%; max-height: 70vh; object-fit: contain; }
.viewfinder::before, .viewfinder::after {
  content: "";
  position: absolute;
  inset: 10px;
  pointer-events: none;
  border: 2px solid var(--vf, var(--cyan));
  /* Four corner brackets cut out of one border. */
  mask: conic-gradient(at 28px 28px, transparent 75%, #000 0) 0 0 / calc(100% - 28px) calc(100% - 28px);
  -webkit-mask: conic-gradient(at 28px 28px, transparent 75%, #000 0) 0 0 / calc(100% - 28px) calc(100% - 28px);
}
.viewfinder::after {
  /* Centre reticle. */
  inset: calc(50% - 12px) calc(50% - 12px);
  border-width: 1px;
  mask: none;
  -webkit-mask: none;
  border-radius: 50%;
  opacity: 0.6;
}

/* Electric arcs round the sender's frame while receiving, and a time
   jump on completion (spark-worker.js). The canvas covers the preview
   only and is drawn by a worker on an OffscreenCanvas, so it never
   competes with the capture loop for the main thread. */
.viewfinder .spark { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

/* Receiving states are static colours: CSS animation would compete with
   the capture loop for the main thread. Only completion pulses, once. */
#step-receive[data-state="searching"] { --vf: var(--cyan); }
#step-receive[data-state="foreign"] { --vf: var(--warn); }
#step-receive[data-state="synced"] { --vf: var(--ok); }
#step-receive[data-state="done"] { --vf: var(--ok); }
#step-receive[data-state="done"] .viewfinder { animation: pulse var(--dur-status) var(--ease-out) 1; }
#step-receive[data-state="foreign"] .status::before { color: var(--warn); }
#step-receive[data-state="synced"] .status::before { color: var(--ok); }

#step-receive[data-state="stopped"] { --vf: var(--muted); }

/* The headline is read from arm's length while aiming a phone at a
   laptop, so it is large and takes the viewfinder's state colour. The
   indeterminate meter is a static hatch, not an animation, for the same
   reason as above. */
.phase { margin: 8px 0 12px; }
.phase-label { margin: 0; font: 700 1.4rem/1.3 var(--sans); color: var(--vf, var(--cyan)); }
.phase-hint { margin: 4px 0 0; min-height: 1.5em; color: var(--text); }
.meter { height: 10px; margin-top: 8px; border: 1px solid var(--line); background: rgba(5, 10, 18, 0.6); }
.meter span { display: block; height: 100%; width: calc(var(--p, 0) * 1%); background: var(--vf, var(--cyan)); }
.meter[data-indeterminate] span {
  width: 100%;
  opacity: 0.6;
  background: repeating-linear-gradient(135deg, var(--vf, var(--cyan)) 0 8px, transparent 8px 16px);
}

/* box-shadow rather than transform: the ring must grow outside the box
   without scaling its contents. It runs once, after capture has stopped
   or before it starts, so it never competes with the decode loop. */
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 var(--pulse, rgba(93, 255, 154, 0.7)); }
  100% { box-shadow: 0 0 0 18px transparent; }
}

/* The sender's fingerprint is the safety check; draw the eye to it once
   when it appears. It is readable from the first frame. */
#fp-line:not([hidden]) strong {
  --pulse: rgba(62, 242, 255, 0.7);
  animation: pulse var(--dur-status) var(--ease-out) 1;
}

/* ---- file cards with progress ---- */

#files { list-style: none; padding: 0; }
.file {
  margin: 8px 0;
  padding: 10px 12px;
  font: 0.9rem var(--mono);
  border: 1px solid var(--line);
  border-left: 3px solid var(--cyan);
  background:
    linear-gradient(90deg, rgba(62, 242, 255, 0.16) calc(var(--p, 0) * 1%), transparent 0),
    rgba(5, 10, 18, 0.6);
}
.file[data-state="done"] { border-left-color: var(--ok); background: rgba(93, 255, 154, 0.07); }
.file[data-state="failed"] { border-left-color: var(--err); color: var(--err); }
.file a { font-weight: 700; }
img { display: block; max-width: 100%; max-height: 200px; margin: 6px 0 0; }
img.preview {
  /* An 80 px thumbnail, scaled up to a recognisable size. */
  height: 120px;
  width: auto;
  /* A wide preview is clamped by max-width; keep its proportions. */
  object-fit: contain;
  object-position: left;
  opacity: 0.7;
  mask: repeating-linear-gradient(0deg, #000 0 2px, rgba(0, 0, 0, 0.35) 2px 4px);
  -webkit-mask: repeating-linear-gradient(0deg, #000 0 2px, rgba(0, 0, 0, 0.35) 2px 4px);
}

video { width: 100%; max-height: 40vh; background: #000; }

/* ---- page chrome ---- */

.topbar { max-width: 680px; margin: 0 auto; padding: 12px 16px 0; }
.brand { display: inline-flex; align-items: center; gap: 6px; font: 700 0.85rem var(--mono); letter-spacing: 0.2em; text-decoration: none; }
.brand .mark { display: inline-block; margin: 0; }
.advanced { margin-top: 16px; color: var(--muted); }
.advanced summary { cursor: pointer; font: 0.9rem var(--mono); }
a.button, label.button {
  display: inline-block;
  cursor: pointer;
  font: 600 0.95rem var(--mono);
  color: var(--cyan);
  border: 1px solid var(--cyan);
  padding: 10px 18px;
  text-decoration: none;
}
label.button:hover, .visually-hidden:focus-visible + label.button { background: rgba(62, 242, 255, 0.14); box-shadow: var(--glow); }
.visually-hidden:focus-visible + label.button { outline: 2px solid var(--magenta); outline-offset: 2px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.picker { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin-bottom: 12px; }
.picker .note { overflow-wrap: anywhere; }

/* On a wide screen the key code sits beside its instructions, so the
   panel is not a column of QR with empty space to its right. */
@media (min-width: 720px) {
  #step-key { display: grid; grid-template-columns: auto 1fr; column-gap: 28px; align-items: center; }
  #step-key .plate { grid-column: 1; grid-row: 1 / span 5; }
  #step-key > :not(.plate) { grid-column: 2; }
  #step-key button { justify-self: start; }
}

#step-receive .fingerprint strong { font-size: 1.2rem; padding: 4px 10px; }

/* ---- landing ---- */

main.landing { min-height: 100vh; display: flex; flex-direction: column; justify-content: center; }
.hero { padding: 0 0 8px; text-align: center; }
.hero .mark { display: block; margin: 0 auto 12px; }
.logo {
  position: relative;
  display: inline-block;
  margin: 0;
  font-size: clamp(2.4rem, 9vw, 4rem);
  letter-spacing: 0.3em;
}
.logo::before, .logo::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
}
.logo::before { color: var(--magenta); }
.logo::after { color: var(--cyan); }
/* Plays once on load; the page has no script to replay it. */
.logo::before { animation: glitch-a 0.6s steps(2) 0.2s 1 forwards; }
.logo::after { animation: glitch-b 0.6s steps(2) 0.2s 1 forwards; }
@keyframes glitch-a {
  0% { opacity: 0.8; transform: translate(-3px, 1px); clip-path: inset(10% 0 60% 0); }
  50% { opacity: 0.8; transform: translate(3px, -1px); clip-path: inset(55% 0 20% 0); }
  100% { opacity: 0; transform: none; }
}
@keyframes glitch-b {
  0% { opacity: 0.8; transform: translate(3px, -1px); clip-path: inset(40% 0 35% 0); }
  50% { opacity: 0.8; transform: translate(-2px, 2px); clip-path: inset(5% 0 75% 0); }
  100% { opacity: 0; transform: none; }
}
.tagline { color: var(--muted); max-width: 32em; margin: 12px auto 0; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin: 28px 0; }
.tile {
  display: block;
  margin: 0;
  padding: 22px 20px;
  text-decoration: none;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
.tile:hover, .tile:focus-visible { border-color: var(--cyan); box-shadow: var(--glow); text-shadow: none; }
.tile .code { display: block; font: 700 0.8rem var(--mono); letter-spacing: 0.2em; color: var(--magenta); }
.tile strong { display: block; margin: 6px 0; font: 700 1.4rem var(--mono); color: var(--cyan); }
.tile strong::after { content: "→"; display: inline-block; margin-left: 0.4em; transition: transform var(--dur-fast) var(--ease-out); }
.tile:hover strong::after, .tile:focus-visible strong::after { transform: translateX(4px); }
.tile span:last-child { color: var(--muted); font-size: 0.9rem; }

/* ---- sender stage: plain white, frame untouched ---- */

#stage { position: fixed; inset: 0; z-index: 10; background: #fff; display: grid; place-items: center; }
#stage canvas { image-rendering: pixelated; }
#hint { position: fixed; bottom: 4px; left: 8px; margin: 0; font: 11px var(--mono); color: #6b6b6b; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
