:root {
  --red: #ff4d4f;
  --yellow: #fadb14;
  --green: #3ddc84;
  --fg: #fff;
  --bg: #000;
  --muted: rgba(255, 255, 255, 0.72);
  --panel: rgba(0, 0, 0, 0.55);
  --line: rgba(255, 255, 255, 0.35);
  --ring: min(64vw, 44vh);
  --ring: min(64vw, 44dvh);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.screen { position: fixed; inset: 0; }

/* ---------- Buttons ---------- */

.btn {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 12px 20px;
  background: var(--panel);
  color: var(--fg);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--fg); border-color: var(--fg); color: #000; }
.btn.small { padding: 6px 14px; font-size: 0.9rem; }
.btn:active { transform: scale(0.97); }

/* ---------- Start ---------- */

#start {
  display: grid;
  place-items: center;
  padding: 24px 16px;
  text-align: center;
}
.start-card { max-width: 22rem; }
.logo { width: 96px; height: 96px; }
h1 { margin: 12px 0 8px; font-size: 1.75rem; }
#start p { color: var(--muted); }
#start .btn { margin-top: 12px; padding: 14px 32px; font-size: 1.1rem; }
.error { min-height: 1.4em; color: var(--red) !important; }

/* ---------- Viewfinder ---------- */

#viewfinder { --state: var(--red); }
#viewfinder[data-status="near"] { --state: var(--yellow); }
#viewfinder[data-status="level"] { --state: var(--green); }

#video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain; /* show the whole frame: what you see is what gets saved */
  background: #000;
}

.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 16px;
  background: linear-gradient(rgba(0, 0, 0, 0.65), transparent);
}
.readout {
  min-width: 4.5ch;
  color: var(--state);
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.badge {
  margin-right: auto;
  padding: 3px 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--panel);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.badge.surface { border-color: var(--green); color: var(--green); }

.ring {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--ring);
  height: var(--ring);
  translate: -50% -50%;
  border: 2px solid var(--state);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4), inset 0 0 0 1px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  transition: border-color 0.15s;
}
.cross { position: absolute; inset: 0; }
.cross::before, .cross::after {
  content: "";
  position: absolute;
  background: rgba(255, 255, 255, 0.45);
}
.cross::before { left: 50%; top: 0; bottom: 0; width: 1px; }
.cross::after { top: 50%; left: 0; right: 0; height: 1px; }
/* Tolerance zone: 2° of the ring's 10° range = 20% of its diameter. */
.target {
  position: absolute;
  inset: 40%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
}
.bubble {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 12%;
  height: 12%;
  translate: -50% -50%;
  border-radius: 50%;
  background: var(--state);
  box-shadow: 0 0 12px var(--state), 0 0 0 2px rgba(0, 0, 0, 0.5);
  transition: background-color 0.15s;
  will-change: transform;
}

.banner {
  position: absolute;
  left: 16px;
  right: 16px;
  top: calc(env(safe-area-inset-top) + 72px);
  margin: 0;
  padding: 10px 14px;
  border-radius: 8px;
  background: rgba(255, 77, 79, 0.92);
  text-align: center;
}

.bottombar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 28px);
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
}
.shutter {
  width: 76px;
  height: 76px;
  padding: 0;
  border: 5px solid var(--state);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s, border-color 0.15s;
}
.shutter:disabled { opacity: 0.35; cursor: not-allowed; }
.shutter:not(:disabled):active { transform: scale(0.9); }
.shutter.busy { opacity: 0.6; pointer-events: none; }

.flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}
.flash.on { animation: flash 0.3s ease-out; }
@keyframes flash { from { opacity: 0.85; } to { opacity: 0; } }

/* ---------- Review ---------- */

#review {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
#photo {
  flex: 1;
  min-height: 0;
  width: 100%;
  object-fit: contain;
}
.photo-info {
  margin: 8px 16px 0;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}
.actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 24px);
}
.actions .btn { flex: 1; max-width: 9rem; }

/* ---------- Calibration overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px 16px;
  background: rgba(0, 0, 0, 0.9);
  text-align: center;
}
.cal-count {
  min-height: 1.2em;
  font-size: 6rem;
  font-weight: 800;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}
.cal-msg { max-width: 20rem; margin: 0; font-size: 1.15rem; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 128px);
  z-index: 20;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  translate: -50% 0;
  border-radius: 8px;
  background: rgba(30, 30, 30, 0.95);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.toast.on { opacity: 1; }
