* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg-dark: #090d16;
  --panel-bg: rgba(22, 30, 49, 0.75);
  --panel-border: rgba(255, 255, 255, 0.12);
  --accent-gold: #f59e0b;
  --accent-cyan: #06b6d4;
  --accent-purple: #a855f7;
  --square-light: #e2e8f0;
  --square-dark: #475569;
  --square-highlight: rgba(245, 158, 11, 0.4);
  --square-target: rgba(6, 182, 212, 0.5);
  --square-capture: rgba(239, 68, 68, 0.6);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-dark);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: var(--text-main);
}

/* Play Cover Overlay */
#play-cover {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: radial-gradient(circle at center, #1e1b4b 0%, #0f172a 60%, #030712 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 16px;
}

.cover-card {
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 28px 24px;
  max-width: 520px;
  width: 100%;
  max-height: 94vh;
  overflow-y: auto;
  text-align: center;
  backdrop-filter: blur(16px);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7), 0 0 30px rgba(168, 85, 247, 0.15);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.cover-badge {
  display: inline-block;
  align-self: center;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--accent-gold);
  background: rgba(245, 158, 11, 0.15);
  border: 1px solid rgba(245, 158, 11, 0.3);
  padding: 4px 14px;
  border-radius: 50px;
}

.cover-title {
  font-size: 2rem;
  font-weight: 800;
  background: linear-gradient(135deg, #ffffff 0%, #cbd5e1 50%, #94a3b8 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 1px;
}

.cover-subtitle {
  font-size: 0.88rem;
  color: var(--text-muted);
  line-height: 1.4;
}

/* Level Selector */
.level-selector-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
}

.level-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text-muted);
}

.level-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.level-btn {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 10px 6px;
  color: var(--text-main);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.level-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.25);
  transform: translateY(-2px);
}

.level-btn.active {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.25) 0%, rgba(168, 85, 247, 0.25) 100%);
  border-color: var(--accent-purple);
  box-shadow: 0 0 15px rgba(168, 85, 247, 0.3);
}

.lvl-icon { font-size: 1.25rem; }
.lvl-title { font-weight: 700; font-size: 0.8rem; }
.lvl-desc { font-size: 0.65rem; color: var(--text-muted); text-align: center; }

/* Mode selector */
.mode-selector-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mode-btn {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 8px;
  color: var(--text-main);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.mode-btn.active {
  background: rgba(6, 182, 212, 0.2);
  border-color: var(--accent-cyan);
  color: #fff;
}

.play-cover-btn {
  background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  border: none;
  color: #ffffff;
  padding: 14px 28px;
  font-size: 1.1rem;
  font-weight: 800;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 10px 25px -5px rgba(99, 102, 241, 0.5);
  transition: all 0.25s ease;
}

.play-cover-btn:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 15px 30px -5px rgba(168, 85, 247, 0.6);
}

/* Main Game Layout */
#game-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at top right, #1e1b4b 0%, #0f172a 50%, #030712 100%);
  position: relative;
  overflow: hidden;
}

/* Top HUD */
#hud-top {
  height: 48px;
  padding: 0 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(15, 23, 42, 0.8);
  border-bottom: 1px solid var(--panel-border);
  backdrop-filter: blur(10px);
  z-index: 20;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.game-logo {
  font-weight: 800;
  font-size: 1rem;
  background: linear-gradient(90deg, #38bdf8, #818cf8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.hud-badge {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 20px;
  background: rgba(245, 158, 11, 0.15);
  color: var(--accent-gold);
  border: 1px solid rgba(245, 158, 11, 0.3);
}

#ui-layer {
  display: flex;
  gap: 8px;
}

.ui-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: var(--text-main);
  padding: 5px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  transition: all 0.2s ease;
}

.ui-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

/* Workspace Layout */
#main-workspace {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
}

.board-column {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 520px;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.player-card {
  width: 100%;
}

/* Responsive Visibility Utilities */
.mobile-only { display: none !important; }
.desktop-only { display: inline-flex !important; }

/* Kebab Button */
.kebab-btn {
  font-size: 1.25rem;
  font-weight: 800;
  padding: 4px 10px !important;
  background: rgba(168, 85, 247, 0.2) !important;
  border: 1px solid var(--accent-purple) !important;
  color: #fff !important;
  border-radius: 8px;
}

/* Sidebar Drawer Backdrop */
#sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

#sidebar-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--panel-border);
  margin-bottom: 4px;
}

.drawer-title {
  font-weight: 800;
  font-size: 0.9rem;
  color: var(--accent-gold);
}

.quick-settings {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.hud-timers {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-timer-badge {
  font-family: monospace;
  font-size: 0.82rem;
  font-weight: 700;
  background: rgba(0, 0, 0, 0.4);
  padding: 2px 8px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-muted);
}

.hud-timer-badge.active {
  color: var(--accent-cyan);
  border-color: var(--accent-cyan);
}

/* Mobile & Tablet Landscape Optimization (Height <= 550px or Width <= 850px) */
@media (max-height: 550px), (max-width: 850px) {
  .mobile-only { display: flex !important; }
  .desktop-only { display: none !important; }

  /* Hide Opponent and Player cards above/below board on mobile */
  .board-column > .player-card {
    display: none !important;
  }

  #hud-top {
    height: 38px;
    padding: 0 10px;
  }
  .game-logo { font-size: 0.88rem; }
  .hud-badge { font-size: 0.65rem; padding: 2px 6px; }

  #main-workspace {
    padding: 0 !important;
    margin: 0 auto;
    justify-content: center;
    align-items: center;
    overflow: hidden;
  }

  /* Maximized Board Column (Zero space above & below) */
  .board-column {
    max-width: 100% !important;
    width: auto !important;
    height: 100% !important;
    gap: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
    justify-content: center;
    align-items: center;
  }

  #board-wrapper {
    height: calc(100vh - 40px) !important;
    max-height: calc(100vh - 40px) !important;
    width: auto !important;
    max-width: calc(100vw - 8px) !important;
    aspect-ratio: 1 / 1 !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  #board-container {
    padding: 2px;
    border-radius: 6px;
    width: 100%;
    height: 100%;
  }

  /* Kebab Slide-Out Sidebar Drawer */
  #game-sidebar {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 310px;
    max-width: 85vw;
    height: 100vh;
    max-height: 100vh;
    z-index: 1000;
    background: rgba(15, 23, 42, 0.96);
    border-left: 1px solid var(--panel-border);
    backdrop-filter: blur(16px);
    box-shadow: -10px 0 30px rgba(0, 0, 0, 0.7);
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

  #game-sidebar.drawer-open {
    transform: translateX(0);
  }

  .sidebar-card {
    padding: 10px 12px;
    border-radius: 10px;
  }

  .status-heading { font-size: 0.82rem; }
  .ctrl-btn { padding: 8px 4px; font-size: 0.8rem; }
  .q-lvl-btn { padding: 4px 8px; font-size: 0.75rem; }
  .history-card { min-height: 120px; }
  .log-row { padding: 3px 6px; font-size: 0.8rem; }

  /* Play Cover inside mobile landscape */
  .cover-card {
    padding: 14px 18px;
    gap: 8px;
  }
  .cover-title { font-size: 1.4rem; }
  .cover-subtitle { font-size: 0.78rem; }
  .level-btn { padding: 6px 4px; }
  .lvl-icon { font-size: 1rem; }
  .lvl-title { font-size: 0.75rem; }
  .lvl-desc { display: none; }
  .play-cover-btn { padding: 8px 20px; font-size: 0.95rem; }
}

/* Mobile Portrait Aspect / Small Width (< 768px) Layout */
@media (max-width: 768px) and (min-height: 521px) {
  #main-workspace {
    flex-direction: column;
    overflow-y: auto;
    gap: 12px;
    padding: 10px;
  }
  .board-column {
    max-width: 100%;
    height: auto;
  }
  #board-wrapper {
    max-height: calc(100vw - 40px);
  }
  #game-sidebar {
    width: 100%;
    max-width: 520px;
    height: auto;
    max-height: none;
  }
}

/* Player Info Cards */
.player-card {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 10px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  backdrop-filter: blur(8px);
}

.player-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.player-avatar {
  font-size: 1.4rem;
}

.player-name {
  font-size: 0.9rem;
  font-weight: 700;
}

.captured-row {
  display: flex;
  gap: 2px;
  font-size: 0.85rem;
  min-height: 18px;
}

.timer-badge {
  font-family: monospace;
  font-size: 1.1rem;
  font-weight: 700;
  background: rgba(0, 0, 0, 0.4);
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-muted);
}

.timer-badge.active {
  color: var(--accent-cyan);
  border-color: var(--accent-cyan);
  box-shadow: 0 0 10px rgba(6, 182, 212, 0.3);
}

/* Chessboard Section */
#board-wrapper {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: calc(100vh - 160px);
}

#board-container {
  width: 100%;
  height: 100%;
  max-width: 580px;
  max-height: 580px;
  aspect-ratio: 1 / 1;
  background: #1e293b;
  padding: 10px;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6), inset 0 0 0 1px rgba(255, 255, 255, 0.1);
}

#chessboard {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.15);
}

/* Squares */
.square {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.square.light {
  background-color: #cbd5e1;
  color: #1e293b;
}

.square.dark {
  background-color: #334155;
  color: #f8fafc;
}

.square.selected {
  background-color: #0284c7 !important;
  box-shadow: inset 0 0 12px rgba(255, 255, 255, 0.5);
}

.square.last-move {
  background-color: rgba(245, 158, 11, 0.45) !important;
}

.square.in-check {
  background-color: rgba(239, 68, 68, 0.65) !important;
  animation: pulseCheck 1s infinite alternate;
}

@keyframes pulseCheck {
  from { box-shadow: inset 0 0 5px #ef4444; }
  to { box-shadow: inset 0 0 20px #ef4444; }
}

/* Move target indicator dot */
.square.target::after {
  content: '';
  width: 24%;
  height: 24%;
  background: rgba(6, 182, 212, 0.85);
  border-radius: 50%;
  box-shadow: 0 0 8px var(--accent-cyan);
  pointer-events: none;
}

/* Capture target ring */
.square.target-capture {
  background-color: rgba(239, 68, 68, 0.35) !important;
}

.square.target-capture::after {
  content: '';
  position: absolute;
  inset: 4px;
  border: 3px solid #ef4444;
  border-radius: 50%;
  pointer-events: none;
  animation: pulseCapture 0.8s infinite alternate;
}

@keyframes pulseCapture {
  from { opacity: 0.6; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1.02); }
}

/* Square Coordinates Notation (a1, h8) */
.sq-coord {
  position: absolute;
  font-size: 0.65rem;
  font-weight: 700;
  pointer-events: none;
  opacity: 0.6;
}

.sq-coord.rank { top: 2px; left: 4px; }
.sq-coord.file { bottom: 2px; right: 4px; }

/* SVG Chess Pieces */
.chess-piece {
  width: 85%;
  height: 85%;
  object-fit: contain;
  transition: transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.4));
  pointer-events: none;
}

.square:hover .chess-piece {
  transform: scale(1.1);
}

/* Status Overlay Popup */
#status-overlay {
  position: absolute;
  inset: 0;
  z-index: 50;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

#status-overlay.hidden { display: none; }

.status-modal {
  background: rgba(30, 41, 59, 0.95);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 28px;
  text-align: center;
  max-width: 380px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
}

#status-icon { font-size: 3rem; }
#status-title { font-size: 1.6rem; font-weight: 800; color: var(--accent-gold); }
#status-message { font-size: 0.95rem; color: var(--text-muted); }

.status-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.action-btn {
  padding: 12px;
  border-radius: 10px;
  font-weight: 700;
  cursor: pointer;
  border: none;
  font-size: 0.95rem;
  transition: all 0.2s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  color: #fff;
}

.action-btn.secondary {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

/* Sidebar Styling */
#game-sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: 100%;
  max-height: 580px;
}

.sidebar-card {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 14px;
  backdrop-filter: blur(8px);
}

/* Turn Status Card */
.status-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.status-heading {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 0.95rem;
}

.turn-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.turn-dot.white-turn {
  background: #ffffff;
  box-shadow: 0 0 10px #ffffff;
}

.turn-dot.black-turn {
  background: #334155;
  box-shadow: 0 0 10px #334155;
  border: 1px solid #94a3b8;
}

#advantage-bar-container {
  width: 100%;
  height: 8px;
  background: #1e293b;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

#advantage-bar {
  height: 100%;
  background: linear-gradient(90deg, #f8fafc 0%, #cbd5e1 100%);
  transition: width 0.3s ease;
}

/* Control Buttons */
.controls-card {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.ctrl-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text-main);
  padding: 10px 4px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  transition: all 0.2s ease;
}

.ctrl-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

.ctrl-btn.warning:hover {
  background: rgba(239, 68, 68, 0.2);
  border-color: #ef4444;
}

/* Level Quick Bar */
.level-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.bar-title {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.5px;
}

.quick-level-btns {
  display: flex;
  gap: 6px;
}

.q-lvl-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
}

.q-lvl-btn.active {
  background: rgba(245, 158, 11, 0.2);
  border-color: var(--accent-gold);
  color: var(--accent-gold);
}

/* Move Log Card */
.history-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 180px;
  overflow: hidden;
}

.history-header {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-muted);
}

#move-log-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 4px;
  font-family: monospace;
  font-size: 0.85rem;
}

.log-row {
  display: grid;
  grid-template-columns: 36px 1fr 1fr;
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.2);
}

.log-row:nth-child(even) {
  background: rgba(255, 255, 255, 0.02);
}

.log-num { color: var(--text-muted); }
.log-move { color: var(--text-main); font-weight: 600; }

/* Ad Overlay Modal */
#ad-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 16px;
  backdrop-filter: blur(8px);
}

#ad-overlay.hidden { display: none; }

.ad-modal {
  position: relative;
  width: 100%;
  max-width: 800px;
  height: 90vh;
  max-height: 500px;
  background: #1e293b;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.ad-header {
  height: 48px;
  background: #0f172a;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.ad-badge {
  color: #94a3b8;
  font-size: 0.875rem;
  font-weight: 500;
}

.ad-close-btn {
  background: #334155;
  color: #94a3b8;
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: not-allowed;
  transition: all 0.2s ease;
}

.ad-close-btn:not([disabled]) {
  background: #2563eb;
  color: #ffffff;
  cursor: pointer;
}

.ad-close-btn:not([disabled]):hover {
  background: #1d4ed8;
}

.ad-body {
  flex: 1;
  width: 100%;
  height: calc(100% - 48px);
}

.ad-body iframe {
  width: 100%;
  height: 100%;
  border: none;
}
