:root {
  /* Light only, matching the store (the page never follows the phone's dark mode). */
  color-scheme: only light;
  --bg: #eef2ec;
  --card: #ffffff;
  --fg: #16281d;
  --muted: #56675b;
  --panel: #f4f7f2;
  --line: #d5ddd3;
  --primary: #20432f;
  --primary-fg: #fff;
  --searching: #9aa4b2;
  --misaligned: #ef4444;
  --nearly: #f59e0b;
  --aligned: #22c55e;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}
[hidden] {
  display: none !important;
}

/* ---- Card screens (intro / fallback) ---- */
#intro,
#fallback {
  align-items: center;
  justify-content: center;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  overflow-y: auto;
}
.card {
  width: 100%;
  max-width: 460px;
  background: var(--card);
  border-radius: 16px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
h1 {
  font-size: 1.5rem;
  margin: 0;
}
.lead {
  color: var(--muted);
  margin: 0;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.95rem;
}
select {
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--fg);
  font-size: 1rem;
}
.tip {
  background: var(--panel);
  border-left: 3px solid var(--nearly);
  border-radius: 8px;
  padding: 12px;
  font-size: 0.9rem;
  color: var(--muted);
}
.warn {
  color: var(--nearly);
  font-size: 0.9rem;
  margin: 0;
}

/* ---- Buttons ---- */
.btn {
  appearance: none;
  border: 0;
  border-radius: 12px;
  padding: 16px;
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.btn-primary {
  background: var(--primary);
  color: var(--primary-fg);
}
.btn-secondary {
  background: var(--line);
  color: var(--fg);
}
.file-btn {
  display: block;
}
.link {
  color: var(--muted);
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: 0;
  font-size: 0.95rem;
}
.link-light {
  color: #fff;
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 16px;
  z-index: 3;
}

/* ---- Camera ---- */
#camera {
  background: #000;
}
#video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
#frame-wrap {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  pointer-events: none;
  z-index: 2;
}
/* Long axis = 80% of the phone's long edge in BOTH orientations (80vh portrait /
   80vw landscape map to the same physical length when the device is rotated), so
   the strip guide is the same size whichever way the phone is held. */
#frame {
  position: relative;
  height: 80vh;
  aspect-ratio: 1 / 6.5; /* set from data-aspect at runtime */
  border: 3px dashed var(--searching);
  border-radius: 10px;
  transition:
    border-color 120ms linear,
    box-shadow 120ms linear;
}
#frame[data-orientation='landscape'] {
  height: auto;
  width: 80vw;
  aspect-ratio: 6.5 / 1;
}
#frame.state-searching {
  border-style: dashed;
  border-color: var(--searching);
}
#frame.state-misaligned {
  border-style: solid;
  border-color: var(--misaligned);
}
#frame.state-nearly {
  border-style: solid;
  border-color: var(--nearly);
}
#frame.state-aligned {
  border-style: solid;
  border-color: var(--aligned);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.35);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
  50% {
    box-shadow: 0 0 0 7px rgba(34, 197, 94, 0.18);
  }
}
@media (prefers-reduced-motion: reduce) {
  #frame.state-aligned {
    animation: none;
  }
}
/* ---- strip guide ghost (inside the frame) ---- */
#strip-ghost {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.45;
  z-index: 1;
}
#strip-ghost > div {
  position: absolute;
}
.g-body {
  border: 1.5px dashed rgba(255, 255, 255, 0.75);
  border-radius: 6px;
}
/* Test area = the alignment target; make it the prominent part of the guide. */
.g-testarea {
  border: 2px solid rgba(34, 197, 94, 0.95);
  background: rgba(34, 197, 94, 0.14);
  border-radius: 4px;
}
.g-grip {
  overflow: hidden;
  border-radius: 6px;
}
.g-test {
  position: absolute;
  white-space: nowrap;
  color: rgba(255, 255, 255, 0.75);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 1px;
  word-spacing: 2px;
  line-height: 1;
}
.g-line {
  background: #fff;
}
.g-label {
  position: absolute;
  font-size: 0.72rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  text-shadow: 0 0 3px rgba(0, 0, 0, 0.8);
  line-height: 1;
}

.corner {
  position: absolute;
  width: 16px;
  height: 16px;
  border: 3px solid #fff;
}
.corner.tl {
  top: -3px;
  left: -3px;
  border-right: 0;
  border-bottom: 0;
}
.corner.tr {
  top: -3px;
  right: -3px;
  border-left: 0;
  border-bottom: 0;
}
.corner.bl {
  bottom: -3px;
  left: -3px;
  border-right: 0;
  border-top: 0;
}
.corner.br {
  bottom: -3px;
  right: -3px;
  border-left: 0;
  border-top: 0;
}
.hint {
  color: var(--muted);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 0.95rem;
  max-width: 84vw;
  text-align: center;
}
/* #10: pin the camera hint near the TOP so it never sits under the shutter button
   (it used to be centered in the flex column, right above the bottom controls). */
#frame-wrap > #hint {
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  position: absolute;
  top: calc(16px + var(--safe-top, 0px) + 44px);
  left: 50%;
  transform: translateX(-50%);
}

#cam-controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(24px + var(--safe-bottom));
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 36px;
}
.btn-icon {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 1.3rem;
  cursor: pointer;
}
.shutter {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 5px solid #fff;
  background: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.shutter:active {
  transform: scale(0.95);
}
.countdown {
  font-size: 1.6rem;
  font-weight: 700;
  color: #111;
}

/* ---- Review ---- */
#review {
  background: #000;
  padding: calc(12px + var(--safe-top)) 12px calc(16px + var(--safe-bottom));
  gap: 12px;
}
.review-img-wrap {
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 0;
}
#shot {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}
.review-actions {
  display: flex;
  gap: 12px;
}
.review-actions .btn {
  flex: 1;
}
progress {
  width: 100%;
}

/* ---- Result (D2) ---- */
#result {
  align-items: center;
  overflow-y: auto;
}
.result-scroll {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  overflow-y: auto;
}
.result-card {
  gap: 18px;
}
textarea,
input[type='email'] {
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--fg);
  font: inherit;
  width: 100%;
  resize: vertical;
}

.verdict {
  display: flex;
  align-items: center;
  gap: 14px;
  border-radius: 12px;
  padding: 16px;
}
.verdict-icon {
  font-size: 2rem;
  line-height: 1;
}
.verdict-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.verdict-text strong {
  font-size: 1.5rem;
}
.verdict-sub {
  color: var(--muted);
  font-size: 0.95rem;
}
/* Drug-screen semantics: negative = all-clear, positive = flagged. */
.verdict-negative {
  background: rgba(34, 197, 94, 0.12);
  border-left: 4px solid var(--aligned);
}
.verdict-positive {
  background: rgba(239, 68, 68, 0.12);
  border-left: 4px solid var(--misaligned);
}
.verdict-invalid {
  background: var(--panel);
  border-left: 4px solid var(--searching);
}
.verdict-uncertain {
  background: rgba(245, 158, 11, 0.12);
  border-left: 4px solid var(--nearly);
}

.confidence {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.crop-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
}
#result-crop {
  max-width: 100%;
  max-height: 45vh;
  object-fit: contain;
  border-radius: 8px;
  border: 1px solid var(--line);
}
.crop-cap {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.zones {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.zone {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  background: var(--panel);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.9rem;
}
.zone .zone-name {
  font-weight: 600;
}
.zone .zone-lines {
  color: var(--muted);
  font-size: 0.85rem;
}
.disclaimer {
  background: var(--panel);
  border-left: 3px solid var(--nearly);
  border-radius: 8px;
  padding: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}
.result-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.subform {
  display: flex;
  flex-direction: column;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 16px;
}
.case-box {
  background: var(--panel);
  border-radius: 10px;
  padding: 16px;
  text-align: center;
}
.case-box p {
  margin: 0 0 4px;
  color: var(--muted);
}
.case-ref {
  font-size: 1.4rem;
  letter-spacing: 0.05em;
}
.consent {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}
.consent input {
  margin-top: 2px;
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Desktop hand-off (§1): centred QR + status inside the card. */
#desktop-qr {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.qr-img {
  width: 220px;
  height: 220px;
  image-rendering: pixelated; /* keep QR modules crisp when scaled */
  background: #fff;
  padding: 12px;
  border-radius: 12px;
}

/* #11: big C/T zone labels beside the result crop. T = test (top), C = control
   (bottom), ~a quarter of the crop height. */
.crop-framed {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 10px;
}
.crop-zonebar {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: 2px 0;
}
.zlabel {
  font-weight: 800;
  line-height: 1;
  font-size: clamp(1.8rem, 11vh, 5rem);
}
.zlabel-t { color: #d98f45; }
.zlabel-c { color: #37b26b; }

/* #13: feedback / review modal overlay (replaces the inline page-expanding forms). */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-card {
  background: var(--card);
  color: var(--fg);
  width: min(440px, 100%);
  max-height: 90vh;
  overflow: auto;
  border-radius: 14px;
  padding: 22px 20px;
  position: relative;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal-card h2 { margin: 0 6px 8px 0; font-size: 1.2rem; }
.modal-close {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}
.req { color: #e0574a; }
