:root {
  --bg: #0f1428;
  --bg2: #171f3d;
  --card: #1b2340;
  --card2: #222c52;
  --line: #2d3a68;
  --text: #eef2ff;
  --muted: #9fabd0;
  --primary: #ffd166;
  --primary-ink: #4a3800;
  --accent: #748ffc;
  --pink: #f783ac;
  --green: #8ce99a;
  --red: #ff8fa3;
  --radius: 16px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", "Sarabun", "Noto Sans Thai", sans-serif;
  background:
    radial-gradient(900px 500px at 85% -10%, rgba(116, 143, 252, .18), transparent 60%),
    radial-gradient(700px 400px at -10% 30%, rgba(247, 131, 172, .12), transparent 60%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 18px 26px;
  border-bottom: 1px solid var(--line);
  background: rgba(15, 20, 40, .7);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
  flex-wrap: wrap;
}
.brand { display: flex; gap: 14px; align-items: center; }
.logo { font-size: 40px; filter: drop-shadow(0 2px 8px rgba(255, 209, 102, .35)); }
h1 { margin: 0; font-size: 22px; letter-spacing: .2px; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.header-badges { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.badge {
  display: inline-block; padding: 4px 10px; border-radius: 999px;
  background: var(--card2); border: 1px solid var(--line);
  font-size: 12px; color: var(--muted); white-space: nowrap;
}
.badge.ok { color: var(--green); border-color: rgba(140, 233, 154, .4); }
.badge.warn { color: var(--primary); border-color: rgba(255, 209, 102, .4); }
.badge.err { color: var(--red); border-color: rgba(255, 143, 163, .4); }

main {
  display: grid; grid-template-columns: minmax(0, 7fr) minmax(0, 5fr);
  gap: 20px; padding: 22px 26px; max-width: 1400px; margin: 0 auto;
}
@media (max-width: 980px) { main { grid-template-columns: 1fr; } }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px 20px; margin-bottom: 20px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .25);
}
h2 { margin: 0 0 12px; font-size: 17px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ═══════════ โหมดเด็ก ═══════════ */

#left-col, #side-col { min-width: 0; }

.kid-step {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px 18px; margin-bottom: 16px;
  transition: opacity .3s;
}
.kid-step h2 { font-size: 19px; margin-bottom: 14px; }
.step-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 50%;
  background: linear-gradient(135deg, #ffd166, #ffb703); color: var(--primary-ink);
  font-size: 18px; font-weight: 800;
  box-shadow: 0 3px 10px rgba(255, 209, 102, .35);
}
.kid-step.locked { opacity: .45; }
.kid-step.locked .kid-grid { pointer-events: none; filter: grayscale(.6); }
.step-hint { display: none; color: var(--primary); font-size: 14px; margin: 8px 0 0; }
.kid-step.locked .step-hint { display: block; }

.kid-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 12px;
}
.kid-card {
  background: var(--bg2); border: 3px solid var(--line); border-radius: 18px;
  padding: 14px 8px 12px; text-align: center; cursor: pointer;
  transition: transform .12s ease, border-color .15s, box-shadow .15s;
  font-family: inherit; color: var(--text);
}
.kid-card:hover { transform: translateY(-4px) scale(1.03); border-color: var(--accent); }
.kid-card .em { font-size: 52px; line-height: 1; display: block; margin-bottom: 8px; }
.kid-card .nm { font-size: 16px; font-weight: 700; }
.kid-card.picked {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(255, 209, 102, .25), 0 8px 24px rgba(255, 183, 3, .25);
  animation: pop .25s ease;
}
@keyframes pop { 50% { transform: scale(1.12); } }

.kid-step[data-done="1"] h2::after { content: " ✅"; }

.kid-go {
  text-align: center; padding: 10px 0 4px;
}
.kid-go p { color: var(--muted); font-size: 16px; margin: 0 0 12px; }
#btn-kid-run {
  font-family: inherit;
  font-size: 26px; font-weight: 800; color: var(--primary-ink);
  background: linear-gradient(135deg, #ffd166, #ff9e2c);
  border: none; border-radius: 22px; padding: 18px 48px; cursor: pointer;
  box-shadow: 0 6px 28px rgba(255, 158, 44, .4);
  transition: transform .1s ease, filter .15s, opacity .2s;
}
#btn-kid-run:hover:not(:disabled) { filter: brightness(1.07); transform: scale(1.03); }
#btn-kid-run:active:not(:disabled) { transform: scale(.96); }
#btn-kid-run:disabled { opacity: .4; cursor: not-allowed; box-shadow: none; }

.kid-ai-row { margin-top: 12px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; justify-content: center; }
#btn-kid-ai { font-size: 15px; padding: 10px 18px; border-radius: 14px; }
#btn-kid-ai[disabled] { opacity: .45; cursor: not-allowed; }
.ai-scenes { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; color: var(--muted); }
.ai-scenes button {
  font-family: inherit; font-size: 14px; padding: 6px 12px; border-radius: 10px;
  background: var(--card2); color: var(--text); border: 2px solid var(--line); cursor: pointer;
}
.ai-scenes button.picked { border-color: var(--primary); color: var(--primary); font-weight: 700; }

.eta-row { display: flex; gap: 14px; align-items: baseline; font-size: 14px; color: var(--muted); margin-bottom: 10px; }
#elapsed { color: var(--primary); font-variant-numeric: tabular-nums; font-weight: 700; }

.story-preview {
  margin-top: 12px; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; max-height: 220px; overflow: auto;
}
.story-title { color: var(--primary); font-size: 14px; font-weight: 700; margin-bottom: 8px; }
.story-line { font-size: 14.5px; line-height: 1.7; color: var(--text); padding: 3px 0; }
.story-line b { color: var(--muted); font-size: 13px; margin: 0 4px 0 2px; }

.modal-overlay {
  position: fixed; inset: 0; z-index: 90; display: flex; align-items: center; justify-content: center;
  background: rgba(6, 9, 20, .72); backdrop-filter: blur(3px);
}
.modal {
  width: min(480px, calc(100vw - 40px)); background: var(--card);
  border: 1px solid var(--line); border-radius: 16px; padding: 22px 24px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, .5);
}
.modal h3 { margin: 0 0 14px; font-size: 17px; }
.modal input[type="text"] {
  width: 100%; background: var(--bg2); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 13px;
  font-family: inherit; font-size: 15px;
}
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

.proj-del {
  background: none; border: none; cursor: pointer; font-size: 14px;
  padding: 3px 6px; border-radius: 8px; opacity: .55;
}
.proj-del:hover { opacity: 1; background: rgba(255, 143, 163, .15); }

/* ปุ่มกดค้างสลับโหมด */
#btn-adult-toggle { position: relative; overflow: hidden; }
#btn-adult-toggle.holding::after {
  content: ''; position: absolute; left: 0; bottom: 0; height: 3px; width: 0%;
  background: var(--primary); animation: holdfill .9s linear forwards;
}
@keyframes holdfill { to { width: 100%; } }


.progress-fun {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px 16px; font-size: 18px; margin-bottom: 12px;
}
.fun-emoji { font-size: 34px; animation: bounce 1.2s ease infinite; display: inline-block; }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }

.progress-bar {
  height: 14px; border-radius: 999px; background: var(--bg2);
  border: 1px solid var(--line); overflow: hidden; margin-top: 12px;
}
#progress-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #f783ac, #ffd166, #8ce99a);
  background-size: 200% 100%;
  animation: shimmer 2s linear infinite;
  transition: width .6s ease;
  border-radius: 999px;
}
@keyframes shimmer { to { background-position: -200% 0; } }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 6px 14px; border-radius: 999px; font-size: 15px;
  background: var(--bg2); border: 2px solid var(--line); color: var(--muted);
}
.chip.done { color: var(--green); border-color: rgba(140, 233, 154, .45); }
.chip.work { color: var(--primary); border-color: rgba(255, 209, 102, .45); }
.chip.fail { color: var(--red); border-color: rgba(255, 143, 163, .45); }

.result-card { border-color: rgba(140, 233, 154, .5); }
#kid-video { width: 100%; border-radius: 12px; background: #000; }
.result-actions { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.result-actions .btn { flex: 1; font-size: 17px; }

.tech-details { margin-top: 12px; }
.tech-details summary { color: var(--muted); font-size: 12.5px; cursor: pointer; }
pre#log, .tech-details pre {
  background: #0a0f22; border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; font-size: 12px; line-height: 1.6;
  max-height: 240px; overflow: auto; white-space: pre-wrap; word-break: break-word;
  margin: 8px 0 0; color: #c9d4f5;
}
pre#log .t { color: #5c6a99; margin-right: 8px; }

/* ═══════════ projects / detail ═══════════ */

.projects { display: flex; flex-direction: column; gap: 10px; max-height: 420px; overflow: auto; }
.empty { color: var(--muted); font-size: 13.5px; }
.project {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; cursor: pointer; transition: border-color .15s;
}
.project:hover { border-color: var(--accent); }
.project .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.project .name { font-weight: 600; font-size: 14.5px; }
.project .meta { color: var(--muted); font-size: 12px; margin-top: 4px; display: flex; gap: 10px; flex-wrap: wrap; }

#kid-result video, .detail-final video { width: 100%; border-radius: 12px; background: #000; margin: 8px 0 4px; }

/* ═══════════ โหมดผู้ใหญ่ ═══════════ */

.adult-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }

textarea {
  width: 100%; min-height: 150px; resize: vertical;
  background: var(--bg2); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  font-family: inherit; font-size: 14.5px; line-height: 1.7;
}
textarea:focus, select:focus, input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

.story-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.btn-ghost {
  background: var(--card2); color: var(--text); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 10px; cursor: pointer; font-size: 13px; font-family: inherit;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn-ghost:hover { border-color: var(--accent); }

.opts {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px 14px; margin: 14px 0 6px;
}
.opts label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); }
select, input[type="number"] {
  background: var(--bg2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; font-family: inherit; font-size: 14px; width: 100%;
}
.hint { cursor: help; }

.opts-row2 { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.check { display: inline-flex; gap: 7px; align-items: center; font-size: 14px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.file-label { color: var(--muted); border: 1px dashed var(--line); padding: 6px 12px; border-radius: 10px; }
.file-label:hover { border-color: var(--accent); color: var(--text); }
.file-label.loaded { color: var(--green); border-style: solid; border-color: rgba(140, 233, 154, .4); }
.character-preview { max-width: 130px; max-height: 130px; border-radius: 12px; margin-top: 8px; border: 1px solid var(--line); }

/* Advanced section (collapsible) */
.advanced {
  margin: 8px 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg2);
  overflow: hidden;
}
.advanced > summary {
  padding: 9px 14px;
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s, color 0.15s;
}
.advanced > summary::-webkit-details-marker { display: none; }
.advanced > summary::before {
  content: "▶";
  font-size: 9px;
  color: var(--muted);
  transition: transform 0.2s;
}
.advanced[open] > summary::before { transform: rotate(90deg); }
.advanced > summary:hover { background: rgba(255,255,255,0.03); color: var(--text); }
.advanced-body {
  padding: 10px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid var(--line);
}
.advanced-body .hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  margin: 0;
  padding: 8px 10px;
  background: rgba(255,255,255,0.02);
  border-radius: 8px;
  border-left: 3px solid var(--accent);
}
.advanced-body .hint b { color: var(--text); }
.advanced select { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; font-family: inherit; font-size: 13px; }

.actions { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; align-items: center; }
.btn {
  border: none; border-radius: 12px; padding: 12px 22px; font-size: 15px; font-family: inherit;
  font-weight: 600; cursor: pointer; transition: transform .06s ease, filter .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: linear-gradient(135deg, #ffd166, #ffb703); color: var(--primary-ink); box-shadow: 0 4px 18px rgba(255, 209, 102, .25); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.06); }
.btn.secondary { background: var(--card2); color: var(--text); border: 1px solid var(--line); }

.plan-scenes { display: flex; flex-direction: column; gap: 8px; max-height: 320px; overflow: auto; }
.plan-scene { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; }
.plan-scene b { color: var(--primary); }
.plan-scene .nar { color: var(--muted); font-size: 13.5px; margin-top: 3px; }
.plan-scene .pr { font-size: 12.5px; color: var(--muted); margin-top: 4px; display: block; }

.detail-scenes { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin-top: 14px; }
.scene-card { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.scene-card video { width: 100%; border-radius: 8px; background: #000; margin: 6px 0; }
.scene-card .nar { font-size: 13px; line-height: 1.55; margin: 4px 0; }
.scene-card .links { display: flex; gap: 8px; flex-wrap: wrap; font-size: 12px; margin-top: 6px; }
.scene-card .links a { color: #74c0fc; text-decoration: none; }
.resume-box { background: rgba(255, 209, 102, .08); border: 1px solid rgba(255, 209, 102, .35); border-radius: 12px; padding: 12px 14px; margin: 10px 0; font-size: 13.5px; }

/* ═══════════ confetti + toast + footer ═══════════ */

#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
#confetti span {
  position: absolute; top: -4vh; width: 12px; height: 18px; border-radius: 3px;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(108vh) rotate(720deg); }
}

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #2b1230; color: #ffd6e0; border: 1px solid rgba(255,143,163,.5);
  padding: 10px 18px; border-radius: 12px; font-size: 14px; z-index: 99; box-shadow: 0 6px 24px rgba(0,0,0,.4); }
.toast.ok { background: #12301b; color: #c9f7d1; border-color: rgba(140,233,154,.5); }

footer { text-align: center; color: var(--muted); font-size: 12px; padding: 18px 20px 26px; }

/* ---------- ประตูอนุมัติฉาก (stepMode) ---------- */
#scene-gate {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
  display: grid; place-items: end center; padding: 10px 10px 14px;
  background: linear-gradient(transparent, rgba(25, 12, 48, 0.55) 34%);
}
#scene-gate[hidden] { display: none; }
.gate-card {
  width: min(560px, 96vw); background: #fff; border-radius: 22px;
  padding: 12px 14px 10px; box-shadow: 0 18px 50px rgba(40, 20, 80, 0.35);
  text-align: center; animation: gatePop 0.35s ease;
}
@keyframes gatePop { from { transform: translateY(30px); opacity: 0; } }
#gate-title { margin: 2px 0 8px; font-size: 1.15rem; color: #4a2f80; }
#gate-video { width: 100%; max-height: 44vh; border-radius: 14px; background: #000; }
.gate-actions { display: flex; gap: 10px; margin-top: 10px; }
.gate-actions button {
  flex: 1; font-size: 1.12rem; font-weight: 700; padding: 14px 8px;
  border-radius: 999px; border: 0; cursor: pointer;
}
.gate-ok { background: #2ecc71; color: #fff; }
.gate-ok:disabled, .gate-redo:disabled { opacity: 0.55; cursor: wait; }
.gate-redo { background: #ffd166; color: #6b4b00; }
.gate-error { color: #c0392b; font-size: 0.9rem; margin: 6px 0 0; }
.gate-hint { margin: 8px 0 0; font-size: 0.8rem; color: #8b7aa8; }
.gate-takes { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 8px; }
.gate-takes[hidden] { display: none; }
.gate-takes button {
  width: 44px; height: 38px; border-radius: 12px; border: 0; cursor: pointer;
  background: #efe8ff; color: #4a2f80; font-size: 1rem; font-weight: 700;
}
.gate-takes button:active { transform: scale(0.94); }
#gate-take-label { font-size: 0.95rem; font-weight: 700; color: #6b5a95; }

/* ---------- โหมดวาดเอง: ปุ่มการ์ด + modal 3 ขั้น + เทียบก่อน/หลัง ---------- */
.draw-card { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; border: 2px dashed var(--line, #b9a6e8); background: repeating-linear-gradient(45deg, #f8f4ff, #f8f4ff 12px, #f2ecff 12px, #f2ecff 24px); }
.draw-card .draw-sub { font-size: 0.68rem; color: #8b7aa8; }
#step-char h2 { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
#draw-modal { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; background: rgba(30, 15, 60, 0.6); padding: 14px; }
#draw-modal[hidden] { display: none; }
#draw-modal > .draw-panel { position: relative; width: min(560px, 96vw); max-height: 92vh; overflow: auto; background: #fff; border-radius: 24px; padding: 20px 18px; text-align: center; }
.draw-close { position: absolute; top: 10px; right: 12px; width: 38px; height: 38px; border-radius: 50%; border: 0; background: #efe8ff; color: #4a2f80; font-size: 1.1rem; cursor: pointer; }
.draw-text { color: #6b5a95; font-size: 0.95rem; line-height: 1.6; }
.draw-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 14px 0 6px; align-items: center; }
.draw-actions .btn { cursor: pointer; }
#dc-photo-preview { max-width: min(420px, 88vw); max-height: 40vh; border-radius: 14px; border: 4px solid #efe8ff; }
.draw-compare { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 10px 0; }
.draw-compare figure { margin: 0; }
.draw-compare img { width: min(200px, 40vw); border-radius: 12px; border: 3px solid #efe8ff; }
.draw-compare figcaption { font-size: 0.75rem; color: #8b7aa8; margin-top: 4px; }
.draw-arrow { font-size: 1.6rem; }
.draw-wait { font-size: 0.85rem; color: #a08cc8; margin-top: 8px; }
#dc-name { padding: 11px 14px; border-radius: 12px; border: 2px solid #efe8ff; font-family: inherit; font-size: 1rem; width: min(240px, 60vw); }
