/* ==========================================================================
   Chess Arena (国际象棋竞技场) - Modern Stylesheet
   Themes: Wood (default), Dark, Tournament
   ========================================================================== */

:root {
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --header-bg: #0f172a;
  --header-color: #f8fafc;
  --body-bg: #0f172a;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --card-bg: #1e293b;
  --border-color: rgba(255, 255, 255, 0.08);
  --primary-color: #38bdf8;
  --primary-hover: #0ea5e9;
  --accent-green: #10b981;
  --accent-red: #ef4444;

  /* Default Dark Slate Board Theme (No Brown) */
  --sq-light: #334155;
  --sq-dark: #1e293b;
  --sq-selected: rgba(56, 189, 248, 0.6);
  --sq-last-move: rgba(56, 189, 248, 0.35);
  --sq-check: radial-gradient(circle, rgba(239,68,68,0.9) 0%, rgba(239,68,68,0.3) 65%, transparent 75%);
  --dot-color: rgba(255, 255, 255, 0.35);
  --ring-color: rgba(255, 255, 255, 0.4);
}

[data-theme="dark"] {
  --header-bg: #0f172a;
  --header-color: #f8fafc;
  --body-bg: #0f172a;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --card-bg: #1e293b;
  --border-color: rgba(255, 255, 255, 0.08);
  --primary-color: #38bdf8;
  --primary-hover: #0ea5e9;

  /* Dark Slate Board Theme */
  --sq-light: #334155;
  --sq-dark: #1e293b;
  --sq-selected: rgba(56, 189, 248, 0.6);
  --sq-last-move: rgba(56, 189, 248, 0.35);
  --dot-color: rgba(255, 255, 255, 0.35);
  --ring-color: rgba(255, 255, 255, 0.4);
}

[data-theme="midnight"] {
  --header-bg: #090d16;
  --header-color: #f8fafc;
  --body-bg: #090d16;
  --text-main: #f8fafc;
  --text-muted: #64748b;
  --card-bg: #0f172a;
  --border-color: rgba(255, 255, 255, 0.06);
  --primary-color: #38bdf8;
  --primary-hover: #0ea5e9;

  /* Deep Midnight Obsidian Board */
  --sq-light: #1e293b;
  --sq-dark: #090d16;
  --sq-selected: rgba(56, 189, 248, 0.65);
  --sq-last-move: rgba(56, 189, 248, 0.4);
  --dot-color: rgba(255, 255, 255, 0.3);
  --ring-color: rgba(255, 255, 255, 0.35);
}

[data-theme="wood"] {
  /* Remap any legacy wood requests to Dark Slate */
  --header-bg: #0f172a;
  --header-color: #f8fafc;
  --body-bg: #0f172a;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --card-bg: #1e293b;
  --border-color: rgba(255, 255, 255, 0.08);
  --primary-color: #38bdf8;
  --primary-hover: #0ea5e9;

  --sq-light: #334155;
  --sq-dark: #1e293b;
  --sq-selected: rgba(56, 189, 248, 0.6);
  --sq-last-move: rgba(56, 189, 248, 0.35);
  --dot-color: rgba(255, 255, 255, 0.35);
  --ring-color: rgba(255, 255, 255, 0.4);
}

[data-theme="tournament"] {
  --header-bg: #0f172a;
  --header-color: #f8fafc;
  --body-bg: #0f172a;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --card-bg: #1e293b;
  --border-color: rgba(255, 255, 255, 0.08);
  --primary-color: #10b981;
  --primary-hover: #059669;

  /* Tournament Dark Green Board */
  --sq-light: #475569;
  --sq-dark: #23473b;
  --sq-selected: rgba(245, 158, 11, 0.65);
  --sq-last-move: rgba(245, 158, 11, 0.45);
  --dot-color: rgba(255, 255, 255, 0.35);
}

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

html, body {
  max-width: 100vw;
  overflow-x: hidden;
}

body {
  font-family: var(--font-family);
  background-color: var(--body-bg);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  line-height: 1.5;
}

/* Prevent auto zoom on mobile iOS for form inputs */
input, select, textarea {
  font-family: inherit;
  font-size: 16px !important;
}

/* ==========================================================================
   Header & Navigation
   ========================================================================== */

header {
  background: var(--header-bg);
  color: var(--header-color);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 8px 16px 4px;
}

.top-bar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-zone {
  display: flex;
  align-items: center;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #fff;
  text-decoration: none;
  font-weight: 700;
  font-size: 1.25rem;
  letter-spacing: -0.5px;
}

.brand-icon {
  font-size: 1.6rem;
}

.nav-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Pulsing Green Dot Online Badge */
.online-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(22, 163, 74, 0.15);
  border: 1px solid rgba(22, 163, 74, 0.35);
  color: #4ade80;
  padding: 4px 10px;
  border-radius: 9999px;
  font-size: 0.85rem;
  font-weight: 600;
}

.pulse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #22c55e;
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7);
  animation: pulse 1.8s infinite;
}

@keyframes pulse {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7); }
  70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
  100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

.player-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255,255,255,0.1);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  border: 1px solid rgba(255,255,255,0.15);
  transition: background 0.2s;
  white-space: nowrap;
  max-width: 160px;
}

#display-player-name {
  max-width: 110px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-badge:hover {
  background: rgba(255,255,255,0.2);
}

.edit-icon {
  font-size: 0.75rem;
  opacity: 0.7;
}

.btn-icon {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.15);
  color: #fff;
  border-radius: 6px;
  padding: 5px 8px;
  cursor: pointer;
  font-size: 0.95rem;
  transition: all 0.2s;
}

.btn-icon:hover {
  background: rgba(255,255,255,0.2);
}

/* Tabs */
.main-tabs {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  border-bottom: 2px solid transparent;
  overflow-x: auto;
  scrollbar-width: none;
}
.main-tabs::-webkit-scrollbar { display: none; }

.tab-btn {
  background: transparent;
  border: none;
  color: rgba(255,255,255,0.7);
  padding: 8px 14px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  transition: all 0.2s;
}

.tab-btn:hover {
  color: #fff;
}

.tab-btn.active {
  color: #fff;
  border-bottom-color: var(--primary-color);
}

/* ==========================================================================
   Main Content Views
   ========================================================================== */

main {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 16px;
}

.tab-view {
  display: none;
}

.tab-view.active {
  display: block;
  animation: fadeIn 0.2s ease-in;
}

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

/* Common Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background: var(--card-bg);
  color: var(--text-main);
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover {
  background: rgba(0,0,0,0.05);
}

.btn.active {
  background: var(--primary-color);
  color: #fff;
  border-color: var(--primary-color);
}

.btn-primary {
  background: var(--primary-color);
  color: #fff;
  border-color: var(--primary-color);
}
.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-danger {
  background: var(--accent-red);
  color: #fff;
  border-color: var(--accent-red);
}

.btn-sm {
  padding: 4px 10px;
  font-size: 0.85rem !important;
}

.btn-group {
  display: inline-flex;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.btn-group .btn {
  border-radius: 0;
  border: none;
  border-right: 1px solid var(--border-color);
}
.btn-group .btn:last-child {
  border-right: none;
}

/* Sub-toolbar */
.sub-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
  background: var(--card-bg);
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.sub-toolbar-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.status-banner {
  width: 100%;
  padding: 8px 12px;
  background: rgba(37, 99, 235, 0.1);
  color: var(--primary-color);
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.95rem;
  text-align: center;
  margin-bottom: 14px;
}

.status-banner.game-over {
  background: rgba(220, 38, 38, 0.1);
  color: var(--accent-red);
}

/* ==========================================================================
   Chess Board & Layout
   ========================================================================== */

.chess-layout {
  display: flex;
  gap: 20px;
  align-items: flex-start;
}

.board-container-card {
  background: var(--card-bg);
  padding: 10px;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 560px;
}

#solo-board,
#pvp-board,
.chess-board {
  width: 540px;
  height: 540px;
  max-width: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 3px solid #334155;
  border-radius: 4px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.25);
  user-select: none;
  touch-action: manipulation;
  margin: 0 auto;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
}

.square.light {
  background-color: var(--sq-light);
}

.square.dark {
  background-color: var(--sq-dark);
}

.square.selected {
  background-color: var(--sq-selected) !important;
}

.square.last-move {
  background-color: var(--sq-last-move) !important;
}

.square.in-check {
  background: var(--sq-check) !important;
}

/* Coordinates */
.coord {
  position: absolute;
  font-size: 11px;
  font-weight: bold;
  pointer-events: none;
  opacity: 0.75;
}

.rank-coord {
  top: 2px;
  left: 3px;
}

.file-coord {
  bottom: 2px;
  right: 3px;
}

.square.light .coord {
  color: var(--sq-dark);
}
.square.dark .coord {
  color: var(--sq-light);
}

/* Legal Move Indicators */
.move-dot {
  width: 28%;
  height: 28%;
  background: var(--dot-color);
  border-radius: 50%;
  pointer-events: none;
}

.capture-ring {
  width: 80%;
  height: 80%;
  border: 6px solid var(--ring-color);
  border-radius: 50%;
  pointer-events: none;
  position: absolute;
}

/* Chess Pieces */
.chess-piece {
  width: 90%;
  height: 90%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  transition: transform 0.1s ease-out;
}

.chess-piece-svg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.2));
}

/* Side Panel (Moves, Clocks, Captured) */
.side-panel {
  flex: 1;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.side-panel-card {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}

/* Captured pieces drawer */
.captured-tray {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 32px;
  flex-wrap: wrap;
  padding: 4px 0;
}

.captured-tray svg {
  width: 24px;
  height: 24px;
}

.material-lead {
  font-size: 0.8rem;
  font-weight: 700;
  background: rgba(0,0,0,0.08);
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 4px;
}

/* Move history list */
.history-list {
  height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 6px;
  background: rgba(0,0,0,0.02);
  font-size: 0.9rem;
}

.history-row {
  display: grid;
  grid-template-columns: 36px 1fr 1fr;
  padding: 3px 6px;
  border-radius: 4px;
}

.history-row:nth-child(even) {
  background: rgba(0,0,0,0.03);
}

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

.hist-san {
  font-family: monospace;
  font-weight: 600;
}

/* ==========================================================================
   PvP Battle Layout & Clocks
   ========================================================================== */

.battle-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 12px 18px;
  margin-bottom: 14px;
}

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

.hud-captured-tray {
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 18px;
  margin-top: 3px;
  flex-wrap: wrap;
}

.hud-captured-tray svg {
  width: 18px;
  height: 18px;
}

.hud-captured-tray .material-lead {
  font-size: 0.72rem;
  font-weight: 700;
  background: rgba(0,0,0,0.08);
  padding: 1px 4px;
  border-radius: 3px;
  margin-left: 2px;
}

.clock-display {
  font-family: monospace;
  font-size: 1.7rem;
  font-weight: 800;
  background: #18181b;
  color: #22c55e;
  padding: 4px 12px;
  border-radius: 6px;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.4);
}

.battle-banner {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  color: #fff;
  font-weight: 700;
  text-align: center;
  padding: 10px;
  border-radius: 8px;
  margin-bottom: 12px;
  animation: bounceIn 0.3s;
}

@keyframes bounceIn {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.emote-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

/* Toast */
.game-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1c1917;
  color: #fff;
  padding: 10px 20px;
  border-radius: 9999px;
  font-size: 0.95rem;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  z-index: 10000;
  animation: fadeInUp 0.25s ease-out;
}

.toast-success { background: #16a34a; }
.toast-warning { background: #d97706; }
.toast-error { background: #dc2626; }

@keyframes fadeInUp {
  from { opacity: 0; transform: translate(-50%, 15px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

.game-toast.fade-out {
  opacity: 0;
  transform: translate(-50%, 15px);
  transition: all 0.35s;
}

/* ==========================================================================
   Leaderboard Table
   ========================================================================== */

.leaderboard-card {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.04);
}

.table-responsive {
  overflow-x: auto;
  margin-top: 14px;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.leaderboard-table th {
  padding: 10px 14px;
  background: rgba(0,0,0,0.03);
  border-bottom: 2px solid var(--border-color);
  font-weight: 700;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-transform: uppercase;
}

.leaderboard-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-color);
  font-size: 0.95rem;
}

.leaderboard-table tr:hover {
  background: rgba(0,0,0,0.02);
}

/* ==========================================================================
   Modals
   ========================================================================== */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 16px;
}

.modal-card {
  background: var(--card-bg);
  color: var(--text-main);
  border-radius: 12px;
  max-width: 580px;
  width: 100%;
  padding: 24px;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.2);
  position: relative;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-muted);
}

/* Promotion Modal */
.promo-choices {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin: 20px 0;
}

.promo-choice {
  width: 70px;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  background: var(--card-bg);
  cursor: pointer;
  transition: all 0.15s;
}

.promo-choice:hover {
  border-color: var(--primary-color);
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

.promo-choice svg {
  width: 55px;
  height: 55px;
}

/* Replay Board Modal */
.replay-modal-layout {
  display: flex;
  gap: 16px;
}

.replay-board {
  width: min(80vw, 360px) !important;
  height: min(80vw, 360px) !important;
}

.replay-controls {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}

.replay-moves-list {
  flex: 1;
  height: 360px;
  overflow-y: auto;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 8px;
}

.replay-move-row {
  display: grid;
  grid-template-columns: 32px 1fr 1fr;
  padding: 3px 6px;
}

.move-san {
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
}

.move-san:hover {
  background: rgba(0,0,0,0.06);
}

.move-san.active {
  background: var(--primary-color);
  color: #fff;
  font-weight: bold;
}

/* ==========================================================================
   Mobile Responsive Optimizations
   ========================================================================== */

@media (max-width: 768px) {
  .desktop-only,
  .long-label {
    display: none !important;
  }

  html, body {
    overflow-x: hidden;
    width: 100%;
  }

  main {
    padding: 8px 4px;
    overflow-x: hidden;
  }

  .nav-container {
    padding: 6px 6px 2px;
  }

  .top-bar-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 4px;
  }

  .brand {
    gap: 4px;
  }

  .brand-icon {
    font-size: 1.15rem;
  }

  .brand-text {
    font-size: 0.9rem;
    white-space: nowrap;
  }

  .nav-controls {
    display: flex;
    align-items: center;
    gap: 3px;
    flex-shrink: 0;
  }

  .online-badge {
    padding: 2px 6px;
    font-size: 0.72rem;
  }

  .online-label {
    display: none;
  }

  .player-badge {
    padding: 2px 6px;
    font-size: 0.75rem;
    max-width: 110px;
  }

  .btn-icon {
    padding: 2px 5px;
    font-size: 0.8rem;
    min-width: 26px;
    height: 26px;
  }

  .main-tabs {
    width: 100%;
    display: flex;
    gap: 3px;
    box-sizing: border-box;
    padding: 2px 0;
  }

  .main-tabs .tab-btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 6px 1px;
    font-size: 0.78rem;
    text-align: center;
    white-space: nowrap;
  }

  .chess-layout {
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .board-container-card {
    padding: 6px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  #solo-board,
  #pvp-board,
  .chess-board {
    width: 326px !important;
    height: 326px !important;
    max-width: 326px !important;
    margin: 0 auto;
  }

  .side-panel {
    width: 100%;
    padding: 0 4px;
  }

  .replay-modal-layout {
    flex-direction: column;
    align-items: center;
  }

  .replay-board {
    width: min(calc(100vw - 32px), 310px) !important;
    height: min(calc(100vw - 32px), 310px) !important;
    max-width: 310px !important;
  }

  .sub-toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 6px 8px;
    gap: 6px;
  }

  .leaderboard-table th,
  .leaderboard-table td {
    padding: 8px 5px;
    font-size: 0.8rem;
    white-space: nowrap;
  }

  .leaderboard-card {
    padding: 12px 10px;
  }
}

.battle-banner.game-over {
  background: linear-gradient(135deg, #0f172a, #1e293b) !important;
  border: 1px solid rgba(56, 189, 248, 0.4);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  padding: 12px 14px;
}
