:root {
  color-scheme: dark;
  --ink: #f7fbff;
  --muted: #b9c9ea;
  --panel: rgba(12, 20, 47, .88);
  --gold: #ffd52f;
  --blue: #5ab5ff;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  padding: clamp(14px, 3vw, 36px);
  overflow-x: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 15%, rgba(59, 125, 240, .25), transparent 30rem),
    radial-gradient(circle at 88% 88%, rgba(126, 71, 195, .22), transparent 28rem),
    #080d21;
  font-family: Inter, ui-rounded, "Segoe UI", system-ui, sans-serif;
}

button { font: inherit; }

.game-shell { width: min(1120px, 100%); margin: 0 auto; }

.game-header, footer, .header-actions, .button-row, .mobile-controls, .direction-pad {
  display: flex;
  align-items: center;
}

.game-header { justify-content: space-between; gap: 20px; margin-bottom: 14px; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(2rem, 5vw, 3.65rem); line-height: .9; letter-spacing: -.05em; }
.eyebrow { margin-bottom: 7px; color: var(--gold); font-size: .75rem; font-weight: 900; letter-spacing: .16em; text-transform: uppercase; }
.header-actions, .button-row { gap: 10px; }

button {
  border: 0;
  border-radius: 999px;
  padding: .8rem 1.15rem;
  cursor: pointer;
  color: var(--ink);
  font-weight: 800;
  touch-action: manipulation;
}

button:focus-visible, canvas:focus-visible { outline: 3px solid white; outline-offset: 3px; }
.icon-button, .secondary { border: 1px solid rgba(255,255,255,.22); background: rgba(255,255,255,.08); }
.primary { color: #17203d; background: var(--gold); box-shadow: 0 8px 24px rgba(255, 213, 47, .24); }
button:hover { filter: brightness(1.1); transform: translateY(-1px); }

.stage {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: clamp(16px, 3vw, 28px);
  background: #82c8ef;
  box-shadow: 0 30px 80px rgba(0,0,0,.42), inset 0 1px rgba(255,255,255,.35);
}

canvas { display: block; width: 100%; height: auto; aspect-ratio: 12 / 7; }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: linear-gradient(120deg, rgba(6,12,32,.45), rgba(6,12,32,.2));
  transition: opacity .18s ease;
}

.overlay.hidden { opacity: 0; pointer-events: none; }
.panel {
  width: min(520px, 94%);
  padding: clamp(24px, 5vw, 46px);
  border: 1px solid rgba(255,255,255,.25);
  border-radius: 24px;
  background: var(--panel);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
  backdrop-filter: blur(12px);
}
.panel h2, dialog h2 { margin-bottom: 12px; font-size: clamp(2rem, 5vw, 3.5rem); line-height: 1; letter-spacing: -.04em; }
.panel > p:not(.eyebrow) { color: var(--muted); line-height: 1.55; }
.best-score { margin: 20px 0 0; font-size: .9rem; }
.hidden { display: none !important; }

.mobile-controls { display: none; justify-content: space-between; padding: 14px 2px 0; }
.direction-pad { gap: 12px; }
.touch-button { width: 70px; height: 58px; padding: 0; border: 1px solid rgba(255,255,255,.2); border-radius: 18px; background: #18254b; box-shadow: inset 0 -4px rgba(0,0,0,.25); user-select: none; -webkit-user-select: none; }
.touch-button.active { transform: translateY(2px); box-shadow: inset 0 -1px rgba(0,0,0,.25); background: #2d4b88; }
.jump-button { width: 100px; color: #15203f; background: var(--gold); }
.touch-button { touch-action: none; -webkit-touch-callout: none; background-color: rgba(45, 65, 110, .75); }
.jump-button { background-color: rgba(255, 213, 47, .85); }
@media (max-width: 900px), (pointer: coarse) {
  .mobile-controls {
    position: fixed;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    z-index: 5;
  }
}
.orientation-tip { display: none; color: var(--muted); font-size: .8rem; text-align: center; margin: 10px 0; }
.level-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 18px 0; }
.level-grid button { text-align: left; border-radius: 12px; min-height: 48px; background: #253b61; font-size: .9rem; }
.level-grid button:disabled { opacity: .45; cursor: default; }
dialog { max-height: 90dvh; overflow: auto; }
.overlay { overflow-y: auto; }
.header-actions { flex-wrap: wrap; justify-content: flex-end; }
body { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
@media (max-width: 900px), (pointer: coarse) {
  .mobile-controls { display: flex; }
  .game-shell { width: min(100%, calc((100dvh - 170px) * 12 / 7)); }
  .game-header { gap: 8px; margin-bottom: 8px; }
  h1 { font-size: 1.7rem; }
  .game-header .eyebrow, footer { display: none; }
  .icon-button { min-height: 44px; padding: .6rem .75rem; font-size: .8rem; }
  .panel { padding: 16px; width: min(520px, 98%); }
  .panel h2 { font-size: clamp(1.35rem, 4vw, 2.5rem); margin-bottom: 8px; }
  .panel p { margin-bottom: 8px; font-size: .85rem; }
  .best-score { margin-top: 8px; }
  .stage { border-radius: 14px; }
}
@media (max-width: 620px) and (orientation: portrait) {
  .game-shell { width: 100%; }
  .game-header { align-items: center; flex-wrap: wrap; }
  .header-actions { gap: 4px; }
  .overlay { padding: 6px; }
  .panel { padding: 12px; }
  .panel .eyebrow, .panel .best-score { display: none; }
  .panel h2 { font-size: 1.35rem; }
  .panel p { line-height: 1.25 !important; font-size: .78rem; }
  .panel button { min-height: 44px; padding: .5rem .75rem; font-size: .85rem; }
  .orientation-tip { display: block; }
  .touch-button { height: 72px; width: 76px; }
  .jump-button { width: 100px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  body { padding-top: max(4px, env(safe-area-inset-top)); }
  .game-shell { width: min(100%, calc((100dvh - 125px) * 12 / 7)); }
  .mobile-controls { padding-top: 5px; }
  .touch-button { height: 56px; }
  .panel { padding: 10px 16px; }
  .panel .eyebrow, .panel .best-score { display: none; }
  .game-header > div:first-child { display: none; }
  .header-actions { width: 100%; justify-content: center; gap: 4px; }
  .header-actions .icon-button { font-size: .72rem; padding: .5rem .6rem; }
}

footer { justify-content: center; flex-wrap: wrap; gap: 14px 24px; padding: 18px 8px 0; color: var(--muted); font-size: .82rem; }
kbd { padding: 2px 6px; border: 1px solid rgba(255,255,255,.2); border-radius: 5px; color: white; background: rgba(255,255,255,.08); font: inherit; font-weight: 800; }

dialog { width: min(620px, calc(100% - 28px)); padding: 0; border: 1px solid rgba(255,255,255,.25); border-radius: 24px; color: var(--ink); background: #101a38; box-shadow: 0 30px 90px #000; }
dialog::backdrop { background: rgba(3,7,20,.75); backdrop-filter: blur(5px); }
dialog form { position: relative; padding: clamp(24px, 6vw, 48px); }
.dialog-close { position: absolute; top: 14px; right: 14px; padding: 4px 12px; color: white; background: transparent; font-size: 1.8rem; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 22px 0; }
.help-grid div { padding: 16px; border-radius: 14px; background: rgba(255,255,255,.07); }
.help-grid b, .help-grid span { display: block; }
.help-grid b { margin-bottom: 4px; color: var(--gold); }
.help-grid span, dialog form > p:not(.eyebrow) { color: var(--muted); line-height: 1.45; }

@media (hover: none), (pointer: coarse) { .mobile-controls { display: flex; } footer { display: none; } }
@media (max-width: 620px) {
  body { padding: 12px; }
  .game-header { align-items: flex-end; }
  .header-actions { gap: 6px; }
  .icon-button { padding: .65rem .78rem; font-size: .78rem; }
  .help-grid { grid-template-columns: 1fr; }
  .panel { text-align: center; }
  .button-row { justify-content: center; flex-wrap: wrap; }
}

.mobile-menu-button, .mobile-fullscreen, .fullscreen-status { display: none; }

/* Phone presentation only. Keep the desktop rules and canvas coordinates intact. */
@media (pointer: coarse) and (max-width: 1100px), (max-width: 620px) {
  body { --viewport-height: 100vh; }
  @supports (height: 100dvh) { body { --viewport-height: 100dvh; } }
  body { padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left)); }
  .game-shell {
    --screen-height: calc(var(--viewport-height) - max(4px, env(safe-area-inset-top)) - max(4px, env(safe-area-inset-bottom)));
    width: 100%;
  }
  .game-header { position: relative; height: 44px; margin: 0 0 6px; }
  .game-header > div:first-child { display: none; }
  .mobile-menu-button {
    display: block; position: fixed;
    top: max(4px, env(safe-area-inset-top)); right: max(4px, env(safe-area-inset-right));
    z-index: 9; min-height: 44px;
  }
  .header-actions { gap: 6px; width: auto; }
  .header-actions > :not(#pauseButton) { display: none; }
  .header-actions #pauseButton {
    position: fixed; top: max(4px, env(safe-area-inset-top)); left: max(4px, env(safe-area-inset-left));
    z-index: 9; min-height: 44px;
  }
  .mobile-menu-button, .header-actions #pauseButton { background: rgba(17,26,56,.88); padding: .5rem .65rem; font-size: .8rem; }
  .mobile-menu-open .header-actions {
    display: grid; grid-template-columns: 1fr 1fr; position: fixed; z-index: 8;
    top: calc(max(4px, env(safe-area-inset-top)) + 50px); right: max(4px, env(safe-area-inset-right));
    width: min(320px, calc(100% - 8px)); max-height: calc(var(--screen-height) - 56px);
    overflow-y: auto; padding: 12px; border: 1px solid #596588; border-radius: 16px;
    background: #111a38; box-shadow: 0 12px 32px #0008;
  }
  .mobile-menu-open .header-actions > :not(#pauseButton) { display: block; }
  .mobile-menu-open .header-actions .icon-button { min-height: 44px; font-size: .85rem; padding: .5rem; }
  .mobile-menu-open .fullscreen-status { grid-column: 1 / -1; margin: 0; color: var(--muted); font-size: .8rem; line-height: 1.4; text-align: left; }
  .fullscreen-status:empty { display: none !important; }
  .mobile-fullscreen:disabled { opacity: .55; cursor: default; }
  .mobile-controls {
    display: flex; padding: 0;
    bottom: max(6px, env(safe-area-inset-bottom));
    left: max(6px, env(safe-area-inset-left)); right: max(6px, env(safe-area-inset-right));
  }
  .direction-pad { gap: 8px; }
  .touch-button { width: 60px; height: 56px; border-radius: 16px; background: rgba(45,65,110,.65); }
  .jump-button { width: 72px; background: rgba(255,213,47,.72); }
  .orientation-tip { display: block; padding: 0 8px; }
  footer { display: none; }
  .stage { border-radius: 10px; }
  dialog { max-height: calc(var(--viewport-height) - 16px); }
}

@media (pointer: coarse) and (max-width: 1100px) and (orientation: landscape), (max-width: 620px) and (orientation: landscape) {
  .game-shell {
    --play-height: calc(var(--screen-height) - 68px);
    height: var(--screen-height); display: grid; grid-template-rows: minmax(0, 1fr) 68px;
  }
  .game-header { position: absolute; height: 0; margin: 0; }
  .stage { grid-area: 1 / 1; place-self: center; width: min(calc(100% - 136px), calc((var(--play-height) - 2px) * 12 / 7 + 2px)); }
  .orientation-tip { display: none; }
  .overlay { padding: 8px; }
  .panel { padding: 12px 16px; }
  .panel .eyebrow, .panel .best-score { display: none; }
}

/* Wide phones have room for thumb controls beside a nearly full-height canvas. */
@media (pointer: coarse) and (max-width: 1100px) and (orientation: landscape) and (min-aspect-ratio: 2/1), (max-width: 620px) and (orientation: landscape) and (min-aspect-ratio: 2/1) {
  .game-shell { --play-height: var(--screen-height); grid-template-rows: 1fr; }
  .stage { width: min(calc(100% - 184px), calc((var(--play-height) - 2px) * 12 / 7 + 2px)); }
  .direction-pad { flex-direction: column; }
}
