:root {
  --bg: #05070c;
  --ink: #f5f7fb;
  --muted: #9aa6b8;
  --accent: #5ee1ff;
  --chip: rgba(8, 11, 18, 0.72);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

[hidden] { display: none !important; }

/* ---------- camera stage ---------- */
#stage { position: fixed; inset: 0; overflow: hidden; background: #000; }

#cam { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* The card is moved only by its transform (set every frame by app.js). */
#card {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  will-change: transform;
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}
#stage[data-mode="playing"] #card,
#stage[data-mode="docked"] #card { opacity: 1; }

#inner {
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 14px;
  transition: border-radius 0.3s ease;
  isolation: isolate;
  /* Safari: forces the rounded clip to apply to the composited video layer */
  -webkit-mask-image: -webkit-radial-gradient(white, black);
  background: #000 center / cover no-repeat;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
/* Pinned on the poster: near-square corners so the printed corners do not peek out behind the video. */
#stage[data-mode="playing"] #inner { border-radius: 3px; }
#card.pop #inner { animation: pop 0.5s cubic-bezier(0.2, 1.25, 0.35, 1) both; }
@keyframes pop { from { transform: scale(0.82); opacity: 0; } }

#clip { display: block; width: 100%; height: 100%; object-fit: cover; }

/* ---------- scanning overlay ---------- */
#scan {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at center, transparent 45%, rgba(0, 0, 0, 0.45) 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}
#stage[data-mode="scanning"] #scan { opacity: 1; }

#reticle {
  position: relative;
  width: min(62vw, 34vh);
  aspect-ratio: 9 / 16;
  animation: breathe 2.4s ease-in-out infinite;
}
#reticle i {
  position: absolute;
  width: 34px;
  height: 34px;
  border: 0 solid var(--ink);
  filter: drop-shadow(0 0 6px rgba(94, 225, 255, 0.55));
}
#reticle i:nth-child(1) { top: 0; left: 0; border-top-width: 3px; border-left-width: 3px; border-top-left-radius: 12px; }
#reticle i:nth-child(2) { top: 0; right: 0; border-top-width: 3px; border-right-width: 3px; border-top-right-radius: 12px; }
#reticle i:nth-child(3) { bottom: 0; left: 0; border-bottom-width: 3px; border-left-width: 3px; border-bottom-left-radius: 12px; }
#reticle i:nth-child(4) { bottom: 0; right: 0; border-bottom-width: 3px; border-right-width: 3px; border-bottom-right-radius: 12px; }
#reticle::after {
  content: "";
  position: absolute;
  left: 6%;
  right: 6%;
  top: 0;
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: sweep 2.2s ease-in-out infinite alternate;
}
@keyframes breathe { 50% { transform: scale(1.03); } }
@keyframes sweep { to { transform: translateY(calc(min(62vw, 34vh) * 16 / 9)); } }

#hint {
  position: absolute;
  left: 50%;
  bottom: calc(34px + var(--safe-b));
  transform: translateX(-50%);
  max-width: 86vw;
  padding: 12px 20px;
  border-radius: 999px;
  background: var(--chip);
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}
#stage[data-mode="scanning"] #hint { opacity: 1; }

/* ---------- controls ---------- */
#bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(26px + var(--safe-b));
  display: flex;
  justify-content: center;
  gap: 14px;
}
#bar button {
  display: none;
  width: 52px;
  height: 52px;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 50%;
  background: var(--chip);
  color: var(--ink);
  cursor: pointer;
}
#bar svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#stage[data-mode="playing"].has-sound #sound,
#stage[data-mode="docked"].has-sound #sound,
#stage[data-mode="playing"].ended #replay,
#stage[data-mode="docked"] #close { display: block; }
#sound .cross { display: none; }
#sound.off .waves { display: none; }
#sound.off .cross { display: block; }
#sound.nudge { border-color: var(--accent); animation: nudge 1s ease-in-out infinite; }
@keyframes nudge { 50% { transform: scale(1.14); } }

/* ---------- landscape guard (phones only) ---------- */
#rotate {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  font-size: 18px;
  font-weight: 600;
  text-align: center;
}
@media (orientation: landscape) and (pointer: coarse) { #rotate { display: grid; } }

/* ---------- start screen ---------- */
#start {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: calc(32px + var(--safe-t)) 28px calc(32px + var(--safe-b));
  background: radial-gradient(120% 80% at 50% 0%, #12233a 0%, var(--bg) 62%);
  text-align: center;
}
/* Starting in landscape would lock the tracker to landscape frames; show the "hold upright" message instead. */
@media (orientation: landscape) and (pointer: coarse) { #start { display: none; } }
#start h1 { font-size: 30px; letter-spacing: 0.04em; }
#lead { max-width: 320px; color: var(--muted); font-size: 16px; line-height: 1.7; }
#status { min-height: 20px; color: var(--muted); font-size: 13px; }
#status.error { color: #ffb4a8; font-size: 15px; line-height: 1.6; }

#go {
  min-width: 220px;
  margin-top: 8px;
  padding: 16px 28px;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--accent), #7c8cff);
  color: #04121c;
  font: inherit;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(94, 225, 255, 0.28);
}
#go:disabled { opacity: 0.6; }
#copy { padding: 10px 18px; border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 999px; background: none; color: var(--ink); font: inherit; font-size: 14px; }

#poster-icon {
  position: relative;
  width: 84px;
  height: 132px;
  margin-bottom: 6px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  border-radius: 14px;
  overflow: hidden;
}
#poster-icon span {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 40%;
  background: linear-gradient(180deg, transparent, rgba(94, 225, 255, 0.5));
  border-bottom: 2px solid var(--accent);
  animation: icon-scan 1.8s ease-in-out infinite alternate;
}
@keyframes icon-scan { from { transform: translateY(-100%); } to { transform: translateY(250%); } }

@media (prefers-reduced-motion: reduce) {
  #reticle, #reticle::after, #poster-icon span, #sound.nudge { animation: none; }
}
