:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --accent: #38bdf8;
  --danger: #f87171;
  --success: #4ade80;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --board-light: #f1f5f9;
  --board-dark: #94a3b8;
  --fog-overlay: rgba(15, 23, 42, 0.82);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background: #000;
  color: var(--text);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 12px;
}

.hidden {
  display: none !important;
}

.app {
  width: min(1080px, 100%);
  padding: 32px 24px 48px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  margin: 0 auto;
  align-items: center;
}

header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 600;
  max-width: 520px;
  margin-bottom: 12px;
}



.header-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.brand {
  font-size: clamp(2rem, 8vw, 3rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: center;
}

.turn-indicator {
  align-self: center;
}

.turn-indicator {
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.18);
  color: var(--accent);
  font-size: 0.9rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  gap: 8px;
}

.turn-indicator.waiting {
  background: rgba(148, 163, 184, 0.22);
  color: var(--muted);
}

.turn-indicator.active {
  background: rgba(56, 189, 248, 0.24);
  color: var(--accent);
  animation: pulse-glow 2s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% {
    background: rgba(56, 189, 248, 0.24);
    box-shadow: 0 0 0 0 rgba(56, 189, 248, 0.3);
  }
  50% {
    background: rgba(56, 189, 248, 0.32);
    box-shadow: 0 0 0 6px rgba(56, 189, 248, 0.15);
  }
}

.turn-indicator.victory {
  background: rgba(74, 222, 128, 0.22);
  color: var(--success);
}

.turn-indicator.defeat {
  background: rgba(248, 113, 113, 0.22);
  color: var(--danger);
}

.card {
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.start-card {
  align-items: center;
  gap: 24px;
  text-align: center;
  width: 100%;
  max-width: 420px;
}

#entry form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
}

.start-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
}

.start-or {
  font-size: 0.9rem;
  color: var(--muted);
  text-transform: lowercase;
  text-align: center;
}

.code-entry {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.95rem;
  color: var(--muted);
}

.code-entry input {
  min-width: 220px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  text-align: center;
}

form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

input[type="text"],
input[type="checkbox"] {
  font-size: 1rem;
}

input[type="text"] {
  background: rgba(30, 41, 59, 0.8);
  border: 1px solid rgba(148, 163, 184, 0.35);
  border-radius: 12px;
  padding: 10px 14px;
  color: var(--text);
  outline: none;
  min-width: 200px;
}

input[type="text"]:focus {
  border-color: var(--accent);
}

.actions {
  display: flex;
  gap: 12px;
}

button {
  all: unset;
  padding: 10px 16px;
  border-radius: 12px;
  background: linear-gradient(135deg, #38bdf8, #6366f1);
  color: #f8fafc;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.secondary {
  background: rgba(148, 163, 184, 0.24);
}

button.surrender {
  background: rgba(148, 163, 184, 0.24);
  color: var(--muted);
}

button.surrender:hover:not(:disabled) {
  box-shadow: 0 8px 16px rgba(148, 163, 184, 0.25);
}

button.rematch {
  background: rgba(74, 222, 128, 0.24);
  color: var(--success);
}

button.rematch:hover:not(:disabled) {
  box-shadow: 0 8px 16px rgba(74, 222, 128, 0.25);
}

button.rematch-requested {
  background: rgba(56, 189, 248, 0.24);
  color: var(--accent);
}

button.rematch:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.footer-controls {
  display: flex;
  gap: 12px;
  justify-content: center;
  align-items: center;
}

.rematch-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #4ade80;
  color: #000;
  padding: 20px 30px;
  border-radius: 16px;
  font-weight: 700;
  font-size: 1.2rem;
  box-shadow: 0 10px 40px rgba(74, 222, 128, 0.6);
  z-index: 9999;
  border: 3px solid #22c55e;
  animation: pulse 2s ease-in-out infinite;
  pointer-events: none;
  min-width: 200px;
  text-align: center;
}

.rematch-indicator-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.rematch-icon {
  width: 24px;
  height: 24px;
  animation: spin 2s linear infinite;
  flex-shrink: 0;
}

.rematch-text {
  white-space: nowrap;
}

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

@keyframes pulse {
  0%, 100% { 
    transform: translate(-50%, -50%) scale(1);
    box-shadow: 0 8px 32px rgba(74, 222, 128, 0.3);
  }
  50% { 
    transform: translate(-50%, -50%) scale(1.05);
    box-shadow: 0 12px 40px rgba(74, 222, 128, 0.5);
  }
}

#game {
  position: relative;
}

.captured-pieces {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-top: 16px;
  max-width: 500px;
  margin-left: auto;
  margin-right: auto;
}

.captured-pieces-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  align-items: flex-start;
}

.captured-pieces-section:last-child {
  align-items: flex-end;
}

.captured-pieces-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: flex-start;
  align-content: flex-start;
  min-height: 32px;
}

.captured-pieces-section:last-child .captured-pieces-list {
  justify-content: flex-end;
}

.captured-piece {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(148, 163, 184, 0.24);
  border-radius: 4px;
  padding: 2px;
}

.captured-piece img {
  width: 20px;
  height: 20px;
  pointer-events: none;
  user-select: none;
}

.captured-piece.enemy img {
  filter: invert(1) sepia(0.1) saturate(0.6) brightness(0.6);
}

.captured-piece.own img {
  filter: none;
}

button.small {
  padding: 8px 12px;
  font-size: 0.85rem;
  border-radius: 10px;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button:hover:not(:disabled) {
  box-shadow: 0 10px 20px rgba(56, 189, 248, 0.28);
}

#share-row {
  display: none;
  align-items: center;
  gap: 12px;
  font-size: 0.95rem;
  color: var(--muted);
  margin-top: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

#share-code {
  padding: 6px 12px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.4);
  background: rgba(15, 23, 42, 0.4);
  color: var(--text);
  font-family: "JetBrains Mono", "Fira Code", monospace;
  letter-spacing: 0.08em;
  font-size: 1.1rem;
}

.share-buttons {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 1.5em;
  height: 1.5em;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.18);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#copy-btn {
  background: rgba(148, 163, 184, 0.18);
  color: var(--text);
}

#copy-btn:hover:not(:disabled) {
  box-shadow: 0 6px 12px rgba(148, 163, 184, 0.25);
}

#share-btn {
  background: rgba(56, 189, 248, 0.18);
  color: var(--accent);
}

#share-btn:hover:not(:disabled) {
  box-shadow: 0 6px 12px rgba(56, 189, 248, 0.25);
}

#game {
  display: none;
}

#game.active {
  display: block;
}

#board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-auto-rows: 1fr;
  gap: 2px;
  width: min(95vw, 500px);
  min-width: 280px;
  max-width: 500px;
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  padding: 0;
}

#board:empty::after {
  content: "Waiting for opponent...";
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.95rem;
}

.square {
  position: relative;
  border: none;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  background-clip: padding-box;
  transition: background-color 0.12s ease, box-shadow 0.12s ease;
}

.square:focus-visible {
  outline: 2px solid var(--accent);
}

.square {
  background: rgba(226, 232, 240, 0.92);
  box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.15);
}

.square.fog {
  filter: saturate(0.2) brightness(0.5);
}

.square.selected-target {
  box-shadow: inset 0 0 0 3px rgba(56, 189, 248, 0.65);
}

.square.selected-target:hover {
  box-shadow: inset 0 0 0 4px rgba(56, 189, 248, 0.75);
}

.square.selected {
  box-shadow: inset 0 0 0 3px rgba(56, 189, 248, 0.6), 0 0 12px rgba(56, 189, 248, 0.25);
}

.square.danger {
  box-shadow: inset 0 0 0 3px rgba(248, 113, 113, 0.55);
}

.square.danger:hover {
  box-shadow: inset 0 0 0 3px rgba(248, 113, 113, 0.55);
}

.square.attacked {
  box-shadow: inset 0 0 0 2px rgba(251, 191, 36, 0.55);
}

.square img {
  width: 68%;
  height: auto;
  pointer-events: none;
  user-select: none;
  transition: transform 0.2s ease;
}

.square:hover .piece.self {
  transform: scale(1.1);
}

.piece.enemy {
  filter: invert(1) sepia(0.1) saturate(0.6) brightness(0.6);
}

.piece.self {
  filter: none;
}

.square .ghost {
  position: absolute;
  inset: 16%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.square .ghost img {
  width: 60%;
  filter: grayscale(1) brightness(0.85);
  opacity: 0.2;
}

.form-hidden form {
  display: none;
}

.form-hidden #create-btn {
  display: none;
}

.form-hidden .start-or,
.form-hidden .code-entry,
.form-hidden #share-row {
  display: none;
}

.turn-indicator.hidden {
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 960px) {
  #board {
    margin: 0 auto;
  }
}

@media (max-width: 640px) {
  .app {
    padding: 24px 16px 32px;
  }

  header {
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .header-right {
    width: 100%;
    justify-content: center;
  }

  .start-card {
    max-width: 100%;
  }

  .code-entry input {
    min-width: 180px;
  }

  #board {
    width: min(95vw, 400px);
    min-width: 280px;
  }

  .captured-pieces {
    gap: 16px;
    margin-top: 12px;
  }

  .captured-piece img {
    width: 16px;
    height: 16px;
  }
}

@media (max-width: 420px) {
  .code-entry input {
    min-width: 150px;
  }

  .start-actions {
    gap: 10px;
  }

  button {
    width: 100%;
  }

  .code-entry {
    width: 100%;
  }

  #board {
    width: min(98vw, 350px);
    min-width: 260px;
  }
}

@media (max-width: 320px) {
  .app {
    padding: 16px 8px 24px;
  }

  #board {
    width: min(99vw, 300px);
    min-width: 240px;
  }
}
