/*
  Chess Coach — Mobile-first UI (chess.com Game Review inspired)
  
  Design philosophy:
  - Board is HERO (~60% of mobile viewport)
  - Coach bubble for feedback (one message at a time)
  - Horizontal move strip
  - Bottom action bar (4 buttons max)
  - Everything else tucked away in settings
*/

:root {
  /* Board colors */
  --light: #f0d9b5;
  --dark: #b58863;

  /* Theme */
  --bg: #1a1a2e;
  --bg-gradient: radial-gradient(ellipse at 50% 0%, #252547 0%, #1a1a2e 70%);
  --surface: rgba(30, 30, 50, 0.95);
  --surface-elevated: rgba(40, 40, 65, 0.98);
  --border: rgba(255, 255, 255, 0.08);
  --text: #f0f0f5;
  --text-muted: rgba(240, 240, 245, 0.6);

  /* Highlights */
  --last-from: rgba(90, 180, 255, 0.45);
  --last-to: rgba(90, 255, 160, 0.45);
  --valid: rgba(255, 214, 90, 0.4);
  --selected: rgba(255, 255, 255, 0.25);
  --best-move: rgba(90, 255, 160, 0.5);

  /* Move quality colors */
  --book: #9b7bff;
  --excellent: #5aff9e;
  --good: #5ab4ff;
  --inaccuracy: #ffd65a;
  --mistake: #ff8c5a;
  --blunder: #ff5a78;

  /* Sizing */
  --tap: 48px;
  --radius: 14px;
  --radius-sm: 10px;
  --header-h: 52px;
  --action-bar-h: 72px;
  --move-strip-h: 48px;
  --coach-bubble-h: 80px;

  /* Safe area insets for notched devices */
  --sai-top: env(safe-area-inset-top, 0px);
  --sai-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg);
  background-image: var(--bg-gradient);
  color: var(--text);
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ===== HEADER ===== */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: calc(var(--header-h) + var(--sai-top));
  padding-top: var(--sai-top);
  padding-inline: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(to bottom, rgba(26, 26, 46, 0.98), rgba(26, 26, 46, 0.85));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 100;
}

.header-title {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.elo-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  background: rgba(90, 255, 158, 0.15);
  border: 1px solid rgba(90, 255, 158, 0.3);
  border-radius: 999px;
  color: var(--excellent);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.elo-badge:hover {
  background: rgba(90, 255, 158, 0.25);
}

.elo-badge:active {
  transform: scale(0.95);
}

.elo-badge[data-mode="free"] {
  background: rgba(90, 180, 255, 0.15);
  border-color: rgba(90, 180, 255, 0.3);
  color: var(--good);
}

.icon-btn {
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.15);
}

/* ===== MAIN LAYOUT ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding-top: calc(var(--header-h) + var(--sai-top));
  padding-bottom: var(--sai-bottom);
  min-height: 0;
  overflow: hidden;
}

/* ===== BOARD SECTION ===== */
.board-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  min-height: 0;
  position: relative;
}

.board {
  --board-size: min(calc(100vw - 16px), calc(100svh - var(--header-h) - var(--sai-top) - var(--coach-bubble-h) - var(--move-strip-h) - var(--action-bar-h) - var(--sai-bottom) - 32px), 560px);
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  touch-action: none;
  user-select: none;
  position: relative;
}

.board.thinking {
  pointer-events: none;
}

.board.thinking::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(26, 26, 46, 0.4);
  backdrop-filter: blur(2px);
  z-index: 10;
}

.board.thinking::before {
  content: '';
  position: absolute;
  width: 48px;
  height: 48px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--excellent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  z-index: 11;
}

@keyframes spin {
  to { transform: translate(-50%, -50%) rotate(360deg); }
}

/* Thinking indicator in coach bubble */
.coach-bubble.thinking .coach-avatar {
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.8; }
}

/* Arrow overlay */
.arrow-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.arrow-line {
  stroke: var(--excellent);
  stroke-width: 0.15;
  stroke-linecap: round;
  marker-end: url(#arrowhead);
  opacity: 0.9;
}

/* ===== SQUARES ===== */
.square {
  position: relative;
  display: grid;
  place-items: center;
  font-size: clamp(28px, 9vw, 52px);
}

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

.square::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.square.selected::after { background: var(--selected); }
.square.last-from::after { background: var(--last-from); }
.square.last-to::after { background: var(--last-to); }
.square.valid::after { background: var(--valid); }
.square.best-from::after,
.square.best-to::after { background: var(--best-move); }

/* Valid move dot indicator */
.square.valid:not(:has(.piece))::before {
  content: '';
  position: absolute;
  width: 26%;
  height: 26%;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 50%;
}

/* Capture ring for valid captures */
.square.valid:has(.piece)::before {
  content: '';
  position: absolute;
  inset: 4%;
  border: 4px solid rgba(0, 0, 0, 0.2);
  border-radius: 50%;
}

.piece {
  line-height: 1;
  cursor: grab;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.3));
  transition: transform 0.1s ease;
  z-index: 2;
}

.piece:active {
  cursor: grabbing;
  transform: scale(1.05);
}

.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  font-size: clamp(36px, 12vw, 64px);
  filter: drop-shadow(0 12px 20px rgba(0, 0, 0, 0.5));
  transform: translate(-50%, -50%);
}

/* ===== COACH BUBBLE ===== */
.coach-bubble {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  margin: 0 8px;
  min-height: var(--coach-bubble-h);
  /* Smooth transition for content changes */
  transition: opacity 0.15s ease;
}

.coach-avatar {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  font-size: 28px;
  background: var(--surface-elevated);
  border-radius: 50%;
  border: 2px solid var(--border);
}

.coach-content {
  flex: 1;
  min-width: 0;
}

.coach-message {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  border-top-left-radius: 4px;
  padding: 12px 14px;
  position: relative;
  /* Ensure text is readable */
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.coach-message::before {
  content: '';
  position: absolute;
  left: -8px;
  top: 12px;
  width: 0;
  height: 0;
  border: 8px solid transparent;
  border-right-color: var(--surface);
  border-left: 0;
}

.coach-text {
  font-size: 14px;
  line-height: 1.4;
  display: block;
}

.coach-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  flex-wrap: wrap;
}

/* Move quality badges */
.quality-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid;
}

.quality-badge.book {
  background: rgba(155, 123, 255, 0.15);
  border-color: rgba(155, 123, 255, 0.3);
  color: var(--book);
}

.quality-badge.excellent {
  background: rgba(90, 255, 158, 0.15);
  border-color: rgba(90, 255, 158, 0.3);
  color: var(--excellent);
}

.quality-badge.good {
  background: rgba(90, 180, 255, 0.15);
  border-color: rgba(90, 180, 255, 0.3);
  color: var(--good);
}

.quality-badge.inaccuracy {
  background: rgba(255, 214, 90, 0.15);
  border-color: rgba(255, 214, 90, 0.3);
  color: var(--inaccuracy);
}

.quality-badge.mistake {
  background: rgba(255, 140, 90, 0.15);
  border-color: rgba(255, 140, 90, 0.3);
  color: var(--mistake);
}

.quality-badge.blunder {
  background: rgba(255, 90, 120, 0.15);
  border-color: rgba(255, 90, 120, 0.3);
  color: var(--blunder);
}

.eval-badge {
  font-size: 12px;
  font-weight: 600;
  font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, monospace;
  color: var(--text-muted);
}

/* ===== MOVE STRIP ===== */
.move-strip-wrap {
  height: var(--move-strip-h);
  padding: 0 8px;
}

.move-strip {
  height: 100%;
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 8px;
  margin: 0;
  list-style: none;
}

.move-strip::-webkit-scrollbar {
  display: none;
}

.move-strip li {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.move-strip-placeholder {
  color: var(--text-muted);
  font-size: 13px;
  white-space: nowrap;
}

.move-number {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  padding: 0 2px;
}

.move-btn {
  flex-shrink: 0;
  padding: 8px 12px;
  font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, monospace;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, transform 0.1s;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 36px;
  /* Better touch target */
  min-width: 44px;
  justify-content: center;
}

.move-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.move-btn:active {
  transform: scale(0.96);
}

.move-btn[aria-current="true"] {
  background: rgba(90, 180, 255, 0.3);
  box-shadow: 0 0 0 2px rgba(90, 180, 255, 0.4);
}

/* Move quality indicators in strip */
.move-btn .move-quality {
  font-size: 11px;
}

.move-btn .move-quality.book { color: var(--book); }
.move-btn .move-quality.excellent { color: var(--excellent); }
.move-btn .move-quality.good { color: var(--good); }
.move-btn .move-quality.inaccuracy { color: var(--inaccuracy); }
.move-btn .move-quality.mistake { color: var(--mistake); }
.move-btn .move-quality.blunder { color: var(--blunder); }

/* ===== ACTION BAR ===== */
.action-bar {
  height: var(--action-bar-h);
  padding: 8px;
  padding-bottom: calc(8px + var(--sai-bottom));
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  min-height: var(--tap);
}

.action-btn:hover {
  background: var(--surface-elevated);
}

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

.action-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.action-icon {
  font-size: 20px;
  line-height: 1;
}

.action-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}

/* ===== SETTINGS DIALOG ===== */
.settings-dialog,
.pgn-dialog {
  position: fixed;
  inset: 0;
  width: 100%;
  max-width: 400px;
  height: fit-content;
  max-height: calc(100svh - 48px);
  margin: auto;
  padding: 0;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  overflow: hidden;
}

.settings-dialog::backdrop,
.pgn-dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.settings-form,
.pgn-form {
  display: flex;
  flex-direction: column;
  max-height: calc(100svh - 48px);
}

.settings-header,
.pgn-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.settings-title,
.pgn-title {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
}

.settings-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

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

.setting-label {
  font-size: 14px;
  font-weight: 500;
}

.setting-select {
  padding: 8px 12px;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

.toggle-input {
  width: 44px;
  height: 24px;
  appearance: none;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
}

.toggle-input::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: transform 0.2s;
}

.toggle-input:checked {
  background: var(--excellent);
}

.toggle-input:checked::before {
  transform: translateX(20px);
}

.setting-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.setting-btn {
  flex: 1;
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
}

.setting-btn:hover {
  background: rgba(255, 255, 255, 0.15);
}

.setting-btn.danger {
  background: rgba(255, 90, 120, 0.15);
  border-color: rgba(255, 90, 120, 0.3);
  color: var(--blunder);
}

.setting-btn.danger:hover {
  background: rgba(255, 90, 120, 0.25);
}

/* ===== PGN DIALOG ===== */
.pgn-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.pgn-status {
  font-size: 13px;
  color: var(--text-muted);
  margin-left: auto;
}

.pgn-text {
  width: 100%;
  min-height: 200px;
  padding: 12px 16px;
  font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, monospace;
  font-size: 13px;
  line-height: 1.5;
  background: rgba(0, 0, 0, 0.2);
  border: none;
  color: var(--text);
  resize: vertical;
}

/* ===== OPENING BADGE ===== */
.opening-badge {
  position: fixed;
  top: calc(var(--header-h) + var(--sai-top) + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 50;
  animation: fadeInOut 3s ease-in-out forwards;
}

.opening-badge[hidden] {
  display: none;
}

.opening-eco {
  color: var(--text-muted);
}

@keyframes fadeInOut {
  0% { opacity: 0; transform: translateX(-50%) translateY(-10px); }
  10% { opacity: 1; transform: translateX(-50%) translateY(0); }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

/* ===== RESPONSIVE: TABLET ===== */
@media (min-width: 600px) {
  :root {
    --header-h: 56px;
    --action-bar-h: 80px;
    --move-strip-h: 52px;
    --coach-bubble-h: 88px;
  }

  .header {
    padding-inline: 24px;
  }

  .header-title {
    font-size: 20px;
  }

  .board-section {
    padding: 16px;
  }

  .coach-bubble {
    padding: 12px 24px;
    gap: 14px;
  }

  .coach-avatar {
    width: 52px;
    height: 52px;
    font-size: 32px;
  }

  .coach-text {
    font-size: 15px;
  }

  .move-strip-wrap {
    padding: 0 24px;
  }

  .action-bar {
    padding: 12px 24px;
    gap: 12px;
  }

  .action-btn {
    flex-direction: row;
    gap: 8px;
    padding: 0 20px;
  }

  .action-icon {
    font-size: 22px;
  }

  .action-label {
    font-size: 13px;
  }
}

/* ===== RESPONSIVE: DESKTOP ===== */
@media (min-width: 900px) {
  .main {
    max-width: 700px;
    margin: 0 auto;
  }

  .board {
    --board-size: min(520px, calc(100svh - var(--header-h) - var(--sai-top) - var(--coach-bubble-h) - var(--move-strip-h) - var(--action-bar-h) - var(--sai-bottom) - 48px));
  }

  .settings-dialog,
  .pgn-dialog {
    max-width: 480px;
  }
}

/* ===== ACCESSIBILITY ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Focus styles */
:focus-visible {
  outline: 2px solid var(--excellent);
  outline-offset: 2px;
}

button:focus-visible {
  outline: 2px solid var(--excellent);
  outline-offset: 2px;
}
