:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --text: #1a202c;
  --text-muted: #64748b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --player: #ef4444;
  --ai: #eab308;
  --board: #1e3a5f;
  --board-hole: #0f172a;
  --shadow: 0 6px 16px -4px rgb(0 0 0 / 0.12);
  --radius: 14px;
  --transition: 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  --cell-gap: 6px;
  --board-pad: 10px;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #60a5fa;
  --primary-hover: #3b82f6;
  --board: #0c1929;
  --board-hole: #020617;
  --shadow: 0 6px 16px -4px rgb(0 0 0 / 0.4);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px 8px;
  transition: background var(--transition), color var(--transition);
  overflow: hidden;
}

.container {
  width: 100%;
  max-width: 520px;
  height: 100%;
  max-height: 100dvh;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow: hidden;
}

/* Header - compact, title centered */
header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  flex-shrink: 0;
  gap: 6px;
  min-height: 36px;
  position: relative;
}

h1 {
  font-size: clamp(1.05rem, 4.2vw, 1.45rem);
  font-weight: 700;
  letter-spacing: -0.4px;
  white-space: nowrap;
  text-align: center;
  grid-column: 2;
  margin: 0;
}

.controls {
  display: flex;
  gap: 5px;
  align-items: center;
  flex-wrap: nowrap;
  grid-column: 3;
  justify-self: end;
}

button, select {
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  padding: 6px 10px;
  cursor: pointer;
  transition: all var(--transition);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  white-space: nowrap;
}

button:hover { filter: brightness(1.06); }
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-special {
  background: linear-gradient(135deg, #8b5cf6, #ec4899);
  color: white;
}

select {
  appearance: none;
  padding-left: 22px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 8px center;
}
[data-theme="dark"] select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
}

.theme-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  padding: 0;
  flex-shrink: 0;
}

/* Status */
.status {
  text-align: center;
  font-size: clamp(0.9rem, 3.5vw, 1.05rem);
  font-weight: 600;
  flex-shrink: 0;
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.status .dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  display: inline-block;
}

/* ===== Board (flexible) ===== */
.board-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.board-wrapper {
  position: relative;
  background: var(--board);
  border-radius: var(--radius);
  padding: var(--board-pad);
  box-shadow: var(--shadow);
  user-select: none;
  width: 100%;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: auto;
  transition: transform 0.3s ease;
}
.board-wrapper.shake {
  animation: shake 0.35s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-3px); }
}

.board {
  display: grid;
  gap: var(--cell-gap);
  width: 100%;
  height: 100%;
}

.cell {
  aspect-ratio: 1;
  background: var(--board-hole);
  border-radius: 50%;
  position: relative;
  cursor: pointer;
  transition: transform 0.12s ease;
  overflow: hidden;
}
.cell:hover:not(.filled) {
  transform: scale(1.04);
}

.cell .disc {
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  transform: scale(0);
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.cell .disc.dropped { transform: scale(1); }
.cell .disc.bounce {
  animation: bounceLand 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes bounceLand {
  0% { transform: scale(1); }
  30% { transform: scale(1.12, 0.88); }
  55% { transform: scale(0.92, 1.08); }
  100% { transform: scale(1); }
}

.cell .disc.player {
  background: radial-gradient(circle at 32% 32%, color-mix(in srgb, var(--player) 72%, white), var(--player));
  box-shadow: inset -2px -2px 6px rgba(0,0,0,0.25);
}
.cell .disc.ai {
  background: radial-gradient(circle at 32% 32%, color-mix(in srgb, var(--ai) 72%, white), var(--ai));
  box-shadow: inset -2px -2px 6px rgba(0,0,0,0.25);
}

.cell.win .disc {
  animation: pulseWin 0.5s ease infinite alternate;
}
@keyframes pulseWin {
  from { transform: scale(1); filter: brightness(1); }
  to { transform: scale(1.08); filter: brightness(1.25); }
}

.preview-disc {
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  opacity: 0.32;
  pointer-events: none;
}
.preview-disc.player {
  background: radial-gradient(circle at 32% 32%, color-mix(in srgb, var(--player) 72%, white), var(--player));
}

.falling-disc {
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
  z-index: 20;
  transition: top 0.34s cubic-bezier(0.22, 1, 0.36, 1);
}
.falling-disc.player {
  background: radial-gradient(circle at 32% 32%, color-mix(in srgb, var(--player) 72%, white), var(--player));
}
.falling-disc.ai {
  background: radial-gradient(circle at 32% 32%, color-mix(in srgb, var(--ai) 72%, white), var(--ai));
}

.particle {
  position: absolute;
  width: 7px; height: 7px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 30;
  animation: particleFly 0.8s ease-out forwards;
}
@keyframes particleFly {
  0% { transform: translate(0,0) scale(1); opacity: 1; }
  100% { transform: translate(var(--tx), var(--ty)) scale(0); opacity: 0; }
}

/* Powers */
.powers {
  display: flex;
  gap: 5px;
  justify-content: center;
  flex-shrink: 0;
  flex-wrap: nowrap;
}
.power-btn {
  font-size: 0.72rem;
  padding: 5px 9px;
  position: relative;
}
.power-btn .badge {
  position: absolute;
  top: -5px; left: -5px;
  background: #ef4444;
  color: white;
  font-size: 0.6rem;
  width: 15px; height: 15px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Score */
.score-bar {
  display: flex;
  justify-content: space-around;
  background: var(--surface);
  border-radius: 12px;
  padding: 6px 4px;
  box-shadow: var(--shadow);
  font-weight: 600;
  flex-shrink: 0;
}
.score-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
}
.score-item span:first-child {
  font-size: 0.65rem;
  color: var(--text-muted);
}
.score-item span:last-child {
  font-size: 1.05rem;
  line-height: 1.2;
}

/* Settings - compact */
.settings {
  background: var(--surface);
  border-radius: 12px;
  padding: 8px;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 6px;
  flex-shrink: 0;
}
.settings label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.65rem;
  color: var(--text-muted);
  font-weight: 600;
}
.settings input[type="color"] {
  width: 100%;
  height: 28px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: transparent;
  padding: 0;
}
.settings select {
  width: 100%;
  padding: 4px 6px;
  padding-left: 18px;
  font-size: 0.72rem;
}

.footer {
  text-align: center;
  font-size: 0.62rem;
  color: var(--text-muted);
  line-height: 1.3;
  flex-shrink: 0;
  opacity: 0.95;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}
.footer-tips {
  opacity: 0.8;
}
.copyright {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.03em;
}
.footer-logo {
  height: 44px;
  width: auto;
  max-width: 220px;
  object-fit: contain;
  margin-top: 2px;
  opacity: 1;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  padding: 12px;
}
.modal-overlay.show {
  opacity: 1;
  pointer-events: auto;
}
.modal {
  background: var(--surface);
  border-radius: 18px;
  padding: 22px 18px;
  text-align: center;
  box-shadow: var(--shadow);
  transform: scale(0.9);
  transition: transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 300px;
  width: 100%;
}
.modal-overlay.show .modal { transform: scale(1); }
.modal h2 { font-size: 1.35rem; margin-bottom: 4px; }
.modal p { color: var(--text-muted); margin-bottom: 16px; font-size: 0.9rem; }
.modal-buttons { display: flex; gap: 8px; justify-content: center; }

.cell.hint {
  box-shadow: inset 0 0 0 2.5px #22c55e, 0 0 10px #22c55e;
  animation: hintPulse 0.7s ease infinite alternate;
}
@keyframes hintPulse {
  from { box-shadow: inset 0 0 0 2.5px #22c55e, 0 0 6px #22c55e; }
  to { box-shadow: inset 0 0 0 2.5px #4ade80, 0 0 12px #4ade80; }
}

/* ===== Very small screens / short height ===== */
@media (max-height: 700px) {
  body { padding: 4px 6px; }
  .container { gap: 4px; }
  .settings { padding: 6px; gap: 4px; }
  .settings label { font-size: 0.6rem; }
  .settings input[type="color"] { height: 24px; }
  .score-bar { padding: 4px; }
  .score-item span:last-child { font-size: 0.95rem; }
  .footer-tips { display: none; }
  .footer-logo { height: 32px; }
  .copyright { font-size: 0.95rem; }
  :root { --cell-gap: 4px; --board-pad: 7px; }
}

@media (max-height: 580px) {
  h1 { font-size: 1rem; }
  .status { font-size: 0.85rem; min-height: 20px; }
  .powers { gap: 3px; }
  .power-btn { font-size: 0.65rem; padding: 4px 7px; }
  .settings { grid-template-columns: 1fr 1fr; }
  button, select { font-size: 0.7rem; padding: 4px 7px; }
  .theme-btn { width: 30px; height: 30px; font-size: 0.9rem; }
  :root { --cell-gap: 3px; --board-pad: 5px; }
}

@media (max-width: 380px) {
  .settings { grid-template-columns: 1fr 1fr; }
  .controls select { max-width: 78px; }
}

@media (max-width: 320px) {
  h1 { font-size: 0.95rem; }
  .power-btn { font-size: 0.6rem; padding: 3px 5px; }
}
