:root {
  --bg: #08111f;
  --bg2: #0e1730;
  --panel: rgba(14, 20, 39, 0.78);
  --panel-solid: #121b33;
  --panel-soft: #182341;
  --border: rgba(255, 255, 255, 0.08);
  --text: #eef3ff;
  --muted: rgba(238, 243, 255, 0.68);
  --accent: #6ea8ff;
  --accent2: #8b7bff;
  --good: #48d39a;
  --warn: #ffbe5c;
  --danger: #ff6b7a;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
  --radius-xl: 28px;
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 12px;
  --grid-size: min(78vw, 540px);
  --tile: calc(var(--grid-size) / 10);
  --piece-size: 58px;
  --hotbar-slot-size: clamp(82px, 12vw, 108px);
  --hotbar-gap: 12px;
}

* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  font-family: "Geist Sans", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-weight: 400;
  color: var(--text);
  background: linear-gradient(145deg, var(--bg), var(--bg2));
  overflow-x: hidden;
  overflow-y: auto;
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
}
input, textarea {
  user-select: auto;
  -webkit-user-select: auto;
}
button, .button, .pill, .stat, .leader-row, .cell, .piece-slot {
  user-select: none;
  -webkit-user-select: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background-image: linear-gradient(0deg, rgba(255,255,255,0.015) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,0.015) 1px, transparent 1px);
  background-size: 44px 44px;
  -webkit-mask-image: radial-gradient(circle at center, black, transparent 95%);
  mask-image: radial-gradient(circle at center, black, transparent 95%);
  pointer-events: none;
  opacity: 0.7;
  z-index: -2;
}

/* Blueprint pattern layer - technical grid, diagonal hatching, and measurement lines */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  opacity: 1;
  background-image:
    /* Technical grid - fine white lines at responsive spacing */
    linear-gradient(0deg, rgba(255,255,255,0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.12) 1px, transparent 1px),
    /* Diagonal hatching - 45-degree lines at responsive spacing */
    repeating-linear-gradient(45deg, transparent, transparent calc(var(--bp-diagonal) - 1px), rgba(255,255,255,0.06) calc(var(--bp-diagonal) - 1px), rgba(255,255,255,0.06) var(--bp-diagonal)),
    repeating-linear-gradient(-45deg, transparent, transparent calc(var(--bp-diagonal) - 1px), rgba(255,255,255,0.06) calc(var(--bp-diagonal) - 1px), rgba(255,255,255,0.06) var(--bp-diagonal)),
    /* Architectural measurement lines - dimension ticks at responsive intervals */
    linear-gradient(0deg, transparent calc(var(--bp-measure) - 1px), rgba(255,255,255,0.15) calc(var(--bp-measure) - 1px), rgba(255,255,255,0.15) var(--bp-measure), transparent var(--bp-measure)),
    linear-gradient(90deg, transparent calc(var(--bp-measure) - 1px), rgba(255,255,255,0.15) calc(var(--bp-measure) - 1px), rgba(255,255,255,0.15) var(--bp-measure), transparent var(--bp-measure)),
    /* Wireframe block outlines - subtle 3D-like shapes */
    linear-gradient(135deg, transparent 48%, rgba(255,255,255,0.1) 48%, rgba(255,255,255,0.1) 52%, transparent 52%),
    linear-gradient(45deg, transparent 48%, rgba(255,255,255,0.1) 48%, rgba(255,255,255,0.1) 52%, transparent 52%);
  --bp-grid: clamp(16px, 2.5vw, 24px);
  --bp-diagonal: clamp(32px, 5vw, 48px);
  --bp-measure: clamp(80px, 12vw, 120px);
  --bp-wireframe-1: clamp(96px, 15vw, 144px);
  --bp-wireframe-2: clamp(128px, 20vw, 192px);
  background-size: var(--bp-grid) var(--bp-grid), var(--bp-grid) var(--bp-grid), var(--bp-diagonal) var(--bp-diagonal), var(--bp-diagonal) var(--bp-diagonal), var(--bp-measure) var(--bp-measure), var(--bp-measure) var(--bp-measure), var(--bp-wireframe-1) var(--bp-wireframe-1), var(--bp-wireframe-2) var(--bp-wireframe-2);
  background-position: 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, calc(var(--bp-wireframe-2) * 0.5) calc(var(--bp-wireframe-2) * 0.5);
}

/* Keep the original gradient on body */
body {
  background: linear-gradient(145deg, var(--bg), var(--bg2));
}

.app {
  width: 100%;
  max-width: 100vw;
  position: relative;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 18px;
  min-height: 100vh;
  overflow-x: hidden;
}

.screen {
  position: relative;
  display: none;
  padding: 18px;
  width: 100%;
  overflow: visible;
}
.screen.active { display: flex; flex-direction: column; }

/* Loading Screen */
#loadingScreen {
  position: fixed;
  inset: 0;
  background: linear-gradient(145deg, var(--bg), var(--bg2));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.3s ease;
}

/* Fade-out state — not .hidden (avoids fighting global .hidden specificity / source order) */
#loadingScreen.is-hiding {
  opacity: 0;
  pointer-events: none;
}

.loader {
  width: 60px;
  height: 60px;
  position: relative;
  margin-bottom: 20px;
}

.loader::before,
.loader::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 3px solid transparent;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1.2s linear infinite;
}

.loader::after {
  border-top-color: var(--accent2);
  animation-name: spin-inner;
  animation-delay: -0.6s;
}

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

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

.loading-text {
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.05em;
}

.loading-quote {
  font-size: 13px;
  color: var(--muted);
  font-weight: 500;
  margin-top: 12px;
  text-align: center;
  max-width: 320px;
  line-height: 1.5;
  transition: opacity 0.3s ease;
  opacity: 0;
}

.shell {
  width: min(1380px, 100%);
  max-width: 100%;
  margin: auto;
  display: grid;
  gap: clamp(12px, 3vw, 20px);
  min-width: 0;
  position: relative;
}

.glass {
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow);
  border-radius: var(--radius-xl);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: clamp(14px, 4vw, 24px) clamp(16px, 5vw, 28px);
  gap: clamp(12px, 3vw, 20px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
  flex: 1;
}

.brand-badge {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, rgba(110,168,255,.28), rgba(139,123,255,.22));
  border: 1px solid rgba(255,255,255,0.10);
  flex-shrink: 0;
}

.brand-title {
  margin: 0;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.1;
}
.brand-subtitle {
  font-size: 13px;
  color: var(--muted);
  margin-top: 3px;
  font-weight: 500;
}

.brand-tagline {
  margin-left: auto;
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.brand-trophy {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(255, 215, 0, 0.15), rgba(255, 193, 7, 0.10));
  border: 1px solid rgba(255, 215, 0, 0.25);
  color: #ffd700;
  font-weight: 800;
  font-size: 11px;
  flex-shrink: 0;
  min-width: fit-content;
}

.game-controls .button {
  flex-shrink: 0;
  min-width: fit-content;
}
.brand-trophy .icon {
  width: 14px;
  height: 14px;
}

.pill-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 999px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.10);
  color: var(--text);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.01em;
  transition: all .18s ease;
}
.pill:hover { background: rgba(255,255,255,0.08); }

.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1em;
  height: 1em;
  color: currentColor;
  flex-shrink: 0;
}

.inline-help-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  margin-left: 4px;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.18s ease;
}
.inline-help-icon:hover {
  color: var(--accent);
}

.icon svg {
  width: 100%;
  height: 100%;
  display: block;
  stroke: currentColor;
}

.button {
  appearance: none;
  border: 0;
  outline: 0;
  cursor: pointer;
  user-select: none;
  border-radius: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 14px 20px;
  color: var(--text);
  font-weight: 800;
  font-size: 14px;
  letter-spacing: 0.01em;
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease, opacity .18s ease;
}
.button:hover { transform: translateY(-2px) scale(1.02); }
.button:active { transform: translateY(0) scale(0.98); }
.button.primary {
  background: linear-gradient(135deg, #6ea8ff, #8b7bff);
  box-shadow: 0 14px 30px rgba(110,168,255,0.24);
}
.button.primary:hover { box-shadow: 0 18px 40px rgba(110,168,255,0.32); }
.button.secondary {
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.08);
}
.button.secondary:hover { background: rgba(255,255,255,0.08); }
.button.ghost {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.08);
  color: var(--text);
}
.button.danger {
  background: linear-gradient(135deg, #ff6b7a, #ff8a5c);
  box-shadow: 0 14px 30px rgba(255,107,122,0.18);
}
.button.danger:hover { box-shadow: 0 18px 40px rgba(255,107,122,0.28); }
.button.small { padding: 10px 14px; border-radius: 14px; font-size: 13px; }
.button.icon-square {
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 16px;
  flex: 0 0 auto;
}
.button.icon-square .icon { width: 24px; height: 24px; }

.username-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.username-text {
  flex: 1;
  min-width: 0;
}

.username-text .username-label {
  display: block;
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.username-text .username-value {
  font-size: 16px;
  color: var(--accent);
  font-weight: 800;
  word-break: break-all;
}

.user-stats-bar {
  padding: clamp(14px, 4vw, 24px) clamp(16px, 5vw, 28px);
  margin-top: 12px;
  min-height: auto;
}

.user-stats-content {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.stats-divider {
  width: 1px;
  height: 20px;
  background: rgba(255, 255, 255, 0.1);
  flex-shrink: 0;
}

.username-value {
  font-size: 14px;
  color: var(--accent);
  font-weight: 800;
}

#changeUsernameBtn {
  flex-shrink: 0;
  display: none;
}
#changeUsernameBtn.is-visible { display: flex; }

.hero-stats-divider {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.stats-section-label {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 10px;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.layout-start {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: 18px;
  align-items: stretch;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}

.hero {
  padding: 32px;
  min-height: auto;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}
.hero-actions {
  position: relative;
}
.hero::after {
  content: "";
  position: absolute;
  inset: auto -120px -120px auto;
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(110,168,255,.24), transparent 65%);
  filter: blur(20px);
  pointer-events: none;
}

.hero-copy {
  position: relative;
  z-index: 1;
  max-width: 620px;
}
.kicker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(72,211,154,0.12), rgba(110,168,255,0.10));
  border: 1px solid rgba(110,168,255,0.20);
  color: rgba(110,168,255,0.85);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-bottom: 20px;
  transition: all .18s ease;
}
.kicker .icon {
  width: 16px;
  height: 16px;
}
.hero h1 {
  margin: 0;
  font-size: clamp(44px, 7vw, 76px);
  line-height: 0.92;
  letter-spacing: -0.06em;
  background: linear-gradient(135deg, #eef3ff, rgba(238,243,255,0.75));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.hero p {
  margin: 20px 0 0;
  color: var(--muted);
  max-width: 58ch;
  font-size: 15px;
  line-height: 1.8;
  font-weight: 500;
}
.inline-help-icon {
  display: inline-flex;
  align-items: center;
  vertical-align: middle;
  margin-left: 4px;
}
.inline-help-icon .icon {
  width: 16px;
  height: 16px;
  color: var(--muted);
}

.hero-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 32px;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.2) transparent;
}

.hero-actions::-webkit-scrollbar {
  height: 4px;
}

.hero-actions::-webkit-scrollbar-track {
  background: transparent;
}

.hero-actions::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.2);
  border-radius: 4px;
}

.hero-actions .button {
  min-height: 56px;
  min-width: 56px;
  width: 56px;
  height: 56px;
  flex-shrink: 0;
}
.hero-actions #howBtn {
  position: static;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 24px;
}
@media (min-width: 600px) {
  .stats-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
.stat {
  padding: 18px;
  border-radius: 20px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  transition: all .18s ease;
}
.stat:hover { background: rgba(255,255,255,0.06); }
.stat .label {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 6px;
  letter-spacing: 0.01em;
}
.stat .value {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: rgba(110,168,255,0.95);
}
.stat .value.golden {
  color: #ffd700;
  text-shadow: 0 0 10px rgba(255, 215, 0, 0.6);
}

.side-panel > .button { width: 100%; }

.side-panel {
  padding: 24px;
  display: grid;
  gap: 18px;
  align-content: start;
  min-height: auto;
  max-height: 600px;
  overflow-y: auto;
}

.panel-card {
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 22px;
  padding: 20px;
  transition: all .18s ease;
}
.panel-card:hover { background: rgba(255,255,255,0.05); }
.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  font-weight: 800;
  font-size: 16px;
  letter-spacing: -0.01em;
}
.leaderboard-meta {
  font-size: 12px;
  margin-bottom: 10px;
  color: var(--muted);
  display: block;
  line-height: 1.45;
}
.muted { color: var(--muted); }

.leaderboard {
  display: grid;
  gap: 10px;
  max-height: 300px;
  overflow: auto;
  padding-right: 4px;
}
.leader-row {
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 14px;
  align-items: center;
  padding: 12px 14px;
  border-radius: 16px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.06);
  transition: all .18s ease;
}
.leader-row:hover { background: rgba(255,255,255,0.06); }
.rank-badge {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 900;
  font-size: 14px;
  background: linear-gradient(135deg, rgba(110,168,255,.30), rgba(139,123,255,.25));
  border: 1px solid rgba(255,255,255,0.12);
  color: rgba(110,168,255,0.95);
}
.rank-badge-hidden {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}
.rank-badge--me {
  background: linear-gradient(135deg, rgba(110,168,255,.55), rgba(139,123,255,.45));
  border-color: rgba(110,168,255,0.55);
  color: #fff;
}
.leader-row.current-user {
  border: 2px solid rgba(110,168,255,0.6);
  background: rgba(110,168,255,0.08);
}
.leader-row.current-user:hover {
  background: rgba(110,168,255,0.12);
}
.leader-name { font-weight: 800; font-size: 15px; }
.leader-score { font-weight: 900; font-size: 16px; color: rgba(110,168,255,0.95); }

.grid-shell {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) 280px;
  gap: 18px;
  align-items: start;
}

.game-panel {
  padding: 20px;
  display: grid;
  gap: 18px;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  position: relative;
}
.score-label {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 4px;
}
.score-value {
  font-size: 32px;
  font-weight: 800;
  color: var(--accent);
}
.prominent-score {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(14, 20, 39, 0.95);
  border: 2px solid rgba(110, 168, 255, 0.5);
  border-radius: 20px;
  padding: 12px 24px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(12px);
}
.prominent-score .label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.prominent-score .value {
  font-size: 28px;
  font-weight: 900;
  color: var(--accent);
  line-height: 1.2;
}
.game-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: nowrap;
  justify-content: flex-end;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.2) transparent;
}

#exitBtn {
  margin-left: auto;
}

.game-controls::-webkit-scrollbar {
  height: 4px;
}

.game-controls::-webkit-scrollbar-track {
  background: transparent;
}

.game-controls::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.2);
  border-radius: 4px;
}
.score-strip {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.score-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 999px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.10);
  font-weight: 800;
  font-size: 13px;
  transition: all .18s ease;
}
.score-chip:hover { background: rgba(255,255,255,0.08); }

.game-canvas-wrap {
  padding: 18px;
  display: grid;
  place-items: center;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

#board {
  width: var(--grid-size);
  height: var(--grid-size);
  max-width: 100%;
  max-height: 100%;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(10, 1fr);
  gap: 8px;
  padding: 12px;
  border-radius: 28px;
  background: linear-gradient(145deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
  border: 1px solid rgba(255,255,255,0.08);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.02), 0 20px 50px rgba(0,0,0,0.15);
  touch-action: none;
  position: relative;
}

.cell {
  border-radius: 14px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.05);
  position: relative;
  overflow: hidden;
  transition: background .10s ease, box-shadow .12s ease, outline .08s ease;
  cursor: pointer;
  will-change: background;
}
.cell:hover { background: rgba(255,255,255,0.05); }
.cell.filled {
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.12);
}
.cell.highlight-valid { outline: 2.5px solid rgba(72,211,154,0.80); outline-offset: -1px; background: rgba(72,211,154,0.18); }
.cell.highlight-invalid { outline: 2.5px solid rgba(255,107,122,0.80); outline-offset: -1px; background: rgba(255,107,122,0.16); }
.cell.clearance-highlight {
  outline: 3px solid rgba(255, 235, 59, 0.95);
  outline-offset: -1px;
  animation: clearance-pulse 0.8s ease-in-out infinite;
}
@keyframes clearance-pulse {
  0%, 100% {
    outline-color: rgba(255, 235, 59, 0.95);
    outline-width: 3px;
    box-shadow: 0 0 8px rgba(255, 235, 59, 0.4);
  }
  50% {
    outline-color: rgba(255, 235, 59, 0.65);
    outline-width: 2px;
    box-shadow: 0 0 4px rgba(255, 235, 59, 0.2);
  }
}
.cell.ghost-piece::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--ghost-c1, #6ea8ff) 0%, var(--ghost-c2, #8b7bff) 100%);
  opacity: 0.32;
  pointer-events: none;
  z-index: 1;
  transform: scale(0.98);
}
.cell.ghost-piece.preview-squircle::before { border-radius: 18px; }
.cell.ghost-piece.preview-round::before,
.cell.ghost-piece.preview-pill::before { border-radius: 999px; }
.cell.ghost-piece.preview-diamond::before {
  border-radius: 4px;
  transform: scale(0.98) rotate(45deg);
}
.cell.ghost-piece.preview-hex::before {
  clip-path: polygon(25% 4%, 75% 4%, 100% 50%, 75% 96%, 25% 96%, 0% 50%);
  border-radius: 0;
}

.cell-piece {
  width: 100%;
  height: 100%;
  border-radius: 14px;
  transform: scale(0.98);
  background: linear-gradient(145deg, var(--c1, #6ea8ff), var(--c2, #8b7bff));
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.20), inset 0 1px 0 rgba(255,255,255,0.15);
  transition: all .12s ease;
}
.cell-piece::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.35), transparent 35%, transparent 65%, rgba(0,0,0,0.12));
  pointer-events: none;
}
.cell-piece.round,
.cell-piece.pill { border-radius: 999px; }
.cell-piece.squircle { border-radius: 18px; }
.cell-piece.diamond { border-radius: 4px; transform: scale(0.98) rotate(45deg); }
.cell-piece.hex {
  clip-path: polygon(25% 4%, 75% 4%, 100% 50%, 75% 96%, 25% 96%, 0% 50%);
  border-radius: 0;
}

.side-tools {
  padding: 20px;
  display: grid;
  gap: 18px;
}

.piece-tray {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--hotbar-gap);
  align-items: stretch;
  width: 100%;
}

.piece-slot {
  display: grid;
  place-items: center;
  min-height: var(--hotbar-slot-size);
  aspect-ratio: 1 / 1;
  border-radius: 24px;
  padding: 10px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  transition: all .18s ease;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  min-width: 64px;
  touch-action: none;
  z-index: 1;
}
.piece-slot:hover {
  transform: translateY(-1px);
  background: rgba(255,255,255,0.07);
  border-color: rgba(255,255,255,0.14);
}
.piece-slot:active {
  transform: scale(0.96);
  background: rgba(110,168,255,0.10);
}
.piece-slot.selected {
  border-color: rgba(110,168,255,0.55);
  box-shadow: 0 0 0 2px rgba(110,168,255,0.12);
  background: rgba(110,168,255,0.12);
}
.piece-slot.used {
  opacity: 0.45;
  cursor: default;
}
.piece-slot.empty-slot {
  opacity: 0.35;
  cursor: default;
}
.piece-slot.empty-slot::after {
  content: 'Waiting...';
  color: rgba(238,243,255,0.55);
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  line-height: 1.3;
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}

.piece-box {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  border-radius: 22px;
  background: transparent;
  border: none;
  padding: 0;
  margin: 0;
  cursor: grab;
  touch-action: none;
}
.piece-box:active { cursor: grabbing; }
.piece-box.dragging { opacity: 0.5; }

/* Rotate animation when tools rotate a tray piece */
.piece-box.rotating {
  animation: piece-rotate 420ms ease-in-out;
}
@keyframes piece-rotate {
  0% { transform: rotate(0deg) scale(1); }
  40% { transform: rotate(120deg) scale(1.03); }
  100% { transform: rotate(360deg) scale(1); }
}
#dragGhost {
  position: fixed;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0;
  transition: opacity .14s ease;
  display: grid;
  place-items: center;
  background: rgba(14,20,39,0.96);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 22px;
  padding: 10px;
  backdrop-filter: blur(10px);
  box-shadow: 0 18px 34px rgba(0,0,0,0.28);
  will-change: transform, opacity;
  /* start off-screen to avoid visible top-left flash while positioning */
  transform: translate(-9999px, -9999px) scale(1);
  width: auto;
  height: auto;
  min-width: var(--hotbar-slot-size);
  min-height: var(--hotbar-slot-size);
}
#dragGhost.active {
  opacity: 1;
}
.ghost-grid {
  display: grid;
  width: fit-content;
  height: fit-content;
  gap: 4px;
  place-items: center;
  --ghost-cell-size: clamp(16px, 4vw, 24px);
}
.ghost-cell {
  width: var(--ghost-cell-size);
  height: var(--ghost-cell-size);
  border-radius: 10px;
  background: rgba(255,255,255,0.08);
  opacity: 0;
  transition: opacity .12s ease, transform .12s ease;
}
.ghost-cell.on {
  opacity: 1;
  background: linear-gradient(145deg, var(--c1, #6ea8ff), var(--c2, #8b7bff));
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.14);
  transform: scale(0.98);
}
.ghost-cell.on.squircle { border-radius: 18px; }
.ghost-cell.on.round,
.ghost-cell.on.pill { border-radius: 999px; }
.ghost-cell.on.diamond { border-radius: 4px; transform: scale(0.98) rotate(45deg); }
.ghost-cell.on.hex {
  clip-path: polygon(25% 4%, 75% 4%, 100% 50%, 75% 96%, 25% 96%, 0% 50%);
  border-radius: 0;
}

.piece-grid {
  display: grid;
  grid-template-columns: repeat(var(--w), 1fr);
  grid-template-rows: repeat(var(--h), 1fr);
  gap: 5px;
  place-items: center;
  width: fit-content;
  margin: 0 auto;
  --piece-cell-size: clamp(12px, 3vw, 18px);
}
.piece-cell {
  width: var(--piece-cell-size);
  height: var(--piece-cell-size);
  border-radius: 5px;
  opacity: 0;
  transition: opacity .12s ease;
}
.piece-cell.on {
  opacity: 1;
  background: linear-gradient(145deg, var(--c1), var(--c2));
  box-shadow: 0 4px 12px rgba(0,0,0,0.22);
}
.piece-cell.on.round { border-radius: 999px; }
.piece-cell.on.diamond { transform: rotate(45deg); border-radius: 4px; }
.piece-cell.on.hex { clip-path: polygon(25% 4%, 75% 4%, 100% 50%, 75% 96%, 25% 96%, 0% 50%); border-radius: 0; }

.tray-caption {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.game-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

.stat-item {
  padding: 12px;
  border-radius: 14px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  text-align: center;
}

.stat-item .label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
  margin-bottom: 4px;
}

.stat-item .value {
  font-size: 18px;
  font-weight: 800;
  color: var(--accent);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(20px) scale(0.92);
  background: rgba(13,18,36,0.95);
  border: 1px solid rgba(255,255,255,0.10);
  color: var(--text);
  padding: 14px 18px;
  border-radius: 999px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.40);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease, transform .22s ease;
  z-index: 50;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.01em;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }

.flying-indicator {
  position: absolute;
  font-size: 24px;
  font-weight: 900;
  color: #ffd700;
  text-shadow: 0 0 10px rgba(255, 215, 0, 0.8), 0 0 20px rgba(255, 215, 0, 0.5);
  pointer-events: none;
  z-index: 1300;
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.5);
  animation: flyUp 0.8s ease-out forwards;
}
@keyframes flyUp {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(0.5);
  }
  20% {
    transform: translate(-50%, -100%) scale(1.2);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -200%) scale(1);
  }
}

/* Gold sparks animation for high score achievement */
.gold-spark {
  position: fixed;
  width: 8px;
  height: 8px;
  background: radial-gradient(circle, #ffd700 0%, #ff8c00 50%, transparent 70%);
  border-radius: 50%;
  pointer-events: none;
  z-index: 2000;
  opacity: 0;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.8), 0 0 20px rgba(255, 215, 0, 0.5);
}

@keyframes sparkFadeIn {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0);
  }
  20% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
  }
}

@keyframes sparkMoveToTarget {
  0% {
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    transform: translate(var(--target-x), var(--target-y)) scale(0.3);
    opacity: 0;
  }
}

.flash-red {
  animation: flashRed 0.3s ease;
}

@keyframes flashRed {
  0%, 100% {
    background: linear-gradient(135deg, #6ea8ff, #8b7bff);
    box-shadow: 0 14px 30px rgba(110,168,255,0.24);
  }
  50% {
    background: linear-gradient(135deg, #ff6b7a, #ff8a5c);
    box-shadow: 0 14px 30px rgba(255,107,122,0.18);
  }
}

.button.flash-red-cooldown {
  animation: flashRedCooldown 1s ease-in-out infinite;
}

@keyframes flashRedCooldown {
  0%, 100% {
    background: linear-gradient(135deg, #6ea8ff, #8b7bff);
    box-shadow: 0 14px 30px rgba(110,168,255,0.24);
  }
  50% {
    background: linear-gradient(135deg, #ff6b7a, #ff8a5c);
    box-shadow: 0 14px 30px rgba(255,107,122,0.18);
  }
}

textarea.flash-red {
  animation: flashRedTextarea 0.3s ease;
}

@keyframes flashRedTextarea {
  0%, 100% {
    border-color: rgba(255,255,255,0.08);
    background: rgba(14,20,39,0.94);
  }
  50% {
    border-color: #ff6b7a;
    background: rgba(255,107,122,0.1);
  }
}

/* Game start flash animation */
.game-start-flash {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0;
  background: transparent;
}

.game-start-flash.animate {
  animation: gameStartFlash 0.9s ease-out forwards;
}

.game-start-flash.animate::before {
  animation: gameStartFlashLine 0.9s ease-out forwards;
}

.game-start-flash::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 60px;
  background: linear-gradient(90deg, transparent, #fff9c4, #ffffff, #fff9c4, transparent);
  transform: rotate(-45deg);
}

@keyframes gameStartFlash {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@keyframes gameStartFlashLine {
  0% {
    top: -50%;
    left: -50%;
  }
  100% {
    top: 150%;
    left: 150%;
  }
}

/* Transition portal for Canvas-based animation */
#transition-portal {
  position: fixed;
  inset: 0;
  z-index: 9999;
  pointer-events: none;
  overflow: hidden;
}

#transition-portal canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

/* Simple fade transition for accessibility fallback */
.simple-fade-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 9999;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.simple-fade-overlay.active {
  opacity: 1;
}

.eye-button {
  position: fixed;
  top: calc(50% + 180px);
  left: 50%;
  transform: translate(-50%, 0);
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.15);
  backdrop-filter: blur(10px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  z-index: 1001;
}
.eye-button.is-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}
.eye-button:active {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* Focus mode for eye button - shows only board and piece tray */
body.focus-mode {
  overflow: hidden;
}
body.focus-mode .topbar,
body.focus-mode .game-header,
body.focus-mode .game-stats,
body.focus-mode .prominent-score,
body.focus-mode .connection-indicator,
body.focus-mode .toast {
  opacity: 0;
  pointer-events: none;
}
body.focus-mode .game-panel {
  padding: 0;
}
body.focus-mode .game-canvas-wrap {
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
body.focus-mode #board {
  border-radius: 0;
  border: none;
  box-shadow: none;
  background: transparent;
}
body.focus-mode .grid-shell {
  gap: 0;
  background: transparent;
  border: none;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
}
body.focus-mode .side-tools {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  opacity: 1;
  pointer-events: auto;
}
body.focus-mode .piece-tray {
  opacity: 0.6;
  pointer-events: none;
}
body.focus-mode .piece-slot {
  cursor: default;
}
body.focus-mode .piece-slot:hover {
  transform: none;
  background: rgba(255,255,255,0.04);
}
body.focus-mode .tray-caption {
  opacity: 0.6;
}
body.focus-mode .powerup-container {
  display: none;
}
body.focus-mode .shell {
  max-width: 100%;
  padding: 0;
}
body.focus-mode #gameScreen {
  padding: 0;
}
body.focus-mode .app {
  padding: 0;
  min-height: 100vh;
  overflow: hidden;
}

/* Dialog fade transition */
.dialog {
  transition: opacity 0.3s ease;
}
.dialog.faded-out {
  opacity: 0;
  pointer-events: none;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 10, 20, 0.70);
  backdrop-filter: blur(16px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 80;
  padding: 18px;
  animation: fadeIn .22s ease;
  transition: opacity 0.3s ease;
}
.overlay.show { display: flex; }
.overlay.faded-out {
  opacity: 0;
  pointer-events: none;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.dialog {
  width: min(580px, 100%);
  padding: 28px;
  border-radius: 28px;
  background: rgba(14,20,39,0.94);
  border: 1px solid rgba(255,255,255,0.08);
  box-shadow: 0 25px 60px rgba(0,0,0,0.35);
  max-height: 90vh;
  overflow-y: auto;
}

.dialog--large {
  width: min(900px, 100%);
  max-height: calc(100vh - 36px);
}
.dialog h2 {
  margin: 0 0 12px;
  font-size: 26px;
  letter-spacing: -0.03em;
  font-weight: 800;
}
.dialog p {
  margin: 0;
  color: var(--muted);
  line-height: 1.8;
  font-size: 15px;
}
.dialog-lead {
  color: var(--muted);
  margin: 0 0 24px;
}
.dialog-lead--compact {
  margin-bottom: 16px;
  font-size: 14px;
}
.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  gap: 16px;
}
.dialog-header h2 { margin: 0; }
.dialog-header .brand { flex: 1; }
.shop-header-center {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-left: auto;
}

.feedback-close-btn {
  width: 48px;
  height: 48px;
  padding: 0;
  flex: 0 0 auto;
}

#closeMoreStatsBtn {
  position: relative;
  width: 48px;
  height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 16px;
  font-weight: 800;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  z-index: 120;
  flex: 0 0 auto;
}

/* Override ghost styling for change username, credits, and more stats close buttons */
#closeChangeUsernameBtn,
#closeCreditsBtn,
#closeMoreStatsBtn {
  background: transparent !important;
  box-shadow: none !important;
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.08);
}
#closeChangeUsernameBtn:hover,
#closeCreditsBtn:hover,
#closeMoreStatsBtn:hover {
  background: rgba(255,255,255,0.08) !important;
}

/* Shop grid in dialog context */
.dialog .shop-grid {
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.stats-credits-btn {
  position: fixed;
  bottom: 20px;
  left: 20px;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

body[data-screen="stats"] .stats-credits-btn {
  opacity: 1;
  pointer-events: auto;
}

body.focus-mode .stats-credits-btn {
  opacity: 0;
  pointer-events: none;
}

/* Shop Screen */
.shop-coins {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(255, 215, 0, 0.15), rgba(255, 193, 7, 0.10));
  border: 1px solid rgba(255, 215, 0, 0.25);
  color: #ffd700;
  font-weight: 800;
  font-size: 14px;
  flex-shrink: 0;
}
.shop-coins .icon {
  width: 20px;
  height: 20px;
}

.shop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.shop-item {
  padding: 20px;
  border-radius: 20px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  transition: all .18s ease;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.shop-item:hover {
  background: rgba(255,255,255,0.06);
  border-color: rgba(255,255,255,0.12);
}

.shop-item-icon {
  width: 32px;
  height: 32px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, rgba(110,168,255,.15), rgba(139,123,255,.12));
  border: 1px solid rgba(255,255,255,0.10);
}
.shop-item-icon .icon {
  width: 18px;
  height: 18px;
  color: var(--accent);
}

.shop-item-name {
  font-size: 16px;
  font-weight: 800;
  color: var(--text);
}

.shop-item-description {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.6;
  flex: 1;
}

.shop-item-price {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 18px;
  font-weight: 900;
  color: #ffd700;
}
.shop-item-price .icon {
  width: 18px;
  height: 18px;
}

.shop-item-owned {
  font-size: 12px;
  color: var(--good);
  font-weight: 700;
  margin-top: 4px;
}

.shop-item-cooldown {
  font-size: 12px;
  color: var(--warn);
  font-weight: 700;
  margin-top: 4px;
}

.shop-item-purchase-btn {
  margin-top: 8px;
}

/* Shop Item Cell in Game Screen */
.shop-item-cell {
  width: 100%;
  aspect-ratio: 1 / 1;
  min-height: var(--hotbar-slot-size);
  border-radius: 24px;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  cursor: pointer;
  position: relative;
  transition: all .18s ease;
  margin-top: 12px;
}
.shop-item-cell:hover {
  background: rgba(255,255,255,0.07);
  border-color: rgba(255,255,255,0.14);
  transform: translateY(-1px);
}
.shop-item-cell:active {
  transform: scale(0.96);
}
.shop-item-cell .icon {
  width: 24px;
  height: 24px;
  color: var(--accent);
}

.shop-item-count {
  position: absolute;
  top: 8px;
  right: 8px;
  background: linear-gradient(135deg, #6ea8ff, #8b7bff);
  color: #fff;
  font-size: 12px;
  font-weight: 900;
  padding: 4px 8px;
  border-radius: 999px;
  min-width: 20px;
  text-align: center;
}

.inventory-item-name-display {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(14, 20, 39, 0.95);
  border: 1px solid rgba(110,168,255,0.5);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: opacity .18s ease, visibility .18s ease;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  pointer-events: none;
}
.inventory-item-name-display.show {
  opacity: 1;
  visibility: visible;
}

/* Powerup Bar (replaces item slide-in) - removed fixed positioning */
.powerup-bar {
  display: none;
}

/* Powerup Container in game screen - thin horizontally scrollable row */
.powerup-container {
  display: flex;
  gap: 10px;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  margin-top: 12px;
  padding: 10px 14px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 16px;
  min-height: 72px;
  align-items: center;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.2) transparent;
  scroll-snap-type: x proximity;
  scroll-padding: 0 4px;
}

/* Tool targeting mode: dim UI and focus board */
.tool-target-mask {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  pointer-events: none;
  z-index: 40;
}

.tool-targeting .shell > .glass,
.tool-targeting .side-panel,
.tool-targeting .hero-actions,
.tool-targeting .side-tools,
.tool-targeting .panel-card,
.tool-targeting .shop-grid,
.tool-targeting .dialog {
  opacity: 0.2;
  transition: opacity .18s ease;
}

.tool-targeting #board,
.tool-targeting .game-panel,
.tool-targeting .grid-shell {
  opacity: 1 !important;
  z-index: 50;
}

/* Keep piece tray visible for rotator, dim for others */
.tool-targeting .piece-tray {
  opacity: 0.3;
  transition: opacity .18s ease;
}

.tool-targeting[data-tool-mode="rotator"] .piece-tray {
  opacity: 1 !important;
}

/* Hide powerup container during targeting */
.tool-targeting .powerup-container {
  display: none !important;
}

/* Unified dialog close button used for overlays and menus */
.dialog-close-btn {
  position: relative;
  width: 48px;
  height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #ff6b7a, #ff8a5c);
  color: #fff;
  border: none;
  border-radius: 16px;
  font-weight: 800;
  font-size: 16px;
  line-height: 1;
  box-shadow: 0 6px 14px rgba(255, 107, 122, 0.28);
  cursor: pointer;
  z-index: 120;
  flex: 0 0 auto;
}
.dialog-close-btn:hover { box-shadow: 0 10px 24px rgba(255, 107, 122, 0.38); }
.dialog-close-btn.ghost:hover { box-shadow: none; }
.dialog .dialog-close-btn { pointer-events: auto; }
.dialog-close-btn .icon { width: 24px; height: 24px; }

.shop-header-center {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-left: auto;
}

.powerup-container::-webkit-scrollbar {
  height: 4px;
}

.powerup-container::-webkit-scrollbar-track {
  background: transparent;
}

.powerup-container::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.2);
  border-radius: 4px;
}

.powerup-icon {
  width: 52px;
  height: 52px;
  min-width: 52px;
  min-height: 52px;
  max-width: 52px;
  max-height: 52px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  cursor: pointer;
  position: relative;
  transition: all .18s ease;
  scroll-snap-align: center;
  flex-shrink: 0;
  touch-action: manipulation;
}
.powerup-icon:hover {
  background: rgba(255,255,255,0.07);
  border-color: rgba(255,255,255,0.14);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.powerup-icon:hover .icon {
  transform: scale(1.1);
  transition: transform .18s ease;
}
.powerup-icon:active {
  transform: scale(0.95);
}
.powerup-icon.disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
  filter: grayscale(100%);
  background: rgba(255,255,255,0.02);
  border-color: rgba(255,255,255,0.04);
}
.powerup-icon.disabled .icon {
  filter: grayscale(100%);
  color: rgba(238, 243, 255, 0.3);
}
.powerup-icon .icon {
  width: 24px;
  height: 24px;
  color: var(--accent);
  transition: transform .18s ease;
}
.powerup-icon .powerup-badge {
  position: absolute;
  bottom: 6px;
  right: 6px;
  background: linear-gradient(135deg, #6ea8ff, #8b7bff);
  color: #fff;
  font-size: 9px;
  font-weight: 800;
  padding: 1px 6px;
  border-radius: 999px;
  min-width: 14px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.powerup-icon .powerup-name-display {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(14, 20, 39, 0.95);
  border: 1px solid rgba(110,168,255,0.5);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: opacity .18s ease, visibility .18s ease;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  pointer-events: none;
}
.powerup-name-display.show {
  opacity: 1;
  visibility: visible;
}
.powerup-icon .powerup-name-tooltip {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(14, 20, 39, 0.95);
  border: 1px solid rgba(255,255,255,0.10);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: opacity .18s ease, visibility .18s ease;
  pointer-events: none;
  z-index: 60;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.powerup-icon:hover .powerup-name-tooltip {
  opacity: 1;
  visibility: visible;
}

/* Active tool state highlighting */
.powerup-icon.active {
  background: linear-gradient(135deg, rgba(110,168,255,0.15), rgba(139,123,255,0.12));
  border-color: rgba(110,168,255,0.6);
  box-shadow: 0 0 0 2px rgba(110,168,255,0.2), 0 4px 12px rgba(110,168,255,0.3);
  animation: tool-pulse 1.5s ease-in-out infinite;
}

@keyframes tool-pulse {
  0%, 100% {
    box-shadow: 0 0 0 2px rgba(110,168,255,0.2), 0 4px 12px rgba(110,168,255,0.3);
  }
  50% {
    box-shadow: 0 0 0 3px rgba(110,168,255,0.4), 0 4px 16px rgba(110,168,255,0.5);
  }
}

/* Cursor changes based on tool mode */
body[data-tool-mode="cleanser"] {
  cursor: crosshair;
}
body[data-tool-mode="cleanser"] .cell:hover {
  cursor: crosshair;
  background: rgba(72,211,154,0.15);
}
body[data-tool-mode="rotator"] {
  cursor: pointer;
}
body[data-tool-mode="rotator"] .piece-slot:hover {
  cursor: pointer;
  background: rgba(110,168,255,0.15);
}

/* Tool mode indicator */
.tool-mode-indicator {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(14,20,39,0.96);
  border: 1px solid rgba(255,255,255,0.12);
  padding: 12px 20px;
  border-radius: 12px;
  display: none;
  align-items: center;
  gap: 12px;
  z-index: 10000;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  backdrop-filter: blur(16px);
}
.tool-mode-indicator.show {
  display: flex;
  animation: indicator-fade-in 0.2s ease-out;
}
@keyframes indicator-fade-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}
.tool-mode-indicator .icon {
  width: 20px;
  height: 20px;
  color: var(--accent);
}
.tool-mode-indicator-text {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}
.tool-mode-indicator-cancel {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  padding: 4px 8px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 6px;
  cursor: pointer;
  transition: all .18s ease;
}
.tool-mode-indicator-cancel:hover {
  background: rgba(255,107,122,0.15);
  border-color: rgba(255,107,122,0.3);
  color: #ff6b7a;
}

/* Tool UI defaults - ensure transient tool UIs are centered and not left-top anchored */
.tool-screen-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 90;
}
.tool-screen-overlay.active {
  pointer-events: none;
  background: rgba(0,0,0,0.5);
}

/* Highlight clickable elements when tool is active */
body[data-tool-mode="rotator"] .piece-slot:not(.used):not(.empty-slot) {
  cursor: pointer;
  animation: pulse-glow 1.5s ease-in-out infinite;
}
body[data-tool-mode="rotator"] .piece-slot:not(.used):not(.empty-slot):hover {
  transform: scale(1.05);
  box-shadow: 0 0 20px rgba(110, 168, 255, 0.8);
}

body[data-tool-mode="cleanser"] .cell:not(.empty) {
  cursor: pointer;
  animation: pulse-glow 1.5s ease-in-out infinite;
}
body[data-tool-mode="cleanser"] .cell:not(.empty):hover {
  transform: scale(1.1);
  box-shadow: 0 0 15px rgba(255, 100, 100, 0.8);
}

@keyframes pulse-glow {
  0%, 100% {
    box-shadow: 0 0 5px rgba(110, 168, 255, 0.5);
  }
  50% {
    box-shadow: 0 0 15px rgba(110, 168, 255, 0.8);
  }
}
.magnetic-ui,
.foresight-preview,
.tool-double-click-message {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 2001;
  background: rgba(14,20,39,0.96);
  border: 1px solid rgba(255,255,255,0.12);
  padding: 16px 20px;
  border-radius: 16px;
  display: none;
  max-width: 90vw;
  box-shadow: 0 20px 50px rgba(0,0,0,0.4);
  backdrop-filter: blur(16px);
}
.magnetic-ui.show,
.foresight-preview.show,
.tool-double-click-message.show { 
  display: block;
  animation: tool-ui-fade-in 0.2s ease-out;
}

@keyframes tool-ui-fade-in {
  from {
    opacity: 0;
    transform: translate(-50%, -45%) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

.magnetic-ui {
  display: flex;
  gap: 12px;
  align-items: center;
}
.magnetic-ui .button {
  min-width: 100px;
}

.foresight-preview {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.preview-piece-container {
  padding: 12px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 12px;
  text-align: center;
}
.preview-piece-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 8px;
}
.preview-piece-shape {
  display: grid;
  gap: 2px;
  justify-content: center;
  margin: 0 auto;
}
.preview-cell {
  width: 16px;
  height: 16px;
  border-radius: 3px;
}
.preview-cell.empty {
  background: rgba(255,255,255,0.02);
}
.preview-cell.filled {
  border: 1px solid rgba(255,255,255,0.1);
}

.tool-double-click-message {
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
  text-align: center;
}

/* Cleanser remove animation */
.cell.cleanser-remove {
  animation: cleanser-remove 0.3s ease-in-out;
}

@keyframes cleanser-remove {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
    background: rgba(72,211,154,0.4);
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

/* Foresight preview enhancements */
.foresight-preview {
  display: none;
  flex-direction: column;
  gap: 16px;
  padding: 24px;
  min-width: 320px;
}

.foresight-header {
  font-size: 16px;
  font-weight: 800;
  color: var(--accent);
  text-align: center;
  margin-bottom: 8px;
}

.foresight-pieces {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.foresight-close {
  margin-top: 8px;
}

/* Magnetic UI enhancements */
.magnetic-ui {
  display: none;
  flex-direction: column;
  gap: 16px;
  padding: 24px;
  min-width: 280px;
}

.magnetic-header {
  font-size: 16px;
  font-weight: 800;
  color: var(--accent);
  text-align: center;
  margin-bottom: 8px;
}

.magnetic-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.mag-cancel {
  margin-top: 8px;
}

/* Mobile responsive for shop */
@media (max-width: 600px) {
  .shop-grid {
    grid-template-columns: 1fr;
  }
  
  .powerup-bar {
    display: none;
  }
  
  .powerup-icon {
    width: 46px;
    height: 46px;
  }
  
  .powerup-container {
    padding: 8px 10px;
    gap: 6px;
    min-height: 56px;
  }
}

.credits-content {
  display: grid;
  gap: 16px;
  margin-top: 8px;
}

.credits-item {
  padding: 16px;
  border-radius: 16px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
}

.credits-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
  text-transform: uppercase;
}

.credits-value {
  font-size: 15px;
  color: var(--text);
  font-weight: 600;
  word-break: break-all;
}
.how-rules {
  margin: 0;
  color: var(--muted);
  line-height: 1.8;
  font-size: 15px;
}
.how-rules dt {
  font-weight: 700;
  color: var(--text);
  margin-top: 12px;
}
.how-rules dt:first-child { margin-top: 0; }
.how-rules dd { margin: 4px 0 0; }
.form-error {
  color: #ff4d6d;
  font-size: 13px;
  display: none;
}
.form-error.is-visible { display: block; }
#usernameError,
#changeUsernameError { margin-top: 8px; }
#feedbackError { margin-bottom: 12px; }
#feedbackCooldown {
  display: none;
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 190, 92, 0.12);
  border: 1px solid rgba(255, 190, 92, 0.28);
  color: var(--warn);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.45;
}
#feedbackCooldown.is-visible { display: block; }
.input-readonly {
  background: rgba(255, 255, 255, 0.04);
  cursor: not-allowed;
}
.label-hint {
  color: var(--muted);
  font-size: 12px;
}
.dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 22px;
  flex-wrap: wrap;
}

#usernameInitOverlay .dialog-actions {
  justify-content: space-between;
}

.dialog-actions--confirm {
  justify-content: center;
  gap: 16px;
}

.dialog-actions--confirm .button {
  min-width: 120px;
}

.how-dialog-actions {
  justify-content: center;
}

.game-over-timed-best {
  display: flex;
  flex-direction: column;
  gap: 8px;
  justify-content: center;
  align-items: center;
  margin-bottom: 18px;
}

.game-over-timed-best-line {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text);
  font-weight: 700;
}

.form-group {
  margin: 16px 0;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text);
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.10);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  transition: all .18s ease;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  background: rgba(255,255,255,0.08);
  border-color: rgba(110,168,255,0.40);
  box-shadow: 0 0 0 3px rgba(110,168,255,0.10);
}

.form-group textarea {
  resize: vertical;
  min-height: 100px;
}

.bottom-note {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
  margin-top: 10px;
  flex-wrap: wrap;
}

.hidden { display: none !important; }

.connection-indicator {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  font-size: 12px;
  font-weight: 700;
  z-index: 100;
  pointer-events: none;
  backdrop-filter: blur(12px);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  animation: pulse 2s infinite;
}

.status-dot.online {
  background: #48d39a;
  box-shadow: 0 0 8px rgba(72,211,154,0.6);
}

.status-dot.offline {
  background: #ff6b7a;
  box-shadow: 0 0 8px rgba(255,107,122,0.6);
}

.status-text {
  color: var(--text);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* Responsive Design with clamp() for flexible scaling */
@media (max-width: 1100px) {
  .layout-start, .grid-shell { grid-template-columns: 1fr; }
  .hero, .side-panel { min-height: auto; }
  .game-canvas-wrap { padding: clamp(12px, 3vw, 18px); }
  #board { width: clamp(280px, 90vw, 540px); height: clamp(280px, 90vw, 540px); }
}
@media (max-width: 768px) {
  .screen { padding: clamp(10px, 2vw, 14px); }
  .app { padding: clamp(10px, 2vw, 16px); }
  .topbar, .game-panel, .side-tools, .hero, .side-panel { padding: clamp(12px, 3vw, 16px); }
  .game-header { flex-direction: column; align-items: flex-start; }
  .game-controls { width: 100%; flex-direction: row; justify-content: space-between; align-items: center; gap: 10px; }
  .game-controls .brand-trophy { flex-shrink: 0; padding: 8px 12px; font-size: 12px; }
  .game-controls .brand-trophy .icon { width: 16px; height: 16px; }
  .score-strip { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; flex-wrap: nowrap; }
  #exitBtn { width: auto; flex-shrink: 0; }
  .hero h1 { font-size: clamp(38px, 6vw, 48px); }
  .button { min-height: clamp(40px, 8vw, 48px); }
  .piece-box { min-height: clamp(90px, 20vw, 110px); }
  .pill-row { width: 100%; flex-direction: column; }
  .pill { width: 100%; justify-content: center; font-size: 14px; padding: 12px 14px; }
  .topbar { flex-wrap: wrap; }
  .dialog { max-height: 85vh; max-width: 95vw; overflow-y: auto; }
  .form-group { margin: clamp(8px, 2vw, 12px) 0; }
  .prominent-score {
    top: 10px;
    padding: 10px 20px;
  }
  .prominent-score .value {
    font-size: 24px;
  }
}
@media (max-width: 640px) {
  .hero h1 { font-size: clamp(26px, 5vw, 42px); }
  .dialog-actions, .hero-actions { width: 100%; }
  .hero-actions .button { width: 100%; }
  .topbar { flex-direction: column; align-items: flex-start; }
  #statsScreen .topbar { flex-direction: row; align-items: center; }
  .score-strip { grid-template-columns: 1fr; }
  .button.icon-square { width: clamp(36px, 10vw, 44px); height: clamp(36px, 10vw, 44px); border-radius: 14px; padding: 0; }
  .piece-box { min-height: clamp(90px, 20vw, 100px); padding: 8px; }
  .piece-grid { gap: 4px; }
  .piece-tray { gap: clamp(6px, 2vw, 10px); }
  .score-chip { padding: 10px 12px; font-size: 12px; }
  .dialog { padding: clamp(16px, 4vw, 24px); max-height: 90vh; max-width: 90vw; }
  .dialog h2 { font-size: clamp(16px, 4vw, 22px); }
  .form-group label { font-size: 13px; margin-bottom: 6px; }
  .form-group input, .form-group textarea { padding: 10px; font-size: 14px; }
  .form-group textarea { min-height: 80px; }
  .prominent-score {
    top: 8px;
    padding: 8px 16px;
  }
  .prominent-score .value {
    font-size: 20px;
  }
}

/* Mobile-first menu revamp for phones */
@media (max-width: 480px) {
  html, body { overflow-x: hidden; }
  .app { padding: max(8px, env(safe-area-inset-left)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)); }
  .screen { padding: 8px; max-width: 100%; overflow-x: hidden; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .shell { gap: 12px; max-width: 100%; min-width: 0; }
  #startScreen .layout-start,
  #startScreen .hero,
  #startScreen .side-panel,
  #startScreen .topbar {
    max-width: 100%;
    min-width: 0;
  }
  
  .topbar { 
    flex-wrap: wrap; 
    padding: 10px; 
    gap: 8px; 
    align-items: flex-start;
    width: 100%;
    border-radius: 20px;
  }
  .brand { gap: 8px; flex: 1; min-width: 0; }
  .brand-badge { width: 36px; height: 36px; flex: 0 0 auto; border-radius: 12px; }
  .brand-title { font-size: 14px; font-weight: 800; }
  .brand-subtitle { font-size: 10px; }
  
  .pill-row { 
    width: 100%; 
    flex-direction: row; 
    justify-content: space-between; 
    align-items: center; 
    gap: 6px; 
    margin-top: 6px;
  }
  .button.icon-square { width: 36px; height: 36px; border-radius: 10px; flex: 0 0 auto; } 
  .pill { flex: 1; font-size: 11px; padding: 8px 10px; border-radius: 20px; gap: 4px; }
  .pill .icon { width: 16px; height: 16px; }
  
  .layout-start { 
    grid-template-columns: 1fr; 
    gap: 10px;
    border-radius: 20px;
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
  }
  .hero { 
    padding: 14px; 
    min-height: auto; 
    justify-content: flex-start;
    gap: 12px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 20px;
  }
  .hero::after { display: none; }
  .hero h1 {
    font-size: 26px;
    line-height: 1.15;
    margin: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
    max-width: 100%;
  }
  .hero p { font-size: 12px; margin: 4px 0 0; }
  .kicker { font-size: 11px; margin-bottom: 6px; padding: 6px 10px; }
  
  .hero-copy { max-width: 100%; }
  .hero-actions { 
    margin-top: 10px; 
    gap: 6px; 
    width: 100%;
    flex-direction: column;
  }
  .hero-actions .button { 
    min-height: 40px; 
    width: 100%;
    font-size: 13px;
    padding: 10px 14px;
  }
  
  /* Enhanced Player Stats for mobile */
  .username-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
  }

  .username-text .username-label {
    font-size: 10px;
    margin-bottom: 3px;
  }

  .username-text .username-value {
    font-size: 14px;
  }

  .stats-grid { 
    grid-template-columns: repeat(3, minmax(0, 1fr)); 
    gap: 6px; 
    margin-top: 10px; 
    width: 100%;
  }
  .stat { 
    text-align: center; 
    padding: 10px 6px;
    min-width: 0;
    border-radius: 14px;
    background: rgba(110,168,255,0.08);
    border: 1px solid rgba(110,168,255,0.15);
  }
  .stat:hover {
    background: rgba(110,168,255,0.12);
    border-color: rgba(110,168,255,0.25);
  }
  .stat .label { font-size: 10px; font-weight: 600; letter-spacing: 0.02em; }
  .stat .value { font-size: 16px; margin-top: 4px; }
  
  #usernameDisplay { 
    margin-bottom: 8px; 
    font-size: clamp(13px, 3.5vw, 14px); 
    margin-top: 4px;
    font-weight: 800;
  }
  
  .side-panel { 
    padding: 14px; 
    gap: 10px; 
    min-height: auto; 
    max-height: none;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 20px;
    align-content: start;
  }
  .panel-card { 
    padding: 12px;
    border-radius: 16px;
    background: rgba(255,255,255,0.03);
  }
  .panel-title { font-size: 13px; margin-bottom: 10px; }
  .leaderboard { max-height: 220px; gap: 8px; }
  .leader-row { padding: 10px 10px; border-radius: 12px; gap: 10px; }
  .rank-badge { width: 30px; height: 30px; font-size: 12px; border-radius: 8px; }
  .leader-name { font-size: 13px; }
  .leader-score { font-size: 14px; }
  
  #feedbackBtn {
    width: 100%;
    font-size: 12px;
    padding: 10px 14px;
    min-height: 40px;
  }
  
  .dialog { 
    padding: 18px; 
    max-height: 85vh; 
    max-width: 92vw; 
    border-radius: 20px;
  }
  .dialog h2 { 
    font-size: 18px; 
    margin: 0 0 10px;
  }
  .dialog p,
  .how-rules { 
    font-size: 13px;
    line-height: 1.6;
  }
  .form-group { margin: 10px 0; }
  .form-group label { font-size: 12px; margin-bottom: 6px; }
  .form-group input, .form-group textarea { padding: 10px; font-size: 13px; }
  .form-group textarea { min-height: 80px; }
  .dialog-actions { 
    gap: 8px; 
    margin-top: 16px;
    flex-direction: column;
  }
  .dialog-actions .button { 
    width: 100%;
    min-height: 40px;
    font-size: 13px;
    padding: 10px;
  }
  
  .prominent-score {
    top: 6px;
    padding: 6px 14px;
  }
  .prominent-score .value {
    font-size: 18px;
  }
}

/* In-game compact layout (phones / narrow viewports) */
html.compact-game-menu {
  --hotbar-gap: 8px;
}
html.compact-game-menu .app,
html.compact-game-menu .screen,
html.compact-game-menu .shell,
html.compact-game-menu .grid-shell,
html.compact-game-menu .game-panel,
html.compact-game-menu .side-tools {
  max-width: 100%;
  min-width: 0;
  overflow-x: hidden;
}
html.compact-game-menu .screen {
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}
html.compact-game-menu .grid-shell {
  grid-template-columns: 1fr;
  gap: 12px;
}
html.compact-game-menu .game-panel,
html.compact-game-menu .side-tools {
  padding: 12px;
}
html.compact-game-menu .game-canvas-wrap {
  padding: 8px;
  width: 100%;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}
html.compact-game-menu #board {
  width: min(100%, var(--grid-size));
  max-width: calc(100vw - 40px);
  margin: 0 auto;
}
html.compact-game-menu .game-header {
  gap: 10px;
}
html.compact-game-menu .score-value {
  font-size: clamp(24px, 7vw, 32px);
}
html.compact-game-menu .piece-slot {
  min-height: var(--hotbar-slot-size);
  padding: 8px;
  border-radius: 18px;
}
html.compact-game-menu .game-stats {
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* Touch hint: subtle pulse on board cells when piece is selected + touch device */
@media (pointer: coarse) {
  #board.has-selection .cell:not(.filled) {
    animation: cell-breathe 1.8s ease-in-out infinite;
  }
  @keyframes cell-breathe {
    0%, 100% { background: rgba(255,255,255,0.03); }
    50% { background: rgba(110,168,255,0.07); }
  }
  /* Active touch feedback on board cells */
  #board .cell:active {
    background: rgba(110,168,255,0.15) !important;
    transform: scale(0.95);
  }
}

/* Game over coins display with explosion animation */
.game-over-coins {
  display: none;
  text-align: center;
  font-size: 32px;
  font-weight: 900;
  color: #ffd700;
  text-shadow: 0 0 20px rgba(255, 215, 0, 0.8), 0 0 40px rgba(255, 215, 0, 0.5), 0 0 60px rgba(255, 215, 0, 0.3);
  margin: 20px 0;
  transform: rotate(-5deg) scale(0);
  opacity: 0;
  letter-spacing: 0.02em;
}

.game-over-coins.show {
  display: block;
  animation: coinsExplosion 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes coinsExplosion {
  0% {
    transform: rotate(-5deg) scale(0);
    opacity: 0;
  }
  50% {
    transform: rotate(-5deg) scale(1.3);
    opacity: 1;
  }
  100% {
    transform: rotate(-5deg) scale(1);
    opacity: 1;
  }
}

.game-over-timed-best {
  display: none;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
  width: 100%;
  max-width: 580px;
  align-items: center;
  position: relative;
  z-index: 85;
}

.game-over-timed-best-line {
  display: none;
  text-align: center;
  font-size: 28px;
  font-weight: 900;
  color: #ffb703;
  text-shadow: 0 0 18px rgba(255, 183, 3, 0.65), 0 0 30px rgba(255, 148, 0, 0.35);
  margin: 0 auto;
  letter-spacing: 0.04em;
  transform: scale(0.8);
  opacity: 0;
}

.game-over-timed-best-line.show {
  display: block;
  animation: timedBestExplosion 0.45s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes timedBestExplosion {
  0% {
    transform: scale(0.75);
    opacity: 0;
  }
  60% {
    transform: scale(1.18);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* Landscape mode optimizations for phones */
@media (max-width: 480px) and (orientation: landscape) {
  .app { padding: 8px; }
  .screen { padding: 8px; }
  .topbar { padding: 8px; gap: 6px; }
  .brand-badge { width: 32px; height: 32px; }
  .brand-title { font-size: 12px; }
  .hero { padding: 10px; }
  .hero h1 { font-size: 20px; }
  .hero p { font-size: 11px; }
  .hero-actions { margin-top: 8px; gap: 4px; }
  .hero-actions .button { min-height: 36px; font-size: 12px; }
  .stats-grid { gap: 8px; margin-top: 10px; }
  .stat { padding: 8px; }
  .stat .label { font-size: 10px; }
  .stat .value { font-size: 16px; }
  .side-panel { max-height: 280px; padding: 10px; }
  .game-over-coins { font-size: 24px; margin: 15px 0; }
}
