:root {
  --bg: #1a1a1a;
  --board-color: #deb887; 
  /* Point this to your local file name */
  --board-img: url('board.jpeg'); 
  --grid-color: #2a2a2a;
  --text: #e7ecff;
  --dim: #9aa6d6;
  --shadow: 0 12px 40px rgba(0,0,0,0.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, sans-serif;
  background: radial-gradient(circle at center, #2c3e50 0%, #000 100%);
  color: var(--text);
  overflow-x: hidden;
}

.hidden { 
  display: none !important; 
}
.topbar { display: flex; align-items: flex-start; justify-content: space-between; padding: 18px; gap: 16px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; font-weight: 800; background: linear-gradient(135deg, #444, #000); border: 1px solid rgba(255,255,255,0.15); }
.title { font-weight: 800; font-size: 18px; }
.subtitle { color: var(--dim); font-size: 12px; }

.controls { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.control { display: flex; flex-direction: column; gap: 6px; }
.control label { font-size: 12px; color: var(--dim); }
select { background: rgba(255,255,255,.06); color: var(--text); border: 1px solid rgba(255,255,255,.14); border-radius: 10px; padding: 10px; outline: none; }

.btn { background: #f0f0f0; color: #111; border: 0; border-radius: 12px; padding: 10px 14px; font-weight: 800; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.3); transition: transform .08s ease; }
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: rgba(255,255,255,.06); color: var(--text); border: 1px solid rgba(255,255,255,.16); }

.main { display: grid; grid-template-columns: 1fr 320px; gap: 16px; padding: 18px; }
@media (max-width: 980px) { .main { grid-template-columns: 1fr; } }
.panel { background: rgba(255,255,255,0.03); border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; padding: 20px; box-shadow: var(--shadow); }

.board-wrap { 
  position: relative; 
  background-color: var(--board-color);
  background-image: var(--board-img);
  background-size: cover;
  background-position: center;
  padding: 0; 
  border: 4px solid var(--grid-color); 
  border-radius: 2px; 
  margin: 0 auto; 
  box-shadow: var(--shadow), inset 0 0 100px rgba(0,0,0,0.3); /* Darkened inner shadow slightly for rich wood feel */
  overflow: visible; 
}

.board-wrap.no-border { 
  border: none; 
  background-color: var(--board-color); 
  background-image: var(--board-img);
  background-size: cover;
  background-position: center;
  box-shadow: var(--shadow); 
}

.board { display: grid; width: min(85vmin, 720px); height: min(85vmin, 720px); margin: 0 auto; position: relative; overflow: visible; }

.cell { position: relative; background: none; border: none; cursor: pointer; aspect-ratio: 1 / 1; }
.cell::before { content: ""; position: absolute; top: 50%; left: 0; right: 0; height: 1.5px; background: var(--grid-color); transform: translateY(-50%); z-index: 1; }
.cell::after { content: ""; position: absolute; left: 50%; top: 0; bottom: 0; width: 1.5px; background: var(--grid-color); transform: translateX(-50%); z-index: 1; }

.cell.edge-top::after { top: 50%; }
.cell.edge-bottom::after { bottom: 50%; }
.cell.edge-left::before { left: 50%; }
.cell.edge-right::before { right: 50%; }

.piece { position: absolute; inset: 8%; border-radius: 50%; z-index: 5; opacity: 0; transition: opacity 500ms ease, transform 200ms ease; box-shadow: 3px 6px 10px rgba(0,0,0,0.6); }
.piece.p1 { background: radial-gradient(circle at 35% 35%, #555 0%, #000 75%); }
.piece.p2 { background: radial-gradient(circle at 35% 35%, #fff 0%, #dcdcdc 85%); border: 1px solid rgba(0,0,0,0.1); }

.pop-in { animation: stonePop .2s ease-out forwards; }
@keyframes stonePop { 0% { transform: scale(1.25); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }

/* --- Slow Fade Out --- */
.fade-out { 
  animation: fadeOut 2s ease forwards !important; 
}

@keyframes fadeOut { 
  /* Explicitly tell it to start at full visibility so it doesn't drop instantly */
  0% { opacity: 1; transform: scale(1); } 
  100% { opacity: 0; transform: scale(0.95); } 
}

/* Only reveal pieces that actually belong to a player */
.reveal .piece.p1,
.reveal .piece.p2 {
  opacity: 1 !important;
  transform: scale(1) !important;
  animation: none !important;
}

.win-line { z-index: 10; }
.win-line::before, .win-line::after { background: #ffcf40 !important; box-shadow: 0 0 12px #ffcf40; height: 2.5px !important; width: 2.5px !important; }

.side { display: flex; flex-direction: column; gap: 12px; }
.legend, .mini { background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; padding: 14px; }
.turn-box { display: flex; align-items: center; justify-content: space-between; padding: 12px; border-radius: 12px; background: rgba(0,0,0,0.3); }
/*.turn-piece { width: 24px; height: 24px; border-radius: 50%; box-shadow: 2px 3px 5px rgba(0,0,0,0.4); }
.turn-piece.p1 { background: radial-gradient(circle at 30% 30%, #555, #000); }
.turn-piece.p2 { background: radial-gradient(circle at 30% 30%, #fff, #ccc); border: 1px solid #999; }*/
/* Base shape with a crisp white border for contrast */
.turn-piece { 
    width: 24px; 
    height: 24px; 
    border-radius: 50%; 
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.6); 
    border: 2px solid rgba(255, 255, 255, 0.85); /* The new white border */
}

/* Black Pill */
.turn-piece.p1 { 
    background: radial-gradient(circle at 30% 30%, #555, #000); 
}

/* White Pill */
.turn-piece.p2 { 
    background: radial-gradient(circle at 30% 30%, #fff, #ccc); 
    border: 2px solid #aaa; /* A slightly darker grey border so the white piece doesn't wash out */
}

.meter-box { padding: 12px; border-radius: 14px; background: rgba(0,0,0,0.3); }
.meter { 
    display: grid; 
    grid-template-columns: repeat(7, 1fr); 
    gap: 6px; 
    padding: 8px; 
    background: #000; 
    border-radius: 10px; 
    flex-shrink: 0;
    
    /* These three lines make it match the button width */
    width: 80%;
    max-width: 300px;
    margin: 10px auto 0 auto;
}

.meter-slot { height: 16px; border-radius: 4px; background: #222; position: relative; }
.meter-slot.marker::after { content: ""; position: absolute; inset: 2px; border-radius: 2px; background: #fff; box-shadow: 0 0 5px #fff; }
.meter-slot.endcap { border: 1px solid #ff4b6e; }

/* --- Floating Bottom Overlay --- */
.overlay { 
  position: absolute; 
  inset: 0; 
  display: flex; 
  align-items: flex-end; /* Push the card to the bottom of the board */
  justify-content: center; 
  padding-bottom: 24px;
  /* Gradient backdrop: completely clear at the top, dark at the bottom for text contrast */
  background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.2) 60%, rgba(0,0,0,0.8) 100%); 
  border-radius: 2px; 
  z-index: 100; 
}

.overlay.hidden { 
  display: none; 
}

.overlay-card { 
  width: min(500px, 95%); 
  /* Slightly transparent dark grey with a glassmorphism blur */
  background: rgba(26, 26, 26, 0.90); 
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.15); 
  border-radius: 16px; 
  padding: 24px; 
  text-align: center; 
  box-shadow: 0 16px 40px rgba(0,0,0,0.6);
  /* Smooth entry animation */
  animation: slideUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes slideUp {
  0% { transform: translateY(30px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}

.overlay-title { 
  font-weight: 800; 
  font-size: 22px; 
  margin-bottom: 12px; 
  color: #ff4b6e; 
}

.overlay-body { 
  line-height: 1.6; 
  color: #ddd; 
  margin-bottom: 24px; 
  font-size: 15px;
}

/* Dynamic Swap2 Choice buttons */
.choice-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-bottom: 15px;
}

.choice-container .btn {
  flex: 1;
  min-width: 120px;
}

/* Dynamic Swap2 Choice buttons */
.choice-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-bottom: 15px;
}
.choice-container .btn {
  flex: 1;
  min-width: 120px;
}

/* Panic mode for the last 5 seconds */
.timer.panic {
  color: #ff4040 !important; /* Bright red */
  font-size: 20px;
  border-color: #ff4040;
  animation: shake 0.2s infinite;
}

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

.board.tictactoe .cell { background: none; }
.board.tictactoe .cell::before, .board.tictactoe .cell::after { display: none; }
.board.tictactoe .cell { border: 2px solid var(--grid-color); }
.board.tictactoe .cell:nth-child(-n + 3) { border-top: none; }
.board.tictactoe .cell:nth-last-child(-n + 3) { border-bottom: none; }
.board.tictactoe .cell:nth-child(3n + 1) { border-left: none; }
.board.tictactoe .cell:nth-child(3n) { border-right: none; }
.board.tictactoe .piece { inset: 15%; opacity: 0; }

.cell.hoshi::before { content: ""; position: absolute; top: 50%; left: 50%; width: 8px; height: 8px; background: var(--grid-color); border-radius: 50%; transform: translate(-50%, -50%); z-index: 2; }
.board:not(.tictactoe) .piece { inset: 6%; }

/* Glicko-2 Rank Up Banner */
.rank-up-banner {
  position: fixed;
  top: 20%;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #ffcf40, #ffab00);
  color: #000;
  padding: 20px 40px;
  border-radius: 15px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  z-index: 9999;
  border: 2px solid #fff;
  animation: popIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.rank-title { font-weight: 800; font-size: 24px; margin-bottom: 5px; }
.rank-math { font-family: monospace; font-size: 20px; font-weight: bold; }

@keyframes popIn {
  0% { transform: translateX(-50%) scale(0.5); opacity: 0; }
  100% { transform: translateX(-50%) scale(1); opacity: 1; }
}
/* World Rankings Sidebar UI */
.leaderboard-list {
  list-style: none;
  padding: 0;
  margin: 10px 0 0 0;
}

.leaderboard-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.03);
  margin-bottom: 4px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 13px;
}

.leaderboard-list .rank {
  font-weight: 800;
  color: #ffcf40; /* Gold for rank numbers */
  width: 25px;
}

.leaderboard-list .name {
  flex-grow: 1;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 10px;
}

.leaderboard-list .rating {
  font-family: monospace;
  font-weight: bold;
  color: #9aa6d6;
}

/* --- MOBILE LOCKDOWN --- */
html, body {
  overflow: hidden !important;
  touch-action: none !important; /* Kills iOS bouncing */
  padding-top: env(safe-area-inset-top); 
  padding-bottom: env(safe-area-inset-bottom);
}

#landing-page, #game-page {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
}

#landing-page { justify-content: center; align-items: center; gap: 30px; }

#game-top-bar {
  display: flex; 
  justify-content: space-between; 
  align-items: center; 
  padding: 12px 15px;
  
  /* The new background styles */
  background: rgba(255, 255, 255, 0.08); /* Soft, slightly transparent grey */
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4); /* Drops a shadow onto the board area */
}

/* Guarantees the board container perfectly fills the middle of the screen */
#board-container {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 0; /* CRITICAL: Prevents the board from pushing controls off screen */
    width: 100%;
}

/* Prevent the board from being crushed vertically */
#board {
    flex-shrink: 0;
    aspect-ratio: 1 / 1; 
}

.menu-container { display: flex; flex-direction: column; gap: 15px; width: 85%; max-width: 350px; }
.option-group { display: flex; justify-content: space-between; align-items: center; background: rgba(255,255,255,0.05); padding: 12px; border-radius: 8px; }

/* Matchmaking UI */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.85); z-index: 1000; display: flex; justify-content: center; align-items: center; }
.modal-content { background: #222; padding: 30px; border-radius: 16px; width: 85%; max-width: 350px; text-align: center; border: 1px solid rgba(255,255,255,0.1); }
.spinner { border: 4px solid rgba(255,255,255,0.1); border-top: 4px solid #ffcf40; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite; margin: 20px auto; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* --- Dynamic Player Display --- */
.player-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 14px;
    color: #888; /* Dimmed when not their turn */
    transition: color 0.3s ease;
    max-width: 110px;
}

.player-info span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.player-info.active {
    color: #ffcf40; /* Gold highlight for active player */
}

/* Minimize button inside the overlay card */
#minimize-overlay-btn {
    position: absolute;
    top: 10px;
    right: 15px;
    background: transparent;
    border: none;
    color: var(--text-color, #fff);
    font-size: 20px;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s;
    padding: 5px;
}

#minimize-overlay-btn:hover {
    opacity: 1;
}

/* Floating Restore Button */
.restore-btn {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-card, #2a2a35);
    color: var(--text-color, #fff);
    border: 2px solid var(--accent-gold, #ffcf40);
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    transition: background 0.2s, color 0.2s;
}

.restore-btn:hover {
    background: var(--accent-gold, #ffcf40);
    color: #000;
}


/* The Square Selection Cursor */
.selected-cell {
    outline: 3px solid #000;
    outline-offset: -3px; /* Pulls the border inside the cell */
    background-color: rgba(0, 0, 0, 0.25) !important;
    z-index: 5;
}


@keyframes pulse-ring {
    0% { transform: scale(0.85); opacity: 0.6; }
    50% { transform: scale(1.05); opacity: 1; }
    100% { transform: scale(0.85); opacity: 0.6; }
}

/* Compact Confirm Button */
#btn-place {
    width: 80%; /* Don't stretch all the way across */
    max-width: 300px; /* Keep it neat on tablets/desktop */
    margin: 10px auto; /* Centers it and reduces top/bottom gap */
    display: block; /* Ensures auto-margins work */
    background-color: var(--accent-gold, #ffcf40);
    color: #000;
    font-size: 16px; /* Slightly smaller text */
    font-weight: bold;
    padding: 10px; /* Less padding to save vertical space */
    border-radius: 8px;
    border: none;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    transition: transform 0.1s;
}

#btn-place:active {
    transform: scale(0.96);
}

#btn-place:disabled {
    background-color: #4a4a5a;
    color: #888;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
    opacity: 0.6;
}

/* Prevent the bottom controls and mistake meter from squishing */
#bottom-controls, 
/* Force Mistake Meter to perfectly match the Confirm button's width */
#mistakeMeter {
    width: 80%;
    max-width: 300px;
    margin: 10px auto 0 auto; /* Centers it and adds a small gap below the button */
    box-sizing: border-box;
}

#game-top-bar {
  padding-top: max(12px, env(safe-area-inset-top));
}
#bottom-controls {
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}

.info-card {
    max-width: 450px;
    background: #1a1a1a;
    border: 1px solid rgba(255, 207, 64, 0.3); /* Slight gold border */
}

.info-tabs {
    display: flex;
    gap: 5px;
    margin-bottom: 20px;
    border-bottom: 1px solid #333;
}

.tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: #888;
    padding: 10px;
    cursor: pointer;
    font-weight: bold;
}

.tab-btn.active {
    color: #ffcf40;
    border-bottom: 2px solid #ffcf40;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    padding: 12px;
    background: rgba(255,255,255,0.03);
    margin-bottom: 5px;
    border-radius: 8px;
}

.btn:disabled {
    background-color: #333 !important;
    color: #666 !important;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
    border: 1px solid #444;
}