:root {
  --bg: #1e2240;
  --surface: #16213e;
  --primary: #0f3460;
  --accent: #e94560;
  --success: #4ecca3;
  --text: #eee;
  --text-dim: #999;
  --warning: #f0a500;
  --primary-hover: #1a4a8a;
}

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

#demo-banner {
  background: var(--primary);
  text-align: center;
  padding: 0.4rem 1rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}

#demo-banner a {
  color: var(--success);
  text-decoration: underline;
}

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

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

header h1 {
  font-size: 1.1rem;
  font-weight: 600;
}

/* Mode toggle (segmented control) */
#header-title {
  flex: 1;
  text-align: center;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

/* --- Game list toolbar --- */
#game-list-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  flex-wrap: wrap;
}

#game-list-toolbar select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 4px;
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
  cursor: pointer;
}

#select-all-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-dim);
  cursor: pointer;
}

#game-limit-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 4px;
  padding: 0.25rem 0.5rem;
  font-size: 0.8rem;
}

#analyze-selected-btn {
  margin-left: auto;
  background: var(--primary);
  color: var(--text);
  border: none;
  border-radius: 4px;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
}

#analyze-selected-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.game-card-checkbox {
  margin-right: 0.25rem;
  cursor: pointer;
  flex-shrink: 0;
}

.game-list-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.game-list-pagination button {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.15);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
}

.game-list-pagination button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.game-list-pagination button:hover:not(:disabled) {
  background: var(--primary);
}

#review-train-btn {
  background: #1baca6;
  color: var(--text);
  border: none;
  border-radius: 4px;
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
  cursor: pointer;
}

#session-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.95rem;
}

#analysis-progress {
  font-size: 0.8rem;
  color: var(--text);
  background: var(--primary);
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

#menu-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 0.25rem;
}

/* Slide-out navigation menu */
#nav-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
}

#nav-menu {
  position: fixed;
  top: 0;
  left: 0;
  width: 260px;
  height: 100%;
  background: var(--surface);
  z-index: 201;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  display: flex;
  flex-direction: column;
}

#nav-menu.nav-open {
  transform: translateX(0);
}

#nav-header {
  padding: 1.25rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}

#nav-version {
  font-size: 0.8rem;
  color: var(--text-dim);
  font-weight: 400;
}

#nav-items {
  list-style: none;
  padding: 0.5rem 0;
}

.nav-item {
  padding: 0.75rem 1rem;
  cursor: pointer;
  transition: background 0.15s;
}

.nav-item:hover {
  background: var(--primary);
}

.nav-item.disabled {
  color: var(--text-dim);
  cursor: default;
  opacity: 0.5;
}

.nav-item.disabled:hover {
  background: none;
}

.nav-separator {
  border-top: 1px solid rgba(255,255,255,0.1);
  margin: 0.25rem 0;
}

#training-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  gap: 1rem;
}

/* Board wrapper: contains material balance + board */
#board-wrapper {
  width: min(88vw, 480px);
}

/* Board sizing: square, responsive, with space for outside coordinates */
#board-container {
  width: 100%;
  aspect-ratio: 1;
  padding-left: 1.5rem;
  padding-bottom: 1.5rem;
  position: relative;
}

#board {
  width: 100%;
  height: 100%;
  position: relative;
}

.board-info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.clock {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.material-balance {
  height: 1.6rem;
  font-size: 1.3rem;
  color: var(--text-dim);
  letter-spacing: 0.08em;
  padding-left: 1.5rem;
  white-space: nowrap;
  line-height: 1.6rem;
}

/* Override chessground dimensions */
cg-container {
  width: 100% !important;
  height: 100% !important;
}

/* Move coordinates outside the board.
   IMPORTANT: do NOT override transform or flex-direction on coords —
   Chessground uses transform to position each coord based on orientation. */
.cg-wrap coords {
  pointer-events: none;
}

.cg-wrap coords.ranks {
  left: -1.5rem !important;
  top: 0 !important;
  width: 1.5rem !important;
  align-items: center !important;
}

.cg-wrap coords.ranks coord {
  transform: none !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

.cg-wrap coords.files {
  bottom: -1.5rem !important;
  left: 0 !important;
  height: 1.5rem !important;
  align-items: center !important;
  justify-content: space-around !important;
}

.cg-wrap coords coord {
  font-weight: 600 !important;
  color: var(--text-dim) !important;
  font-size: 0.8rem !important;
  text-shadow: none !important;
  text-align: center !important;
}

#info-panel {
  width: min(88vw, 480px);
  text-align: center;
}

#prompt {
  font-size: 1.05rem;
  margin-bottom: 0.5rem;
  line-height: 1.4;
}

#game-info {
  font-size: 0.8rem;
  color: var(--text-dim);
}

#game-info a {
  color: var(--text-dim);
  text-decoration: underline;
}

#game-info a:hover {
  color: var(--accent);
}

.see-moves-link {
  display: inline-block;
  margin-top: 0.4rem;
  font-size: 1.05rem;
  color: var(--text-dim) !important;
  text-decoration: underline;
}

.see-moves-link:hover {
  color: var(--text) !important;
}

#feedback {
  margin-top: 0.75rem;
  padding: 0.75rem;
  border-radius: 8px;
  background: var(--surface);
}

.correct {
  color: var(--success);
  font-weight: 600;
  font-size: 1.1rem;
}

.incorrect {
  color: var(--accent);
  font-weight: 600;
  font-size: 1.1rem;
}

.try-again {
  color: var(--warning);
  font-weight: 600;
}

#explanation {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.5;
}

#eval-summary {
  margin-top: 0.5rem;
  font-size: 0.95rem;
  color: var(--success);
  font-weight: 600;
  font-family: monospace;
  white-space: pre-line;
}

#controls {
  margin-top: 0.75rem;
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
}

#pv-line {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  color: var(--success);
  font-family: monospace;
  letter-spacing: 0.03em;
}

#show-position-btn,
#play-line-btn {
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid rgba(255,255,255,0.2);
  padding: 0.6rem 1.2rem;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
}

#show-position-btn:hover,
#play-line-btn:hover {
  background: var(--primary);
  color: var(--text);
}

#dismiss-btn {
  background: none;
  color: var(--text-dim);
  border: 1px solid rgba(255,255,255,0.1);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
  flex-basis: 100%;
  text-align: center;
}

#dismiss-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

#skip-btn,
#show-answer-btn {
  background: none;
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.2);
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.15s;
}

#skip-btn:hover,
#show-answer-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

#retry-btn,
#next-btn,
#new-session {
  background: var(--primary);
  color: var(--text);
  border: none;
  padding: 0.6rem 2rem;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s;
}

#retry-btn:hover,
#next-btn:hover,
#new-session:hover {
  background: var(--primary-hover);
}

.thinking {
  color: var(--text-dim);
  font-size: 0.95rem;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.hidden {
  display: none !important;
}

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

.modal-content {
  background: var(--surface);
  padding: 1.5rem;
  border-radius: 12px;
  width: min(90vw, 400px);
  max-height: 85vh;
  overflow-y: auto;
}

.modal-content h2 {
  margin-bottom: 1rem;
}

.modal-content label {
  display: block;
  margin-bottom: 0.75rem;
}

.modal-content input[type="number"],
.modal-content select {
  margin-left: 0.5rem;
  padding: 0.3rem 0.5rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 4px;
}

.narrow-input {
  width: 4rem;
}

.modal-content details {
  margin-top: 0.75rem;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
}

.modal-content summary {
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.modal-content details[open] summary {
  margin-bottom: 0.5rem;
}

.modal-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.2);
}

.fetch-option {
  background: var(--primary);
  color: var(--text);
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s;
}

.fetch-option:hover {
  background: var(--primary-hover);
}

.fetch-option:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.fetch-count-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.fetch-count-row input {
  width: 5rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 4px;
  padding: 0.4rem;
  font-size: 1rem;
  text-align: center;
}

.modal-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 1.25rem;
}

.modal-actions button {
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  border: none;
  font-size: 0.9rem;
}

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

.modal-body {
  margin-bottom: 1rem;
  font-size: 0.9rem;
  line-height: 1.8;
}

#reset-progress {
  background: var(--accent);
  color: white;
}

#summary-stats {
  margin-bottom: 1rem;
  font-size: 1.1rem;
  line-height: 1.6;
}

#about-content {
  font-size: 0.95rem;
}

#about-content a {
  color: var(--success);
  text-decoration: underline;
}

/* Unified Settings Modal */
.settings-wide {
  width: min(90vw, 440px);
}

.settings-section {
  border: none;
  padding: 0;
  margin: 0 0 1rem 0;
  padding-bottom: 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.settings-section legend {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 0.75rem;
  color: var(--text);
}

.preset-group {
  margin-top: 0.5rem;
}

.preset-label {
  display: block;
  margin-bottom: 0.5rem;
}

.preset-buttons {
  display: flex;
  gap: 0.5rem;
}

.preset-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem 0.25rem;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.2);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
  transition: border-color 0.15s, background 0.15s;
}

.preset-btn:hover {
  border-color: var(--primary);
}

.preset-btn.active {
  border-color: var(--primary);
  background: rgba(79, 134, 247, 0.15);
}

.preset-hint {
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 0.15rem;
}

.settings-danger-zone {
  margin-top: 0.5rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--accent);
}

.settings-danger-zone h4 {
  color: var(--accent);
  margin-bottom: 0.5rem;
  font-size: 0.85rem;
}

#settings-status {
  font-size: 0.8rem;
  color: var(--success);
}

#save-settings {
  background: var(--success);
  color: var(--bg);
  font-weight: 600;
}

.modal-content input[type="text"] {
  margin-left: 0.5rem;
  padding: 0.3rem 0.5rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 4px;
}

/* --- Analysis view --- */

#analysis-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  gap: 1rem;
}

/* Game selector */
#game-selector {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.game-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.game-card:hover {
  background: var(--primary);
}

.game-card-unanalyzed {
  opacity: 0.6;
}

.unanalyzed-badge {
  font-size: 0.75rem;
  color: var(--text-dim);
  font-style: italic;
}

.queued-badge {
  font-size: 0.75rem;
  color: var(--primary);
  font-style: italic;
}

.game-card-result {
  font-weight: 700;
  font-size: 1.1rem;
  width: 2rem;
  text-align: center;
}

.game-card-result.win { color: var(--success); }
.game-card-result.loss { color: var(--accent); }
.game-card-result.draw { color: var(--text-dim); }

.game-card-info {
  flex: 1;
  min-width: 0;
}

.game-card-opponent {
  font-weight: 600;
  font-size: 0.95rem;
}

.game-card-meta {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.game-card-accuracy {
  text-align: right;
  min-width: 5rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.accuracy-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.3rem;
  flex-wrap: wrap;
}

.accuracy-row.opponent {
  opacity: 0.55;
}

.accuracy-label {
  font-size: 0.7rem;
  color: var(--text-dim);
}

.game-card-accuracy .accuracy-value {
  font-size: 0.9rem;
}

/* Review layout */
#review-game-info {
  text-align: center;
  font-size: 0.95rem;
  padding: 0.25rem 0;
}

#review-game-info a {
  color: var(--success);
  text-decoration: none;
}

#review-game-info a:hover {
  text-decoration: underline;
}

#review-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 900px;
}

#review-board-col {
  display: flex;
  gap: 0;
  align-items: stretch;
  width: min(88vw, 480px);
}

/* Eval bar */
#eval-bar {
  width: 30px;
  position: relative;
  background: #333;
  border-radius: 4px;
  overflow: hidden;
  flex-shrink: 0;
}

#eval-bar-fill {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50%;
  background: #fff;
  transition: height 0.3s ease;
}

#eval-bar-label {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.65rem;
  font-weight: 700;
  z-index: 1;
  color: #888;
  white-space: nowrap;
  writing-mode: vertical-lr;
  text-orientation: mixed;
}

#review-board-container {
  flex: 1;
  aspect-ratio: 1;
  position: relative;
  margin-left: 1.5rem;
}

#review-board {
  width: 100%;
  height: 100%;
  position: relative;
}

/* Review panel (move list + info) */
#review-panel {
  width: min(88vw, 480px);
  max-height: 300px;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 8px;
  padding: 0.5rem;
}

#review-summary {
  display: flex;
  justify-content: space-around;
  padding: 0.5rem 0;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  margin-bottom: 0.5rem;
}

.accuracy-block {
  text-align: center;
}

.accuracy-value {
  font-size: 1.4rem;
  font-weight: 700;
  font-family: monospace;
}

.accuracy-label {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.class-badge {
  font-size: 0.7rem;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-weight: 600;
}

#review-opening {
  font-size: 0.8rem;
  color: var(--text-dim);
  padding: 0.25rem 0.5rem;
  text-align: center;
}

/* Move list grid */
#review-moves {
  display: grid;
  grid-template-columns: 2rem 1fr 1fr;
  font-size: 0.85rem;
  gap: 0;
}

.move-number {
  color: var(--text-dim);
  text-align: right;
  padding: 0.2rem 0.3rem;
  font-size: 0.75rem;
}

.move-cell {
  padding: 0.2rem 0.4rem;
  cursor: pointer;
  border-radius: 3px;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.move-cell:hover {
  background: rgba(255,255,255,0.08);
}

.move-cell.active-move {
  background: var(--primary);
}

.move-cell .class-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.move-cell .class-symbol {
  font-size: 0.7rem;
  font-weight: 700;
}

.theory-marker {
  border-left: 2px solid var(--warning);
}

/* PV line */
#review-pv {
  font-family: monospace;
  font-size: 0.8rem;
  color: var(--text-dim);
  padding: 0.5rem;
  border-top: 1px solid rgba(255,255,255,0.1);
  min-height: 1.5rem;
}

/* Score chart + controls footer — aligned with board column */
#review-board-footer {
  width: 100%;
  max-width: 900px;
  margin-top: -0.75rem;
}

#score-chart {
  width: 100%;
  height: 80px;
  cursor: pointer;
}

/* Navigation controls */
#review-controls {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 0.25rem;
}

#review-controls button {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.15);
  padding: 0.4rem 0.7rem;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
}

#review-controls button:hover {
  background: var(--primary);
}

#review-top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

#review-back-btn {
  display: inline-block;
  color: var(--text, #e0e0e0);
  text-decoration: none;
  font-size: 1rem;
  font-weight: 600;
  padding: 0.4rem 0;
  cursor: pointer;
  transition: color 0.15s;
}

#review-back-btn:hover {
  color: var(--primary);
}

/* Desktop: side-by-side layout */
@media (min-width: 768px) {
  #training-view {
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;
    gap: 2rem;
    padding: 2rem;
  }

  #info-panel {
    text-align: left;
    max-width: 360px;
  }

  #review-layout {
    flex-direction: row;
    align-items: flex-start;
  }

  #review-board-col {
    width: min(50vw, 480px);
  }

  #review-panel {
    width: 300px;
    max-height: 500px;
  }

  #review-board-footer {
    width: min(50vw, 480px);
  }

  #review-game-info {
    width: min(50vw, 480px);
  }
}
