:root {
  color-scheme: dark;
  --bg: #070b14;
  --surface: rgba(16, 23, 39, 0.88);
  --surface-strong: #121a2b;
  --line: rgba(255, 255, 255, 0.09);
  --text: #f4f7fb;
  --muted: #8c98ae;
  --cyan: #5df2cf;
  --cyan-dark: #1bbca4;
  --coral: #ff6c7d;
  --coral-dark: #d9415f;
  --fruit: #ffd45d;
  --radius: 22px;
  --shadow: 0 28px 80px rgba(0, 0, 0, 0.42);
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 50% -20%, rgba(54, 83, 137, 0.16), transparent 46%),
    linear-gradient(145deg, #070b14 0%, #0a0f1b 50%, #070b14 100%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

body.game-active { overflow: hidden; }

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.25;
  background-image: linear-gradient(rgba(255,255,255,.018) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.018) 1px, transparent 1px);
  background-size: 42px 42px;
  mask-image: linear-gradient(to bottom, black, transparent 80%);
}

button, input { font: inherit; }
button { color: inherit; }

.ambient {
  position: fixed;
  width: 440px;
  height: 440px;
  border-radius: 50%;
  filter: blur(110px);
  opacity: 0.1;
  pointer-events: none;
}
.ambient-one { top: 5%; left: -220px; background: var(--cyan); }
.ambient-two { right: -240px; bottom: -100px; background: var(--coral); }

.app-shell { width: min(1180px, calc(100% - 40px)); margin: 0 auto; position: relative; z-index: 1; }

body.game-active .app-shell {
  width: 100%;
  height: 100dvh;
  padding-inline: clamp(10px, 1.5vw, 28px);
}

body.game-active .topbar { height: clamp(58px, 8.5vh, 86px); }

.topbar { height: 92px; display: flex; align-items: center; justify-content: space-between; }
.brand { display: inline-flex; align-items: center; gap: 13px; color: var(--text); text-decoration: none; font-size: 14px; font-weight: 900; letter-spacing: .24em; }
.brand-mark { display: flex; align-items: center; gap: 2px; }
.brand-mark span { width: 8px; height: 8px; border-radius: 3px; background: var(--cyan); box-shadow: 0 0 16px rgba(93,242,207,.6); }
.brand-mark span:nth-child(1) { opacity: .45; }
.brand-mark span:nth-child(2) { transform: translateY(5px); opacity: .65; }
.brand-mark span:nth-child(3) { transform: translateY(5px); }
.brand-mark span:nth-child(4) { width: 11px; height: 11px; }

.sound-toggle { border: 1px solid var(--line); background: rgba(255,255,255,.035); height: 42px; padding: 0 15px; border-radius: 12px; display: flex; align-items: center; gap: 9px; cursor: pointer; color: var(--muted); transition: .2s ease; }
.sound-toggle:hover { color: var(--text); border-color: rgba(255,255,255,.18); }
.sound-toggle svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sound-toggle .sound-off { display: none; }
.sound-toggle[aria-pressed="false"] .sound-on { display: none; }
.sound-toggle[aria-pressed="false"] .sound-off { display: block; }

.screen { display: none; animation: enter .45s ease both; }
.screen.active { display: flex; }
@keyframes enter { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.start-screen { min-height: calc(100vh - 92px); align-items: center; justify-content: space-between; gap: 80px; padding: 40px 3vw 100px; }
.hero-copy { flex: 1; max-width: 610px; }
.eyebrow, .lobby-status { display: flex; align-items: center; gap: 10px; color: var(--cyan); font-size: 12px; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }
.eyebrow span { width: 32px; height: 1px; background: var(--cyan); box-shadow: 0 0 10px var(--cyan); }
.hero-copy h1 { margin: 24px 0 22px; font-size: clamp(48px, 6vw, 82px); letter-spacing: -.065em; line-height: .98; font-weight: 840; }
.hero-copy h1 em { color: var(--cyan); font-style: normal; text-shadow: 0 0 46px rgba(93,242,207,.2); }
.hero-copy > p { max-width: 530px; margin: 0; color: var(--muted); font-size: 18px; line-height: 1.65; }
.feature-row { display: flex; gap: 28px; margin-top: 38px; color: var(--muted); font-size: 13px; }
.feature-row span { display: flex; align-items: baseline; gap: 7px; }
.feature-row b { color: var(--text); font-size: 20px; }

.panel { background: linear-gradient(145deg, rgba(20,29,48,.94), rgba(11,17,30,.94)); border: 1px solid var(--line); box-shadow: var(--shadow); backdrop-filter: blur(20px); }
.join-card { width: min(100%, 418px); padding: 38px; border-radius: var(--radius); position: relative; overflow: hidden; }
.card-accent { position: absolute; left: 38px; right: 38px; top: 0; height: 2px; background: linear-gradient(90deg, transparent, var(--cyan), transparent); box-shadow: 0 0 18px var(--cyan); }
.join-card h2 { font-size: 26px; letter-spacing: -.03em; margin: 0 0 8px; }
.card-lead { margin: 0 0 30px; color: var(--muted); line-height: 1.5; font-size: 14px; }
.field-label { display: block; margin: 0 0 9px; color: #c6cede; font-size: 12px; font-weight: 700; letter-spacing: .04em; }
.text-input { width: 100%; height: 52px; padding: 0 16px; color: var(--text); border: 1px solid rgba(255,255,255,.1); border-radius: 12px; outline: none; background: rgba(3,7,14,.5); transition: .2s ease; }
.text-input::placeholder { color: #566176; }
.text-input:focus { border-color: rgba(93,242,207,.65); box-shadow: 0 0 0 3px rgba(93,242,207,.08); }
.primary-button, .secondary-button { border: 0; border-radius: 12px; font-weight: 800; cursor: pointer; transition: transform .18s ease, filter .18s ease, opacity .18s ease; }
.primary-button { min-height: 54px; padding: 0 18px; width: 100%; margin-top: 14px; display: flex; align-items: center; justify-content: center; gap: 12px; background: linear-gradient(135deg, #6af6d6, #3cdbbe); color: #07120f; box-shadow: 0 12px 30px rgba(55,216,186,.15); }
.primary-button:hover:not(:disabled), .secondary-button:hover { filter: brightness(1.08); transform: translateY(-1px); }
.primary-button:disabled { opacity: .35; cursor: not-allowed; }
.button-arrow { font-size: 20px; font-weight: 500; }
.divider { display: flex; align-items: center; gap: 13px; margin: 24px 0; color: #657086; font-size: 11px; text-transform: uppercase; letter-spacing: .15em; }
.divider::before, .divider::after { content: ""; height: 1px; background: var(--line); flex: 1; }
.code-row { display: grid; grid-template-columns: 1fr 108px; gap: 10px; }
.code-input { letter-spacing: .32em; font-weight: 800; font-variant-numeric: tabular-nums; }
.secondary-button { background: #252e41; border: 1px solid rgba(255,255,255,.08); }
.form-error { min-height: 18px; margin: 12px 0 -8px; color: #ff8d9a; font-size: 13px; line-height: 1.35; }

.lobby-screen { min-height: calc(100vh - 92px); align-items: center; justify-content: center; padding: 30px 0 100px; }
.lobby-card { width: min(610px, 100%); padding: 46px; border-radius: 28px; text-align: center; }
.lobby-status { justify-content: center; color: var(--cyan); }
.pulse-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--cyan); box-shadow: 0 0 0 5px rgba(93,242,207,.09), 0 0 16px var(--cyan); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }
.overline { color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .17em; text-transform: uppercase; }
.lobby-card > .overline { margin: 33px 0 7px; }
.room-code { appearance: none; border: 0; background: transparent; color: var(--text); cursor: copy; font-size: clamp(52px, 9vw, 72px); font-weight: 900; letter-spacing: .14em; line-height: 1; font-variant-numeric: tabular-nums; text-shadow: 0 0 40px rgba(255,255,255,.12); }
.copy-hint { margin: 8px 0 38px; color: #5f6b80; font-size: 11px; }
.lobby-card h2 { margin: 0 0 24px; font-size: 24px; }
.players-list { display: flex; align-items: center; gap: 12px; }
.player-card { min-width: 0; flex: 1; min-height: 82px; padding: 15px; display: flex; align-items: center; gap: 12px; text-align: left; border-radius: 16px; background: rgba(255,255,255,.035); border: 1px solid var(--line); }
.player-orb { flex: 0 0 38px; width: 38px; height: 38px; display: grid; place-items: center; border-radius: 12px; color: #07120f; font-weight: 900; }
.player-cyan .player-orb { background: var(--cyan); }
.player-coral .player-orb { background: var(--coral); }
.player-card div { min-width: 0; }
.player-card small { display: block; color: var(--muted); font-size: 10px; margin-bottom: 4px; }
.player-card strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.connection-dot { margin-left: auto; width: 8px; height: 8px; border-radius: 50%; background: #444e61; }
.connection-dot.connected { background: var(--cyan); box-shadow: 0 0 10px rgba(93,242,207,.6); }
.versus { flex: 0 0 auto; color: #5a6579; font-size: 10px; font-weight: 900; }
.start-button { max-width: 260px; margin: 28px auto 0; }
.waiting-note { height: 21px; color: var(--muted); font-size: 12px; }
.loader-dots i { display: inline-block; width: 4px; height: 4px; margin-right: 3px; border-radius: 50%; background: var(--cyan); animation: dots 1.2s infinite; }
.loader-dots i:nth-child(2) { animation-delay: .15s; }
.loader-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes dots { 50% { transform: translateY(-3px); opacity: .4; } }

.game-screen { min-height: calc(100vh - 92px); padding: 5px 0 28px; flex-direction: column; align-items: center; }
body.game-active .game-screen {
  height: calc(100dvh - clamp(58px, 8.5vh, 86px));
  min-height: 0;
  padding: 0 0 clamp(8px, 1.5vh, 20px);
  gap: clamp(6px, 1.2vh, 14px);
  overflow: hidden;
}
.scorebar { width: min(960px, 100%); min-height: 76px; border-radius: 18px; display: grid; grid-template-columns: 1fr 130px 1fr; align-items: center; margin-bottom: 14px; overflow: hidden; }
body.game-active .scorebar {
  flex: 0 0 auto;
  height: clamp(58px, 8vh, 76px);
  min-height: 0;
  margin-bottom: 0;
}
.score-player { min-width: 0; display: flex; align-items: center; gap: 12px; padding: 12px 20px; }
.score-player-two { justify-content: flex-end; text-align: right; }
.score-player div { min-width: 0; }
.score-player small, .timer-block small { display: block; color: var(--muted); font-size: 9px; text-transform: uppercase; letter-spacing: .14em; margin-bottom: 3px; }
.score-player strong { display: block; max-width: 180px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.score-player > b { font-size: 30px; font-variant-numeric: tabular-nums; }
.score-player-one > b { color: var(--cyan); margin-left: auto; }
.score-player-two > b { color: var(--coral); margin-right: auto; }
.score-snake { width: 10px; height: 30px; border-radius: 6px; background: var(--cyan); box-shadow: 0 0 14px rgba(93,242,207,.4); }
.score-player-two .score-snake { background: var(--coral); box-shadow: 0 0 14px rgba(255,108,125,.4); }
.timer-block { align-self: stretch; display: grid; place-content: center; text-align: center; border-inline: 1px solid var(--line); background: rgba(0,0,0,.12); }
.timer-block strong { font-size: 24px; letter-spacing: .08em; font-variant-numeric: tabular-nums; }

.board-wrap { position: relative; width: min(1080px, 100%); border-radius: 22px; padding: 10px; overflow: hidden; }
body.game-active .board-wrap {
  flex: 0 0 auto;
  width: auto;
  max-width: 100%;
  padding: clamp(4px, .6vw, 10px);
}
.board-wrap::before { content: ""; position: absolute; inset: 0; pointer-events: none; border-radius: inherit; box-shadow: inset 0 1px 0 rgba(255,255,255,.035); }
#gameCanvas { display: block; width: 100%; height: auto; aspect-ratio: 40 / 28; border-radius: 15px; background: #080e19; }
.countdown { display: none; position: absolute; inset: 10px; place-items: center; border-radius: 15px; background: rgba(5,9,17,.42); color: var(--text); font-size: clamp(80px, 18vw, 170px); font-weight: 900; text-shadow: 0 0 50px rgba(93,242,207,.28); backdrop-filter: blur(2px); }
.countdown.visible { display: grid; animation: countdownPop .35s ease both; }
@keyframes countdownPop { from { opacity: 0; transform: scale(.82); } to { opacity: 1; transform: scale(1); } }

.pause-overlay, .finish-overlay { display: none; position: absolute; inset: 10px; z-index: 3; border-radius: 15px; background: rgba(5,9,17,.86); backdrop-filter: blur(9px); text-align: center; align-items: center; justify-content: center; flex-direction: column; }
.pause-overlay.visible, .finish-overlay.visible { display: flex; animation: enter .28s ease; }
.pause-icon { width: 54px; height: 54px; display: flex; align-items: center; justify-content: center; gap: 7px; border-radius: 50%; border: 1px solid rgba(93,242,207,.24); background: rgba(93,242,207,.06); }
.pause-icon i { width: 5px; height: 19px; border-radius: 4px; background: var(--cyan); box-shadow: 0 0 10px var(--cyan); }
.pause-overlay h2 { margin: 22px 0 8px; font-size: 30px; }
.pause-overlay p { margin: 0; color: var(--muted); line-height: 1.55; }
.waiting-line { width: 140px; height: 2px; margin-top: 28px; background: rgba(255,255,255,.08); overflow: hidden; }
.waiting-line::after { content: ""; display: block; width: 40%; height: 100%; background: var(--cyan); box-shadow: 0 0 10px var(--cyan); animation: waiting 1.4s ease-in-out infinite; }
@keyframes waiting { from { transform: translateX(-100%); } to { transform: translateX(350%); } }

.finish-overlay .trophy { width: 60px; height: 60px; display: grid; place-items: center; border-radius: 20px; background: rgba(255,212,93,.1); border: 1px solid rgba(255,212,93,.22); color: var(--fruit); font-size: 32px; }
.finish-overlay .overline { margin: 20px 0 8px; }
.finish-overlay h2 { margin: 0 0 20px; font-size: clamp(32px, 5vw, 48px); }
.final-score { min-width: 340px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 20px; color: var(--muted); font-size: 13px; }
.final-score span:first-child { text-align: right; }
.final-score span:last-child { text-align: left; }
.final-score b { color: var(--text); font-size: 30px; font-variant-numeric: tabular-nums; }
.finish-overlay .primary-button { width: 220px; margin-top: 24px; }
.rematch-note { min-height: 18px; color: var(--muted); font-size: 12px; margin: 12px 0 0; }

.controls-hint { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 10px; margin-top: 16px; color: #69758a; font-size: 11px; }
body.game-active .controls-hint { flex: 0 0 auto; min-height: 25px; margin-top: 0; }
.controls-hint > span { display: flex; gap: 3px; }
kbd { min-width: 25px; height: 25px; padding: 0 5px; display: grid; place-items: center; border: 1px solid rgba(255,255,255,.1); border-bottom-color: rgba(255,255,255,.2); border-radius: 6px; background: rgba(255,255,255,.035); color: #aeb7c8; font: 10px ui-monospace, SFMono-Regular, Menlo, monospace; }
.hint-divider { width: 1px; height: 15px; background: var(--line); margin: 0 2px; }
.toast { position: fixed; left: 50%; bottom: 30px; z-index: 10; transform: translate(-50%, 20px); padding: 11px 16px; border: 1px solid var(--line); border-radius: 11px; background: rgba(17,25,41,.94); box-shadow: 0 16px 40px rgba(0,0,0,.35); font-size: 13px; opacity: 0; pointer-events: none; transition: .25s ease; }
.toast.visible { opacity: 1; transform: translate(-50%, 0); }
.toast.collision { color: #ffb1ba; border-color: rgba(255,108,125,.28); }

@media (max-width: 850px) {
  .app-shell { width: min(100% - 28px, 680px); }
  .start-screen { flex-direction: column; justify-content: center; align-items: stretch; gap: 44px; padding: 30px 0 70px; }
  .hero-copy { text-align: center; margin: 0 auto; }
  .eyebrow, .feature-row { justify-content: center; }
  .hero-copy > p { margin-inline: auto; }
  .join-card { margin: 0 auto; }
  .score-player strong { max-width: 110px; }
}

@media (max-width: 600px) {
  .app-shell { width: calc(100% - 20px); }
  .topbar { height: 70px; }
  .sound-toggle span { display: none; }
  .start-screen, .lobby-screen { min-height: calc(100vh - 70px); }
  .hero-copy h1 { font-size: 44px; }
  .hero-copy > p { font-size: 15px; }
  .feature-row { gap: 16px; }
  .join-card, .lobby-card { padding: 28px 22px; }
  .players-list { flex-direction: column; }
  .player-card { width: 100%; }
  .versus { margin-block: -5px; }
  .scorebar { grid-template-columns: 1fr 82px 1fr; min-height: 66px; }
  .score-player { padding: 8px 10px; gap: 6px; }
  .score-player small, .score-snake { display: none; }
  .score-player strong { max-width: 84px; font-size: 11px; }
  .score-player > b { font-size: 23px; }
  .timer-block strong { font-size: 16px; }
  .board-wrap { padding: 5px; border-radius: 15px; }
  #gameCanvas, .countdown, .pause-overlay, .finish-overlay { border-radius: 11px; }
  .countdown, .pause-overlay, .finish-overlay { inset: 5px; }
  .controls-hint { margin-top: 12px; }
  .final-score { min-width: 280px; gap: 10px; }
  .finish-overlay .trophy { width: 44px; height: 44px; border-radius: 14px; font-size: 24px; }
  .finish-overlay .overline { margin-top: 12px; }
  .finish-overlay h2 { margin-bottom: 12px; }
  .finish-overlay .primary-button { margin-top: 14px; min-height: 46px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
}
