:root {
  --bg: #0f0e0c;
  --ink: #f4efe4;
  --muted: #b9b0a0;
  --gold: #c9a24a;
  --gold-ink: #1d1606;
  --panel: rgba(18, 16, 13, 0.72);
  --line: rgba(244, 239, 228, 0.16);
  --target-aspect: 1.5;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "SF Arabic", "Geeza Pro", "Noto Kufi Arabic", "Noto Sans Arabic", Tahoma, sans-serif;
  line-height: 1.6;
  overflow: hidden;
  overscroll-behavior: none;
}
[hidden], .hidden { display: none !important; }
button { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: currentColor; flex: none; }

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

/* ---------------------------------------------------------------- start */
#start { display: flex; align-items: flex-end; }
.start-bg {
  position: absolute; inset: 0;
  background: #1a1815 center 40% / cover no-repeat;
  filter: saturate(0.9);
}
.start-bg::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(15,14,12,0.15) 0%, rgba(15,14,12,0.55) 38%, rgba(15,14,12,0.94) 66%, var(--bg) 100%);
}
.start-inner {
  position: relative; width: 100%; max-width: 520px; margin: 0 auto;
  padding: 24px 16px calc(24px + var(--safe-bottom));
}
.chip {
  display: inline-block; font-size: 13px; padding: 3px 10px; border-radius: 999px;
  border: 1px solid rgba(201, 162, 74, 0.6); color: var(--gold); background: rgba(201,162,74,0.08);
}
h1 { font-size: clamp(28px, 8vw, 40px); line-height: 1.25; margin: 12px 0 6px; font-weight: 800; }
.lead { margin: 0 0 20px; color: #ddd5c6; font-size: 17px; }
.choices { display: grid; gap: 10px; }
.choice {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: start;
  padding: 14px 16px; border-radius: 16px; border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06); cursor: pointer; min-height: 64px;
  transition: transform .12s ease, background .2s ease;
}
.choice:active { transform: scale(0.985); }
.choice b { display: block; font-size: 17px; }
.choice small { display: block; color: var(--muted); font-size: 13.5px; }
.choice.primary { background: var(--gold); color: var(--gold-ink); border-color: transparent; }
.choice.primary small { color: rgba(29, 22, 6, 0.75); }
.choice:disabled { opacity: 0.5; }
.choice svg { width: 26px; height: 26px; }
.note { margin: 14px 0 0; font-size: 13.5px; color: var(--muted); text-align: center; }
.note a { color: var(--gold); }
.desktop-only { display: none; }
body.desktop .desktop-only { display: block; }

/* ---------------------------------------------------------------- AR */
#ar-screen { background: #000; }
#ar { position: absolute; inset: 0; overflow: hidden; z-index: 0; }

.overlay {
  position: absolute; inset: 0; z-index: 5; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 18px; pointer-events: none;
  text-align: center; padding: 16px;
}
.overlay p {
  margin: 0; padding: 8px 16px; border-radius: 999px; background: var(--panel);
  font-size: 16px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#loading { background: rgba(0,0,0,0.55); }
.spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.18); border-top-color: var(--gold);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.scan-frame {
  position: relative; width: min(82vw, 62vh * var(--target-aspect), 560px);
  aspect-ratio: var(--target-aspect);
}
.scan-frame img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
  opacity: 0.22; border-radius: 6px;
}
.scan-frame .c { position: absolute; width: 34px; height: 34px; border: 3px solid var(--ink); }
.scan-frame .tl { top: -6px; right: -6px; border-left: 0; border-bottom: 0; border-top-right-radius: 12px; }
.scan-frame .tr { top: -6px; left: -6px; border-right: 0; border-bottom: 0; border-top-left-radius: 12px; }
.scan-frame .bl { bottom: -6px; right: -6px; border-left: 0; border-top: 0; border-bottom-right-radius: 12px; }
.scan-frame .br { bottom: -6px; left: -6px; border-right: 0; border-top: 0; border-bottom-left-radius: 12px; }
.scanline {
  position: absolute; left: 4%; right: 4%; height: 2px; top: 0;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
  animation: scan 2.2s ease-in-out infinite alternate;
}
@keyframes scan { to { top: calc(100% - 2px); } }
body.holding #scanning { display: none !important; }

.pill {
  position: absolute; z-index: 7; top: calc(64px + var(--safe-top)); left: 50%;
  transform: translateX(-50%); padding: 8px 14px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); font-size: 14px;
  white-space: nowrap; display: none;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
body.holding #hold-pill { display: block; }
.pill.sound { top: auto; bottom: calc(110px + var(--safe-bottom)); background: var(--gold); color: var(--gold-ink); border: 0; font-weight: 700; cursor: pointer; }
body.needs-sound #sound-pill { display: block; }

#caption {
  position: absolute; z-index: 6; left: 16px; right: 16px; bottom: calc(104px + var(--safe-bottom));
  margin: 0 auto; max-width: 560px; text-align: center; font-size: 17px; font-weight: 600;
  padding: 10px 14px; border-radius: 14px; background: rgba(10, 9, 8, 0.66);
  opacity: 0; transform: translateY(6px); transition: opacity .25s ease, transform .25s ease;
  pointer-events: none;
}
#caption.on { opacity: 1; transform: none; }
body:not(.mode-model) #caption { display: none; }

.hud { position: absolute; inset: 0; z-index: 8; pointer-events: none; }
.hud button { pointer-events: auto; cursor: pointer; }
.icon-btn {
  position: absolute; top: calc(14px + var(--safe-top)); right: 14px;
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  border: 1px solid var(--line); background: var(--panel);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.controls {
  position: absolute; left: 0; right: 0; bottom: calc(18px + var(--safe-bottom));
  display: flex; justify-content: center; gap: 10px; padding: 0 16px;
  opacity: 0; transform: translateY(10px); transition: opacity .3s ease, transform .3s ease;
}
body.found .controls { opacity: 1; transform: none; }
.ctl {
  display: flex; align-items: center; gap: 8px; min-height: 48px; padding: 0 16px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--panel);
  font-size: 15px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
body.ended #btn-replay { background: var(--gold); color: var(--gold-ink); border-color: transparent; font-weight: 700; }
#btn-mute .off { display: none; }
body.muted #btn-mute .on { display: none; }
body.muted #btn-mute .off { display: block; }
.model-only { display: none; }
body.mode-model .model-only { display: flex; }

/* ---------------------------------------------------------------- error */
#error, #camera-paused { display: flex; align-items: center; justify-content: center; padding: 16px; }
#error[hidden] { display: none !important; }
.error-box { max-width: 420px; text-align: center; }
.error-box h2 { margin: 0 0 8px; font-size: 22px; }
.error-box p { color: var(--muted); margin: 0 0 20px; }
.error-box .choice { justify-content: center; text-align: center; }

/* ---------------------------------------------------------------- debug */
#debug { display: none; }
body.debug #debug {
  display: block; position: fixed; z-index: 20; left: 8px; bottom: 8px; max-width: 70vw; max-height: 32vh;
  overflow: hidden; margin: 0; padding: 6px 8px; border-radius: 8px; direction: ltr; text-align: left;
  font: 11px/1.35 ui-monospace, Menlo, monospace; background: rgba(0,0,0,0.6); color: #9fe39f;
  pointer-events: none; white-space: pre-wrap;
}

/* ---------------------------------------------------------------- camera-first layout */
#scanning {
  position: absolute; left: 0; right: 0; bottom: calc(28px + var(--safe-bottom)); z-index: 5;
  display: flex; justify-content: center; pointer-events: none; padding: 0 16px;
}
#scanning .hint {
  margin: 0; padding: 8px 16px; border-radius: 999px; background: var(--panel); font-size: 15px;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#loading { background: transparent; justify-content: flex-end; padding-bottom: calc(28px + var(--safe-bottom)); }
#camera { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: -2; }
.pill.sound { display: none; align-items: center; gap: 8px; top: calc(16px + var(--safe-top)); bottom: auto; }
.pill.sound svg { width: 18px; height: 18px; }
body.needs-sound #sound-pill { display: flex; }
body.needs-sound #hold-pill { top: calc(64px + var(--safe-top)); }
/* the browser would not start the video by itself (iPhone in Low Power Mode): a tap starts it */
body.needs-tap #play-pill { display: flex; }
/* a 3D card on the iPhone: one tap allows the phone's rotation, which keeps the model still */
body.needs-motion #motion-pill { display: flex; }
body.needs-motion #sound-pill { display: none; }
body.needs-tap #sound-pill { display: none; }
body.needs-tap #hold-pill { top: calc(64px + var(--safe-top)); }
#hold-pill { top: calc(16px + var(--safe-top)); }
.ctl { width: 48px; justify-content: center; padding: 0; }

/* videos laid out behind the camera picture (iPhone needs them on the page to start without a tap) */
#video-pool { position: absolute; inset: 0; z-index: -3; pointer-events: none; }
#video-pool video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
#video-pool::after { content: ""; position: absolute; inset: 0; background: #000; }

#ar { will-change: transform; }
