:root {
  --bg: #fff6e9;
  --card: #ffffff;
  --ink: #2b2140;
  --muted: #7a6d8c;
  --line: #eadcc8;
  --accent: #ff6b5b;
  --accent-2: #6b5bff;
  --soft: #f6ecff;
  --ok: #2fb56f;
  --r: 18px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: Fredoka, ui-rounded, system-ui, -apple-system, sans-serif;
  overflow: hidden;
  -webkit-user-select: none; user-select: none;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button { font: inherit; color: inherit; cursor: pointer; }
#video { position: fixed; width: 1px; height: 1px; opacity: 0; pointer-events: none; left: 0; top: 0; }

.top { display: flex; align-items: center; gap: 12px; padding: 10px 16px; }
.brand { display: flex; align-items: center; gap: 8px; font-size: 22px; }
.logo { font-size: 28px; }
.chip { background: var(--card); border: 2px solid var(--line); border-radius: 99px; padding: 4px 12px; font-size: 14px; color: var(--muted); }
.chip.ok { color: var(--ok); border-color: #bfe8d1; }
.chip.warn { color: #c27a00; border-color: #f3dcad; }
.top-actions { margin-left: auto; display: flex; gap: 8px; }
.icon-btn { width: 44px; height: 44px; border-radius: 50%; border: 2px solid var(--line); background: var(--card); font-size: 20px; }

.app { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 340px; gap: 12px; padding: 0 12px 12px; }
@media (max-aspect-ratio: 1/1), (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) minmax(260px, 42%); }
}

.stage-area { position: relative; min-height: 0; min-width: 0; display: grid; place-items: center; }
.stage-box { position: relative; border-radius: var(--r); overflow: hidden; box-shadow: 0 10px 30px rgba(80, 50, 20, .18); background: #fff; }
#stage { display: block; width: 100%; height: 100%; touch-action: none; }

.page-btn {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 56px; height: 84px; border: 0; border-radius: 16px;
  background: rgba(43, 33, 64, .35); color: #fff; font-size: 44px; line-height: 1;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.page-btn.prev { left: 10px; } .page-btn.next { right: 10px; }
.page-btn:active { background: rgba(43, 33, 64, .6); }
.page-btn[hidden] { display: none; }

.rec-badge { position: absolute; top: 12px; right: 12px; background: rgba(0, 0, 0, .6); color: #fff; border-radius: 99px; padding: 6px 14px; display: flex; gap: 8px; align-items: center; font-weight: 600; }
.rec-badge .dot { width: 12px; height: 12px; border-radius: 50%; background: #ff3b30; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .25; } }
.countdown { position: absolute; inset: 0; display: grid; place-items: center; font-size: 180px; font-weight: 700; color: #fff; text-shadow: 0 6px 30px rgba(0, 0, 0, .45); background: rgba(43, 33, 64, .25); }
.countdown.pop { animation: pop .9s ease-out; }
@keyframes pop { from { transform: scale(1.6); opacity: 0; } 30% { opacity: 1; } to { transform: scale(1); } }
.hint { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); background: rgba(255, 255, 255, .92); border-radius: 99px; padding: 6px 16px; font-weight: 500; }

.start { position: absolute; inset: 0; display: grid; place-items: center; background: linear-gradient(135deg, #ffe3c7, #e9dcff); padding: 16px; overflow: auto; }
.start-card { max-width: 520px; text-align: center; }
.start-chars { font-size: 44px; letter-spacing: 6px; }
.start h1 { font-size: clamp(26px, 4vw, 40px); line-height: 1.1; margin: 10px 0; }
.start p { color: #5a4d6c; font-size: 17px; margin: 0 0 18px; }
.start .small { margin-top: 12px; font-size: 14px; min-height: 1em; }
.big-btn { border: 0; background: var(--accent); color: #fff; font-size: 22px; font-weight: 600; padding: 16px 34px; border-radius: 99px; box-shadow: 0 6px 0 #d64a3c; }
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #d64a3c; }
.big-btn:disabled { opacity: .6; }
.link-btn { display: block; margin: 14px auto 0; background: none; border: 0; color: var(--accent-2); text-decoration: underline; font-size: 16px; }

.rec-btn {
  position: absolute; right: 18px; bottom: 18px; width: 84px; height: 84px; border-radius: 50%;
  border: 6px solid #fff; background: rgba(255, 255, 255, .35); display: grid; place-items: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25); z-index: 3;
}
.rec-core { width: 56px; height: 56px; border-radius: 50%; background: #ff3b30; transition: all .2s; }
.rec-btn.on .rec-core { width: 30px; height: 30px; border-radius: 6px; }
.rec-btn:disabled { opacity: .4; }
.mic-meter { position: absolute; right: 118px; bottom: 34px; width: 10px; height: 52px; border-radius: 6px; background: rgba(255, 255, 255, .6); overflow: hidden; z-index: 3; display: flex; align-items: flex-end; }
.mic-meter span { display: block; width: 100%; height: 0%; background: linear-gradient(#ff6b5b, #2fb56f); transition: height .08s; }

.panel { background: var(--card); border-radius: var(--r); border: 2px solid var(--line); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.tabs { display: flex; border-bottom: 2px solid var(--line); }
.tab { flex: 1; border: 0; background: none; padding: 8px 2px; font-size: 22px; display: flex; flex-direction: column; align-items: center; gap: 2px; position: relative; color: var(--muted); }
.tab span { font-size: 12px; font-weight: 500; }
.tab.active { color: var(--ink); background: var(--soft); }
.tab i { position: absolute; top: 4px; right: 12%; background: var(--accent); color: #fff; font-style: normal; font-size: 11px; border-radius: 99px; padding: 1px 6px; }
.pane { display: none; padding: 14px; overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; }
.pane.active { display: block; }
.pane h2 { margin: 0 0 10px; font-size: 20px; }
.pane h3 { margin: 16px 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; }
.small { font-size: 13px; color: var(--muted); line-height: 1.4; }

.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 10px; }
.char { border: 3px solid transparent; background: var(--soft); border-radius: 16px; padding: 4px 4px 8px; text-align: center; font-size: 13px; font-weight: 500; }
.char img { width: 100%; aspect-ratio: 1; display: block; }
.char.active { border-color: var(--accent-2); background: #ebe6ff; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg.three { grid-template-columns: repeat(3, 1fr); }
.seg button { border: 2px solid var(--line); background: #fff; border-radius: 14px; padding: 10px 6px; font-size: 15px; font-weight: 500; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.seg button small { font-size: 11px; color: var(--muted); font-weight: 400; }
.seg button.active { border-color: var(--accent-2); background: #ebe6ff; }

.slider { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; font-size: 14px; color: var(--muted); }
.slider input { width: 100%; accent-color: var(--accent-2); }
.toggle { display: flex; gap: 8px; align-items: center; margin-top: 14px; font-size: 15px; }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent-2); }

.backdrops { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.backdrops button { border: 3px solid transparent; border-radius: 12px; height: 54px; font-size: 11px; color: #fff; font-weight: 600; text-shadow: 0 1px 3px rgba(0, 0, 0, .6); display: flex; align-items: flex-end; justify-content: center; padding: 4px; }
.backdrops button.active { border-color: var(--accent-2); box-shadow: 0 0 0 2px #fff inset; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.field input { font: inherit; font-size: 16px; color: var(--ink); padding: 10px 12px; border-radius: 12px; border: 2px solid var(--line); -webkit-user-select: text; user-select: text; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
.row.center { justify-content: center; }
.btn { border: 2px solid var(--line); background: #fff; border-radius: 99px; padding: 9px 16px; font-size: 15px; font-weight: 500; display: inline-flex; align-items: center; gap: 4px; }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.btn.ghost { background: none; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; font-size: 100px; }
.thumbs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.thumb { position: relative; border: 3px solid transparent; border-radius: 10px; overflow: hidden; background: #f3eee6; aspect-ratio: 4/3; padding: 0; }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb.active { border-color: var(--accent-2); }
.thumb b { position: absolute; left: 4px; top: 4px; background: rgba(43, 33, 64, .7); color: #fff; font-size: 11px; border-radius: 6px; padding: 0 5px; }
.thumb .x { position: absolute; right: 2px; top: 2px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .9); font-size: 13px; line-height: 24px; padding: 0; }

.voice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.voice-grid button { border: 2px solid var(--line); background: #fff; border-radius: 14px; padding: 10px 4px; display: flex; flex-direction: column; align-items: center; font-size: 28px; }
.voice-grid button span { font-size: 13px; font-weight: 500; }
.voice-grid button.active { border-color: var(--accent-2); background: #ebe6ff; }
.voice-grid button:disabled { opacity: .4; }

.takes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.take { border: 0; padding: 0; background: var(--soft); border-radius: 12px; overflow: hidden; text-align: left; }
.take img { width: 100%; aspect-ratio: 16/9; object-fit: cover; display: block; background: #ddd; }
.take div { padding: 6px 8px; font-size: 13px; }
.empty { grid-column: 1 / -1; color: var(--muted); text-align: center; padding: 20px 0; }

.modal { position: fixed; inset: 0; background: rgba(43, 33, 64, .55); display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card { background: #fff; border-radius: 22px; padding: 14px; width: min(900px, 100%); }
.modal video { width: 100%; max-height: 66vh; border-radius: 14px; background: #000; display: block; }
[hidden] { display: none !important; }
.char.loading img { background: linear-gradient(90deg, #efe7fb, #fff, #efe7fb); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; border-radius: 12px; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---- full-screen stage mode: only the character, record button and exit */
.fs-btn {
  position: absolute; right: 12px; top: 12px; z-index: 3; display: flex; align-items: center; gap: 8px;
  border: 0; border-radius: 99px; padding: 12px 20px; font-size: 18px; font-weight: 600;
  background: rgba(43, 33, 64, .72); color: #fff; box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.fs-btn .fs-ico { font-size: 22px; line-height: 1; }
.fs-btn:active { transform: scale(.96); }
body.fs { background: #000; }
body.fs .top, body.fs .panel { display: none; }
body.fs .app { grid-template-columns: 1fr; grid-template-rows: 1fr; padding: 0; gap: 0; }
body.fs .stage-box { border-radius: 0; box-shadow: none; }
body.fs .rec-btn { width: 96px; height: 96px; right: 28px; bottom: 28px; }
body.fs .rec-core { width: 64px; height: 64px; }
body.fs .rec-btn.on .rec-core { width: 34px; height: 34px; }
body.fs .mic-meter { right: 140px; bottom: 50px; }
body.fs .fs-btn { right: auto; left: 20px; top: 20px; padding: 10px 16px; font-size: 16px; }
body.recording .fs-btn { opacity: 0; pointer-events: none; }

/* ======================================================================
   PHONES — portrait: big stage on top, compact swipeable picker below,
   record button centred on the stage like a camera app.
   ====================================================================== */
@supports (height: 100dvh) { html, body { height: 100dvh; } }

@media (max-width: 600px) and (orientation: portrait) {
  .top { padding: 6px 10px; gap: 8px; }
  .brand b { display: none; }
  .logo { font-size: 24px; }
  .chip { font-size: 12px; padding: 3px 10px; max-width: 64vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .icon-btn { width: 38px; height: 38px; font-size: 18px; }

  .app { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; gap: 8px; padding: 0 8px 8px; }
  .stage-box { border-radius: 14px; }
  .panel { max-height: 38vh; max-height: 38dvh; border-radius: 16px; }
  .tab { font-size: 18px; padding: 6px 2px; }
  .tab span { font-size: 11px; }
  .pane { padding: 10px; }
  .pane h2 { font-size: 15px; margin: 0 0 8px; }
  .pane h3 { margin: 10px 0 6px; font-size: 13px; }
  .small { font-size: 12px; }
  .toggle { margin-top: 10px; font-size: 14px; }

  /* one swipeable row of characters / voices instead of a tall grid */
  .char-grid, .voice-grid { display: flex; overflow-x: auto; gap: 8px; padding-bottom: 6px; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; }
  .char { flex: 0 0 84px; scroll-snap-align: start; font-size: 12px; }
  .voice-grid button { flex: 0 0 88px; font-size: 24px; }
  .backdrops button { height: 46px; }

  .rec-btn { width: 70px; height: 70px; border-width: 5px; right: auto; left: 50%; bottom: 12px; transform: translateX(-50%); }
  .rec-core { width: 46px; height: 46px; }
  .rec-btn.on .rec-core { width: 26px; height: 26px; }
  .mic-meter { right: auto; left: calc(50% + 48px); bottom: 22px; height: 44px; }
  .fs-btn { top: 8px; right: 8px; padding: 8px 12px; font-size: 14px; gap: 6px; }
  .fs-btn .fs-ico { font-size: 17px; }
  .rec-badge { top: 8px; right: 8px; padding: 4px 10px; font-size: 14px; }
  .hint { bottom: 92px; font-size: 13px; }
  .countdown { font-size: 120px; }

  body.fs .rec-btn { width: 80px; height: 80px; right: auto; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); }
  body.fs .rec-core { width: 54px; height: 54px; }
  body.fs .mic-meter { right: auto; left: calc(50% + 54px); bottom: calc(34px + env(safe-area-inset-bottom)); }
  body.fs .fs-btn { left: auto; right: 12px; top: calc(12px + env(safe-area-inset-top)); }

  .modal { padding: 10px; }
  .modal-card { padding: 10px; border-radius: 18px; }
  .modal video { max-height: 62vh; }
  .modal .btn { padding: 8px 12px; font-size: 14px; }
}

/* PHONES — landscape: stage on the left, slim icon-tab panel on the right, no header */
@media (max-height: 500px) and (orientation: landscape) {
  .top { display: none; }
  .app { grid-template-columns: minmax(0, 1fr) 250px; grid-template-rows: minmax(0, 1fr); gap: 6px; padding: 6px; }
  .stage-box { border-radius: 12px; }
  .panel { border-radius: 14px; }
  .tab { font-size: 18px; padding: 5px 2px; }
  .tab span { display: none; }
  .pane { padding: 8px; }
  .pane h2 { font-size: 14px; margin: 0 0 6px; }
  .pane h3 { margin: 8px 0 6px; font-size: 12px; }
  .small { font-size: 11px; }
  .toggle { margin-top: 8px; font-size: 13px; }
  .char-grid { grid-template-columns: repeat(auto-fill, minmax(66px, 1fr)); gap: 6px; }
  .char { font-size: 11px; padding: 2px 2px 4px; border-radius: 12px; }
  .voice-grid { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
  .voice-grid button { font-size: 20px; padding: 6px 2px; }
  .backdrops button { height: 40px; font-size: 10px; }
  .rec-btn { width: 62px; height: 62px; border-width: 4px; right: 12px; bottom: 12px; }
  .rec-core { width: 42px; height: 42px; }
  .rec-btn.on .rec-core { width: 24px; height: 24px; }
  .mic-meter { right: 82px; bottom: 22px; height: 40px; }
  .fs-btn { top: 8px; right: 8px; padding: 7px 12px; font-size: 13px; gap: 6px; }
  .fs-btn .fs-ico { font-size: 16px; }
  .countdown { font-size: 96px; }
  body.fs .rec-btn { width: 72px; height: 72px; right: calc(16px + env(safe-area-inset-right)); bottom: 16px; }
  body.fs .rec-core { width: 48px; height: 48px; }
  body.fs .mic-meter { right: calc(100px + env(safe-area-inset-right)); bottom: 30px; }
  body.fs .fs-btn { left: calc(12px + env(safe-area-inset-left)); top: 12px; }
  .modal video { max-height: 58vh; }
}

/* the welcome card covers the whole screen on small devices so nothing is cut off */
@media (max-width: 600px), (max-height: 500px) {
  .start { position: fixed; inset: 0; z-index: 10; padding: calc(20px + env(safe-area-inset-top)) 20px calc(20px + env(safe-area-inset-bottom)); }
  .start-chars { font-size: 34px; letter-spacing: 4px; }
  .start h1 { font-size: 26px; }
  .start p { font-size: 15px; margin-bottom: 14px; }
  .big-btn { font-size: 19px; padding: 14px 28px; }
}

/* ---- video editor */
.editor-card { width: min(1100px, 100%); max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px); display: flex; flex-direction: column; gap: 10px; }
.ed-head { display: flex; align-items: center; justify-content: space-between; font-size: 20px; }
.ed-body { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(260px, 1fr); gap: 16px; min-height: 0; overflow: auto; }
.ed-left { min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.ed-preview { background: #000; border-radius: 14px; overflow: hidden; max-height: 62vh; display: grid; place-items: center; aspect-ratio: 16 / 9; }
.ed-preview canvas { max-width: 100%; max-height: 62vh; display: block; }
.ed-transport { display: flex; align-items: center; gap: 10px; }
.ed-play { width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--accent-2); color: #fff; font-size: 18px; flex: none; }
.ed-transport input { flex: 1; accent-color: var(--accent-2); }
.ed-time { font-size: 13px; color: var(--muted); white-space: nowrap; }
.ed-right h3 { margin: 12px 0 6px; font-size: 15px; }
.ed-right h3:first-child { margin-top: 0; }
.ed-bgs { grid-template-columns: repeat(4, 1fr); }
.ed-bgs button { height: 46px; font-size: 10px; }
.ed-bgs button:disabled { opacity: .35; }
.ed-trim { display: grid; grid-template-columns: 44px 1fr 40px; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); margin: 4px 0; }
.ed-trim input { width: 100%; accent-color: var(--accent); }
.ed-text { width: 100%; font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 12px; border: 2px solid var(--line); -webkit-user-select: text; user-select: text; }
.ed-row { display: flex; gap: 10px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.ed-pos { flex: 1; min-width: 150px; }
.ed-pos button { padding: 8px 6px; }
.ed-cols { display: flex; gap: 6px; }
.ed-cols button { width: 38px; height: 38px; border-radius: 50%; border: 3px solid transparent; font-weight: 700; font-size: 13px; box-shadow: 0 0 0 1px var(--line); }
.ed-cols button.active { border-color: var(--accent-2); }
.ed-foot { display: flex; align-items: center; gap: 12px; justify-content: flex-end; }
.ed-busy { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.ed-prog { height: 10px; border-radius: 6px; background: var(--soft); overflow: hidden; }
.ed-prog span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--accent-2), var(--accent)); transition: width .2s; }
.ed-go { font-size: 17px; padding: 12px 22px; }
#editor.busy .ed-right, #editor.busy .ed-transport, #editor.busy #edClose { opacity: .4; pointer-events: none; }
@media (max-width: 760px), (max-height: 500px) {
  #editor { padding: 6px; align-items: stretch; }
  .editor-card { max-height: none; height: 100%; padding: 10px; border-radius: 16px; }
  .ed-body { grid-template-columns: 1fr; gap: 10px; }
  .ed-preview, .ed-preview canvas { max-height: 42vh; }
  .ed-bgs { grid-template-columns: repeat(4, 1fr); }
  .ed-head { font-size: 17px; }
}
@media (max-height: 500px) and (orientation: landscape) {
  .ed-body { grid-template-columns: minmax(0, 1.2fr) minmax(220px, 1fr); }
  .ed-preview, .ed-preview canvas { max-height: 60vh; }
}

/* editor scrolling: the whole card scrolls (works with touch on iPad/phone); the save bar stays visible */
#editor { overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; align-items: start; touch-action: pan-y; }
#editor .editor-card { max-height: none; height: auto; margin: auto 0; overflow: visible; display: block; }
#editor .ed-head { margin-bottom: 10px; }
#editor .ed-body { overflow: visible; }
#editor .ed-foot { position: sticky; bottom: -14px; background: #fff; padding: 10px 0 4px; margin-top: 8px; z-index: 2; }
#editor input[type=range] { touch-action: pan-y; }
@media (max-width: 760px), (max-height: 500px) {
  #editor { padding: 8px; }
  #editor .ed-foot { bottom: -10px; }
}

/* picker: spinner on the card while its 3D model downloads; red ring if it failed (tap again to retry) */
.char { position: relative; }
.char-spin { display: none; position: absolute; top: 50%; left: 50%; width: 28px; height: 28px; margin: -24px 0 0 -14px;
  border-radius: 50%; border: 4px solid rgba(107, 91, 255, .25); border-top-color: var(--accent-2); animation: spin .8s linear infinite; }
.char.busy .char-spin { display: block; }
.char.busy img { opacity: .45; }
.char.failed { border-color: #ff6b5b; }
@keyframes spin { to { transform: rotate(360deg); } }
