:root {
  --bg-top: #fff7e4;
  --bg-bottom: #f8d58c;
  --text-main: #2f2413;
  --text-soft: #6d5531;
  --accent: #f08c32;
  --accent-strong: #da5e16;
  --shadow: 0 20px 60px rgba(89, 56, 11, 0.18);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Nunito", sans-serif;
  color: var(--text-main);
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.8), transparent 30%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
}

body::before,
body::after {
  content: "";
  position: fixed;
  inset: auto;
  width: 28vw;
  height: 28vw;
  border-radius: 999px;
  filter: blur(18px);
  opacity: 0.35;
  z-index: -1;
}

body::before {
  top: -8vw;
  right: -8vw;
  background: rgba(240, 140, 50, 0.4);
}

body::after {
  bottom: -10vw;
  left: -4vw;
  background: rgba(105, 181, 113, 0.28);
}

.app-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 18px;
}

.board-panel {
  position: relative;
  width: min(92vmin, 860px);
  aspect-ratio: 1;
  border-radius: 34px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  background: linear-gradient(180deg, rgba(255, 249, 232, 0.9), rgba(255, 237, 194, 0.98));
}

body.touch-available #game-canvas {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.touch-controls {
  position: absolute;
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  display: grid;
  align-items: end;
  gap: clamp(12px, 4vmin, 28px);
  left: calc(14px + env(safe-area-inset-left, 0px));
  right: calc(14px + env(safe-area-inset-right, 0px));
  z-index: 2;
  pointer-events: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.touch-controls[hidden] {
  display: none;
}

.touch-controls-single {
  grid-template-columns: 1fr;
  justify-items: center;
}

.touch-controls-dual {
  grid-template-columns: 1fr 1fr;
  justify-items: stretch;
}

.touch-pad {
  width: clamp(132px, 22vmin, 170px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  pointer-events: auto;
}

.touch-pad[hidden] {
  display: none;
}

.touch-pad-p1 {
  justify-self: start;
}

.touch-controls-single .touch-pad-p1 {
  justify-self: center;
}

.touch-pad-p2 {
  justify-self: end;
}

.touch-control {
  min-width: 44px;
  min-height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(110, 76, 22, 0.24);
  border-radius: 16px;
  background: rgba(255, 249, 236, 0.82);
  box-shadow: 0 10px 24px rgba(89, 56, 11, 0.18);
  color: var(--text-main);
  font: inherit;
  font-size: clamp(1.35rem, 4vmin, 1.9rem);
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
  transition: transform 100ms ease, background 100ms ease, border-color 100ms ease;
}

.touch-control-up {
  grid-column: 2;
  grid-row: 1;
}

.touch-control-left {
  grid-column: 1;
  grid-row: 2;
}

.touch-control-right {
  grid-column: 3;
  grid-row: 2;
}

.touch-control-down {
  grid-column: 2;
  grid-row: 3;
}

.touch-control:active,
.touch-control.pressed {
  transform: scale(0.95);
  border-color: rgba(47, 159, 98, 0.5);
  background: rgba(47, 159, 98, 0.22);
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(43, 29, 12, 0.16);
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease;
  z-index: 3;
}

.overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay-card {
  min-width: min(320px, 80%);
  padding: 26px 28px;
  border-radius: 26px;
  text-align: center;
  background: rgba(255, 249, 236, 0.72);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(110, 76, 22, 0.14);
  box-shadow: var(--shadow);
}

.mode-menu {
  width: min(620px, 88%);
  max-height: calc(100% - 24px);
  display: grid;
  justify-items: center;
  gap: 18px;
  overflow-y: auto;
  padding: 4px;
  text-align: center;
  transition: opacity 260ms ease;
}

.mode-menu-title {
  margin: 0;
  font-family: "Baloo 2", cursive;
  font-size: clamp(3.4rem, 9vw, 5.6rem);
  line-height: 0.9;
  color: rgba(47, 36, 19, 0.82);
  transition: transform 360ms ease;
}

.mode-menu-versus {
  font-family: "Baloo 2", cursive;
  font-size: clamp(2.4rem, 7vw, 4.2rem);
  font-weight: 800;
  color: rgba(218, 94, 22, 0.72);
  transition: transform 360ms ease;
}

.player-count-picker {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  width: min(360px, 100%);
}

.player-count-button {
  min-height: 54px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid rgba(110, 76, 22, 0.14);
  border-radius: 18px;
  background: rgba(255, 249, 236, 0.74);
  color: var(--text-main);
  font: inherit;
  cursor: pointer;
  transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}

.player-count-button:hover {
  transform: translateY(-1px);
  border-color: rgba(218, 94, 22, 0.28);
}

.player-count-button.selected {
  background: rgba(47, 159, 98, 0.18);
  border-color: rgba(47, 159, 98, 0.46);
}

.mode-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.mode-grid-stacked {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.multiplication-flow,
.learning-flow {
  width: 100%;
}

.flow-panel {
  display: grid;
  gap: 14px;
}

.flow-panel[hidden] {
  display: none;
}

.flow-header {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
}

.flow-header h2 {
  margin: 0;
  font-family: "Baloo 2", cursive;
  font-size: clamp(2rem, 5vw, 3rem);
  line-height: 1;
}

.flow-back {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(110, 76, 22, 0.14);
  border-radius: 999px;
  background: rgba(255, 249, 236, 0.74);
  color: var(--text-main);
  font: inherit;
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
}

.row-chip-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.row-chip {
  min-height: 48px;
  border: 1px solid rgba(110, 76, 22, 0.16);
  border-radius: 16px;
  background: rgba(255, 249, 236, 0.74);
  color: var(--text-main);
  font: inherit;
  font-weight: 900;
  cursor: pointer;
  transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}

.row-chip:hover {
  transform: translateY(-1px);
  border-color: rgba(218, 94, 22, 0.28);
}

.row-chip.selected {
  background: rgba(47, 159, 98, 0.18);
  border-color: rgba(47, 159, 98, 0.46);
}

.flow-action {
  justify-self: center;
}

.flow-action:disabled {
  cursor: not-allowed;
  opacity: 0.46;
  transform: none;
  box-shadow: none;
}

.mode-card {
  min-height: 116px;
  display: grid;
  align-content: center;
  gap: 6px;
  padding: 18px;
  border: 1px solid rgba(110, 76, 22, 0.14);
  border-radius: 18px;
  background: rgba(255, 249, 236, 0.74);
  box-shadow: 0 14px 36px rgba(89, 56, 11, 0.13);
  color: var(--text-main);
  font: inherit;
  cursor: pointer;
  transition: transform 220ms ease, opacity 180ms ease, border-color 180ms ease, background 180ms ease;
}

.mode-card:hover {
  transform: translateY(-2px);
  border-color: rgba(218, 94, 22, 0.28);
  background: rgba(255, 252, 244, 0.86);
}

.mode-card.incompatible {
  cursor: not-allowed;
  opacity: 0.48;
}

.mode-card.incompatible:hover {
  transform: none;
  border-color: rgba(110, 76, 22, 0.14);
  background: rgba(255, 249, 236, 0.74);
}

.mode-card strong {
  font-size: 1.25rem;
  font-weight: 900;
}

.mode-card span {
  font-size: 0.95rem;
  color: var(--text-soft);
}

.mode-menu.selecting .mode-menu-title {
  transform: scale(1.12);
}

.mode-menu.selecting .mode-menu-versus {
  transform: scale(1.18);
}

.mode-menu.selecting .mode-card {
  opacity: 0;
  pointer-events: none;
}

.mode-menu.selecting .mode-card.selected {
  opacity: 1;
  transform: scale(1.18);
  border-color: rgba(218, 94, 22, 0.32);
}

.mode-menu.configuring .mode-menu-title,
.mode-menu.configuring .mode-menu-versus,
.mode-menu.configuring > .player-count-picker,
.mode-menu.configuring > .mode-grid {
  display: none;
}

.overlay-card h2 {
  margin: 0 0 10px;
  font-family: "Baloo 2", cursive;
  font-size: clamp(2rem, 5vw, 3rem);
}

.overlay-card p,
.overlay-eyebrow {
  color: var(--text-soft);
}

.mode-picker,
.difficulty-picker {
  display: grid;
  gap: 10px;
  margin: 18px 0 20px;
  text-align: left;
}

.mode-picker {
  margin-bottom: 14px;
}

.difficulty-picker {
  margin-top: 0;
}

.mode-option,
.difficulty-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 18px;
  border: 1px solid rgba(110, 76, 22, 0.12);
  background: rgba(255, 255, 255, 0.44);
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease;
}

.mode-option:hover,
.difficulty-option:hover {
  transform: translateY(-1px);
  border-color: rgba(218, 94, 22, 0.22);
}

.mode-option input,
.difficulty-option input {
  margin: 0;
  accent-color: var(--accent-strong);
}

.mode-copy,
.difficulty-copy {
  display: grid;
  gap: 2px;
}

.mode-copy strong,
.difficulty-copy strong {
  font-size: 1rem;
  color: var(--text-main);
}

.mode-copy span,
.difficulty-copy span {
  font-size: 0.9rem;
  color: var(--text-soft);
}

.overlay-eyebrow {
  margin: 0 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-size: 0.75rem;
  font-weight: 800;
}

.button {
  appearance: none;
  border: none;
  border-radius: 999px;
  padding: 14px 24px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  transition: transform 160ms ease, box-shadow 160ms ease;
}

.button:hover {
  transform: translateY(-1px);
}

.button-primary {
  color: white;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  box-shadow: 0 12px 24px rgba(218, 94, 22, 0.28);
}

.button-secondary {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.58);
  border: 1px solid rgba(110, 76, 22, 0.16);
}

.game-over-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 18px;
}

@media (max-width: 1024px) {
  .board-panel {
    width: min(94vmin, 820px);
  }
}

@media (max-width: 720px) {
  .app-shell {
    padding: 10px;
  }

  .overlay {
    padding: 14px;
  }

  .board-panel {
    width: calc(100vw - 20px);
    border-radius: 26px;
  }

  .mode-grid {
    grid-template-columns: 1fr;
  }

  .mode-card {
    min-height: 92px;
  }

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

  .touch-controls {
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    left: calc(10px + env(safe-area-inset-left, 0px));
    right: calc(10px + env(safe-area-inset-right, 0px));
    gap: 10px;
  }

  .touch-pad {
    width: clamp(112px, 36vw, 148px);
    gap: 5px;
  }

  .touch-control {
    min-width: 38px;
    min-height: 38px;
    border-radius: 13px;
  }
}
