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

html, body {
  height: 100%;
  background: #000;
  color: #fff;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
body { display: grid; place-items: center; height: 100dvh; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Story-shaped stage (9:16), letterboxed inside the viewport */
.stage {
  position: relative;
  aspect-ratio: 9 / 16;
  height: min(100dvh, calc(100vw * 16 / 9));
  overflow: hidden;
  background: #111;
  container-type: size;
}
@media (min-width: 600px) {
  .stage { height: min(94dvh, calc(100vw * 16 / 9)); border-radius: 18px; }
}

.view { position: absolute; inset: 0; }
.stage[data-mode="camera"] .editor-view,
.stage[data-mode="editor"] .camera-view { display: none; }

video, .overlay, .photo {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
}
video { background: #111; }
video.mirror { transform: scaleX(-1); }
.overlay { pointer-events: none; }

.cam-msg {
  position: absolute; left: 10%; right: 10%; top: 40%;
  text-align: center; font-size: 14px; line-height: 1.5;
  background: rgba(0,0,0,.6); padding: 14px 16px; border-radius: 12px;
}

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

/* ── Camera controls ── */
.cam-bottom {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 0 0 max(18px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(0,0,0,.55));
}
.filter-strip {
  display: flex; gap: 12px;
  overflow-x: auto; scrollbar-width: none;
  padding: 10px 16px 14px;
  scroll-snap-type: x proximity;
}
.filter-strip::-webkit-scrollbar { display: none; }

.chip {
  position: relative;
  flex: 0 0 auto;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 600;
  text-shadow: 0 1px 3px rgba(0,0,0,.6);
  scroll-snap-align: center;
}
.chip .thumb {
  position: relative;
  width: 54px; height: 54px; border-radius: 50%;
  overflow: hidden;
  border: 2px solid rgba(255,255,255,.35);
  transition: transform .15s, border-color .15s;
}
.chip .thumb i {
  position: absolute; inset: 0;
  background: linear-gradient(135deg, #feda75 0%, #fa7e1e 30%, #d62976 60%, #4f5bd5 100%);
}
.chip .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.chip.active .thumb { border: 3px solid #fff; transform: scale(1.08); }
.chip.add .thumb {
  display: grid; place-items: center;
  background: rgba(255,255,255,.15);
  border-style: dashed;
  font-size: 26px; font-weight: 300;
}
.chip .remove {
  position: absolute; top: -4px; right: -4px;
  width: 20px; height: 20px; border-radius: 50%;
  background: #ff3b30; color: #fff;
  font-size: 13px; line-height: 20px; text-align: center;
  display: none;
}
.chip.active .remove { display: block; }

.shutter-row {
  display: flex; align-items: center; justify-content: space-around;
  padding: 0 24px;
}
.shutter {
  width: 76px; height: 76px; border-radius: 50%;
  border: 5px solid #fff;
  background: rgba(255,255,255,.25);
  box-shadow: 0 2px 12px rgba(0,0,0,.35);
  transition: transform .1s;
}
.shutter:active { transform: scale(.9); background: rgba(255,255,255,.6); }

.icon-btn {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(0,0,0,.35);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.icon-btn:active { transform: scale(.92); }
.text-icon { font-weight: 700; font-size: 17px; }

/* ── Editor ── */
.layers { position: absolute; inset: 0; touch-action: none; }
.layer {
  position: absolute;
  transform-origin: center;
  cursor: grab;
  touch-action: none;
}
.layer.pop { animation: pop .25s ease-out; }
@keyframes pop { from { opacity: 0; } to { opacity: 1; } }
.layer.doomed { opacity: .4; }

.editor-top, .editor-bottom {
  position: absolute; left: 0; right: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 14px;
  pointer-events: none;
  transition: opacity .2s;
}
.editor-top > *, .editor-bottom > * { pointer-events: auto; }
.editor-top { top: 0; padding-top: max(14px, env(safe-area-inset-top)); }
.editor-bottom { bottom: 0; justify-content: flex-end; padding-bottom: max(16px, env(safe-area-inset-bottom)); }
.spacer { flex: 1; }
.stage.dragging .editor-top,
.stage.dragging .editor-bottom { opacity: 0; pointer-events: none; }
.stage.dragging .editor-top > *,
.stage.dragging .editor-bottom > * { pointer-events: none; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  height: 44px; padding: 0 18px; border-radius: 22px;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  font-weight: 600; font-size: 15px;
}
.pill svg { width: 18px; height: 18px; }
.pill.primary { background: #fff; color: #000; }
.pill.small { height: 36px; padding: 0 14px; font-size: 14px; }
.pill.icon-only { width: 36px; padding: 0; justify-content: center; font-weight: 800; border: 2px solid #fff; background: transparent; }
.pill.icon-only.on { background: #fff; color: #000; }
.pill:active { transform: scale(.95); }

.trash {
  position: absolute; left: 50%; bottom: 28px;
  width: 56px; height: 56px; margin-left: -28px;
  border-radius: 50%;
  display: grid; place-items: center;
  border: 2px solid #fff;
  background: rgba(0,0,0,.4);
  opacity: 0; transform: scale(.6);
  transition: opacity .15s, transform .15s, background .15s;
  pointer-events: none;
}
.trash.show { opacity: 1; transform: scale(1); }
.trash.hot { background: #ff3b30; border-color: #ff3b30; transform: scale(1.2); }

/* ── Text editor ── */
.text-editor {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  z-index: 5;
}
.te-top { display: flex; align-items: center; gap: 10px; padding: 14px; padding-top: max(14px, env(safe-area-inset-top)); }
.te-center { flex: 1; display: grid; place-items: center; padding: 0 7.5cqw; overflow: auto; }
.text-input {
  /* 80px on a 1080px-wide story → matches the exported text size */
  font-size: calc(80cqw / 10.8);
  line-height: 1.25;
  font-weight: 700;
  text-align: center;
  min-width: 1ch;
  max-width: 85cqw;
  padding: calc(28cqw / 10.8) calc(32cqw / 10.8);
  border-radius: calc(28cqw / 10.8);
  outline: none;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text; -webkit-user-select: text;
  caret-color: #fff;
  text-shadow: 0 1px 6px rgba(0,0,0,.35);
}
.text-input.boxed { text-shadow: none; }
.color-row {
  display: flex; gap: 10px; justify-content: center; flex-wrap: wrap;
  padding: 14px 14px max(20px, env(safe-area-inset-bottom));
}
.color-row button {
  width: 30px; height: 30px; border-radius: 50%;
  border: 3px solid #fff;
  transition: transform .1s;
}
.color-row button.active { transform: scale(1.25); }

/* ── Sticker sheet ── */
.sheet-backdrop {
  position: absolute; inset: 0; z-index: 5;
  background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end;
}
.sheet {
  width: 100%; max-height: 60%;
  display: flex; flex-direction: column;
  background: rgba(28,28,30,.96);
  border-radius: 18px 18px 0 0;
  padding: 8px 14px max(16px, env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
.sheet-handle { width: 40px; height: 5px; border-radius: 3px; background: #555; margin: 0 auto 10px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; font-weight: 700; margin-bottom: 10px; }
.sheet-head .pill { background: #3a3a3c; }
.sticker-grid {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px;
  overflow-y: auto;
}
.sticker-grid button { font-size: 32px; aspect-ratio: 1; border-radius: 10px; }
.sticker-grid button:hover { background: rgba(255,255,255,.08); }

/* ── Toast ── */
.toast {
  position: absolute; left: 50%; top: 42%;
  transform: translate(-50%, -50%);
  font-size: 26px; font-weight: 700;
  text-shadow: 0 2px 10px rgba(0,0,0,.6);
  opacity: 0; pointer-events: none;
  transition: opacity .25s;
  white-space: nowrap;
  z-index: 10;
}
.toast.show { opacity: 1; }
.toast.small { font-size: 15px; background: rgba(0,0,0,.7); padding: 10px 16px; border-radius: 20px; text-shadow: none; }

/* ── Background-filter preview ── */
.preview { position: absolute; inset: 0; width: 100%; height: 100%; }
.chip .thumb i.blurred, .tile .thumb i.blurred { filter: blur(4px); transform: scale(1.3); }
/* little person silhouette marks background filters */
.thumb .person {
  position: absolute; left: 50%; bottom: -14%;
  width: 46%; height: 62%; transform: translateX(-50%);
  background:
    radial-gradient(circle at 50% 22%, #f2d0b8 0 20%, transparent 21%),
    radial-gradient(ellipse at 50% 100%, #333 0 55%, transparent 56%);
}

/* ── Filter library ── */
.library {
  position: absolute; inset: 0; z-index: 6;
  display: flex; align-items: flex-end;
}
.lib-panel {
  width: 100%; height: 62%;
  display: flex; flex-direction: column;
  background: rgba(22,22,24,.94);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-radius: 18px 18px 0 0;
  padding: 8px 16px max(16px, env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
.lib-head { display: flex; align-items: center; justify-content: space-between; font-weight: 700; font-size: 17px; margin-bottom: 10px; }
.tabs { display: flex; background: #2c2c2e; border-radius: 10px; padding: 3px; }
.tabs button { flex: 1; height: 32px; border-radius: 8px; font-size: 13px; font-weight: 600; color: #aaa; }
.tabs button.active { background: #48484a; color: #fff; }
.lib-hint { font-size: 12px; color: #999; margin: 10px 2px; line-height: 1.4; }
.lib-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px 8px;
  overflow-y: auto; padding: 4px 2px;
}
.tile {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 600; color: #ddd;
}
.tile span:not(.thumb):not(.remove) { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .thumb {
  position: relative;
  width: 100%; aspect-ratio: 9 / 13; border-radius: 10px;
  overflow: hidden;
  border: 2px solid transparent;
}
.tile .thumb i {
  position: absolute; inset: 0;
  background: linear-gradient(135deg, #feda75 0%, #fa7e1e 30%, #d62976 60%, #4f5bd5 100%);
}
.tile .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.tile.active .thumb { border-color: #fff; }
.tile.add .thumb {
  display: grid; place-items: center;
  background: #2c2c2e; border: 2px dashed #555;
  font-size: 30px; font-weight: 300;
}
.tile .remove {
  position: absolute; top: -6px; right: -4px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #ff3b30; color: #fff;
  font-size: 15px; line-height: 22px; text-align: center;
}

.color-form { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; padding-top: 4px; }
.slider { display: grid; grid-template-columns: 92px 1fr; align-items: center; gap: 10px; font-size: 13px; color: #ccc; }
.slider input { width: 100%; accent-color: #fff; }
.name-input {
  height: 40px; border-radius: 10px; border: 0; padding: 0 12px;
  background: #2c2c2e; color: #fff; font: inherit; font-size: 15px;
  user-select: text; -webkit-user-select: text;
}
.form-actions { display: flex; justify-content: flex-end; gap: 8px; }
.form-actions .pill:not(.primary) { background: #3a3a3c; }

/* ── Kudax brand mark (camera screen) ── */
.brand-mark {
  position: absolute; top: max(16px, env(safe-area-inset-top)); left: 16px;
  height: 22px; width: auto;
  opacity: .9; pointer-events: none;
  filter: drop-shadow(0 1px 4px rgba(0,0,0,.45));
  transition: opacity .2s;
}
.stage.has-frame .brand-mark { opacity: 0; }

/* ── Environment badge (staging / development only) ── */
.env-badge {
  position: absolute; top: max(14px, env(safe-area-inset-top)); right: 14px; z-index: 20;
  padding: 4px 10px; border-radius: 12px;
  font-size: 11px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase;
  background: #ffb020; color: #1a1200;
  pointer-events: none;
}
.env-badge.env-development { background: #34c759; color: #04210c; }
