/*
 * File Purpose: Visual system, responsive layout, motion, and reveal staging for size-detector.
 * Primary Functions/Classes: Instrument layout, scanner field, stage panels, reveal-stamp full-viewport.
 * Inputs and Outputs (I/O): body[data-state], body[data-mode], and prefers-reduced-motion drive presentation.
 */
:root {
  --field: #f1f3ef;
  --paper: #fafbf8;
  --ink: #171a18;
  --muted: #606761;
  --blue: #2447d8;
  --blue-pale: #dde4ff;
  --red: #c63e35;
  --line: #aeb5af;
  --sans: Arial, "Helvetica Neue", Helvetica, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  min-height: 100%;
  color-scheme: light;
}

body {
  display: flex;
  min-height: 100vh;
  margin: 0;
  padding-inline: clamp(1rem, 4vw, 3rem);
  overflow-x: hidden;
  flex-direction: column;
  color: var(--ink);
  background: var(--field);
  font-family: var(--sans);
}

button,
input {
  font: inherit;
}

button,
a,
input,
summary {
  -webkit-tap-highlight-color: transparent;
}

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

.skip-link {
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 20;
  padding: 0.75rem 1rem;
  color: var(--paper);
  background: var(--ink);
  transform: translateY(-160%);
}

.skip-link:focus {
  transform: translateY(0);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
  white-space: nowrap;
}

.instrument {
  width: min(100%, 52rem);
  margin-inline: auto;
}

.instrument {
  display: flex;
  flex: 1;
}

.instrument__body {
  display: flex;
  width: 100%;
  flex-direction: column;
}

.stage-panel {
  display: flex;
  min-width: 0;
  min-height: min(44rem, 82vh);
  padding-block: clamp(4rem, 12vh, 8rem);
  flex-direction: column;
  justify-content: space-between;
}

.stage-panel__content {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
}

.stage-kicker {
  margin: 0 0 1.3rem;
  color: var(--blue);
  font-family: var(--mono);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.stage-title {
  max-width: 12ch;
  margin: 0;
  font-size: clamp(3rem, 5.3vw, 4.6rem);
  font-weight: 400;
  line-height: 0.98;
  letter-spacing: -0.055em;
  text-wrap: balance;
}

.stage-title:focus {
  outline: none;
}

.stage-title--compact {
  font-size: clamp(2.75rem, 4.4vw, 3.9rem);
}

.stage-title--reveal {
  max-width: 13ch;
  color: var(--red);
  font-size: clamp(2.8rem, 4.7vw, 4.1rem);
}

.stage-title--reveal span {
  display: block;
}

.stage-copy {
  max-width: 33rem;
  margin: 1.5rem 0 0;
  color: #333936;
  font-size: 1rem;
  line-height: 1.55;
}

.fine-print,
.protocol-note,
.source-link {
  max-width: 32rem;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.68rem;
  line-height: 1.55;
}

.fine-print {
  margin: 1.25rem 0 0;
}

.source-link {
  margin: 1.4rem 0 0;
}

.source-link a {
  color: var(--ink);
  text-decoration-color: var(--blue);
  text-underline-offset: 0.22rem;
}

.source-link a:hover {
  color: var(--blue);
}

.protocol-note {
  margin: 1.25rem 0 0;
  padding-left: 0.8rem;
  border-left: 2px solid var(--blue);
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 2rem;
  gap: 0.75rem 1rem;
}

.button {
  display: inline-flex;
  min-height: 3rem;
  align-items: center;
  justify-content: center;
  padding: 0.75rem 1rem;
  color: var(--paper);
  border: 1px solid var(--blue);
  border-radius: 0;
  background: var(--blue);
  cursor: pointer;
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  transition: color 140ms ease, background-color 140ms ease;
}

.button:hover {
  color: var(--blue);
  background: transparent;
}

.button--secondary {
  color: var(--blue);
  background: transparent;
}

.button--secondary:hover {
  color: var(--paper);
  background: var(--blue);
}

.button--text {
  min-height: 2.75rem;
  padding-inline: 0.2rem;
  color: var(--ink);
  border-color: transparent;
  background: transparent;
  text-decoration: underline;
  text-underline-offset: 0.25rem;
}

.button--text:hover {
  color: var(--blue);
  background: transparent;
}

.button[disabled] {
  color: var(--muted);
  border-color: var(--line);
  background: transparent;
  cursor: not-allowed;
}

.mode-form,
.consent-form {
  margin-top: 2rem;
}

.mode-options {
  border-top: 1px solid var(--ink);
}

.mode-option {
  position: relative;
  display: block;
}

.mode-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.mode-option__surface {
  display: grid;
  min-height: 5.2rem;
  grid-template-columns: 2.5rem 1fr;
  align-items: center;
  padding: 0.9rem 0.75rem;
  gap: 0.75rem;
  border-bottom: 1px solid var(--ink);
  cursor: pointer;
  transition: background-color 140ms ease;
}

.mode-option__surface:hover {
  background: rgba(221, 228, 255, 0.48);
}

.mode-option input:checked + .mode-option__surface {
  background: var(--blue-pale);
  box-shadow: inset 3px 0 0 var(--blue);
}

.mode-option input:focus-visible + .mode-option__surface {
  outline: 2px solid var(--red);
  outline-offset: 3px;
}

.mode-option__index {
  color: var(--blue);
  font-family: var(--mono);
  font-size: 0.7rem;
}

.mode-option strong,
.mode-option small {
  display: block;
}

.mode-option strong {
  font-size: 0.9rem;
  font-weight: 500;
}

.mode-option small {
  margin-top: 0.25rem;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.64rem;
  line-height: 1.4;
}

.consent-check {
  display: grid;
  max-width: 33rem;
  grid-template-columns: 1.25rem 1fr;
  align-items: start;
  gap: 0.75rem;
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1.45;
}

.consent-check input {
  width: 1.1rem;
  height: 1.1rem;
  margin: 0.05rem 0 0;
  accent-color: var(--blue);
}

.scan-log {
  max-width: 33rem;
  margin: 1.6rem 0 0;
  padding: 0.7rem 0;
  color: var(--blue);
  border-top: 1px solid var(--blue);
  border-bottom: 1px solid var(--blue);
  font-family: var(--mono);
  font-size: 0.68rem;
  line-height: 1.45;
}

.scan-log::before {
  margin-right: 0.5rem;
  content: "λ";
}

.result-summary {
  margin: 1.75rem 0 0;
  padding-block: 0.75rem;
  color: var(--red);
  border-top: 1px solid var(--red);
  border-bottom: 1px solid var(--red);
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  line-height: 1.45;
  text-transform: uppercase;
}

.reassurance {
  max-width: 33rem;
  margin: 1.3rem 0 0;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 600;
  line-height: 1.45;
}

.scanner-shell {
  min-width: 0;
  padding-bottom: clamp(5rem, 12vh, 9rem);
  scroll-margin-top: 1rem;
}

.scanner-glass {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  color: var(--blue);
  border-bottom: 1px solid var(--ink);
  background: var(--paper);
}

.scanner-grid {
  position: absolute;
  inset: 0;
  opacity: 0.42;
  background-image:
    linear-gradient(rgba(36, 71, 216, 0.18) 1px, transparent 1px),
    linear-gradient(90deg, rgba(36, 71, 216, 0.18) 1px, transparent 1px);
  background-size: 2.5rem 2.5rem;
  transition: opacity 280ms ease;
}

.scanner-grid::before,
.scanner-grid::after {
  position: absolute;
  background: var(--blue);
  content: "";
}

.scanner-grid::before {
  top: 50%;
  right: 0;
  left: 0;
  height: 1px;
}

.scanner-grid::after {
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
}

.coordinate {
  position: absolute;
  z-index: 2;
  color: var(--blue);
  font-family: var(--mono);
  font-size: 0.62rem;
}

.coordinate--x {
  right: 0.6rem;
  top: calc(50% + 0.45rem);
}

.coordinate--y {
  top: 0.6rem;
  left: calc(50% + 0.45rem);
}

.ruler-confession {
  position: absolute;
  right: 1rem;
  bottom: 0.9rem;
  left: 1rem;
  z-index: 5;
  display: flex;
  justify-content: space-between;
  color: var(--red);
  opacity: 0;
  font-family: var(--mono);
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  transform: translateY(0.4rem);
  transition: opacity 220ms ease 120ms, transform 220ms ease 120ms;
}

.scanner-standby {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-content: center;
  color: var(--blue);
  text-align: center;
  transition: opacity 180ms ease;
}

.scanner-standby__cross {
  position: relative;
  display: block;
  width: 5rem;
  height: 5rem;
  margin-inline: auto;
  border: 1px solid currentColor;
  border-radius: 50%;
}

.scanner-standby__cross::before,
.scanner-standby__cross::after {
  position: absolute;
  top: 50%;
  left: 50%;
  background: currentColor;
  content: "";
  transform: translate(-50%, -50%);
}

.scanner-standby__cross::before {
  width: 7rem;
  height: 1px;
}

.scanner-standby__cross::after {
  width: 1px;
  height: 7rem;
}

.scanner-standby small {
  margin-top: 1.7rem;
  font-family: var(--mono);
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.alignment-mark {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 3;
  display: none;
  width: 42%;
  height: auto;
  aspect-ratio: 1;
  align-items: center;
  justify-content: center;
  opacity: 0.7;
  transform: translate(-50%, -50%);
}

.alignment-mark::before,
.alignment-mark::after {
  position: absolute;
  width: 1.25rem;
  height: 1.25rem;
  border-color: currentColor;
  border-style: solid;
  content: "";
}

.alignment-mark::before {
  top: 0;
  left: 0;
  border-width: 1px 0 0 1px;
}

.alignment-mark::after {
  right: 0;
  bottom: 0;
  border-width: 0 1px 1px 0;
}

.alignment-mark--package > span {
  position: relative;
  width: 28%;
  height: 72%;
  border: 1px dashed currentColor;
  border-radius: 45% 45% 38% 38%;
  transform: translateY(-3%);
}

.alignment-mark--package > span::after {
  position: absolute;
  right: -55%;
  bottom: -9%;
  left: -55%;
  height: 19%;
  border: 1px dashed currentColor;
  border-radius: 50%;
  content: "";
}

.alignment-mark--chest {
  gap: clamp(0.75rem, 1.5vw, 1rem);
}

.alignment-mark--chest > span {
  position: relative;
  width: 36%;
  height: auto;
  aspect-ratio: 1;
  flex: 0 0 auto;
  border: 1px dashed currentColor;
  border-radius: 50%;
}

.alignment-mark--chest > span::after {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 12%;
  aspect-ratio: 1;
  border: 1px solid currentColor;
  border-radius: 50%;
  content: "";
  transform: translate(-50%, -50%);
}

.placement-guide {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1.1rem;
  flex-direction: column;
  gap: clamp(1rem, 4vh, 2.25rem);
  color: var(--blue);
  pointer-events: none;
  text-align: center;
}

.placement-guide small {
  max-width: 22rem;
  font-family: var(--mono);
  font-size: 0.61rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-wrap: balance;
}

.placement-guide strong {
  color: var(--red);
  font-size: clamp(4.5rem, 13vw, 8rem);
  font-weight: 400;
  line-height: 1;
}

.scan-line {
  position: absolute;
  top: 10%;
  right: 0;
  left: 0;
  z-index: 4;
  height: 1px;
  opacity: 0;
  background: var(--blue);
}

.scan-line::after {
  position: absolute;
  top: -1.4rem;
  right: 0;
  width: 4.5rem;
  padding: 0.25rem 0;
  color: var(--blue);
  background: var(--paper);
  content: "λ sweep";
  font-family: var(--mono);
  font-size: 0.55rem;
  text-align: right;
  text-transform: uppercase;
}

.reveal-stamp {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 2rem;
  color: var(--red);
  opacity: 0;
  text-align: center;
  transition: opacity 240ms ease;
}

.reveal-stamp small {
  margin-bottom: 1rem;
  font-family: var(--mono);
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.reveal-stamp strong {
  font-size: clamp(2rem, 4.4vw, 3.8rem);
  font-weight: 400;
  line-height: 1;
  letter-spacing: -0.04em;
}

.live-status,
#progress-value {
  font-family: var(--mono);
  font-size: 0.61rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.scan-meta {
  display: flex;
  min-height: 3rem;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  border-bottom: 1px solid var(--line);
}

#progress-value {
  flex: 0 0 auto;
}

.progress-track {
  height: 2px;
  background: var(--line);
}

.progress-track span {
  display: block;
  width: 0%;
  height: 100%;
  background: var(--blue);
  transition: width 90ms linear;
}

.live-status {
  margin: 0;
  color: var(--muted);
}

.noscript-message {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  left: 1rem;
  margin: 0;
  padding: 1rem;
  color: var(--paper);
  background: var(--red);
  text-align: center;
}

body[data-state="landing"] .scanner-shell,
body[data-state="mode"] .scanner-shell,
body[data-state="instructions"] .scanner-shell {
  display: none;
}

body[data-state="mode"] .scanner-standby,
body[data-state="instructions"] .scanner-standby,
body[data-state="placement"] .scanner-standby,
body[data-state="scanning"] .scanner-standby,
body[data-state="reveal"] .scanner-standby {
  opacity: 0;
}

body[data-mode="package"] .alignment-mark--package,
body[data-mode="chest"] .alignment-mark--chest {
  display: flex;
}

body[data-state="placement"] .placement-guide {
  display: flex;
}

body[data-state="scanning"] .scan-line {
  opacity: 1;
  animation: scanner-sweep 1.5s linear infinite alternate;
}

body[data-state="reveal"] .scanner-grid {
  opacity: 0.08;
}

body[data-state="reveal"] .alignment-mark,
body[data-state="reveal"] .coordinate {
  display: none;
}

body[data-state="reveal"] .scan-line {
  top: 66%;
  right: 16%;
  left: 16%;
  opacity: 1;
  background: var(--red);
  transition: inset 220ms ease, top 220ms ease;
}

body[data-state="reveal"] .scan-line::after {
  display: none;
}

body[data-state="reveal"] .reveal-stamp,
body[data-state="reveal"] .ruler-confession {
  opacity: 1;
  transform: none;
}

body[data-state="reveal"] .reveal-stamp {
  transition-delay: 100ms;
}

body:not([data-state="reveal"]) .reveal-stamp,
body:not([data-state="reveal"]) .ruler-confession {
  transition: none;
}

body[data-state="reveal"] .stage-kicker,
body[data-state="reveal"] .live-status {
  color: var(--red);
}

body[data-state="reveal"] .progress-track span {
  background: var(--red);
}

body[data-state="placement"],
body[data-state="scanning"] {
  height: 100svh;
  min-height: 100svh;
  overflow: hidden;
  padding-block: 0;
}

body[data-state="placement"] .instrument,
body[data-state="scanning"] .instrument {
  position: relative;
  min-height: 100%;
}

body[data-state="placement"] .instrument__body,
body[data-state="scanning"] .instrument__body {
  position: relative;
  display: flex;
  min-height: 100%;
  align-items: center;
  justify-content: center;
}

body[data-state="placement"] .scanner-shell,
body[data-state="scanning"] .scanner-shell,
body[data-state="reveal"] .scanner-shell {
  order: 1;
  padding-top: clamp(3rem, 8vh, 6rem);
  padding-bottom: clamp(4rem, 8vh, 6rem);
}

body[data-state="placement"] .scanner-shell,
body[data-state="scanning"] .scanner-shell {
  position: relative;
  width: 100%;
  height: 100svh;
  min-height: 100svh;
  padding: 0;
}

body[data-state="reveal"] .scanner-shell {
  padding-top: 0;
}

body[data-state="placement"] .scanner-glass,
body[data-state="scanning"] .scanner-glass,
body[data-state="reveal"] .scanner-glass {
  height: 100svh;
  min-height: 100svh;
  aspect-ratio: auto;
}

body[data-state="reveal"] .reveal-stamp strong {
  font-size: clamp(2.6rem, 8vw, 5.5rem);
}

body[data-state="placement"] .scan-meta,
body[data-state="scanning"] .scan-meta,
body[data-state="placement"] .progress-track,
body[data-state="scanning"] .progress-track {
  position: absolute;
  right: 0;
  left: 0;
  z-index: 8;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(2px);
}

body[data-state="placement"] .scan-meta,
body[data-state="scanning"] .scan-meta {
  bottom: 0.85rem;
  padding-inline: 0.15rem;
  border-bottom-color: rgba(23, 26, 24, 0.35);
}

body[data-state="placement"] .progress-track,
body[data-state="scanning"] .progress-track {
  bottom: 0.6rem;
}

body[data-state="placement"] .stage-panel,
body[data-state="scanning"] .stage-panel,
body[data-state="reveal"] .stage-panel {
  min-height: auto;
  order: 2;
  padding-block: clamp(4rem, 10vh, 7rem);
}

body[data-state="placement"] .stage-panel,
body[data-state="scanning"] .stage-panel {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
  background: none;
}

@keyframes scanner-sweep {
  from {
    top: 9%;
  }

  to {
    top: 91%;
  }
}

@media (max-width: 820px) {
  .instrument__body {
    display: flex;
  }

  .stage-panel {
    min-height: auto;
    padding-block: 4.5rem 3rem;
  }

  body[data-state="placement"] .stage-panel,
  body[data-state="scanning"] .stage-panel,
  body[data-state="reveal"] .stage-panel {
    order: 2;
  }

  body[data-state="placement"] .scanner-shell,
  body[data-state="scanning"] .scanner-shell,
  body[data-state="reveal"] .scanner-shell {
    order: 1;
  }

  body[data-state="reveal"] .scanner-shell {
    padding-top: 0;
  }

  body:not([data-state="placement"]):not([data-state="scanning"]):not([data-state="reveal"]) .scanner-shell {
    padding-block: 1rem 3rem;
  }

  .scanner-glass {
    min-height: 0;
  }
}

@media (max-width: 520px) {
  body {
    padding-inline: 1rem;
  }

  .stage-panel {
    padding-block: 3.5rem 2.5rem;
  }

  .stage-title {
    font-size: clamp(2.65rem, 14vw, 3.6rem);
  }

  .stage-title--compact {
    font-size: clamp(2.4rem, 12vw, 3.1rem);
  }

  .stage-title--reveal {
    font-size: clamp(2.35rem, 11.5vw, 3rem);
  }

  .stage-copy {
    font-size: 0.95rem;
  }

  .button-row {
    align-items: stretch;
    flex-direction: column;
  }

  .button {
    width: 100%;
  }

  .scanner-glass {
    aspect-ratio: 1 / 1;
    min-height: 0;
  }

  body[data-state="reveal"] .scanner-shell {
    padding-top: 0;
    padding-bottom: 2rem;
  }

  body[data-mode="package"][data-state="placement"],
  body[data-mode="package"][data-state="scanning"] {
    height: 100svh;
    min-height: 100svh;
    overflow: hidden;
  }

  body[data-mode="package"][data-state="placement"] .instrument__body,
  body[data-mode="package"][data-state="scanning"] .instrument__body {
    min-height: 100svh;
  }

  body[data-mode="package"][data-state="placement"] .scanner-shell,
  body[data-mode="package"][data-state="scanning"] .scanner-shell,
  body[data-mode="package"][data-state="placement"] .scanner-glass,
  body[data-mode="package"][data-state="scanning"] .scanner-glass {
    height: 80svh;
    min-height: 80svh;
  }

  .alignment-mark {
    width: min(64%, 14rem);
  }

  .alignment-mark--package {
    height: 88%;
    aspect-ratio: auto;
  }

  .alignment-mark--package > span {
    width: 36%;
    height: 84%;
  }

  .alignment-mark--chest {
    width: 92%;
    aspect-ratio: 2 / 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }

  body[data-state="scanning"] .scan-line {
    top: 50%;
  }
}
