:root {
  --sky-top: #dff4ff;
  --sky-bot: #fff6e8;
  --ink: #2b3a4a;
  --muted: #6b7c8d;
  --accent: #ff7a59;
  --accent-2: #5ec8ff;
  --good: #3ecf8e;
  --warn: #ffb020;
  --wood: #ffd27a;
  --wood-edge: #e29b3d;
  --panel: rgba(255, 255, 255, 0.72);
  --font: "Fredoka", "Trebuchet MS", "Segoe UI", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.9), transparent 28%),
    radial-gradient(circle at 82% 18%, rgba(94, 200, 255, 0.35), transparent 30%),
    linear-gradient(180deg, var(--sky-top), var(--sky-bot));
  color: var(--ink);
  font-family: var(--font);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#wrap {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.top {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  z-index: 3;
}

.stars {
  display: flex;
  gap: 8px;
}

.star {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  box-shadow: inset 0 0 0 2px rgba(43, 58, 74, 0.1);
  position: relative;
  transition: transform 180ms ease, background 180ms ease;
}

.star::after {
  content: "★";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 12px;
  color: rgba(43, 58, 74, 0.22);
  transition: color 180ms ease;
}

.star.on {
  background: #ffe566;
  transform: scale(1.1);
}

.star.on::after {
  color: #f0a000;
}

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  border-radius: 28px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0.18) 40%, rgba(94, 200, 255, 0.12));
  border: 3px solid rgba(255, 255, 255, 0.95);
  box-shadow: 0 14px 34px rgba(90, 130, 170, 0.14);
  transition: background 160ms ease, box-shadow 160ms ease;
}

.sky-deco {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 20% 18%, rgba(255, 255, 255, 0.9) 0 18px, transparent 19px),
    radial-gradient(circle at 78% 14%, rgba(255, 255, 255, 0.75) 0 12px, transparent 13px),
    radial-gradient(circle at 62% 28%, rgba(255, 255, 255, 0.65) 0 9px, transparent 10px);
  opacity: 0.9;
}

#ruler {
  position: absolute;
  left: 50%;
  top: 0;
  width: min(88px, 26vw);
  height: 58%;
  transform: translate(-50%, 0);
  transform-origin: top center;
  will-change: transform;
  z-index: 2;
}

#ruler.falling {
  filter: none;
}

.ruler-face {
  width: 100%;
  height: 100%;
  border-radius: 18px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.45), transparent 22%, transparent 78%, rgba(0, 0, 0, 0.05)),
    repeating-linear-gradient(
      180deg,
      #ffe08a 0 9%,
      #ffb4e2 9% 18%,
      #9fe7ff 18% 27%,
      #b8f5c5 27% 36%
    );
  border: 3px solid #ff8fb8;
  box-shadow: 0 10px 22px rgba(255, 140, 180, 0.22);
  position: relative;
  overflow: hidden;
}

.ruler-hole {
  position: absolute;
  top: 10px;
  left: 50%;
  width: 14px;
  height: 14px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: inset 0 0 0 3px #ff8fb8;
}

.ruler-marks {
  position: absolute;
  inset: 34px 0 0;
  background:
    repeating-linear-gradient(
      180deg,
      transparent 0,
      transparent calc(10% - 2px),
      rgba(80, 60, 90, 0.28) calc(10% - 2px),
      rgba(80, 60, 90, 0.28) 10%
    );
  mask-image: linear-gradient(90deg, #000 0 28%, transparent 28% 72%, #000 72% 100%);
}

.go-flash {
  position: absolute;
  left: 50%;
  top: 16%;
  transform: translate(-50%, -50%) scale(0.86);
  z-index: 4;
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: clamp(1.4rem, 7vw, 2rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  opacity: 0;
  pointer-events: none;
  box-shadow: 0 10px 24px rgba(255, 122, 89, 0.35);
}

body.state-falling .go-flash {
  animation: goPop 900ms ease;
  opacity: 1;
}

@keyframes goPop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.7); }
  12% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  70% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0.15; transform: translate(-50%, -42%) scale(1.04); }
}

.catch-zone {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 118px;
  height: 40px;
  z-index: 3;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

body.state-ready .catch-zone,
body.state-falling .catch-zone,
body.state-caught .catch-zone {
  opacity: 1;
}

.measure-line {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 240px;
  height: 3px;
  border-radius: 999px;
  background: #ff7a59;
  box-shadow: 0 0 0 4px rgba(255, 122, 89, 0.15);
  transform: translate(-50%, -50%);
  z-index: 2;
  transition: background 160ms ease, box-shadow 160ms ease;
}

body.state-falling .measure-line {
  background: #2bb673;
  box-shadow: 0 0 0 4px rgba(43, 182, 115, 0.18);
}

#ruler-cm {
  position: absolute;
  left: calc(50% + 120px + 8px);
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.92rem;
  font-weight: 700;
  color: #5a3a60;
  background: #fff;
  padding: 4px 10px;
  border-radius: 999px;
  border: 2px solid rgba(255, 122, 89, 0.35);
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(90, 130, 170, 0.12);
}

body.state-falling #ruler-cm {
  border-color: rgba(43, 182, 115, 0.45);
}

.bottom-dock {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 8px;
  height: 96px;
  z-index: 5;
  pointer-events: none;
}

.hold-targets {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.hold-targets.hidden .hold-circle {
  opacity: 0.38;
  animation: none;
  transform: scale(0.92);
}

.hold-circle {
  position: absolute;
  bottom: 10px;
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: linear-gradient(180deg, #fff, #ffe0d2);
  border: 4px solid #ff9b7a;
  box-shadow: 0 8px 18px rgba(255, 122, 89, 0.22);
  animation: holdPulse 1.4s ease-in-out infinite;
  display: grid;
  place-items: center;
}

.hold-circle.left {
  left: max(8px, env(safe-area-inset-left));
}

.hold-circle.right {
  right: max(8px, env(safe-area-inset-right));
  animation-delay: 0.2s;
}

.hold-circle::after {
  content: "";
  position: absolute;
  inset: -8px;
  border-radius: 50%;
  border: 3px dashed rgba(255, 155, 122, 0.55);
  animation: spinDash 8s linear infinite;
}

/* Drop moment: keep circles bright under thumbs and spell the action */
body.state-falling .hold-targets .hold-circle,
body.state-falling .hold-targets.hidden .hold-circle {
  opacity: 1;
  transform: scale(1.06);
  animation: holdPulse 0.55s ease-in-out infinite;
  background: linear-gradient(180deg, #e8fff3, #9ef0c4);
  border-color: #2bb673;
  box-shadow: 0 8px 20px rgba(43, 182, 115, 0.35);
}

body.state-falling .hold-circle::before {
  content: "LET GO";
  position: relative;
  z-index: 1;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.05;
  text-align: center;
  color: #146b42;
  max-width: 3.4em;
}

body.state-falling .hold-circle::after {
  border-color: rgba(43, 182, 115, 0.65);
  animation: none;
}

.action-hint {
  position: absolute;
  left: 88px;
  right: 88px;
  bottom: 22px;
  margin: 0;
  text-align: center;
  font-size: clamp(1rem, 4.4vw, 1.2rem);
  font-weight: 700;
  line-height: 1.25;
  color: var(--ink);
  z-index: 1;
}

@keyframes holdPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

@keyframes spinDash {
  to { transform: rotate(360deg); }
}

body.state-ready #stage {
  background:
    linear-gradient(180deg, rgba(255, 248, 220, 0.85), rgba(255, 236, 180, 0.35));
}

body.state-falling #stage {
  background:
    linear-gradient(180deg, rgba(210, 255, 230, 0.9), rgba(160, 240, 200, 0.4));
  box-shadow: 0 0 0 4px rgba(62, 207, 142, 0.25), 0 14px 34px rgba(90, 130, 170, 0.14);
}

.grab-edges {
  pointer-events: none;
  position: absolute;
  inset: 0;
  opacity: 0;
  box-shadow:
    inset 22px 0 30px rgba(62, 207, 142, 0.28),
    inset -22px 0 30px rgba(62, 207, 142, 0.28);
  transition: opacity 120ms ease;
  z-index: 5;
}

.grab-edges.on {
  opacity: 1;
}

.scoreline {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--muted);
  z-index: 3;
}

.toast.hidden,
.result.hidden {
  display: none !important;
}

.toast {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  z-index: 8;
  padding: 14px 18px;
  border-radius: 18px;
  background: #fff;
  border: 3px solid var(--warn);
  color: #8a5a00;
  font-weight: 700;
  font-size: 1.05rem;
  text-align: center;
  min-width: 180px;
  box-shadow: 0 12px 28px rgba(120, 90, 30, 0.18);
}

.toast.bad {
  border-color: var(--accent);
  color: #b33d22;
}

.result {
  position: absolute;
  inset: 0;
  z-index: 9;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 24px;
  background: rgba(255, 250, 240, 0.88);
  backdrop-filter: blur(8px);
}

.result-emoji {
  margin: 0;
  font-size: 2.4rem;
}

.result-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
}

.result-score {
  margin: 4px 0 0;
  font-size: clamp(1.5rem, 7vw, 2.1rem);
  font-weight: 700;
  color: var(--ink);
}

.result-tier {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.result-hint {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}

body.state-false #stage,
body.state-missed #stage {
  animation: softShake 320ms ease;
}

@keyframes softShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* Tablet / large phone: keep a phone-width play column so side holds stay reachable */
@media (min-width: 600px) {
  #wrap {
    max-width: 520px;
    gap: 10px;
    padding:
      max(16px, env(safe-area-inset-top))
      max(18px, env(safe-area-inset-right))
      max(16px, env(safe-area-inset-bottom))
      max(18px, env(safe-area-inset-left));
  }

  #stage {
    border-radius: 32px;
  }

  #ruler {
    width: min(108px, 22vw);
  }

  .bottom-dock {
    height: 112px;
  }

  .catch-zone {
    bottom: 132px;
  }

  .hold-circle {
    width: 84px;
    height: 84px;
    bottom: 12px;
  }

  body.state-falling .hold-circle::before {
    font-size: 0.85rem;
  }

  .action-hint {
    left: 108px;
    right: 108px;
    bottom: 28px;
    font-size: clamp(1.15rem, 2.4vw, 1.4rem);
  }

  .scoreline {
    font-size: 1rem;
  }

  .toast {
    font-size: 1.15rem;
    padding: 16px 22px;
  }

  .result-title {
    font-size: 1.55rem;
  }

  .result-score {
    font-size: clamp(1.8rem, 4vw, 2.4rem);
  }
}

@media (min-width: 900px) {
  #wrap {
    max-width: 560px;
  }

  .hold-circle {
    width: 92px;
    height: 92px;
  }

  .action-hint {
    left: 116px;
    right: 116px;
  }
}

/* Landscape tablet: keep the portrait playfield centered and height-bounded */
@media (orientation: landscape) and (min-width: 700px) {
  #wrap {
    max-width: min(480px, 52vh);
    max-height: 100%;
  }

  .bottom-dock {
    height: 88px;
  }

  .catch-zone {
    bottom: 104px;
  }

  .hold-circle {
    width: 64px;
    height: 64px;
  }

  .action-hint {
    left: 84px;
    right: 84px;
    font-size: 1.05rem;
  }
}
