/* ==================== SHOGI GAME STYLES ==================== */

.shogi-game {
  --koma-face: #f3d9a4;
  --koma-face-hi: #fbeccb;
  --koma-edge: #7a5322;
  --koma-ink: #2b1a08;
  --koma-ink-promoted: #b3261e;
  --board-wood: #e3b96b;
  --board-line: #8a6023;

  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  margin: 0 auto;
  max-width: 100%;
}

.shogi-status {
  min-height: 1.5rem;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
}

/* ==================== LAYOUT ==================== */

/* Portrait / mobile: opponent komadai, board, player komadai stacked. */
.shogi-layout {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "opp"
    "board"
    "you";
  gap: 0.5rem;
  width: 100%;
  max-width: 30rem;
}

.komadai-opponent { grid-area: opp; }
.shogi-board-wrapper { grid-area: board; }
.komadai-player { grid-area: you; }

/* Landscape / desktop: komadai flank the board. */
@media (orientation: landscape) and (min-width: 48rem) {
  .shogi-layout {
    grid-template-columns: minmax(4.5rem, 7rem) minmax(0, 1fr) minmax(4.5rem, 7rem);
    grid-template-areas: "opp board you";
    align-items: center;
    max-width: 46rem;
  }

  .komadai-pieces {
    /* Vertical stands read better beside the board. */
    flex-direction: column;
    align-content: flex-start;
  }
}

/* ==================== BOARD ==================== */

.shogi-board-wrapper {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 1.25rem;
  grid-template-rows: 1.25rem minmax(0, 1fr);
  grid-template-areas:
    "files corner"
    "board ranks";
  gap: 0.15rem;
  padding: 0.5rem;
  background: var(--bg-card);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 8px 24px var(--shadow);
}

.shogi-files,
.shogi-ranks {
  display: grid;
  font-size: 0.7rem;
  color: var(--text-muted);
  line-height: 1;
}

.shogi-files {
  grid-area: files;
  grid-template-columns: repeat(9, 1fr);
  align-items: center;
  justify-items: center;
}

.shogi-ranks {
  grid-area: ranks;
  grid-template-rows: repeat(9, 1fr);
  align-items: center;
  justify-items: center;
}

.shogi-board {
  grid-area: board;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1;
  background: var(--board-wood);
  border: 2px solid var(--board-line);
  border-radius: 3px;
  overflow: hidden;
}

.shogi-square {
  position: relative;
  aspect-ratio: 1;
  border-right: 1px solid var(--board-line);
  border-bottom: 1px solid var(--board-line);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* The board's own border supplies the outer edges. */
.shogi-square:nth-child(9n) { border-right: none; }
.shogi-square:nth-child(n + 73) { border-bottom: none; }

.shogi-square.selected {
  background: rgba(251, 191, 36, 0.55);
}

.shogi-square.last-move {
  background: rgba(168, 85, 247, 0.28);
}

/* Hint overlays — a dot for a quiet move, a ring for a capture. */
.shogi-square.legal-move::after,
.shogi-square.legal-capture::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  pointer-events: none;
}

.shogi-square.legal-move::after {
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(16, 185, 129, 0.7);
}

.shogi-square.legal-capture::after {
  width: 86%;
  height: 86%;
  border-radius: 50%;
  border: 3px solid rgba(236, 72, 153, 0.85);
}

.shogi-square.in-check {
  background: rgba(239, 68, 68, 0.45);
}

/* ==================== PIECES ==================== */

.shogi-piece {
  width: 92%;
  height: 92%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease;
  transform-origin: center;
}

/* Gote's pieces face the other way. */
.shogi-piece.gote { transform: rotate(180deg); }

.shogi-piece-svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.koma-body {
  fill: var(--koma-face);
  stroke: var(--koma-edge);
  stroke-width: 3;
  stroke-linejoin: round;
}

.koma-kanji {
  fill: var(--koma-ink);
  font-family: 'Yu Mincho', 'Hiragino Mincho ProN', 'MS Mincho', serif;
  font-weight: 700;
  user-select: none;
}

.koma-kanji.promoted { fill: var(--koma-ink-promoted); }

.koma-arrows line {
  stroke: rgba(43, 26, 8, 0.55);
  stroke-width: 3;
  stroke-linecap: round;
}

.koma-arrows polygon {
  fill: rgba(43, 26, 8, 0.55);
}

/* Press and hold: enlarge the koma and drop the arrows so the bare kanji
   is readable. Gote keeps its 180° rotation while scaling. */
.shogi-piece.pressing {
  transform: scale(1.6);
  z-index: 20;
}

.shogi-piece.gote.pressing {
  transform: rotate(180deg) scale(1.6);
}

.shogi-piece.pressing .koma-arrows { opacity: 0; }
.shogi-piece.pressing .koma-body { fill: var(--koma-face-hi); }

/* ==================== KOMADAI (piece stands) ==================== */

.komadai {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.4rem 0.5rem;
  min-height: 3.25rem;
}

.komadai-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.komadai-pieces {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.komadai-piece {
  position: relative;
  width: 2.25rem;
  height: 2.5rem;
  cursor: pointer;
  border-radius: 4px;
}

.komadai-piece.selected {
  background: rgba(251, 191, 36, 0.4);
  box-shadow: 0 0 0 2px var(--accent-yellow);
}

.komadai-count {
  position: absolute;
  right: -0.15rem;
  bottom: -0.15rem;
  min-width: 1rem;
  padding: 0 0.2rem;
  border-radius: 999px;
  background: var(--accent-purple);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 1rem;
  text-align: center;
}

/* Note: captured pieces in a komadai belong to that stand's owner, so the
   base .sente / .gote rules already orient them correctly. No override here. */

/* ==================== CONTROLS ==================== */

.shogi-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.shogi-toggle {
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.shogi-toggle:hover { background: var(--bg-card-hover); }

.shogi-toggle.on {
  background: var(--accent-purple);
  border-color: var(--accent-purple);
  color: #fff;
}

/* ==================== PROMOTION PROMPT ==================== */

.shogi-promo-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 15, 0.65);
  backdrop-filter: blur(3px);
}

.shogi-promo-dialog {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 1.25rem 1.5rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  text-align: center;
  animation: shogi-promo-in 0.15s ease-out;
}

@keyframes shogi-promo-in {
  from { opacity: 0; transform: translateY(8px) scale(0.97); }
  to   { opacity: 1; transform: none; }
}

.shogi-promo-title {
  color: var(--text-primary);
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 0.9rem;
}

.shogi-promo-actions {
  display: flex;
  gap: 0.75rem;
}

.shogi-promo-actions button {
  flex: 1;
  padding: 0.55rem 1.1rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.shogi-promo-yes {
  background: var(--accent-purple);
  border-color: var(--accent-purple);
  color: #fff;
}

.shogi-promo-no {
  background: var(--bg-secondary);
  color: var(--text-secondary);
}

/* ==================== TUTORIAL ==================== */

.tutorial-progress {
  width: 100%;
  max-width: 30rem;
}

.tutorial-progress-bar {
  height: 6px;
  border-radius: 999px;
  background: var(--bg-secondary);
  overflow: hidden;
}

.tutorial-progress-bar span {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent-purple), var(--accent-magenta));
  transition: width 0.25s ease;
}

.tutorial-progress-text {
  margin-top: 0.3rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

.tutorial-panel {
  width: 100%;
  max-width: 30rem;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 0.9rem 1rem;
}

.tutorial-text {
  color: var(--text-primary);
  font-size: 1.05rem;
  line-height: 1.9;
}

.tutorial-prompt {
  margin-top: 0.6rem;
  font-size: 0.85rem;
  color: var(--accent-yellow);
}

.tutorial-feedback {
  margin-top: 0.6rem;
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
}

.tutorial-feedback.success {
  background: rgba(16, 185, 129, 0.15);
  border: 1px solid rgba(16, 185, 129, 0.5);
  color: #34d399;
}

.tutorial-feedback.hint {
  background: rgba(251, 191, 36, 0.13);
  border: 1px solid rgba(251, 191, 36, 0.45);
  color: var(--accent-yellow);
}

/* Kanji the learner can press and hold for a reading + gloss. */
.tutorial-kanji {
  position: relative;
  border-bottom: 2px dotted var(--accent-purple);
  cursor: help;
  padding: 0 1px;
  user-select: none;
}

.tutorial-kanji.revealed {
  background: rgba(168, 85, 247, 0.2);
  border-radius: 3px;
}

.tutorial-popover {
  position: absolute;
  bottom: 125%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  min-width: 6rem;
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  background: rgba(30, 30, 46, 0.96);
  border: 1px solid var(--accent-purple);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(6px);
  text-align: center;
  white-space: nowrap;
  pointer-events: none;
}

.tutorial-popover-reading {
  font-size: 0.85rem;
  color: var(--accent-yellow);
}

.tutorial-popover-en {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.shogi-toggle:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ==================== SPECTATOR ==================== */

.shogi-game.spectator .shogi-square,
.shogi-game.spectator .komadai-piece {
  cursor: default;
}

.shogi-spectator-note {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
}

/* A newly placed piece settles in, so a remote move is easy to spot. */
@keyframes shogi-place {
  from { opacity: 0; transform: scale(0.82); }
  to   { opacity: 1; transform: none; }
}

.shogi-square.last-move .shogi-piece.sente {
  animation: shogi-place 0.18s ease-out;
}

.shogi-square.last-move .shogi-piece.gote {
  /* Keep Gote's 180 rotation while it animates in. */
  animation: shogi-place-gote 0.18s ease-out;
}

@keyframes shogi-place-gote {
  from { opacity: 0; transform: rotate(180deg) scale(0.82); }
  to   { opacity: 1; transform: rotate(180deg); }
}

@media (prefers-reduced-motion: reduce) {
  .shogi-promo-dialog { animation: none; }
  .shogi-piece { transition: none; }
  .tutorial-progress-bar span { transition: none; }
  .shogi-square.last-move .shogi-piece.sente,
  .shogi-square.last-move .shogi-piece.gote { animation: none; }
}
