:root {
  --navy: #0d1424;
  --navy-2: #1b2440;
  --paper: #f4ecd8;
  --yellow: #ffd23f;
  --green: #3ba55c;
  --red: #e2453c;
  --ink: #0b0f19;
  --btn-bg: rgba(13, 20, 36, 0.45);
  --btn-border: rgba(244, 236, 216, 0.55);
  --font: 'Trebuchet MS', 'Segoe UI', system-ui, sans-serif;
  --font-display: 'Arial Black', 'Segoe UI Black', Impact, var(--font);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #000;
  color: var(--paper);
  font-family: var(--font);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game {
  display: block;
  image-rendering: pixelated;
  background: var(--navy);
}

/* Touch controls: only on touch devices, over the bottom corners. */
#controls {
  display: none;
}

body.touch #controls {
  display: block;
}

body.touch.no-controls #controls {
  display: none;
}

.pad {
  position: fixed;
  bottom: max(10px, env(safe-area-inset-bottom));
  display: grid;
  gap: 8px;
}

.pad-move {
  left: max(10px, env(safe-area-inset-left));
  grid-template-columns: 68px 68px;
}

.pad-act {
  right: max(10px, env(safe-area-inset-right));
  grid-template-columns: 64px 76px;
  grid-template-areas:
    'watch goat'
    'heli jump';
  align-items: end;
}

.btn {
  position: relative;
  border: 2px solid var(--btn-border);
  border-radius: 14px;
  background: var(--btn-bg);
  color: var(--paper);
  font: 700 13px var(--font);
  text-shadow: 0 1px 0 #000;
  touch-action: none;
  overflow: hidden;
}

.btn.on {
  background: rgba(255, 210, 63, 0.55);
  border-color: var(--yellow);
}

.btn-dir {
  height: 68px;
  font-size: 26px;
}

.btn-jump {
  grid-area: jump;
  height: 76px;
  border-radius: 50%;
  font-size: 15px;
}

.btn-heli {
  grid-area: heli;
  height: 64px;
  border-radius: 50%;
  font-size: 11px;
}

.btn-watch {
  grid-area: watch;
  height: 52px;
  font-size: 11px;
}

.btn-goat {
  grid-area: goat;
  height: 52px;
  opacity: 0.55;
}

.btn-goat.ready {
  opacity: 1;
  border-color: var(--yellow);
  box-shadow: 0 0 14px var(--yellow);
  animation: pulse 0.8s ease-in-out infinite alternate;
}

/* Cooldown sweep driven by --cooldown (0..1). */
.btn-watch::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  transform-origin: bottom;
  transform: scaleY(var(--cooldown, 0));
}

@keyframes pulse {
  to {
    transform: scale(1.06);
  }
}

#topbar {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
}

.btn-small {
  width: 40px;
  height: 40px;
  border: 2px solid var(--btn-border);
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--paper);
  font: 700 16px var(--font);
}

body:not([data-state='play']):not([data-state='paused']) #pause-btn {
  display: none;
}

.btn-small.muted {
  text-decoration: line-through;
  opacity: 0.6;
}

/* ---------- Screens ---------- */

#screens {
  position: fixed;
  inset: 0;
  pointer-events: none;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 3vw, 32px);
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(9, 13, 24, 0.68);
  pointer-events: auto;
  overflow-y: auto;
  user-select: text;
  -webkit-user-select: text;
}

.screen.active {
  display: flex;
  animation: fade-in 0.18s ease-out;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

.btn-primary,
.btn-secondary {
  font: 700 clamp(15px, 3.6vh, 20px) var(--font);
  padding: 0.55em 1.4em;
  border: 3px solid var(--ink);
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--yellow);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink);
}

.btn-primary:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--ink);
}

.btn-primary:disabled {
  opacity: 0.6;
}

.btn-secondary {
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 3px 0 var(--ink);
}

/* Title */
@media (max-width: 600px) {
  #scr-title {
    flex-wrap: wrap;
  }
}

.title-main {
  display: flex;
  align-items: center;
  gap: clamp(10px, 2.5vw, 24px);
}

.portrait {
  width: clamp(96px, 30vh, 176px);
  height: auto;
  image-rendering: pixelated;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.35));
}

.title-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: clamp(4px, 1.4vh, 10px);
}

.logo {
  margin: 0;
  font: 900 clamp(30px, 10vh, 60px) / 1 var(--font-display);
  color: var(--yellow);
  letter-spacing: -0.02em;
  text-shadow:
    3px 3px 0 var(--ink),
    -1px -1px 0 var(--ink),
    1px -1px 0 var(--ink),
    -1px 1px 0 var(--ink),
    0 7px 0 #8a5b00;
  transform: rotate(-3deg);
  margin-bottom: 6px;
}

.tagline {
  margin: 0 0 4px;
  font: italic 700 clamp(13px, 3.6vh, 18px) var(--font);
  color: #9fe0a6;
  letter-spacing: 0.04em;
}

.hint {
  max-width: 240px;
  margin: 2px 0 0;
  font-size: clamp(10px, 2.6vh, 12px);
  line-height: 1.35;
  color: #c9d2e3;
}

/* Memo-style leaderboard */
.memo {
  flex-shrink: 0;
  width: min(270px, 38vw);
  padding: 10px 14px 12px;
  background: var(--paper);
  color: var(--ink);
  border-radius: 3px;
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.2),
    6px 7px 0 rgba(0, 0, 0, 0.35);
  transform: rotate(1.2deg);
  font-size: clamp(11px, 3vh, 14px);
}

.memo.wide {
  width: min(360px, 60vw);
}

.memo-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding-bottom: 5px;
  margin-bottom: 4px;
  border-bottom: 2px solid var(--ink);
  font-size: 0.85em;
}

.memo-head b {
  font-family: var(--font-display);
  letter-spacing: 0.08em;
}

.board {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: rank;
}

.board li {
  display: flex;
  gap: 6px;
  align-items: baseline;
  padding: 1px 0;
  counter-increment: rank;
  border-bottom: 1px dotted rgba(11, 15, 25, 0.25);
}

.board li::before {
  content: counter(rank) '.';
  width: 1.6em;
  font-weight: 700;
  color: #7a6f57;
}

.board .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board li.me {
  background: var(--yellow);
  margin: 0 -4px;
  padding: 1px 4px;
}

.memo-note {
  margin: 6px 0 0;
  font-style: italic;
  font-size: 0.9em;
}

.memo-note:empty {
  display: none;
}

/* Level card */
#scr-card {
  flex-direction: column;
  gap: 4px;
  text-align: center;
  cursor: pointer;
}

.card-level {
  margin: 0;
  font: 700 clamp(13px, 4vh, 18px) var(--font);
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: #9fe0a6;
}

.card-title {
  margin: 0;
  font: 900 clamp(30px, 11vh, 58px) / 1.05 var(--font-display);
  color: var(--paper);
  text-shadow: 3px 3px 0 var(--ink);
}

.card-quote {
  margin: 6px 0 14px;
  font: italic clamp(14px, 4.4vh, 20px) var(--font);
  color: var(--yellow);
}

.card-tap {
  margin: 0;
  font-size: 12px;
  color: #c9d2e3;
  animation: blink 1.2s steps(2) infinite;
}

@keyframes blink {
  50% {
    opacity: 0.25;
  }
}

/* Pause and end panels */
.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(6px, 1.8vh, 12px);
  padding: clamp(12px, 3vh, 22px) clamp(18px, 4vw, 34px);
  background: var(--navy-2);
  border: 3px solid var(--ink);
  border-radius: 14px;
  box-shadow: 0 6px 0 var(--ink);
  max-width: min(560px, 100%);
}

.panel h2 {
  margin: 0;
  font: 900 clamp(20px, 6.5vh, 32px) / 1.1 var(--font-display);
  color: var(--yellow);
  text-align: center;
}

.panel .sub {
  margin: 0 0 4px;
  font-style: italic;
  color: #c9d2e3;
}

.big-score {
  margin: 0;
  font: 900 clamp(18px, 6vh, 28px) var(--font-display);
  color: var(--paper);
}

#ui-end-form {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
}

#ui-end-form label {
  font-size: 12px;
  color: #c9d2e3;
}

#ui-end-form .row {
  display: flex;
  gap: 8px;
}

#ui-end-name {
  flex: 1;
  min-width: 0;
  font: 700 18px var(--font);
  padding: 0.4em 0.6em;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  user-select: text;
  -webkit-user-select: text;
  touch-action: manipulation;
}

.error {
  margin: 0;
  min-height: 1.2em;
  font-size: 13px;
  font-weight: 700;
  color: #ff9a8f;
}

.debug-note {
  margin: 0;
  font-weight: 700;
  color: #9fe0a6;
}

.breakdown {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 18px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: clamp(11px, 3vh, 13px);
  color: #dfe6f2;
}

#scr-board {
  flex-direction: column;
}

#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--navy);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  font: 700 20px var(--font);
}

.rotate-phone {
  width: 46px;
  height: 80px;
  border: 4px solid var(--paper);
  border-radius: 9px;
  animation: rotate-hint 1.6s ease-in-out infinite;
}

@keyframes rotate-hint {
  50%,
  100% {
    transform: rotate(-90deg);
  }
}

@media (orientation: portrait) and (pointer: coarse) {
  #rotate {
    display: flex;
  }
}
