* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #7cc8ff;
  font-family: "Chalkboard SE", "Comic Sans MS", "Comic Neue", "Marker Felt", system-ui, sans-serif;
  color: #222;
  user-select: none; -webkit-user-select: none;
  touch-action: none;
}
#game, #game canvas { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
button { font: inherit; cursor: pointer; border: none; }
.hidden { display: none !important; }

/* Screen flash (lightning, death, void) */
#flash { position: fixed; inset: 0; pointer-events: none; opacity: 0; background: #fff; transition: opacity .25s; z-index: 5; }

/* HUD */
#hud {
  position: fixed; top: max(10px, env(safe-area-inset-top)); left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  display: flex; justify-content: space-between; align-items: center; z-index: 10; pointer-events: none;
}
#hud > *, .hud-right > * { pointer-events: auto; }
.hud-right { display: flex; gap: 8px; }
.pill {
  background: rgba(255,255,255,.88); color: #222; border-radius: 999px; padding: 8px 16px;
  font-size: 18px; font-weight: bold; box-shadow: 0 3px 0 rgba(0,0,0,.18);
}
.pill.round { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; }

#toast {
  position: fixed; top: 70px; left: 50%; transform: translateX(-50%) translateY(-10px);
  background: #fff8c4; border: 3px solid #222; border-radius: 16px; padding: 10px 20px;
  font-size: 22px; font-weight: bold; z-index: 11; opacity: 0; transition: opacity .2s, transform .2s;
  pointer-events: none; white-space: nowrap; max-width: 94vw; overflow: hidden; text-overflow: ellipsis;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

#prompt {
  position: fixed; bottom: calc(96px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.72); color: #fff; padding: 10px 18px; border-radius: 14px;
  font-size: 20px; z-index: 10; pointer-events: none; white-space: nowrap;
}
#prompt kbd {
  display: inline-block; background: #fff; color: #222; border-radius: 6px; padding: 0 8px; margin-right: 6px;
  font-family: inherit; font-weight: bold; box-shadow: 0 2px 0 #999;
}
#inventory {
  position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  display: flex; gap: 8px; z-index: 10; pointer-events: none;
}
.slot {
  width: 58px; height: 58px; border-radius: 12px; background: rgba(255,255,255,.85); border: 3px solid #222;
  display: grid; place-items: center; font-size: 32px;
}

/* Touch controls */
#touch { position: fixed; inset: 0; z-index: 9; pointer-events: none; }
#joy {
  position: absolute; width: 130px; height: 130px; border-radius: 50%;
  background: rgba(255,255,255,.25); border: 3px solid rgba(255,255,255,.6);
  left: 30px; bottom: calc(30px + env(safe-area-inset-bottom)); opacity: .55;
}
#joyKnob {
  position: absolute; width: 56px; height: 56px; border-radius: 50%; background: rgba(255,255,255,.85);
  left: 37px; top: 37px;
}
.tbtn {
  position: absolute; pointer-events: auto; border-radius: 50%;
  background: rgba(255,255,255,.8); border: 3px solid #222; font-size: 30px;
  display: grid; place-items: center; box-shadow: 0 4px 0 rgba(0,0,0,.25);
}
.tbtn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.25); }
#btnJump { width: 84px; height: 84px; right: 28px; bottom: calc(34px + env(safe-area-inset-bottom)); }
#btnAct  { width: 70px; height: 70px; right: 124px; bottom: calc(40px + env(safe-area-inset-bottom)); background: #fff8c4; }
#btnSwing { width: 70px; height: 70px; right: 40px; bottom: calc(134px + env(safe-area-inset-bottom)); }

/* Screens */
.screen {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; overflow-y: auto;
  padding: max(16px, env(safe-area-inset-top)) 16px 16px;
  background: rgba(124, 200, 255, .55); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
}
.card {
  background: #fffdf5; border: 4px solid #222; border-radius: 24px; padding: 22px 24px;
  width: min(720px, 100%); box-shadow: 0 8px 0 rgba(0,0,0,.2); margin: auto;
}
.card.center { text-align: center; }
.title { margin: 0; font-size: clamp(36px, 9vw, 64px); letter-spacing: 4px; text-align: center; line-height: 1.05; }
.title span { color: #e8a200; }
.sub { text-align: center; font-size: 18px; margin: 8px 0 16px; }
h2 { margin: 18px 0 10px; font-size: 24px; }
h2 span { font-size: 18px; color: #666; }
.big {
  display: block; margin: 0 auto; background: #4fc04f; color: #fff; font-size: 30px; font-weight: bold;
  padding: 12px 40px; border-radius: 18px; border: 4px solid #222; box-shadow: 0 6px 0 #222;
}
.big:active { transform: translateY(4px); box-shadow: 0 2px 0 #222; }
.small {
  background: #fff; border: 3px solid #222; border-radius: 12px; padding: 8px 14px; font-size: 16px;
  box-shadow: 0 3px 0 #222;
}
.small.ghost { border-color: #aaa; color: #888; box-shadow: none; }
.menu-row { display: flex; gap: 10px; justify-content: center; align-items: center; flex-wrap: wrap; margin-top: 16px; }
.touch-only { display: none; }
body.is-touch .touch-only { display: grid; }
body.is-touch .mouse-only { display: none; }

#endingGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(124px, 1fr)); gap: 8px; }
.end-tile {
  border: 3px solid #222; border-radius: 14px; padding: 8px; text-align: center; background: #fff;
  min-height: 96px;
}
.end-tile .e { font-size: 30px; }
.end-tile .n { font-weight: bold; font-size: 15px; }
.end-tile .h { font-size: 12px; color: #777; margin-top: 2px; }
.end-tile.locked { background: #ececec; border-color: #999; border-style: dashed; }
.end-tile.locked .e { filter: grayscale(1) brightness(.4); opacity: .5; }
.help {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 4px 12px;
  margin-top: 16px; font-size: 14px; color: #555;
}

.end-emoji { font-size: 84px; line-height: 1; }
.end-got { font-size: 20px; color: #666; margin-top: 6px; }
.end-name { margin: 2px 0 6px; font-size: clamp(30px, 7vw, 48px); }
.end-text { font-size: 20px; margin: 8px 0; }
.end-count { font-size: 18px; }
.new-badge {
  display: inline-block; background: #ff4f6d; color: #fff; font-weight: bold; padding: 4px 14px;
  border-radius: 999px; transform: rotate(-4deg); font-size: 20px;
}
#ending.void { background: rgba(0,0,0,.85); }
#ending.lava { background: rgba(255,90,0,.55); }
#drawing img { width: 100%; max-height: 70vh; object-fit: contain; border-radius: 12px; margin-bottom: 12px; }

@media (max-width: 480px) {
  .card { padding: 16px 14px; border-radius: 20px; }
  #endingGrid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .end-tile { min-height: 84px; padding: 6px 4px; border-radius: 12px; }
  .end-tile .e { font-size: 24px; }
  .end-tile .n { font-size: 12px; }
  .end-tile .h { font-size: 10px; }
  h2 { margin: 14px 0 8px; }
}
@media (max-height: 500px) {
  .card { padding: 14px 18px; }
  .end-emoji { font-size: 56px; }
  .big { font-size: 24px; padding: 8px 28px; }
}

/* Touch layout: keep the bottom free for the joystick and buttons */
body.is-touch #inventory { top: calc(62px + env(safe-area-inset-top)); bottom: auto; }
body.is-touch .slot { width: 46px; height: 46px; font-size: 26px; border-width: 2px; }
body.is-touch #toast { top: calc(118px + env(safe-area-inset-top)); font-size: 18px; }
body.is-touch #prompt { bottom: calc(222px + env(safe-area-inset-bottom)); font-size: 18px; }
@media (orientation: landscape) and (max-height: 500px) {
  body.is-touch #prompt { bottom: calc(24px + env(safe-area-inset-bottom)); }
  body.is-touch #toast { top: calc(60px + env(safe-area-inset-top)); }
  body.is-touch #inventory { top: calc(10px + env(safe-area-inset-top)); }
}
