:root {
  color-scheme: dark;
  font-family: "Trebuchet MS", system-ui, sans-serif;
  color: #fff7fe;
  background: #291632;
  --gold: #ffe6b3;
  --rim: #ffd7f39c;
}

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

body {
  --outer: clamp(8px, 1.7vw, 24px);
  margin: 0;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: max(var(--outer), env(safe-area-inset-top))
           max(var(--outer), env(safe-area-inset-right))
           max(var(--outer), env(safe-area-inset-bottom))
           max(var(--outer), env(safe-area-inset-left));
  background: radial-gradient(ellipse at 32% 0, #784878, #291632 72%);
}

button {
  font: inherit;
  font-weight: 750;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:disabled { cursor: default; }
button:focus-visible { outline: 2px solid #fff8db; outline-offset: 3px; }
h1:focus { outline: none; }

.studio {
  position: relative;
  isolation: isolate;
  width: min(100%, 1520px);
  height: min(850px, calc(100svh - max(var(--outer), env(safe-area-inset-top)) - max(var(--outer), env(safe-area-inset-bottom))));
  min-height: 348px;
  display: grid;
  grid-template-columns: minmax(0, 17fr) minmax(0, 39fr) minmax(0, 44fr);
  grid-template-areas: "rail scene panel";
  gap: clamp(6px, 1vw, 15px);
  padding: clamp(9px, 1.45vw, 22px);
  overflow: hidden;
  border: 1px solid #fbd2dfaa;
  border-radius: clamp(16px, 2.5vw, 34px);
  background: #794c86 var(--selected-background, none) center / cover no-repeat;
  background-image: var(--selected-background, none), var(--selected-thumbnail, none), url("./assets/processed/room.webp");
  box-shadow: 0 20px 60px #18091db8, inset 0 0 78px #5124516b;
}

.studio::before {
  position: absolute;
  z-index: -1;
  inset: 0;
  content: "";
  background: linear-gradient(90deg, #34153d48, transparent 34% 52%, #3d224347);
  pointer-events: none;
}

.rail {
  grid-area: rail;
  min-width: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: space-between;
  gap: clamp(8px, 1.6svh, 16px);
  padding: clamp(5px, 1.35svh, 15px) 0 0;
}

.wordmark {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 0;
  padding: clamp(9px, 1.8svh, 20px) 4px;
  border: 1px solid #ffe1a6;
  border-radius: 44% / 28%;
  background: radial-gradient(ellipse at 50% 72%, #60316c, #3d224cf0 84%);
  box-shadow: 0 0 15px #ffbe857d, inset 0 0 11px #f9c0d382;
  text-align: center;
  text-shadow: 0 1px 7px #e879bd8a;
  font: italic 600 clamp(15px, 2.05vw, 34px)/1.05 Georgia, serif;
}

.wordmark img {
  width: clamp(22px, 4.5vw, 64px);
  height: clamp(16px, 4.5svh, 46px);
  margin-bottom: 3px;
  object-fit: contain;
}

.wordmark em { color: #fff0fa; font-size: .77em; }

#categories { display: grid; gap: clamp(5px, 1.15svh, 11px); margin: auto 0; }

.category, .round-action {
  border: 1px solid #efc0f4be;
  background: linear-gradient(174deg, #8e5c9ace 0%, #5b376dda 54%, #4d2e61f0 100%);
  box-shadow: inset 0 1px 6px #ffe3fc8c, 0 3px 9px #3c1a4b70;
}

.category {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: clamp(4px, .7vw, 12px);
  padding: 4px clamp(6px, .95vw, 16px);
  min-height: clamp(32px, 8.1svh, 75px);
  border-radius: 999px;
  text-align: left;
  font-size: clamp(.66rem, 1.45vw, 1.4rem);
  text-shadow: 0 2px 3px #2c1436;
}

.category img {
  width: clamp(18px, 3.3vw, 48px);
  height: clamp(19px, 5.2svh, 48px);
  flex: none;
  object-fit: contain;
  filter: drop-shadow(0 1px 2px #24112a);
}

.category[aria-pressed="true"] {
  border-color: #ffe1b5;
  background: linear-gradient(165deg, #dcadf3, #a463c0 53%, #9251ad);
  box-shadow: inset 0 1px 9px #ffecfdad, 0 0 10px #ffd2b999;
}

.rail-actions { display: flex; gap: clamp(6px, 1vw, 13px); }

.round-action {
  width: clamp(37px, 5.4vw, 65px);
  height: clamp(37px, 5.4vw, 65px);
  padding: 0;
  border-radius: 50%;
  font-size: clamp(1.25rem, 3vw, 2.2rem);
  line-height: 1;
  text-shadow: 0 1px 4px #34143d;
}

.round-action:hover, .category:hover { filter: brightness(1.12); }

.sound-action {
  position: absolute;
  z-index: 5;
  inset-block-start: 0;
  inset-inline-end: 0;
  width: clamp(44px, 5vw, 54px);
  height: clamp(44px, 5vw, 54px);
  font-size: clamp(1.4rem, 2.6vw, 1.8rem);
  color: var(--gold);
}

.sound-dialog {
  position: fixed;
  inset-inline-start: auto;
  inset-inline-end: max(18px, env(safe-area-inset-right));
  width: min(300px, calc(100% - 36px));
  padding: 18px;
  border: 1px solid var(--gold);
  border-radius: 22px;
  background: linear-gradient(150deg, #683d75, #41254e);
  color: inherit;
  box-shadow: 0 8px 38px #21102bc7;
}

.sound-dialog[open] { display: grid; gap: 10px; }
.sound-dialog::backdrop { background: #27153166; }
.sound-dialog form { display: grid; }
.sound-dialog .primary { min-height: 44px; font-size: 1rem; }

.sound-toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 48px;
  padding: 7px 11px;
  border: 1px solid var(--rim);
  border-radius: 12px;
  background: #382044;
  text-align: left;
}

.sound-toggle strong { min-width: 3.5ch; text-align: right; color: #f1cae5; }
.sound-toggle[aria-checked="true"] strong { color: var(--gold); }

.scene {
  --stage-bottom: 3.5%;
  --stage-height: clamp(40px, 15%, 124px);
  grid-area: scene;
  position: relative;
  min-width: 0;
  min-height: 0;
  isolation: isolate;
  container-type: inline-size;
}

.stage {
  position: absolute;
  width: 100%;
  height: var(--stage-height);
  bottom: var(--stage-bottom);
  left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
}

.actor {
  --character-height: min(88%, 141cqw);
  position: absolute;
  z-index: 2;
  left: 50%;
  /* Preserve the V0.2 foot anchor at y=1005 on the shared 1020px canvas. */
  bottom: calc(var(--stage-bottom) + var(--stage-height) * .57 - var(--character-height) * .014706);
  height: var(--character-height);
  max-width: 94%;
  aspect-ratio: 680 / 1020;
  transform: translateX(-50%);
  transform-origin: 50% 98.53%;
  pointer-events: none;
}

.actor img { position: absolute; inset: 0; width: 100%; height: 100%; user-select: none; }

.actor.drop-target::after {
  position: absolute;
  inset: 2% 3%;
  border: 2px solid #fff0c1b3;
  border-radius: 46%;
  box-shadow: 0 0 22px #ffdda689, inset 0 0 15px #ffe1b54d;
  content: "";
}

.panel { grid-area: panel; min-width: 0; }

.panel:not(#dress) {
  align-self: center;
  display: grid;
  place-items: center;
  padding: clamp(16px, 3vw, 42px);
}

.welcome-content {
  width: min(100%, 504px);
  display: grid;
  justify-items: center;
  gap: clamp(6px, 2svh, 20px);
  text-align: center;
  padding: clamp(18px, 4.9svh, 50px) 12px;
  border: 1px solid var(--rim);
  border-radius: 26px;
  background: radial-gradient(ellipse at 50% 0, #9d568aaa, #422650ec 72%);
  box-shadow: 0 8px 30px #31163a72, inset 0 0 13px #f0b1f067;
}

.ornament {
  width: 38%;
  display: flex;
  align-items: center;
  gap: .55rem;
  color: var(--gold);
  font-size: clamp(16px, 3svh, 30px);
  text-shadow: 0 0 14px #ffccacaa;
}

.ornament span { flex: 1; height: 1px; background: var(--rim); }

h1 {
  font: 700 clamp(25px, 4.7vw, 76px)/.99 Georgia, serif;
  letter-spacing: -.04em;
  text-wrap: balance;
}

h1 em { color: #ffcbe9; font-weight: 400; }
#welcome-title { font-size: clamp(25px, 4.1vw, 66px); }

.primary {
  min-height: clamp(43px, 8.2svh, 75px);
  min-width: 0;
  max-width: 100%;
  padding: .28em .8em;
  border: 1px solid #ffe5f2;
  border-radius: 999px;
  background: linear-gradient(#fbc4e5 0%, #e159a4 31%, #b83078 55%, #d54c92 83%, #f197cc 100%);
  box-shadow: 0 0 0 2px #d962a87d, inset 0 2px 7px #fff5fcce, 0 4px 12px #4b1a456b;
  color: #fffafd;
  text-shadow: 0 2px 4px #762452;
  font-size: clamp(.9rem, 2vw, 1.8rem);
  white-space: nowrap;
}

.primary:enabled:hover { filter: brightness(1.12); }
.primary:enabled:active { transform: translateY(1px); }
.primary:disabled { opacity: .6; }
.primary span { color: #fff6fc; }
.welcome-content .primary { min-width: min(160px,100%); margin-top: 4px; }
.quiet { min-height: 36px; border: 0; padding: .25em .75em; background: transparent; color: #ffebf8; }
.quiet:hover { text-decoration: underline; }

#dress {
  display: grid;
  grid-template-rows: minmax(0,1fr) auto;
  gap: clamp(7px, 1.45svh, 16px);
  padding: clamp(5px, 2.7svh, 25px) 0 0;
}

.options { display: grid; grid-template-rows: repeat(4,minmax(0,1fr)); gap: clamp(4px, .9svh, 9px); min-width: 0; }

.options[data-category="background"] { grid-template-rows: 1fr; }

.shelf {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: clamp(2px, .5svh, 6px);
  padding: clamp(4px, .9svh, 10px) clamp(6px, 1.15vw, 17px);
  border: 1px solid #f8cfedbb;
  border-bottom-color: #ffe1a3de;
  border-radius: clamp(11px, 2vw, 25px);
  background: linear-gradient(160deg, #563662ec, #62396cdd 58%, #603552eb);
  box-shadow: inset 0 0 10px #e6acd773, 0 2px 9px #3817336b;
}

.shelf[data-active="true"] {
  border-color: #ffe6bd;
  box-shadow: 0 0 10px #f9c298a0, inset 0 0 12px #eab0eb78;
}

.shelf h2 {
  font-size: clamp(.76rem, 1.55vw, 1.55rem);
  line-height: 1;
  text-shadow: 0 2px 4px #2e1737;
}

.background-shelf { align-self: start; grid-template-rows: auto auto; }

.background-choices {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(5px, 1vw, 11px);
}

.background-choice {
  min-width: 0;
  min-height: 44px;
  display: grid;
  align-content: start;
  gap: 5px;
  padding: 5px;
  border: 1px solid var(--rim);
  border-radius: 12px;
  background: #381e47;
  font-size: clamp(.64rem, 1.15vw, 1rem);
}

.background-choice img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 7px; }

.background-choice[aria-pressed="true"] {
  border-color: var(--gold);
  background: #855493;
  box-shadow: 0 0 9px #ffce80a6;
}

.items { display: grid; grid-template-columns: repeat(5,minmax(0,1fr)); gap: clamp(3px, .66vw, 9px); }

.item, .empty-slot {
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 1px solid #f1c4ed54;
  border-radius: clamp(7px, 1vw, 16px);
  background: #381e4766;
}

.empty-slot { border-color: #e0b0db29; background: #44254e43; }

.item {
  position: relative;
  display: grid;
  place-items: center;
  touch-action: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.item img { position: absolute; width: 87%; height: 88%; object-fit: contain; pointer-events: none; }
.item:enabled { cursor: grab; }
.item:enabled:hover { background-color: #753d86; }
.item[data-dragging] { cursor: grabbing; }
.item:disabled img { opacity: .42; }
.item:disabled::after {
  position: absolute;
  right: 3px;
  bottom: 3px;
  font-size: clamp(.6rem, 1.25vw, .95rem);
  content: "🔒";
}
.item[aria-pressed="true"] {
  border-color: #fff0bf;
  background: radial-gradient(ellipse at 50% 80%, #ad66a8, #6d4386);
  box-shadow: 0 0 9px #ffce80d9, inset 0 0 8px #ffd995b8;
}
.item[aria-pressed="true"]::after {
  position: absolute;
  bottom: 2px;
  right: 2px;
  display: grid;
  place-items: center;
  width: clamp(13px, 1.8vw, 24px);
  aspect-ratio: 1;
  border: 1px solid #ffe7ae;
  border-radius: 50%;
  color: #fff;
  background: linear-gradient(120deg, #ffd27c, #c38329);
  font-size: clamp(.54rem, 1.2vw, .92rem);
  content: "✓";
}

.play-action { justify-self: end; width: min(55%, 260px); }

.drag-preview {
  position: fixed;
  z-index: 10;
  pointer-events: none;
  width: clamp(56px, 11vw, 120px);
  height: clamp(56px, 11vw, 120px);
  object-fit: contain;
  filter: drop-shadow(0 4px 7px #35162c) drop-shadow(0 0 8px #ffe5b8);
  transform: translate(-50%, -50%);
}

.dance-stage, .spotlight, .show-party { display: none; }

.studio[data-screen="play"] { background: #3d2352; }

[data-screen="play"] .scene {
  overflow: hidden;
  border-radius: clamp(12px, 2vw, 28px);
  background: #552b70;
}

[data-screen="play"] .stage { display: none; }
[data-screen="play"] .dance-stage { display: block; }

.dance-stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 74%;
  pointer-events: none;
}

[data-screen="play"] .actor {
  --character-height: min(68%, 126cqw);
  /* Stand at the stage image's 74% anchor at any aspect ratio. */
  bottom: calc(26% - var(--character-height) * .014706);
  filter: drop-shadow(0 4px 5px #411b7859);
}

[data-screen="play"] .sound-action {
  inset-block-start: clamp(9px, 1.5vw, 22px);
  inset-inline-end: clamp(9px, 1.5vw, 22px);
}

#play .welcome-content { padding-block: clamp(10px, 2.4svh, 28px); }
#play-title { font-size: clamp(20px, 3vw, 48px); }

.spotlight {
  position: absolute;
  z-index: 1;
  inset: -10% 11% 10%;
  background: radial-gradient(ellipse at 50% 45%, #fff4db55, transparent 62%);
  pointer-events: none;
}

.scene[data-show-phase] .spotlight { display: block; opacity: .55; }
.scene[data-show-phase]:not([data-show-phase="complete"]) .spotlight { animation: spotlight-sweep 4s ease-in-out infinite; }
[data-show-phase="dance"] .actor { animation: dance-step 2s ease-in-out 4; }
[data-show-phase="finale"] .show-party { display: block; }

.show-party { position: absolute; z-index: 3; inset: 0; pointer-events: none; }
.show-party i {
  position: absolute;
  top: var(--top);
  left: var(--left);
  color: #ffe6a3;
  text-shadow: 0 0 5px #f5a0eb99;
  font: clamp(15px, 2.2vw, 28px)/1 Georgia, serif;
  animation: confetti-drift 1.75s var(--delay, 0s) ease-out both;
}
.show-party i:empty { width: 7px; height: 11px; border-radius: 2px; background: currentColor; }
.show-party i:nth-child(4n), .show-party i:nth-child(4n + 1) { color: #ffacd9; }
.show-party i:nth-child(1) { --left: 24%; --top: 22%; }
.show-party i:nth-child(2) { --left: 29%; --top: 34%; --delay: .14s; }
.show-party i:nth-child(3) { --left: 35%; --top: 16%; --delay: .08s; }
.show-party i:nth-child(4) { --left: 34%; --top: 48%; }
.show-party i:nth-child(5) { --left: 43%; --top: 15%; --delay: .18s; }
.show-party i:nth-child(6) { --left: 39%; --top: 31%; }
.show-party i:nth-child(7) { --left: 60%; --top: 29%; --delay: .22s; }
.show-party i:nth-child(8) { --left: 57%; --top: 16%; }
.show-party i:nth-child(9) { --left: 68%; --top: 22%; --delay: .05s; }
.show-party i:nth-child(10) { --left: 65%; --top: 48%; --delay: .17s; }
.show-party i:nth-child(11) { --left: 74%; --top: 34%; }
.show-party i:nth-child(12) { --left: 78%; --top: 14%; --delay: .10s; }

@keyframes spotlight-sweep {
  0%, 100% { opacity: .55; transform: translateX(-11%); }
  50% { opacity: .78; transform: translateX(11%); }
}
@keyframes dance-step {
  0%, 50%, 100% { transform: translateX(-50%); }
  25% { transform: translate(-60%, -.7%) rotate(-1.2deg); }
  75% { transform: translate(-40%, -.7%) rotate(1.2deg); }
}
@keyframes confetti-drift {
  0% { opacity: 0; transform: translate(-4px, -12px) rotate(-18deg); }
  28% { opacity: .95; }
  74% { opacity: .8; }
  100% { opacity: 0; transform: translate(9px, clamp(35px, 15svh, 120px)) rotate(47deg); }
}

@media (orientation: landscape) {
  [data-screen="play"] .scene { position: absolute; inset: 0; grid-area: auto; }
  .panel#play { z-index: 3; align-self: end; justify-items: end; padding: 0; }
  #play .welcome-content { width: min(100%, clamp(150px, 23vw, 310px)); }
}

.sr-only { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; }

@media (orientation: landscape) and (max-height: 500px) {
  body { --outer: 6px; }
  .studio { grid-template-columns: minmax(0,18fr) minmax(0,40fr) minmax(0,42fr); padding: 8px; gap: 7px; }
  .rail { gap: 5px; padding-top: 2px; }
  .wordmark { padding-block: 5px; }
  #categories { gap: 4px; }
  .category { min-height: 32px; }
  #dress { padding-top: 2px; gap: 6px; }
  .shelf { padding-block: 3px; }
  .welcome-content { padding-block: 16px; }
}

@media (orientation: landscape) and (max-height: 440px) {
  #categories { grid-template-columns: minmax(0,.78fr) minmax(0,1.22fr); }
  .category { grid-column: 1 / -1; }
  .category[aria-controls="actor"], .category[aria-controls="shelf-background"] {
    grid-column: auto;
    grid-row: 1;
    justify-content: center;
    padding-inline: 2px;
  }
  .category[aria-controls="actor"] img { width: 18px; height: 18px; }
  .category[aria-controls="shelf-background"] { font-size: clamp(.6rem, 1.35vw, .8rem); }
  .category[aria-controls="shelf-background"] img { display: none; }
}

@media (orientation: portrait) {
  .studio { height: auto; min-height: 0; grid-template: auto auto auto / minmax(0,1fr); grid-template-areas: "rail" "scene" "panel"; padding: 11px; }
  .rail { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 0; }
  .wordmark { grid-column: 1; font-size: 19px; padding: 5px; }
  .rail-actions { grid-column: 3; grid-row: 1; }
  .done-action { grid-column: 2; grid-row: 1; }
  #categories { grid-column: 1 / -1; grid-row: 2; grid-template-columns: repeat(6,minmax(0,1fr)); }
  .category { display: grid; justify-items: center; min-height: 46px; gap: 2px; padding: 3px 0; text-align: center; font-size: clamp(.57rem, 2.35vw, .9rem); }
  .scene { height: min(58svh, 650px); min-height: 285px; }
  .stage { width: min(100%, 500px); }
  #dress { grid-template-rows: auto auto; padding: 0; }
  .options { grid-auto-rows: minmax(76px,auto); grid-template-rows: repeat(4,minmax(76px,auto)); }
  .panel:not(#dress) { padding: 0; }
  .sound-dialog { inset-inline: 0; margin: auto; }
}

@media (orientation: portrait) and (max-width: 380px) {
  #categories { grid-template-columns: repeat(3,minmax(0,1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  [data-show-phase="dance"] .actor,
  .scene[data-show-phase]:not([data-show-phase="complete"]) .spotlight,
  .show-party i { animation: none; }
}
