:root {
  --red: #d62839;
  --red-hi: #ef3b4f;
  --red-lo: #8a1020;
  --green: #1f8a45;
  --green-hi: #2fb36b;
  --green-lo: #0f5a2b;
  --gold: #f4c542;
  --gold-lo: #b8860b;
  --cream: #fff8ec;
  --ink: #2a1a12;
  --night: #0b1d3a;
  --font: 'Fredoka', 'Trebuchet MS', system-ui, sans-serif;
  --title: 'Mountains of Christmas', 'Fredoka', cursive;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

body {
  background-color: var(--night);
  background-image:
    radial-gradient(circle at 12% 18%, rgba(255, 200, 90, 0.10) 0 60px, transparent 61px),
    radial-gradient(circle at 85% 30%, rgba(255, 90, 90, 0.08) 0 80px, transparent 81px),
    radial-gradient(circle at 70% 85%, rgba(120, 200, 255, 0.07) 0 90px, transparent 91px),
    radial-gradient(ellipse at 50% 0%, #1e4a70 0%, #0e2546 45%, #06142a 100%);
}

/* Portrait play area: fills phones, letterboxed (pillarboxed) on wide screens. */
body { background: #040b18 radial-gradient(ellipse at 50% 40%, #0c1d36 0%, #040b18 70%); }
#app {
  position: fixed; top: 0; bottom: 0; left: 0; right: 0; margin: 0 auto;
  width: min(100vw, calc(100vh * 0.62)); width: min(100vw, calc(100dvh * 0.62));
  overflow: hidden; container: app / size;
  background-color: var(--night);
  background-image:
    radial-gradient(circle at 12% 18%, rgba(255, 200, 90, 0.10) 0 60px, transparent 61px),
    radial-gradient(circle at 85% 30%, rgba(255, 90, 90, 0.08) 0 80px, transparent 81px),
    radial-gradient(circle at 70% 85%, rgba(120, 200, 255, 0.07) 0 90px, transparent 91px),
    radial-gradient(ellipse at 50% 0%, #1e4a70 0%, #0e2546 45%, #06142a 100%);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .06), 0 0 60px rgba(0, 0, 0, .6);
}

button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- layers ---------- */
#snow { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 0; }
#game { position: absolute; inset: 0; z-index: 1; display: block; touch-action: none; visibility: hidden; }
#game.on { visibility: visible; }

/* ---------- Christmas lights string ---------- */
#lights {
  position: absolute; top: 0; left: 0; right: 0; height: 34px; z-index: 2; pointer-events: none;
  display: flex; justify-content: space-around; align-items: flex-start;
  background: radial-gradient(ellipse 60% 14px at 50% -6px, transparent 98%, transparent) ;
  transition: opacity .3s;
}
#lights::before {
  content: ''; position: absolute; left: 0; right: 0; top: 6px; height: 2px; background: #1c2a1c; opacity: .8;
}
#lights .b {
  position: relative; width: 10px; height: 16px; margin-top: 7px; border-radius: 50% 50% 50% 50% / 40% 40% 60% 60%;
  background: var(--c); box-shadow: 0 0 12px 3px var(--c);
  animation: twinkle 1.6s ease-in-out infinite; animation-delay: var(--d);
}
#lights .b:nth-child(even) { margin-top: 12px; }
#lights .b::before { content: ''; position: absolute; top: -4px; left: 2px; width: 6px; height: 5px; background: #2b3a2b; border-radius: 2px; }
@keyframes twinkle { 0%, 100% { opacity: 1; } 50% { opacity: .35; box-shadow: 0 0 4px 1px var(--c); } }
body[data-screen="none"] #lights, body[data-screen="countdown"] #lights, body[data-screen="pause"] #lights { opacity: 0; }

/* ---------- HUD ---------- */
#hud {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 12px 8px;
  pointer-events: none; transition: opacity .25s;
}
#hud.hidden { visibility: hidden; opacity: 0; }
.pill {
  display: flex; align-items: center; gap: 8px; padding: 6px 14px; border-radius: 999px;
  background: rgba(8, 20, 42, .72); border: 2px solid rgba(255, 255, 255, .18);
  box-shadow: 0 4px 12px rgba(0, 0, 0, .3); backdrop-filter: blur(4px);
}
.pill .lbl { font-size: .72rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; opacity: .75; }
.pill .val { font-size: 1.35rem; font-weight: 700; line-height: 1; min-width: 1.2em; font-variant-numeric: tabular-nums; }
.pill.score .val { color: var(--gold); }
.pill.score.bump { animation: bump .45s ease-out; }
@keyframes bump { 30% { transform: scale(1.25); } 100% { transform: scale(1); } }
.pill.timer { border-color: rgba(244, 197, 66, .6); padding: 6px 16px; }
.pill.timer .clock { width: 20px; height: 20px; color: var(--gold); }
.pill.timer .val { font-size: 1.6rem; }
.pill.timer.warn { background: rgba(160, 16, 32, .85); border-color: #ff8a8a; animation: pulse .5s ease-in-out infinite alternate; }
.pill.timer.warn .clock { color: #fff; }
@keyframes pulse { to { transform: scale(1.1); box-shadow: 0 0 18px rgba(255, 80, 80, .8); } }
.pill.timer.frozen { opacity: .55; filter: grayscale(.8); animation: none; }
.hud-right { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  pointer-events: auto; width: 42px; height: 42px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .25);
  background: rgba(8, 20, 42, .72); display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; fill: #fff; }
.icon-btn:active { transform: scale(.92); }
@container app (max-width: 440px) {
  #hud { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .hud-right { gap: 6px; }
  .pill { padding: 5px 10px; gap: 6px; }
  .pill .lbl { font-size: .62rem; }
  .pill .val { font-size: 1.15rem; }
  .pill.timer { padding: 5px 12px; }
  .pill.timer .val { font-size: 1.35rem; }
  .pill.timer .clock { width: 17px; height: 17px; }
  .pill.puzzle .lbl { display: none; }
  .pill.puzzle .val::before { content: '#'; opacity: .7; }
  .icon-btn { width: 38px; height: 38px; }
}

/* ---------- screens ---------- */
.screen {
  position: absolute; inset: 0; z-index: 10; display: none; overflow-y: auto; overflow-x: hidden;
  padding: max(40px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}
.screen.active { display: flex; animation: fadein .3s ease-out; }
.screen.overlay { z-index: 20; }
.screen.dim { background: rgba(4, 10, 24, .7); }
@keyframes fadein { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.inner { margin: auto; width: 100%; max-width: 460px; display: flex; flex-direction: column; align-items: center; gap: 18px; }
.inner.narrow { max-width: 400px; }

.title { font-family: var(--title); text-align: center; line-height: .95; text-shadow: 0 4px 0 var(--red-lo), 0 8px 24px rgba(0, 0, 0, .5); }
.title .t1 { display: block; font-size: clamp(2.6rem, 12cqw, 4rem); color: #fff; }
.title .t2 { display: block; font-size: clamp(2.1rem, 9.5cqw, 3.2rem); color: var(--gold); text-shadow: 0 4px 0 #7a4a00, 0 8px 24px rgba(0, 0, 0, .5); }
.heading { font-family: var(--title); font-size: clamp(2.1rem, 9cqw, 2.8rem); text-align: center; color: var(--gold); text-shadow: 0 3px 0 #7a4a00, 0 6px 18px rgba(0, 0, 0, .45); }
.heading.dark { color: var(--red); text-shadow: 0 2px 0 rgba(0, 0, 0, .12); }
.tagline { text-align: center; font-size: 1.05rem; opacity: .9; max-width: 340px; line-height: 1.35; }
.best { font-size: .95rem; opacity: .8; min-height: 1.2em; }
.best b { color: var(--gold); }

.menu-art { position: relative; width: min(62cqw, 250px); aspect-ratio: 1; margin: 4px 0; }
.menu-art canvas { position: absolute; left: 0; top: 0; }
#menu-art-base { width: 100%; height: 100%; filter: drop-shadow(0 10px 18px rgba(0, 0, 0, .5)); }
#menu-art-piece { animation: float 2.6s ease-in-out infinite; filter: drop-shadow(0 14px 12px rgba(0, 0, 0, .45)); }
@keyframes float { 0%, 100% { transform: translate(12%, -8%) rotate(8deg); } 50% { transform: translate(14%, -13%) rotate(4deg); } }

/* ---------- buttons ---------- */
.btn-col { display: flex; flex-direction: column; align-items: stretch; gap: 12px; width: 100%; max-width: 320px; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; text-decoration: none;
  font-weight: 700; font-size: 1.1rem; letter-spacing: .02em; color: #fff;
  border: 0; border-radius: 999px; padding: 12px 20px; min-height: 50px; cursor: pointer;
  background: linear-gradient(var(--red-hi), #c41f33);
  box-shadow: 0 5px 0 var(--red-lo), 0 10px 20px rgba(0, 0, 0, .3);
  text-shadow: 0 2px 0 rgba(0, 0, 0, .25);
  transition: transform .08s, box-shadow .08s, filter .15s;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--red-lo), 0 4px 10px rgba(0, 0, 0, .3); }
.btn.big { font-size: 1.45rem; min-height: 60px; }
.btn.small { min-height: 40px; font-size: .95rem; padding: 8px 18px; }
.btn.green { background: linear-gradient(var(--green-hi), var(--green)); box-shadow: 0 5px 0 var(--green-lo), 0 10px 20px rgba(0, 0, 0, .3); }
.btn.green:active { box-shadow: 0 1px 0 var(--green-lo), 0 4px 10px rgba(0, 0, 0, .3); }
.btn.ghost { background: rgba(255, 255, 255, .1); box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .35); text-shadow: none; font-size: 1rem; }
.btn.ghost:active { transform: translateY(2px); box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .5); }
.btn.ghost.dark { color: var(--ink); background: rgba(42, 26, 18, .06); box-shadow: inset 0 0 0 2px rgba(42, 26, 18, .25); }
.btn:disabled { opacity: .6; pointer-events: none; }

.toggles { display: flex; gap: 10px; }
.toggle {
  cursor: pointer; border: 2px solid rgba(255, 255, 255, .3); background: rgba(8, 20, 42, .5);
  border-radius: 999px; padding: 8px 16px; font-size: .95rem; font-weight: 600; min-height: 40px;
}
.toggle b { color: var(--green-hi); }
.toggle[aria-pressed="false"] b { color: #ff8a8a; }
.toggles.dark .toggle { color: var(--ink); background: rgba(42, 26, 18, .06); border-color: rgba(42, 26, 18, .25); }
.toggles.dark .toggle b { color: var(--green); }
.toggles.dark .toggle[aria-pressed="false"] b { color: var(--red); }

/* ---------- cards ---------- */
.candy {
  width: 100%; padding: 7px; border-radius: 28px;
  background: repeating-linear-gradient(45deg, var(--red) 0 14px, #fff 14px 28px);
  box-shadow: 0 14px 34px rgba(0, 0, 0, .45);
}
.card { background: var(--cream); color: var(--ink); border-radius: 22px; padding: 22px 18px; display: flex; flex-direction: column; gap: 14px; }
.card.center { align-items: center; text-align: center; }

/* ---------- instructions ---------- */
.steps { list-style: none; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; counter-reset: step; }
.steps li {
  counter-increment: step; position: relative;
  background: rgba(255, 248, 236, .96); color: var(--ink); border-radius: 18px; padding: 12px 10px 14px;
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .18), 0 10px 20px rgba(0, 0, 0, .25);
  border: 3px solid var(--gold);
}
.steps li::before {
  content: counter(step); position: absolute; top: -10px; left: -8px; width: 28px; height: 28px; border-radius: 50%;
  background: var(--red); color: #fff; font-weight: 700; display: grid; place-items: center; font-size: .95rem;
  box-shadow: 0 2px 0 var(--red-lo);
}
.steps .ico { width: 64px; height: 64px; background: #13314f; border-radius: 14px; display: grid; place-items: center; overflow: hidden; }
.steps .ico svg { width: 56px; height: 56px; overflow: visible; }
.steps p { font-size: .9rem; line-height: 1.25; font-weight: 500; }
.steps b { color: var(--red); }

/* ---------- countdown ---------- */
.countdown { margin: auto; text-align: center; }
.cd-num {
  font-family: var(--font); font-weight: 700; font-size: clamp(7rem, 34cqw, 12rem); line-height: 1; color: var(--gold);
  text-shadow: 0 6px 0 #7a4a00, 0 0 40px rgba(255, 210, 80, .6);
  animation: cdpop .9s ease-out both;
}
.cd-hint { font-size: 1.2rem; font-weight: 600; margin-top: 8px; text-shadow: 0 2px 8px rgba(0, 0, 0, .6); }
@keyframes cdpop { 0% { transform: scale(2.2); opacity: 0; } 25% { transform: scale(.92); opacity: 1; } 40% { transform: scale(1.05); } 100% { transform: scale(1); opacity: .9; } }

/* ---------- result ---------- */
.result .lbl { font-weight: 600; letter-spacing: .1em; text-transform: uppercase; opacity: .7; font-size: .85rem; }
.final-score { font-size: clamp(4rem, 20cqw, 5.5rem); font-weight: 700; line-height: 1; color: var(--red); text-shadow: 0 4px 0 rgba(122, 15, 27, .25); }
.new-best { color: var(--gold-lo); font-weight: 700; animation: pulse2 .8s ease-in-out infinite alternate; }
@keyframes pulse2 { to { transform: scale(1.06); } }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: 100%; }
.stats div { background: rgba(31, 138, 69, .1); border-radius: 14px; padding: 10px 4px; display: flex; flex-direction: column; }
.stats b { font-size: 1.5rem; color: var(--green); }
.stats span { font-size: .8rem; opacity: .7; font-weight: 600; }
.brand-msg { font-size: .95rem; opacity: .8; }

/* ---------- form (gift tag) ---------- */
.tag {
  position: relative; width: 100%; background: var(--cream); color: var(--ink);
  padding: 34px 20px 22px; border-radius: 18px 18px 22px 22px;
  clip-path: polygon(14% 0, 86% 0, 100% 9%, 100% 100%, 0 100%, 0 9%);
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 14px 34px rgba(0, 0, 0, .45);
  background-image: linear-gradient(90deg, transparent 0 calc(100% - 10px), rgba(214, 40, 57, .9) 0), linear-gradient(90deg, rgba(214, 40, 57, .9) 0 10px, transparent 0);
}
.tag-hole { position: absolute; top: 12px; left: 50%; width: 18px; height: 18px; margin-left: -9px; border-radius: 50%; background: var(--night); box-shadow: inset 0 0 0 3px var(--gold); }
.tag-score { text-align: center; font-weight: 600; font-size: 1.05rem; }
.tag-score b { color: var(--red); font-size: 1.4rem; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: .9rem; }
.field input {
  font: inherit; font-size: 1.05rem; font-weight: 500; padding: 12px 14px; border-radius: 12px;
  border: 2px solid rgba(42, 26, 18, .2); background: #fff; color: var(--ink); user-select: text; -webkit-user-select: text;
}
.field input:focus { outline: none; border-color: var(--green); box-shadow: 0 0 0 3px rgba(31, 138, 69, .2); }
.field input.bad { border-color: var(--red); }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: .82rem; line-height: 1.35; cursor: pointer; }
.check input { width: 22px; height: 22px; flex: none; accent-color: var(--green); margin-top: 1px; }
.form-error { color: var(--red); font-weight: 600; font-size: .9rem; min-height: 1.2em; text-align: center; }
.tag .btn-col { align-self: center; }

/* ---------- leaderboard ---------- */
.board { gap: 10px; }
.board-you { text-align: center; font-weight: 600; }
.board-you b { color: var(--red); }
.lb-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.lb-list li {
  display: grid; grid-template-columns: 2.2em 1fr auto; align-items: center; gap: 8px;
  padding: 9px 12px; border-radius: 12px; background: rgba(42, 26, 18, .05); font-weight: 600;
}
.lb-list li .rk { font-weight: 700; color: rgba(42, 26, 18, .55); text-align: center; }
.lb-list li:nth-child(1) .rk { color: #c99a00; }
.lb-list li:nth-child(2) .rk { color: #8a9aa8; }
.lb-list li:nth-child(3) .rk { color: #b06a2c; }
.lb-list li .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-list li .sc { font-weight: 700; color: var(--green); font-variant-numeric: tabular-nums; }
.lb-list li.me { background: linear-gradient(90deg, rgba(244, 197, 66, .45), rgba(244, 197, 66, .2)); box-shadow: inset 0 0 0 2px var(--gold); }
.lb-list li.gap { background: none; justify-content: center; display: block; text-align: center; padding: 0; opacity: .5; }
.lb-status { text-align: center; font-size: .92rem; opacity: .75; }
.board .btn { align-self: center; }

@container app (max-height: 640px) {
  .menu-art { width: min(40cqw, 170px); }
  .inner { gap: 12px; }
  .btn.big { min-height: 52px; font-size: 1.25rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
