/* Chirality Stylesheet */
:root {
  --bg-main: #0a0b0e;
  --bg-panel: #111318;
  --bg-card: #181b22;
  --bg-card-hover: #222630;
  --bg-cell: #0e1117;
  --bg-cell-target: #1e2532;
  
  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-card: rgba(255, 255, 255, 0.14);
  --border-active: #3b82f6;

  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  /* Player Colors */
  --p1-color: #22c55e;
  --p2-color: #ef4444;
  --p3-color: #f97316;
  --p4-color: #3b82f6;

  --p1-faint: rgba(34, 197, 94, 0.25);
  --p2-faint: rgba(239, 68, 68, 0.25);
  --p3-faint: rgba(249, 115, 22, 0.25);
  --p4-faint: rgba(59, 130, 246, 0.25);

  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
}

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

body {
  background-color: var(--bg-main);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.app-container {
  width: 100%;
  max-width: 1080px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Header */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-panel);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 12px 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-section h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.btn-nav-home {
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 6px 12px;
  border: 1px solid var(--border-card);
  border-radius: var(--radius-md);
  transition: all 0.2s;
}

.btn-nav-home:hover {
  background: var(--bg-card);
  color: var(--text-primary);
}

/* Mode Selection Screen */
.view-modes {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-top: 10px;
}

.modes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.mode-card {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 24px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 16px;
  transition: transform 0.2s, border-color 0.2s;
}

.mode-card:hover {
  transform: translateY(-4px);
  border-color: var(--border-active);
  background: var(--bg-card-hover);
}

.mode-card h2 {
  font-size: 1.25rem;
  margin-bottom: 8px;
}

.mode-card p {
  color: var(--text-secondary);
  font-size: 0.95rem;
  line-height: 1.5;
}

.btn-select-mode {
  background: #2563eb;
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  padding: 10px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  text-align: center;
}

.btn-select-mode:hover {
  background: #3b82f6;
}

/* 4-Player Scoreboard Bar */
.h2h-scoreboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 12px;
}

.player-status-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: transform 0.2s;
}

.player-status-card.p1 { border-top: 3px solid var(--p1-color); }
.player-status-card.p2 { border-top: 3px solid var(--p2-color); }
.player-status-card.p3 { border-top: 3px solid var(--p3-color); }
.player-status-card.p4 { border-top: 3px solid var(--p4-color); }

.player-name {
  font-weight: 700;
  font-size: 0.9rem;
}

.player-score {
  font-size: 1.1rem;
  font-weight: 800;
}

.toggle-outline-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  cursor: pointer;
}

/* Winner Flash Animation */
@keyframes winnerFlash {
  0%, 100% { transform: scale(1); box-shadow: none; }
  50% { transform: scale(1.04); box-shadow: 0 0 16px rgba(255, 255, 255, 0.8), 0 0 8px var(--p1-color); }
}

.winner-flash {
  animation: winnerFlash 0.4s ease-in-out 3;
}

/* Game Area Layout */
.game-workspace {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

@media (max-width: 820px) {
  .game-workspace {
    grid-template-columns: 1fr;
  }
}

.board-container {
  background: var(--bg-panel);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.top-bar-info {
  display: flex;
  justify-content: space-between;
  width: 100%;
  align-items: center;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* 5x5 Grid Board */
.grid-5x5 {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  width: 100%;
  max-width: 320px;
  aspect-ratio: 1 / 1;
  background: #06070a;
  padding: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}

.grid-cell {
  background-color: var(--bg-cell);
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: all 0.15s;
}

/* Target silhouette cell */
.grid-cell.cell-target {
  background-color: var(--bg-cell-target);
  border: 1px dashed rgba(255, 255, 255, 0.2);
}

/* Active Player Placed Piece */
.grid-cell.cell-active {
  background-color: var(--p1-color) !important;
  box-shadow: inset 0 0 0 1px #fff;
}

/* Opponent Faint Outline Overlays */
.grid-cell.ghost-p2 {
  box-shadow: inset 0 0 0 3px var(--p2-color);
  background-color: var(--p2-faint);
}

.grid-cell.ghost-p3 {
  box-shadow: inset 0 0 0 3px var(--p3-color);
  background-color: var(--p3-faint);
}

.grid-cell.ghost-p4 {
  box-shadow: inset 0 0 0 3px var(--p4-color);
  background-color: var(--p4-faint);
}

/* Multi-ghost blend */
.grid-cell.ghost-p2.ghost-p3 {
  box-shadow: inset 0 0 0 2px var(--p2-color), inset 0 0 0 4px var(--p3-color);
}

/* Piece Tray & Actions */
.tray-panel {
  background: var(--bg-panel);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.piece-tray {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.tray-item {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-md);
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.15s;
}

.tray-item:hover {
  border-color: rgba(255, 255, 255, 0.4);
}

.tray-item.selected {
  border-color: var(--border-active);
  background: #1e293b;
  transform: translateY(-2px);
}

.tray-item.placed {
  opacity: 0.4;
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  width: 45px;
  height: 45px;
}

.mini-cell {
  background: #111;
  border-radius: 1px;
}

.mini-cell.filled {
  background: #38bdf8;
}

/* Action Button Bar */
.action-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.btn {
  background: var(--bg-card);
  color: var(--text-primary);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-md);
  padding: 8px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover {
  background: var(--bg-card-hover);
  border-color: rgba(255, 255, 255, 0.3);
}

.btn-primary {
  background: #2563eb;
  border-color: #3b82f6;
}

.btn-primary:hover {
  background: #3b82f6;
}

.btn-success {
  background: #16a34a;
  border-color: #22c55e;
}

.btn-success:hover {
  background: #22c55e;
}

/* Win Stack / Blackout View */
.view-winstack {
  background: var(--bg-panel);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.winstack-cards-list {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.won-card-item {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-md);
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-backdrop.active {
  display: flex;
}

.modal-content {
  background: var(--bg-panel);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-lg);
  padding: 24px;
  max-width: 440px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* Mobile Responsiveness */
@media (max-width: 480px) {
  body {
    padding: 8px;
  }
  .h2h-scoreboard {
    grid-template-columns: repeat(2, 1fr);
  }
  .piece-tray {
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
  }
  .tray-item {
    padding: 4px;
  }
  .mini-grid {
    width: 36px;
    height: 36px;
  }
}
