/* Reset */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --paper: #e4d6b8;
  --paper-dark: #cdbb95;
  --ink: #2a1e14;
  --accent: #a8421a;
  --chalk-white: #f7ecd3;
  --font-chalk: "Chalkduster", "Bradley Hand", "Segoe Script", "Marker Felt",
                "Comic Sans MS", cursive;
}

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-chalk);
  position: relative;
  /* Construction-paper noise: two stacked radial-dot patterns at odd sizes. */
  background-image:
    radial-gradient(circle at 20% 30%, rgba(0,0,0,0.05) 0, transparent 1.5px),
    radial-gradient(circle at 80% 60%, rgba(0,0,0,0.04) 0, transparent 1.5px),
    radial-gradient(circle at 50% 85%, rgba(255,255,255,0.06) 0, transparent 2px),
    radial-gradient(circle at 10% 70%, rgba(255,255,255,0.05) 0, transparent 1.5px);
  background-size: 13px 13px, 17px 17px, 23px 23px, 29px 29px;
}

#game {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  display: block;
}

/* ── HUD (score, top bar) ── */
#hud {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 20px;
  right: 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 5;
  mix-blend-mode: multiply;
}
#step-count {
  font-size: clamp(40px, 10vw, 72px);
  line-height: 0.9;
  transform: rotate(-3deg);
  color: var(--ink);
  letter-spacing: 0.02em;
}
#timer {
  font-size: clamp(28px, 7vw, 44px);
  line-height: 0.9;
  opacity: 0.72;
  transform: rotate(2deg);
  letter-spacing: 0.02em;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
#timer.urgent { color: var(--accent); opacity: 1; }
#timer.pending { opacity: 0.4; font-style: italic; }
body[data-state="idle"] #hud,
body[data-state="result"] #hud { opacity: 0; }

/* Reset button — small, low-key chalk pill at the bottom corner. Only
   visible during a run so it doesn't compete with the start/result panels. */
#btn-reset {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  right: max(16px, env(safe-area-inset-right));
  padding: 6px 14px;
  font-size: clamp(13px, 3.4vw, 16px);
  letter-spacing: 0.08em;
  min-height: 36px;
  opacity: 0.55;
  transform: rotate(2deg);
  z-index: 6;
  display: none;
  background: rgba(228, 214, 184, 0.7);
}
#btn-reset:hover { background: var(--ink); color: var(--paper); opacity: 1; }
#btn-reset:active { transform: rotate(2deg) scale(0.94); }
body[data-state="playing"] #btn-reset { display: inline-block; }

/* Time-bonus toast — pops in next to the timer, drifts up as it fades. */
.bonus-toast {
  position: fixed;
  top: max(56px, calc(env(safe-area-inset-top) + 40px));
  right: 24px;
  font-family: var(--font-chalk);
  font-size: clamp(28px, 7vw, 44px);
  color: var(--accent);
  font-weight: bold;
  letter-spacing: 0.02em;
  transform: rotate(4deg) translateY(-6px) scale(0.7);
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.32s cubic-bezier(.2, 1.4, .4, 1);
  pointer-events: none;
  z-index: 6;
  text-shadow: 2px 2px 0 rgba(228, 214, 184, 0.8);
}
.bonus-toast.show {
  opacity: 1;
  transform: rotate(4deg) translateY(0) scale(1);
}
.bonus-toast.hide {
  opacity: 0;
  transform: rotate(4deg) translateY(-32px) scale(1);
  transition: opacity 0.55s ease, transform 0.55s ease;
}

/* "BARRELS!" warning — flashes center-screen before the first barrel falls.
   Holds visible at peak while pulsing scale, then fades. */
.barrels-warning {
  position: fixed;
  top: 32%;
  left: 50%;
  transform-origin: 50% 50%;
  font-family: var(--font-chalk);
  font-size: clamp(64px, 16vw, 140px);
  color: var(--accent);
  font-weight: bold;
  letter-spacing: 0.04em;
  pointer-events: none;
  z-index: 11;                 /* above panels just in case */
  white-space: nowrap;
  text-shadow:
    3px 3px 0 rgba(42, 30, 20, 0.25),
    0 0 22px rgba(168, 66, 26, 0.45);
  animation: barrels-flash 1.6s cubic-bezier(.2, .8, .2, 1) forwards;
}
@keyframes barrels-flash {
  0%   { opacity: 0; transform: translateX(-50%) rotate(-3deg) scale(0.55); }
  10%  { opacity: 1; transform: translateX(-50%) rotate(-5deg) scale(1.18); }
  22%  { opacity: 1; transform: translateX(-50%) rotate(-1deg) scale(1.00); }
  35%  { opacity: 1; transform: translateX(-50%) rotate(-4deg) scale(1.14); }
  50%  { opacity: 1; transform: translateX(-50%) rotate(-2deg) scale(1.02); }
  65%  { opacity: 1; transform: translateX(-50%) rotate(-4deg) scale(1.12); }
  80%  { opacity: 1; transform: translateX(-50%) rotate(-3deg) scale(1.06); }
  100% { opacity: 0; transform: translateX(-50%) rotate(-3deg) scale(1.30); }
}

/* "ZERO G!" warning — fires at the start of the zero-gravity power-up.
   Same shape as the barrels warning but in a cool/floaty teal so it reads
   as a different kind of event. */
.zero-g-warning {
  position: fixed;
  top: 32%;
  left: 50%;
  transform-origin: 50% 50%;
  font-family: var(--font-chalk);
  font-size: clamp(64px, 16vw, 140px);
  color: #2f6e7a;
  font-weight: bold;
  letter-spacing: 0.04em;
  pointer-events: none;
  z-index: 11;
  white-space: nowrap;
  text-shadow:
    3px 3px 0 rgba(228, 214, 184, 0.8),
    0 0 26px rgba(47, 110, 122, 0.5);
  animation: zero-g-flash 1.6s cubic-bezier(.2, .8, .2, 1) forwards;
}
@keyframes zero-g-flash {
  0%   { opacity: 0; transform: translateX(-50%) rotate(2deg) scale(0.55); }
  10%  { opacity: 1; transform: translateX(-50%) rotate(4deg) scale(1.18); }
  22%  { opacity: 1; transform: translateX(-50%) rotate(0deg) scale(1.00); }
  40%  { opacity: 1; transform: translateX(-50%) rotate(3deg) scale(1.10); }
  60%  { opacity: 1; transform: translateX(-50%) rotate(-1deg) scale(1.02); }
  80%  { opacity: 1; transform: translateX(-50%) rotate(2deg) scale(1.06); }
  100% { opacity: 0; transform: translateX(-50%) rotate(2deg) scale(1.30); }
}

/* Persistent zero-G HUD — small countdown shown at top center while the
   power-up is active. Hidden otherwise. */
#zero-g-hud {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%) rotate(-1deg);
  font-family: var(--font-chalk);
  font-size: clamp(22px, 5.5vw, 34px);
  color: #2f6e7a;
  letter-spacing: 0.06em;
  z-index: 6;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
  text-shadow: 2px 2px 0 rgba(228, 214, 184, 0.6);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
#zero-g-hud.active { opacity: 0.95; animation: zero-g-bob 1.6s ease-in-out infinite; }
@keyframes zero-g-bob {
  0%, 100% { transform: translateX(-50%) translateY(0) rotate(-1deg); }
  50%      { transform: translateX(-50%) translateY(-3px) rotate(1deg); }
}
body[data-state="idle"] #zero-g-hud,
body[data-state="result"] #zero-g-hud { opacity: 0; }

/* ── Panels ── */
.panel { display: none; }
body[data-state="idle"]   #panel-idle,
body[data-state="result"] #panel-result {
  display: flex;
  position: fixed; inset: 0;
  z-index: 10;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 36px 24px;
  text-align: center;
  pointer-events: none;
}
body[data-state="result"] #panel-result {
  justify-content: flex-start;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
  overflow-y: auto;
  pointer-events: auto;
}
.panel > * { pointer-events: auto; }

/* Idle panel */
#panel-idle h1 {
  font-size: clamp(56px, 16vw, 128px);
  line-height: 0.85;
  letter-spacing: -0.02em;
  transform: rotate(-3deg);
  margin-bottom: 14px;
  text-shadow: 2px 2px 0 rgba(168, 66, 26, 0.12);
}
#panel-idle h1 .accent {
  color: var(--accent);
  transform: rotate(1deg);
  display: inline-block;
}
.pitch {
  font-size: clamp(15px, 4vw, 21px);
  line-height: 1.7;
  opacity: 0.72;
  max-width: 340px;
  margin-bottom: 14px;
}
.hint {
  font-size: clamp(11px, 2.8vw, 14px);
  opacity: 0.45;
  letter-spacing: 0.1em;
  margin-top: 14px;
}
.hint-bonus {
  margin-top: 4px;
  color: var(--accent);
  opacity: 0.65;
  letter-spacing: 0.06em;
}

/* Buttons — hand-drawn uneven pill */
button {
  background: none;
  border: 2.5px solid var(--ink);
  font-family: var(--font-chalk);
  color: var(--ink);
  padding: 12px 32px;
  font-size: clamp(20px, 5vw, 28px);
  cursor: pointer;
  letter-spacing: 0.06em;
  border-radius: 22px 4px 18px 8px / 8px 20px 4px 18px;
  transform: rotate(-1.5deg);
  min-height: 48px;
  transition: transform 0.12s ease, background 0.12s ease;
}
button:hover { background: var(--ink); color: var(--paper); }
button:active { transform: rotate(-1.5deg) scale(0.94); }

/* Result panel */
#result-count {
  font-size: clamp(110px, 30vw, 200px);
  line-height: 0.9;
  color: var(--accent);
  transform: rotate(-2.5deg);
  text-shadow: 3px 3px 0 rgba(42, 30, 20, 0.08);
}
#result-label {
  font-size: clamp(18px, 5vw, 28px);
  opacity: 0.72;
  transform: rotate(1deg);
}
#result-best {
  font-size: clamp(13px, 3.2vw, 17px);
  opacity: 0.5;
  margin: 4px 0 16px;
  letter-spacing: 0.08em;
  min-height: 1.2em;
}
#result-best.new-best { color: var(--accent); opacity: 0.95; font-weight: bold; }

/* Scoreboard — chalkboard pinned to the wall */
.scoreboard {
  width: min(360px, 100%);
  background: rgba(42, 30, 20, 0.04);
  border: 2.5px dashed rgba(42, 30, 20, 0.35);
  border-radius: 18px 6px 14px 8px / 10px 22px 6px 18px;
  padding: 14px 16px 16px;
  color: var(--ink);
  transform: rotate(-0.75deg);
}
.scoreboard__header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  font-size: clamp(15px, 3.6vw, 18px);
  letter-spacing: 0.03em;
  padding-bottom: 8px;
  border-bottom: 1.5px dashed rgba(42, 30, 20, 0.3);
}
.scoreboard__status {
  font-size: clamp(11px, 2.6vw, 13px);
  color: var(--accent);
  opacity: 0.85;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.scoreboard__list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: grid;
  gap: 2px;
}
.scoreboard__row,
.scoreboard__empty {
  display: grid;
  grid-template-columns: 2.2rem 1fr 3.5rem;
  gap: 10px;
  align-items: baseline;
  font-size: clamp(16px, 3.8vw, 20px);
  letter-spacing: 0.04em;
  text-align: left;
  padding: 2px 4px;
}
.scoreboard__row span:first-child { opacity: 0.45; font-size: 0.85em; }
.scoreboard__row span:last-child { text-align: right; font-weight: bold; }
.scoreboard__row.is-current {
  color: var(--accent);
  background: rgba(168, 66, 26, 0.08);
  border-radius: 8px 3px 6px 4px / 4px 8px 3px 6px;
}
.scoreboard__empty {
  grid-template-columns: 1fr;
  text-align: center;
  opacity: 0.55;
  font-style: italic;
}
.initials-form {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1.5px dashed rgba(42, 30, 20, 0.3);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.initials-form[hidden] { display: none; }
.initials-form label {
  font-size: clamp(11px, 2.8vw, 13px);
  letter-spacing: 0.14em;
  color: var(--accent);
  text-transform: uppercase;
  opacity: 0.85;
}
.initials-form__row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.initials-form input {
  flex: 1;
  background: rgba(247, 236, 211, 0.5);
  border: 2.5px solid var(--ink);
  border-radius: 12px 4px 10px 6px / 6px 12px 4px 10px;
  padding: 8px 12px;
  font-family: var(--font-chalk);
  font-size: clamp(20px, 5vw, 26px);
  text-align: center;
  letter-spacing: 0.3em;
  color: var(--ink);
  text-transform: uppercase;
  min-width: 0;
}
.initials-form input:focus { outline: none; background: var(--chalk-white); }
.initials-form__save {
  padding: 8px 16px;
  font-size: clamp(15px, 4vw, 19px);
  transform: rotate(1deg);
  border-radius: 12px 4px 10px 6px / 6px 12px 4px 10px;
  min-height: 0;
}
.initials-form__skip {
  align-self: center;
  background: none;
  border: none;
  padding: 4px 8px;
  margin-top: 2px;
  font-family: var(--font-chalk);
  font-size: clamp(12px, 3vw, 14px);
  color: var(--ink);
  opacity: 0.55;
  text-decoration: underline;
  min-height: 0;
  cursor: pointer;
}
.initials-form__skip:hover {
  background: none;
  color: var(--accent);
  opacity: 0.9;
}

.row {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}
#btn-again {
  border: none;
  opacity: 0.68;
  text-decoration: underline;
  padding: 8px 12px;
  font-size: clamp(16px, 4vw, 19px);
  transform: none;
  min-height: 44px;
}
#btn-again:hover { background: none; color: var(--ink); opacity: 1; transform: none; }
