:root {
  --bg: #0f1216;
  --card: #171b21;
  --fg: #f3f5f7;
  --muted: #9aa4b2;
  --primary: #2f6df6;
  --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 #2a313b;
  background: #0f1216;
  color: var(--fg);
  font-size: 1rem;
}
.tip {
  background: #0f1216;
  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: #2a313b;
  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;
}
#frame {
  position: relative;
  height: 74vh;
  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: 92vw;
  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;
  }
}
.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 {
  background: rgba(0, 0, 0, 0.55);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 0.95rem;
  max-width: 84vw;
  text-align: center;
}

#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 #2a313b;
  background: #0f1216;
  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: #0f1216;
  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%;
  border-radius: 8px;
  border: 1px solid #2a313b;
}
.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: #0f1216;
  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: #0f1216;
  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 #2a313b;
  padding-top: 16px;
}
.case-box {
  background: #0f1216;
  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;
}
