:root {
  --bg: #fff8ec;
  --ink: #2b2233;
  --muted: #6d6478;
  --card: #ffffff;
  --primary: #ff7a3d;
  --primary-dark: #e2652b;
  --accent: #6c5ce7;
  --danger: #e63946;
  --radius: 18px;
  --shadow: 0 6px 20px rgba(60, 30, 0, 0.12);
  --font: "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  --text-size: 1.25rem;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
body.bigtext { --text-size: 1.6rem; }
button, input, textarea, select { font-family: inherit; font-size: 1.05rem; }

.screen { display: none; position: fixed; inset: 0; overflow: auto; }
.screen.active { display: flex; flex-direction: column; }

.btn {
  border: 0; border-radius: 999px; padding: 0.8rem 1.4rem; cursor: pointer;
  background: #fff; color: var(--ink); box-shadow: var(--shadow); font-weight: 700;
  transition: transform 0.08s, box-shadow 0.08s; touch-action: manipulation; min-height: 48px;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { background: #ffe3e6; color: var(--danger); }
.btn.big { font-size: 1.3rem; padding: 1rem 2rem; width: 100%; max-width: 420px; }
.btn.small { padding: 0.55rem 1rem; font-size: 0.95rem; min-height: 40px; }
.btn.icon { width: 48px; height: 48px; padding: 0; font-size: 1.4rem; display: inline-flex; align-items: center; justify-content: center; }
.btn.glass { background: rgba(255,255,255,0.75); backdrop-filter: blur(6px); }
.btn:disabled { opacity: 0.5; cursor: default; }

/* ---------------- home */
#screen-home { align-items: center; justify-content: flex-start; overflow-y: auto; background: linear-gradient(180deg, #ffe9c7, #fff8ec 60%); }
.home-card { display: flex; flex-direction: column; align-items: center; gap: 0.8rem; padding: 1rem 1.2rem 2rem; margin: auto 0; text-align: center; width: 100%; max-width: 640px; }
.logo-img { width: 100%; max-width: 640px; border-radius: var(--radius); box-shadow: var(--shadow); }
.logo { font-size: clamp(1.8rem, 5vw, 2.6rem); margin: 0; color: var(--primary); letter-spacing: -0.02em; text-shadow: 0 4px 0 #ffd5b8; }
.tagline { margin: 0 0 0.5rem; color: var(--muted); font-size: 1.15rem; }
.home-heroes { display: flex; gap: 0.5rem; justify-content: center; height: 150px; margin-bottom: 0.5rem; }
.home-heroes svg { height: 150px; }

/* ---------------- top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 0.7rem 1rem; gap: 0.5rem; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,0.06); position: sticky; top: 0; z-index: 5; }
.topbar h2 { margin: 0; font-size: 1.3rem; }

/* ---------------- saved */
.saved-list { padding: 1rem; display: grid; gap: 0.8rem; max-width: 720px; width: 100%; margin: 0 auto; }
.saved-item { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 0.9rem 1rem; display: flex; gap: 0.8rem; align-items: center; cursor: pointer; }
.saved-item .thumbs { display: flex; height: 80px; }
.saved-item .thumbs svg { height: 80px; }
.saved-item .info { flex: 1; min-width: 0; }
.saved-item .info b { font-size: 1.15rem; display: block; }
.saved-item .info span { color: var(--muted); font-size: 0.95rem; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.saved-item .del { background: none; border: 0; font-size: 1.4rem; color: var(--muted); cursor: pointer; padding: 0.5rem; }
.empty { text-align: center; color: var(--muted); padding: 2rem; }

/* ---------------- builder */
.builder { display: flex; flex: 1; min-height: 0; }
.builder-preview { width: 42%; max-width: 380px; display: flex; flex-direction: column; align-items: center; padding: 0.8rem; gap: 0.6rem; background: linear-gradient(180deg, #e3f4ff, #fff8ec); }
.char-tabs { display: flex; gap: 0.4rem; flex-wrap: wrap; justify-content: center; }
.char-tab { border: 2px solid transparent; background: #fff; border-radius: 999px; padding: 0.35rem 0.9rem; cursor: pointer; font-weight: 700; }
.char-tab.active { border-color: var(--accent); color: var(--accent); }
.preview-stage { flex: 1; min-height: 220px; width: 100%; display: flex; align-items: flex-end; justify-content: center; }
.preview-stage svg { height: 100%; max-height: 360px; }
.name-input { width: 100%; padding: 0.7rem 1rem; border-radius: 999px; border: 2px solid #ddd; text-align: center; font-size: 1.25rem; font-weight: 700; }
.name-input:focus { outline: none; border-color: var(--accent); }
.preview-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; justify-content: center; }
.builder-options { flex: 1; overflow: auto; padding: 0.8rem 1rem 3rem; }
.option-group { margin-bottom: 1rem; }
.option-group h4 { margin: 0 0 0.4rem; color: var(--muted); font-size: 0.95rem; text-transform: uppercase; letter-spacing: 0.05em; }
.swatches { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.swatch { border: 3px solid transparent; border-radius: 14px; background: #fff; padding: 0.5rem 0.8rem; cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,0.08); font-weight: 700; min-height: 44px; }
.swatch.color { width: 44px; height: 44px; padding: 0; border-radius: 50%; }
.swatch.active { border-color: var(--accent); }

.cat-chips { display: none; gap: 0.4rem; overflow-x: auto; padding: 0.2rem 0 0.6rem; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
.cat-chips::-webkit-scrollbar { display: none; }
.chip { flex: 0 0 auto; border: 2px solid transparent; background: #fff; border-radius: 999px; padding: 0.55rem 1rem; font-weight: 700; cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,0.08); min-height: 44px; }
.chip.active { border-color: var(--accent); color: var(--accent); background: #f1eeff; }

@media (max-width: 900px), (orientation: portrait) {
  .builder { flex-direction: column; overflow: hidden; }
  .builder-preview { width: 100%; max-width: none; flex: 0 0 auto; flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: center; gap: 0.5rem 0.8rem; padding: 0.5rem 0.8rem; }
  .char-tabs { width: 100%; }
  .preview-stage { flex: 0 0 auto; width: 110px; min-height: 150px; height: 150px; max-height: 150px; }
  .preview-stage svg { height: 150px; }
  .builder-preview .name-input { width: auto; flex: 1 1 140px; font-size: 1.1rem; padding: 0.55rem 0.8rem; }
  .preview-actions { width: 100%; }
  .builder-options { flex: 1; min-height: 0; overflow: auto; padding: 0.6rem 0.8rem 4rem; }
  .cat-chips { display: flex; position: sticky; top: 0; background: var(--bg); z-index: 2; }
  .option-group { display: none; }
  .option-group.active { display: block; }
  .option-group h4 { display: none; }
  .swatches { gap: 0.6rem; }
  .swatch { font-size: 1.1rem; padding: 0.7rem 1.1rem; min-height: 52px; }
  .swatch.color { width: 54px; height: 54px; }
}
@media (min-width: 600px) and (max-width: 900px), (min-width: 600px) and (orientation: portrait) {
  .preview-stage { width: 170px; min-height: 230px; height: 230px; max-height: 230px; }
  .preview-stage svg { height: 230px; }
}

/* ---------------- premise */
.premise-wrap { padding: 1rem; display: flex; flex-direction: column; gap: 0.9rem; max-width: 720px; width: 100%; margin: 0 auto; align-items: center; }
.hint { color: var(--muted); margin: 0; text-align: center; }
textarea#premise-text { width: 100%; border-radius: var(--radius); border: 2px solid #ddd; padding: 0.9rem 1rem; font-size: 1.15rem; resize: vertical; }
textarea#premise-text:focus { outline: none; border-color: var(--accent); }
.idea-list { display: grid; gap: 0.6rem; width: 100%; }
.idea { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 0.9rem 1rem; cursor: pointer; border: 3px solid transparent; font-size: 1.05rem; }
.idea.active { border-color: var(--accent); }

/* ---------------- play */
#screen-play { background: #111; }
.stage { position: relative; flex: 0 0 auto; height: 46vh; min-height: 240px; overflow: hidden; background: #8fd3ff; }
.stage-bg, .stage-bg svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.stage-chars { position: absolute; left: 0; right: 0; bottom: 4%; height: 62%; display: flex; align-items: flex-end; justify-content: center; gap: 4%; padding: 0 6%; }
.stage-chars .char-slot { height: 100%; display: flex; align-items: flex-end; animation: enter 0.6s ease-out both; }
.stage-chars .char-slot svg { height: 100%; filter: drop-shadow(0 6px 4px rgba(0,0,0,0.25)); }
.stage-chars .char-slot:nth-child(2) { animation-delay: 0.15s; }
.stage-chars .char-slot:nth-child(3) { animation-delay: 0.3s; }
.stage-top { position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: center; justify-content: space-between; padding: 0.6rem 0.8rem; gap: 0.5rem; }
.story-title { flex: 1; text-align: center; font-weight: 800; color: #fff; text-shadow: 0 2px 6px rgba(0,0,0,0.5); font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.panel { flex: 1; min-height: 0; background: var(--bg); border-radius: 22px 22px 0 0; margin-top: -18px; position: relative; padding: 1rem 1.2rem 1.5rem; overflow: auto; display: flex; flex-direction: column; gap: 0.9rem; box-shadow: 0 -6px 20px rgba(0,0,0,0.25); }
.narration { font-size: var(--text-size); line-height: 1.5; background: #fff; padding: 1rem 1.2rem; border-radius: var(--radius); box-shadow: var(--shadow); animation: fadeup 0.5s ease-out; }
.narration.hidden-text { display: none; }
.voice-bar { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.choices { display: grid; gap: 0.7rem; }
.choice { background: #fff; border: 3px solid #ffd5b8; border-radius: var(--radius); padding: 0.9rem 1.1rem; font-size: 1.15rem; font-weight: 700; text-align: left; cursor: pointer; box-shadow: var(--shadow); display: flex; gap: 0.8rem; align-items: center; animation: fadeup 0.5s ease-out both; }
.choice:nth-child(2) { animation-delay: 0.1s; } .choice:nth-child(3) { animation-delay: 0.2s; }
.choice .num { flex: 0 0 38px; height: 38px; border-radius: 50%; background: var(--primary); color: #fff; display: inline-flex; align-items: center; justify-content: center; }
.choice:active { transform: scale(0.98); }
.custom { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 0.9rem 1rem; display: flex; flex-direction: column; gap: 0.6rem; }
.custom-label { font-weight: 800; color: var(--muted); }
.custom input { flex: 1; min-width: 0; border: 2px solid #ddd; border-radius: 999px; padding: 0.7rem 1rem; font-size: 1.1rem; }
.custom input:focus { outline: none; border-color: var(--accent); }
.input-row { display: flex; gap: 0.5rem; align-items: stretch; width: 100%; }
.input-row textarea { flex: 1; min-width: 0; }
.btn.mic { flex: 0 0 56px; width: 56px; padding: 0; font-size: 1.6rem; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; align-self: center; height: 56px; }
.btn.mic.listening { background: var(--danger); color: #fff; animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 0%,100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(230,57,70,0.5); } 50% { transform: scale(1.06); box-shadow: 0 0 0 12px rgba(230,57,70,0); } }
.ending { display: flex; flex-direction: column; align-items: center; gap: 0.8rem; padding: 0.5rem 0; }
.the-end { font-size: 2.4rem; font-weight: 900; color: var(--primary); }

.loading { position: absolute; inset: 0; background: rgba(20, 10, 30, 0.55); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1rem; color: #fff; font-size: 1.3rem; font-weight: 700; z-index: 20; backdrop-filter: blur(3px); }
.spinner { width: 56px; height: 56px; border: 6px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; animation: spin 0.9s linear infinite; }

@media (min-width: 900px) {
  #screen-play { flex-direction: row; }
  .stage { height: 100%; flex: 1; }
  .panel { flex: 0 0 420px; margin-top: 0; border-radius: 0; box-shadow: -6px 0 20px rgba(0,0,0,0.25); }
}

/* ---------------- modal / toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 1rem; }
.modal-card { background: #fff; border-radius: var(--radius); padding: 1.3rem; width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 0.9rem; }
.modal-card h3 { margin: 0; }
.modal-card p { margin: 0; line-height: 1.5; font-size: 1.05rem; }
.home-small { display: flex; gap: 0.5rem; flex-wrap: wrap; justify-content: center; }
.modal-card .row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; font-size: 1.05rem; }
.modal-card input[type=checkbox] { width: 26px; height: 26px; }
.modal-card select { padding: 0.4rem 0.6rem; border-radius: 10px; }
.toast { position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); background: #2b2233; color: #fff; padding: 0.8rem 1.2rem; border-radius: 999px; z-index: 60; max-width: 90vw; text-align: center; }

/* ---------------- character animation */
.char { overflow: visible; }
.char .char-body { transform-origin: 100px 300px; }
.char.act-idle .char-body { animation: idle 3s ease-in-out infinite; }
.char.act-walk .char-body { animation: walk 0.7s ease-in-out infinite; }
.char.act-jump .char-body { animation: jump 1.4s ease-in-out infinite; }
.char.act-dance .char-body { animation: dance 1.2s ease-in-out infinite; }
.char.act-sit .char-body { animation: sit 3s ease-in-out infinite; }
.char.act-point .char-body { animation: idle 3s ease-in-out infinite; }
.char.act-wave .char-body { animation: idle 3s ease-in-out infinite; }
.char .arm { transform-origin: 136px 145px; transform-box: view-box; }
.char .arm-l { transform-origin: 64px 145px; }
.char .arm-r.wave { animation: wave 0.9s ease-in-out infinite; }
.char .wings { animation: flap 1.2s ease-in-out infinite; transform-origin: 100px 160px; transform-box: view-box; }
.char .cape { animation: sway 2.4s ease-in-out infinite; transform-origin: 100px 142px; transform-box: view-box; }

@keyframes idle { 0%,100% { transform: translateY(0) scaleY(1); } 50% { transform: translateY(-4px) scaleY(1.015); } }
@keyframes walk { 0%,100% { transform: translateY(0) rotate(-3deg); } 50% { transform: translateY(-10px) rotate(3deg); } }
@keyframes jump { 0%,25%,100% { transform: translateY(0) scaleY(1); } 35% { transform: translateY(6px) scaleY(0.92); } 60% { transform: translateY(-70px) scaleY(1.05); } 85% { transform: translateY(0) scaleY(0.95); } }
@keyframes dance { 0%,100% { transform: rotate(-8deg) translateX(-6px); } 50% { transform: rotate(8deg) translateX(6px); } }
@keyframes sit { 0%,100% { transform: translateY(40px) scaleY(0.85); } 50% { transform: translateY(38px) scaleY(0.86); } }
@keyframes wave { 0%,100% { transform: rotate(-12deg); } 50% { transform: rotate(18deg); } }
@keyframes flap { 0%,100% { transform: scaleX(1); } 50% { transform: scaleX(0.85); } }
@keyframes sway { 0%,100% { transform: skewX(0deg); } 50% { transform: skewX(-6deg); } }
@keyframes enter { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fadeup { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- scene animation */
.scene .cloud { animation: drift 40s linear infinite; }
.scene .cloud:nth-of-type(2) { animation-duration: 55s; }
.scene .star { animation: twinkle 2.2s ease-in-out infinite; }
.scene .firefly { animation: twinkle 1.6s ease-in-out infinite; }
.scene .waves { animation: bob 3s ease-in-out infinite; }
.scene .sun { animation: glow 4s ease-in-out infinite; transform-origin: center; transform-box: fill-box; }
.scene .flake { animation: fall 8s linear infinite; }
.scene .crystals { animation: twinkle 3s ease-in-out infinite; }
@keyframes drift { from { transform: translateX(0); } to { transform: translateX(200px); } }
@keyframes twinkle { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }
@keyframes bob { 0%,100% { transform: translateX(0); } 50% { transform: translateX(14px); } }
@keyframes glow { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06); } }
@keyframes fall { from { transform: translateY(-40px); } to { transform: translateY(600px); } }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
