:root {
  --cyan:   #22e0ff;
  --magenta:#ff3d9e;
  --purple: #b45cff;
  --lime:   #35ee7a;
  --amber:  #ffc14d;
  --red:    #ff3d6e;
  --ink:    #06070f;
  --glass:  rgba(10, 14, 32, .72);
  --edge:   rgba(120, 200, 255, .28);
  --font-display: 'Orbitron', ui-monospace, 'SF Mono', Menlo, monospace;
  --font-ui: 'Rajdhani', ui-sans-serif, system-ui, 'Segoe UI', sans-serif;
}

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

/* One visible focus ring for every control, so keyboard users can always see
   where they are. :focus-visible keeps it off mouse and touch presses. */
:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 3px;
  border-radius: 8px;
}
.cta:focus-visible,
.mode:focus-visible,
.tool:focus-visible,
.tbtn:focus-visible {
  outline-color: #ffffff;
  box-shadow: 0 0 0 4px rgba(34, 224, 255, .45), 0 0 26px rgba(34, 224, 255, .5);
}
.check input:focus-visible,
.slider input:focus-visible { outline-offset: 4px; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

html, body {
  width: 100%;
  height: 100%;
  /* dvh tracks the mobile address bar as it collapses; the 100% above is the
     fallback for browsers that don't know it. */
  height: 100dvh;
  overflow: hidden;
  background: var(--ink);
  color: #dff2ff;
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: none;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 0;
}

/* ── overlay screens ───────────────────────────────────────────────── */
.screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  /* A plain scrim rather than a full-viewport backdrop-filter: blurring the
     whole animating canvas every frame is ruinous on modest hardware. The
     blur is confined to the panel below, where it costs almost nothing. */
  background:
    radial-gradient(120% 90% at 50% 10%, rgba(80, 30, 140, .42), transparent 62%),
    rgba(4, 5, 12, .9);
  animation: fade-in .32s ease both;
  overflow-y: auto;
  /* body sets touch-action: none to lock the playfield, so overlays have to
     opt back in or tall panels become unscrollable on a phone. */
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
.screen.hidden { display: none; }

@keyframes fade-in { from { opacity: 0 } to { opacity: 1 } }

.panel {
  width: min(560px, 100%);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: clamp(20px, 4vh, 40px) clamp(18px, 4vw, 40px);
  border: 1px solid var(--edge);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(20, 24, 54, .78), rgba(8, 9, 22, .85));
  backdrop-filter: blur(12px) saturate(1.3);
  -webkit-backdrop-filter: blur(12px) saturate(1.3);
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, .04) inset,
    0 30px 90px rgba(0, 0, 0, .7),
    0 0 70px rgba(90, 40, 180, .35);
}

/* ── logo ──────────────────────────────────────────────────────────── */
.logo {
  position: relative;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(46px, 13vw, 104px);
  letter-spacing: clamp(4px, 1.4vw, 14px);
  line-height: 1;
  background: linear-gradient(180deg, #ffffff 6%, var(--cyan) 42%, var(--purple) 76%, var(--magenta) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 18px rgba(60, 200, 255, .55)) drop-shadow(0 0 46px rgba(180, 92, 255, .45));
  animation: logo-pulse 3.6s ease-in-out infinite;
}
.logo::before,
.logo::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  background: none;
  -webkit-background-clip: border-box;
  background-clip: border-box;
  color: var(--magenta);
  opacity: .5;
  mix-blend-mode: screen;
  animation: glitch 4.5s steps(1) infinite;
}
.logo::after { color: var(--cyan); animation-delay: .22s; }

@keyframes logo-pulse {
  0%, 100% { filter: drop-shadow(0 0 16px rgba(60, 200, 255, .45)) drop-shadow(0 0 40px rgba(180, 92, 255, .35)); }
  50%      { filter: drop-shadow(0 0 26px rgba(60, 200, 255, .75)) drop-shadow(0 0 66px rgba(255, 61, 158, .5)); }
}
@keyframes glitch {
  0%, 88%, 100% { transform: none; opacity: 0; }
  89% { transform: translate(-3px, 1px); opacity: .55; }
  91% { transform: translate(3px, -2px); opacity: .4; }
  93% { transform: translate(-2px, -1px); opacity: .5; }
  95% { transform: none; opacity: 0; }
}

.tagline {
  font-family: var(--font-ui);
  font-weight: 600;
  letter-spacing: .5em;
  text-transform: uppercase;
  font-size: clamp(10px, 2.4vw, 14px);
  color: rgba(180, 220, 255, .62);
  margin-left: .5em;
}

.heading {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(30px, 8vw, 54px);
  letter-spacing: .12em;
  color: #fff;
  text-shadow: 0 0 22px rgba(90, 220, 255, .8), 0 0 60px rgba(120, 90, 255, .6);
}
.heading.danger {
  color: #fff;
  text-shadow: 0 0 20px rgba(255, 61, 110, .9), 0 0 62px rgba(255, 61, 110, .55);
}

/* ── buttons ───────────────────────────────────────────────────────── */
.cta {
  position: relative;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(15px, 3.4vw, 20px);
  letter-spacing: .18em;
  color: #041018;
  padding: 15px 34px;
  min-width: 250px;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  background: linear-gradient(100deg, var(--cyan), #7ee8ff 30%, var(--purple) 72%, var(--magenta));
  background-size: 220% 100%;
  box-shadow: 0 0 26px rgba(80, 210, 255, .5), 0 10px 30px rgba(0, 0, 0, .5);
  transition: transform .12s ease, box-shadow .2s ease, background-position .5s ease;
  animation: sheen 6s linear infinite;
}
@keyframes sheen { 0% { background-position: 0% 50% } 100% { background-position: 220% 50% } }
.cta:hover  { transform: translateY(-2px) scale(1.02); box-shadow: 0 0 40px rgba(120, 220, 255, .75), 0 14px 34px rgba(0,0,0,.55); }
.cta:active { transform: translateY(1px) scale(.99); }
/* The one button the title screen is really about. */
.cta.huge {
  width: 100%;
  padding: clamp(18px, 3.6vh, 28px) 34px;
  font-size: clamp(26px, 8vw, 44px);
  letter-spacing: .3em;
  text-indent: .3em;
  border-radius: 22px;
  box-shadow: 0 0 44px rgba(80, 210, 255, .55), 0 14px 40px rgba(0, 0, 0, .55);
}
.cta.huge:hover {
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 0 66px rgba(120, 220, 255, .8), 0 18px 46px rgba(0, 0, 0, .6);
}

.cta.ghost.small {
  min-width: 0;
  padding: 9px 22px;
  font-size: clamp(11px, 2.4vw, 13px);
  letter-spacing: .2em;
}

.cta.ghost {
  background: transparent;
  color: rgba(190, 225, 255, .8);
  border: 1px solid var(--edge);
  box-shadow: none;
  animation: none;
  font-size: clamp(12px, 2.8vw, 15px);
  padding: 11px 26px;
  min-width: 200px;
}
.cta.ghost:hover { color: #fff; border-color: rgba(140, 220, 255, .7); box-shadow: 0 0 22px rgba(80, 200, 255, .28); }

/* ── title extras ──────────────────────────────────────────────────── */
.hiscore {
  font-family: var(--font-display);
  font-size: 13px;
  letter-spacing: .3em;
  color: rgba(160, 200, 255, .55);
}
.hiscore span { color: var(--amber); text-shadow: 0 0 14px rgba(255, 193, 77, .7); }

.keys {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
  gap: 7px 14px;
  width: 100%;
  margin-top: 4px;
}
.keys > div {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: rgba(175, 205, 235, .72);
  letter-spacing: .06em;
}
kbd {
  font-family: var(--font-display);
  font-size: 10px;
  padding: 4px 7px;
  border-radius: 6px;
  border: 1px solid rgba(130, 190, 255, .3);
  background: rgba(120, 190, 255, .09);
  color: #cfe9ff;
  box-shadow: 0 2px 0 rgba(80, 150, 220, .22);
  white-space: nowrap;
}
.hint {
  font-size: 12.5px;
  letter-spacing: .08em;
  color: rgba(160, 200, 255, .5);
  display: none;
}
body.touch .hint  { display: block; }
body.touch .keys  { display: none; }

.final {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(40px, 12vw, 72px);
  color: #fff;
  text-shadow: 0 0 26px rgba(80, 220, 255, .8);
  line-height: 1;
}
.newbest {
  font-family: var(--font-display);
  letter-spacing: .22em;
  font-size: 13px;
  color: var(--amber);
  text-shadow: 0 0 18px rgba(255, 193, 77, .8);
  animation: blink 1s steps(2) infinite;
}
.newbest.hidden { display: none; }
@keyframes blink { 50% { opacity: .25 } }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
  gap: 10px;
  width: 100%;
}
.stats div {
  padding: 9px 6px;
  border-radius: 12px;
  border: 1px solid rgba(120, 190, 255, .16);
  background: rgba(120, 190, 255, .05);
}
.stats b {
  display: block;
  font-family: var(--font-display);
  font-size: 19px;
  color: #eaf6ff;
}
.stats i {
  font-style: normal;
  font-size: 10.5px;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: rgba(160, 200, 255, .55);
}

/* ── toolbar ───────────────────────────────────────────────────────── */
#toolbar {
  position: fixed;
  z-index: 30;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
}
.tool {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  cursor: pointer;
  color: #bfe6ff;
  border: 1px solid rgba(120, 190, 255, .22);
  background: var(--glass);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: color .18s, border-color .18s, box-shadow .18s, transform .12s;
  font-size: 17px;
  line-height: 1;
}
.tool:hover  { color: #fff; border-color: rgba(140, 220, 255, .6); box-shadow: 0 0 18px rgba(80, 200, 255, .35); }
.tool:active { transform: scale(.93); }
.tool.off { color: rgba(150, 175, 205, .35); border-color: rgba(120, 150, 190, .16); }
.tool.off .ico { position: relative; }
.tool.off .ico::after {
  content: '';
  position: absolute;
  left: -4px; right: -4px; top: 50%;
  height: 1.5px;
  background: currentColor;
  transform: rotate(-38deg);
}

/* ── touch pad ─────────────────────────────────────────────────────── */
#touchpad {
  position: fixed;
  z-index: 15;
  left: 0; right: 0;
  bottom: max(8px, env(safe-area-inset-bottom));
  padding: 0 8px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 7px;
}
#touchpad.hidden { display: none; }
.tbtn {
  height: 54px;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 700;
  color: #cfeaff;
  border-radius: 14px;
  border: 1px solid rgba(120, 190, 255, .22);
  background: var(--glass);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  cursor: pointer;
  transition: background .1s, transform .07s, box-shadow .1s;
}
.tbtn:active,
.tbtn.down {
  transform: scale(.94);
  background: rgba(60, 190, 255, .28);
  box-shadow: 0 0 22px rgba(80, 210, 255, .45);
  color: #fff;
}
.tbtn.wide { grid-column: span 2; font-size: 13px; letter-spacing: .16em; }
.tbtn.accent {
  color: #06121a;
  background: linear-gradient(100deg, var(--cyan), var(--purple));
  border-color: transparent;
}

/* ── landscape: controls flank the well ─────────────────────────────────
   A 128px bar across the bottom of a 390px-tall phone leaves no room for a
   twenty-row well, so in landscape the pad splits into two thumb clusters
   and the middle of the screen stays clear. */
body.pad-sides #touchpad {
  position: fixed;
  inset: 0;
  bottom: 0;
  padding: 10px max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  grid-template-columns: 62px 62px 1fr 62px 62px;
  grid-template-rows: 44px 1fr 58px 58px;
  gap: 8px;
  pointer-events: none;
}
body.pad-sides .tbtn {
  height: 100%;
  pointer-events: auto;
}
body.pad-sides .tbtn.wide { grid-column: auto; }
body.pad-sides .tbtn[data-act="pause"] { grid-area: 1 / 1; height: 40px; }
body.pad-sides .tbtn[data-act="left"]  { grid-area: 3 / 1; }
body.pad-sides .tbtn[data-act="right"] { grid-area: 3 / 2; }
body.pad-sides .tbtn[data-act="down"]  { grid-area: 4 / 1 / 5 / 3; }
body.pad-sides .tbtn[data-act="ccw"]   { grid-area: 3 / 4; }
body.pad-sides .tbtn[data-act="cw"]    { grid-area: 3 / 5; }
body.pad-sides .tbtn[data-act="hold"]  { grid-area: 4 / 4; font-size: 11px; }
body.pad-sides .tbtn[data-act="drop"]  { grid-area: 4 / 5; font-size: 11px; }

/* ── mode picker ───────────────────────────────────────────────────── */
.logo-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; }

.modes {
  display: grid;
  gap: 9px;
  width: 100%;
}
.mode {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  padding: 12px 18px;
  text-align: left;
  cursor: pointer;
  border-radius: 14px;
  border: 1px solid var(--edge);
  background: linear-gradient(100deg, rgba(60, 180, 255, .1), rgba(180, 92, 255, .07));
  color: inherit;
  transition: transform .12s ease, border-color .18s, box-shadow .18s, background .18s;
}
.mode b {
  font-family: var(--font-display);
  font-size: clamp(14px, 3.2vw, 17px);
  font-weight: 700;
  letter-spacing: .14em;
  color: #eaf7ff;
}
.mode i {
  font-style: normal;
  font-size: 12.5px;
  letter-spacing: .04em;
  color: rgba(165, 205, 245, .6);
}
.mode:hover {
  transform: translateX(3px);
  border-color: rgba(120, 220, 255, .65);
  box-shadow: 0 0 26px rgba(70, 200, 255, .3);
  background: linear-gradient(100deg, rgba(60, 190, 255, .2), rgba(180, 92, 255, .14));
}
.mode:active { transform: scale(.99); }

/* Two players sharing one keyboard needs, well, a keyboard. */
body.touch .mode[data-mode="local"] i::after {
  content: ' · needs a keyboard';
  color: var(--amber);
  opacity: .8;
}

.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.row .cta.ghost { min-width: 0; flex: 1 1 130px; }

.sub {
  font-family: var(--font-display);
  font-size: 12px;
  letter-spacing: .26em;
  color: rgba(160, 200, 255, .55);
  margin-top: 4px;
}

/* ── online lobby ──────────────────────────────────────────────────── */
.lobby {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
}
.lobby.hidden { display: none; }

.join { display: flex; gap: 9px; width: 100%; }
.join input {
  flex: 1;
  min-width: 0;
  padding: 13px 16px;
  font-family: var(--font-display);
  font-size: clamp(17px, 5vw, 23px);
  font-weight: 700;
  letter-spacing: .34em;
  text-align: center;
  text-transform: uppercase;
  color: #eaf7ff;
  background: rgba(8, 12, 28, .8);
  border: 1px solid var(--edge);
  border-radius: 999px;
  outline: none;
  transition: border-color .18s, box-shadow .18s;
}
.join input:focus {
  border-color: rgba(120, 220, 255, .8);
  box-shadow: 0 0 22px rgba(70, 200, 255, .35);
}
.join input::placeholder { color: rgba(140, 180, 220, .3); letter-spacing: .3em; }
.join .cta { min-width: 0; padding: 13px 26px; }

.label {
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: .3em;
  color: rgba(160, 200, 255, .55);
}
.code {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(38px, 12vw, 60px);
  letter-spacing: .22em;
  text-indent: .22em;
  color: #fff;
  text-shadow: 0 0 24px rgba(90, 220, 255, .9), 0 0 60px rgba(120, 90, 255, .6);
  user-select: all;
}
.status { font-size: 13.5px; letter-spacing: .08em; color: rgba(170, 210, 250, .7); }
.error {
  font-size: 13.5px;
  letter-spacing: .04em;
  color: #ff7a97;
  padding: 9px 14px;
  border: 1px solid rgba(255, 61, 110, .35);
  background: rgba(255, 61, 110, .1);
  border-radius: 10px;
}
.error.hidden { display: none; }

.spinner {
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 2px solid rgba(120, 190, 255, .2);
  border-top-color: var(--cyan);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg) } }

/* ── settings ──────────────────────────────────────────────────────── */
.settings { display: flex; flex-direction: column; gap: 12px; width: 100%; }

.slider {
  display: grid;
  grid-template-columns: 74px 1fr 38px;
  align-items: center;
  gap: 12px;
}
.slider > span {
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: .18em;
  color: rgba(160, 200, 255, .62);
}
.slider > b {
  font-family: var(--font-display);
  font-size: 13px;
  color: #cfeaff;
  text-align: right;
}
.slider input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 5px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cyan), var(--purple));
  outline: none;
  cursor: pointer;
}
.slider input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 17px; height: 17px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 12px rgba(90, 210, 255, .9);
  cursor: pointer;
}
.slider input[type="range"]::-moz-range-thumb {
  width: 17px; height: 17px;
  border: 0;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 12px rgba(90, 210, 255, .9);
  cursor: pointer;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  letter-spacing: .04em;
  color: rgba(190, 220, 250, .82);
  cursor: pointer;
  text-align: left;
}
.check input {
  appearance: none;
  -webkit-appearance: none;
  width: 19px; height: 19px;
  flex: none;
  border-radius: 6px;
  border: 1px solid var(--edge);
  background: rgba(10, 14, 32, .8);
  cursor: pointer;
  transition: background .16s, border-color .16s, box-shadow .16s;
}
.check input:checked {
  background: linear-gradient(135deg, var(--cyan), var(--purple));
  border-color: transparent;
  box-shadow: 0 0 14px rgba(80, 200, 255, .55);
}
.check input:checked::after {
  content: '✓';
  display: block;
  text-align: center;
  line-height: 17px;
  font-size: 12px;
  font-weight: 700;
  color: #04121c;
}

/* ── versus results ────────────────────────────────────────────────── */
.scoreline {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(32px, 10vw, 54px);
  letter-spacing: .1em;
  color: #fff;
  text-shadow: 0 0 24px rgba(90, 220, 255, .75);
}
.versus-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
}
.versus-stats > div {
  padding: 12px 10px;
  border-radius: 14px;
  border: 1px solid rgba(120, 190, 255, .18);
  background: rgba(120, 190, 255, .05);
}
.versus-stats h4 {
  font-family: var(--font-display);
  font-size: 12px;
  letter-spacing: .16em;
  margin-bottom: 7px;
}
.versus-stats dl {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 3px 8px;
  font-size: 12.5px;
}
.versus-stats dt { color: rgba(160, 200, 245, .6); text-align: left; }
.versus-stats dd { font-family: var(--font-display); color: #e9f6ff; }
.versus-stats .win { color: var(--lime); }
.versus-stats .lose { color: rgba(255, 120, 150, .85); }

@media (max-height: 620px) {
  .panel { gap: 12px; padding-top: 18px; padding-bottom: 18px; }
  .logo { font-size: clamp(34px, 9vw, 58px); }
  .mode { padding: 9px 15px; }
  .mode i { display: none; }
}
@media (max-height: 430px) {
  .keys { display: none; }
  .panel { gap: 10px; }
  .tagline { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .logo, .logo::before, .logo::after, .cta, .newbest, .spinner, .screen {
    animation: none !important;
  }
}
