:root {
  color-scheme: dark;
  font-family: Inter, ui-rounded, "Segoe UI", system-ui, sans-serif;
  background: #080d18;
  color: #e7edf9;
  --red: #ff5368;
  --red-dark: #9c3340;
  --blue: #20bfdf;
  --blue-dark: #087b94;
  --panel: rgba(7, 12, 23, 0.90);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

button,
select,
input {
  font: inherit;
}

button {
  color: #edf3ff;
  background: #1b2942;
  border: 1px solid #3a5278;
  border-radius: 14px;
  cursor: pointer;
  font-weight: 700;
}

button:active {
  transform: translateY(1px) scale(0.985);
  filter: brightness(1.15);
}

.hidden {
  display: none !important;
}

.menu-shell {
  width: 100vw;
  height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow-y: auto;
  touch-action: pan-y;
  background:
    radial-gradient(circle at 15% 15%, rgba(28, 190, 222, 0.18), transparent 36%),
    radial-gradient(circle at 85% 80%, rgba(255, 79, 99, 0.16), transparent 32%),
    #080d18;
}

.menu-card {
  width: min(560px, 100%);
  padding: 30px;
  border: 1px solid #293957;
  border-radius: 24px;
  background: rgba(12, 19, 34, 0.94);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
}

.brand-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-bottom: 26px;
}

.brand-icon {
  width: clamp(66px, 16vw, 96px);
  height: clamp(66px, 16vw, 96px);
  border-radius: 22%;
  box-shadow: 0 12px 35px rgba(5, 8, 17, 0.48);
}

.brand-header h1 {
  font-size: clamp(36px, 8vw, 58px);
}

.menu-panel h2 {
  margin: 0 0 16px;
  font-size: 23px;
}

.eyebrow {
  margin: 0 0 8px;
  color: #59d9ef;
  font: 700 12px/1.3 ui-monospace, Consolas, monospace;
  letter-spacing: 0.14em;
}

h1 {
  margin: 0;
  font-size: clamp(36px, 8vw, 62px);
  line-height: 0.95;
}

.lead {
  margin: 14px 0 28px;
  color: #9caccc;
}

.random-map-note {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 0 14px;
  color: #cbd7ed;
  background: #0a1221;
  border: 1px solid #35496b;
  border-radius: 12px;
  font-size: 14px;
}

.field-label {
  display: block;
  margin: 0 0 7px;
  color: #aab8d2;
  font-size: 13px;
  font-weight: 700;
}

select,
input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  color: #eff5ff;
  background: #0a1221;
  border: 1px solid #35496b;
  border-radius: 12px;
  outline: none;
}

select:focus,
input:focus {
  border-color: #42cbe7;
}

.menu-actions {
  display: grid;
  gap: 10px;
  margin: 0 0 14px;
}

.choice-card {
  display: flex;
  align-items: center;
  gap: 13px;
  min-height: 62px;
  margin-bottom: 10px;
  padding: 10px 14px;
  border: 1px solid #35496b;
  border-radius: 14px;
  background: #0a1221;
  cursor: pointer;
}

.choice-card.disabled {
  opacity: 0.45;
  cursor: default;
}

.choice-card input {
  width: 24px;
  min-height: 24px;
  accent-color: #35cde6;
}

.choice-card span {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.choice-card small {
  color: #7f90af;
  font-size: 12px;
}

#custom-code-input {
  margin-bottom: 10px;
  text-align: center;
  text-transform: uppercase;
  font: 800 20px/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.12em;
}

.dialog-actions {
  display: grid;
  grid-template-columns: minmax(100px, 0.75fr) minmax(180px, 1.4fr);
  gap: 10px;
  margin-top: 14px;
}

.dialog-actions button,
.wide-secondary {
  min-height: 48px;
  padding: 0 15px;
}

.primary {
  color: #06121b;
  background: linear-gradient(135deg, #24c5e4, #65e4bd);
  border: none;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-heading h2 {
  margin: 0;
}

.small-action {
  width: 42px;
  height: 42px;
  padding: 0;
  border-radius: 50%;
  font-size: 20px;
}

.rooms-list {
  display: grid;
  gap: 9px;
  min-height: 100px;
  max-height: min(42vh, 330px);
  margin: 16px 0;
  overflow-y: auto;
}

.room-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 64px;
  padding: 10px 14px;
  text-align: left;
}

.room-row span {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.room-row strong {
  color: #e9f1ff;
  font: 800 19px/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.1em;
}

.room-row small,
.rooms-empty {
  color: #8192b1;
  font-size: 12px;
}

.room-row b {
  color: #64e2a0;
}

.rooms-empty {
  display: grid;
  place-items: center;
  min-height: 100px;
  border: 1px dashed #304462;
  border-radius: 14px;
  text-align: center;
}

.wide-secondary {
  width: 100%;
}

.menu-actions button,
.join-row button {
  min-height: 50px;
  padding: 0 15px;
}

.menu-actions .primary {
  color: #06121b;
  background: linear-gradient(135deg, #24c5e4, #65e4bd);
  border: none;
}

.join-row {
  display: grid;
  grid-template-columns: 1fr 126px;
  gap: 10px;
}

#room-input {
  text-align: center;
  text-transform: uppercase;
  font: 800 22px/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.18em;
}

.status {
  min-height: 20px;
  margin: 16px 0 0;
  color: #67dc99;
  font-size: 14px;
}

.status.error {
  color: #ff7181;
}

.hint {
  margin: 9px 0 0;
  color: #7788a8;
  font-size: 12px;
  line-height: 1.45;
}

.game-shell {
  position: relative;
  width: 100vw;
  height: 100dvh;
  background: #090e19;
  isolation: isolate;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hud {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 7px 13px;
  border: 1px solid rgba(103, 126, 165, 0.28);
  border-radius: 14px;
  background: var(--panel);
  backdrop-filter: blur(8px);
  pointer-events: none;
}

.top-left {
  left: max(12px, env(safe-area-inset-left));
}

.top-right {
  right: max(12px, env(safe-area-inset-right));
}

.top-center {
  left: 50%;
  flex-direction: column;
  gap: 1px;
  min-width: 152px;
  transform: translateX(-50%);
}

.hud strong {
  font-size: 24px;
  line-height: 1;
}

.top-center strong {
  color: #f3d55a;
  font: 800 17px/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.12em;
}

.top-center span {
  max-width: 220px;
  color: #8fa1c1;
  font-size: 10px;
  white-space: nowrap;
}

.top-center .network-status {
  color: #63e08f;
  font: 800 9px/1.15 ui-monospace, Consolas, monospace;
  letter-spacing: 0.06em;
}

.top-center .network-status.medium {
  color: #ffd34c;
}

.top-center .network-status.poor {
  color: #ff697b;
}

.score-label {
  font-size: 11px;
  font-weight: 800;
}

.red-label {
  color: var(--red);
}

.blue-label {
  color: var(--blue);
}

.round-action {
  position: absolute;
  z-index: 5;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  background: rgba(8, 14, 25, 0.86);
  font-size: 21px;
}

.back-action {
  left: max(12px, env(safe-area-inset-left));
  top: max(68px, calc(env(safe-area-inset-top) + 58px));
}

.share-action {
  left: max(58px, calc(env(safe-area-inset-left) + 46px));
  top: max(68px, calc(env(safe-area-inset-top) + 58px));
}

.sound-action {
  left: max(104px, calc(env(safe-area-inset-left) + 92px));
  top: max(68px, calc(env(safe-area-inset-top) + 58px));
  font-size: 17px;
}

.sound-action.muted {
  color: #7788a8;
  filter: grayscale(1);
}

.fullscreen-action {
  right: max(58px, calc(env(safe-area-inset-right) + 46px));
  top: max(68px, calc(env(safe-area-inset-top) + 58px));
}

.reset-action {
  right: max(12px, env(safe-area-inset-right));
  top: max(68px, calc(env(safe-area-inset-top) + 58px));
}

.touch-controls {
  position: absolute;
  z-index: 4;
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-between;
  pointer-events: none;
}

.control-cluster {
  display: flex;
  gap: clamp(10px, 2.2vw, 22px);
  pointer-events: auto;
}

.drive-button {
  width: clamp(74px, 12vw, 126px);
  height: clamp(74px, 12vw, 126px);
  padding: 0 0 8px;
  border: 6px solid rgba(0, 0, 0, 0.78);
  border-radius: 50%;
  color: #05070b;
  font: 900 clamp(58px, 10vw, 104px)/0.7 system-ui, sans-serif;
  opacity: 0.84;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.30);
  touch-action: none;
}

.player-one .drive-button {
  background: var(--blue-dark);
}

.player-two .drive-button {
  background: var(--red-dark);
}

.drive-button.active,
.drive-button:active {
  opacity: 1;
  filter: brightness(1.35);
  transform: scale(0.95);
}

body.role-1 #controls-p2,
body.role-2 #controls-p1,
body.spectator #touch-controls {
  display: none;
}

body.role-1 .touch-controls,
body.role-2 .touch-controls {
  justify-content: stretch;
}

body.role-1 .control-cluster,
body.role-2 .control-cluster {
  width: 100%;
  justify-content: space-between;
}

/* On one shared screen each player keeps a compact pair in their own corner.
   Online players retain the larger centered controls for one pair of thumbs. */
body.local-room .touch-controls {
  left: max(7px, env(safe-area-inset-left));
  right: max(7px, env(safe-area-inset-right));
  bottom: max(7px, env(safe-area-inset-bottom));
}

body.local-room .control-cluster {
  gap: clamp(5px, 1vw, 10px);
}

body.local-room .drive-button {
  width: clamp(56px, 8.2vw, 78px);
  height: clamp(56px, 8.2vw, 78px);
  padding-bottom: 5px;
  border-width: 4px;
  font-size: clamp(44px, 6.5vw, 62px);
  opacity: 0.76;
}

.audio-unlock {
  position: absolute;
  z-index: 12;
  left: 50%;
  bottom: calc(max(14px, env(safe-area-inset-bottom)) + clamp(74px, 12vw, 126px) + 12px);
  min-height: 48px;
  padding: 8px 18px;
  border-color: rgba(101, 228, 189, 0.72);
  background: rgba(8, 20, 31, 0.94);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.42);
  transform: translateX(-50%);
  animation: audio-unlock-pulse 1.4s ease-in-out infinite alternate;
}

.audio-unlock:active {
  transform: translateX(-50%) scale(0.97);
}

@keyframes audio-unlock-pulse {
  to {
    border-color: #65e4bd;
    box-shadow: 0 8px 34px rgba(70, 220, 190, 0.30);
  }
}

body.local-room .share-action {
  display: none;
}

body:not(.local-room) .reset-action {
  display: none;
}

.victory-overlay {
  position: absolute;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: radial-gradient(circle, rgba(21, 30, 49, 0.62), rgba(2, 5, 11, 0.91));
  pointer-events: auto;
}

.dialog-overlay {
  position: absolute;
  z-index: 35;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(3, 6, 12, 0.78);
  backdrop-filter: blur(5px);
}

.modal-card {
  width: min(480px, 94vw);
  padding: 26px;
  border: 1px solid #526885;
  border-radius: 22px;
  background: #0c1424;
  box-shadow: 0 24px 90px rgba(0, 0, 0, 0.55);
  text-align: center;
}

.modal-card > strong {
  color: #ffd75c;
  font-size: 24px;
}

.modal-card p {
  color: #a8b5cf;
}

.victory-rays {
  position: absolute;
  width: min(90vw, 760px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: repeating-conic-gradient(
    from 0deg,
    rgba(255, 211, 76, 0.18) 0deg 8deg,
    transparent 8deg 20deg
  );
  animation: victory-spin 8s linear infinite;
}

.victory-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
  padding: clamp(22px, 5vw, 46px) clamp(38px, 8vw, 90px);
  border: 2px solid rgba(255, 218, 88, 0.72);
  border-radius: 28px;
  background: rgba(8, 13, 24, 0.91);
  box-shadow: 0 0 80px rgba(255, 205, 58, 0.30);
  animation: victory-arrive 0.58s cubic-bezier(.2, 1.35, .35, 1) both;
}

.victory-cup {
  font-size: clamp(54px, 12vw, 112px);
  line-height: 1;
  animation: victory-bounce 0.9s ease-in-out infinite alternate;
}

#victory-title {
  color: #ffe06b;
  font-size: clamp(30px, 7vw, 72px);
  line-height: 1;
  text-shadow: 0 0 28px rgba(255, 215, 70, 0.55);
}

#victory-score {
  color: #f4f7ff;
  font: 800 clamp(22px, 4vw, 40px)/1 ui-monospace, Consolas, monospace;
}

.victory-card small {
  color: #9dacC7;
}

.victory-actions {
  display: grid;
  grid-template-columns: minmax(140px, 1fr) minmax(140px, 1fr);
  gap: 10px;
  width: min(430px, 78vw);
  margin-top: 10px;
}

.victory-actions button {
  min-height: 50px;
  padding: 8px 18px;
}

.victory-actions button:disabled {
  cursor: default;
  opacity: 0.68;
  transform: none;
}

@keyframes victory-spin {
  to { transform: rotate(360deg); }
}

@keyframes victory-arrive {
  from { opacity: 0; transform: scale(0.55) translateY(40px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes victory-bounce {
  to { transform: translateY(-10px) rotate(4deg); }
}

@media (max-height: 520px) {
  .menu-card {
    padding: 18px 24px;
  }

  .brand-header {
    gap: 12px;
    margin-bottom: 14px;
  }

  .brand-icon {
    width: 58px;
    height: 58px;
    border-radius: 14px;
  }

  .brand-header h1 {
    font-size: 38px;
  }

  .victory-card {
    gap: 5px;
    padding: 14px 28px;
  }

  .victory-cup {
    font-size: 48px;
  }

  .victory-actions {
    margin-top: 5px;
  }

  .victory-actions button {
    min-height: 42px;
  }

  body.local-room .drive-button {
    width: clamp(52px, 14vh, 68px);
    height: clamp(52px, 14vh, 68px);
    font-size: clamp(40px, 11vh, 54px);
  }

  .hud {
    min-height: 34px;
    padding: 5px 10px;
  }

  .hud strong {
    font-size: 19px;
  }

  .score-label {
    display: none;
  }

  .round-action {
    top: max(50px, calc(env(safe-area-inset-top) + 42px));
    width: 34px;
    height: 34px;
    font-size: 18px;
  }
}

@media (orientation: portrait) and (max-width: 700px) {
  .game-shell::before {
    content: "Поверните телефон горизонтально";
    position: absolute;
    z-index: 20;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 30px;
    color: #e9effb;
    background: rgba(5, 9, 17, 0.94);
    font-size: 24px;
    font-weight: 800;
    text-align: center;
    pointer-events: none;
  }
}
