/* ========== Reset & Base ========== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --light-square: #e0d5c0;
  --dark-square: #8b6914;
  --highlight-selected: rgba(255, 255, 0, 0.4);
  --highlight-move: rgba(255, 255, 0, 0.2);
  --highlight-last-from: rgba(155, 199, 0, 0.4);
  --highlight-last-to: rgba(155, 199, 0, 0.4);
  --highlight-check: rgba(255, 0, 0, 0.4);
  --bg-primary: #312e2b;
  --bg-secondary: #272522;
  --bg-panel: #21201d;
  --text-primary: #bababa;
  --text-secondary: #999;
  --accent: #81b64c;
  --header-bg: #302e2b;
  --border-color: #3d3b38;
}

html {
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

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

a:hover {
  text-decoration: underline;
}

/* ========== Header ========== */
.header {
  background: var(--header-bg);
  border-bottom: 1px solid var(--border-color);
  padding: 0.5rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-logo {
  font-size: 1.25rem;
  font-weight: bold;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header-logo span {
  color: var(--accent);
}

.header-nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.header-nav a {
  color: var(--text-primary);
  font-size: 0.9rem;
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
  transition: background 0.2s;
}

.header-nav a:hover {
  background: rgba(255, 255, 255, 0.1);
  text-decoration: none;
}

/* Language Dropdown */
.lang-dropdown {
  position: relative;
}

.lang-btn {
  background: none;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.lang-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.lang-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  min-width: 180px;
  max-height: 300px;
  overflow-y: auto;
  z-index: 200;
  margin-top: 4px;
}

.lang-menu.open {
  display: block;
}

.lang-menu a {
  display: block;
  padding: 0.5rem 1rem;
  color: var(--text-primary);
  font-size: 0.85rem;
}

.lang-menu a:hover {
  background: rgba(255, 255, 255, 0.1);
  text-decoration: none;
}

.lang-menu a.active {
  color: var(--accent);
}

/* Hamburger (mobile) */
.hamburger {
  display: none;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.25rem;
}

/* ========== Main Layout ========== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Ad Containers */
.ad-container {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 0.5rem 0;
  background: var(--bg-secondary);
}

.ad-container-side {
  display: none;
}

/* Game Layout */
.game-layout {
  display: flex;
  gap: 1rem;
  padding: 1rem;
  max-width: 1200px;
  width: 100%;
  justify-content: center;
}

.game-board-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

/* Player Info */
.player-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  max-width: 560px;
  padding: 0.4rem 0.5rem;
  background: var(--bg-panel);
  border-radius: 4px;
}

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

.player-name.active-turn {
  color: var(--accent);
}

.captured-pieces {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  flex: 1;
}

.captured-pieces img {
  width: 20px;
  height: 20px;
  opacity: 0.7;
}

.material-diff {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-left: auto;
}

/* ========== Chess Board ========== */
.board-wrapper {
  position: relative;
  max-width: 560px;
  width: 100%;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  border: 2px solid var(--border-color);
  border-radius: 2px;
  overflow: hidden;
  width: 100%;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

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

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

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

.square.move-hint::after {
  content: '';
  position: absolute;
  width: 30%;
  height: 30%;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 50%;
}

.square.capture-hint::after {
  content: '';
  position: absolute;
  width: 90%;
  height: 90%;
  border: 4px solid rgba(0, 0, 0, 0.15);
  border-radius: 50%;
}

.square.last-move {
  background: var(--highlight-last-from) !important;
}

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

.square .piece {
  width: 85%;
  height: 85%;
  pointer-events: none;
  z-index: 1;
  position: relative;
}

.piece-animating {
  z-index: 10 !important;
  transition: transform 200ms ease;
}

.piece-captured-anim {
  transition: transform 150ms ease, opacity 150ms ease !important;
  transform: scale(1.15) !important;
  opacity: 0 !important;
  z-index: 0 !important;
}

/* Board Coordinates */
.square .coord-file {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 0.65rem;
  font-weight: 600;
  pointer-events: none;
}

.square .coord-rank {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 0.65rem;
  font-weight: 600;
  pointer-events: none;
}

.square.light .coord-file,
.square.light .coord-rank {
  color: var(--dark-square);
}

.square.dark .coord-file,
.square.dark .coord-rank {
  color: var(--light-square);
}

/* ========== Side Panel ========== */
.side-panel {
  width: 240px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* Controls */
.controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn {
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

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

.btn-primary:hover {
  background: #6fa33e;
}

.btn-danger {
  border-color: #c33;
}

.btn-danger:hover {
  background: rgba(204, 51, 51, 0.2);
}

/* Move History */
.move-history {
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  flex: 1;
  min-height: 200px;
  max-height: 400px;
  overflow-y: auto;
}

.move-history-title {
  padding: 0.5rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border-color);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.move-history-body {
  padding: 0.5rem;
}

.move-row {
  display: flex;
  align-items: center;
  padding: 0.15rem 0.25rem;
  font-size: 0.85rem;
  font-family: 'SF Mono', 'Consolas', monospace;
}

.move-row:hover {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 2px;
}

.move-number {
  color: var(--text-secondary);
  width: 2rem;
  text-align: right;
  margin-right: 0.5rem;
  font-size: 0.75rem;
}

.move-white, .move-black {
  width: 4rem;
  padding: 0.1rem 0.25rem;
  cursor: pointer;
  border-radius: 2px;
}

.move-white:hover, .move-black:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* ========== Promotion Modal ========== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 500;
  align-items: center;
  justify-content: center;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5rem;
  text-align: center;
  max-width: 400px;
  width: 90%;
}

.modal h2 {
  margin-bottom: 1rem;
  font-size: 1.2rem;
}

.modal p {
  margin-bottom: 1rem;
  color: var(--text-secondary);
}

.promotion-choices {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin: 1rem 0;
}

.promotion-choices .piece-option {
  width: 60px;
  height: 60px;
  background: var(--light-square);
  border: 2px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s, transform 0.2s;
}

.promotion-choices .piece-option:hover {
  border-color: var(--accent);
  transform: scale(1.1);
}

.promotion-choices .piece-option img {
  width: 44px;
  height: 44px;
}

.modal-buttons {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1rem;
}

/* ========== Landing Page ========== */
.landing {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem 1rem;
  text-align: center;
}

.landing h1 {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
  color: #fff;
}

.landing h1 span {
  color: var(--accent);
}

.landing .subtitle {
  font-size: 1.1rem;
  color: var(--text-secondary);
  margin-bottom: 2rem;
}

.mode-cards {
  display: flex;
  gap: 1.5rem;
  justify-content: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
}

.mode-card {
  background: var(--bg-panel);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  padding: 2rem 1.5rem;
  cursor: pointer;
  transition: border-color 0.3s, transform 0.2s;
  width: 280px;
  text-align: center;
}

.mode-card:hover {
  border-color: var(--accent);
  transform: translateY(-4px);
}

.mode-card .icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.mode-card h2 {
  font-size: 1.3rem;
  margin-bottom: 0.5rem;
  color: #fff;
}

.mode-card p {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* Difficulty Selection */
.difficulty-select {
  display: none;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.difficulty-select.visible {
  display: flex;
}

.diff-btn {
  padding: 0.6rem 1.5rem;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  cursor: pointer;
  font-size: 0.95rem;
  transition: border-color 0.2s, background 0.2s;
}

.diff-btn:hover {
  border-color: var(--accent);
}

.diff-btn .stars {
  display: block;
  color: var(--accent);
  font-size: 0.8rem;
  margin-top: 0.25rem;
}

/* Features Section */
.features {
  margin-top: 3rem;
  text-align: left;
}

.features h2 {
  font-size: 1.5rem;
  margin-bottom: 1.5rem;
  text-align: center;
  color: #fff;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

.feature-item {
  background: var(--bg-panel);
  padding: 1.25rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.feature-item .icon {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
}

.feature-item h3 {
  font-size: 1rem;
  margin-bottom: 0.3rem;
  color: #fff;
}

.feature-item p {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ========== Footer ========== */
.footer {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  padding: 1.5rem 1rem;
  text-align: center;
}

.footer-langs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.footer-langs a {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.footer-langs a:hover {
  color: var(--accent);
}

.footer-copy {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* ========== Responsive: Tablet ========== */
@media (max-width: 1023px) {
  .ad-container-side {
    display: none !important;
  }

  .side-panel {
    width: 200px;
  }
}

/* ========== Responsive: Mobile ========== */
@media (max-width: 767px) {
  .hamburger {
    display: block;
  }

  .header-nav {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border-color);
    flex-direction: column;
    padding: 0.5rem;
  }

  .header-nav.open {
    display: flex;
  }

  .game-layout {
    flex-direction: column;
    align-items: center;
    padding: 0.5rem;
  }

  .side-panel {
    width: 100%;
    max-width: 560px;
  }

  .move-history {
    max-height: 150px;
  }

  .move-history-collapse {
    cursor: pointer;
  }

  .move-history.collapsed .move-history-body {
    display: none;
  }

  .board-wrapper {
    max-width: 100vw;
    padding: 0 4px;
  }

  .landing h1 {
    font-size: 1.8rem;
  }

  .mode-cards {
    flex-direction: column;
    align-items: center;
  }

  .mode-card {
    width: 100%;
    max-width: 320px;
  }

  .controls {
    justify-content: center;
  }
}

/* ========== Desktop: Side Ad ========== */
@media (min-width: 1024px) {
  .ad-container-side {
    display: flex;
    align-items: flex-start;
  }

  .ad-bottom-mobile {
    display: none;
  }
}

/* ========== RTL Support ========== */
[dir="rtl"] .header {
  flex-direction: row-reverse;
}

[dir="rtl"] .header-nav {
  flex-direction: row-reverse;
}

[dir="rtl"] .player-info {
  flex-direction: row-reverse;
}

[dir="rtl"] .move-number {
  text-align: left;
  margin-right: 0;
  margin-left: 0.5rem;
}

[dir="rtl"] .move-history-title {
  text-align: right;
}

[dir="rtl"] .controls {
  flex-direction: row-reverse;
}

[dir="rtl"] .lang-menu {
  right: auto;
  left: 0;
}

[dir="rtl"] .material-diff {
  margin-left: 0;
  margin-right: auto;
}

/* ========== Status Messages ========== */
.status-bar {
  text-align: center;
  padding: 0.4rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--accent);
  min-height: 1.8rem;
}

/* ========== Loading/Thinking Indicator ========== */
.thinking {
  display: none;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.thinking.visible {
  display: flex;
}

.thinking-dots span {
  animation: blink 1.4s infinite both;
  font-size: 1.2rem;
}

.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; }
  40% { opacity: 1; }
}
