:root {
  --bg1: #2a1b5e;
  --bg2: #10264f;
  --text: #ffffff;
  --muted: #c9c3ef;
  --panel: rgba(255, 255, 255, 0.10);
  --good: #22c55e;
  --bad: #ef4444;
  --gold: #ffd23f;
  --tile0: #d6336c;
  --tile1: #e8590c;
  --tile2: #0c8599;
  --tile3: #7048e8;
  --radius: 22px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Author `display` rules (e.g. .choices { display: grid }) would otherwise beat [hidden]. */
[hidden] { display: none !important; }

/* Registered so the gradient colours can transition as the streak heats up. */
@property --bg1 { syntax: "<color>"; inherits: true; initial-value: #2a1b5e; }
@property --bg2 { syntax: "<color>"; inherits: true; initial-value: #10264f; }

html, body {
  margin: 0;
  height: 100%;
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

html { background: var(--bg2); }

body {
  background: linear-gradient(160deg, var(--bg1), var(--bg2));
  transition: --bg1 900ms ease-out, --bg2 900ms ease-out;
}
/* Losing the streak cools down a bit more slowly, so the drop is visible. */
body.cooling { transition-duration: 1600ms; }

/* Bright backgrounds: keep white text readable and warm up the muted tone. */
body.hot { --muted: #fff4e0; --panel: rgba(60, 0, 0, 0.22); }
body.hot .task,
body.hot .bottom,
body.hot .best,
body.hot .feedback.bad { text-shadow: 0 2px 8px rgba(90, 0, 0, 0.55), 0 1px 0 rgba(90, 0, 0, 0.5); }
body.hot .mode-btn[aria-selected="false"] { color: #fff4e0; }
body.hot .slot { color: #fff; border-color: rgba(255, 255, 255, 0.6); }
/* Orange/pink tiles would melt into a fiery background without an outline. */
body.hot .tile:not(.right) { box-shadow: 0 6px 0 rgba(0, 0, 0, 0.28), inset 0 0 0 3px rgba(255, 255, 255, 0.45); }

/* At the peak, add a slow flickering fire glow from the bottom. */
body.peak::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse 120% 60% at 50% 110%, rgba(255, 230, 120, 0.55), transparent 70%);
  animation: fire 1.4s ease-in-out infinite alternate;
}
@keyframes fire {
  0% { opacity: 0.55; transform: scaleY(1); }
  50% { opacity: 0.9; transform: scaleY(1.06); }
  100% { opacity: 0.65; transform: scaleY(0.97); }
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}

button:focus-visible { outline: 4px solid var(--gold); outline-offset: 3px; }

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

#app {
  position: relative;
  z-index: 1;
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto auto 1fr auto auto;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px calc(env(safe-area-inset-bottom) + 10px);
}

body.flash-good #app { animation: flash-good 600ms ease-out; }
@keyframes flash-good {
  0% { background: radial-gradient(circle at 50% 40%, rgba(255, 210, 63, 0.35), transparent 70%); }
  100% { background: transparent; }
}

/* ---------- top bar & streak ---------- */
.top {
  display: grid;
  grid-template-columns: 48px 1fr 48px;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 48px; height: 48px;
  border: 0; border-radius: 50%;
  background: var(--panel);
  font-size: 1.4rem;
}

.best {
  justify-self: end;
  font-size: 1rem;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
}

.streak {
  justify-self: center;
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 112px;
  justify-content: center;
  padding: 8px 20px;
  border-radius: 999px;
  background: var(--panel);
  font-size: 1.9rem;
  font-weight: 800;
  transition: background 300ms, transform 300ms;
}

.streak .flame { filter: grayscale(1) opacity(0.45); transition: filter 300ms, transform 300ms; }
.streak.active .flame { filter: none; }

.streak.bump { animation: bump 380ms cubic-bezier(.3, 1.6, .5, 1); }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.3); } 100% { transform: scale(1); } }

.streak.broken { animation: broken 500ms ease-in-out; }
@keyframes broken {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px) rotate(-4deg); }
  40% { transform: translateX(8px) rotate(4deg); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

/* glow from 3 */
.streak.glow {
  background: rgba(255, 180, 40, 0.25);
  animation: glow 1.6s ease-in-out infinite;
}
.streak.glow .flame { animation: flicker 0.9s ease-in-out infinite; }
@keyframes glow {
  0%, 100% { box-shadow: 0 0 10px 2px rgba(255, 190, 50, 0.6), 0 0 26px 6px rgba(255, 120, 0, 0.35); }
  50% { box-shadow: 0 0 18px 6px rgba(255, 210, 63, 0.9), 0 0 44px 14px rgba(255, 120, 0, 0.5); }
}
@keyframes flicker {
  0%, 100% { transform: scale(1) rotate(-4deg); }
  50% { transform: scale(1.18) rotate(5deg); }
}

/* special effect from 5: rotating rainbow ring + sparkles */
.streak.super {
  background: #1b1240;
  isolation: isolate;
  animation: glow-rainbow 2s linear infinite;
}
.streak.super::before {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: inherit;
  padding: 4px;
  background: conic-gradient(from var(--angle, 0deg), #ff4d6d, #ffd23f, #3ddc97, #4dabf7, #b197fc, #ff4d6d);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  animation: spin 2s linear infinite;
  z-index: -1;
}
.streak.super::after {
  content: "✨";
  position: absolute;
  right: -14px; top: -14px;
  font-size: 1.3rem;
  animation: twinkle 1.2s ease-in-out infinite;
}
.streak.super .streak-count {
  background: linear-gradient(90deg, #ffd23f, #ff4d6d, #b197fc, #4dabf7, #3ddc97, #ffd23f);
  background-size: 300% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: rainbow-text 2.4s linear infinite;
}
.streak.super .flame { animation: flicker 0.6s ease-in-out infinite; font-size: 2.2rem; }

@property --angle { syntax: "<angle>"; initial-value: 0deg; inherits: false; }
@keyframes spin { to { --angle: 360deg; } }
@keyframes rainbow-text { to { background-position: 300% 0; } }
@keyframes twinkle { 0%, 100% { opacity: 0.4; transform: scale(0.8) rotate(0); } 50% { opacity: 1; transform: scale(1.3) rotate(20deg); } }
@keyframes glow-rainbow {
  0% { box-shadow: 0 0 22px 6px rgba(255, 77, 109, 0.7); }
  33% { box-shadow: 0 0 22px 6px rgba(255, 210, 63, 0.7); }
  66% { box-shadow: 0 0 22px 6px rgba(77, 171, 247, 0.7); }
  100% { box-shadow: 0 0 22px 6px rgba(255, 77, 109, 0.7); }
}

/* ---------- mode switch ---------- */
.modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 4px;
  border-radius: 999px;
  background: var(--panel);
}
.mode-btn {
  border: 0;
  border-radius: 999px;
  padding: 10px 6px;
  background: transparent;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--muted);
}
.mode-btn[aria-selected="true"] { background: #fff; color: #2a1b5e; }

/* ---------- task ---------- */
.task-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
}
.task {
  display: flex;
  align-items: baseline;
  gap: 0.25em;
  font-size: clamp(3.2rem, 17vw, 5.4rem);
  font-weight: 800;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
}
.task .eq { color: var(--muted); }
.slot {
  display: inline-block;
  min-width: 1.6em;
  text-align: center;
  border-bottom: 6px solid rgba(255, 255, 255, 0.35);
  color: var(--gold);
}
.task.pop { animation: pop 450ms cubic-bezier(.3, 1.6, .5, 1); }
.task.enter { animation: enter 300ms ease-out; }
.task.good .slot { color: var(--good); border-color: var(--good); }
.task.bad .slot { color: var(--bad); border-color: var(--bad); }
.task.shake { animation: broken 450ms ease-in-out; }
@keyframes pop { 0% { transform: scale(1); } 45% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes enter { from { opacity: 0; transform: translateY(14px) scale(0.92); } to { opacity: 1; transform: none; } }

.feedback {
  min-height: 2.2em;
  margin: 10px 0 0;
  font-size: 1.6rem;
  font-weight: 800;
  text-align: center;
}
.feedback.good {
  background: linear-gradient(90deg, #ffd23f, #ff8fab, #8ce99a, #74c0fc);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: pop 450ms cubic-bezier(.3, 1.6, .5, 1);
}
.feedback.bad { color: #ffc9c9; }

/* ---------- choice tiles ---------- */
.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.tile {
  height: clamp(84px, 15dvh, 130px);
  border: 0;
  border-radius: var(--radius);
  font-size: clamp(2.2rem, 11vw, 3rem);
  font-weight: 800;
  color: #fff;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.28);
  transition: transform 90ms, box-shadow 90ms, opacity 250ms, filter 250ms;
}
.tile:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.28); }
.tile.c0 { background: var(--tile0); }
.tile.c1 { background: var(--tile1); }
.tile.c2 { background: var(--tile2); }
.tile.c3 { background: var(--tile3); }
.tile.dim { opacity: 0.35; filter: grayscale(0.7); }
.tile.right {
  background: var(--good);
  animation: pop 450ms cubic-bezier(.3, 1.6, .5, 1);
  box-shadow: 0 0 0 5px #fff, 0 0 30px 8px rgba(34, 197, 94, 0.7);
}
.tile.wrong { background: var(--bad); animation: broken 450ms ease-in-out; }

/* ---------- keypad ---------- */
.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.keypad[hidden] { display: none; }
.key {
  height: clamp(54px, 8.6dvh, 76px);
  border: 0;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.16);
  font-size: 2rem;
  font-weight: 800;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
  transition: transform 80ms, box-shadow 80ms, background 150ms;
}
.key:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.3); background: rgba(255, 255, 255, 0.3); }
.key-del { background: rgba(255, 255, 255, 0.08); }
.key-ok { background: var(--good); }
.key-ok:disabled { background: rgba(34, 197, 94, 0.3); color: rgba(255, 255, 255, 0.5); cursor: default; }

/* ---------- bottom ---------- */
.bottom {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--muted);
}
.timer { font-variant-numeric: tabular-nums; min-width: 4.5em; }
.timer.stopped { color: #fff; }

/* ---------- milestone banner ---------- */
.banner {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-size: clamp(2.6rem, 13vw, 4.4rem);
  font-weight: 900;
  text-align: center;
  color: #fff;
  text-shadow: 0 4px 0 #d6336c, 0 0 30px rgba(255, 210, 63, 0.9);
  opacity: 0;
  z-index: 20;
}
.banner.show { animation: banner 1.6s cubic-bezier(.2, 1.4, .4, 1) forwards; }
@keyframes banner {
  0% { opacity: 0; transform: scale(0.3) rotate(-12deg); }
  25% { opacity: 1; transform: scale(1.15) rotate(3deg); }
  40% { transform: scale(1) rotate(0); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: scale(1.1) translateY(-40px); }
}

#fx {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
}

/* ---------- settings ---------- */
dialog {
  width: min(92vw, 440px);
  border: 0;
  border-radius: 24px;
  padding: 0;
  background: #1d1546;
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.settings { display: flex; flex-direction: column; gap: 18px; padding: 22px; }
.settings h2 { margin: 0; font-size: 1.5rem; }
fieldset { border: 0; margin: 0; padding: 0; }
legend { font-weight: 700; margin-bottom: 10px; }
.row-chips { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.row-chip {
  height: 48px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-radius: 14px;
  background: transparent;
  font-size: 1.2rem;
  font-weight: 800;
}
.row-chip[aria-pressed="true"] { background: var(--gold); border-color: var(--gold); color: #2a1b5e; }
.row-actions { margin-top: 8px; }
.chip-action { border: 0; background: transparent; color: var(--gold); font-weight: 700; padding: 6px 0; }
.toggle, .lang label { display: flex; align-items: center; gap: 10px; font-size: 1.1rem; }
.lang { display: flex; gap: 22px; }
.toggle input, .lang input { width: 22px; height: 22px; accent-color: var(--gold); }
.primary, .danger {
  border: 0;
  border-radius: 14px;
  padding: 14px;
  font-size: 1.1rem;
  font-weight: 800;
}
.primary { background: var(--gold); color: #2a1b5e; }
.danger { background: transparent; border: 2px solid rgba(255, 120, 120, 0.6); color: #ffc9c9; }

/* Short phones: tighten vertical spacing */
@media (max-height: 640px) {
  #app { gap: 8px; }
  .streak { font-size: 1.5rem; padding: 6px 16px; }
  .feedback { font-size: 1.3rem; margin-top: 4px; }
}

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