:root {
  --scene-bg: #c8e4ff;
  --scene-board: #eff9ff;
  --scene-grid: #cae4f6;
  --scene-accent: #72b7ec;
  --scene-text: #244573;
  --scene-image: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  min-height: 100svh;
  background: linear-gradient(160deg, #ffffff70, #ffffff00), var(--scene-bg);
  color: var(--scene-text);
  font-family: ui-rounded, system-ui, sans-serif;
}
button { border: 0; font: inherit; cursor: pointer; touch-action: manipulation; }
button:focus-visible, summary:focus-visible { outline: 3px solid #e883c3; outline-offset: 4px; }
button:disabled { cursor: default; opacity: .5; }
button:not(:disabled):active { transform: translateY(2px); }
button, summary { -webkit-tap-highlight-color: transparent; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

main {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-areas: "header header" "status status" "board board" "message message" "actions catalog" "keyboard footer";
  align-items: center;
  gap: 8px 14px;
  width: min(100%, 540px);
  margin: auto;
  padding: 68px 10px 12px;
  text-align: center;
  background: var(--scene-image) center top / 100% auto no-repeat var(--scene-bg);
  border-radius: 0 0 24px 24px;
}
main > header { grid-area: header; }
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
  padding: 3px 8px;
  border-radius: 16px;
  background: #ffffffb8;
}
h1 {
  margin: 0 4px;
  color: #fff;
  font-size: clamp(29px, 7.5vw, 42px);
  line-height: 1.2;
  letter-spacing: 1px;
  text-shadow: 2px 3px #28559d, -1px -1px #28559d;
  white-space: nowrap;
}
h1 span { color: #ffdb7d; }
.small {
  display: grid;
  place-items: center;
  flex: 0 0 38px;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 13px;
  background: #ffffffa6;
  color: #477bb8;
  font-size: 23px;
}
#sound { font-size: 10px; line-height: 1.15; font-weight: 750; white-space: pre-line; }
#sound[aria-pressed="true"] { background: #d9f0ff; color: #245786; }
#sound[aria-pressed="false"] { background: #eef2f6; color: #596d83; }
.status {
  grid-area: status;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  min-width: 0;
  font-weight: 800;
}
.status span { padding: 6px 12px; border-radius: 16px; background: #ffffffe0; white-space: nowrap; }
#hearts { color: #ec759d; letter-spacing: 3px; }
#scene-name {
  position: absolute;
  top: 14px;
  right: 14px;
  max-width: calc(100% - 28px);
  padding: 4px 10px;
  overflow: hidden;
  border-radius: 10px;
  background: #ffffffdf;
  font-size: 11px;
  line-height: 1.4;
  letter-spacing: .3px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.board-wrap { grid-area: board; position: relative; min-width: 0; }
canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  background: var(--scene-board);
  border: 4px solid var(--scene-accent);
  border-radius: 23px;
  box-shadow: 0 6px #83addc, 0 10px 25px #6c8eb72b;
  touch-action: none;
}
#loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  border-radius: 23px;
  background: #eff9fff2;
  color: #28559d;
  line-height: 1.5;
}
#retry-load { padding: 11px 20px; border-radius: 14px; background: #458fdb; color: #fff; font-weight: 700; }
#message {
  grid-area: message;
  justify-self: center;
  max-width: 100%;
  margin: 5px 0 0;
  padding: 5px 12px;
  border-radius: 12px;
  background: #ffffffe0;
  font-size: 13px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}
nav { grid-area: actions; display: flex; justify-content: center; gap: 14px; }
nav button {
  display: grid;
  place-items: center;
  width: 60px;
  height: 60px;
  padding: 4px;
  border-radius: 19px;
  background: #eef9ff;
  box-shadow: 0 4px #8cb6e0;
}
nav img { width: 52px; height: 52px; object-fit: contain; }
.catalog-entry {
  grid-area: catalog;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border-radius: 16px;
  background: #ffffffe0;
  color: #315c8a;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 3px #8cb6e04d;
}
.catalog-entry span { font-size: 24px; line-height: 1; }
.roster { display: none; }
main > details { grid-area: keyboard; position: relative; min-width: 0; }
summary { display: list-item; padding: 9px 3px; color: #3e6289; font-size: 11px; cursor: pointer; }
#cat-controls {
  position: absolute;
  z-index: 5;
  bottom: calc(100% + 8px);
  left: 0;
  width: min(290px, 85vw);
  max-height: 35svh;
  overflow: auto;
  padding: 10px;
  border: 2px solid #a6cbed;
  border-radius: 16px;
  background: #f1faff;
  box-shadow: 0 8px 30px #24457333;
}
details button { margin: 3px; padding: 9px; border-radius: 9px; background: #dcecff; color: #244573; }
footer { grid-area: footer; display: flex; flex-direction: column; align-items: center; gap: 2px; color: #42678c; font-size: 10px; }
footer button { min-height: 30px; padding: 6px 12px; border-radius: 10px; background: #ffffffd9; color: #315c8a; font-size: 11px; }
footer > span { font-size: 9px; }
noscript { display: block; margin: 20px; text-align: center; }

dialog {
  width: min(92vw, 440px);
  max-height: calc(100svh - 28px);
  overflow: auto;
  padding: 24px;
  border: 2px solid #90c4ee;
  border-radius: 26px;
  background: #f2faff;
  color: #244573;
  text-align: center;
  box-shadow: 0 18px 80px #20467744;
}
dialog::backdrop { background: #26486c66; backdrop-filter: blur(4px); }
dialog h2 { margin: 0 0 12px; font-size: 23px; }
dialog button { padding: 12px 20px; border-radius: 14px; background: #458fdb; color: #fff; font-weight: 700; }
dialog .small { padding: 0; background: #dcecff; color: #315c8a; }
.celebrate { color: #f3b146; font-size: 40px; }
#win-art { width: 160px; height: 140px; object-fit: contain; }
#result-detail { line-height: 1.6; }
#level-summary { color: #5f7a98; font-size: 13px; }
#level-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; max-height: min(50svh, 430px); overflow: auto; margin: 18px 0; padding: 4px; }
#level-grid button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; min-height: 57px; padding: 7px 2px; border: 2px solid transparent; border-radius: 13px; }
#level-grid button:disabled { background: #dce7f2; color: #8299af; opacity: 1; }
#level-grid button[data-complete="true"] { background: #dcedff; color: #3267a2; }
#level-grid button[aria-current="true"] { border-color: #f0b04d; background: #fff1c9; color: #745726; }
.level-number { font-size: 15px; line-height: 1.3; }
.level-stars { min-height: 12px; color: #f1b341; font-size: 11px; line-height: 1; letter-spacing: -1px; }
#help-dialog { line-height: 1.7; }
#help-dialog ol { padding-left: 22px; text-align: left; font-size: 14px; }
#help-dialog li + li { margin-top: 10px; }
#help-dialog p { font-size: 13px; }

#catalog { width: min(94vw, 510px); padding: 18px; }
#catalog header { margin-bottom: 14px; padding: 0; background: transparent; }
#catalog h2 { margin: 0; font-size: 21px; }
#catalog-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 9px; }
.cat-card { display: flex; flex-direction: column; align-items: center; padding: 8px 4px; border: 2px solid transparent; border-radius: 17px; background: #e2effc; color: #30547e; font-size: 11px; }
.cat-card.active { border-color: #51a4e5; background: #f9fcff; }
.cat-card img { width: 65px; height: 65px; object-fit: contain; }
.cat-card small { margin-top: 4px; color: #617d9b; font-size: 9px; }
.soon-cat { display: grid; place-items: center; height: 65px; font-size: 43px; filter: grayscale(1); opacity: .3; }
#cat-preview { margin-top: 15px; padding: 15px; border-radius: 20px; background: #e9f4ff; }
.preview-stage { position: relative; width: 120px; margin: auto; }
#preview-head { width: 110px; height: 110px; object-fit: contain; }
#preview-effect { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; opacity: 0; pointer-events: none; }
#preview-effect.playing { animation: signature-pop .85s ease-out; }
#preview-name { margin: 0; }
#preview-status { margin: 6px; font-size: 12px; }
#expression-controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; margin: 12px 0; }
#expression-controls button { padding: 8px; border-radius: 10px; background: #d6e7fa; color: #315c8a; font-size: 11px; }
#expression-controls button[aria-pressed="true"] { background: #4b98db; color: #fff; }
#choose-cat, #play-effect { padding: 11px 14px; font-size: 13px; }
#choose-cat:disabled { background: #bccddd; color: #55728e; opacity: 1; }
#play-effect { background: #8777ce; }
.team-note { color: #64809e; font-size: 11px; }
@keyframes signature-pop { 0% { opacity: 0; transform: scale(.5); } 30% { opacity: 1; transform: scale(1.15); } 100% { opacity: 0; transform: scale(1.65); } }

@media (min-width: 800px) {
  body { background: url('assets/backgrounds/desktop-blue-playground.webp') center / cover fixed no-repeat #d6edff; }
  main {
    --board-size: clamp(330px, calc(100svh - 225px), 520px);
    grid-template-columns: var(--board-size) 150px;
    grid-template-areas: "header header" "status scene" "board message" "board actions" "board catalog" "board roster" "board keyboard" "board footer";
    grid-template-rows: 50px 34px repeat(6, auto);
    gap: 10px 16px;
    width: calc(var(--board-size) + 202px);
    margin: 10px auto;
    padding: 78px 16px 12px;
    border: 1px solid #ffffffb3;
    border-radius: 25px;
    box-shadow: 0 10px 40px #537da02e;
  }
  .board-wrap { width: var(--board-size); align-self: start; }
  #scene-name { grid-area: scene; position: static; max-width: 100%; margin: 0; padding: 6px 8px; font-size: 10px; }
  #message { margin: 0; width: 100%; }
  nav { gap: 12px; }
  nav button { width: 64px; height: 64px; }
  nav img { width: 56px; height: 56px; }
  .catalog-entry { padding: 10px; }
  .roster { grid-area: roster; display: flex; justify-content: center; flex-wrap: wrap; gap: 2px; align-content: center; padding: 5px; border: 1px solid #fff; border-radius: 16px; background: #ffffffad; }
  .roster img { width: 40px; height: 40px; object-fit: contain; }
  #cat-controls { right: 0; left: auto; }
  footer { padding-bottom: 4px; }
}
@media (max-width: 380px) {
  main { padding: 44px 8px 8px; gap: 8px 10px; }
  header { padding: 3px 5px; }
  h1 { font-size: 29px; }
  .small { width: 36px; height: 36px; flex-basis: 36px; }
  .status { font-size: 13px; }
  .status span { padding: 6px 9px; }
  #scene-name { top: 8px; right: 10px; font-size: 10px; }
  nav { gap: 10px; }
  nav button { width: 56px; height: 56px; }
  nav img { width: 48px; height: 48px; }
  .catalog-entry { padding: 9px; font-size: 12px; }
  #message { padding: 4px 10px; font-size: 12px; }
  footer > span { display: none; }
  dialog { padding: 18px; }
  #catalog { padding: 12px; }
  #catalog-grid { gap: 6px; }
  #level-grid { gap: 5px; padding: 3px; }
  .level-stars { font-size: 10px; }
}
@media (max-height: 760px) {
  #catalog .cat-card img { width: 48px; height: 48px; }
  #catalog .soon-cat { height: 48px; font-size: 34px; }
  #catalog .cat-card { padding: 5px 3px; }
  #catalog #preview-head { width: 75px; height: 75px; }
  #catalog #cat-preview { padding: 9px; margin-top: 10px; }
  #catalog #expression-controls { margin: 8px 0; }
  #catalog .team-note { margin: 8px 0 0; }
}
@media (prefers-reduced-motion: reduce) {
  dialog::backdrop { backdrop-filter: none; }
  #preview-effect.playing { animation: signature-fade 1s; }
  @keyframes signature-fade { 0%, 100% { opacity: 0; } 20%, 80% { opacity: 1; } }
}

/* Secondary player tools stay behind one compact entry. */
.footer-actions { display:flex; flex-wrap:wrap; justify-content:center; gap:3px; }
#save-status { display:block; max-width:100%; padding:3px; line-height:1.4; font-size:10px; }
#save-status[data-state="error"] { color:#8c2643; background:#fff0f4; border-radius:6px; }
#save-dialog { text-align:left; line-height:1.6; font-size:14px; }
#save-dialog h2 { text-align:center; }
#save-dialog button, #result-share button { padding:9px 12px; font-size:13px; }
#close-save { display:block; margin:18px auto 0; }
.quiet-actions { display:flex; flex-wrap:wrap; justify-content:center; gap:8px; }
.quiet-actions button { background:#dceeff; color:#24568c; }
.tool-section { border-top:1px solid #c5dcee; margin:16px 0; padding:4px 0; }
.tool-section summary { font-size:14px; font-weight:750; padding:10px 0; }
.tool-note, #share-status { font-size:12px; color:#496987; }
#save-feedback { color:#31597a; font-size:13px; }
.setting { display:flex; align-items:center; gap:10px; min-height:44px; }
.setting input { width:20px; height:20px; }
.file-label { display:flex; flex-direction:column; gap:8px; margin:12px 0; }
.file-label input { max-width:100%; font:inherit; }
#milestones { list-style:none; padding:0; }
#milestones li { padding:8px 0; }
#milestones strong, #milestones span { display:block; }
#milestones span { font-size:12px; }
#diagnostics-preview, #share-fallback { width:100%; min-height:140px; resize:vertical; border:1px solid #a6c9e7; border-radius:8px; padding:8px; font:12px ui-monospace,monospace; background:#fff; color:#244573; }
#result-share { margin-top:22px; border-top:1px solid #c5dcee; padding-top:10px; }
.share-label { font-size:13px; }
#save-dialog a { color:#2462a5; }
html[data-motion="reduced"] #preview-effect.playing { animation:none; }
#module-failure { position:relative; padding:20px; text-align:center; background:#fff2f5; color:#733049; }
