/* The scanner box on #scan is a live camera (beta/scan-camera.mjs). The video
   fills the box; outside the outline it dims, so the one bright shape is where
   the card goes. Gold is the guide, as in the empty scanner it replaces; green
   is the machine reading, as in the hero deck's scan (scan-motion.css). The
   outline is drawn to real scale, so the millimetre marks on its edges are
   true, and switching RAW to SLAB grows it exactly as much as a slab is bigger.
   The viewfinder stays dark in the light theme: it is a camera. */
.capture-zone.has-cam {
  --cam-ease: cubic-bezier(.2, .85, .2, 1);
  height: 440px;
  min-height: 0;
  padding: 0;
  gap: 0;
}
.capture-zone.has-cam > p { display: none; }
.capture-zone.has-cam.cam-on > :not(.scan-cam) { visibility: hidden; }
.scan-cam {
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  border-radius: inherit;
  color: #f4f1e9;
  isolation: isolate;
  -webkit-tap-highlight-color: transparent;
}
.scan-cam:is([data-state="live"], [data-state="still"]) { background: #050504; }
.cam-video,
.cam-still > * {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.cam-video { opacity: 0; transition: opacity .45s ease; }
.scan-cam[data-state="live"] .cam-video { opacity: 1; }
.scan-cam:not([data-state="still"]) .cam-still { display: none; }
.cam-still > .cam-upload { object-fit: contain; }

/* The outline. Its own shadow is the dim outside it. */
.cam-frame {
  --cam-accent: #e9be4f;
  position: absolute;
  left: 0;
  top: 0;
  width: var(--fw, 0px);
  height: var(--fh, 0px);
  transform: translate(var(--fx, 0px), var(--fy, 0px));
  border-radius: var(--fr, 0px);
  box-shadow: 0 0 0 1px #e9be4f66, inset 0 0 28px #e9be4f12, 0 0 0 200vmax #050504a8;
  opacity: 0;
  pointer-events: none;
  transition: width .55s var(--cam-ease), height .55s var(--cam-ease), transform .55s var(--cam-ease), border-radius .55s var(--cam-ease), opacity .35s ease;
}
.scan-cam[data-state="live"] .cam-frame,
.scan-cam[data-still="shot"] .cam-frame { opacity: 1; }
.scan-cam[data-still="shot"] .cam-frame { --cam-accent: #4fe896; box-shadow: 0 0 0 1px #4fe89680, 0 0 0 200vmax #050504b8; }
.cam-window { position: absolute; inset: 0; overflow: hidden; border-radius: inherit; }
.cam-corner {
  --k: clamp(16px, calc(var(--fw, 0px) * .17), 36px);
  position: absolute;
  width: var(--k);
  height: var(--k);
  border: 0 solid var(--cam-accent);
  filter: drop-shadow(0 0 6px color-mix(in srgb, var(--cam-accent) 55%, transparent));
  transition: border-color .3s ease, translate .4s var(--cam-ease);
  animation: cam-breathe 3.2s ease-in-out infinite;
}
.cam-corner.tl { left: -6px; top: -6px; border-top-width: 3px; border-left-width: 3px; border-top-left-radius: calc(var(--fr, 0px) + 6px); }
.cam-corner.tr { right: -6px; top: -6px; border-top-width: 3px; border-right-width: 3px; border-top-right-radius: calc(var(--fr, 0px) + 6px); }
.cam-corner.br { right: -6px; bottom: -6px; border-bottom-width: 3px; border-right-width: 3px; border-bottom-right-radius: calc(var(--fr, 0px) + 6px); }
.cam-corner.bl { left: -6px; bottom: -6px; border-bottom-width: 3px; border-left-width: 3px; border-bottom-left-radius: calc(var(--fr, 0px) + 6px); }
/* A shot locks on: the corners close onto the card's edge. */
.scan-cam[data-still="shot"] .cam-corner { animation: none; }
.scan-cam[data-still="shot"] .cam-corner.tl { translate: 3px 3px; }
.scan-cam[data-still="shot"] .cam-corner.tr { translate: -3px 3px; }
.scan-cam[data-still="shot"] .cam-corner.br { translate: -3px -3px; }
.scan-cam[data-still="shot"] .cam-corner.bl { translate: 3px -3px; }
.scan-cam.is-morph .cam-corner { animation: cam-morph .6s var(--cam-ease); }

/* Technical-drawing marks: width above, height at the right, a millimetre
   ruler down the left. They are measured, not decoration. */
.cam-dim {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #e9be4fd9;
  transition: opacity .3s ease;
}
.cam-dim::before { content: ""; position: absolute; background: #e9be4f59; }
.cam-dim b {
  position: relative;
  padding: 3px 6px;
  border-radius: 4px;
  background: #050504d9;
  font: 500 8.5px/1 var(--mono);
  letter-spacing: .14em;
  white-space: nowrap;
}
.cam-dim-w { left: 0; right: 0; top: -21px; height: 9px; border-inline: 1px solid #e9be4f80; }
.cam-dim-w::before { left: 0; right: 0; top: 4px; height: 1px; }
.cam-dim-h { top: 0; bottom: 0; right: -21px; width: 9px; border-block: 1px solid #e9be4f80; }
.cam-dim-h::before { top: 0; bottom: 0; left: 4px; width: 1px; }
.cam-dim-h b { writing-mode: vertical-rl; padding: 6px 3px; }
.cam-ruler {
  position: absolute;
  top: 0;
  bottom: 0;
  left: -13px;
  width: 7px;
  opacity: .75;
  background:
    repeating-linear-gradient(to bottom, #e9be4fb3 0 1px, transparent 1px calc(var(--mm, 2px) * 10)) right top / 7px 100% no-repeat,
    repeating-linear-gradient(to bottom, #e9be4f73 0 1px, transparent 1px calc(var(--mm, 2px) * 5)) right top / 4px 100% no-repeat;
}
.scan-cam[data-still] .cam-dim,
.scan-cam[data-still] .cam-ruler { opacity: 0; }

/* SLAB: the outline is the largest of the four graders' holders; the inner
   line is the smallest. Whichever slab it is, its edge falls between them.
   Then, faint, where the grade label and the card sit inside. */
.cam-tol {
  position: absolute;
  inset: var(--tol-y, 0px) var(--tol-x, 0px);
  border: 1px dashed #e9be4f61;
  border-radius: max(0px, calc(var(--fr, 0px) - var(--tol-x, 0px)));
  opacity: 0;
  transition: opacity .3s ease;
}
.scan-cam[data-mode="slab"] .cam-tol { opacity: 1; transition-delay: .18s; }
.cam-band,
.cam-well {
  position: absolute;
  opacity: 0;
  transition: opacity .3s ease, transform .5s var(--cam-ease);
}
.cam-band {
  left: var(--band-x, 0px);
  right: var(--band-x, 0px);
  top: var(--band-y, 0px);
  height: var(--band-h, 0px);
  display: grid;
  place-items: center;
  border: 1px dashed #e9be4f80;
  border-radius: calc(var(--mm, 2px) * 1.6);
  background: linear-gradient(180deg, #e9be4f14, #e9be4f05);
  transform: translateY(-8px);
}
/* Its name shows as the outline arrives, then gets out of the way of the real label. */
.cam-band b { font: 500 8px/1 var(--mono); letter-spacing: .28em; color: #e9be4fcc; opacity: 0; }
.scan-cam[data-state="live"][data-mode="slab"] .cam-band b { animation: cam-tag 2.6s .3s ease both; }
.cam-well {
  left: var(--well-x, 0px);
  top: var(--well-y, 0px);
  width: var(--well-w, 0px);
  height: var(--well-h, 0px);
  border: 1px solid #e9be4f38;
  border-radius: var(--well-r, 0px);
  transform: scale(.94);
}
.scan-cam[data-mode="slab"] .cam-band,
.scan-cam[data-mode="slab"] .cam-well { opacity: 1; transform: none; transition-delay: .18s; }
.scan-cam[data-still] .cam-band,
.scan-cam[data-still] .cam-well,
.scan-cam[data-still] .cam-tol { opacity: 0; }

/* The read line: one sweep, a rest, again. */
.cam-sweep {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 2px;
  background: #4fe896;
  box-shadow: 0 0 12px 1px #4fe89699;
  opacity: 0;
  animation: cam-sweep 3.4s .8s cubic-bezier(.45, 0, .25, 1) infinite;
}
.cam-sweep::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 2px;
  height: 52px;
  background: linear-gradient(to top, #4fe89622, transparent);
}
.scan-cam:not([data-state="live"]) .cam-sweep { animation: none; opacity: 0; }

.cam-flash { position: absolute; inset: 0; z-index: 3; background: #fff; opacity: 0; pointer-events: none; }
.scan-cam.is-flash .cam-flash { animation: cam-flash .34s ease-out; }

.cam-chip {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 2;
  display: none;
  align-items: center;
  gap: 7px;
  padding: 6px 10px 6px 9px;
  border: 1px solid #ffffff17;
  border-radius: 999px;
  background: #0a0a09b8;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  font: 500 8.5px/1 var(--mono);
  letter-spacing: .22em;
  color: #f4f1e9e6;
}
.cam-chip b { font-weight: 500; }
.cam-chip i { width: 6px; height: 6px; border-radius: 50%; background: #4fe896; box-shadow: 0 0 8px #4fe896; animation: cam-pulse 1.8s ease-in-out infinite; }
.scan-cam:is([data-state="live"], [data-state="still"]) .cam-chip { display: flex; }
.scan-cam[data-state="still"] .cam-chip { color: #4fe896; border-color: #4fe89640; }
.scan-cam[data-state="still"] .cam-chip i { animation: none; }
.cam-torch {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 2;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1px solid #ffffff1f;
  border-radius: 50%;
  background: #0a0a09b8;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: #f4f1e9;
  cursor: pointer;
}
.cam-torch[hidden] { display: none; }
.cam-torch svg { width: 18px; height: 18px; }
.cam-torch[aria-pressed="true"] { background: #e9be4f; color: #211b0c; border-color: transparent; }
.scan-cam:not([data-state="live"]) .cam-torch { display: none; }
.cam-hint {
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: 13px;
  z-index: 2;
  margin: 0;
  text-align: center;
  font-size: 12.5px;
  line-height: 1.35;
  color: #f4f1e9e6;
  text-shadow: 0 1px 10px #000;
  display: none;
  text-wrap: balance;
}
.scan-cam[data-state="live"] .cam-hint { display: block; }
.cam-hint.is-warn { color: #ffd479; }
.cam-hint.is-warn::before { content: ""; display: inline-block; width: 6px; height: 6px; margin: 0 7px 1px 0; border-radius: 50%; background: currentColor; box-shadow: 0 0 8px currentColor; }

/* Before the camera runs, or when it can't: the empty scanner stays, with a word on what to do. */
.cam-note {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: none;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  font-size: 12px;
  line-height: 1.45;
  color: var(--muted);
  text-align: left;
}
.cam-note b { display: block; margin-bottom: 2px; font-size: 13px; color: var(--text); }
.scan-cam:is([data-state="off"], [data-state="starting"], [data-state="paused"], [data-state="blocked"], [data-state="busy"], [data-state="missing"]) .cam-note { display: block; }
.scan-cam[data-state="starting"] .cam-note b::after { content: ""; display: inline-block; width: 12px; height: 12px; margin-left: 8px; vertical-align: -1px; border: 2px solid #e9be4f40; border-top-color: var(--gold); border-radius: 50%; animation: cam-spin .8s linear infinite; }
.capture-zone.has-cam .camera-frame { margin-bottom: 64px; }

/* The RAW / SLAB switch, where the scanner's header said FRONT OF CARD. */
.cam-switch {
  position: relative;
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--deep);
}
.cam-switch button {
  position: relative;
  z-index: 1;
  min-width: 62px;
  height: 32px;
  padding: 0 12px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: 600 10px/1 var(--mono);
  letter-spacing: .2em;
  cursor: pointer;
  transition: color .3s ease;
}
.cam-switch button b { font-weight: 600; }
.cam-switch button[aria-checked="true"] { color: #211b0c; }
.cam-switch button:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.cam-switch > i {
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc(50% - 3px);
  border-radius: 999px;
  background: linear-gradient(180deg, #f6d886, #e9be4f);
  box-shadow: 0 2px 12px #e9be4f40, inset 0 1px 0 #fff8;
  transition: transform .4s cubic-bezier(.2, .85, .2, 1);
}
.cam-switch[data-mode="slab"] > i { transform: translateX(100%); }
/* The header keeps the switch's height either way, so nothing jumps when it comes or goes. */
.scanner-top:has(+ .capture-zone.has-cam) { min-height: 40px; margin-bottom: 14px; }

/* While live, the button is the shutter. */
#camera.is-shutter svg { color: #211b0c; }

@keyframes cam-sweep {
  0% { transform: translateY(0); opacity: 0; }
  7%, 58% { opacity: .9; }
  64%, 100% { transform: translateY(var(--fh, 0px)); opacity: 0; }
}
@keyframes cam-breathe { 50% { opacity: .72; } }
@keyframes cam-morph { 0% { opacity: 1; } 30% { opacity: .35; } 100% { opacity: 1; } }
@keyframes cam-pulse { 50% { opacity: .35; } }
@keyframes cam-flash { from { opacity: .7; } to { opacity: 0; } }
@keyframes cam-spin { to { transform: rotate(360deg); } }
@keyframes cam-tag { 0%, 100% { opacity: 0; } 15%, 75% { opacity: 1; } }

@media (max-width: 900px) {
  /* A phone's scanner is the page: the viewfinder takes the screen down to the buttons. */
  .capture-zone.has-cam { width: 100%; height: auto; aspect-ratio: 3 / 4; min-height: 300px; max-height: calc(100vh - 330px); max-height: calc(100svh - 330px); }
}
@media (prefers-reduced-motion: reduce) {
  .cam-frame, .cam-band, .cam-well, .cam-video, .cam-corner, .cam-switch > i { transition: none; }
  .cam-corner, .cam-chip i, .scan-cam.is-morph .cam-corner { animation: none; }
  .cam-sweep { display: none; }
  .scan-cam[data-state="starting"] .cam-note b::after { animation: none; }
}
