:root {
  --ink: #eef1fb;
  --muted: #9aa3c7;
  --bg-deep: #0c0f1c;
  --bg-panel: #161b2e;
  --bg-panel-alt: #1e2440;
  --line: rgba(148, 163, 255, 0.16);
  --teal: #33e6c2;
  --coral: #ff6b7a;
  --amber: #ffcd5c;
  --violet: #9b8bfb;
  --danger: #ff5c6c;
  --success: #33e6c2;
  --piece-cell: 16px;
  --drag-cell: 30px;
  --drag-lift: 64px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(155, 139, 251, 0.22), transparent 42%),
    radial-gradient(circle at 88% 92%, rgba(51, 230, 194, 0.16), transparent 46%),
    var(--bg-deep);
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
}

.game {
  position: relative;
  display: grid;
  gap: 12px;
  min-height: 0;
  padding: 14px 16px 20px;
}

h1, h2, h3, p {
  margin: 0;
}

.hidden {
  display: none !important;
}

button {
  border: 0;
  border-radius: 10px;
  padding: 10px 16px;
  color: #0c0f1c;
  background: var(--teal);
  font: inherit;
  font-weight: 800;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 18px rgba(51, 230, 194, 0.25);
}

button:disabled {
  background: #3a405e;
  color: #7a8099;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

button.secondary {
  background: var(--bg-panel-alt);
  color: var(--ink);
  border: 1px solid var(--line);
}

button.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

button.danger {
  background: var(--coral);
  color: #2a0d10;
}

button.danger:hover:not(:disabled) {
  box-shadow: 0 8px 18px rgba(255, 107, 122, 0.3);
}

header.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

#contextLabel {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.header-actions button {
  min-height: 34px;
  padding: 8px 14px;
  font-size: 13px;
}

.panel {
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.02), transparent 40%), var(--bg-panel);
  border: 1px solid var(--line);
  padding: 16px;
}

/* ---------- Lobby ---------- */

.lobby-grid {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 14px;
}

.lobby-card {
  min-width: 0;
  padding: 14px;
  border-radius: 12px;
  background: var(--bg-panel-alt);
  border: 1px solid var(--line);
}

.lobby-card h2 {
  margin-bottom: 10px;
  font-size: 15px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--muted);
}

.lobby-card h2.spaced {
  margin-top: 16px;
}

.lobby-card h3 {
  margin: 12px 0 8px;
  font-size: 13px;
  color: var(--muted);
}

.mode-list {
  display: grid;
  gap: 8px;
  margin-bottom: 4px;
}

.duration-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
}

.duration-option {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}

.duration-option.selected {
  border-color: var(--teal);
  background: rgba(51, 230, 194, 0.14);
  color: var(--teal);
}

.duration-option.disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.mode-option {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
  cursor: pointer;
}

.mode-option strong {
  font-size: 14px;
}

.mode-option span {
  font-size: 12px;
  color: var(--muted);
}

.mode-option.selected {
  border-color: var(--teal);
  background: rgba(51, 230, 194, 0.1);
  box-shadow: 0 0 0 1px rgba(51, 230, 194, 0.35) inset;
}

.mode-option.disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.toggle-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--teal);
}

.pill {
  padding: 2px 9px;
  border-radius: 999px;
  background: rgba(51, 230, 194, 0.18);
  color: var(--teal);
  font-size: 12px;
  font-weight: 800;
}

.pill.off {
  background: rgba(255, 107, 122, 0.16);
  color: var(--coral);
}

.hint {
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.lobby-players {
  display: grid;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
  margin-bottom: 8px;
}

.lobby-player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.02);
  font-size: 13px;
}

.lobby-player-row .you-tag {
  color: var(--teal);
  font-size: 11px;
  font-weight: 800;
  margin-left: 6px;
}

/* ---------- Countdown ---------- */

.countdown-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(12, 15, 28, 0.72);
}

.countdown-number {
  font-size: clamp(72px, 22vmin, 180px);
  font-weight: 900;
  color: var(--teal);
  text-shadow: 0 0 40px rgba(51, 230, 194, 0.65);
  animation: countdown-pop 1s ease-out;
}

@keyframes countdown-pop {
  0% { transform: scale(0.4); opacity: 0; }
  40% { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ---------- Match ---------- */

.match .hud {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.meter {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--bg-panel-alt);
  border: 1px solid var(--line);
}

.meter span {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.meter strong {
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

.score-meter strong {
  color: var(--teal);
}

.match-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  gap: 16px;
  align-items: start;
}

.board-column {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.board-wrap {
  position: relative;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 3px;
  aspect-ratio: 1 / 1;
  width: min(100%, 560px);
  margin: 0 auto;
  padding: 8px;
  border-radius: 12px;
  border: 2px solid rgba(148, 163, 255, 0.28);
  background: #0a0d18;
  box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.5);
  touch-action: none;
}

.cell {
  position: relative;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid rgba(148, 163, 255, 0.06);
}

.cell.filled {
  border: none;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.14), 0 2px 4px rgba(0, 0, 0, 0.35);
}

.cell.obstacle {
  background: repeating-linear-gradient(135deg, #4a5170, #4a5170 4px, #3a4160 4px, #3a4160 8px);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.08);
}

.cell.frozen::after {
  content: "❄";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 60%;
  color: #bfe9ff;
  background: rgba(120, 200, 255, 0.28);
  border-radius: 5px;
}

.cell.fogged {
  background: #232a44 !important;
  box-shadow: none !important;
}

.cell.fogged::after {
  content: "?";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 800;
}

.cell.preview-valid {
  box-shadow: inset 0 0 0 2px var(--teal);
  background: rgba(51, 230, 194, 0.22);
}

.cell.preview-invalid {
  box-shadow: inset 0 0 0 2px var(--coral);
  background: rgba(255, 107, 122, 0.2);
}

.cell.just-placed {
  animation: cell-pop 0.24s ease-out;
}

.cell.clearing {
  animation: cell-clear 0.36s ease-in forwards;
  z-index: 2;
}

@keyframes cell-pop {
  0% { transform: scale(0.55); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@keyframes cell-clear {
  0% { transform: scale(1); opacity: 1; filter: brightness(2.2); }
  100% { transform: scale(0.2) rotate(12deg); opacity: 0; filter: brightness(2.6); }
}

.color-teal { background: linear-gradient(160deg, #6cf5da, var(--teal) 70%); }
.color-coral { background: linear-gradient(160deg, #ffa1ab, var(--coral) 70%); }
.color-amber { background: linear-gradient(160deg, #ffe29a, var(--amber) 70%); }
.color-violet { background: linear-gradient(160deg, #c6bafe, var(--violet) 70%); }
.color-sky { background: linear-gradient(160deg, #9fd8ff, #4fb3ff 70%); }
.color-lime { background: linear-gradient(160deg, #d4ff9a, #9fe64f 70%); }
.color-rose { background: linear-gradient(160deg, #ffb6e6, #f45fc4 70%); }

.topped-banner {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(12, 15, 28, 0.85);
  border: 1px solid var(--coral);
  color: var(--coral);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.score-popup-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
}

.score-popup {
  position: absolute;
  font-weight: 900;
  font-size: 18px;
  color: var(--amber);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  animation: popup-float 0.9s ease-out forwards;
  white-space: nowrap;
}

.score-popup.combo {
  color: var(--coral);
  font-size: 22px;
}

@keyframes popup-float {
  0% { transform: translateY(0) scale(0.7); opacity: 0; }
  20% { transform: translateY(-8px) scale(1.15); opacity: 1; }
  100% { transform: translateY(-52px) scale(1); opacity: 0; }
}

.tray {
  display: flex;
  justify-content: center;
  gap: clamp(8px, 3vw, 22px);
  min-height: 96px;
  padding: 10px;
  border-radius: 12px;
  background: var(--bg-panel-alt);
  border: 1px solid var(--line);
}

.tray-slot {
  position: relative;
  display: grid;
  place-items: center;
  width: 88px;
  min-width: 60px;
  touch-action: none;
}

.tray-slot.empty::after {
  content: "";
}

.tray-slot.locked {
  opacity: 0.4;
}

.tray-slot.locked::before {
  content: "🔒";
  font-size: 20px;
}

.piece-grid {
  display: grid;
  gap: 2px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.piece-grid.dragging {
  cursor: grabbing;
}

.piece-cell {
  width: var(--piece-cell-size, var(--piece-cell));
  height: var(--piece-cell-size, var(--piece-cell));
  border-radius: 3px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}

.piece-cell.blank {
  visibility: hidden;
}

.drag-ghost {
  position: fixed;
  z-index: 40;
  display: grid;
  gap: 2px;
  pointer-events: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  opacity: 0.95;
  filter: drop-shadow(0 10px 16px rgba(0, 0, 0, 0.5));
}

.drag-ghost.invalid {
  opacity: 0.72;
  filter: drop-shadow(0 10px 16px rgba(0, 0, 0, 0.5)) saturate(0.65);
}

.drag-ghost.invalid .piece-cell:not(.blank) {
  box-shadow: inset 0 0 0 2px var(--coral), 0 0 0 1px rgba(255, 107, 122, 0.36);
}

.drag-ghost .piece-cell {
  width: var(--piece-cell-size, var(--drag-cell));
  height: var(--piece-cell-size, var(--drag-cell));
  border-radius: 5px;
}

.ranking-column {
  display: grid;
  gap: 10px;
  align-content: start;
}

.ranking-column h2 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.ranking-list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 420px;
  overflow-y: auto;
}

.ranking-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 9px;
  background: var(--bg-panel-alt);
  border: 1px solid var(--line);
  font-size: 13px;
}

.ranking-row.me {
  border-color: var(--teal);
  background: rgba(51, 230, 194, 0.1);
}

.ranking-row .rk-pos {
  width: 20px;
  color: var(--muted);
  font-weight: 800;
}

.ranking-row .rk-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ranking-row .rk-score {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  color: var(--teal);
}

.ranking-row .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #4a5170;
  flex-shrink: 0;
}

.ranking-row .dot.online {
  background: var(--success);
  box-shadow: 0 0 6px rgba(51, 230, 194, 0.7);
}

.ranking-row .dot.topped {
  background: var(--coral);
}

.attack-toast-layer {
  position: fixed;
  top: 70px;
  right: 16px;
  z-index: 25;
  display: grid;
  gap: 8px;
}

.attack-toast {
  padding: 8px 14px;
  border-radius: 10px;
  background: rgba(12, 15, 28, 0.92);
  border: 1px solid var(--coral);
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
  max-width: 240px;
  animation: toast-in-out 3.2s ease forwards;
}

.attack-toast.incoming {
  border-color: var(--danger);
}

.attack-toast.outgoing {
  border-color: var(--teal);
}

@keyframes toast-in-out {
  0% { transform: translateX(24px); opacity: 0; }
  10% { transform: translateX(0); opacity: 1; }
  85% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- Results ---------- */

.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 35;
  display: grid;
  place-items: center;
  background: rgba(12, 15, 28, 0.85);
  padding: 16px;
}

.result-box {
  width: min(92vw, 460px);
  max-height: 88vh;
  overflow-y: auto;
  padding: 22px;
  border-radius: 16px;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  text-align: center;
}

.result-box h1 {
  font-size: 26px;
  margin-bottom: 14px;
  color: var(--amber);
}

.podium {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
}

.podium-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--bg-panel-alt);
  font-size: 14px;
  text-align: left;
}

.podium-row .medal {
  font-size: 18px;
}

.podium-row .name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}

.podium-row .pts {
  font-variant-numeric: tabular-nums;
  color: var(--teal);
  font-weight: 800;
}

.personal-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 14px;
}

.stat-tile {
  padding: 10px;
  border-radius: 10px;
  background: var(--bg-panel-alt);
  border: 1px solid var(--line);
}

.stat-tile span {
  display: block;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
}

.stat-tile strong {
  font-size: 18px;
}

.result-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.result-actions button {
  flex: 1;
}

.full-ranking {
  text-align: left;
  display: grid;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  max-height: 160px;
  overflow-y: auto;
}

/* ---------- Confirm dialog ---------- */

.confirm-overlay {
  position: fixed;
  inset: 0;
  z-index: 45;
  display: grid;
  place-items: center;
  background: rgba(12, 15, 28, 0.85);
  padding: 16px;
}

.confirm-box {
  width: min(92vw, 360px);
  padding: 20px;
  border-radius: 16px;
  background: var(--bg-panel);
  border: 1px solid var(--coral);
  text-align: center;
}

.confirm-box p {
  margin-bottom: 16px;
  font-size: 15px;
  line-height: 1.4;
}

.confirm-actions {
  display: flex;
  gap: 10px;
}

.confirm-actions button {
  flex: 1;
}

@media (max-width: 860px) {
  .lobby-grid {
    grid-template-columns: 1fr;
  }

  .match-body {
    grid-template-columns: 1fr;
  }

  .ranking-column {
    order: 2;
  }
}

@media (max-width: 620px) {
  :root {
    --piece-cell: 13px;
    --drag-cell: 24px;
    --drag-lift: 52px;
  }

  .game {
    gap: 8px;
    padding: 8px 8px 12px;
  }

  .header-actions button {
    min-width: 0;
    padding: 7px 10px;
  }

  .match {
    padding: 10px;
  }

  .match .hud {
    gap: 6px;
    margin-bottom: 8px;
  }

  .meter {
    padding: 6px 8px;
  }

  .meter strong {
    font-size: 18px;
  }

  .board-column {
    gap: 8px;
  }

  .board {
    width: min(100%, calc(100dvh - 214px), 390px);
    padding: 6px;
    gap: 2px;
  }

  .tray {
    gap: 8px;
    min-height: 68px;
    padding: 6px;
  }

  .tray-slot {
    width: 62px;
    min-width: 48px;
  }
}

@media (max-width: 620px) and (max-height: 740px) {
  :root {
    --piece-cell: 12px;
    --drag-cell: 22px;
    --drag-lift: 46px;
  }

  .board {
    width: min(100%, calc(100dvh - 190px), 360px);
  }

  .tray {
    min-height: 60px;
  }
}
