:root {
  --night0: #08161d;
  --night1: #0d2430;
  --teal: #17505d;
  --teal-soft: #2a7f88;
  --violet: #4b3a78;
  --leaf: #2f7350;
  --leaf-soft: #5aa46e;
  --coral: #ff6f61;
  --coral-soft: #ff9d8c;
  --gold: #f5b544;
  --gold-soft: #ffdd8a;
  --cream: #fff4dc;
  --ink: #161020;
  --hair: rgba(255, 244, 220, 0.14);
  --glass: rgba(10, 27, 35, 0.55);
  --glass-strong: rgba(10, 27, 35, 0.82);
  --radius: 22px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--night0);
  color: var(--cream);
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, -apple-system, sans-serif;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

#stage {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#ui button,
#ui input,
#ui .modal,
#ui .overlay,
#ui .screen {
  pointer-events: auto;
}

.hidden {
  display: none !important;
}

/* ---- screens ---- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 12px) 14px calc(env(safe-area-inset-bottom) + 12px);
}

.home-spacer {
  flex: 1;
}

#gamehud {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  pointer-events: none;
}
#gamehud button {
  pointer-events: auto;
}

/* ---- topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 48px;
}
.topbar .spacer {
  width: 44px;
}

.home-settings {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  right: 14px;
}

.preview-back {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 14px;
}

.topbar.center {
  justify-content: center;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--glass);
  border: none;
  border-radius: 999px;
  padding: 9px 16px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.2px;
  color: var(--cream);
  backdrop-filter: blur(10px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
}

.stars {
  color: var(--gold-soft);
}
.star-svg {
  width: 18px;
  height: 18px;
  fill: var(--gold);
  flex: none;
  filter: drop-shadow(0 0 4px rgba(245, 181, 68, 0.55));
}

.icon-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 14px;
  border: 1px solid var(--hair);
  background: var(--glass);
  color: var(--cream);
  display: grid;
  place-items: center;
  cursor: pointer;
  backdrop-filter: blur(10px);
  transition: background 0.15s, transform 0.12s;
}
.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
  opacity: 0.9;
}
.icon-btn:active {
  transform: scale(0.95);
  background: rgba(255, 244, 220, 0.1);
}

/* ---- buttons ---- */
.primary-btn {
  border: none;
  border-radius: 18px;
  padding: 15px 28px;
  min-height: 52px;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0.2px;
  color: #2a1a05;
  background: linear-gradient(180deg, #ffe9ad, var(--gold));
  box-shadow: 0 10px 24px rgba(245, 181, 68, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.55);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s, box-shadow 0.15s;
}
.primary-btn:active {
  transform: translateY(1px) scale(0.99);
  box-shadow: 0 5px 14px rgba(245, 181, 68, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.55);
}
.primary-btn.big {
  width: 100%;
}
.ghost-btn {
  border: 1px solid var(--hair);
  background: rgba(255, 244, 220, 0.04);
  color: var(--cream);
  border-radius: 18px;
  padding: 14px 20px;
  min-height: 50px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, transform 0.12s;
}
.ghost-btn:active {
  transform: scale(0.98);
  background: rgba(255, 244, 220, 0.09);
}
.danger-btn {
  border: 1px solid rgba(255, 111, 97, 0.4);
  background: rgba(255, 111, 97, 0.1);
  color: var(--coral-soft);
  border-radius: 18px;
  padding: 14px 20px;
  min-height: 50px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}
.restore-btn {
  border: none;
  border-radius: 18px;
  padding: 14px 22px;
  min-height: 50px;
  font-size: 15px;
  font-weight: 800;
  color: #0f2410;
  background: linear-gradient(180deg, #b6efba, #5cc07a);
  box-shadow: 0 10px 22px rgba(92, 192, 122, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  cursor: pointer;
  animation: pulse-soft 2.4s ease-in-out infinite;
  transition: transform 0.12s;
}
.restore-btn:active {
  transform: translateY(1px) scale(0.99);
}
@keyframes pulse-soft {
  0%,
  100% {
    box-shadow: 0 10px 22px rgba(92, 192, 122, 0.18), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  }
  50% {
    box-shadow: 0 10px 28px rgba(92, 192, 122, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  }
}

/* ---- home panel ---- */
.home-panel {
  background: linear-gradient(180deg, rgba(10, 26, 34, 0.6), rgba(8, 20, 27, 0.78));
  border: 1px solid rgba(255, 244, 220, 0.09);
  border-radius: 26px;
  padding: 18px 18px 16px;
  backdrop-filter: blur(16px);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.4);
}
.task-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.task-kicker {
  font-size: 10.5px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: rgba(255, 221, 138, 0.7);
  font-weight: 700;
}
.task-name {
  font-size: 19px;
  font-weight: 800;
  margin-top: 3px;
  line-height: 1.18;
  letter-spacing: -0.2px;
}
.task-cost {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13.5px;
  color: rgba(255, 221, 138, 0.9);
  margin-top: 5px;
  font-weight: 600;
}
.task-cost .star-svg {
  width: 14px;
  height: 14px;
}
.task-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}
.task-actions > * {
  flex: 1;
}
.restore-btn.hidden {
  display: none;
}

/* ---- speech ---- */
.speech {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(10, 27, 35, 0.62);
  border: 1px solid rgba(255, 244, 220, 0.08);
  border-radius: 22px;
  padding: 9px 16px 9px 9px;
  margin-bottom: 10px;
  backdrop-filter: blur(14px);
  max-width: 100%;
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.3);
}
.speech p {
  margin: 0;
  font-size: 15px;
  line-height: 1.35;
  color: rgba(255, 244, 220, 0.94);
}
.speech-bust {
  width: 58px;
  height: 58px;
  flex: none;
  border-radius: 999px;
  overflow: hidden;
  background: radial-gradient(circle at 40% 30%, rgba(90, 164, 110, 0.28), rgba(20, 50, 40, 0.4));
}
.speech-bust canvas {
  width: 100%;
  height: 100%;
  display: block;
}
.speech-bust.small {
  width: 42px;
  height: 42px;
}
.speech.inline {
  margin: 0;
  padding: 5px 14px 5px 5px;
}

/* ---- preview ---- */
.preview-card {
  margin: auto 0 0;
  width: min(420px, 100%);
  align-self: center;
  background: linear-gradient(180deg, rgba(14, 38, 48, 0.92), rgba(9, 24, 32, 0.96));
  border: 1px solid rgba(255, 244, 220, 0.1);
  border-radius: 26px;
  padding: 20px 22px 20px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(16px);
  pointer-events: auto;
}
.preview-card h2 {
  margin: 2px 0 20px;
  font-size: 27px;
  letter-spacing: -0.4px;
  color: var(--gold-soft);
}
.preview-sub {
  margin: 0 0 12px;
  font-size: 11px;
  letter-spacing: 2.2px;
  text-transform: uppercase;
  color: rgba(255, 244, 220, 0.55);
  font-weight: 700;
}
.moves-line {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  margin: 22px 0 26px;
}
.moves-num {
  font-size: 44px;
  font-weight: 900;
  letter-spacing: -1px;
  color: var(--cream);
}
.moves-label {
  font-size: 14px;
  color: rgba(255, 244, 220, 0.6);
}
#btnStart {
  width: 100%;
}

/* ---- game hud ---- */
.topbar.game {
  padding: 0 4px;
  justify-content: space-between;
}
.moves-block {
  text-align: center;
  line-height: 1;
}
.moves-big {
  font-size: 30px;
  font-weight: 900;
  letter-spacing: -0.5px;
  color: var(--gold-soft);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
}
.moves-cap {
  font-size: 9.5px;
  letter-spacing: 2.6px;
  text-transform: uppercase;
  color: rgba(255, 244, 220, 0.6);
  margin-top: 3px;
}
.goal-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.goal-row.hud {
  padding: 6px 8px 0;
}
.goal-chip {
  display: flex;
  align-items: center;
  gap: 7px;
  background: rgba(9, 25, 33, 0.5);
  border: none;
  border-radius: 999px;
  padding: 4px 13px 4px 4px;
  backdrop-filter: blur(10px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
  transition: opacity 0.25s;
}
.goal-chip canvas {
  width: 30px;
  height: 30px;
  display: block;
}
.goal-chip .goal-count {
  font-size: 15px;
  font-weight: 800;
  color: var(--cream);
  min-width: 24px;
  letter-spacing: 0.2px;
}
.goal-chip.done {
  background: rgba(46, 92, 60, 0.55);
}
.goal-chip.done .goal-count::after {
  content: " ✓";
  color: #a7ecb0;
}
.reaction {
  margin-top: auto;
  padding: 0 10px;
  display: flex;
  justify-content: flex-start;
  width: min(520px, 80%);
}

/* ---- overlays ---- */
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(4, 12, 17, 0.62);
  backdrop-filter: blur(7px);
  padding: 20px;
  z-index: 20;
}
.overlay .card {
  width: min(380px, 100%);
  background: linear-gradient(180deg, rgba(16, 42, 53, 0.96), rgba(9, 24, 32, 0.98));
  border: 1px solid rgba(255, 244, 220, 0.1);
  border-radius: 32px;
  padding: 30px 24px 24px;
  text-align: center;
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.55);
  pointer-events: auto;
}
.overlay .card h2 {
  margin: 0 0 8px;
  font-size: 27px;
  letter-spacing: -0.4px;
  color: var(--gold-soft);
}
.overlay .card p {
  margin: 0 0 20px;
  color: rgba(255, 244, 220, 0.78);
  font-size: 15px;
  line-height: 1.4;
}
.overlay .card .goal-row {
  margin-bottom: 20px;
}
.btn-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.star-pop {
  width: 92px;
  height: 92px;
  margin: 0 auto 8px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(245, 181, 68, 0.28), rgba(245, 181, 68, 0));
  animation: star-pop 0.7s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}
.star-pop svg {
  width: 64px;
  height: 64px;
  fill: var(--gold);
  filter: drop-shadow(0 0 16px rgba(245, 181, 68, 0.8));
}
.star-pop.dim svg {
  fill: #5f5f68;
  filter: none;
}
@keyframes star-pop {
  0% {
    transform: scale(0.3) rotate(-24deg);
    opacity: 0;
  }
  60% {
    transform: scale(1.1) rotate(5deg);
    opacity: 1;
  }
  100% {
    transform: scale(1) rotate(0);
  }
}
.chapter .chapter-sub {
  color: var(--gold-soft);
}
#chapterSpeech {
  text-align: left;
  margin: 8px 0 20px;
}

/* ---- modals ---- */
.modal {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(4, 12, 17, 0.66);
  backdrop-filter: blur(7px);
  padding: 18px;
  z-index: 30;
}
.modal-card {
  width: min(420px, 100%);
  max-height: 86vh;
  overflow: auto;
  background: linear-gradient(180deg, rgba(16, 42, 53, 0.97), rgba(9, 24, 32, 0.98));
  border: 1px solid rgba(255, 244, 220, 0.1);
  border-radius: 28px;
  padding: 22px;
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.55);
  pointer-events: auto;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.modal-head h3 {
  margin: 0;
  font-size: 21px;
  letter-spacing: -0.3px;
  color: var(--gold-soft);
}
.modal-sub {
  color: rgba(255, 244, 220, 0.66);
  font-size: 14px;
  line-height: 1.4;
  margin: 10px 0 14px;
}
.switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 13px 2px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 1px solid rgba(255, 244, 220, 0.06);
}
.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.switch .track {
  width: 52px;
  height: 31px;
  border-radius: 999px;
  background: rgba(255, 244, 220, 0.13);
  position: relative;
  transition: background 0.2s;
  flex: none;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: var(--cream);
  transition: transform 0.2s;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
.switch input:checked + .track {
  background: linear-gradient(180deg, #ffe9ad, var(--gold));
}
.switch input:checked + .track::after {
  transform: translateX(21px);
}
#btnReset {
  margin-top: 20px;
  width: 100%;
}
.btn-row {
  display: flex;
  gap: 10px;
}
.btn-row > * {
  flex: 1;
}
.level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.level-era {
  grid-column: 1 / -1;
  font-size: 11px;
  letter-spacing: 2.2px;
  text-transform: uppercase;
  font-weight: 800;
  color: rgba(255, 221, 138, 0.72);
  padding: 4px 2px 0;
}
.level-era:not(:first-child) {
  margin-top: 6px;
  border-top: 1px solid rgba(255, 244, 220, 0.07);
  padding-top: 12px;
}
.level-cell {
  position: relative;
  aspect-ratio: 1;
  border-radius: 20px;
  border: 1px solid rgba(255, 244, 220, 0.1);
  background: rgba(255, 244, 220, 0.04);
  color: var(--cream);
  font-weight: 800;
  font-size: 18px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s, transform 0.12s;
}
.level-cell:active {
  transform: scale(0.96);
}
.level-cell.locked {
  opacity: 0.32;
  cursor: default;
}
.level-cell.done {
  border-color: rgba(245, 181, 68, 0.5);
  background: rgba(245, 181, 68, 0.1);
}
.level-cell .lvl-star {
  position: absolute;
  bottom: 5px;
  width: 13px;
  height: 13px;
  fill: var(--gold);
}
.decor-row {
  display: flex;
  gap: 12px;
}
.decor-opt {
  flex: 1;
  border-radius: 20px;
  border: 1px solid rgba(255, 244, 220, 0.1);
  background: rgba(255, 244, 220, 0.04);
  padding: 12px 8px;
  color: var(--cream);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
  transition: background 0.15s, transform 0.12s;
}
.decor-opt:active {
  transform: scale(0.97);
}
.decor-opt .swatch {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid rgba(255, 244, 220, 0.35);
  box-shadow: inset 0 2px 6px rgba(255, 255, 255, 0.25);
}
.decor-opt.sel {
  border-color: rgba(245, 181, 68, 0.7);
  background: rgba(245, 181, 68, 0.12);
}

/* ---- toast ---- */
#toast {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  background: rgba(9, 25, 33, 0.92);
  border: 1px solid rgba(255, 244, 220, 0.12);
  color: var(--cream);
  padding: 12px 18px;
  border-radius: 16px;
  font-size: 13.5px;
  max-width: 90%;
  text-align: center;
  z-index: 40;
  pointer-events: none;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(10px);
}

/* ---- responsive ---- */
#home,
#preview {
  pointer-events: none;
}
#home .topbar,
#home .home-panel,
#home .speech,
#preview .topbar,
#preview .preview-card {
  pointer-events: auto;
}

@media (min-width: 760px) {
  .home-panel {
    width: min(560px, 60%);
    margin: 0 auto;
  }
  .speech {
    width: min(560px, 60%);
    margin-left: auto;
    margin-right: auto;
  }
  .screen {
    padding-left: 28px;
    padding-right: 28px;
  }
  .goal-row.hud,
  .topbar.game {
    width: min(680px, 80%);
    margin: 0 auto;
    padding-left: 0;
    padding-right: 0;
  }
}

@media (max-height: 640px) {
  .speech p {
    font-size: 13px;
  }
  .task-name {
    font-size: 17px;
  }
  .home-panel {
    padding: 14px;
  }
}
