:root {
  --bg: #f5f8ff;
  --surface: #ffffff;
  --ink: #1f2f4a;
  --muted: #5b6d8a;
  --primary: #0f85b8;
  --primary-dark: #0a5f83;
  --accent: #ff8a3d;
  --accent-2: #ffd05a;
  --good: #1f9d73;
  --bad: #db4e5a;
  --ring: rgba(15, 133, 184, 0.32);
  --shadow-soft: 0 10px 28px rgba(31, 47, 74, 0.12);
  --shadow-pop: 0 8px 0 rgba(13, 36, 65, 0.16);
}

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

html,
body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  font-family: "Nunito", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 14% 18%, rgba(255, 208, 90, 0.38), transparent 24%),
    radial-gradient(circle at 84% 15%, rgba(79, 201, 255, 0.34), transparent 30%),
    radial-gradient(circle at 15% 86%, rgba(126, 239, 193, 0.34), transparent 26%),
    linear-gradient(180deg, #f7fbff 0%, #f2f7ff 100%);
  position: relative;
}

.bg-shape {
  position: fixed;
  z-index: -1;
  border-radius: 999px;
  filter: none;
  opacity: 0.55;
  pointer-events: none;
}

.bg-shape-a {
  width: 240px;
  height: 240px;
  right: -80px;
  top: 22%;
  background: #ffc27f;
}

.bg-shape-b {
  width: 210px;
  height: 210px;
  left: -75px;
  bottom: 18%;
  background: #90e9ff;
}

.app {
  width: min(980px, 94vw);
  margin: 1.2rem auto 2rem;
  display: grid;
  gap: 1rem;
}

.card {
  background: var(--surface);
  border: 2px solid rgba(22, 58, 100, 0.08);
  border-radius: 22px;
  box-shadow: var(--shadow-soft);
}

.hero {
  padding: 1rem 1rem 1.15rem;
  background:
    linear-gradient(140deg, rgba(79, 201, 255, 0.14), rgba(255, 208, 90, 0.14)),
    var(--surface);
}

.badge {
  display: inline-block;
  font-size: 0.84rem;
  font-weight: 800;
  color: #744c00;
  background: #ffe29c;
  border-radius: 999px;
  padding: 0.26rem 0.72rem;
  margin: 0;
}

h1,
h2,
h3 {
  font-family: "Baloo 2", cursive;
  letter-spacing: 0.02em;
  margin: 0;
}

h1 {
  font-size: clamp(2.05rem, 4vw, 2.7rem);
  color: #15345e;
}

h2 {
  font-size: clamp(1.45rem, 3vw, 1.95rem);
  color: #17395f;
}

h3 {
  font-size: 1.19rem;
  color: #193e66;
}

.subtitle {
  color: var(--muted);
  margin: 0.1rem 0 0;
  font-weight: 700;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.44rem;
  padding: 0.5rem;
  background:
    linear-gradient(120deg, rgba(255, 208, 90, 0.17), rgba(79, 201, 255, 0.16)),
    var(--surface);
}

.tab {
  border: 0;
  background: rgba(255, 255, 255, 0.9);
  color: #2c4266;
  border-radius: 14px;
  font-weight: 900;
  padding: 0.67rem 0.62rem;
  cursor: pointer;
  transition: background-color 130ms ease, color 130ms ease;
}

.tab.is-active {
  background: linear-gradient(145deg, #1fa5d6, #0f7ca8);
  color: #ffffff;
  box-shadow: 0 6px 14px rgba(15, 124, 168, 0.3);
}

.tab:hover {
  background: #f4f9ff;
}

.tab.is-active:hover {
  background: linear-gradient(145deg, #1fa5d6, #0f7ca8);
  color: #ffffff;
}

.tab:focus-visible,
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
.learn-mode-btn:focus-visible,
.table-key:focus-visible,
.multiplier-key:focus-visible,
.table-toggle:focus-visible,
.mode-btn:focus-visible,
.castle-difficulty-btn:focus-visible,
.keypad-key:focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: 1px;
}

.panel {
  display: none;
  padding: 1rem;
  animation: none;
}

.panel.is-active {
  display: block;
}

@keyframes panel-enter {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.panel-head p {
  color: var(--muted);
  margin: 0.18rem 0 0.95rem;
  font-weight: 700;
}

.learn-controls,
.train-top,
.challenge-head {
  display: flex;
  flex-wrap: wrap;
  gap: 0.72rem;
  align-items: center;
}

.learn-mode-switch {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.2rem 0 0.9rem;
}

.learn-mode-btn {
  border: 2px solid rgba(36, 57, 90, 0.16);
  border-radius: 14px;
  min-height: 44px;
  padding: 0.46rem 0.75rem;
  font: inherit;
  font-weight: 900;
  color: #1f365a;
  background: #f8fbff;
  cursor: pointer;
}

.learn-mode-btn.is-active {
  background: linear-gradient(145deg, #7fdcff, #57bde7);
  border-color: #3ba8d5;
  color: #083b56;
}

.learn-subpanel {
  display: none;
}

.learn-subpanel.is-active {
  display: block;
}

.learn-slider-wrap {
  margin-top: 0.72rem;
  display: grid;
  gap: 0.35rem;
  color: #294b74;
  font-weight: 800;
}

#learn-multiplier-slider {
  width: min(430px, 100%);
  padding: 0;
  border: 0;
  min-height: 26px;
}

.picker-title {
  margin: 0 0 0.36rem;
  font-weight: 800;
  color: #294b74;
}

.table-keypad,
.multiplier-keypad {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.45rem;
  max-width: 430px;
}

.table-key,
.multiplier-key,
.table-toggle {
  border: 2px solid rgba(36, 57, 90, 0.16);
  border-radius: 14px;
  min-height: 46px;
  font: inherit;
  font-weight: 900;
  color: #1f365a;
  background: #f8fbff;
  cursor: pointer;
  transition: background-color 110ms ease, border-color 110ms ease;
}

.table-key:hover,
.multiplier-key:hover,
.table-toggle:hover {
  background: #eef7ff;
}

.table-key.is-active,
.multiplier-key.is-active,
.table-toggle.is-active {
  background: linear-gradient(145deg, #7fdcff, #57bde7);
  border-color: #3ba8d5;
  color: #083b56;
}

.table-key-ten,
.multiplier-key-ten {
  grid-column: span 2;
}

label {
  font-weight: 800;
  color: #27466e;
  display: grid;
  gap: 0.24rem;
}

input,
select {
  border: 2px solid rgba(36, 57, 90, 0.2);
  border-radius: 12px;
  padding: 0.55rem 0.65rem;
  min-height: 42px;
  font: inherit;
  color: var(--ink);
}

.equation {
  margin-top: 0.85rem;
  background: linear-gradient(120deg, rgba(79, 201, 255, 0.16), rgba(255, 208, 90, 0.2));
  border: 2px solid rgba(40, 76, 118, 0.16);
  border-radius: 14px;
  padding: 0.82rem;
  font-size: 1.24rem;
  font-weight: 900;
  color: #163a62;
}

.array-grid {
  margin-top: 0.74rem;
  display: grid;
  gap: 0.36rem;
  justify-content: start;
}

.array-grid-row {
  display: grid;
  gap: 0.36rem;
}

.array-dot {
  width: 20px;
  height: 20px;
  border-radius: 7px;
  background: linear-gradient(140deg, #9ee8ff, #33a8d7);
  border: 1px solid rgba(24, 79, 120, 0.25);
}

.table-list {
  margin: 0.92rem 0 0;
  padding: 0 0 0 1.2rem;
  display: grid;
  gap: 0.25rem;
}

.mystery-board {
  margin-top: 0.5rem;
  display: grid;
  grid-template-columns: repeat(11, minmax(0, 1fr));
  gap: 0.18rem;
  width: min(520px, 100%);
}

.mystery-head,
.mystery-cell {
  border: 1px solid rgba(36, 57, 90, 0.25);
  border-radius: 8px;
  min-height: 28px;
  aspect-ratio: 1 / 1;
  padding: 0;
  font-size: 0.78rem;
  font-weight: 900;
  display: grid;
  place-items: center;
}

.mystery-head {
  background: #d9ecff;
  color: #1c446d;
}

.mystery-corner {
  background: #c7e4ff;
}

.mystery-cell {
  background: #f7fbff;
  color: #31527d;
  cursor: pointer;
}

.mystery-cell:hover {
  background: #ebf5ff;
}

.mystery-cell.is-revealed {
  background: #fff2d4;
  border-color: rgba(220, 157, 28, 0.45);
  color: #7e5300;
}

.mystery-reset-btn {
  margin-top: 0.65rem;
}

.chip-group {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.chip {
  background: #ebf5ff;
  border: 1px solid rgba(44, 86, 138, 0.18);
  color: #27456d;
  border-radius: 999px;
  font-weight: 900;
  padding: 0.3rem 0.72rem;
  font-size: 0.9rem;
}

.timer-gauge {
  width: min(220px, 62vw);
  min-width: 120px;
  height: 14px;
  border-radius: 999px;
  border: 2px solid rgba(44, 86, 138, 0.2);
  background: #eaf3ff;
  overflow: hidden;
}

.timer-gauge-fill {
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #2fc48f, #f0be2f, #ef5d4f);
  transition: width 220ms linear;
}

.game-mode-switch {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.mode-btn {
  border: 2px solid rgba(36, 57, 90, 0.18);
  background: #f7fbff;
  color: #24456f;
  border-radius: 12px;
  padding: 0.5rem 0.78rem;
  font: inherit;
  font-weight: 900;
  cursor: pointer;
}

.mode-btn.is-active {
  background: linear-gradient(145deg, #7fdcff, #57bde7);
  border-color: #3ba8d5;
  color: #083b56;
}

#challenge.panel.is-active {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  gap: 0.9rem;
}

#challenge .panel-head,
#challenge .game-mode-switch,
#challenge .challenge-head,
#challenge .leaderboard {
  grid-column: 1 / -1;
}

.game-visual {
  border: 2px solid rgba(37, 73, 116, 0.14);
  border-radius: 16px;
  padding: 0.7rem;
  margin-bottom: 0.85rem;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95), rgba(246, 251, 255, 0.95));
}

.game-label {
  margin: 0 0 0.45rem;
  font-weight: 900;
  color: #26496f;
}

.timed-note {
  margin: 0;
  color: #35557c;
  font-weight: 800;
}

.image-reveal {
  position: relative;
  width: min(280px, 100%);
  aspect-ratio: 1 / 1;
  border: 2px solid rgba(36, 57, 90, 0.2);
  border-radius: 16px;
  overflow: hidden;
  background: linear-gradient(150deg, #fef6d9, #def3ff);
  display: grid;
  padding: 0.5rem;
}

.image-grid {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 0.28rem;
}

.image-emoji-cell {
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.42);
  border: 1px solid rgba(36, 57, 90, 0.18);
  font-size: clamp(2.6rem, 9vw, 4.2rem);
  line-height: 1;
}

.image-mask {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

.image-mask-tile {
  background: linear-gradient(180deg, #1490be, #0c6e94);
  border: 1px solid #0c5a78;
}

.image-mask-tile[hidden] {
  display: none;
}

.tower-stack {
  min-height: 300px;
  width: min(330px, 100%);
  border: 2px solid rgba(36, 57, 90, 0.2);
  border-radius: 16px;
  background: linear-gradient(180deg, #f9fcff, #edf5ff);
  padding: 0.55rem;
  display: grid;
  grid-template-rows: repeat(10, minmax(0, 1fr));
  gap: 0.28rem;
}

.tower-slot {
  border: 2px dashed rgba(52, 88, 134, 0.26);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.5);
  overflow: hidden;
}

.boss-scene,
.combo-scene,
.castle-scene {
  width: min(340px, 100%);
  border: 2px solid rgba(36, 57, 90, 0.2);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.66);
  padding: 0.62rem;
}

.boss-scene {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.65rem;
  align-items: center;
}

.boss-emoji {
  font-size: clamp(2rem, 8vw, 3.3rem);
  line-height: 1;
}

.boss-stats {
  display: grid;
  gap: 0.33rem;
}

.status-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.4rem;
  font-weight: 900;
  color: #2a4f79;
  font-size: 0.92rem;
}

.status-bar {
  height: 14px;
  border-radius: 999px;
  border: 2px solid rgba(36, 57, 90, 0.2);
  background: #eaf3ff;
  overflow: hidden;
}

.status-bar-fill {
  height: 100%;
  width: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #1fb57f, #f0c13d, #e95a4f);
  transition: width 220ms linear;
}

.boss-shield,
.combo-best,
.castle-lives {
  margin: 0;
  color: #36597f;
  font-weight: 800;
  font-size: 0.92rem;
}

.combo-scene {
  display: grid;
  gap: 0.45rem;
}

.combo-value {
  margin: 0;
  font-family: "Baloo 2", cursive;
  font-size: clamp(1.35rem, 5vw, 2rem);
  color: #1e446e;
}

.combo-bar .combo-fill {
  background: linear-gradient(90deg, #ff8e32, #f44f69, #8e4ef5);
}

.castle-scene {
  display: grid;
  gap: 0.4rem;
  position: relative;
}

.castle-difficulty {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-bottom: 0.5rem;
}

.castle-difficulty-btn {
  border: 2px solid rgba(36, 57, 90, 0.17);
  border-radius: 12px;
  min-height: 40px;
  padding: 0.34rem 0.7rem;
  font: inherit;
  font-weight: 900;
  color: #214068;
  background: #f7fbff;
  cursor: pointer;
  transition: background-color 100ms ease, border-color 100ms ease;
}

.castle-difficulty-btn:hover {
  background: #eef7ff;
}

.castle-difficulty-btn.is-active {
  background: linear-gradient(145deg, #7fdcff, #57bde7);
  border-color: #3ba8d5;
  color: #083b56;
}

.castle-wave-detail,
.castle-incoming {
  margin: 0;
  color: #36597f;
  font-weight: 800;
  font-size: 0.9rem;
}

.castle-track-wrap {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.35rem;
}

.castle-side-icon {
  font-size: 1.25rem;
  line-height: 1;
}

.castle-track {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 0.2rem;
}

.castle-lane-cell {
  min-height: 28px;
  border: 2px solid rgba(53, 90, 136, 0.2);
  border-radius: 8px;
  background: linear-gradient(180deg, #f7fbff, #eef6ff);
  display: grid;
  place-items: center;
}

.castle-enemy {
  font-size: 0.95rem;
  font-weight: 900;
  color: #8e2a3d;
  animation: enemy-pulse 700ms ease-in-out infinite;
}

.castle-fx {
  margin: 0;
  min-height: 1.1rem;
  font-family: "Baloo 2", cursive;
  font-weight: 800;
  font-size: 1.05rem;
  opacity: 0;
}

.castle-fx.is-good {
  color: #1f9d73;
  animation: castle-fx-pop 900ms ease;
}

.castle-fx.is-bad {
  color: #db4e5a;
  animation: castle-fx-pop 900ms ease;
}

.castle-fx.is-breach {
  color: #b33641;
  animation: castle-fx-breach 1100ms ease;
}

.castle-scene.fx-good {
  animation: castle-scene-good 420ms ease;
}

.castle-scene.fx-bad {
  animation: castle-scene-bad 420ms ease;
}

.castle-scene.fx-breach {
  animation: castle-scene-breach 620ms ease;
}

@keyframes enemy-pulse {
  0% {
    transform: scale(0.96);
  }
  50% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(0.96);
  }
}

@keyframes castle-scene-good {
  0% {
    box-shadow: 0 0 0 rgba(31, 157, 115, 0);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(31, 157, 115, 0.25);
  }
  100% {
    box-shadow: 0 0 0 rgba(31, 157, 115, 0);
  }
}

@keyframes castle-scene-bad {
  0% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-4px);
  }
  50% {
    transform: translateX(4px);
  }
  75% {
    transform: translateX(-3px);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes castle-scene-breach {
  0% {
    transform: scale(1);
    background: rgba(255, 255, 255, 0.66);
  }
  45% {
    transform: scale(1.02);
    background: rgba(255, 214, 214, 0.9);
  }
  100% {
    transform: scale(1);
    background: rgba(255, 255, 255, 0.66);
  }
}

@keyframes castle-fx-pop {
  0% {
    opacity: 0;
    transform: translateY(6px) scale(0.9);
  }
  30% {
    opacity: 1;
    transform: translateY(0) scale(1.04);
  }
  100% {
    opacity: 0;
    transform: translateY(-3px) scale(1);
  }
}

@keyframes castle-fx-breach {
  0% {
    opacity: 0;
    transform: translateY(8px) scale(0.85);
  }
  35% {
    opacity: 1;
    transform: translateY(0) scale(1.07);
  }
  100% {
    opacity: 0;
    transform: translateY(-4px) scale(1);
  }
}

.leaderboard {
  margin-top: 0.9rem;
  border: 2px solid rgba(37, 73, 116, 0.14);
  border-radius: 16px;
  padding: 0.75rem;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95), rgba(246, 251, 255, 0.95));
}

.leaderboard-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
}

.leaderboard-list {
  margin: 0.55rem 0 0;
  padding-left: 1.2rem;
  display: grid;
  gap: 0.35rem;
}

.leaderboard-empty {
  list-style: none;
  margin-left: -1.1rem;
  color: #607390;
  font-weight: 700;
}

.tower-brick {
  height: 100%;
  border-radius: 7px;
  border: 1px solid rgba(22, 33, 50, 0.2);
  position: relative;
}

.table-1 {
  background: #fcd8d8;
}
.table-2 {
  background: #fde3ba;
}
.table-3 {
  background: #fff1a9;
}
.table-4 {
  background: #dcf5b2;
}
.table-5 {
  background: #b8f0d2;
}
.table-6 {
  background: #b7ecf8;
}
.table-7 {
  background: #bfdfff;
}
.table-8 {
  background: #d5d2ff;
}
.table-9 {
  background: #efcfff;
}
.table-10 {
  background: #ffcfe5;
}

.tower-brick.crack-1::after,
.tower-brick.crack-2::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.tower-brick.crack-1::after {
  background: linear-gradient(122deg, transparent 48%, rgba(70, 70, 70, 0.45) 50%, transparent 52%);
}

.tower-brick.crack-2::after {
  background:
    linear-gradient(122deg, transparent 46%, rgba(70, 70, 70, 0.5) 50%, transparent 54%),
    linear-gradient(58deg, transparent 46%, rgba(70, 70, 70, 0.45) 50%, transparent 54%);
}

.question-card {
  margin-top: 0.95rem;
  border: 2px solid rgba(37, 73, 116, 0.14);
  border-radius: 18px;
  padding: 0.92rem;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.95), rgba(246, 251, 255, 0.95));
}

.question {
  font-size: clamp(1.5rem, 5vw, 2.25rem);
  font-family: "Baloo 2", cursive;
  font-weight: 700;
  margin: 0;
  color: #173a61;
}

.question-line {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center;
  gap: 0.45rem;
}

.inline-result {
  margin: 0;
  min-height: 2.1rem;
  min-width: 118px;
  max-width: 100%;
  padding: 0.3rem 0.65rem;
  border-radius: 12px;
  border: 2px dashed rgba(36, 57, 90, 0.25);
  background: #f8fbff;
  color: #30476c;
  font-weight: 900;
  font-size: 0.95rem;
  text-align: center;
  display: inline-flex;
  align-items: center;
}

.inline-result:empty {
  visibility: hidden;
}

.inline-result.good {
  background: #e8f9f1;
  color: var(--good);
  border-color: rgba(31, 157, 115, 0.42);
}

.inline-result.bad {
  background: #fff0f1;
  color: var(--bad);
  border-color: rgba(219, 78, 90, 0.43);
}

.hint {
  margin: 0.08rem 0 0.65rem;
  color: var(--muted);
  font-size: 0.94rem;
  font-weight: 800;
}

form {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
}

form input[type="number"] {
  width: min(180px, 100%);
}

.keypad {
  margin-top: 0.78rem;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.45rem;
  max-width: 320px;
}

.keypad-key {
  border: 2px solid rgba(36, 57, 90, 0.18);
  border-radius: 14px;
  min-height: 52px;
  font: inherit;
  font-weight: 900;
  font-size: 1.05rem;
  color: #1d3558;
  background: #f8fbff;
  cursor: pointer;
  transition: background-color 100ms ease, border-color 100ms ease;
}

.keypad-key:hover {
  background: #eef7ff;
}

.keypad-key:active {
  background: #e8f3ff;
}

.keypad-action {
  background: #fff5e8;
}

.keypad-key:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn {
  border: 0;
  cursor: pointer;
  font-weight: 900;
  border-radius: 12px;
  padding: 0.66rem 1rem;
  transition: opacity 120ms ease;
  box-shadow: var(--shadow-pop);
}

.btn:hover {
  box-shadow: var(--shadow-pop);
}

.btn:active {
  box-shadow: var(--shadow-pop);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-primary {
  color: #fff;
  background: linear-gradient(145deg, #19a2d7, #0c789f);
}

.btn-ghost {
  background: #ebf5ff;
  color: #1f4d79;
}

.btn-danger {
  color: #fff;
  background: linear-gradient(140deg, #df5561, #b93541);
}

.feedback {
  min-height: 1.2em;
  font-weight: 800;
  margin: 0.68rem 0 0;
}

.feedback.good {
  color: var(--good);
}

.feedback.bad {
  color: var(--bad);
}

.celebration-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 40;
  overflow: hidden;
}

.celebration-layer.is-active {
  display: block;
}

.celebration-badge {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  border-radius: 16px;
  padding: 0.6rem 1rem;
  font-family: "Baloo 2", cursive;
  font-size: clamp(1.9rem, 7vw, 3.4rem);
  font-weight: 800;
  color: #ffffff;
  border: 3px solid rgba(255, 255, 255, 0.95);
  text-align: center;
  text-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
  animation: badge-hold 5000ms ease both;
}

.celebration-layer.effect-fireworks .celebration-badge {
  background: linear-gradient(145deg, #0d8cb8, #0a5c84);
}

.celebration-layer.effect-champion .celebration-badge {
  background: linear-gradient(145deg, #f6a429, #d67b00);
}

.celebration-layer.effect-stars .celebration-badge {
  background: linear-gradient(145deg, #556cf0, #3649be);
}

.celebration-layer.effect-party .celebration-badge {
  background: linear-gradient(145deg, #ef5f7f, #cb2f58);
}

.celebration-layer.effect-space .celebration-badge {
  background: linear-gradient(145deg, #4e63f1, #28379b);
}

.celebration-layer.effect-animals .celebration-badge {
  background: linear-gradient(145deg, #2bb788, #1b8360);
}

.celebration-layer.effect-ocean .celebration-badge {
  background: linear-gradient(145deg, #11a2b8, #0f7085);
}

.celebration-layer.effect-dino .celebration-badge {
  background: linear-gradient(145deg, #63b435, #3a7e17);
}

.celebration-layer.effect-magic .celebration-badge {
  background: linear-gradient(145deg, #8a56e4, #5e29bb);
}

.celebration-layer.effect-hearts .celebration-badge {
  background: linear-gradient(145deg, #f45d93, #c63e6d);
}

.celebration-layer.effect-volcano .celebration-badge {
  background: linear-gradient(145deg, #f4661b, #bb2e11);
}

.celebration-layer.effect-rainbow .celebration-badge {
  background: linear-gradient(145deg, #3ca6ff, #7b4cf3, #ef4e8d);
}

.celebration-layer.effect-thunder .celebration-badge {
  background: linear-gradient(145deg, #f7c72a, #f08a00);
}

.celebration-layer.effect-galaxy .celebration-badge {
  background: linear-gradient(145deg, #2d3fa7, #1b226d);
}

.celebration-layer.effect-dragon .celebration-badge {
  background: linear-gradient(145deg, #30a15d, #1b7447);
}

.celebration-layer.effect-volcano,
.celebration-layer.effect-thunder,
.celebration-layer.effect-galaxy,
.celebration-layer.effect-dragon {
  backdrop-filter: saturate(1.15);
}

.celebration-particle {
  position: absolute;
  font-size: clamp(1.2rem, 3vw, 2rem);
  opacity: 0;
  animation-name: particle-pop;
  animation-timing-function: ease-out;
  animation-fill-mode: forwards;
}

.screen-shake {
  animation: screen-shake 300ms linear 2;
}

.castle-danger-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 44;
  opacity: 0;
  overflow: hidden;
  transition: opacity 240ms ease;
}

.castle-danger-layer::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 50% 95%, rgba(255, 90, 50, 0.5), transparent 42%),
    linear-gradient(180deg, rgba(120, 0, 0, 0.08) 0%, rgba(255, 76, 28, 0.16) 100%);
}

.castle-danger-layer::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 20vh;
  background:
    radial-gradient(circle at 10% 100%, rgba(255, 183, 0, 0.65), transparent 55%),
    radial-gradient(circle at 28% 100%, rgba(255, 119, 0, 0.62), transparent 58%),
    radial-gradient(circle at 48% 100%, rgba(255, 65, 0, 0.64), transparent 57%),
    radial-gradient(circle at 67% 100%, rgba(255, 168, 0, 0.6), transparent 56%),
    radial-gradient(circle at 86% 100%, rgba(255, 78, 0, 0.62), transparent 58%);
  animation: castle-fire-wave 620ms ease-in-out infinite alternate;
}

.castle-danger-layer.is-active {
  opacity: 1;
}

.castle-danger-flame {
  position: absolute;
  bottom: -12px;
  font-size: clamp(1.2rem, 2.8vw, 2.2rem);
  opacity: 0;
  animation: castle-flame-rise 1450ms ease-out forwards;
}

.castle-breach-shake {
  animation: castle-breach-shake 210ms linear infinite;
}

@keyframes badge-hold {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.2);
  }
  12% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.08);
  }
  82% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -52%) scale(0.95);
  }
}

@keyframes particle-pop {
  0% {
    opacity: 0;
    transform: translate(0, 0) rotate(0deg) scale(0.8);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(1.1);
  }
}

@keyframes screen-shake {
  0% {
    transform: translate(0, 0);
  }
  25% {
    transform: translate(4px, -2px);
  }
  50% {
    transform: translate(-4px, 3px);
  }
  75% {
    transform: translate(3px, 1px);
  }
  100% {
    transform: translate(0, 0);
  }
}

@keyframes castle-breach-shake {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(7px, -4px) rotate(0.2deg);
  }
  50% {
    transform: translate(-7px, 5px) rotate(-0.2deg);
  }
  75% {
    transform: translate(5px, 2px) rotate(0.15deg);
  }
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
}

@keyframes castle-fire-wave {
  0% {
    transform: scaleY(1) translateY(0);
  }
  100% {
    transform: scaleY(1.08) translateY(-3px);
  }
}

@keyframes castle-flame-rise {
  0% {
    opacity: 0;
    transform: translateY(0) scale(0.75) rotate(0deg);
  }
  12% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateY(-240px) scale(1.24) rotate(12deg);
  }
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.58rem;
}

.stat {
  border: 2px solid rgba(36, 57, 90, 0.14);
  border-radius: 15px;
  padding: 0.72rem;
  background: #fcfeff;
}

.stat p {
  margin: 0.16rem 0 0;
  font-size: 1.35rem;
  font-weight: 900;
  color: #1f4b76;
}

.progress-layout {
  margin-top: 0.95rem;
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.94rem;
}

.heatmap {
  margin-top: 0.5rem;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 0.25rem;
}

.heat-cell {
  aspect-ratio: 1 / 1;
  border-radius: 9px;
  border: 1px solid rgba(28, 45, 72, 0.18);
  font-size: 0.72rem;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: #153a60;
}

.m0 {
  background: #f1f6fb;
}

.m1 {
  background: #ffe6c6;
}

.m2 {
  background: #ffd197;
}

.m3 {
  background: #c9effb;
}

.m4 {
  background: #8fdcf3;
}

.weak-list {
  margin: 0.5rem 0 0;
  padding-left: 1.2rem;
  display: grid;
  gap: 0.35rem;
}

.settings-block + .settings-block {
  margin-top: 1rem;
}

.settings-text {
  color: var(--muted);
  margin: 0.36rem 0 0.72rem;
  font-weight: 700;
}

.table-toggle-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
}

.handedness-toggle {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.handedness-toggle .table-toggle {
  min-width: 170px;
  padding: 0 0.72rem;
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}

.settings-danger {
  border: 2px solid rgba(219, 78, 90, 0.28);
  border-radius: 14px;
  padding: 0.82rem;
  background: #fff7f8;
}

.settings-status {
  min-height: 1.3rem;
  margin: 0.9rem 0 0;
  font-weight: 900;
  color: #1f4d79;
}

.settings-status.good {
  color: var(--good);
}

.settings-status.bad {
  color: var(--bad);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

@media (max-width: 520px) {
  #train-form {
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.35rem;
  }

  #train-form input[type="number"] {
    width: 66px;
    min-width: 66px;
    padding: 0.44rem 0.38rem;
  }

  #train-form .btn {
    padding: 0.54rem 0.64rem;
    font-size: 0.9rem;
    white-space: nowrap;
  }
}

@media (min-width: 700px) {
  .app {
    margin-top: 1.4rem;
  }

  .hero,
  .panel {
    padding: 1.2rem;
  }

  .tabs {
    grid-template-columns: repeat(5, 1fr);
  }

  .stats-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .table-toggle-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 860px) {
  #challenge.panel.is-active {
    grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
    column-gap: 1rem;
  }

  #challenge .challenge-head {
    grid-column: 1 / -1;
    grid-row: 3;
  }

  #challenge .game-visual {
    margin-bottom: 0;
    grid-column: 1;
    grid-row: 4;
  }

  #challenge .question-card {
    margin-top: 0;
    grid-column: 2;
    grid-row: 4;
  }

  #challenge.visual-right .game-visual {
    grid-column: 2;
  }

  #challenge.visual-right .question-card {
    grid-column: 1;
  }

  #challenge .leaderboard {
    grid-row: 5;
  }
}

@media (min-width: 940px) {
  .progress-layout {
    grid-template-columns: 1.35fr 1fr;
    align-items: start;
  }

  .table-keypad,
  .multiplier-keypad,
  .table-toggle-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
}
