/* Base & Design Tokens — Dez' Nutz Canopy Arcade */
:root {
  --font-display: 'Clash Display', 'Trebuchet MS', sans-serif;
  --font-body: 'Satoshi', 'Segoe UI', sans-serif;

  /* Woodland Twilight + Autumn Gold Palette */
  --color-bg: #131c18;
  --color-surface: rgba(18, 28, 22, 0.88);
  --color-surface-solid: #18251e;
  --color-surface-2: rgba(34, 48, 39, 0.92);
  --color-border: rgba(244, 197, 110, 0.25);
  --color-text: #f7f1e3;
  --color-text-muted: #c8beaa;

  /* Player & Semantic Accents */
  --color-p1: #f06a2b;
  --color-p2: #4ecdc4;
  --color-gold: #ffd166;
  --color-primary: #f29d38;
  --color-danger: #ef476f;
  --color-success: #80ed99;

  /* Fluid Type Scale */
  --text-xs: clamp(0.75rem, 0.72rem + 0.14vw, 0.84rem);
  --text-sm: clamp(0.875rem, 0.84rem + 0.16vw, 0.95rem);
  --text-base: clamp(1rem, 0.96rem + 0.18vw, 1.06rem);
  --text-lg: clamp(1.12rem, 1.04rem + 0.4vw, 1.35rem);
  --text-xl: clamp(1.55rem, 1.25rem + 1.15vw, 2.2rem);
  --text-2xl: clamp(2.25rem, 1.65rem + 2.5vw, 3.85rem);

  /* Spacing (4px base) */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;

  --panel-radius: 12px;
  --panel-blur: 14px;
  --transition-ui: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: var(--app-w, 100%);
  height: var(--app-h, 100dvh);
  overflow: hidden;
  background-color: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  touch-action: none;
}

input {
  font: inherit;
  color: var(--color-text);
  user-select: text;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  transition:
    transform var(--transition-ui),
    background-color var(--transition-ui),
    border-color var(--transition-ui),
    box-shadow var(--transition-ui),
    color var(--transition-ui);
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--color-gold);
  outline-offset: 2px;
}

.hidden {
  display: none !important;
}

/* Main Stage */
.stage {
  position: relative;
  width: var(--app-w, 100vw);
  height: var(--app-h, 100dvh);
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 35%, #233329 0%, #101713 100%);
  overflow: hidden;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
}

/* Shared Game Panel */
.game-panel {
  background: var(--color-surface);
  backdrop-filter: blur(var(--panel-blur));
  -webkit-backdrop-filter: blur(var(--panel-blur));
  border: 1px solid var(--color-border);
  border-radius: var(--panel-radius);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.45);
}

/* Top HUD */
.hud {
  position: fixed;
  top: var(--space-3);
  left: 50%;
  transform: translateX(-50%);
  width: min(94vw, 920px);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--space-3);
  z-index: 20;
  pointer-events: none;
}

.hud-player {
  background: rgba(16, 24, 19, 0.84);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(244, 197, 110, 0.24);
  border-radius: 10px;
  padding: var(--space-2) var(--space-3);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

.hud-name-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
}

.hud-name {
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--color-text);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.75);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 18ch;
}

.hud-score {
  font-size: var(--text-sm);
  font-weight: 700;
  font-variant-numeric: tabular-nums lining-nums;
  color: var(--color-gold);
  margin-left: auto;
  flex-shrink: 0;
}

.hud-p2 .hud-score {
  margin-left: 0;
  margin-right: auto;
}

.player-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  flex-shrink: 0;
}

.p1-dot {
  background: var(--color-p1);
  box-shadow: 0 0 8px var(--color-p1);
}

.p2-dot {
  background: var(--color-p2);
  box-shadow: 0 0 8px var(--color-p2);
}

.meter-track {
  position: relative;
  height: 14px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.meter-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  transition: width 80ms linear;
}

.p1-rage {
  background: linear-gradient(90deg, #e07a22, #ff3d5a);
}

.p2-rage {
  background: linear-gradient(90deg, #2fb7ae, #ff477e);
}

.meter-fill.craze-active {
  animation: pulse-bar 260ms infinite alternate;
}

@keyframes pulse-bar {
  from { filter: brightness(1); }
  to { filter: brightness(1.45); }
}

.meter-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
}

.hud-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  pointer-events: auto;
}

.hud-meta-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hud-level-pill {
  font-size: var(--text-xs);
  font-weight: 800;
  letter-spacing: 0.08em;
  background: linear-gradient(180deg, rgba(255, 198, 92, 0.24) 0%, rgba(214, 128, 34, 0.2) 100%);
  border: 1px solid rgba(255, 209, 102, 0.55);
  padding: 4px 9px;
  border-radius: 999px;
  color: #ffd166;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.85);
  white-space: nowrap;
}

.hud-clock {
  font-size: var(--text-sm);
  font-weight: 700;
  font-variant-numeric: tabular-nums lining-nums;
  background: rgba(16, 24, 19, 0.88);
  border: 1px solid rgba(244, 197, 110, 0.28);
  padding: 4px 12px;
  border-radius: 999px;
  color: var(--color-text);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.hud-actions {
  display: flex;
  gap: var(--space-2);
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(16, 24, 19, 0.88);
  border: 1px solid rgba(244, 197, 110, 0.26);
  color: var(--color-text);
}

.icon-btn:hover {
  background: rgba(42, 60, 48, 0.95);
  border-color: var(--color-gold);
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3);
}

.title-overlay {
  background: #101713;
  overflow-y: auto;
}

.title-bg-img {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.7;
  filter: saturate(1.08);
  pointer-events: none;
}

.title-scrim {
  position: fixed;
  inset: 0;
  background: radial-gradient(
    ellipse at 50% 42%,
    rgba(16, 23, 19, 0.44) 0%,
    rgba(12, 18, 15, 0.88) 76%
  );
  pointer-events: none;
}

.title-content {
  position: relative;
  z-index: 2;
  width: min(100%, 800px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  margin: auto;
}

.title-header {
  text-align: center;
  width: min(calc(100vw - 20px), 1080px);
}

.eyebrow {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.16em;
  color: var(--color-gold);
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.85);
  margin-bottom: clamp(18px, 2.8vw, 34px);
}

.game-title {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  position: relative;
  font-family: 'Bangers', var(--font-display), Impact, sans-serif;
  font-size: clamp(3.6rem, 9.8vw, 8.2rem);
  font-weight: 400;
  line-height: 0.88;
  letter-spacing: 0.04em;
  margin: 4px auto 4px;
  user-select: none;
  filter: drop-shadow(0 12px 26px rgba(0, 0, 0, 0.84));
}

.title-word {
  display: inline-flex;
  align-items: baseline;
  position: relative;
  z-index: 2;
}

.title-word .tl {
  display: inline-block;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-stroke: clamp(1.8px, 0.34vw, 3.2px) #121f08;
  paint-order: stroke fill;
}

.title-squirrel {
  transform: rotate(-3deg) translateY(2px);
  padding-right: clamp(6px, 1vw, 14px);
  filter:
    drop-shadow(0 4px 0 #162b0b)
    drop-shadow(0 0 14px rgba(255, 232, 36, 0.38));
}

.title-squirrel .tl {
  background-image: linear-gradient(180deg, #fffd82 0%, #ffe119 46%, #f2a200 100%);
}

.title-squirrel .tl:nth-child(1) { transform: rotate(-4deg) scale(1.06); }
.title-squirrel .tl:nth-child(2) { transform: rotate(2deg) translateY(-2px); }
.title-squirrel .tl:nth-child(3) { transform: rotate(-2deg); }
.title-squirrel .tl:nth-child(4) { transform: rotate(3deg) translateY(-1px); }
.title-squirrel .tl:nth-child(5) { transform: rotate(-3deg) scale(1.03); }
.title-squirrel .tl:nth-child(6) { transform: rotate(2deg) translateY(-2px); }
.title-squirrel .tl:nth-child(7) { transform: rotate(-2deg); }
.title-squirrel .tl:nth-child(8) { transform: rotate(4deg) translateY(1px); }

.title-crazy {
  transform: rotate(3.5deg) translateY(-1px);
  padding-left: clamp(6px, 1vw, 14px);
  filter:
    drop-shadow(0 4px 0 #102409)
    drop-shadow(0 0 18px rgba(132, 255, 24, 0.5));
}

.title-crazy .tl {
  background-image: linear-gradient(180deg, #f2ff52 0%, #86ff1a 46%, #1ec422 100%);
}

.title-crazy .tl:nth-child(1) { transform: rotate(-7deg) scale(1.06) translateY(-2px); }
.title-crazy .tl:nth-child(2) { transform: rotate(6deg) scale(0.98) translateY(3px); }
.title-crazy .tl:nth-child(3) { transform: rotate(-5deg) scale(1.1) translateY(-3px); }
.title-crazy .tl:nth-child(4) { transform: rotate(8deg) scale(1.02) translateY(2px); }
.title-crazy .tl:nth-child(5) { transform: rotate(-6deg) scale(1.14) translateY(-3px); }

.title-mascot-burst {
  position: relative;
  z-index: 4;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: clamp(132px, 22vw, 285px);
  margin: -32px clamp(-28px, -2.5vw, -12px) -24px;
  pointer-events: none;
  flex-shrink: 0;
}

.title-burst-glow {
  position: absolute;
  inset: 6% 4%;
  border-radius: 50%;
  background: radial-gradient(
    circle,
    rgba(246, 255, 68, 0.52) 0%,
    rgba(118, 255, 28, 0.34) 44%,
    rgba(18, 36, 10, 0) 74%
  );
  filter: blur(6px);
  z-index: 0;
}

.title-mascot-img {
  position: relative;
  z-index: 1;
  width: 100%;
  height: auto;
  display: block;
  transform-origin: 50% 62%;
  filter:
    drop-shadow(0 5px 10px rgba(0, 0, 0, 0.72))
    drop-shadow(0 0 10px rgba(208, 255, 42, 0.38));
  animation: manic-squirrel-pop 2.6s ease-in-out infinite;
}

@keyframes manic-squirrel-pop {
  0%, 100% { transform: scale(1) rotate(-2deg) translateY(0); }
  22% { transform: scale(1.06) rotate(3.5deg) translateY(-3px); }
  38% { transform: scale(1.02) rotate(-3.5deg) translateY(-1px); }
  55% { transform: scale(1.07) rotate(2deg) translateY(-4px); }
  75% { transform: scale(1) rotate(-1.5deg) translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .title-mascot-img { animation: none; }
}

@media (max-width: 640px) {
  .game-title {
    font-size: clamp(2.55rem, 11.4vw, 3.6rem);
    margin: 4px auto 2px;
  }
  .title-mascot-burst {
    width: clamp(96px, 28vw, 132px);
    margin: -16px -12px -12px;
  }
}

.subtitle {
  margin-top: clamp(6px, 1vw, 12px);
  font-size: var(--text-sm);
  color: #efe7d6;
  max-width: 58ch;
  margin-inline: auto;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.85);
}

.setup-panel {
  width: 100%;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* Profile & Ranking Bar */
.profile-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.handle-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1 1 210px;
}

.handle-input-row {
  display: flex;
  gap: var(--space-2);
}

.text-input {
  width: 100%;
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.42);
  border: 1px solid rgba(255, 255, 255, 0.18);
  font-size: var(--text-sm);
  color: var(--color-text);
}

.text-input::placeholder {
  color: rgba(200, 190, 170, 0.55);
}

.mini-btn {
  padding: 7px 12px;
  border-radius: 8px;
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: var(--color-text);
  white-space: nowrap;
}

.mini-btn:hover {
  background: rgba(255, 255, 255, 0.18);
  border-color: var(--color-gold);
}

.rank-pill {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px var(--space-2);
  background: rgba(0, 0, 0, 0.42);
  border: 1px solid rgba(244, 197, 110, 0.28);
  border-radius: 999px;
  padding: 6px 12px;
}

.saved-handle-badge {
  font-size: var(--text-xs);
  font-weight: 800;
  color: #fff7e6;
  letter-spacing: 0.02em;
}

.rank-sep {
  font-size: var(--text-xs);
  color: rgba(200, 190, 170, 0.5);
}

.rank-num-badge {
  font-size: var(--text-xs);
  font-weight: 800;
  font-variant-numeric: tabular-nums lining-nums;
  color: var(--color-gold);
  background: rgba(255, 209, 102, 0.16);
  border: 1px solid rgba(255, 209, 102, 0.42);
  border-radius: 999px;
  padding: 1px 7px;
  line-height: 1.3;
}

.tier-badge {
  font-size: var(--text-xs);
  font-weight: 700;
  color: var(--color-gold);
}

.elo-readout {
  font-size: var(--text-xs);
  font-weight: 700;
  font-variant-numeric: tabular-nums lining-nums;
  color: var(--color-text);
}

.rankings-btn {
  padding: 8px 14px;
  border-radius: 8px;
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 700;
  letter-spacing: 0.05em;
  background: rgba(255, 209, 102, 0.16);
  border: 1px solid rgba(255, 209, 102, 0.45);
  color: var(--color-gold);
}

.rankings-btn:hover {
  background: rgba(255, 209, 102, 0.26);
}

.top10-dropdown {
  background: rgba(0, 0, 0, 0.34);
  border: 1px solid rgba(244, 197, 110, 0.24);
  border-radius: 10px;
  overflow: hidden;
}

.top10-summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s ease;
}

.top10-summary::-webkit-details-marker {
  display: none;
}

.top10-summary:hover {
  background: rgba(255, 209, 102, 0.08);
}

.top10-title {
  font-family: var(--font-display);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--color-gold);
  white-space: nowrap;
}

.top10-leader-preview {
  flex: 1 1 auto;
  text-align: right;
  font-size: var(--text-xs);
  color: rgba(247, 241, 227, 0.8);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top10-chevron {
  font-size: 0.9rem;
  color: var(--color-gold);
  line-height: 1;
  transition: transform 0.18s ease;
}

.top10-dropdown[open] .top10-chevron {
  transform: rotate(180deg);
}

.top10-menu {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  max-height: 250px;
  overflow-y: auto;
  padding: 6px;
}

.top10-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.top10-row {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.04);
  font-size: var(--text-xs);
}

.top10-row.is-me {
  background: rgba(255, 209, 102, 0.16);
  border: 1px solid rgba(255, 209, 102, 0.38);
}

.top10-rank {
  font-weight: 800;
  font-variant-numeric: tabular-nums lining-nums;
  text-align: center;
  border-radius: 999px;
  padding: 1px 6px;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(247, 241, 227, 0.85);
}

.top10-rank.rank-1 {
  background: rgba(255, 209, 102, 0.25);
  color: #ffd166;
}

.top10-rank.rank-2 {
  background: rgba(210, 220, 232, 0.22);
  color: #e2e8f0;
}

.top10-rank.rank-3 {
  background: rgba(224, 152, 92, 0.24);
  color: #f4a261;
}

.top10-name-wrap {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.top10-name {
  font-weight: 700;
  color: #fff7e6;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top10-tier {
  font-size: 0.68rem;
  font-weight: 700;
  opacity: 0.92;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top10-record {
  font-size: 0.7rem;
  color: rgba(200, 190, 170, 0.82);
  font-variant-numeric: tabular-nums lining-nums;
  white-space: nowrap;
}

.top10-elo {
  font-weight: 800;
  color: var(--color-gold);
  font-variant-numeric: tabular-nums lining-nums;
  white-space: nowrap;
}

.top10-empty {
  padding: 10px 8px;
  text-align: center;
  font-size: var(--text-xs);
  color: rgba(200, 190, 170, 0.78);
}

.setup-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  justify-content: space-between;
  align-items: stretch;
}

.option-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1 1 280px;
}

.option-label {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--color-text-muted);
}

.setup-row .option-label {
  font-size: 0.88rem;
  font-weight: 800;
  color: #f4d58d;
}

.segmented {
  display: flex;
  gap: var(--space-1);
  background: rgba(0, 0, 0, 0.35);
  padding: 4px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.setup-row .segmented {
  gap: 6px;
  padding: 6px;
  border-radius: 13px;
  border: 1px solid rgba(255, 255, 255, 0.16);
}

.seg-btn {
  flex: 1;
  padding: 7px 8px;
  font-size: var(--text-sm);
  font-weight: 700;
  border-radius: 7px;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.setup-row .seg-btn {
  min-height: 48px;
  padding: 12px 10px;
  font-size: 1rem;
  font-weight: 800;
  border-radius: 9px;
}

@media (max-width: 420px) {
  .setup-row .seg-btn {
    min-height: 48px;
    padding: 12px 6px;
    font-size: 0.94rem;
  }
}

.seg-btn:hover {
  color: var(--color-text);
  background: rgba(255, 255, 255, 0.06);
}

.seg-btn.active {
  background: var(--color-primary);
  color: #1b1108;
  box-shadow: 0 2px 10px rgba(242, 157, 56, 0.35);
}

.ai-difficulty-row {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(10, 15, 12, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.segmented-4 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  width: 100%;
}

.segmented-4 .seg-btn {
  padding: 7px 8px;
}

.seg-btn-epic.active {
  background: linear-gradient(135deg, #ff4d6d 0%, #ff9f1c 55%, #ffd166 100%);
  color: #1b0a05;
  font-weight: 800;
  box-shadow: 0 0 16px rgba(255, 77, 109, 0.55);
}

.level-select-row {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(10, 15, 12, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.level-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.level-name-tag {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.05em;
  color: #ffd166;
  text-align: right;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 5px;
  width: 100%;
}

.lvl-btn {
  position: relative;
  padding: 7px 4px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(20, 30, 24, 0.85);
  color: var(--color-text-muted);
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.lvl-btn:hover {
  border-color: rgba(255, 209, 102, 0.5);
  color: var(--color-text);
}

.lvl-btn.cleared {
  border-color: rgba(132, 224, 150, 0.55);
  color: #c8f7d2;
}

.lvl-btn.cleared::after {
  content: '★';
  position: absolute;
  top: 2px;
  right: 3px;
  font-size: 8px;
  color: #ffd166;
  line-height: 1;
}

.lvl-btn.active {
  background: var(--color-primary);
  border-color: #ffe2a4;
  color: #1b1108;
  box-shadow: 0 2px 10px rgba(242, 157, 56, 0.4);
}

.lvl-btn.active.cleared::after {
  color: #1b1108;
}

.level-meta-note {
  margin: 0;
  font-size: 11px;
  line-height: 1.3;
  color: var(--color-text-muted);
  text-align: center;
}

@media (max-width: 600px) {
  .level-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .lvl-btn {
    padding: 8px 4px;
  }
}

.primary-cta {
  width: 100%;
  padding: 12px 18px;
  border-radius: 10px;
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: 0.05em;
  background: linear-gradient(180deg, #ffba52 0%, #e77d24 100%);
  color: #1c1006;
  border: 1px solid #ffd894;
  box-shadow: 0 6px 22px rgba(231, 125, 36, 0.42);
}

.primary-cta:hover {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

.primary-cta:active {
  transform: translateY(1px);
}

.secondary-btn {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: var(--text-sm);
  font-weight: 700;
  letter-spacing: 0.05em;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: var(--color-text);
}

.secondary-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: var(--color-gold);
}

/* Online Lobby Panel */
.online-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.online-actions {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1.2fr;
  gap: var(--space-2);
  align-items: stretch;
}

.join-row {
  display: flex;
  gap: var(--space-1);
}

.code-input {
  width: 82px;
  text-align: center;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.14em;
}

.join-btn {
  flex: 1;
  white-space: nowrap;
}

.lobby-status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: 8px;
  background: rgba(78, 205, 196, 0.14);
  border: 1px solid rgba(78, 205, 196, 0.4);
  font-size: var(--text-sm);
}

.room-code-badge {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  letter-spacing: 0.16em;
  color: var(--color-gold);
  background: rgba(0, 0, 0, 0.45);
  padding: 2px 10px;
  border-radius: 6px;
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
}

.control-card {
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: var(--space-3);
}

.p1-card {
  border-color: rgba(240, 106, 43, 0.38);
}

.p2-card {
  border-color: rgba(78, 205, 196, 0.38);
}

.control-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  font-weight: 700;
  margin-bottom: var(--space-1);
  color: var(--color-text);
}

.control-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  margin-right: 3px;
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 700;
  color: #fff6e5;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 5px;
}

/* Modal Overlays (Pause / Victory / Rankings) */
.modal-overlay {
  background: rgba(8, 12, 10, 0.72);
  backdrop-filter: blur(6px);
}

.modal-card {
  width: min(100%, 480px);
  padding: var(--space-5);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.rankings-card {
  width: min(100%, 680px);
  max-height: 86dvh;
  text-align: left;
}

.modal-head-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}

.rankings-scroll {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding-right: 4px;
}

.rank-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums lining-nums;
}

.rank-table th,
.rank-table td {
  padding: 8px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  text-align: left;
}

.rank-table th {
  color: var(--color-text-muted);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.rank-table tr.is-me {
  background: rgba(242, 157, 56, 0.16);
}

.table-empty {
  text-align: center;
  color: var(--color-text-muted);
  padding: var(--space-3);
}

.recent-title {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.recent-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: var(--text-xs);
}

.recent-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-2);
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.elo-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(255, 209, 102, 0.14);
  border: 1px solid rgba(255, 209, 102, 0.42);
  font-size: var(--text-sm);
}

.modal-title {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  color: #fff6e5;
  letter-spacing: 0.03em;
}

.modal-sub {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-3);
  text-align: left;
}

.stat-col {
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  font-weight: 700;
  margin-bottom: 2px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums lining-nums;
}

.stat-row strong {
  color: var(--color-gold);
}

.victory-card {
  max-height: 92dvh;
  overflow-y: auto;
}

.rematch-status {
  margin: 0 0 12px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(255, 209, 102, 0.12);
  border: 1px solid rgba(255, 209, 102, 0.34);
  color: var(--color-gold);
  font-size: var(--text-sm);
  font-weight: 600;
  text-align: center;
}

.rematch-status.is-left {
  background: rgba(239, 71, 111, 0.14);
  border-color: rgba(239, 71, 111, 0.4);
  color: #ff8fa8;
}

.btn-rematch-ready {
  box-shadow: 0 0 0 2px rgba(128, 237, 153, 0.6), 0 8px 20px rgba(128, 237, 153, 0.28) !important;
}

.modal-actions button:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

/* Ergonomic Spaced Touch Controls */
.touch-controls {
  position: fixed;
  bottom: max(18px, calc(env(safe-area-inset-bottom, 0px) + 10px));
  left: max(16px, calc(env(safe-area-inset-left, 0px) + 10px));
  right: max(16px, calc(env(safe-area-inset-right, 0px) + 10px));
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  z-index: 25;
  user-select: none;
  -webkit-user-select: none;
}

.tc-move-pad,
.tc-action-pad {
  pointer-events: none;
  display: flex;
  flex-direction: column;
  touch-action: none;
}

.tc-move-pad {
  align-items: center;
}

.tc-dpad-grid {
  display: grid;
  grid-template-columns: 52px 46px 52px;
  grid-template-rows: 50px 50px 50px;
  grid-template-areas:
    ". up ."
    "left . right"
    ". down .";
  gap: 8px;
  align-items: center;
  justify-items: center;
}

.tc-up { grid-area: up; }
.tc-left { grid-area: left; }
.tc-right { grid-area: right; }
.tc-down { grid-area: down; }

.tc-btn {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 60ms ease, background 60ms ease, box-shadow 60ms ease;
}

.tc-dir-btn {
  width: 52px;
  height: 50px;
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(48, 54, 60, 0.92) 0%, rgba(24, 28, 32, 0.94) 100%);
  color: #f4f1e6;
  font-size: 18px;
  font-weight: 700;
  border: 1.5px solid rgba(255, 255, 255, 0.24);
  box-shadow: 0 5px 0 rgba(10, 12, 14, 0.92), 0 8px 16px rgba(0, 0, 0, 0.52), inset 0 1px 2px rgba(255, 255, 255, 0.24);
}

.tc-dir-btn:active,
.tc-dir-btn.is-pressed {
  background: linear-gradient(180deg, rgba(30, 34, 38, 0.98) 0%, rgba(44, 50, 56, 0.98) 100%);
  color: var(--color-gold);
  border-color: rgba(255, 209, 102, 0.65);
  box-shadow: 0 1px 0 rgba(10, 12, 14, 0.92), inset 0 3px 6px rgba(0, 0, 0, 0.72);
  transform: translateY(3px) scale(0.96);
}

.tc-action-pad {
  align-items: flex-end;
  gap: 28px;
}

.tc-toss-row {
  display: flex;
  justify-content: flex-end;
  padding-right: 14px;
}

.tc-toss-btn {
  min-width: 86px;
  height: 42px;
  padding: 0 18px;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(96, 106, 114, 0.92) 0%, rgba(52, 58, 64, 0.95) 100%);
  border: 1.5px solid rgba(255, 255, 255, 0.28);
  box-shadow: 0 4px 0 rgba(20, 24, 28, 0.95), 0 6px 14px rgba(0, 0, 0, 0.5), inset 0 1px 2px rgba(255, 255, 255, 0.28);
  color: #f8f5eb;
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.12em;
}

.tc-toss-btn:active,
.tc-toss-btn.is-pressed {
  transform: translateY(2px) scale(0.96);
  box-shadow: 0 1px 0 rgba(20, 24, 28, 0.95), inset 0 2px 5px rgba(0, 0, 0, 0.65);
  color: var(--color-gold);
  border-color: rgba(255, 209, 102, 0.65);
}

.tc-ab-row {
  display: flex;
  align-items: flex-end;
  gap: 26px;
}

.tc-ab-unit {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.tc-unit-a {
  transform: translateY(-10px);
}

.tc-round-btn {
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: radial-gradient(circle at 32% 28%, #df4276 0%, #a91f4d 58%, #6d1030 100%);
  border: 2px solid rgba(255, 218, 230, 0.38);
  box-shadow: 0 5px 0 #47081e, 0 8px 16px rgba(0, 0, 0, 0.56), inset 0 2px 4px rgba(255, 255, 255, 0.34);
  color: #fff6f8;
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 800;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65);
}

.tc-btn-jump {
  width: 68px;
  height: 68px;
  font-size: 24px;
}

.tc-round-btn:active,
.tc-round-btn.is-pressed {
  transform: translateY(3px) scale(0.95);
  box-shadow: 0 1px 0 #47081e, inset 0 3px 7px rgba(0, 0, 0, 0.6);
  filter: brightness(1.12);
}

.tc-btn-caption {
  font-family: var(--font-display);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: rgba(244, 236, 214, 0.92);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.95);
}

/* Portrait phone: frame canvas above a spacious dedicated control deck */
@media (orientation: portrait) and (max-width: 600px) {
  .stage {
    align-items: flex-start;
    padding-top: calc(max(8px, env(safe-area-inset-top, 0px)) + 78px);
    padding-bottom: calc(max(24px, env(safe-area-inset-bottom, 0px)) + 225px);
  }
  .touch-controls {
    bottom: max(28px, calc(env(safe-area-inset-bottom, 0px) + 16px));
    left: max(14px, calc(env(safe-area-inset-left, 0px) + 10px));
    right: max(14px, calc(env(safe-area-inset-right, 0px) + 10px));
  }
  .tc-dpad-grid {
    grid-template-columns: 52px 46px 52px;
    grid-template-rows: 50px 50px 50px;
    gap: 8px;
  }
  .tc-dir-btn {
    width: 52px;
    height: 50px;
  }
  .tc-ab-row {
    gap: 22px;
  }
}

/* Landscape phone / short screens: full-height arena (ground 100% visible) with slim floating HUD */
@media (orientation: landscape) and (max-height: 540px) {
  .stage {
    align-items: center;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
  #gameCanvas {
    width: 100%;
    height: 100%;
    max-width: calc(var(--app-h, 100dvh) * 2.05);
    object-fit: fill;
  }
  .hud {
    top: max(3px, env(safe-area-inset-top, 0px));
    width: min(92vw, 720px);
    gap: 8px;
  }
  .hud-player {
    padding: 3px 8px;
    border-radius: 8px;
    background: rgba(16, 24, 19, 0.72);
  }
  .hud-name-row {
    margin-bottom: 2px;
  }
  .hud-name,
  .hud-score {
    font-size: 11px;
  }
  .meter-track {
    height: 10px;
  }
  .meter-label {
    font-size: 8px;
  }
  .hud-center {
    gap: 2px;
  }
  .hud-level-pill,
  .hud-clock {
    padding: 2px 7px;
    font-size: 11px;
  }
  .icon-btn {
    width: 24px;
    height: 24px;
  }
  .touch-controls {
    bottom: max(10px, calc(env(safe-area-inset-bottom, 0px) + 6px));
    left: max(10px, calc(env(safe-area-inset-left, 0px) + 6px));
    right: max(10px, calc(env(safe-area-inset-right, 0px) + 6px));
  }
  .tc-dpad-grid {
    grid-template-columns: 44px 38px 44px;
    grid-template-rows: 42px 42px 42px;
    gap: 7px;
  }
  .tc-dir-btn {
    width: 44px;
    height: 42px;
    font-size: 16px;
    border-radius: 12px;
    opacity: 0.9;
  }
  .tc-action-pad {
    gap: 24px;
  }
  .tc-toss-row {
    padding-right: 10px;
  }
  .tc-toss-btn {
    min-width: 76px;
    height: 36px;
    font-size: 11px;
    opacity: 0.9;
  }
  .tc-ab-row {
    gap: 20px;
  }
  .tc-unit-a {
    transform: translateY(-8px);
  }
  .tc-round-btn {
    width: 52px;
    height: 52px;
    font-size: 19px;
    opacity: 0.9;
  }
  .tc-btn-jump {
    width: 58px;
    height: 58px;
    font-size: 21px;
  }
  .tc-btn-caption {
    font-size: 9px;
  }
}

/* Responsive */
@media (max-width: 760px) {
  .controls-grid,
  .online-actions {
    grid-template-columns: 1fr;
  }
  .hud {
    top: var(--space-2);
    width: 96vw;
    gap: var(--space-2);
  }
  .hud-player {
    padding: 6px 8px;
  }
  .hud-name,
  .hud-score {
    font-size: 12px;
  }
}

/* Online Presence Strip, Invite Actions & Home-Screen Install Entry */
.online-presence {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 10px;
  background: rgba(14, 24, 17, 0.72);
  border: 1px solid rgba(182, 245, 58, 0.28);
  color: #e6f4d7;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.presence-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #6ee830;
  box-shadow: 0 0 8px rgba(110, 232, 48, 0.85);
  flex-shrink: 0;
}

.presence-dot.is-offline {
  background: #ef476f;
  box-shadow: 0 0 8px rgba(239, 71, 111, 0.7);
}

.lobby-btn-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.text-invite-btn {
  background: linear-gradient(180deg, #7be836 0%, #37a81c 100%) !important;
  border-color: rgba(190, 255, 110, 0.65) !important;
  color: #0b1808 !important;
  font-weight: 800 !important;
}

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

.copy-invite-btn {
  background: linear-gradient(180deg, #ffd166 0%, #f4a226 100%) !important;
  border-color: rgba(255, 224, 130, 0.65) !important;
  color: #1c1205 !important;
  font-weight: 800 !important;
}

.copy-invite-btn:hover {
  filter: brightness(1.06);
}

.install-row {
  display: flex;
  justify-content: center;
}

.install-app-btn {
  width: 100%;
  min-height: 58px;
  padding: 16px 22px;
  border-radius: 14px;
  border: 2px solid rgba(182, 245, 58, 0.62);
  background: linear-gradient(180deg, rgba(44, 74, 38, 0.96) 0%, rgba(22, 40, 22, 0.98) 100%);
  box-shadow: 0 6px 20px rgba(132, 204, 22, 0.2);
  color: #ecfccb;
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.14s ease, background 0.14s ease, box-shadow 0.14s ease;
}

.install-app-btn:hover {
  transform: translateY(-1px);
  border-color: rgba(182, 245, 58, 0.9);
  background: linear-gradient(180deg, rgba(54, 92, 46, 0.98) 0%, rgba(26, 48, 27, 0.98) 100%);
  box-shadow: 0 8px 24px rgba(132, 204, 22, 0.3);
}

.install-app-btn:active {
  transform: translateY(0);
}

.install-card {
  max-width: 460px;
}

.install-guide-text {
  margin: 0;
  color: rgba(244, 238, 224, 0.86);
  font-size: 14px;
  line-height: 1.5;
}

.install-steps {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: #fff8e7;
  font-size: 14px;
  line-height: 1.45;
}

