/* ==========================================================================
   4 en Raya — Styles
   Shares the same visual language (topbar/controls/panels/overlay) as the
   Memory game, adapted for a two-player board instead of a card grid.
   ========================================================================== */

:root {
  --bg-base: #140f2b;
  /* Left half = Player 1's theme colours, right half = Player 2's — set in
     JS via applyPlayerBackgrounds() the moment each player picks a theme.
     These are just the pre-pick fallback (blue vs red, matching the ring
     colours below). */
  --p1-c1: 59, 130, 246;
  --p1-c2: 29, 78, 216;
  --p1-c3: 59, 130, 246;
  --p2-c1: 239, 68, 68;
  --p2-c2: 185, 28, 28;
  --p2-c3: 239, 68, 68;
  --panel: rgba(255, 255, 255, 0.08);
  --panel-solid: #241f3d;
  --text: #f5f3ff;
  --text-dim: #c4b5fd;
  --accent: #fbbf24;
  --accent-2: #34d399;
  --p1-color: #3b82f6;   /* blue — player 1 */
  --p2-color: #ef4444;   /* red — player 2 */
  --board-1: #4338ca;
  --board-2: #6d28d9;
  --radius: 16px;
  --topbar-h: 72px;
  --statsbar-h: 52px;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-base);
  /* Two vertical (top-to-bottom) gradients, each clipped to one half of the
     viewport — left = Player 1's theme colours, right = Player 2's. */
  background-image:
    linear-gradient(to bottom, rgba(var(--p1-c1), 0.6) 0%, rgba(var(--p1-c2), 0.45) 50%, rgba(var(--p1-c3), 0.6) 100%),
    linear-gradient(to bottom, rgba(var(--p2-c1), 0.6) 0%, rgba(var(--p2-c2), 0.45) 50%, rgba(var(--p2-c3), 0.6) 100%);
  background-size: 50% 100%, 50% 100%;
  background-position: left top, right top;
  background-repeat: no-repeat, no-repeat;
  background-attachment: fixed;
  color: var(--text);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------------- */
/* Top bar (shared chrome — same as the Memory game)                      */
/* ---------------------------------------------------------------------- */

.topbar {
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding: 10px 20px 10px 200px;
  background-color: #0c0c12;
  background-image:
    linear-gradient(rgba(8, 6, 16, 0.6), rgba(8, 6, 16, 0.6)),
    url("../../assets/textures/carbon-fiber.jpg");
  background-repeat: no-repeat, repeat;
  background-size: cover, 180px 135px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.brand {
  position: fixed;
  top: 12px;
  left: 18px;
  z-index: 70;
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-link {
  display: block;
  line-height: 0;
  transition: opacity 0.15s ease;
}

.brand-link:hover {
  opacity: 0.85;
}

.brand-logo {
  height: 44px;
  width: auto;
  display: block;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.35));
}

.brand-link-mobile {
  display: none;
}

.brand-home-icon {
  display: block;
  font-size: 26px;
  line-height: 1;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
}

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 14px;
}

.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  padding: 12px 18px;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, filter 0.12s ease;
}

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

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #f59e0b);
  color: #1a1a1a;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-ghost {
  background: var(--panel);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 18px;
  padding: 10px 14px;
}

/* ---------------------------------------------------------------------- */
/* Stats bar                                                               */
/* ---------------------------------------------------------------------- */

.statsbar {
  min-height: var(--statsbar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
  flex-wrap: wrap;
  padding: 6px 16px;
  background: rgba(0, 0, 0, 0.15);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.statsbar.hidden { display: none !important; }

.stat {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.stat-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

.stat-value {
  font-size: 17px;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.stat-value.turn-p1 { color: var(--p1-color); }
.stat-value.turn-p2 { color: var(--p2-color); }

/* ---------------------------------------------------------------------- */
/* Main area / shared panel visibility                                    */
/* ---------------------------------------------------------------------- */

#mainArea {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------------- */
/* Theme picker                                                            */
/* ---------------------------------------------------------------------- */

.theme-picker {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  max-width: 920px;
  width: 100%;
  max-height: 100%;
  overflow-y: auto;
  padding: 8px;
}

.theme-picker h1 {
  margin: 0;
  font-size: clamp(20px, 3.5vw, 30px);
  text-align: center;
}

.theme-picker.turn-p1 h1 { color: var(--p1-color); }
.theme-picker.turn-p2 h1 { color: var(--p2-color); }

.picker-subtitle {
  margin: 0 0 8px;
  color: var(--text-dim);
  font-size: 14px;
  text-align: center;
}

.theme-grid {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.theme-row {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.theme-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 130px;
  padding: 8px;
  border-radius: 14px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  background: var(--panel-solid);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.theme-tile:hover {
  transform: translateY(-3px);
}

.theme-tile.taken {
  opacity: 0.35;
  cursor: not-allowed;
  transform: none;
}

.theme-tile.picked-p1 { border-color: var(--p1-color); box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.35); }
.theme-tile.picked-p2 { border-color: var(--p2-color); box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.35); }

.theme-tile-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.theme-tile-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}

.theme-tile-thumb .css-thumb {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 34px;
}

.theme-tile-label {
  font-size: 13px;
  font-weight: 700;
  text-align: center;
  line-height: 1.2;
}

.theme-tile-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 800;
  color: #0a0814;
}

.theme-tile.picked-p1 .theme-tile-badge { display: flex; background: var(--p1-color); color: #fff; }
.theme-tile.picked-p2 .theme-tile-badge { display: flex; background: var(--p2-color); color: #fff; }

.btn-start {
  margin-top: 10px;
}

/* ---------------------------------------------------------------------- */
/* Game view: preview piece + drop arrows + board                         */
/* ---------------------------------------------------------------------- */

.game-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

/* Same width formula as .drop-row's grid content and .board's grid content
   (board's own padding equals --gap, and centering both boxes in the same
   flex column naturally lines up their columns — no extra offset needed). */
.preview-row {
  position: relative;
  height: calc(var(--cell, 60px) * 0.95);
  width: calc(var(--cols, 7) * var(--cell, 60px) + (var(--cols, 7) - 1) * var(--gap, 8px));
}

.preview-piece {
  position: absolute;
  top: 0;
  width: var(--cell, 60px);
  height: var(--cell, 60px);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
  transition: left 0.18s ease;
  animation: previewBob 1.4s ease-in-out infinite;
}

.preview-piece.p1 { box-shadow: 0 0 0 7px var(--p1-color), 0 4px 10px rgba(0, 0, 0, 0.5); }
.preview-piece.p2 { box-shadow: 0 0 0 7px var(--p2-color), 0 4px 10px rgba(0, 0, 0, 0.5); }

@keyframes previewBob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6%); }
}

.drop-row {
  display: grid;
  grid-template-columns: repeat(var(--cols, 7), var(--cell, 60px));
  gap: var(--gap, 8px);
}

.drop-arrow {
  width: var(--cell, 60px);
  height: calc(var(--cell, 60px) * 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell, 60px) * 0.32);
  color: var(--text-dim);
  background: none;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: color 0.15s ease, transform 0.15s ease;
}

.drop-arrow:hover,
.drop-arrow.active {
  color: var(--accent);
  transform: translateY(3px);
}

.drop-arrow:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 7), var(--cell, 60px));
  grid-template-rows: repeat(var(--rows, 6), var(--cell, 60px));
  gap: var(--gap, 8px);
  padding: var(--gap, 8px);
  border-radius: calc(var(--gap, 8px) * 2.5);
  background: linear-gradient(160deg, var(--board-1), var(--board-2));
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45), inset 0 2px 0 rgba(255, 255, 255, 0.15);
}

.column {
  display: contents;
}

.slot {
  position: relative;
  width: var(--cell, 60px);
  height: var(--cell, 60px);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #1c1730, #0a0814 75%);
  box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  touch-action: manipulation;
  overflow: visible;
}

.column.col-active .slot:not(.filled) {
  background: radial-gradient(circle at 35% 30%, #29224a, #120f22 75%);
}

.slot.filled {
  cursor: default;
}

/* The outer .piece is the full-size coloured ring (clear player identity,
   comfortable touch target). The image itself sits in a smaller inner
   circle (.piece-inner) sized so a square image's corners always stay
   inside the circle — nothing gets clipped, no matter the artwork. */
.piece {
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.piece.p1 { box-shadow: 0 0 0 7px var(--p1-color), 0 4px 10px rgba(0, 0, 0, 0.5); }
.piece.p2 { box-shadow: 0 0 0 7px var(--p2-color), 0 4px 10px rgba(0, 0, 0, 0.5); }

.piece-inner {
  width: 68%;
  height: 68%;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Full-bleed scenes (prehistory's cave paintings, wheat field, etc.) have
   no white margin of their own — let them fill the token right up to the
   coloured ring instead of matting them like the framed/object images. */
.piece-inner.full-bleed {
  width: 100%;
  height: 100%;
}

.piece.falling {
  animation: dropFall 0.5s cubic-bezier(0.4, 0.0, 0.2, 1) both;
}

@keyframes dropFall {
  0% { transform: translateY(calc(-1 * var(--drop-distance, 0px))); }
  75% { transform: translateY(calc(-1 * var(--drop-distance, 0px) * 0.03)); }
  100% { transform: translateY(0); }
}

.piece-inner img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}

.piece-inner .css-piece {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell, 60px) * 0.42);
}

.slot.win .piece {
  animation: winPulse 0.9s ease-in-out infinite;
}

@keyframes winPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

/* ---------------------------------------------------------------------- */
/* Info panel (loading)                                                    */
/* ---------------------------------------------------------------------- */

.info-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  background: var(--panel-solid);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius);
  padding: 32px;
  max-width: 720px;
  margin: auto;
}

.info-panel h2 {
  margin: 0;
  font-size: 26px;
}

.info-spinner {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

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

/* ---------------------------------------------------------------------- */
/* Win overlay                                                             */
/* ---------------------------------------------------------------------- */

.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 5, 30, 0.82);
  backdrop-filter: blur(4px);
  cursor: pointer;
  touch-action: manipulation;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.win-content {
  text-align: center;
  padding: 48px 56px;
  border-radius: 24px;
  background: linear-gradient(160deg, #312e81, #581c87);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: winPop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 90vw;
}

@keyframes winPop {
  0% { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.win-emoji {
  font-size: 72px;
  margin-bottom: 6px;
}

.win-content h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 5vw, 48px);
  color: var(--accent);
}

.win-content p {
  margin: 4px 0;
  font-size: clamp(16px, 2.2vw, 22px);
  color: var(--text);
}

.win-stats {
  color: var(--text-dim) !important;
  font-size: clamp(14px, 2vw, 18px) !important;
}

.win-tap {
  margin-top: 18px !important;
  font-weight: 700;
  color: var(--accent-2) !important;
  animation: tapPulse 1.4s ease-in-out infinite;
}

@keyframes tapPulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

/* ---------------------------------------------------------------------- */
/* Small screens                                                           */
/* ---------------------------------------------------------------------- */

@media (max-width: 640px) {
  .topbar {
    padding: 8px 10px;
  }

  .brand {
    position: static;
    top: auto;
    left: auto;
  }

  .brand-link-desktop {
    display: none;
  }

  .brand-link-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: var(--panel);
    border: 1px solid rgba(255, 255, 255, 0.12);
  }

  .btn {
    padding: 8px 12px;
    font-size: 13px;
    min-height: 36px;
  }

  .btn-label { display: none; }
  #newGameBtn { padding: 8px 14px; }

  .statsbar { gap: 14px; }
  .win-content { padding: 32px 24px; }

  .theme-grid {
    gap: 8px;
  }

  .theme-row {
    gap: 8px;
  }

  .theme-tile {
    width: 70px;
    padding: 6px;
  }

  .theme-tile-label { font-size: 11px; }
}
