/* ── API Key Masking (avoids Chrome password save prompt) ── */
.api-key-mask {
  -webkit-text-security: disc;
}

/* ── Analysis Page Layout ────────────────────────── */
.analysis-main {
  padding: 5rem 2rem 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

/* ── Input Section ──────────────────────────────── */
.analysis-input-section {
  max-width: 600px;
  margin: 0 auto 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.analysis-fen-row {
  display: flex;
  gap: 0.5rem;
}

.analysis-fen-input {
  flex: 1;
  font-size: 0.75rem;
}

.input-hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 0.3rem;
  font-style: italic;
}

.input-hint:empty {
  display: none;
}

.analysis-error {
  color: var(--red);
  font-size: 0.8rem;
  padding: 0.6rem;
  border: 1px solid var(--red-dim);
  background: var(--red-faint);
  border-radius: 4px;
}

/* ── Result Layout ──────────────────────────────── */
.analysis-layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2rem;
  align-items: start;
}

.analysis-board-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: sticky;
  top: 5rem;
}

.analysis-board-wrap {
  display: flex;
  gap: 0;
}

.analysis-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: min(400px, calc(100vw - 4rem));
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 3px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 0 30px var(--red-faint);
}

.analysis-board .sq {
  display: flex;
  align-items: center;
  justify-content: center;
}

.analysis-board .sq.light { background: var(--sq-light); }
.analysis-board .sq.dark { background: var(--sq-dark); }

.analysis-board .sq .piece-img {
  width: 85%;
  height: 85%;
  object-fit: contain;
  user-select: none;
  pointer-events: none;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.5));
}

.analysis-board .sq .piece-img.black-piece {
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.4));
}

.analysis-eval-bar {
  width: 20px;
  align-self: stretch;
  background: #1a1a1a;
  border: 1px solid var(--border);
  border-radius: 3px 0 0 3px;
  overflow: hidden;
  position: relative;
}

.analysis-eval-bar .eval-bar-fill {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50%;
  background: #d4d0c8;
  transition: height 0.4s ease;
}

.analysis-board-info {
  margin-top: 0.6rem;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
}

.analysis-actions {
  margin-top: 0.75rem;
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

/* ── Analysis Card ──────────────────────────────── */
.analysis-text-col {
  min-width: 0;
}

.analysis-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.analysis-card-header {
  padding: 0.6rem 1rem;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
}

.analysis-corpus-info {
  font-weight: 400;
  color: var(--text-muted);
  margin-left: 0.5rem;
  font-size: 0.7rem;
}

.analysis-status {
  font-weight: 400;
  color: var(--red);
  font-style: italic;
  margin-left: 0.5rem;
  font-size: 0.7rem;
  animation: pulse 1.5s ease-in-out infinite;
}

/* Phased progress bar shown during analysis.
   Fills through Stockfish → retrieval → LLM streaming phases.
   Not a literal time-remaining estimate — we have no ETA for the
   LLM — but advances predictably so the user sees motion. */
.analysis-progress-bar {
  position: relative;
  height: 2px;
  margin: 0;
  background: rgba(255, 255, 255, 0.04);
  overflow: hidden;
}

.analysis-progress-bar.hidden {
  display: none;
}

.analysis-progress-fill {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 0;
  background: var(--red);
  transition: width 0.25s ease-out;
}

.analysis-card-body {
  padding: 1.2rem 1.5rem;
  font-size: 0.85rem;
  line-height: 1.8;
  color: var(--text-dim);
  white-space: pre-wrap;
  max-height: 70vh;
  overflow-y: auto;
}

/* ── Loading ────────────────────────────────────── */
.analysis-loading {
  text-align: center;
  padding: 4rem 2rem;
}

.analysis-loading-text {
  font-size: 1rem;
  color: var(--text);
  margin-bottom: 0.5rem;
  animation: pulse 1.5s ease-in-out infinite;
}

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

.analysis-loading-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ── Board Editor ────────────────────────────────── */
.board-editor {
  margin-top: 1rem;
  padding: 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.editor-piece-palette {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
  align-items: center;
}

.palette-group {
  display: flex;
  gap: 0.25rem;
}

.palette-piece {
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  max-width: 36px;
  max-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  border: 2px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  padding: 2px;
  font-size: 1rem;
  color: var(--text-dim);
  transition: border-color 0.15s, transform 0.1s;
  overflow: hidden;
}

.palette-piece:hover {
  border-color: var(--text-dim);
  transform: scale(1.05);
}

.palette-piece.active {
  border-color: var(--red);
  box-shadow: 0 0 8px var(--red-glow);
}

.palette-piece img {
  width: 28px;
  height: 28px;
  max-width: 28px;
  max-height: 28px;
  object-fit: contain;
  pointer-events: none;
}

.editor-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: min(400px, calc(100vw - 6rem));
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.editor-board .sq {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: outline 0.1s;
}

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

.editor-board .sq:hover {
  outline: 2px solid var(--red);
  outline-offset: -2px;
  z-index: 1;
}

.editor-board .sq.selected {
  box-shadow: inset 0 0 0 3px rgba(255, 200, 0, 0.7);
  z-index: 1;
}

.editor-board .sq .piece-img {
  width: 85%;
  height: 85%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.5));
}

.editor-board .sq .piece-img.black-piece {
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.4));
}

.editor-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
  gap: 1rem;
  flex-wrap: wrap;
}

.editor-turn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.editor-turn .input-label {
  margin: 0;
  font-size: 0.75rem;
}

.turn-toggle {
  display: flex;
  gap: 0;
}

.turn-btn {
  padding: 0.3rem 0.75rem;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.75rem;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-dim);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.turn-btn:first-child { border-radius: 4px 0 0 4px; }
.turn-btn:last-child { border-radius: 0 4px 4px 0; border-left: none; }

.turn-btn.active {
  background: var(--red);
  color: #fff;
  border-color: var(--red);
}

.editor-actions {
  display: flex;
  gap: 0.5rem;
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.7rem;
}

/* ── Responsive ──────────────────────────────────── */
@media (max-width: 1100px) {
  .analysis-layout {
    grid-template-columns: 1fr;
  }

  /* Keep the board column sticky on mobile too (the base rule
     already sets `position: sticky; top: 5rem`). When a long
     analysis comment scrolls, the board + eval + action buttons
     stay pinned below the nav. Particularly useful when the user
     taps a chess-notation reference: the arrow/highlight on the
     board is immediately visible instead of requiring a scroll-up.
     Opaque background prevents text scrolling past from showing
     through the empty areas of the column. */
  .analysis-board-col {
    background: var(--bg);
    padding-bottom: 0.5rem;
    z-index: 5;
  }

  /* Keep the 360px cap for the 600–1100px range (tablets, narrow
     laptop windows) — there's plenty of viewport to show the
     standard board size. Phones (the real problem case for sticky
     sizing) shrink further in the 600px media query below. */
  .analysis-board {
    width: min(360px, calc(100vw - 4rem));
  }
}

@media (max-width: 600px) {
  .analysis-main {
    padding: 5rem 1rem 1rem;
  }

  /* Phones specifically: shrink the sticky board so there's
     reading room for the scrolling comment below. ~280px is still
     large enough to see piece details but leaves ~200–250px of
     comment text visible beneath the pinned column on a typical
     phone viewport. */
  .analysis-board {
    width: min(calc(100vw - 3rem), 280px);
  }

  .analysis-fen-row {
    flex-direction: column;
  }

  .analysis-card-body {
    padding: 0.8rem 1rem;
    font-size: 0.8rem;
  }
}
