/* ============================================================================
   Hex Dominion - Master Stylesheet
   A polished hex grid strategy game with glassmorphism panels,
   smooth transitions, and 5 color themes.
   ============================================================================ */

/* ─── CSS Custom Properties (Default: Midnight Neon) ──────────────────────── */

:root {
  /* Background */
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --bg-panel: rgba(20, 20, 50, 0.8);
  --bg-panel-solid: #14143280;
  --bg-overlay: rgba(5, 5, 15, 0.85);

  /* Borders */
  --border-color: #2a2a4a;
  --border-glow: rgba(123, 104, 238, 0.3);

  /* Text */
  --text-primary: #e0e0ff;
  --text-secondary: #8888aa;
  --text-dim: #555577;

  /* Player Colors */
  --player1-color: #00f0ff;
  --player1-light: rgba(0, 240, 255, 0.2);
  --player1-dark: rgba(0, 240, 255, 0.08);
  --player2-color: #ff00aa;
  --player2-light: rgba(255, 0, 170, 0.2);
  --player2-dark: rgba(255, 0, 170, 0.08);

  /* Accent & Status */
  --accent: #7b68ee;
  --accent-light: rgba(123, 104, 238, 0.25);
  --success: #00cc66;
  --error: #ff4444;
  --warning: #ffaa00;

  /* Shadows */
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
  --shadow-glow: 0 0 20px rgba(123, 104, 238, 0.15);

  /* Layout */
  --sidebar-width: 260px;
  --header-height: 48px;
  --border-radius-sm: 6px;
  --border-radius-md: 10px;
  --border-radius-lg: 16px;
  --border-radius-xl: 24px;

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
  --transition-slow: 400ms ease;

  /* Font */
  --font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "Cascadia Code", "Fira Code", "Consolas", monospace;
}

/* ─── Theme Overrides ─────────────────────────────────────────────────────── */

/* 1. Midnight Neon (default - defined in :root) */
[data-theme="midnight"] {
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --bg-panel: rgba(20, 20, 50, 0.8);
  --bg-panel-solid: #14143280;
  --bg-overlay: rgba(5, 5, 15, 0.85);
  --border-color: #2a2a4a;
  --border-glow: rgba(123, 104, 238, 0.3);
  --text-primary: #e0e0ff;
  --text-secondary: #8888aa;
  --text-dim: #555577;
  --player1-color: #00f0ff;
  --player1-light: rgba(0, 240, 255, 0.2);
  --player1-dark: rgba(0, 240, 255, 0.08);
  --player2-color: #ff00aa;
  --player2-light: rgba(255, 0, 170, 0.2);
  --player2-dark: rgba(255, 0, 170, 0.08);
  --accent: #7b68ee;
  --accent-light: rgba(123, 104, 238, 0.25);
  --success: #00cc66;
  --error: #ff4444;
  --warning: #ffaa00;
  --shadow-glow: 0 0 20px rgba(123, 104, 238, 0.15);
}

/* 2. Ocean Breeze */
[data-theme="ocean"] {
  --bg-primary: #0c2d48;
  --bg-secondary: #153a5a;
  --bg-panel: rgba(12, 45, 72, 0.82);
  --bg-panel-solid: #0c2d4880;
  --bg-overlay: rgba(8, 25, 42, 0.88);
  --border-color: #1a4a6a;
  --border-glow: rgba(58, 138, 191, 0.3);
  --text-primary: #d0e8f8;
  --text-secondary: #7aa8c8;
  --text-dim: #456880;
  --player1-color: #e0f0ff;
  --player1-light: rgba(224, 240, 255, 0.2);
  --player1-dark: rgba(224, 240, 255, 0.08);
  --player2-color: #ff6b6b;
  --player2-light: rgba(255, 107, 107, 0.2);
  --player2-dark: rgba(255, 107, 107, 0.08);
  --accent: #3a8abf;
  --accent-light: rgba(58, 138, 191, 0.25);
  --success: #2ecc71;
  --error: #e74c3c;
  --warning: #f39c12;
  --shadow-glow: 0 0 20px rgba(58, 138, 191, 0.15);
}

/* 3. Forest & Fire */
[data-theme="forest"] {
  --bg-primary: #0a1a0a;
  --bg-secondary: #0f2a0f;
  --bg-panel: rgba(15, 35, 15, 0.82);
  --bg-panel-solid: #0f230f80;
  --bg-overlay: rgba(5, 15, 5, 0.88);
  --border-color: #1a3a1a;
  --border-glow: rgba(85, 136, 51, 0.3);
  --text-primary: #c8e8c0;
  --text-secondary: #7aaa70;
  --text-dim: #4a6a44;
  --player1-color: #00cc66;
  --player1-light: rgba(0, 204, 102, 0.2);
  --player1-dark: rgba(0, 204, 102, 0.08);
  --player2-color: #ff8800;
  --player2-light: rgba(255, 136, 0, 0.2);
  --player2-dark: rgba(255, 136, 0, 0.08);
  --accent: #558833;
  --accent-light: rgba(85, 136, 51, 0.25);
  --success: #33cc33;
  --error: #cc3300;
  --warning: #ddaa00;
  --shadow-glow: 0 0 20px rgba(85, 136, 51, 0.15);
}

/* 4. Monochrome */
[data-theme="monochrome"] {
  --bg-primary: #1a1a1a;
  --bg-secondary: #222222;
  --bg-panel: rgba(30, 30, 30, 0.85);
  --bg-panel-solid: #1e1e1e80;
  --bg-overlay: rgba(10, 10, 10, 0.88);
  --border-color: #333333;
  --border-glow: rgba(100, 100, 100, 0.3);
  --text-primary: #cccccc;
  --text-secondary: #888888;
  --text-dim: #555555;
  --player1-color: #ffffff;
  --player1-light: rgba(255, 255, 255, 0.15);
  --player1-dark: rgba(255, 255, 255, 0.06);
  --player2-color: #888888;
  --player2-light: rgba(136, 136, 136, 0.15);
  --player2-dark: rgba(136, 136, 136, 0.06);
  --accent: #555555;
  --accent-light: rgba(85, 85, 85, 0.25);
  --success: #aaaaaa;
  --error: #cc4444;
  --warning: #bb9933;
  --shadow-glow: 0 0 20px rgba(100, 100, 100, 0.1);
}

/* 5. Sunset */
[data-theme="sunset"] {
  --bg-primary: #1a0a2e;
  --bg-secondary: #231545;
  --bg-panel: rgba(30, 15, 55, 0.82);
  --bg-panel-solid: #1e0f3780;
  --bg-overlay: rgba(15, 5, 30, 0.88);
  --border-color: #3a2a5a;
  --border-glow: rgba(122, 74, 191, 0.3);
  --text-primary: #e0d0f0;
  --text-secondary: #9980bb;
  --text-dim: #6a5588;
  --player1-color: #ffd700;
  --player1-light: rgba(255, 215, 0, 0.2);
  --player1-dark: rgba(255, 215, 0, 0.08);
  --player2-color: #9b30ff;
  --player2-light: rgba(155, 48, 255, 0.2);
  --player2-dark: rgba(155, 48, 255, 0.08);
  --accent: #7a4abf;
  --accent-light: rgba(122, 74, 191, 0.25);
  --success: #44bb88;
  --error: #ff5566;
  --warning: #eebb33;
  --shadow-glow: 0 0 20px rgba(122, 74, 191, 0.15);
}

/* ─── Keyframe Animations ─────────────────────────────────────────────────── */

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes fadeOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

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

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-40px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(40px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

@keyframes glow {
  0%, 100% {
    box-shadow: 0 0 8px var(--accent-light);
  }
  50% {
    box-shadow: 0 0 20px var(--accent-light), 0 0 40px var(--accent-light);
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 50%, 90% { transform: translateX(-4px); }
  30%, 70% { transform: translateX(4px); }
}

@keyframes scoreChange {
  0% {
    transform: scale(1);
    color: inherit;
  }
  30% {
    transform: scale(1.6);
    color: var(--success);
  }
  100% {
    transform: scale(1);
    color: inherit;
  }
}

@keyframes scorePopup {
  0% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  70% {
    opacity: 1;
    transform: translateY(-30px) scale(1.2);
  }
  100% {
    opacity: 0;
    transform: translateY(-50px) scale(0.8);
  }
}

@keyframes toast {
  0% {
    opacity: 0;
    transform: translateY(-100%) translateX(-50%);
  }
  10% {
    opacity: 1;
    transform: translateY(0) translateX(-50%);
  }
  90% {
    opacity: 1;
    transform: translateY(0) translateX(-50%);
  }
  100% {
    opacity: 0;
    transform: translateY(-100%) translateX(-50%);
  }
}

@keyframes hexFloat {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
    opacity: 0.04;
  }
  50% {
    transform: translateY(-20px) rotate(30deg);
    opacity: 0.08;
  }
}

@keyframes spinIn {
  from {
    opacity: 0;
    transform: scale(0.7) rotate(-10deg);
  }
  to {
    opacity: 1;
    transform: scale(1) rotate(0deg);
  }
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

/* ─── Base Reset & Typography ─────────────────────────────────────────────── */

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  width: 100vw;
  height: 100vh;
  /* Extend into safe area so content fills edge-to-edge */
  padding-bottom: env(safe-area-inset-bottom, 0px);
  box-sizing: border-box;
}

h1, h2, h3, h4 {
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text-primary);
}

h1 { font-size: 2.5rem; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.15rem; }
h4 { font-size: 1rem; }

p {
  font-weight: 400;
  line-height: 1.6;
  color: var(--text-secondary);
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color var(--transition-fast);
}

a:hover {
  color: var(--text-primary);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-dim);
}

/* ─── App Container ───────────────────────────────────────────────────────── */

#app {
  width: 100vw;
  height: 100vh;
  position: relative;
  overflow: hidden;
  background: var(--bg-primary);
}

/* ─── Shared Screen Overlay ───────────────────────────────────────────────── */

.screen-overlay {
  position: absolute;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
  animation: fadeIn 400ms ease;
}

.screen-overlay.hidden {
  display: none;
}

/* ─── Main Menu ───────────────────────────────────────────────────────────── */

#main-menu {
  z-index: 110;
  background: var(--bg-primary);
  position: relative;
  overflow: hidden;
}

/* Animated hex background pattern (CSS only) */
#main-menu::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 20% 30%, var(--accent-light) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, var(--player1-dark) 0%, transparent 40%),
    radial-gradient(circle at 50% 50%, var(--player2-dark) 0%, transparent 60%);
  opacity: 0.6;
  z-index: 0;
}

/* Hex pattern overlay */
#main-menu::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  opacity: 0.03;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='52' viewBox='0 0 60 52'%3E%3Cpolygon points='30,0 60,15 60,37 30,52 0,37 0,15' fill='none' stroke='white' stroke-width='1'/%3E%3C/svg%3E");
  background-size: 60px 52px;
  animation: hexFloat 12s ease-in-out infinite;
}

.main-menu-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  animation: slideUp 600ms ease;
}

.game-title {
  font-size: 3.5rem;
  font-weight: 900;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  text-align: center;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--player1-color), var(--accent), var(--player2-color));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: none;
  filter: drop-shadow(0 0 30px var(--accent-light));
}

.game-subtitle {
  font-size: 1rem;
  font-weight: 400;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 36px;
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 280px;
}

/* ─── Buttons ─────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 28px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-md);
  background: var(--bg-panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition:
    background var(--transition-normal),
    border-color var(--transition-normal),
    box-shadow var(--transition-normal),
    transform var(--transition-fast);
  user-select: none;
  position: relative;
  overflow: hidden;
}

.btn:hover {
  background: var(--accent-light);
  border-color: var(--accent);
  box-shadow: 0 0 16px var(--accent-light), var(--shadow-md);
}

.btn:active {
  transform: scale(0.97);
  box-shadow: 0 0 8px var(--accent-light);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

.btn:disabled:hover {
  background: var(--bg-panel);
  border-color: var(--border-color);
  box-shadow: none;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--player1-color));
  border-color: transparent;
  color: #fff;
  font-weight: 700;
}

.btn-primary:hover {
  background: linear-gradient(135deg, var(--player1-color), var(--accent));
  border-color: transparent;
  box-shadow: 0 0 24px var(--player1-light), var(--shadow-md);
}

.btn-danger {
  border-color: var(--error);
  color: var(--error);
}

.btn-danger:hover {
  background: rgba(255, 68, 68, 0.15);
  border-color: var(--error);
  box-shadow: 0 0 16px rgba(255, 68, 68, 0.2);
}

.btn-small {
  padding: 6px 14px;
  font-size: 0.82rem;
  border-radius: var(--border-radius-sm);
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--border-radius-sm);
  font-size: 1.1rem;
}

.speed-btn {
  color: var(--accent);
  border-color: var(--accent);
  min-width: 40px;
  font-variant-numeric: tabular-nums;
}

.speed-btn:hover {
  background: var(--accent-light);
  box-shadow: 0 0 10px var(--accent-light);
}

/* ─── Form Elements ───────────────────────────────────────────────────────── */

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.form-select,
.form-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 0.9rem;
  transition:
    border-color var(--transition-normal),
    box-shadow var(--transition-normal);
  appearance: none;
  cursor: pointer;
}

.form-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238888aa' stroke-width='2' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

.form-select:focus,
.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.form-select option {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

/* Range / Volume Slider */
.form-range {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--border-color);
  outline: none;
  transition: background var(--transition-normal);
}

.form-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg-primary);
  box-shadow: 0 0 6px var(--accent-light);
  transition: box-shadow var(--transition-normal);
}

.form-range::-webkit-slider-thumb:hover {
  box-shadow: 0 0 12px var(--accent-light);
}

.form-range::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg-primary);
  box-shadow: 0 0 6px var(--accent-light);
}

/* ─── Glassmorphism Panel ─────────────────────────────────────────────────── */

.glass-panel {
  background: var(--bg-panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow-md), var(--shadow-glow);
}

/* ─── Game Setup Screen ───────────────────────────────────────────────────── */

#game-setup {
  z-index: 105;
}

.setup-panel {
  width: 440px;
  max-width: 92vw;
  padding: 36px 32px;
  animation: slideUp 500ms ease;
}

.setup-panel h2 {
  text-align: center;
  margin-bottom: 28px;
  letter-spacing: 0.08em;
}

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.setup-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.setup-actions {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}

.setup-actions .btn {
  flex: 1;
}

/* ─── Game Container Layout ───────────────────────────────────────────────── */

#game-container {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr var(--sidebar-width);
  grid-template-rows: 1fr;
  gap: 0;
  z-index: 1;
}

#game-container.hidden {
  display: none;
}

/* ─── Left Sidebar (Piece Tray) ───────────────────────────────────────────── */

#sidebar-left {
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-right: 1px solid var(--border-color);
  overflow: hidden;
  animation: slideInLeft 500ms ease;
}

.sidebar-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.sidebar-header h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-secondary);
}

.piece-tray {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  align-content: start;
}

.piece-card {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-md);
  background: var(--bg-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    border-color var(--transition-normal),
    background var(--transition-normal),
    box-shadow var(--transition-normal),
    transform var(--transition-fast),
    opacity var(--transition-normal);
  overflow: hidden;
}

.piece-card:hover {
  border-color: var(--accent);
  background: var(--accent-light);
  transform: scale(1.04);
}

.piece-card.selected {
  border-color: var(--player1-color);
  box-shadow: 0 0 14px var(--player1-light), inset 0 0 10px var(--player1-dark);
  animation: glow 2s ease-in-out infinite;
}

.piece-card.selected.player2 {
  border-color: var(--player2-color);
  box-shadow: 0 0 14px var(--player2-light), inset 0 0 10px var(--player2-dark);
}

.piece-card.used {
  opacity: 0.25;
  cursor: not-allowed;
  pointer-events: none;
}

.piece-card.used::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    -45deg,
    transparent,
    transparent 4px,
    rgba(100, 100, 100, 0.15) 4px,
    rgba(100, 100, 100, 0.15) 6px
  );
  border-radius: inherit;
}

.piece-card canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.piece-card .piece-name {
  position: absolute;
  bottom: 3px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 0.62rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  pointer-events: none;
}

.piece-card .piece-size-badge {
  position: absolute;
  top: 3px;
  right: 4px;
  font-size: 0.58rem;
  font-weight: 700;
  color: var(--text-dim);
  background: var(--bg-primary);
  padding: 1px 4px;
  border-radius: 3px;
  pointer-events: none;
}

/* Piece controls (rotate/mirror) */
.piece-controls {
  padding: 10px 16px;
  border-top: 1px solid var(--border-color);
  display: flex;
  gap: 6px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.piece-controls .btn {
  flex: 1;
  min-width: 0;
}

/* ─── Board Container (Center) ────────────────────────────────────────────── */

#board-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--bg-primary);
  min-width: 0;
  min-height: 0;
}

#board-container canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  touch-action: none;
}

/* ─── Right Sidebar (Scoreboard & Info) ───────────────────────────────────── */

#sidebar-right {
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-left: 1px solid var(--border-color);
  overflow-y: auto;
  animation: slideInRight 500ms ease;
}

/* Turn Indicator Banner */
.turn-indicator {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  transition: background var(--transition-normal);
}

.turn-indicator.player1 {
  background: var(--player1-dark);
  border-bottom-color: var(--player1-color);
}

.turn-indicator.player2 {
  background: var(--player2-dark);
  border-bottom-color: var(--player2-color);
}

.turn-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  animation: pulse 1.5s ease-in-out infinite;
}

.turn-indicator.player1 .turn-dot {
  background: var(--player1-color);
  box-shadow: 0 0 8px var(--player1-color);
}

.turn-indicator.player2 .turn-dot {
  background: var(--player2-color);
  box-shadow: 0 0 8px var(--player2-color);
}

.turn-text {
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.turn-ai-badge {
  font-size: 0.65rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 3px;
  background: var(--accent-light);
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Scoreboard */
.scoreboard {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

.scoreboard-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.player-card {
  padding: 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-md);
  background: var(--bg-secondary);
  transition:
    border-color var(--transition-normal),
    box-shadow var(--transition-normal),
    background var(--transition-normal);
}

.player-card.active {
  border-color: var(--accent);
  box-shadow: var(--shadow-glow);
}

.player-card.player1.active {
  border-color: var(--player1-color);
  box-shadow: 0 0 12px var(--player1-light);
  background: var(--player1-dark);
}

.player-card.player2.active {
  border-color: var(--player2-color);
  box-shadow: 0 0 12px var(--player2-light);
  background: var(--player2-dark);
}

.player-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.player-color-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.player-card.player1 .player-color-dot {
  background: var(--player1-color);
}

.player-card.player2 .player-color-dot {
  background: var(--player2-color);
}

.player-name {
  font-size: 0.9rem;
  font-weight: 700;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 800;
  line-height: 1;
  position: relative;
}

.stat-value.animate {
  animation: scoreChange 500ms ease;
}

.stat-label {
  font-size: 0.62rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

/* Score popup float */
.score-popup {
  position: absolute;
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--success);
  pointer-events: none;
  animation: scorePopup 800ms ease forwards;
  z-index: 50;
}

/* Series Score */
.series-score {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  flex-shrink: 0;
}

.series-player {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 700;
}

.series-divider {
  font-size: 0.8rem;
  color: var(--text-dim);
  font-weight: 400;
}

/* Game Info / Piece Count */
.game-info {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
}

.info-row .label {
  color: var(--text-secondary);
}

.info-row .value {
  font-weight: 700;
  color: var(--text-primary);
}

/* Game Actions */
.game-actions {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: auto;
  flex-shrink: 0;
}

/* ─── Piece Preview (floating near cursor or in panel) ────────────────────── */

.piece-preview-panel {
  padding: 10px 16px;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-height: 80px;
}

.piece-preview-panel canvas {
  display: block;
}

/* ─── Game Over Overlay ───────────────────────────────────────────────────── */

#game-over-overlay {
  position: absolute;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-overlay);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 500ms ease;
}

#game-over-overlay.hidden {
  display: none;
}

.game-over-panel {
  width: 480px;
  max-width: 92vw;
  padding: 40px 36px;
  text-align: center;
  animation: spinIn 600ms ease;
}

.game-over-title {
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-bottom: 6px;
}

.game-over-title.player1-win {
  color: var(--player1-color);
  text-shadow: 0 0 20px var(--player1-light);
}

.game-over-title.player2-win {
  color: var(--player2-color);
  text-shadow: 0 0 20px var(--player2-light);
}

.game-over-title.draw {
  color: var(--text-secondary);
}

.game-over-subtitle {
  font-size: 1rem;
  color: var(--text-secondary);
  margin-bottom: 28px;
}

.game-over-scores {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 16px;
  align-items: center;
  margin-bottom: 32px;
}

.game-over-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.game-over-player-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.game-over-player-score {
  font-size: 2.5rem;
  font-weight: 900;
}

.game-over-player.player1 .game-over-player-score {
  color: var(--player1-color);
}

.game-over-player.player2 .game-over-player-score {
  color: var(--player2-color);
}

.game-over-vs {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--text-dim);
}

.game-over-series {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 24px;
  padding: 10px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  background: var(--bg-secondary);
}

.game-over-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.game-over-actions .btn {
  min-width: 140px;
}

/* ─── Settings Modal ──────────────────────────────────────────────────────── */

#settings-modal {
  position: absolute;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-overlay);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 300ms ease;
}

#settings-modal.hidden {
  display: none;
}

.settings-panel {
  width: 420px;
  max-width: 92vw;
  padding: 32px 28px;
  animation: modalSlideIn 400ms ease;
  position: relative;
}

/* Fix: override modalSlideIn since panel is flex-centered, not abs-positioned */
.settings-panel {
  animation: slideUp 400ms ease;
}

.settings-panel h2 {
  text-align: center;
  margin-bottom: 24px;
}

.settings-section {
  margin-bottom: 22px;
}

.settings-section-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  margin-bottom: 12px;
}

/* Theme Picker */
.theme-picker {
  display: flex;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
}

.theme-swatch {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid var(--border-color);
  cursor: pointer;
  transition:
    border-color var(--transition-normal),
    transform var(--transition-fast),
    box-shadow var(--transition-normal);
  position: relative;
}

.theme-swatch:hover {
  transform: scale(1.12);
  border-color: var(--text-secondary);
}

.theme-swatch.active {
  border-color: var(--text-primary);
  box-shadow: 0 0 16px rgba(255, 255, 255, 0.2);
}

.theme-swatch.active::after {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.5);
}

.theme-swatch[data-theme="midnight"] {
  background: linear-gradient(135deg, #00f0ff, #0a0a1a, #ff00aa);
}

.theme-swatch[data-theme="ocean"] {
  background: linear-gradient(135deg, #e0f0ff, #0c2d48, #ff6b6b);
}

.theme-swatch[data-theme="forest"] {
  background: linear-gradient(135deg, #00cc66, #0a1a0a, #ff8800);
}

.theme-swatch[data-theme="monochrome"] {
  background: linear-gradient(135deg, #ffffff, #1a1a1a, #888888);
}

.theme-swatch[data-theme="sunset"] {
  background: linear-gradient(135deg, #ffd700, #1a0a2e, #9b30ff);
}

.theme-name {
  display: block;
  text-align: center;
  font-size: 0.65rem;
  color: var(--text-dim);
  margin-top: 4px;
  font-weight: 600;
}

.theme-option {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Volume Controls */
.volume-control {
  display: flex;
  align-items: center;
  gap: 12px;
}

.volume-control label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  min-width: 70px;
}

.volume-control .form-range {
  flex: 1;
}

.volume-control .volume-value {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-primary);
  min-width: 32px;
  text-align: right;
}

.settings-actions {
  display: flex;
  justify-content: center;
  margin-top: 8px;
}

/* ─── How to Play Overlay ─────────────────────────────────────────────────── */

#how-to-play {
  position: absolute;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-overlay);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 300ms ease;
}

#how-to-play.hidden {
  display: none;
}

.how-to-play-panel {
  width: 560px;
  max-width: 92vw;
  max-height: 85vh;
  padding: 32px 28px;
  overflow-y: auto;
  animation: slideUp 400ms ease;
}

.how-to-play-panel h2 {
  text-align: center;
  margin-bottom: 24px;
}

.rules-section {
  margin-bottom: 20px;
}

.rules-section h3 {
  color: var(--accent);
  margin-bottom: 8px;
  font-size: 1rem;
}

.rules-section p {
  font-size: 0.88rem;
  margin-bottom: 6px;
}

.rules-section ul {
  padding-left: 20px;
  margin-bottom: 6px;
}

.rules-section li {
  font-size: 0.88rem;
  color: var(--text-secondary);
  margin-bottom: 4px;
  line-height: 1.5;
}

.rules-section li strong {
  color: var(--text-primary);
}

.rules-example {
  padding: 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  background: var(--bg-secondary);
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.how-to-play-actions {
  display: flex;
  justify-content: center;
  margin-top: 16px;
}

/* ─── Toast Notifications ─────────────────────────────────────────────────── */

.toast-container {
  position: absolute;
  top: 16px;
  left: 50%;
  z-index: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 24px;
  border-radius: var(--border-radius-md);
  background: var(--bg-panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-md);
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  pointer-events: auto;
  animation: toast var(--toast-duration, 2s) ease forwards;
  transform: translateX(-50%);
}

.toast.info {
  border-color: var(--accent);
  color: var(--accent);
}

.toast.success {
  border-color: var(--success);
  color: var(--success);
}

.toast.error {
  border-color: var(--error);
  color: var(--error);
}

.toast.warning {
  border-color: var(--warning);
  color: var(--warning);
}

/* ─── Confirm Dialog ──────────────────────────────────────────────────────── */

.confirm-overlay {
  position: absolute;
  inset: 0;
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-overlay);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fadeIn 200ms ease;
}

.confirm-panel {
  width: 360px;
  max-width: 88vw;
  padding: 28px 24px;
  text-align: center;
  animation: slideUp 300ms ease;
}

.confirm-message {
  font-size: 1rem;
  margin-bottom: 24px;
  line-height: 1.5;
  color: var(--text-primary);
}

.confirm-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.confirm-actions .btn {
  min-width: 100px;
}

/* ─── Responsive: Tablet (below 1100px) ───────────────────────────────────── */

@media (max-width: 1100px) {
  :root {
    --sidebar-width: 220px;
  }

  .game-title {
    font-size: 2.5rem;
  }

  .piece-tray {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 8px;
  }

  .player-stats {
    grid-template-columns: 1fr 1fr;
  }

  .stat-value {
    font-size: 1.1rem;
  }
}

/* ─── Responsive: Small Tablet / Narrow (below 900px) ─────────────────────── */

/*
 * Mobile layout (top to bottom):
 *   [score bar  — 48px]
 *   [board      — remaining space]
 *   [piece tray — 90px]
 *
 * The score bar is sidebar-right collapsed to a thin horizontal strip.
 * The piece tray is sidebar-left moved to the bottom.
 * This puts pieces near the thumb and keeps the board unobstructed.
 */

@media (max-width: 900px) {
  #game-container {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    /* order: score-bar, board, piece-tray */
  }

  /* Score bar — thin strip at top */
  #sidebar-right {
    order: 1;
    flex: 0 0 48px;
    min-height: 48px;
    max-height: 48px;
    border-left: none;
    border-bottom: 1px solid var(--border-color);
    flex-direction: row;
    align-items: center;
    overflow: hidden;
    padding: 0 8px;
    gap: 8px;
  }

  /* Board — takes all remaining space */
  #board-container {
    order: 2;
    flex: 1 1 0;
    min-height: 0;
    width: 100%;
    overflow: hidden;
  }

  /* Piece tray — fixed height strip at bottom; grows to cover safe area */
  #sidebar-left {
    order: 3;
    flex: 0 0 calc(90px + env(safe-area-inset-bottom, 0px));
    min-height: calc(90px + env(safe-area-inset-bottom, 0px));
    max-height: calc(90px + env(safe-area-inset-bottom, 0px));
    border-right: none;
    border-top: 1px solid var(--border-color);
    display: flex;
    flex-direction: row;
    align-items: stretch;
    overflow: hidden;
  }

  /* Hide decorative sidebar headers */
  #sidebar-left .piece-tray-header,
  .piece-preview-panel,
  .scoreboard-title,
  .series-score,
  .game-info {
    display: none;
  }

  /* Piece tray: horizontal scroll row */
  .piece-tray {
    display: flex;
    flex-wrap: nowrap;
    flex: 1;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 8px;
    padding: 8px 12px;
    /* Add safe-area bottom padding so content clears the Android nav bar */
    padding-bottom: max(8px, env(safe-area-inset-bottom, 8px));
    align-items: center;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .piece-tray::-webkit-scrollbar {
    display: none;
  }

  .piece-tray .piece-card {
    min-width: 68px;
    min-height: 68px;
    max-width: 68px;
    max-height: 68px;
    flex-shrink: 0;
    aspect-ratio: 1;
  }

  .piece-tray .piece-card .piece-name {
    font-size: 0.5rem;
  }

  /* Rotate/Mirror buttons: compact column on the right of the tray */
  .piece-controls {
    border-top: none;
    border-left: 1px solid var(--border-color);
    flex-direction: column;
    padding: 6px 8px;
    gap: 4px;
    justify-content: center;
    flex-shrink: 0;
  }

  .piece-controls .btn {
    font-size: 0.65rem;
    padding: 6px 10px;
    min-width: 58px;
  }

  /* Score bar content: turn dot + name + scores + action buttons */
  .turn-indicator {
    border-bottom: none;
    border-right: 1px solid var(--border-color);
    padding: 0 10px;
    flex-shrink: 0;
    height: 100%;
    min-height: 0;
  }

  .scoreboard {
    flex-direction: row;
    padding: 0 6px;
    gap: 6px;
    flex-shrink: 0;
    align-items: center;
  }

  /* Compact player cards in score bar */
  .player-card {
    padding: 4px 8px;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    border-radius: var(--border-radius-sm);
  }

  .player-card-header {
    margin-bottom: 0;
  }

  .player-stats {
    display: flex;
    flex-direction: row;
    gap: 4px;
  }

  .stat-item {
    flex-direction: row;
    gap: 3px;
    align-items: center;
  }

  .stat-value {
    font-size: 0.9rem;
  }

  .stat-label {
    font-size: 0.6rem;
  }

  .game-actions {
    flex-direction: row;
    border-top: none;
    border-left: 1px solid var(--border-color);
    margin-top: 0;
    padding: 4px 8px;
    gap: 4px;
    flex-shrink: 0;
    align-items: center;
    height: 100%;
    min-height: 0;
  }

  .game-actions .btn {
    padding: 4px 10px;
    font-size: 0.72rem;
  }

  .game-title {
    font-size: 2rem;
    letter-spacing: 0.15em;
  }
}

/* ─── Responsive: Minimum (below 768px) ───────────────────────────────────── */

@media (max-width: 768px) {
  .game-title {
    font-size: 1.8rem;
    letter-spacing: 0.1em;
  }

  .game-subtitle {
    font-size: 0.8rem;
    margin-bottom: 24px;
  }

  .menu-buttons {
    width: 240px;
  }

  .setup-panel {
    padding: 24px 20px;
  }

  .setup-row {
    grid-template-columns: 1fr;
  }

  .game-over-panel {
    padding: 28px 20px;
  }

  .game-over-player-score {
    font-size: 2rem;
  }

  .how-to-play-panel {
    padding: 24px 18px;
  }

  .settings-panel {
    padding: 24px 18px;
  }

  .theme-swatch {
    width: 38px;
    height: 38px;
  }
}

/* ─── Fullscreen Button ───────────────────────────────────────────────────── */

.fullscreen-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  transition:
    border-color var(--transition-fast),
    color var(--transition-fast),
    background var(--transition-fast);
  flex-shrink: 0;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.fullscreen-btn:hover,
.fullscreen-btn:active {
  border-color: var(--accent);
  color: var(--text-primary);
  background: var(--accent-light);
}

/* Fullscreen prompt toast arrow indicator */
.toast.fullscreen-prompt::after {
  content: '↑';
  display: block;
  font-size: 1.4rem;
  margin-top: 4px;
  animation: pulse 1s ease-in-out infinite;
}

/* ─── Utility Classes ─────────────────────────────────────────────────────── */

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.text-center {
  text-align: center;
}

.text-player1 {
  color: var(--player1-color);
}

.text-player2 {
  color: var(--player2-color);
}

.text-accent {
  color: var(--accent);
}

.text-success {
  color: var(--success);
}

.text-error {
  color: var(--error);
}

.text-warning {
  color: var(--warning);
}

.mt-auto {
  margin-top: auto;
}

.mb-0 {
  margin-bottom: 0;
}

.gap-sm {
  gap: 6px;
}

.gap-md {
  gap: 12px;
}

.gap-lg {
  gap: 20px;
}
