:root {
  --bg: #f4efe6;
  --panel: #fffaf2;
  --line: #d7c7ab;
  --text: #2a2118;
  --accent: #8b5e34;
  --light-square: #ecd8b1;
  --dark-square: #b58863;
  --highlight: #4caf50;
  --capture: #d32f2f;
  --selected: #1976d2;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, sans-serif;
  background: linear-gradient(180deg, #f9f3e7, #efe3ce);
  color: var(--text);
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.topbar h1 {
  margin: 0;
}

.subtitle {
  margin: 6px 0 0;
  color: #5e5143;
}

.controls {
  display: flex;
  gap: 10px;
}

button {
  border: none;
  background: var(--accent);
  color: white;
  padding: 10px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 15px;
}

button:hover {
  filter: brightness(1.06);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(320px, 640px) 320px;
  gap: 20px;
  align-items: start;
}

.board-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  position: relative;
}

.board {
  width: min(80vw, 600px);
  height: min(80vw, 600px);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 4px solid #6d4c2f;
  border-radius: 10px;
  overflow: hidden;
  margin: 0 auto;
  position: relative;
}

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

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

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

.square.selected {
  outline: 4px solid var(--selected);
  outline-offset: -4px;
}

.square.move::after,
.square.capture::after {
  content: "";
  position: absolute;
  width: 18%;
  height: 18%;
  border-radius: 50%;
}

.square.move::after {
  background: rgba(76, 175, 80, 0.9);
}

.square.capture::after {
  width: 70%;
  height: 70%;
  border: 4px solid rgba(211, 47, 47, 0.9);
  background: transparent;
}

/* Enhanced highlighting styles */
.square.check {
  background: #ffd700 !important;
  box-shadow: inset 0 0 8px rgba(255, 215, 0, 0.8);
}

.square.last-move-from {
  background: rgba(100, 200, 100, 0.3);
}

.square.last-move-to {
  background: rgba(100, 200, 100, 0.5);
}

.square.checkmate {
  background: #ff6b6b !important;
  box-shadow: inset 0 0 8px rgba(255, 107, 107, 0.8);
}

.piece {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(24px, 4vw, 40px);
  font-weight: 700;
  cursor: pointer;
  position: relative;
}

.piece.white {
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.piece.black {
  color: #111;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.25);
}

.piece img {
  width: 86%;
  height: 86%;
  object-fit: contain;
  pointer-events: none;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.06);
}

.card h2 {
  margin-top: 0;
}

.card ul {
  margin: 0;
  padding-left: 18px;
}

/* Control elements styling */
.control-select,
.control-btn {
  border: none;
  background: var(--accent);
  color: white;
  padding: 10px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  margin-right: 8px;
  transition: all 0.2s ease;
}

.control-select {
  appearance: none;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23ffffff' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 16px;
  padding-right: 32px;
}

.control-select:hover {
  filter: brightness(1.08);
}

.control-btn {
  padding: 10px 12px;
  font-size: 13px;
}

.control-btn:hover {
  filter: brightness(1.08);
}

/* Move history styling */
.move-list {
  list-style: none;
  padding: 8px;
  margin: 0;
  background: rgba(0, 0, 0, 0.02);
  border-radius: 8px;
  max-height: 180px;
  overflow-y: auto;
  font-family: "Courier New", monospace;
}

.move-list li {
  padding: 4px 8px;
  border-radius: 4px;
  margin: 2px 0;
  font-size: 12px;
  transition: background 0.15s;
}

.move-list li:hover {
  background: rgba(139, 94, 52, 0.1);
}

/* ===== Endgame HUD Overlay ===== */
.endgame-hud {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 15px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(2px);
  z-index: 10;
  pointer-events: none;
  animation: fadeIn 0.3s ease-in;
}

.endgame-hud.hidden {
  display: none;
}

.endgame-content {
  text-align: right;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}

.endgame-label {
  font-size: 0.85rem;
  font-weight: bold;
  opacity: 0.9;
  margin-bottom: 0.2rem;
  letter-spacing: 0.5px;
  color: #e0e0e0;
}

.endgame-countdown {
  font-size: 2rem;
  font-weight: bold;
  color: #ff6b6b;
  text-shadow: 0 2px 5px rgba(255, 107, 107, 0.5);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

#movesRemainingDisplay {
  display: inline-block;
  min-width: 2.2rem;
  text-align: center;
}

/* Fullscreen Mode Styles */
body.fullscreen-mode {
  margin: 0;
  padding: 0;
  background: #6d4c2f;
}

.app.fullscreen-mode {
  max-width: 100%;
  margin: 0;
  padding: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar.fullscreen-mode {
  min-height: 50px;
  padding: 10px 15px;
  gap: 10px;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1000;
  background: rgba(109, 76, 47, 0.95);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  transform: translateY(-100%);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.topbar.fullscreen-mode::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  height: 20px;
  background: transparent;
}

.topbar.fullscreen-mode:hover,
.topbar.fullscreen-mode:focus-within {
  transform: translateY(0);
}

.topbar.fullscreen-mode h1 {
  font-size: 18px;
  margin: 0;
  flex-shrink: 0;
}

.topbar.fullscreen-mode .subtitle {
  display: none;
}

.topbar.fullscreen-mode .controls {
  flex-direction: row;
  gap: 8px;
}

.topbar.fullscreen-mode button {
  padding: 8px 10px;
  font-size: 12px;
}

.game-layout.fullscreen-mode {
  grid-template-columns: 1fr;
  gap: 0;
  flex: 1;
  margin: 0;
  padding: 0;
}

.board-panel.fullscreen-mode {
  background: #6d4c2f;
  border: none;
  border-radius: 0;
  padding: 15px;
  box-shadow: none;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  margin: 0;
}

.board.fullscreen-mode {
  width: min(95vh, 95vw);
  height: min(95vh, 95vw);
  margin: 0 auto;
}

.side-panel.fullscreen-mode {
  display: none;
}

.topbar.fullscreen-mode>div:last-child {
  text-align: right;
}

@media (max-width: 980px) {
  .game-layout {
    grid-template-columns: 1fr;
  }

  .board {
    width: min(92vw, 600px);
    height: min(92vw, 600px);
  }

  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }
}

/* Game End Modal Styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease-out;
}

.modal.hidden {
  display: none;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

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

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal-content {
  background: var(--panel);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  max-width: 500px;
  width: 90%;
  animation: slideDown 0.3s ease-out;
  border: 2px solid var(--accent);
  overflow: hidden;
}

.modal-header {
  background: linear-gradient(135deg, var(--accent), #a67c52);
  color: white;
  padding: 30px 20px;
  text-align: center;
}

.modal-header h2 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
}

.modal-body {
  padding: 30px 20px;
}

.result-display {
  text-align: center;
  margin-bottom: 25px;
}

.result-message {
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 10px 0;
  color: var(--accent);
}

.result-message.win {
  color: #4caf50;
}

.result-message.lose {
  color: #d32f2f;
}

.result-message.draw {
  color: #ff9800;
}

.result-subtitle {
  font-size: 16px;
  color: #666;
  margin: 0;
  font-weight: 500;
}

.game-stats {
  background: rgba(139, 94, 52, 0.08);
  border-radius: 12px;
  padding: 20px;
  margin: 20px 0;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid rgba(139, 94, 52, 0.1);
  font-size: 15px;
}

.stat-row:last-child {
  border-bottom: none;
}

.stat-label {
  font-weight: 600;
  color: var(--text);
}

.stat-value {
  color: var(--accent);
  font-weight: 700;
}

.modal-actions {
  padding: 20px;
  display: flex;
  gap: 12px;
  justify-content: center;
}

.modal-btn {
  flex: 1;
  padding: 14px 20px;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.modal-btn.primary {
  background: var(--accent);
  color: white;
}

.modal-btn.primary:hover {
  background: #9d6b3a;
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(139, 94, 52, 0.3);
}

.modal-btn.secondary {
  background: #e0e0e0;
  color: var(--text);
}

.modal-btn.secondary:hover {
  background: #d0d0d0;
  transform: translateY(-2px);
}