:root {
  --bg-1: #3a1d8a;
  --bg-2: #160a35;
  --panel: #2456d6;
  --panel-dark: #173aa0;
  --hole: #0f1c4d;
  --card: #fff6ea;
  --ink: #241a4a;
  --ink-soft: #6a5f96;
  --red-1: #ff8a7a;
  --red-2: #e63946;
  --red-3: #a11f2a;
  --yellow-1: #ffe98a;
  --yellow-2: #f4b400;
  --yellow-3: #a86e00;
  --green: #3fbf6a;
  --purple-btn-1: #8f6bff;
  --purple-btn-2: #5a35d6;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-2);
  font-family: 'Nunito', sans-serif;
  color: var(--ink);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 520px;
  margin: 0 auto;
  background:
    radial-gradient(circle at 25% 0%, rgba(255, 255, 255, 0.12), transparent 45%),
    linear-gradient(180deg, var(--bg-1) 0%, var(--bg-2) 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- Preloader ---------- */
#preloader {
  position: fixed;
  inset: 0;
  z-index: 999;
  background: linear-gradient(160deg, #4a2bb0, #1c0e46 60%, #0d0625);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  gap: 22px;
  transition: opacity .5s ease, visibility .5s ease;
}

#preloader.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.pre-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: 'Baloo 2', cursive;
  font-weight: 800;
  font-size: 32px;
  letter-spacing: .5px;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.25);
}

.pre-discs {
  display: flex;
  gap: 6px;
}

.pre-disc {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.35);
  animation: preBounce 1.1s ease-in-out infinite;
}

.pre-disc.r {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
  animation-delay: 0s;
}

.pre-disc.y {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
  animation-delay: .15s;
}

.pre-disc.r2 {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
  animation-delay: .3s;
}

.pre-disc.y2 {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
  animation-delay: .45s;
}

@keyframes preBounce {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-14px);
  }
}

.pre-bar-track {
  width: 220px;
  height: 14px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.18);
  overflow: hidden;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.25);
}

.pre-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--yellow-2), var(--yellow-1));
  border-radius: 10px;
  transition: width .18s ease-out;
}

.pre-label {
  font-size: 13px;
  opacity: 0.85;
  font-weight: 700;
  letter-spacing: .5px;
}

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 1;
  transition: opacity .35s ease, transform .35s ease;
}

.screen.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.98);
}

#screen-home {
  align-items: center;
  justify-content: center;
  gap: 26px;
  padding: 20px;
}

.home-discs {
  position: relative;
  width: 190px;
  height: 70px;
}

.hdc {
  position: absolute;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.4);
}

.hdc.r {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
  left: 0;
  top: 6px;
  animation: floaty 3.1s ease-in-out infinite;
}

.hdc.y {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
  left: 70px;
  top: 0;
  animation: floaty 3.5s ease-in-out infinite .25s;
}

.hdc.r2 {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
  left: 140px;
  top: 10px;
  animation: floaty 2.8s ease-in-out infinite .5s;
}

@keyframes floaty {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
  }
}

.game-title {
  font-family: 'Baloo 2', cursive;
  font-weight: 800;
  font-size: 42px;
  color: #fff;
  text-align: center;
  line-height: 1.05;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}

.game-title span {
  color: var(--yellow-2);
}

.game-sub {
  color: rgba(255, 255, 255, 0.7);
  font-weight: 700;
  text-align: center;
  margin-top: 8px;
}

.home-buttons {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 240px;
  margin-top: 6px;
}

/* Buttons */
.pill-btn {
  font-family: 'Baloo 2', cursive;
  font-weight: 700;
  font-size: 19px;
  color: #fff;
  letter-spacing: .5px;
  background: linear-gradient(180deg, var(--purple-btn-1), var(--purple-btn-2));
  border: none;
  border-radius: 40px;
  padding: 14px 20px;
  box-shadow: 0 5px 0 #3f22a3, 0 8px 16px rgba(60, 30, 150, 0.4);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .08s ease, box-shadow .08s ease;
}

.pill-btn:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #3f22a3, 0 3px 8px rgba(60, 30, 150, 0.4);
}

.pill-btn.secondary {
  background: linear-gradient(180deg, #fff, #f0e9ff);
  color: var(--purple-btn-2);
  box-shadow: 0 5px 0 #c9b8ff, 0 8px 16px rgba(0, 0, 0, 0.15);
}

.pill-btn.secondary:active {
  box-shadow: 0 1px 0 #c9b8ff;
}

.pill-btn.green {
  background: linear-gradient(180deg, #5ed88a, var(--green));
  box-shadow: 0 5px 0 #229752, 0 8px 16px rgba(30, 180, 100, 0.3);
}

.pill-btn.green:active {
  box-shadow: 0 1px 0 #229752;
}

.pill-btn.red {
  background: linear-gradient(180deg, var(--red-1), var(--red-2));
  box-shadow: 0 5px 0 #8f1822, 0 8px 16px rgba(200, 40, 50, 0.35);
}

.pill-btn.red:active {
  box-shadow: 0 1px 0 #8f1822;
}

.icon-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(180deg, var(--purple-btn-1), var(--purple-btn-2));
  color: #fff;
  font-size: 20px;
  font-weight: 800;
  box-shadow: 0 4px 0 #3f22a3;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform .08s ease, box-shadow .08s ease;
  flex-shrink: 0;
}

.icon-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #3f22a3;
}

.icon-btn.ghost {
  background: linear-gradient(180deg, #fff, #f0e9ff);
  color: var(--purple-btn-2);
  box-shadow: 0 4px 0 #c9b8ff;
}

.icon-btn.ghost:active {
  box-shadow: 0 1px 0 #c9b8ff;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px 10px;
}

.title-pill {
  flex: 1;
  text-align: center;
  background: var(--card);
  border-radius: 40px;
  padding: 10px 14px;
  font-family: 'Baloo 2', cursive;
  font-weight: 700;
  font-size: 17px;
  color: var(--ink);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15), inset 0 0 0 1px rgba(255, 255, 255, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.turn-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: inset 0 -2px 3px rgba(0, 0, 0, 0.25), 0 1px 2px rgba(0, 0, 0, 0.3);
}

.turn-dot.r {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
}

.turn-dot.y {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
}

/* Mode select */
#screen-mode {
  padding: 16px;
  align-items: stretch;
  justify-content: center;
  gap: 16px;
}

.mode-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}

.mode-title {
  flex: 1;
  text-align: center;
  font-family: 'Baloo 2', cursive;
  font-size: 22px;
  color: #fff;
  font-weight: 800;
}

.mode-cards {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 10px 8px;
}

.mode-card {
  background: var(--card);
  border: none;
  border-radius: 22px;
  padding: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 16px;
  text-align: left;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.18), 0 10px 20px rgba(0, 0, 0, 0.25);
  transition: transform .08s ease;
}

.mode-card:active {
  transform: translateY(3px);
}

.mode-icon {
  font-size: 34px;
  width: 56px;
  height: 56px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: linear-gradient(160deg, #ffe9c7, #ffcf8f);
}

.mode-icon.ai {
  background: linear-gradient(160deg, #d6e4ff, #a9c2ff);
}

.mode-text h3 {
  margin: 0 0 4px;
  font-family: 'Baloo 2', cursive;
  font-size: 19px;
  color: var(--ink);
}

.mode-text p {
  margin: 0;
  font-size: 13px;
  color: var(--ink-soft);
  font-weight: 700;
}

.diff-row {
  display: flex;
  gap: 10px;
  padding: 6px 8px;
}

.diff-btn {
  flex: 1;
  padding: 16px 8px;
  border: none;
  border-radius: 16px;
  cursor: pointer;
  background: var(--card);
  font-family: 'Baloo 2', cursive;
  font-weight: 700;
  font-size: 16px;
  color: var(--ink);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.18);
  transition: transform .08s ease;
}

.diff-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
}

.diff-btn .emoji {
  display: block;
  font-size: 22px;
  margin-bottom: 4px;
}

/* Game screen */
#screen-game {
  padding-bottom: 10px;
}

.score-row {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 0 16px 8px;
}

.score-chip {
  flex: 1;
  max-width: 150px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  font-family: 'Baloo 2', cursive;
  font-weight: 700;
  color: #fff;
  font-size: 15px;
}

.score-chip .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.score-chip .dot.r {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
}

.score-chip .dot.y {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
}

.score-chip .dot.d {
  background: linear-gradient(160deg, #ddd, #999);
}

.canvas-wrap {
  flex: 1;
  position: relative;
  margin: 2px 8px 0;
  min-height: 0;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.thinking-badge {
  position: absolute;
  left: 50%;
  top: 10px;
  transform: translate(-50%, -140%);
  background: rgba(20, 10, 50, 0.85);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  padding: 8px 16px;
  border-radius: 30px;
  white-space: nowrap;
  transition: transform .3s ease, opacity .3s ease;
  opacity: 0;
  pointer-events: none;
  font-family: 'Nunito', sans-serif;
  display: flex;
  align-items: center;
  gap: 8px;
}

.thinking-badge.show {
  transform: translate(-50%, 0);
  opacity: 1;
}

.thinking-dots {
  display: flex;
  gap: 3px;
}

.thinking-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--yellow-2);
  animation: tdot 1s infinite ease-in-out;
}

.thinking-dots span:nth-child(2) {
  animation-delay: .15s;
}

.thinking-dots span:nth-child(3) {
  animation-delay: .3s;
}

@keyframes tdot {

  0%,
  80%,
  100% {
    opacity: .25;
    transform: scale(0.7);
  }

  40% {
    opacity: 1;
    transform: scale(1);
  }
}

.bottombar {
  display: flex;
  justify-content: center;
  padding: 12px 16px 18px;
}

.bottombar .pill-btn {
  min-width: 200px;
}

/* Modals */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 5, 30, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  opacity: 0;
  visibility: hidden;
  transition: opacity .25s ease, visibility .25s ease;
  padding: 20px;
}

.modal-overlay.show {
  opacity: 1;
  visibility: visible;
}

.modal-card {
  background: var(--card);
  border-radius: 26px;
  padding: 26px 22px 22px;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 14px 0 rgba(0, 0, 0, 0.15), 0 20px 40px rgba(0, 0, 0, 0.3);
  transform: translateY(14px) scale(0.96);
  transition: transform .3s ease;
}

.modal-overlay.show .modal-card {
  transform: translateY(0) scale(1);
}

.modal-title {
  font-family: 'Baloo 2', cursive;
  font-weight: 800;
  font-size: 26px;
  color: var(--ink);
  margin: 0 0 6px;
}

.modal-sub {
  color: var(--ink-soft);
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 16px;
}

.modal-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}

.win-disc-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin: 0 auto 12px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
}

.win-disc-icon.r {
  background: radial-gradient(circle at 32% 28%, var(--red-1), var(--red-2) 55%, var(--red-3));
}

.win-disc-icon.y {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
}

.win-disc-icon.draw {
  background: linear-gradient(160deg, #ddd, #999);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
}

.htp-diagram {
  background: #f0eaff;
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 14px;
}

.htp-grid {
  display: grid;
  grid-template-columns: repeat(4, 28px);
  grid-gap: 5px;
  justify-content: center;
  margin-bottom: 8px;
}

.htp-cell {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #c9c0ea;
}

.htp-cell.on {
  background: radial-gradient(circle at 32% 28%, var(--yellow-1), var(--yellow-2) 55%, var(--yellow-3));
  box-shadow: 0 0 0 3px rgba(244, 180, 0, 0.35);
}

.htp-text {
  font-size: 13.5px;
  color: var(--ink);
  font-weight: 600;
  line-height: 1.5;
  text-align: left;
}

.htp-text b {
  color: var(--purple-btn-2);
}

.mute-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #f0eaff;
  border-radius: 16px;
  padding: 10px 16px;
  margin-bottom: 14px;
  font-weight: 700;
  color: var(--ink);
}

.switch {
  width: 46px;
  height: 26px;
  border-radius: 16px;
  background: #cbb98a;
  position: relative;
  cursor: pointer;
  transition: background .2s ease;
}

.switch.on {
  background: var(--green);
}

.switch-knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  transition: left .2s ease;
}

.switch.on .switch-knob {
  left: 23px;
}

.hidden {
  display: none !important;
}

@media (max-height:640px) {
  .game-title {
    font-size: 32px;
  }

  .home-discs {
    width: 150px;
    height: 56px;
  }

  .hdc {
    width: 42px;
    height: 42px;
  }
}