/* ===== Theme (pixel art) ===== */
:root {
  --bg: #1a1c2c;
  --panel: #29366f;
  --panel-2: #333c57;
  --border: #0b0b14;
  --ink: #f4f4f4;
  --muted: #94b0c2;
  --accent: #41a6f6;
  --accent-dark: #3b5dc9;
  --board-bg: #3b5dc9;
  --board-bg-dark: #29366f;
  --board-frame: #11131f;
  --red: #e43b44;
  --red-dark: #a02c34;
  --yellow: #ffcd75;
  --yellow-dark: #c98f2b;
  --cell-empty: #11131f;
  --radius: 0px;
  --font-pixel: "Press Start 2P", "Courier New", monospace;
  --font-term: "VT323", "Courier New", monospace;
  --pixel-shadow: 4px 4px 0 #000;
  --pixel-shadow-lg: 6px 6px 0 #000;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  /* Kill pull-to-refresh (accidental reloads wipe games) while letting
     normal scrolling work. Must be on html too: when content overflows,
     html is the real scrolling element. */
  overscroll-behavior-y: none;
}

body {
  font-family: var(--font-term);
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
  padding-bottom: 16px;
  image-rendering: pixelated;
}

/* ===== Header ===== */
.site-header {
  text-align: center;
  padding: 20px 16px 8px;
}

.title {
  margin: 0;
  font-family: var(--font-pixel);
  font-size: clamp(18px, 4vw, 26px);
  letter-spacing: 1px;
  font-weight: 400;
  text-transform: uppercase;
  color: var(--ink);
  text-shadow: 3px 3px 0 #000;
}

/* The title is a link back to the games hub — keep the exact title look. */
.title-link {
  color: inherit;
  text-decoration: none;
}

.subtitle {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 20px;
  letter-spacing: 1px;
}

/* ===== Layout ===== */
.container {
  width: 100%;
  max-width: 860px;
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.view.hidden,
.hidden {
  display: none !important;
}

/* ===== Menu (stacked, big, fills the window height) ===== */
#menu-view {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.menu-grid {
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: 1fr;
  gap: 24px;
  margin-top: 40px;
  flex: 1;
}

.menu-card {
  background: var(--panel);
  border: 4px solid #000;
  border-radius: var(--radius);
  box-shadow: var(--pixel-shadow-lg);
  padding: 48px 28px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.08s steps(2), box-shadow 0.08s steps(2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  min-height: 0;
  justify-content: center;
}

.menu-card:hover {
  transform: translate(-2px, -2px);
  box-shadow: 8px 8px 0 #000;
}

.menu-card:active {
  transform: translate(4px, 4px);
  box-shadow: 2px 2px 0 #000;
}

.menu-card-title {
  font-family: var(--font-pixel);
  font-size: clamp(16px, 5vw, 34px);
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #ffffff;
  text-shadow: 2px 2px 0 #000;
  line-height: 1.5;
}

.menu-card-desc {
  font-family: var(--font-term);
  font-size: 22px;
  color: #d7deec;
  text-align: center;
  letter-spacing: 1px;
}

/* ===== Game bar ===== */
.game-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

/* Push the online "Leave room" button to the far right. */
#online-view [data-action="online-leave"] {
  margin-left: auto;
}

/* Local game bar: Menu on the left, centered color picker, Restart on the right. */
#local-view .game-bar {
  justify-content: space-between;
}

/* Local "Play as" color picker (segmented control). */
.color-pick {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-pixel);
  font-size: 10px;
  color: var(--muted);
}

.btn-pick {
  font-family: var(--font-pixel);
  font-size: 10px;
  border: 3px solid #000;
  background: var(--panel-2);
  color: var(--ink);
  padding: 8px 14px;
  border-radius: 0;
  cursor: pointer;
  box-shadow: 3px 3px 0 #000;
  transition: transform 0.08s steps(2), box-shadow 0.08s steps(2);
}

.btn-pick:active {
  transform: translate(3px, 3px);
  box-shadow: 0 0 0 #000;
}

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

.btn-pick.is-yellow.active {
  background: var(--yellow);
  border-color: #000;
  color: #1a1a1a;
}

.mode-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  font-size: 14px;
  color: var(--muted);
  flex-wrap: wrap;
}

.mode-controls select {
  padding: 8px 10px;
  border-radius: 0;
  border: 3px solid #000;
  background: var(--panel-2);
  color: var(--ink);
  font-family: var(--font-term);
}

/* ===== Buttons ===== */
.btn {
  font-family: var(--font-pixel);
  font-size: 11px;
  border: 3px solid #000;
  border-radius: 0;
  padding: 10px 14px;
  font-weight: 400;
  cursor: pointer;
  box-shadow: var(--pixel-shadow);
  transition: transform 0.08s steps(2), box-shadow 0.08s steps(2),
    background 0.08s steps(2);
}

.btn:hover {
  background: #283252;
}

.btn:active {
  transform: translate(4px, 4px);
  box-shadow: 0 0 0 #000;
}

.btn-ghost {
  background: var(--panel-2);
  color: var(--ink);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-dark);
}

/* ===== Board ===== */
.board-wrap {
  position: relative; /* anchor for the scanline overlay */
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--panel);
  border: 4px solid #000;
  border-radius: var(--radius);
  box-shadow: var(--pixel-shadow-lg);
  padding: 18px;
  width: 100%;
}

/* Retro CRT scanlines over the board (same as the 2048 board). */
.board-wrap::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.07) 0px,
    rgba(0, 0, 0, 0.07) 1px,
    transparent 1px,
    transparent 3px
  );
  pointer-events: none;
  z-index: 1000;
}

.board {
  position: relative; /* anchor for layers */
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0; /* tile the front mask into a solid blue board with holes */
  background: var(--board-bg); /* solid color, no gradient */
  border: 6px solid var(--board-frame); /* classic framed board */
  border-radius: 0;
  padding: 16px; /* MUST stay in sync with .board-back inset below */
  width: 100%;
  max-width: 730px;
  image-rendering: pixelated;
}

/* Back layer: the dark empty-hole cavity, behind the cells. */
.board-back {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  bottom: 16px;
  background: var(--cell-empty);
  border-radius: 0;
  z-index: 1;
  pointer-events: none;
}

/* Settled chips: absolutely positioned in JS at the exact cell center. */
.piece {
  position: absolute;
  box-sizing: border-box;
  border-radius: 50%;
  border: 3px solid #000;
  z-index: 1;
  image-rendering: pixelated;
}

.piece.red {
  background: var(--red);
  box-shadow: inset -5px -5px 0 var(--red-dark),
    inset 4px 4px 0 rgba(255, 255, 255, 0.25);
}

.piece.yellow {
  background: var(--yellow);
  box-shadow: inset -5px -5px 0 var(--yellow-dark),
    inset 4px 4px 0 rgba(255, 255, 255, 0.3);
}

.piece.win {
  animation: winblink 0.7s steps(1, end) infinite;
}

/* White border on the LAST piece played, drawn on the chip itself. */
.piece.last {
  border: 3px solid #ffffff;
}

/* "Disappear / appear" pulse for the winning line (on the pieces themselves). */
@keyframes winblink {
  0%,
  50% {
    opacity: 1;
  }
  51%,
  100% {
    opacity: 0.12;
  }
}

/*
 * Front layer: a blue board panel whose CENTER is a transparent circular hole.
 * The blue material hides the falling token wherever there is no hole.
 */
.cell {
  position: relative;
  z-index: 2;
  aspect-ratio: 1 / 1;
  background: radial-gradient(
    circle at center,
    transparent 0 62%,
    var(--board-bg) 63% 100%
  );
}

/* Falling token / ghost overlay (sits in the back layer, behind the cells). */
.token {
  position: absolute;
  border-radius: 50%;
  border: 3px solid #000;
  z-index: 1;
  pointer-events: none;
  image-rendering: pixelated;
  transition: top 0.55s cubic-bezier(0.3, 0.7, 0.3, 1);
}

/* Hover preview: appears instantly at the landing cell (no fall). */
.token.ghost {
  transition: none;
  opacity: 0.45;
}

.token.red {
  background: var(--red);
  box-shadow: inset -5px -5px 0 var(--red-dark),
    inset 4px 4px 0 rgba(255, 255, 255, 0.25);
}

.token.yellow {
  background: var(--yellow);
  box-shadow: inset -5px -5px 0 var(--yellow-dark),
    inset 4px 4px 0 rgba(255, 255, 255, 0.3);
}

/* ===== Status / lobby ===== */
.status {
  margin-top: 16px;
  text-align: center;
  font-family: var(--font-pixel);
  font-size: 11px;
  line-height: 1.6;
  min-height: 24px;
  color: var(--ink);
  text-shadow: 2px 2px 0 #000;
}

.lobby {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
  margin-top: 24px;
}

.lobby-card {
  background: var(--panel);
  border: 4px solid #000;
  border-radius: var(--radius);
  box-shadow: var(--pixel-shadow-lg);
  padding: 22px;
}

.lobby-card h2 {
  margin: 0 0 14px;
  font-family: var(--font-pixel);
  font-size: 13px;
  letter-spacing: 1px;
  text-shadow: 2px 2px 0 #000;
}

.join-row {
  display: flex;
  gap: 10px;
}

.join-row input {
  flex: 1;
  padding: 11px 12px;
  border-radius: 0;
  border: 3px solid #000;
  background: var(--panel-2);
  color: var(--ink);
  font-family: var(--font-pixel);
  text-transform: uppercase;
  letter-spacing: 2px;
  font-size: 11px;
}

.room-line {
  text-align: center;
  font-family: var(--font-term);
  font-size: 20px;
  color: var(--muted);
  margin: 4px 0 14px;
  letter-spacing: 1px;
}

.room-code {
  font-family: var(--font-pixel);
  font-weight: 400;
  letter-spacing: 2px;
  color: var(--accent);
  font-size: 13px;
}

/* ===== Online lobby: visibility toggle + public rooms ===== */
.seg {
  display: inline-flex;
  border: 3px solid #000;
  box-shadow: var(--pixel-shadow);
  margin-bottom: 14px;
}

.seg-btn {
  border: none;
  border-radius: 0;
  box-shadow: none;
  background: var(--panel-2);
  color: var(--ink);
  text-transform: uppercase;
}

.seg-btn + .seg-btn {
  border-left: 3px solid #000;
}

.seg-btn[aria-pressed="true"] {
  background: #000;
  color: #fff;
}

.hint {
  font-family: var(--font-term);
  font-size: 14px;
  line-height: 1.4;
  color: var(--muted);
  margin: 12px 0 0;
}

.room-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 220px;
  overflow-y: auto;
}

.room-item {
  width: 100%;
  text-align: left;
  justify-content: flex-start;
  text-transform: uppercase;
  letter-spacing: 2px;
}

/* ===== Bottom link (back to hub) ===== */
.bottom-link {
  display: flex;
  justify-content: center;
  margin-top: 32px;
}

.bottom-link a {
  text-decoration: none;
}

/* ===== Responsive ===== */
@media (max-width: 520px) {
  .menu-card {
    min-height: 84px;
    padding: 22px 18px;
  }
  .game-bar {
    gap: 10px;
  }
  .mode-controls {
    width: 100%;
    margin-left: 0;
  }
  .title {
    font-size: 16px;
  }
}
