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

:root {
  --board-light: #f0d9b5;
  --board-dark: #b58863;
  --board-light-hover: #e5d0a8;
  --board-dark-hover: #a87d56;
  --piece-black: #1a1a1a;
  --piece-white: #f5f5f5;
  --piece-black-border: #000;
  --piece-white-border: #ccc;
  --piece-black-king: #ffd700;
  --piece-white-king: #ffd700;
  --bg: #1e1e2e;
  --bg-card: #2a2a3e;
  --bg-card-hover: #32324a;
  --text: #e0e0e0;
  --text-muted: #8888aa;
  --accent: #7c6ff0;
  --accent-hover: #6a5cd8;
  --accent-light: #9b8ff5;
  --danger: #e74c3c;
  --success: #2ecc71;
  --highlight: rgba(124, 111, 240, 0.6);
  --highlight-capture: rgba(231, 76, 60, 0.7);
  --selected: rgba(255, 215, 0, 0.6);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow: 0 4px 24px rgba(0,0,0,0.3);
  --cell-size: 60px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html, body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  padding: 0;
  margin: 0;
}

#app {
  width: 100%;
  max-width: 520px;
  min-height: 100vh;
  position: relative;
  padding: 16px 16px 24px;
}

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

.panel.hidden {
  display: none;
}

.title {
  text-align: center;
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--accent-light);
  letter-spacing: -0.5px;
  margin-top: 8px;
}

.subtitle {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: -8px;
}

.settings-grid {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.settings-section {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.section-title {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

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

.setting-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.setting-label label {
  font-size: 0.95rem;
  font-weight: 500;
}

.setting-desc {
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.preset-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: -4px;
}

.preset-btn {
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius-sm);
  padding: 4px 12px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  touch-action: manipulation;
  min-width: 36px;
  text-align: center;
}

.preset-btn:hover {
  background: var(--bg-card-hover);
  border-color: var(--accent);
}

.preset-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.setting-row select,
.setting-row input[type=number] {
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 0.9rem;
  cursor: pointer;
  min-width: 80px;
  max-width: 110px;
  text-align: center;
  flex-shrink: 0;
}

/* dropdowns size to their longest option ("♛ Standard" was cut off at 110px) */
.setting-row select {
  max-width: none;
  min-width: 132px;
  text-align: left;
}

.setting-row input[type=number] {
  cursor: text;
  -moz-appearance: textfield;
}

.setting-row input[type=number]::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

.setting-row select:focus,
.setting-row input[type=number]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.toggle {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 26px;
  flex-shrink: 0;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: #444;
  border-radius: 26px;
  transition: background 0.25s;
}

.slider::before {
  content: '';
  position: absolute;
  height: 20px;
  width: 20px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.25s;
}

.toggle input:checked + .slider {
  background: var(--accent);
}

.toggle input:checked + .slider::before {
  transform: translateX(22px);
}

.settings-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  font-size: 0.95rem;
  padding: 12px 20px;
  touch-action: manipulation;
}

.btn:active {
  transform: scale(0.97);
}

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

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.1);
}

.btn-secondary:hover {
  background: var(--bg-card-hover);
}

.btn-small {
  padding: 6px 14px;
  font-size: 0.8rem;
}

.btn-lg {
  padding: 14px 20px;
  font-size: 1.05rem;
}

/* Game Panel */
#game-panel {
  gap: 12px;
  padding-top: 4px;
}

#game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  gap: 8px;
}

#turn-indicator {
  flex: 1;
}

#turn-text {
  font-size: 1.1rem;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  background: var(--bg-card);
}

#turn-text.black-turn {
  color: var(--piece-white);
  border-left: 4px solid var(--piece-black);
}

#turn-text.white-turn {
  color: var(--piece-white);
  border-left: 4px solid var(--piece-white);
}

#timer-display {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

#timer-display.hidden {
  display: none;
}

#timer-black {
  color: var(--piece-white);
}

#timer-white {
  color: var(--piece-white);
}

#timer-sep {
  color: var(--text-muted);
}

.timer-low {
  color: var(--danger) !important;
}

#board-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

#board {
  display: grid;
  width: 100%;
  aspect-ratio: 1;
  max-width: 500px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  aspect-ratio: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.cell.light {
  background: var(--board-light);
}

.cell.dark {
  background: var(--board-dark);
}

.cell.highlight {
  background: var(--highlight);
  cursor: pointer;
}

.cell.highlight-capture {
  background: var(--highlight-capture);
  cursor: pointer;
}

.cell.selected {
  background: var(--selected);
}

.cell.last-move-from {
  box-shadow: inset 0 0 0 3px rgba(255, 165, 0, 0.6);
}

.cell.last-move-to {
  box-shadow: inset 0 0 0 3px rgba(255, 165, 0, 0.8);
}

.piece {
  width: 82%;
  height: 82%;
  border-radius: 50%;
  position: relative;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  touch-action: manipulation;
  z-index: 2;
}

.piece:active {
  transform: scale(0.95);
}

.piece.black-piece {
  background: radial-gradient(circle at 35% 35%, #555, var(--piece-black));
  border: 2px solid var(--piece-black-border);
  box-shadow: 0 2px 6px rgba(0,0,0,0.4), inset 0 1px 2px rgba(255,255,255,0.15);
}

.piece.white-piece {
  background: radial-gradient(circle at 35% 35%, #fff, var(--piece-white));
  border: 2px solid var(--piece-white-border);
  box-shadow: 0 2px 6px rgba(0,0,0,0.2), inset 0 1px 2px rgba(255,255,255,0.9);
}

.piece.king::after {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: min(3vw, 18px);
  line-height: 1;
}

.piece.king.king-standard::after {
  content: '♛';
}

.piece.king.king-flying::after {
  content: '✦';
}

.piece.king.king-knight::after {
  content: '♞';
}

.piece.king.king-crown::after {
  content: '♔';
}

.piece.king.king-queen::after {
  content: '♕';
}

.piece.king.king-random::after {
  content: '❓';
}

.piece.black-piece.king::after {
  color: var(--piece-black-king);
  text-shadow: 0 0 4px rgba(0,0,0,0.5);
}

.piece.white-piece.king::after {
  color: var(--piece-white-king);
  text-shadow: 0 0 4px rgba(0,0,0,0.3);
}

.piece.dragging {
  transform: scale(1.08);
  z-index: 10;
}

/* Modern piece style */
.piece-style-modern .piece {
  border-radius: 30%;
}

.piece-style-modern .piece.black-piece {
  background: linear-gradient(145deg, #333, #111);
}

.piece-style-modern .piece.white-piece {
  background: linear-gradient(145deg, #fff, #ddd);
}

/* Flat piece style */
.piece-style-flat .piece {
  border-radius: 50%;
  box-shadow: none;
}

.piece-style-flat .piece.black-piece {
  background: #222;
}

.piece-style-flat .piece.white-piece {
  background: #eee;
}

/* Color Schemes */
.color-green {
  --board-light: #eeeed5;
  --board-dark: #7b9659;
  --board-light-hover: #e0e0c0;
  --board-dark-hover: #6d8850;
}

.color-blue {
  --board-light: #dee3e6;
  --board-dark: #8ca2ad;
  --board-light-hover: #d0d5d8;
  --board-dark-hover: #7e949f;
}

.color-highcontrast {
  --board-light: #ffffff;
  --board-dark: #000000;
  --board-light-hover: #eee;
  --board-dark-hover: #222;
}

/* Game Footer */
#game-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  gap: 8px;
  flex-wrap: wrap;
}

#footer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

#undo-btn {
  font-size: 0.8rem;
  padding: 4px 10px;
  white-space: nowrap;
}

.undo-actions {
  flex-direction: column;
  gap: 8px;
}

/* players under the board: whose turn it is and how many pieces each side lost */
#status-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.status-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  border: 2px solid transparent;
  transition: border-color 0.2s, background 0.2s;
}

.status-card.active {
  border-color: var(--accent);
  background: var(--bg-card-hover);
}

.status-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-swatch.black-piece { background: radial-gradient(circle at 35% 35%, #555, var(--piece-black)); border: 2px solid var(--piece-black-border); }
.status-swatch.white-piece { background: radial-gradient(circle at 35% 35%, #fff, var(--piece-white)); border: 2px solid var(--piece-white-border); }

.status-name {
  font-weight: 700;
  flex: 1;
}

.status-name small {
  font-weight: 500;
  color: var(--text-muted);
}

.status-lost {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* a forced capture: the pieces that can capture */
.cell.must-capture {
  box-shadow: inset 0 0 0 4px var(--danger);
  animation: mustCapture 0.6s ease-in-out 3;
}

@keyframes mustCapture { 50% { box-shadow: inset 0 0 0 7px var(--danger); } }

/* Board Editor */
.editor-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: -4px;
}

.editor-toolbar {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.editor-tool-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.editor-tool-group label {
  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
}

.editor-tool-group select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 0.85rem;
  min-width: 80px;
  cursor: pointer;
  max-width: 120px;
}

.editor-tool-group select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.king-mode-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.mode-chip {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  user-select: none;
  touch-action: manipulation;
}

.mode-chip:has(input:checked) {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.mode-chip input {
  display: none;
}

.editor-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

#editor-board-wrapper {
  display: flex;
  justify-content: center;
  width: 100%;
  margin-top: 4px;
}

#editor-board {
  display: grid;
  width: 100%;
  max-width: 340px;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-select: none;
}

.editor-cell {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  aspect-ratio: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.editor-cell.light {
  background: var(--board-light);
}

.editor-cell.dark {
  background: var(--board-dark);
}

.editor-cell.dark:hover {
  background: var(--board-dark-hover);
}

.editor-piece {
  width: 78%;
  height: 78%;
  border-radius: 50%;
  position: relative;
  transition: transform 0.1s;
  pointer-events: none;
}

.editor-piece.black-piece {
  background: radial-gradient(circle at 35% 35%, #555, var(--piece-black));
  border: 2px solid var(--piece-black-border);
  box-shadow: 0 2px 6px rgba(0,0,0,0.4), inset 0 1px 2px rgba(255,255,255,0.15);
}

.editor-piece.white-piece {
  background: radial-gradient(circle at 35% 35%, #fff, var(--piece-white));
  border: 2px solid var(--piece-white-border);
  box-shadow: 0 2px 6px rgba(0,0,0,0.2), inset 0 1px 2px rgba(255,255,255,0.9);
}

.editor-king-symbol {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: min(2.5vw, 14px);
  line-height: 1;
  pointer-events: none;
}

.editor-piece.black-piece .editor-king-symbol {
  color: var(--piece-black-king);
  text-shadow: 0 0 4px rgba(0,0,0,0.5);
}

.editor-piece.white-piece .editor-king-symbol {
  color: var(--piece-white-king);
  text-shadow: 0 0 4px rgba(0,0,0,0.3);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  backdrop-filter: blur(4px);
  padding: 24px;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: var(--shadow);
}

.modal-content h2 {
  font-size: 1.5rem;
  margin-bottom: 12px;
  color: var(--accent-light);
}

.modal-content p {
  font-size: 1.2rem;
  margin-bottom: 4px;
}

.modal-content .reason {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  color: var(--text);
  padding: 12px 24px;
  border-radius: var(--radius);
  z-index: 200;
  font-weight: 500;
  font-size: 0.9rem;
  border: 1px solid rgba(255,255,255,0.1);
  box-shadow: var(--shadow);
  transition: opacity 0.3s, transform 0.3s;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(12px);
  pointer-events: none;
}

.hidden {
  display: none !important;
}

@media (max-width: 480px) {
  #app {
    padding: 8px 10px 20px;
  }

  .title {
    font-size: 1.4rem;
    margin-top: 4px;
  }

  .subtitle {
    font-size: 0.8rem;
  }

  .settings-section {
    padding: 12px;
  }

  .setting-label label {
    font-size: 0.85rem;
  }

  .setting-desc {
    font-size: 0.65rem;
  }

  .setting-row select,
  .setting-row input[type=number] {
    min-width: 70px;
    padding: 6px 10px;
    font-size: 0.8rem;
  }

  .preset-btn {
    padding: 3px 8px;
    font-size: 0.75rem;
    min-width: 30px;
  }

  .mode-chip {
    padding: 4px 8px;
    font-size: 0.75rem;
  }

  .btn-lg {
    padding: 12px 16px;
    font-size: 0.95rem;
  }

  .modal-content {
    padding: 24px 20px;
  }

  #turn-text {
    font-size: 0.95rem;
    padding: 4px 10px;
  }

  #timer-display {
    font-size: 0.8rem;
  }

  .piece.king::after {
    font-size: min(4vw, 16px);
  }

  /* phones: the space under the board holds the players, larger and easy to read */
  #status-bar { gap: 10px; margin-top: 4px; }
  .status-card { padding: 14px 16px; font-size: 1.05rem; }
  .status-swatch { width: 28px; height: 28px; }
  .setting-row select { min-width: 124px; }
}

@media (max-height: 600px) {
  #app {
    padding: 4px 10px 12px;
  }

  .title {
    font-size: 1.2rem;
    margin-top: 2px;
  }

  .subtitle {
    display: none;
  }

  .settings-section {
    padding: 8px 12px;
    gap: 8px;
  }

  .settings-actions {
    gap: 6px;
  }

  .btn-lg {
    padding: 10px 14px;
    font-size: 0.85rem;
  }

  #game-header {
    padding: 2px 0;
  }
}

/* ===== Multiplayer ===== */
.mp-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  flex-direction: column;
}
.mp-overlay-inner {
  text-align: center;
  color: #fff;
  padding: 40px;
}
.mp-overlay-inner h2 {
  font-size: 1.8rem;
  margin-bottom: 12px;
}
.mp-overlay-inner p {
  font-size: 1rem;
  opacity: 0.8;
}
.mp-spinner {
  width: 48px; height: 48px;
  border: 4px solid rgba(255,255,255,0.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 24px;
}
@keyframes spin { to { transform: rotate(360deg); } }
@-webkit-keyframes spin { to { -webkit-transform: rotate(360deg); } }

.role-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-right: 8px;
  flex-shrink: 0;
}
.role-badge.black {
  background: #111;
  color: #fff;
  border: 1px solid #555;
}
.role-badge.white {
  background: #f5f5f5;
  color: #111;
}

.mp-link {
  margin-top: 14px;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.85rem !important;
  word-break: break-all;
  background: rgba(255,255,255,0.08);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  user-select: all;
}

.mp-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 18px;
}

.mp-overlay-inner {
  max-width: 460px;
}
