/*
Theme Name: Chesson
Theme URI: https://www.idealizer.ch/wordpress/chesson
Author: Idealizer LLC
Author URI: https://www.idealizer.ch
Description: Chesson – a chess training theme with interactive board, move visualization, theory, and PGN storage.
Version: 0.2.0
Text Domain: chesson
Requires at least: 6.4
Tested up to: 6.6
Requires PHP: 8.1
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

/* Base theme variables */
:root {
  --chesson-bg: #0f172a; /* slate-900 */
  --chesson-panel: #111827; /* gray-900 */
  --chesson-text: #e5e7eb; /* gray-200 */
  --chesson-accent: #22c55e; /* green-500 */
  --chesson-muted: #6b7280; /* gray-500 */
  --chesson-border: #1f2937; /* gray-800 */
}

body {
  background: var(--chesson-bg);
  color: var(--chesson-text);
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Helvetica Neue, Arial, "Apple Color Emoji", "Segoe UI Emoji";
}

/* Layout util for two-column app */
.chesson-app {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 16px;
  align-items: start;
}

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

.chesson-panel {
  background: var(--chesson-panel);
  border: 1px solid var(--chesson-border);
  border-radius: 8px;
  padding: 12px;
}

.chesson-heading {
  margin: 0 0 8px 0;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
  color: var(--chesson-text);
}

/* Board wrapper and overlay */
.chesson-board-wrap {
  position: relative;
  width: 100%;
  max-width: calc(100vh - 160px); /* keep board fully visible */
  aspect-ratio: 1 / 1;
  background: #000; /* fallback underlay */
  border-radius: 8px;
  overflow: hidden;
  align-self: start;
  margin: 0 auto;
}

.chesson-left {
  display: grid;
  gap: 8px;
  align-content: start;
}

.chesson-board-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.chesson-board-fs-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--chesson-border);
  background: rgba(0, 0, 0, 0.55);
  color: var(--chesson-text);
  z-index: 20;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.chesson-board-fs-btn:hover {
  background: rgba(0, 0, 0, 0.75);
}

.chesson-game-controls-hidden {
  display: none;
}

.chesson-inline-config {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 4px;
}

/* Chessboard.js styling */
#chessonBoard {
  width: 100%;
  height: 100%;
  touch-action: none;
}

.chesson-board-overlay {
  position: absolute;
  inset: 0; /* fallback; JS syncOverlayToBoard() overrides with exact board dimensions */
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  pointer-events: none;
  z-index: 2;
  box-sizing: border-box;
}

.chesson-square-overlay {
  pointer-events: none;
}

/* Notation panel */
.chesson-notation {
  display: grid;
  gap: 8px;
  min-height: 0;
}

.chesson-notation-main {
  display: grid;
  grid-template-columns: minmax(90px, 120px) minmax(0, 1fr);
  gap: 10px;
  align-items: start;
}

.chesson-moves-wrap {
  min-height: 0;
  max-height: 220px;
  overflow-y: auto;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--chesson-border);
  border-radius: 8px;
  padding: 8px;
}

.chesson-notation .last-move {
  font-size: 30px;
  font-weight: 800;
  line-height: 1.1;
  align-self: center;
  text-align: center;
  padding: 12px 8px;
  background: rgba(34, 197, 94, 0.08);
  border: 1px solid rgba(34, 197, 94, 0.2);
  border-radius: 8px;
}

#chessonMoves {
  margin: 0;
  padding-left: 20px;
  font-size: 12px;
}

/* Move navigation bar */
.chesson-nav {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.chesson-nav-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 32px;
  background: var(--chesson-border);
  border: 1px solid var(--chesson-border);
  color: var(--chesson-text);
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}
.chesson-nav-btn:hover { background: #263244; }
.chesson-nav-btn:disabled {
  opacity: 0.3;
  cursor: default;
}
.chesson-nav-btn:disabled:hover { background: var(--chesson-border); }

.chesson-reviewing .last-move {
  color: var(--chesson-muted);
  font-style: italic;
}

#chessonTheory {
  min-height: 48px;
  max-height: 180px;
  overflow: auto;
}

.chesson-theory-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.chesson-theory-item {
  padding: 6px 8px;
  border: 1px solid var(--chesson-border);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.03);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chesson-fs-tools {
  display: none;
}

.chesson-threat-toggle.active {
  background: rgba(34, 197, 94, 0.2);
  border-color: rgba(34, 197, 94, 0.6);
}

/* Basic buttons and form controls */
.chesson-btn {
  display: inline-flex !important;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--chesson-border);
  border: 1px solid var(--chesson-border);
  color: var(--chesson-text);
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  text-decoration: none;
}

.chesson-btn:hover {
  background: #263244;
}

/* Form elements */
input, select, button {
  font-family: inherit;
  font-size: inherit;
}

/* ELO Slider styling - force visibility */
#chessonEloSlider {
  -webkit-appearance: none !important;
  -moz-appearance: none !important;
  appearance: none !important;
  width: 120px !important;
  height: 8px !important;
  background: var(--chesson-border) !important;
  border-radius: 4px !important;
  outline: none !important;
  cursor: pointer !important;
  display: block !important;
  margin: 4px 0 !important;
}

#chessonEloSlider::-webkit-slider-thumb {
  -webkit-appearance: none !important;
  appearance: none !important;
  width: 20px !important;
  height: 20px !important;
  background: var(--chesson-accent) !important;
  border-radius: 50% !important;
  cursor: pointer !important;
  border: 2px solid #fff !important;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3) !important;
}

#chessonEloSlider::-moz-range-thumb {
  width: 20px !important;
  height: 20px !important;
  background: var(--chesson-accent) !important;
  border-radius: 50% !important;
  border: 2px solid #fff !important;
  cursor: pointer !important;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3) !important;
}

#chessonEloSlider::-moz-range-track {
  background: var(--chesson-border) !important;
  height: 8px !important;
  border-radius: 4px !important;
}

/* Control labels and display */
#chessonEloDisplay {
  font-size: 14px !important;
  color: var(--chesson-accent) !important;
  font-weight: bold !important;
  min-width: 50px !important;
  text-align: center !important;
  display: inline-block !important;
}

/* Debug: make controls highly visible */
#chessonPlayerColor, #chessonEloSlider, #chessonEloDisplay {
  opacity: 1 !important;
  visibility: visible !important;
  display: inline-block !important;
}

/* Animations */
@keyframes pulse {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.7; transform: scale(1.05); }
  100% { opacity: 1; transform: scale(1); }
} 

/* Alignment fixes for board and overlay */
.chesson-board-area { position: absolute; left: 16px; right: 0; top: 8px; bottom: 8px; }
/* Remove default chessboard.js container border to avoid 2px offset */
#chessonBoard .board-b72b1 { border: 0 !important; }

/* Board square color customization via CSS custom properties (set by JS from localStorage prefs) */
#chessonBoard .white-1e1d7 {
  background-color: var(--chesson-sq-light, #f0d9b5) !important;
}
#chessonBoard .black-3c85d {
  background-color: var(--chesson-sq-dark, #b58863) !important;
}

/* Fullscreen: body is the FS element so chessboard.js drag pieces (appended to body) stay visible */
body:fullscreen,
body:-webkit-full-screen {
  background: var(--chesson-bg) !important;
  overflow: hidden !important;
  margin: 0 !important;
  padding: 0 !important;
}

/* chessboard.js appends drag/animation piece <img> directly to <body>.
   In fullscreen .chesson-app gets z-index:9998, so these images must be above it. */
body:fullscreen > .piece-417db,
body:-webkit-full-screen > .piece-417db {
  z-index: 99999 !important;
}

body:fullscreen .chesson-app,
body:-webkit-full-screen .chesson-app {
  position: fixed !important;
  inset: 0 !important;
  z-index: 9998 !important;
  padding: 16px;
  box-sizing: border-box;
  background: var(--chesson-bg);
  height: 100dvh;
  align-content: stretch;
  align-items: stretch;
  grid-template-columns: minmax(280px, min(68vh, calc(100vw - 420px))) minmax(320px, 1fr);
  overflow: hidden;
}

body:fullscreen .chesson-app .chesson-left,
body:-webkit-full-screen .chesson-app .chesson-left {
  display: flex;
  align-items: center;
  justify-content: center;
}

body:fullscreen .chesson-app .chesson-board-wrap,
body:-webkit-full-screen .chesson-app .chesson-board-wrap {
  width: min(68vh, calc(100vw - 420px));
  max-width: calc(100vh - 32px);
  align-self: center;
}

body:fullscreen .chesson-app .chesson-panel,
body:-webkit-full-screen .chesson-app .chesson-panel {
  display: grid;
  grid-auto-rows: max-content;
  align-content: start;
  gap: 8px;
  overflow-y: auto;
  max-height: calc(100vh - 32px);
  align-self: center;
}

body:fullscreen .chesson-app .chesson-fs-tools,
body:-webkit-full-screen .chesson-app .chesson-fs-tools {
  display: flex;
  justify-content: flex-end;
}

body:fullscreen .chesson-app #chessonTheory,
body:-webkit-full-screen .chesson-app #chessonTheory {
  max-height: 200px;
}

body:fullscreen .chesson-app .chesson-moves-wrap,
body:-webkit-full-screen .chesson-app .chesson-moves-wrap {
  max-height: 300px;
}

/* Hide game setup controls in fullscreen (close btn + modal replace them) */
body:fullscreen .chesson-app #chessonGameControls,
body:-webkit-full-screen .chesson-app #chessonGameControls {
  display: none !important;
}

/* Hide WP admin bar in fullscreen */
body:fullscreen #wpadminbar,
body:-webkit-full-screen #wpadminbar {
  display: none !important;
}

/* Fullscreen close button */
.chesson-fs-close {
  display: none;
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid var(--chesson-border);
  color: var(--chesson-text);
  border-radius: 8px;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  z-index: 100;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.chesson-fs-close:hover { background: rgba(220, 38, 38, 0.8); }

body:fullscreen .chesson-app .chesson-fs-close,
body:-webkit-full-screen .chesson-app .chesson-fs-close {
  display: flex;
}

body:fullscreen .chesson-app .chesson-board-fs-btn,
body:-webkit-full-screen .chesson-app .chesson-board-fs-btn {
  display: none;
}

/* Game-over modal */
.chesson-modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  backdrop-filter: blur(2px);
}

.chesson-modal {
  position: relative;
  background: var(--chesson-panel);
  border: 1px solid var(--chesson-border);
  border-radius: 12px;
  padding: 28px 32px;
  min-width: 300px;
  max-width: 90vw;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.chesson-modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  background: transparent;
  border: 1px solid var(--chesson-border);
  color: var(--chesson-muted);
  border-radius: 6px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chesson-modal-close:hover { color: var(--chesson-text); background: rgba(255,255,255,0.1); }

.chesson-modal-title {
  font-size: 22px;
  font-weight: 800;
  margin-bottom: 4px;
  color: var(--chesson-text);
}

.chesson-btn-primary {
  background: var(--chesson-accent) !important;
  color: #000 !important;
  font-weight: 700 !important;
  padding: 10px 20px !important;
}
.chesson-btn-primary:hover { opacity: 0.9; }

.chesson-modal-slider {
  -webkit-appearance: none !important;
  appearance: none !important;
  height: 8px !important;
  background: var(--chesson-border) !important;
  border-radius: 4px !important;
  outline: none !important;
  cursor: pointer !important;
}
.chesson-modal-slider::-webkit-slider-thumb {
  -webkit-appearance: none !important;
  width: 18px !important;
  height: 18px !important;
  background: var(--chesson-accent) !important;
  border-radius: 50% !important;
  cursor: pointer !important;
  border: 2px solid #fff !important;
}
.chesson-modal-slider::-moz-range-thumb {
  width: 18px !important;
  height: 18px !important;
  background: var(--chesson-accent) !important;
  border-radius: 50% !important;
  border: 2px solid #fff !important;
  cursor: pointer !important;
}

/* Settings page */
.chesson-settings {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
}

.chesson-settings-section {
  margin-bottom: 24px;
}

.chesson-theme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 12px;
}

.chesson-theme-swatch {
  background: var(--chesson-panel);
  border: 2px solid var(--chesson-border);
  border-radius: 8px;
  padding: 10px 8px 8px;
  cursor: pointer;
  text-align: center;
  color: var(--chesson-text);
  font-size: 12px;
  transition: border-color 0.2s;
}
.chesson-theme-swatch:hover { border-color: var(--chesson-muted); }
.chesson-theme-swatch.active { border-color: var(--chesson-accent); box-shadow: 0 0 0 1px var(--chesson-accent); }

.chesson-swatch-board {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  width: 48px;
  height: 48px;
  margin: 0 auto 6px;
  border-radius: 4px;
  overflow: hidden;
}

.chesson-preview-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 280px;
  height: 280px;
  border-radius: 4px;
  overflow: hidden;
  border: 2px solid var(--chesson-border);
}

.chesson-color-input {
  width: 40px;
  height: 32px;
  padding: 2px;
  border: 1px solid var(--chesson-border);
  border-radius: 4px;
  background: var(--chesson-panel);
  cursor: pointer;
}

/* Mobile: prevent board from reflowing when panel grows */
@media (max-width: 980px) {
  /* App page only: remove theme chrome to maximize board area. */
  body.chesson-has-app > header,
  body.chesson-has-app > footer {
    display: none !important;
  }

  .chesson-app {
    min-height: 100dvh;
    gap: 10px;
  }
  .chesson-left {
    position: static;
    z-index: 10;
    background: var(--chesson-bg);
    padding-top: 4px;
  }
  .chesson-board-wrap {
    max-width: min(100vw - 12px, 560px);
  }
  .chesson-notation-main {
    grid-template-columns: 88px minmax(0, 1fr);
    gap: 8px;
  }
  .chesson-notation .last-move {
    font-size: 24px;
  }
  .chesson-moves-wrap {
    max-height: 160px;
  }
  .chesson-theory-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
  }
  .chesson-theory-item {
    font-size: 11px;
    padding: 5px 6px;
  }
  .chesson-board-actions .chesson-btn {
    min-width: 96px;
  }
  #chessonMoves {
    font-size: 11px;
  }
  .chesson-preview-board {
    width: 100%;
    max-width: 280px;
    height: auto;
    aspect-ratio: 1;
  }
}