:root {
  --bg: #0d0a14;
  --panel: #171220;
  --line: #2f2540;
  --ink: #f1e8e2;
  --dim: #a99dae;
  --you: #4de1d2;
  --foe: #ff4d5e;
  --gold: #ffc46b;
  --green: #4ade80;
  --display: 'Teko', 'Arial Narrow', Impact, sans-serif;
  --body: 'Barlow', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--body);
  font-size: 15px;
  line-height: 1.4;
  padding-block: 14px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  overflow-x: hidden;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

.wrap {
  max-width: 940px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Header */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.top h1 {
  font: 700 30px/1 var(--display);
  letter-spacing: .08em;
  text-transform: uppercase;
  margin: 0;
}

.topr {
  display: flex;
  align-items: center;
  gap: 10px;
}

.tag {
  font: 600 12px var(--body);
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--gold);
}

.ping-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255, 255, 255, .06);
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-family: monospace;
  color: var(--dim);
  border: 1px solid var(--line);
}

.ping-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
}

.ping-badge.medium .ping-dot { background: #facc15; }
.ping-badge.high .ping-dot { background: #ef4444; }

.ghost {
  background: none;
  border: 1px solid var(--line);
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 13px;
  color: var(--dim);
  transition: all .15s ease;
}

.ghost:hover {
  border-color: var(--dim);
  color: var(--ink);
}

.ghost:disabled {
  opacity: .4;
  cursor: default;
}

.ghost:focus-visible, .btn:focus-visible, .pad button:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: rgba(23, 18, 32, .75);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--dim);
  transition: all .15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover {
  border-color: var(--gold);
  color: var(--ink);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  pointer-events: none;
}

/* HUD (Health Bars, Clock, Pips) */
.hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 14px;
  align-items: end;
  padding-inline: 6px;
}

.bar {
  min-width: 0;
}

.bar .label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font: 600 21px/1 var(--display);
  letter-spacing: .1em;
  text-transform: uppercase;
  margin-bottom: 5px;
  white-space: nowrap;
}

.bar.right .label {
  flex-direction: row-reverse;
}

.bar .label span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
}

.track {
  position: relative;
  height: 17px;
  background: #08060d;
  border: 1px solid var(--line);
  transform: skewX(-16deg);
  overflow: hidden;
}

.track i {
  position: absolute;
  top: 0;
  bottom: 0;
  display: block;
  width: 100%;
}

.left .track i { left: 0; }
.right .track i { right: 0; }

.trail {
  background: rgba(255, 255, 255, .55);
  transition: width .9s ease .3s;
}

.fill {
  transition: width .14s linear;
}

.left .fill {
  background: linear-gradient(90deg, #17a093, var(--you));
}

.right .fill {
  background: linear-gradient(270deg, #b01e33, var(--foe));
}

.pips {
  display: flex;
  gap: 7px;
  padding-inline: 3px;
}

.pip {
  width: 10px;
  height: 10px;
  border: 1.5px solid var(--dim);
  transform: rotate(45deg);
  transition: all .2s ease;
}

.pip.on {
  background: var(--gold);
  border-color: var(--gold);
}

.clock {
  font: 700 42px/.9 var(--display);
  min-width: 58px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Stage & Canvas */
.stage {
  position: relative;
  aspect-ratio: 16/9;
  max-width: 100%;
  background: #000;
  border: 1px solid var(--line);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.over {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(8, 5, 14, .75);
  backdrop-filter: blur(4px);
  z-index: 10;
}

.panel {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  max-width: 520px;
  width: 100%;
}

.panel h2 {
  font: 700 clamp(34px, 8vw, 72px)/.9 var(--display);
  letter-spacing: .06em;
  text-transform: uppercase;
  margin: 0;
  text-wrap: balance;
}

.panel p {
  margin: 0;
  color: var(--dim);
  font-size: clamp(13px, 1.7vw, 16px);
  max-width: 46ch;
}

.keys {
  display: grid;
  grid-template-columns: auto auto;
  gap: 3px 16px;
  font-size: 13px;
  text-align: left;
  color: var(--dim);
  margin-block: 4px;
}

.keys b {
  color: var(--ink);
  font-weight: 600;
}

.btn {
  background: var(--gold);
  color: #1a1020;
  border: 0;
  padding: 10px 30px 8px;
  font: 700 24px/1 var(--display);
  letter-spacing: .12em;
  text-transform: uppercase;
  clip-path: polygon(9px 0, 100% 0, calc(100% - 9px) 100%, 0 100%);
  transition: background .15s ease;
}

.btn:hover {
  background: #ffd48e;
}

.btn.sm {
  font-size: 19px;
  padding: 8px 18px 6px;
}

.banner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font: 700 clamp(44px, 13vw, 118px)/1 var(--display);
  letter-spacing: .08em;
  text-transform: uppercase;
  text-shadow: 0 4px 34px #000, 0 0 2px #000;
  z-index: 5;
}

.banner.pop span {
  animation: pop .45s cubic-bezier(.2, 1.4, .4, 1);
}

.combo {
  position: absolute;
  left: 14px;
  top: 12px;
  font: 700 clamp(22px, 4.5vw, 38px)/1 var(--display);
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--gold);
  text-shadow: 0 2px 10px #000;
  pointer-events: none;
  z-index: 5;
}

.combo.pop {
  animation: pop .25s ease-out;
}

@keyframes pop {
  from { transform: scale(1.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* Touch Pad */
.pad {
  display: none;
  gap: 14px;
  justify-content: space-between;
  align-items: flex-end;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.pad .cr {
  flex: none;
  display: grid;
  grid-template-areas:
    ".     block"
    "punch kick";
  grid-template-columns: 64px 64px;
  grid-template-rows: 48px 64px;
  gap: 8px;
  align-items: center;
  justify-items: center;
}

.joy {
  position: relative;
  flex: none;
  width: 128px;
  height: 128px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, .05) 0%, rgba(23, 18, 32, .7) 70%);
  border: 1.5px solid rgba(255, 255, 255, .15);
  box-shadow: 0 4px 16px rgba(0, 0, 0, .5);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.joy i {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46px;
  height: 46px;
  margin: -23px 0 0 -23px;
  border-radius: 50%;
  background: rgba(241, 232, 226, .3);
  border: 1.5px solid var(--ink);
  pointer-events: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .6);
  transition: transform .05s ease-out;
}

.pad button {
  width: 64px;
  height: 64px;
  min-width: 0;
  padding: 0;
  justify-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 50%;
  font: 600 21px/1 var(--display);
  letter-spacing: .1em;
  text-transform: uppercase;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform .08s ease, background .1s ease, box-shadow .1s ease;
}

.pad button svg {
  width: 26px;
  height: 26px;
  pointer-events: none;
}

.pad button[data-k="block"] {
  grid-area: block;
  width: 48px;
  height: 48px;
  border-color: var(--gold);
  color: var(--gold);
}

.pad button[data-k="block"] svg {
  width: 20px;
  height: 20px;
}

.pad button[data-k="punch"] {
  grid-area: punch;
  border-color: var(--you);
  color: var(--you);
}

.pad button[data-k="kick"] {
  grid-area: kick;
  border-color: var(--foe);
  color: var(--foe);
}

.pad button.on, .pad button:active {
  transform: scale(.95);
}

.pad button[data-k="punch"].on, .pad button[data-k="punch"]:active {
  background: rgba(77, 225, 210, .28);
  box-shadow: 0 0 16px var(--you);
}

.pad button[data-k="kick"].on, .pad button[data-k="kick"]:active {
  background: rgba(255, 77, 94, .28);
  box-shadow: 0 0 16px var(--foe);
}

.pad button[data-k="block"].on, .pad button[data-k="block"]:active {
  background: rgba(255, 196, 107, .28);
  box-shadow: 0 0 14px var(--gold);
}

.hint {
  margin: 0;
  color: var(--dim);
  font-size: 13px;
  text-align: center;
}

/* Modals & Lobby Form */
.row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.code {
  font: 700 clamp(44px, 12vw, 72px)/1 var(--display);
  letter-spacing: .18em;
  color: var(--gold);
  padding-left: .18em;
}

#bCopyLink {
  background: rgba(255, 196, 107, 0.12);
  border: 1px solid var(--gold);
  color: var(--gold);
  letter-spacing: .08em;
  transition: all .15s ease;
}

#bCopyLink:hover {
  background: var(--gold);
  color: #1a1020;
}

.panel input[type=text] {
  width: min(260px, 100%);
  height: 38px;
  background: #08060d;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 4px 8px;
  font: 600 18px var(--body);
  text-align: center;
  user-select: text;
  -webkit-user-select: text;
}

.panel input.code-in {
  letter-spacing: .3em;
  text-transform: uppercase;
  font-size: 22px;
}

.set {
  display: grid;
  grid-template-columns: auto auto;
  gap: 8px 18px;
  align-items: center;
  text-align: left;
  font-size: 14px;
}

.set input[type=range] {
  width: 140px;
  accent-color: var(--gold);
}

#ns {
  min-height: 1.2em;
}

/* Responsive & Mobile Landscape rotation */
@media (pointer: coarse) {
  .pad { display: flex; }
  .hint { display: none; }
}

@media (max-width: 620px) {
  .keys { display: none; }
  .top h1 { font-size: 26px; }
  .bar .label { font-size: 17px; }
  .clock { font-size: 34px; }
}

@media (pointer: coarse) and (orientation: landscape) and (max-height: 600px) {
  :root {
    --ib: env(safe-area-inset-bottom, 0px);
    --il: env(safe-area-inset-left, 0px);
    --ir: env(safe-area-inset-right, 0px);
    --sh: calc(100dvh - var(--ib));
  }
}

@media (pointer: coarse) and (orientation: portrait) {
  :root {
    --ib: 0px;
    --il: env(safe-area-inset-top, 0px);
    --ir: env(safe-area-inset-bottom, 0px);
    --sh: 100vw;
  }
  body {
    position: fixed;
    top: 0;
    left: 100%;
    width: 100vh;
    width: 100dvh;
    height: 100vw;
    overflow: hidden;
    transform-origin: 0 0;
    transform: rotate(90deg);
    padding-left: max(12px, var(--il));
    padding-right: max(12px, var(--ir));
  }
}

@media (pointer: coarse) and (orientation: landscape) and (max-height: 600px),
       (pointer: coarse) and (orientation: portrait) {
  body {
    padding-block: 4px;
    padding-left: max(12px, var(--il));
    padding-right: max(12px, var(--ir));
  }
  .wrap {
    gap: 4px;
    max-width: none;
    height: 100%;
    pointer-events: none;
  }
  .top {
    position: relative;
    z-index: 20;
    pointer-events: auto;
    padding-inline: 4px;
    gap: 8px;
  }
  .game-title {
    display: none;
  }
  .tag {
    font-size: 13px;
  }
  .hud {
    position: relative;
    z-index: 20;
    gap: 8px;
    padding-inline: 4px;
    align-items: center;
  }
  .bar .label {
    font-size: 15px;
    margin-bottom: 2px;
  }
  .track {
    height: 14px;
  }
  .clock {
    font-size: 32px;
    min-width: 44px;
  }
  .pips {
    gap: 5px;
  }
  .pip {
    width: 8px;
    height: 8px;
  }
  .stage {
    position: fixed;
    inset: 0;
    aspect-ratio: auto;
    border: 0;
    background: #08060d;
    z-index: 0;
    pointer-events: auto;
  }
  .stage canvas {
    object-fit: contain;
    width: 100%;
    height: 100%;
  }
  .combo {
    top: 56px;
    left: max(14px, var(--il));
    font-size: clamp(20px, 3.8vw, 32px);
  }
  .banner {
    font-size: clamp(40px, 11vw, 84px);
  }
  .keys {
    display: none;
  }
  .over {
    z-index: 30;
    pointer-events: auto;
    padding: 10px;
  }
  .panel {
    gap: 6px;
    max-height: 90dvh;
    max-width: 520px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px 16px;
  }
  .panel h2 {
    font-size: clamp(26px, 6vw, 40px);
  }
  .panel p {
    font-size: 13px;
    line-height: 1.3;
  }
  .panel input[type=text] {
    height: 36px;
    font-size: 16px;
  }
  .set {
    gap: 6px 14px;
    font-size: 13px;
  }
  .btn {
    padding: 7px 22px 5px;
    font-size: 20px;
  }
  .btn.sm {
    padding: 6px 14px 4px;
    font-size: 16px;
  }
  .pad {
    position: fixed;
    left: max(14px, var(--il));
    right: max(14px, var(--ir));
    bottom: max(12px, var(--ib));
    justify-content: space-between;
    pointer-events: none;
    z-index: 25;
  }
  .pad .cr {
    flex: none;
    grid-template-columns: 56px 56px;
    grid-template-rows: 42px 56px;
    gap: 8px;
    pointer-events: none;
  }
  .pad button {
    pointer-events: auto;
    background: rgba(23, 18, 32, .82);
    backdrop-filter: blur(2px);
  }
  .pad button[data-k="punch"], .pad button[data-k="kick"] {
    width: 56px;
    height: 56px;
  }
  .pad button[data-k="block"] {
    width: 42px;
    height: 42px;
  }
  .pad button svg {
    width: 24px;
    height: 24px;
  }
  .pad button[data-k="block"] svg {
    width: 18px;
    height: 18px;
  }
  .joy {
    pointer-events: auto;
    width: 114px;
    height: 114px;
    background: radial-gradient(circle, rgba(255, 255, 255, .05) 0%, rgba(23, 18, 32, .75) 70%);
    border: 1.5px solid rgba(255, 255, 255, .18);
    box-shadow: 0 4px 16px rgba(0, 0, 0, .5);
    backdrop-filter: blur(2px);
  }
  .joy i {
    width: 42px;
    height: 42px;
    margin: -21px 0 0 -21px;
    background: rgba(241, 232, 226, .35);
    border: 1.5px solid var(--ink);
    box-shadow: 0 2px 8px rgba(0, 0, 0, .6);
  }
}

@media (prefers-reduced-motion: reduce) {
  .banner.pop span, .combo.pop { animation: none; }
  .trail { transition: none; }
}

/* On-Screen Diagnostic Lockstep HUD & Error Logging */
.net-diag {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(14, 11, 20, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  padding: 4px 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 11px;
  line-height: 1.35;
  color: #c9c0d3;
  z-index: 20;
  pointer-events: none;
  backdrop-filter: blur(4px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.diag-line {
  display: flex;
  align-items: center;
  gap: 4px;
}

.diag-sep {
  color: rgba(255, 255, 255, 0.25);
}

.diag-tag {
  font-weight: 700;
  font-size: 10px;
  padding: 1px 4px;
  border-radius: 3px;
  letter-spacing: .04em;
}

.tag-ok {
  background: rgba(77, 225, 210, 0.2);
  color: #4de1d2;
  border: 1px solid rgba(77, 225, 210, 0.4);
}

.tag-wait {
  background: rgba(255, 196, 107, 0.25);
  color: #ffc46b;
  border: 1px solid rgba(255, 196, 107, 0.5);
}

.tag-stall {
  background: rgba(255, 77, 94, 0.25);
  color: #ff4d5e;
  border: 1px solid rgba(255, 77, 94, 0.6);
  animation: pulse-stall 1s infinite alternate;
}

@keyframes pulse-stall {
  from { opacity: 0.7; }
  to { opacity: 1; }
}

/* Global On-Screen Error Banner */
.err-banner {
  position: fixed;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: min(92vw, 540px);
  background: rgba(230, 40, 60, 0.95);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  z-index: 9999;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
  animation: slide-err 0.25s ease-out;
}

@keyframes slide-err {
  from { transform: translate(-50%, -20px); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}

.err-text {
  flex: 1;
  word-break: break-word;
}

.err-close {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
