:root {
  --bg: #0b0b0d;
  --fg: #f4f1ec;
  --muted: #a39d94;
  --accent: #ff8a5b;
  --ok: #7bd389;
  --warn: #ffd166;
  --bad: #ff6b6b;
  --glass: rgba(12, 12, 14, .55);
  --radius: 14px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg);
  font: 15px/1.4 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif; overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
#app { position: fixed; inset: 0; display: grid; grid-template-rows: auto 1fr auto auto; }

/* stage */
#stage { position: relative; grid-row: 1 / span 4; grid-column: 1; overflow: hidden; background: #000; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transition: filter .25s; }
#video.mirror { transform: scaleX(-1); }
#guides { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
#flash.go { animation: flash .35s ease-out; }
@keyframes flash { 0% { opacity: .95 } 100% { opacity: 0 } }
#countdown { position: absolute; inset: 0; display: grid; place-items: center; font-size: 36vmin;
  font-weight: 700; color: #fff; text-shadow: 0 4px 40px rgba(0,0,0,.6); pointer-events: none; }
#countdown:empty { display: none; }

/* level */
#level { position: absolute; left: 50%; top: 50%; width: 120px; height: 120px; margin: -60px 0 0 -60px;
  pointer-events: none; display: none; }
#level.on { display: block; }
#level .horizon { position: absolute; left: 10px; right: 10px; top: 50%; height: 2px; background: #fff;
  opacity: .9; transform-origin: center; box-shadow: 0 0 8px rgba(0,0,0,.6); transition: background .2s; }
#level .horizon::before, #level .horizon::after { content: ""; position: absolute; top: -4px; width: 10px; height: 10px;
  border-radius: 50%; background: inherit; }
#level .horizon::before { left: -5px } #level .horizon::after { right: -5px }
#level.ok .horizon { background: var(--ok); }
#level .deg { position: absolute; left: 50%; top: calc(50% + 14px); transform: translateX(-50%); font-size: 12px;
  color: #fff; text-shadow: 0 1px 4px #000; font-variant-numeric: tabular-nums; }

/* coach */
#coach { position: absolute; left: 12px; right: 12px; top: calc(var(--safe-t) + 72px); display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.hint { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--glass); backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px); border-radius: var(--radius); font-size: 14px; max-width: 420px; pointer-events: auto; }
.hint .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; transition: background .3s; }
.hint.ok .dot { background: var(--ok); } .hint.warn .dot { background: var(--warn); } .hint.bad .dot { background: var(--bad); }
.hint.tip { color: #efe8dd; } .hint.tip .text::before { content: "Pose: "; color: var(--accent); font-weight: 600; }
.hint.tip[data-kind="light"] .text::before { content: "Light: "; } .hint.tip[data-kind="framing"] .text::before { content: "Framing: "; }
.hint.tip[data-kind="expression"] .text::before { content: "Expression: "; }
.hint.tip button { margin-left: auto; width: 30px; height: 30px; border-radius: 50%; background: rgba(255,255,255,.1); flex: none; }

/* start */
#startOverlay { position: absolute; inset: 0; display: grid; place-items: center; background: radial-gradient(ellipse at 50% 30%, #1d1a1f, #0b0b0d 70%); z-index: 5; padding: 24px; }
#startOverlay[hidden] { display: none; }
.card { text-align: center; max-width: 360px; color: var(--fg); }
.card svg { color: var(--accent); }
.card h1 { margin: 10px 0 6px; font-size: 28px; letter-spacing: .5px; }
.card p { color: var(--muted); margin: 0 0 20px; }
.primary { background: var(--accent); color: #1a120d; font-weight: 600; padding: 12px 22px; border-radius: 999px; }
.primary.ghost { background: rgba(255,255,255,.12); color: var(--fg); text-decoration: none; display: inline-block; }
.danger { color: var(--bad); padding: 12px 16px; }
.err { color: var(--bad) !important; font-size: 13px; }
.card .about { display: block; width: fit-content; margin: 14px auto 0; font-size: 13px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.muted { color: var(--muted); }

/* bars */
#topbar { grid-row: 1; grid-column: 1; z-index: 2; display: flex; justify-content: space-between; align-items: flex-start; gap: 6px;
  padding: calc(var(--safe-t) + 8px) 10px 0; }
#topbar .icon { display: flex; flex-direction: column; align-items: center; min-width: 56px; padding: 6px 4px; border-radius: 12px;
  background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); font-size: 18px; line-height: 1.1; }
#topbar .icon small { font-size: 10px; color: var(--muted); margin-top: 2px; }
.toggle.on { outline: 2px solid var(--accent); outline-offset: -2px; }
.toggle.on small { color: var(--accent); }

#looks { grid-row: 3; grid-column: 1; z-index: 2; display: flex; gap: 6px; overflow-x: auto; padding: 6px 12px; scrollbar-width: none; justify-content: safe center; }
#looks::-webkit-scrollbar { display: none; }
#looks button { padding: 6px 12px; border-radius: 999px; background: var(--glass); backdrop-filter: blur(10px); font-size: 13px; white-space: nowrap; }
#looks button.on { background: var(--fg); color: #111; font-weight: 600; }

#bottombar { grid-row: 4; grid-column: 1; z-index: 2; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 8px 20px calc(var(--safe-b) + 18px); background: linear-gradient(transparent, rgba(0,0,0,.55)); }
#shutter { width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; display: grid; place-items: center; justify-self: center; }
#shutter span { width: 60px; height: 60px; border-radius: 50%; background: #fff; transition: transform .1s; }
#shutter:active span, #shutter.busy span { transform: scale(.85); background: var(--accent); }
.thumb { width: 52px; height: 52px; border-radius: 10px; background: rgba(255,255,255,.12); overflow: hidden; position: relative; justify-self: start; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb img[hidden] { display: none; }
.thumb .count { position: absolute; right: 3px; bottom: 2px; font-size: 11px; font-weight: 600; text-shadow: 0 1px 3px #000; }
#levelBtn { justify-self: end; width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,.12); font-size: 18px; }
#levelBtn.on { background: var(--accent); color: #1a120d; }
#levelBtn[hidden] { display: none; }

/* gallery & viewer */
#gallery, #viewer { position: fixed; inset: 0; background: var(--bg); z-index: 10; display: flex; flex-direction: column; }
#gallery[hidden], #viewer[hidden] { display: none; }
#gallery header { display: flex; align-items: center; gap: 10px; padding: calc(var(--safe-t) + 10px) 14px 10px; }
#gallery header h2 { margin: 0; font-size: 18px; flex: 1; }
#gallery header .icon, #viewer .icon { width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,.1); font-size: 24px; line-height: 1; }
#grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 3px; padding: 0 3px; overflow-y: auto; flex: 1; align-content: start; }
#grid button { aspect-ratio: 3/4; overflow: hidden; border-radius: 4px; background: #222; position: relative; padding: 0; }
#grid img { width: 100%; height: 100%; object-fit: cover; display: block; }
#grid .badge { position: absolute; left: 4px; bottom: 4px; font-size: 10px; background: rgba(0,0,0,.6); padding: 2px 6px; border-radius: 999px; }
#galleryEmpty { text-align: center; padding: 40px 20px; }
#galleryEmpty[hidden] { display: none; }
#viewer img { flex: 1; min-height: 0; object-fit: contain; width: 100%; background: #000; }
#viewer .meta { padding: 8px 16px 0; color: var(--muted); font-size: 13px; text-align: center; }
#viewer .actions { display: flex; gap: 10px; align-items: center; padding: 10px 16px calc(var(--safe-b) + 16px); }
#viewer .actions .primary { flex: 1; text-align: center; }

@media (min-width: 900px) {
  #stage { display: grid; place-items: center; }
  #video { position: relative; inset: auto; max-height: 100vh; aspect-ratio: 3/4; width: auto; height: 100%; }
  /* #stage prefix: the overlay's base rule comes later in this file and would otherwise win the tie. */
  #stage #guides, #stage #overlay { inset: 0 auto 0 50%; width: auto; height: 100%; aspect-ratio: 3/4; }
  #stage #guides { transform: translateX(-50%); }
  #stage #overlay { --pos: translateX(-50%); }
}

/* template overlay */
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; pointer-events: none; transform: var(--pos, none) scaleX(var(--flip, 1)); }
#overlay[hidden] { display: none; }
/* active-template bar inside the camera */
#tplBar { position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 4; display: flex; gap: 10px; background: var(--glass); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-radius: 16px; padding: 10px; }
#tplBar[hidden] { display: none; }
#tplBar img { width: 64px; height: 84px; object-fit: cover; border-radius: 8px; flex: none; }
#tplBar .info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
#tplBar .top { display: flex; align-items: center; gap: 8px; }
#tplBar .top strong { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#tplBar .top .icon { width: 28px; height: 28px; border-radius: 50%; background: rgba(255,255,255,.1); font-size: 18px; line-height: 1; flex: none; }
#tplBar .muted { font-size: 12px; line-height: 1.3; }
#tplBar .row { display: flex; align-items: center; gap: 10px; }
#tplBar label { flex: 1; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
#tplBar input[type=range] { flex: 1; accent-color: var(--accent); min-width: 0; }
#tplBar .credit { margin: 0; font-size: 10.5px; color: var(--muted); }
#tplBar .credit a { color: var(--muted); }
#tplBar .credit[hidden] { display: none; }

/* pose browser view */
#tplView { position: fixed; inset: 0; background: var(--bg); z-index: 10; display: flex; flex-direction: column; }
#tplView[hidden] { display: none; }
#tplView header { display: flex; align-items: center; gap: 10px; padding: calc(var(--safe-t) + 10px) 14px 8px; }
#tplView header h2 { margin: 0; font-size: 18px; flex: 1; }
#tplView header .icon { width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,.1); font-size: 24px; line-height: 1; }
#tplTabs { display: flex; gap: 6px; overflow-x: auto; padding: 4px 14px 10px; scrollbar-width: none; flex: none; }
#tplTabs::-webkit-scrollbar { display: none; }
#tplTabs button { padding: 7px 14px; border-radius: 999px; background: rgba(255,255,255,.08); font-size: 13px; white-space: nowrap; }
#tplTabs button.on { background: var(--fg); color: #111; font-weight: 600; }
#tplBody { flex: 1; overflow-y: auto; padding: 0 14px calc(var(--safe-b) + 20px); }
#tplBody section { margin: 6px 0 18px; }
#tplBody h3 { margin: 0 0 2px; font-size: 15px; }
#tplBody p { margin: 0 0 8px; font-size: 12.5px; color: var(--muted); line-height: 1.35; }
#tplBody p.small { margin-top: 10px; }
#tplBody .row { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
#tplBody .row::-webkit-scrollbar { display: none; }
.tpl { flex: none; width: 96px; height: 128px; border-radius: 10px; background: rgba(255,255,255,.08); overflow: hidden; position: relative; display: grid; place-items: center; padding: 0; }
.tpl img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tpl span { position: absolute; left: 4px; right: 4px; bottom: 4px; font-size: 10px; line-height: 1.15; text-shadow: 0 1px 3px #000; text-align: center; }
.tpl.add span { position: static; font-size: 12px; color: var(--muted); padding: 6px; }
.tpl.add { border: 1px dashed rgba(255,255,255,.3); }
.tpl.on { outline: 3px solid var(--accent); outline-offset: -3px; }
.tpl i { position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; border-radius: 50%; background: rgba(0,0,0,.6); font-style: normal; font-size: 15px; line-height: 22px; }
@media (min-width: 900px) { #tplBody { padding-inline: 24px; } .tpl { width: 132px; height: 176px; } }
.chip { padding: 6px 12px; border-radius: 999px; background: rgba(255,255,255,.12); font-size: 13px; white-space: nowrap; }
.chip.on { background: var(--accent); color: #1a120d; font-weight: 600; }
.primary.small { padding: 7px 14px; font-size: 13px; }

/* ai coach card */
.hint.ai { flex-direction: column; align-items: stretch; gap: 8px; border: 1px solid rgba(255,138,91,.35); }
.hint.ai[hidden] { display: none; }
.hint.ai .row { display: flex; align-items: center; gap: 8px; }
.hint.ai .row .text { flex: 1; }
.hint.ai .row .text::before { content: "AI: "; color: var(--accent); font-weight: 600; }
.hint.ai.busy .row .text { color: var(--muted); }
.hint.ai.busy .row .text::before { content: "AI… "; }
#aiScore { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 13px; flex: none; background: var(--muted); color: #111; }
#aiScore[hidden] { display: none; }
#aiScore.ok { background: var(--ok); } #aiScore.warn { background: var(--warn); } #aiScore.bad { background: var(--bad); color: #fff; }
#aiDetail { display: none; margin: 0; padding: 0 0 0 2px; list-style: none; font-size: 12.5px; color: #d9d2c7; }
#aiDetail li { padding: 2px 0; } #aiDetail b { color: var(--accent); text-transform: capitalize; font-weight: 600; }
.hint.ai.open #aiDetail { display: block; }
.hint.ai.ready { border-color: var(--ok); }
#shutter.pulse { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(123,211,137,.0) } 50% { box-shadow: 0 0 0 10px rgba(123,211,137,.45) } }
#bottombar .right { justify-self: end; display: flex; gap: 8px; }
#aiBtn { width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,.12); font-size: 14px; font-weight: 700; letter-spacing: .5px; }
#aiBtn.on { background: var(--accent); color: #1a120d; }
#viewer .actions .ghost { flex: 0 0 auto; padding-inline: 14px; }
.hint.ai .row[hidden] { display: none; }

#tplBody h3 .chip.tiny { margin-left: 10px; padding: 3px 9px; font-size: 11px; vertical-align: middle; }
#tplBody .tpl.add small { display: block; font-size: 10px; margin-top: 2px; }
#tplImportStatus[hidden] { display: none; }
#tplTabs button.private::before { content: "\1F512\00a0"; font-size: 11px; }
/* Outlines (silhouettes/*.svg): white half-opaque strokes, so their thumbs need a mid-grey card. */
.tpl.svg { background: #3b3d44; }
.tpl.svg img { object-fit: contain; }
#tplBar img.svg { background: #3b3d44; object-fit: contain; }
#tplBar input[type=range]:disabled { opacity: .35; }
