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

html, 
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #00361a;
  color: #f5f5f5;
}

#app {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: stretch;
  justify-content: center;
}

#table {
  flex: 1;
  max-width: 820px;
  margin: auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at 10% -20%, #2e7d32 0, #1b5e20 40%, #0c310c 90%);
  padding: 6px 10px 8px;
}

/* Top info bar */

#info-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(10px);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.05), 0 10px 25px rgba(0, 0, 0, 0.8);
  margin-bottom: 6px;

  /* Prevent the top info bar from stretching with the table content */
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
}

#turn-indicator {
  font-weight: 600;
}

#current-color {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
}

#current-color span:first-child {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  opacity: 0.9;
}

#current-color-swatch {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  border: 2px solid rgba(255, 255, 255, 0.5);
}

#direction-indicator {
  font-size: 11px;
  opacity: 0.85;
}

/* Audio controls */
#audio-controls {
  display: inline-flex;
  gap: 8px;
  align-items: center;
}

/* New rules icon button */
.icon-btn {
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.06);
  color: #fff;
  padding: 8px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 8px;
}
.icon-btn svg { display: block; color: #fff; }
.icon-btn:active { transform: translateY(1px) scale(0.995); }

/* Player avatar in top bar */
#player-avatar {
  width: 40px;
  height: 40px;
  margin-left: 8px;
  border-radius: 10px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.06);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
#avatar-swatch {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 2px solid rgba(0,0,0,0.2);
  box-shadow: 0 4px 12px rgba(0,0,0,0.6) inset;
}

/* Leaderboard panel */
#leaderboard-panel {
  position: fixed;
  top: 64px;
  right: 20px;
  width: 260px;
  max-height: 360px;
  overflow: auto;
  padding: 10px;
  border-radius: 12px;
  background: rgba(8, 12, 10, 0.98);
  border: 1px solid rgba(255,255,255,0.06);
  box-shadow: 0 14px 36px rgba(0,0,0,0.8);
  z-index: 100; /* above the table but below overlay */
  display: none;
}
#leaderboard-panel.visible { display: block; }
#leaderboard-panel h4 { margin: 0 0 8px 0; font-size: 13px; text-transform: uppercase; }
.leaderboard-entry {
  display:flex;
  align-items:center;
  gap:8px;
  padding:8px;
  border-radius:8px;
  background: rgba(255,255,255,0.02);
  margin-bottom:6px;
}
.leaderboard-entry .avatar {
  width:36px;height:36px;border-radius:8px;border:1px solid rgba(0,0,0,0.2);
}
.leaderboard-entry .meta { flex:1; display:flex; justify-content:space-between; align-items:center; font-size:13px; }
.leaderboard-empty { text-align:center; opacity:0.7; padding:18px 6px; font-size:13px; }

/* Rules panel (compact) */
#rules-panel {
  position: fixed;
  top: 54px;
  right: 20px;
  width: 220px;
  padding: 10px;
  border-radius: 12px;
  background: rgba(8, 12, 10, 0.96);
  border: 1px solid rgba(255,255,255,0.06);
  box-shadow: 0 14px 36px rgba(0,0,0,0.8);
  z-index: 90; /* above table but below overlay */
  display: none;
  transform-origin: top right;
  animation: panelIn 180ms cubic-bezier(.2,.9,.2,1);
}
@keyframes panelIn {
  from { transform: translateY(-6px) scale(0.98); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}
#rules-panel.visible { display: block; }

#rules-panel h4 {
  margin: 0 0 8px 0;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  opacity: 0.9;
}

.rule-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  margin-bottom: 6px;
  border-radius: 8px;
  cursor: pointer;
  background: rgba(255,255,255,0.02);
  transition: background 120ms ease, transform 120ms ease;
}
.rule-option:hover { background: rgba(255,255,255,0.04); transform: translateY(-2px); }
.rule-option .label { font-size: 13px; }
.rule-option .badge { font-size: 11px; padding: 4px 8px; border-radius: 999px; opacity: 0.95; }

/* ensure panel fits mobile top-right */
@media (max-width: 520px) {
  #rules-panel { right: 10px; top: 60px; width: 200px; }
  #leaderboard-panel { right: 10px; top: 60px; width: 200px; }
}

#mute-btn {
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.06);
  color: #fff;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  min-width: 40px;
  height: 34px;
}

#mute-btn.muted {
  opacity: 0.5;
  filter: grayscale(0.2);
}

#volume-slider {
  width: 86px;
  height: 24px;
  accent-color: #fdd835;
  background: transparent;
}

/* Center row */

#center-row {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 4px 0;
}

/* AI area */

#ai-area {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.opponent {
  flex: 1;
  padding: 6px 6px 4px;
  border-radius: 12px;
  background: radial-gradient(circle at top, rgba(0, 0, 0, 0.8), rgba(5, 10, 9, 0.96));
  border: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.7);
  transition: box-shadow 0.2s ease, transform 0.2s ease, border-color 0.2s ease;
}

.opponent-active {
  border-color: rgba(255, 255, 255, 0.3);
  box-shadow: 0 0 0 1px rgba(245, 245, 245, 0.2), 0 10px 22px rgba(0, 0, 0, 0.9);
  transform: translateY(-2px);
}

.opponent-name {
  font-size: 11px;
  opacity: 0.85;
  margin-bottom: 3px;
  display: flex;
  justify-content: space-between;
}

.opponent-hand {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 2px;
  min-height: 20px;
}

.opponent-card-back {
  width: 14px;
  height: 22px;
  border-radius: 4px;
  background: linear-gradient(135deg, #e53935, #fdd835, #43a047, #1e88e5);
  border: 1px solid rgba(0, 0, 0, 0.8);
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.7);
}

.opponent-count-extra {
  font-size: 10px;
  margin-left: 3px;
  opacity: 0.8;
}

.opponent-badge {
  margin-left: 4px;
}

/* Piles */

#piles-area {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
}

.pile {
  width: 70px;
  height: 102px;
  border-radius: 12px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

#draw-pile {
  background: radial-gradient(circle at 20% 0%, #333 0, #141414 50%, #050505 100%);
  border: 2px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.85);
  cursor: pointer;
  transition: transform 0.1s ease-out;
}

/* Card drawn flash effect (Draw Pile) */
.card-drawn-flash {
    transform: scale(0.95);
    opacity: 0.8;
}

#discard-pile {
  background: radial-gradient(circle at 80% 0%, #171717 0, #050505 50%, #000000 95%);
  border: 2px dashed rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.85);
}

/* Card played flash effect (Discard Pile) */
.card-played-flash {
  animation: pileFlash 0.25s ease-out;
}

@keyframes pileFlash {
  0% {
    transform: scale(1);
    box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.9), 0 8px 18px rgba(0, 0, 0, 0.85);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 8px 18px rgba(0, 0, 0, 0.85);
  }
}

.pile-label {
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  letter-spacing: 0.9px;
  text-transform: uppercase;
  opacity: 0.9;
}

/* Draw pile active pulse */

.pile-active {
  animation: pilePulse 1.1s ease-in-out infinite;
}

@keyframes pilePulse {
  0% {
    box-shadow: 0 0 0 0 rgba(253, 216, 53, 0.35), 0 8px 18px rgba(0, 0, 0, 0.85);
    border-color: rgba(253, 216, 53, 0.7);
  }
  60% {
    box-shadow: 0 0 0 12px rgba(253, 216, 53, 0), 0 10px 22px rgba(0, 0, 0, 0.95);
    border-color: rgba(253, 216, 53, 0.3);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(253, 216, 53, 0), 0 8px 18px rgba(0, 0, 0, 0.9);
    border-color: rgba(253, 216, 53, 0.2);
  }
}

/* Player area */

#player-area {
  padding: 6px 4px 0;
  border-radius: 16px 16px 10px 10px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.9), rgba(4, 9, 8, 0.7));
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 -8px 20px rgba(0, 0, 0, 0.9);
  position: relative;
}

#player-area.hand-updated #player-hand .card {
    /* Suppress transitions for smooth entry */
    transition: opacity 0.1s ease, transform 0.16s ease !important; 
}

#player-area.hand-updated #player-hand .card:last-child {
    opacity: 0;
    transform: translateY(40px) scale(0.8);
    animation: cardEntry 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) forwards; /* Bouncy entry */
}

@keyframes cardEntry {
    100% {
        opacity: 1;
        /* Reset transform, allowing .playable styles to apply immediately after animation end */
        transform: none; 
    }
}

#player-area::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  pointer-events: none;
}

.player-active {
  box-shadow: 0 -10px 26px rgba(253, 216, 53, 0.32);
}

#player-hand {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 4px;
  padding: 6px 2px 8px;
  overflow: visible;
}

/* Cards */

.card {
  width: 50px;
  height: 76px;
  border-radius: 11px;
  border: 2px solid rgba(0, 0, 0, 0.75);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.7);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 20px;
  color: #fff;
  user-select: none;
  touch-action: manipulation;
  transition: transform 0.16s ease, box-shadow 0.16s ease, filter 0.16s ease, opacity 0.16s ease;
}

.card-inner {
  width: 82%;
  height: 82%;
  border-radius: 9px;
  border: 2px solid rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-value {
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
}

.card-small {
  position: absolute;
  top: 4px;
  left: 6px;
  font-size: 11px;
}

.card-small-bottom {
  position: absolute;
  bottom: 4px;
  right: 6px;
  font-size: 11px;
}

/* Card colors */

.card.color-red {
  background: radial-gradient(circle at 25% 0%, #ff7961 0, #f44336 40%, #b71c1c 100%);
}

.card.color-yellow {
  background: radial-gradient(circle at 20% 0%, #fff59d 0, #ffeb3b 40%, #f9a825 100%);
  color: #2c2c2c;
}

.card.color-green {
  background: radial-gradient(circle at 20% 0%, #a5d6a7 0, #66bb6a 40%, #1b5e20 100%);
}

.card.color-blue {
  background: radial-gradient(circle at 20% 0%, #90caf9 0, #42a5f5 40%, #0d47a1 100%);
}

.card.color-wild {
  background: conic-gradient(
    from 210deg,
    #e53935,
    #fdd835,
    #43a047,
    #1e88e5,
    #e53935
  );
}

/* Card states */

.card.playable {
  cursor: pointer;
  transform: translateY(-6px);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.95);
}

.card.playable:active {
  transform: translateY(-2px) scale(0.97);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.85);
}

.card.unplayable {
  opacity: 0.55;
  filter: grayscale(0.15);
}

/* Discard pile card preview */

#discard-pile .card {
  width: 54px;
  height: 78px;
}

/* Overlay */

#overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at center, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.95));
  z-index: 20;
}

#overlay.hidden {
  display: none;
}

#overlay-content {
  min-width: 220px;
  max-width: 280px;
  padding: 16px 14px 12px;
  background: rgba(5, 9, 9, 0.98);
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.9);
}

#overlay-message {
  font-size: 14px;
  margin-bottom: 10px;
  text-align: center;
}

#overlay-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

/* New color picker styles */
.color-picker {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
  justify-items: center;
}

.color-swatch-btn {
  width: 88px;
  height: 44px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.08);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 700;
  color: #111;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,0.6);
  transition: transform 140ms ease, box-shadow 140ms ease, opacity 120ms ease;
}

.color-swatch-btn span.dot {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  border: 1px solid rgba(0,0,0,0.2);
  box-shadow: 0 2px 6px rgba(0,0,0,0.35) inset;
}

.color-swatch-btn:active { transform: translateY(1px) scale(0.99); }
.color-swatch-btn:focus { outline: 3px solid rgba(255,255,255,0.08); }

/* color-specific backgrounds */
.color-red { background: linear-gradient(90deg,#ff8a80,#e53935); color: #fff; }
.color-yellow { background: linear-gradient(90deg,#fff59d,#fdd835); color: #111; }
.color-green { background: linear-gradient(90deg,#a5d6a7,#43a047); color: #fff; }
.color-blue { background: linear-gradient(90deg,#90caf9,#1e88e5); color: #fff; }

/* smaller cancel button spacing */
.overlay-btn.secondary { min-width: 84px; padding: 8px 10px; }

/* ensure overlay content isn't too tall on mobile */
#overlay-content { max-height: 70vh; overflow: auto; }

.overlay-btn {
  min-width: 72px;
  padding: 6px 10px;
  border-radius: 999px;
  border: none;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  background: #2e7d32;
}

.overlay-btn.secondary {
  background: #424242;
}

/* Utility */

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.badge-red {
  background: rgba(244, 67, 54, 0.9);
}

.badge-yellow {
  background: rgba(255, 235, 59, 0.9);
  color: #222;
}

.badge-green {
  background: rgba(76, 175, 80, 0.9);
}

.badge-blue {
  background: rgba(33, 150, 243, 0.9);
}

@media (min-width: 700px) {
  #table {
    border-radius: 20px;
    margin: 8px auto;
  }

  #info-bar {
    font-size: 13px;
    padding-inline: 14px;
  }

  #player-hand .card {
    width: 60px;
    height: 88px;
  }

  #draw-pile, 
  #discard-pile {
    width: 80px;
    height: 116px;
  }
}

/* Card micro-interactions */
.card {
  will-change: transform, box-shadow;
  transition: transform 180ms cubic-bezier(.2,.9,.2,1), box-shadow 180ms ease, opacity 160ms ease;
  transform-origin: 50% 85%;
  perspective: 800px;
}

.card:active {
  transform: translateY(-2px) scale(0.985) rotateX(6deg);
}

.card:hover.playable {
  transform: translateY(-10px) scale(1.04) rotateX(6deg);
  box-shadow: 0 16px 30px rgba(0,0,0,0.9);
}

/* Opponent play flash */
.opponent-card-back.played {
  animation: opponentPlay 280ms ease-out;
}
@keyframes opponentPlay {
  0% { transform: translateY(0) scale(1); opacity: 1; filter: none; }
  50% { transform: translateY(-18px) scale(1.04); opacity: 0.95; filter: brightness(1.12); }
  100% { transform: translateY(0) scale(1); opacity: 1; filter: none; }
}

/* Opponent UNO alert (flashing) */
.opponent-uno {
  animation: unoFlash 900ms steps(2, end) infinite;
  position: relative;
  z-index: 8;
}
@keyframes unoFlash {
  0% { box-shadow: 0 0 0 0 rgba(255, 235, 59, 0.0), 0 6px 18px rgba(0,0,0,0.6); }
  50% { box-shadow: 0 0 0 8px rgba(255, 87, 34, 0.22), 0 12px 28px rgba(0,0,0,0.75); transform: translateY(-2px); }
  100% { box-shadow: 0 0 0 0 rgba(255, 235, 59, 0.0), 0 6px 18px rgba(0,0,0,0.6); transform: translateY(0); }
}

/* UNO badge (prominent near name) */
.opponent-uno-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: linear-gradient(90deg,#ff7043,#ffab40);
  color: #fff;
  padding: 6px 8px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 11px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.5);
  z-index: 12;
}

/* Opponent card count (always visible) */
.opponent-count {
  position: absolute;
  bottom: -6px;
  right: 8px;
  background: rgba(0,0,0,0.6);
  padding: 4px 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  border: 1px solid rgba(255,255,255,0.04);
  z-index: 9;
}

/* Draw pile "explode" visual for penalty (small burst) */
.pile-explode {
  animation: explodeBurst 520ms ease-out forwards;
}
@keyframes explodeBurst {
  0% { transform: scale(1); box-shadow: 0 8px 18px rgba(255,255,255,0.04); }
  30% { transform: scale(1.12) rotate(-6deg); box-shadow: 0 14px 42px rgba(255,87,34,0.35); }
  60% { transform: scale(0.95) rotate(6deg); box-shadow: 0 18px 60px rgba(255,87,34,0.18); }
  100% { transform: scale(1); box-shadow: 0 8px 18px rgba(255,255,255,0.04); }
}

/* small particles overlay anchored to pile (created/removed by JS) */
.pile-particles {
  pointer-events: none;
  position: absolute;
  inset: 0;
  z-index: 40;
}
.pile-particle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  opacity: 0.95;
  transform: translate(-50%, -50%);
  mix-blend-mode: screen;
  animation: particleFall 700ms cubic-bezier(.2,.9,.2,1) forwards;
}
@keyframes particleFall {
  to { transform: translate(var(--dx, 0px), var(--dy, 80px)) rotate(270deg) scale(0.6); opacity: 0; }
}

/* ensure relative positioning for opponent boxes so badges position correctly */
.opponent { position: relative; }

/* Confetti canvas sits above table but below overlays */
#confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 15;
  display: none;
}