:root {
  --ink: #1f2428;
  --cream: #fffaf0;
  --gold: #efb521;
  --gold-light: #ffd95a;
  --wood: #8e4a23;
  --red: #ef4250;
  --green: #43ad62;
  --sky: #dff2ed;
}

* { box-sizing: border-box; }

html,
body {
  min-width: 300px;
  margin: 0;
  background: transparent;
}

button,
canvas { -webkit-tap-highlight-color: transparent; }

button { font: inherit; }

.game {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  width: min(100%, 620px);
  min-height: 500px;
  aspect-ratio: 4 / 5;
  margin: 0 auto;
  border: 2px solid #d7d4c7;
  border-radius: 16px;
  color: var(--ink);
  background: var(--sky);
  box-shadow: inset 0 0 38px rgba(43, 83, 72, .12);
  font-family: "Trebuchet MS", "Arial Rounded MT Bold", sans-serif;
  user-select: none;
}

.hud {
  position: relative;
  z-index: 20;
  display: grid;
  grid-template-columns: 92px 1fr 42px;
  align-items: center;
  min-height: 64px;
  padding: 8px 12px;
  border-bottom: 2px solid rgba(31, 36, 40, .14);
  background: rgba(255, 252, 241, .95);
  box-shadow: 0 5px 0 rgba(31, 36, 40, .07);
}

.level-block {
  display: flex;
  flex-direction: column;
  line-height: 1;
}

.level-block span {
  color: #647d74;
  font-size: .54rem;
  font-weight: 1000;
  letter-spacing: .13em;
}

.level-block strong {
  margin-top: 4px;
  font-size: 1.15rem;
}

.fence-count {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  font-size: 1.18rem;
}

.rope-icon {
  position: relative;
  display: block;
  width: 38px;
  height: 28px;
}

.rope-icon::before,
.rope-icon::after {
  position: absolute;
  top: 1px;
  width: 7px;
  height: 26px;
  border: 2px solid var(--ink);
  border-radius: 4px;
  background: var(--wood);
  content: "";
}

.rope-icon::before { left: 1px; }
.rope-icon::after { right: 1px; }

.rope-icon i,
.rope-icon b {
  position: absolute;
  left: 6px;
  width: 27px;
  height: 6px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--gold);
}

.rope-icon i { top: 7px; }
.rope-icon b { bottom: 6px; }

.reset-button {
  width: 36px;
  min-height: 34px;
  border: 2px solid var(--ink);
  border-radius: 9px;
  color: var(--ink);
  background: #fff;
  box-shadow: 2px 3px 0 var(--ink);
  font-size: 1.08rem;
  font-weight: 1000;
  cursor: pointer;
}

.reset-button:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.stage {
  position: absolute;
  inset: 64px 0 0;
  overflow: hidden;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#game-canvas:active { cursor: grabbing; }

.status-chip {
  position: absolute;
  z-index: 8;
  top: 13px;
  left: 50%;
  min-width: 150px;
  transform: translateX(-50%);
  padding: 8px 13px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: rgba(255,255,255,.93);
  box-shadow: 3px 4px 0 var(--ink);
  font-size: .65rem;
  font-weight: 1000;
  text-align: center;
  letter-spacing: .1em;
  pointer-events: none;
}

.status-chip.is-danger { color: #fff; background: var(--red); }
.status-chip.is-safe { color: #fff; background: var(--green); }

.feedback {
  position: absolute;
  z-index: 14;
  bottom: 122px;
  left: 50%;
  transform: translateX(-50%) translateY(10px) scale(.9);
  opacity: 0;
  padding: 9px 14px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: #fff;
  box-shadow: 3px 4px 0 var(--ink);
  font-size: .68rem;
  font-weight: 1000;
  text-align: center;
  letter-spacing: .06em;
  pointer-events: none;
}

.feedback.is-visible { animation: feedback-pop .85s ease both; }

.overlay {
  position: absolute;
  z-index: 40;
  inset: 0;
  display: grid;
  place-items: center;
  visibility: hidden;
  opacity: 0;
  background: rgba(21, 35, 31, .38);
  backdrop-filter: blur(4px);
  transition: opacity .24s ease, visibility .24s ease;
}

.overlay.is-visible { visibility: visible; opacity: 1; }

.result-card {
  position: relative;
  width: min(70%, 306px);
  transform: translateY(16px) scale(.88) rotate(-1deg);
  padding: 27px 20px 20px;
  border: 3px solid var(--ink);
  border-radius: 19px 14px 21px 15px;
  background: var(--cream);
  box-shadow: 8px 9px 0 var(--ink);
  text-align: center;
  transition: transform .35s cubic-bezier(.18,.9,.28,1.25);
}

.overlay.is-visible .result-card { transform: translateY(0) scale(1) rotate(-1deg); }

.result-icon {
  position: relative;
  display: grid;
  width: 66px;
  aspect-ratio: 1;
  margin: -61px auto 10px;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: var(--gold-light);
  box-shadow: 4px 5px 0 var(--ink);
}

.result-icon::before {
  width: 26px;
  height: 14px;
  transform: rotate(-45deg) translate(2px, -2px);
  border-bottom: 7px solid var(--ink);
  border-left: 7px solid var(--ink);
  content: "";
}

.result-card h1 {
  margin: 6px 0 3px;
  font-size: clamp(1.75rem, 7vw, 2.5rem);
  line-height: .95;
  letter-spacing: -.04em;
}

.result-card p {
  margin: 9px 0 16px;
  color: #627d73;
  font-size: .7rem;
  font-weight: 1000;
  letter-spacing: .12em;
}

.action-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  color: var(--ink);
  background: var(--gold-light);
  box-shadow: 4px 5px 0 var(--ink);
  font-size: .76rem;
  font-weight: 1000;
  letter-spacing: .08em;
  cursor: pointer;
}

.action-button b { font-size: 1.2rem; }
.action-button:active { transform: translate(3px, 3px); box-shadow: 1px 2px 0 var(--ink); }

.result-card.is-loss { width: min(64%, 276px); }
.result-card.is-loss .result-icon { background: var(--red); }
.result-card.is-loss .result-icon::before {
  width: 27px;
  height: 7px;
  transform: rotate(45deg);
  border: 0;
  border-radius: 4px;
  background: #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,.05), 0 0 0 0 #fff;
}
.result-card.is-loss .result-icon::after {
  position: absolute;
  width: 27px;
  height: 7px;
  transform: rotate(-45deg);
  border-radius: 4px;
  background: #fff;
  content: "";
}
.result-card.is-loss .action-button { color: #fff; background: var(--red); }

.confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.confetti i { position: absolute; top: -8%; width: 10px; height: 18px; border-radius: 2px; background: var(--gold-light); animation: confetti-fall 1.5s ease-in forwards; }
.confetti i:nth-child(1) { left: 9%; animation-delay: .04s; }
.confetti i:nth-child(2) { left: 21%; animation-delay: .27s; background: #f18a3f; }
.confetti i:nth-child(3) { left: 34%; animation-delay: .11s; background: #54bfd2; }
.confetti i:nth-child(4) { left: 47%; animation-delay: .34s; background: var(--green); }
.confetti i:nth-child(5) { left: 59%; animation-delay: .17s; }
.confetti i:nth-child(6) { left: 71%; animation-delay: .3s; background: var(--red); }
.confetti i:nth-child(7) { left: 83%; animation-delay: .08s; background: #54bfd2; }
.confetti i:nth-child(8) { left: 94%; animation-delay: .37s; background: #f18a3f; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }

@keyframes feedback-pop {
  0% { opacity: 0; transform: translateX(-50%) translateY(10px) scale(.9); }
  20%, 75% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-8px) scale(.96); }
}

@keyframes confetti-fall { to { transform: translateY(820px) rotate(600deg); opacity: .2; } }

@media (max-width: 420px) {
  .game { min-height: 460px; border-radius: 10px; }
  .hud { grid-template-columns: 78px 1fr 36px; padding-inline: 8px; }
  .reset-button { width: 32px; }
  .result-card { width: 76%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* Iframe sizing: preserve the game ratio while respecting short and narrow frames. */
html,
body {
  width: 100%;
  height: 100%;
  min-width: 0;
  overflow: hidden;
}

body {
  display: grid;
  place-items: center;
  position: relative;
  isolation: isolate;
  background: var(--sky);
}

.ambient-scene-copy {
  position: fixed !important;
  top: 50% !important;
  left: 50% !important;
  z-index: 0 !important;
  margin: 0 !important;
  border-color: transparent !important;
  box-shadow: none !important;
  filter: blur(12px) saturate(.88) brightness(.94);
  opacity: .38;
  pointer-events: none !important;
  transform-origin: center;
  user-select: none;
}

.ambient-scene-copy--left { left: 12% !important; transform: translate(-50%, -50%) scale(1.12) rotate(-3deg); }
.ambient-scene-copy--right { left: 88% !important; transform: translate(-50%, -50%) scale(1.12) rotate(3deg); }

.ambient-scene-copy *,
.ambient-scene-copy *::before,
.ambient-scene-copy *::after { animation-play-state: paused !important; }

@media (max-width: 700px) {
  .ambient-scene-copy--left { left: 50% !important; transform: translate(-50%, -50%) scale(1.9); }
  .ambient-scene-copy--right { display: none !important; }
}

.game {
  z-index: 1;
  width: min(100vw, 620px, 80vh);
  height: auto;
  min-height: 0;
  max-height: 100vh;
  margin: 0;
}

@supports (height: 100dvh) {
  .game {
    width: min(100vw, 620px, 80dvh);
    max-height: 100dvh;
  }
}
