:root {
  --bg: #1a1a2e;
  --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;
}

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

#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;
}

.nav-submenu-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-arrow {
  display: inline-block;
  transition: transform 0.2s;
}

.nav-item.expanded .nav-arrow {
  transform: rotate(90deg);
}

.nav-submenu {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-submenu-header {
  padding: 0.5rem 1rem 0.15rem 1.5rem;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: default;
}

.nav-coming-soon-item {
  padding: 0.5rem 1rem 0.5rem 2rem;
  color: var(--text-dim);
  font-style: italic;
  opacity: 0.5;
  cursor: default;
}

main {
  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;
  width: 1.5rem !important;
}

.cg-wrap coords.files {
  bottom: -1.5rem !important;
  height: 1.5rem !important;
}

.cg-wrap coords coord {
  font-weight: 600 !important;
  color: var(--text-dim) !important;
  font-size: 0.8rem !important;
  text-shadow: none !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;
}

#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;
}

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

#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);
}

.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;
}

.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-interrupt {
  background: var(--accent);
  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;
}

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

#stats-content .stats-label {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.raw-data-header {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 0.25rem;
}

.raw-data-warning {
  color: #e8a838;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.raw-data-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.75rem;
  font-size: 0.85rem;
}

.raw-data-table th {
  text-align: left;
  color: var(--text-dim);
  font-weight: normal;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.25rem 0.5rem;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.raw-data-table td {
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}

.raw-data-table td:nth-child(n+2) {
  text-align: center;
}

.raw-data-table th:nth-child(n+2) {
  text-align: center;
}

.raw-data-table a {
  color: var(--accent);
  text-decoration: none;
}

.raw-data-table a:hover {
  text-decoration: underline;
}

#stats-content .stats-value {
  font-weight: 600;
}

#validate-content {
  line-height: 1.6;
  max-height: 60vh;
  overflow-y: auto;
}

.validate-file {
  margin-bottom: 0.75rem;
  font-weight: 600;
}

.validate-chapter {
  padding-left: 1rem;
  margin-bottom: 0.25rem;
}

.validate-ok { color: var(--success); }
.validate-warn { color: #f0ad4e; }
.validate-error { color: var(--accent); }

.validate-detail {
  padding-left: 2rem;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.status-section {
  margin-bottom: 0.75rem;
}

.status-section-title {
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.status-ok { color: var(--success); }
.status-warn { color: #f0ad4e; }

.status-file {
  padding-left: 1rem;
}

.refresh-step {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

.step-icon {
  width: 1.2rem;
  text-align: center;
  font-weight: bold;
  flex-shrink: 0;
}

.step-done .step-icon { color: var(--success); }
.step-active .step-icon { color: var(--accent); }
.step-pending .step-icon { color: var(--text-dim); }
.step-error .step-icon { color: var(--accent); }

.refresh-summary {
  margin-top: 0.75rem;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.step-progress {
  width: 100%;
  height: 0.8rem;
  margin-top: 0.25rem;
  border-radius: 4px;
  appearance: none;
}

.step-progress::-webkit-progress-bar {
  background: var(--bg);
  border-radius: 4px;
}

.step-progress::-webkit-progress-value {
  background: var(--success);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.step-progress::-moz-progress-bar {
  background: var(--success);
  border-radius: 4px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.step-active .step-icon {
  display: inline-block;
  animation: spin 1s linear infinite;
}

#journal-content {
  line-height: 1.6;
  max-height: 60vh;
  overflow-y: auto;
}

.journal-topic {
  padding: 0.5rem 0;
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}

.journal-topic:hover {
  color: var(--success);
}

.journal-topic-date {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.journal-topic-status {
  font-size: 0.75rem;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  margin-left: 0.5rem;
}

.journal-topic-status.resolved {
  color: var(--success);
  border: 1px solid var(--success);
}

.journal-topic-status.active {
  color: var(--warning);
  border: 1px solid var(--warning);
}

.journal-detail {
  white-space: pre-wrap;
  font-family: inherit;
  line-height: 1.6;
}

.journal-detail h2 {
  font-size: 1rem;
  margin: 0.75rem 0 0.25rem;
  color: var(--success);
}

.journal-detail p {
  margin-bottom: 0.5rem;
}

#journal-back {
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid rgba(255,255,255,0.2);
}

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

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

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

#save-config {
  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;
}

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

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