* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #0d0b1a; color: #cfe0f0; font-family: ui-monospace, Menlo, Consolas, monospace; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; }
html:has(body.title), body.title {
  height: auto; min-height: 100%; min-height: 100dvh;
  overflow-x: hidden; overflow-y: auto; touch-action: pan-y;
}
main { height: 100%; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }
body.title main {
  height: auto; min-height: 100dvh; justify-content: flex-start;
  padding: max(8px, env(safe-area-inset-top, 0px)) max(10px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
}
canvas { image-rendering: pixelated; image-rendering: crisp-edges; background: #000; box-shadow: 0 0 0 3px #2a2350, 0 8px 40px rgba(0,0,0,.6); max-width: 100%; }
#bar {
  display: flex; flex-wrap: wrap; gap: 6px 8px; align-items: center; justify-content: center;
  font-size: 11px; max-width: 100%; padding: 0 4px;
}
#bar button, #bar a {
  background: #1b1740; color: #cfe0f0; border: 1px solid #3b3480; padding: 4px 10px;
  font: inherit; cursor: pointer; text-decoration: none; letter-spacing: .05em; flex: 0 1 auto;
}
#bar button:hover, #bar a:hover { background: #2a2360; }
body.title #bar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  background: #0d0b1a; padding: 6px 4px; width: 100%;
}
#touch { display: none; position: fixed; left: 0; right: 0; bottom: 0; padding: 8px 14px max(8px, env(safe-area-inset-bottom, 0px)); justify-content: space-between; align-items: flex-end; pointer-events: none; }
body.touch #touch { display: flex; }
body.touch.title #touch, body.title #touch { display: none; }
body.touch #touch button { pointer-events: auto; }
body.touch #bar { display: none; }
body.touch.portrait #bar, body.touch.title #bar { display: flex; }
body.touch.portrait:not(.title) main { padding-bottom: calc(140px + env(safe-area-inset-bottom, 0px)); }
.pad { display: flex; gap: 14px; }
#touch button { width: 72px; height: 72px; opacity: .8; border-radius: 50%; border: 3px solid #5b52b8; background: rgba(40,34,100,.65); color: #fff; font: bold 24px ui-monospace, monospace; touch-action: none; }
#touch button.on { background: rgba(90,80,220,.9); }
#touch .jump { width: 88px; height: 88px; font-size: 18px; border-color: #e0a020; background: rgba(120,70,10,.65); }
#touch .jump.on { background: rgba(230,150,30,.9); }

#stage { position: relative; line-height: 0; }

#titlePanel {
  display: flex; flex-direction: column; gap: 10px;
  width: min(720px, 100%); max-width: 100%;
  line-height: 1.3; color: #dfe8f5;
  font: 12px/1.35 ui-monospace, Menlo, Consolas, monospace;
  padding: 0 0 8px;
}
#titlePanel[hidden] { display: none; }
#titlePanel > * { pointer-events: auto; touch-action: auto; max-width: 100%; }

#loadedLevel, #levelBrowser, #titleGen {
  background: rgba(16, 12, 40, 0.94);
  border: 2px solid #5b52b8;
  padding: 7px 9px 8px;
  box-shadow: 0 6px 18px rgba(0,0,0,.4);
}
#loadedLevel { border-color: #4ade80; }
#loadedName { color: #4ade80; font-weight: bold; font-size: 13px; }
#loadedPrompt { color: #cfe0f0; margin-top: 2px; font-style: italic; }
#loadedFork { color: #bfeaff; margin-top: 2px; font-size: 11px; }
.loadedBtns { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.loadedBtns button, .titleExtras button, #btnMoreLevels, #shareOverlay button {
  font: bold 11px ui-monospace, Menlo, Consolas, monospace;
  color: #fff; background: rgba(40,34,100,.95); border: 2px solid #8a7cff;
  padding: 6px 10px; cursor: pointer;
}
button.primary, #genGo, #btnPlayLoaded, #btnShareClear {
  background: #6a3aa8; border-color: #ffd23a;
}
button.quiet { border-color: #3b3480; background: #1b1740; font-weight: normal; color: #9fb0c8; }
.loadedBtns button:hover, .titleExtras button:hover, #genGo:hover, #shareOverlay button:hover { filter: brightness(1.2); }

#levelBrowser { min-height: 0; display: flex; flex-direction: column; gap: 6px; }
#loadedLevel, #titleGen, .titleExtras { flex: 0 0 auto; }
.browserHead { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.browserHead h3 { margin: 0; font-size: 12px; color: #ffd23a; letter-spacing: .04em; }
.sorts { display: flex; gap: 4px; }
.sorts button { font: bold 10px ui-monospace, Menlo, Consolas, monospace; color: #cfe0f0; background: #1b1740; border: 1px solid #3b3480; padding: 3px 8px; cursor: pointer; }
.sorts button.on { background: #6a3aa8; border-color: #ffd23a; color: #fff; }
.cards {
  display: flex; gap: 8px; overflow-x: auto; overflow-y: hidden;
  padding: 2px 0 4px; touch-action: pan-x; -webkit-overflow-scrolling: touch;
  scrollbar-color: #5b52b8 transparent;
}
.cards::-webkit-scrollbar { height: 6px; }
.cards::-webkit-scrollbar-thumb { background: #5b52b8; }
.card {
  flex: 0 0 148px; width: 148px; background: #0b0920; border: 1px solid #3b3480;
  padding: 4px; cursor: pointer; text-align: left; color: inherit; font: inherit;
}
.card:hover, .card:focus { border-color: #ffd23a; }
.card canvas { display: block; width: 100%; height: 36px; image-rendering: pixelated; background: #102; }
.card .cname { display: block; margin-top: 3px; font-weight: bold; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .cmeta { display: block; font-size: 10px; color: #9fb0c8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#btnMoreLevels { align-self: center; padding: 3px 10px; }
#browserEmpty { margin: 0; font-size: 11px; }

#titleGen { border-color: #ffd23a; }
#titleGen label[for="genPrompt"] {
  display: block; font-weight: bold; font-size: 13px; color: #ffd23a; letter-spacing: .04em; margin-bottom: 5px;
}
#genPrompt, #genDiff, dialog input, dialog select, dialog textarea {
  display: block; width: 100%; background: #0b0920; color: #fff; border: 1px solid #8a7cff;
  padding: 7px 8px; font: 13px ui-monospace, Menlo, Consolas, monospace;
  user-select: text; -webkit-user-select: text; touch-action: auto;
}
#genDiff { width: auto; border-color: #3b3480; min-width: 96px; }
.genRow { display: flex; gap: 8px; margin-top: 6px; align-items: stretch; }
#genGo { flex: 1; font: bold 13px ui-monospace, Menlo, Consolas, monospace; padding: 7px 12px; cursor: pointer; border: 2px solid #ffd23a; color: #fff; }
#genGo:disabled { opacity: .55; cursor: wait; }
#genRemain, #shareMsg, .muted { color: #9fb0c8; margin: 5px 0 0; font-size: 11px; }
#genMsg, #shareMsg { white-space: pre-wrap; word-break: break-word; }
#genMsg .err, #lvlMsg .err { color: #ff8a8a; }
#genMsg .ok, #lvlMsg .ok { color: #7dffa8; }
#genMsg .spin::after { content: ''; display: inline-block; width: 1em; text-align: left; animation: dots 1.2s steps(4) infinite; }

.titleExtras { display: flex; gap: 8px; justify-content: center; align-items: center; }

#shareOverlay { position: absolute; left: 0; right: 0; bottom: 6%; text-align: center; display: flex; justify-content: center; gap: 8px; }
#shareOverlay[hidden] { display: none; }
#shareOverlay button { font-size: 12px; padding: 8px 14px; }

@media (max-width: 700px) {
  #bar { gap: 5px; font-size: 10px; }
  #bar button, #bar a { padding: 5px 8px; }
  .cards {
    flex-direction: column; overflow-x: hidden; overflow-y: visible;
    touch-action: pan-y; gap: 8px;
  }
  .card { flex: 0 0 auto; width: 100%; }
  .card canvas { height: 48px; }
}
@media (max-width: 360px) {
  #bar { gap: 4px; }
  #bar button, #bar a { padding: 5px 6px; letter-spacing: 0; }
}

dialog { background: #14102e; color: #dfe8f5; border: 3px solid #5b52b8; padding: 16px 18px; width: min(560px, 94vw); max-height: 92vh; overflow: auto; font: 13px/1.45 ui-monospace, Menlo, Consolas, monospace; }
dialog::backdrop { background: rgba(0,0,0,.65); }
dialog h2 { margin: 0 0 8px; font-size: 16px; color: #ffd23a; }
dialog a { color: #8ad0ff; }
dialog button { font: inherit; background: #1b1740; color: #cfe0f0; border: 1px solid #3b3480; padding: 6px 12px; margin-top: 8px; margin-right: 6px; cursor: pointer; }
dialog button:hover { background: #2a2360; }
dialog button.primary { background: #6a3aa8; border-color: #ffd23a; color: #fff; }
dialog .x { position: absolute; right: 8px; top: 4px; border: 0; background: none; font-size: 22px; margin: 0; }
#lvlMsg { margin-top: 12px; white-space: pre-wrap; word-break: break-word; }
#lvlMsg .warn { color: #ffd98a; } #lvlMsg .hint { color: #9fb0c8; display: block; margin-left: 1.2em; }
#lvlMsg ul { margin: 4px 0; padding-left: 1.2em; }
#lvlMsg .spin::after { content: ''; display: inline-block; width: 1em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }
#lvlActions input { margin-top: 8px; }

#btnPause {
  display: none; position: fixed; z-index: 6;
  top: max(8px, env(safe-area-inset-top, 0px));
  right: max(8px, env(safe-area-inset-right, 0px));
  background: #1b1740; color: #cfe0f0; border: 2px solid #8a7cff;
  padding: 8px 12px; font: bold 12px ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .06em; cursor: pointer; pointer-events: auto;
}
#btnPause[aria-pressed="true"] { background: #6a3aa8; border-color: #ffd23a; color: #fff; }
body.touch:not(.title) #btnPause:not([hidden]) { display: block; }
