:root {
  --bg: #0b0b0f;
  --panel: #16161d;
  --panel-2: #20202a;
  --line: #2c2c38;
  --text: #f1f1f4;
  --muted: #9a9aab;
  --accent: #e0182d;
  --accent-2: #ffb300;
  --ok: #2ecc71;
  --warn: #f5a623;
  --err: #ff4d4f;
  --topbar: 44px;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- buttons ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}
.btn:hover { border-color: #444456; }
.btn.primary { background: var(--accent); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn.big { font-size: 18px; padding: 14px 28px; }
.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); }

/* ---------- home ---------- */
#home {
  max-width: 760px;
  margin: 0 auto;
  padding: 32px 16px 48px;
}
.brand h1 {
  margin: 0;
  font-size: clamp(34px, 8vw, 56px);
  letter-spacing: 0.06em;
  font-weight: 900;
}
.brand h1 span { color: var(--accent); }
.brand p { margin: 4px 0 28px; color: var(--muted); }
.join { margin-bottom: 28px; }
.join label { display: block; color: var(--muted); margin-bottom: 6px; }
.join-row { display: flex; gap: 8px; }
.join input {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  font: 700 22px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  padding: 10px 14px;
}
.lobby { margin-bottom: 28px; }
.lobby h2 { margin: 0 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; }
#lobby-list { display: grid; gap: 8px; }
#lobby-list > p { margin: 0; }
.lobby-room {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 14px;
}
.lobby-game { font-size: 12px; color: var(--accent-2); text-transform: uppercase; letter-spacing: 0.05em; }
.lobby-state { color: var(--muted); font-size: 13px; white-space: nowrap; }
.games { display: grid; gap: 14px; }
.game {
  background: linear-gradient(135deg, #2a0d12 0%, var(--panel) 55%);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
}
.game.disabled { opacity: 0.55; background: var(--panel); }
.game-title h2 { margin: 0; font-size: 22px; }
.game-title p { margin: 2px 0 14px; color: var(--muted); }
.game-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.game-actions .btn { flex: 1 1 180px; }
.footnote { color: var(--muted); font-size: 13px; margin-top: 24px; }

/* ---------- invite ---------- */
#invite { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.invite-card { display: grid; gap: 14px; justify-items: center; text-align: center; }
.invite-code { font: 900 56px/1 ui-monospace, Menlo, monospace; letter-spacing: 0.2em; }

/* ---------- play screen ---------- */
body.playing { overflow: hidden; overscroll-behavior: none; touch-action: none; }
#play {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: #000;
  user-select: none;
  -webkit-user-select: none;
}
.topbar {
  flex: 0 0 var(--topbar);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 calc(6px + var(--safe-r)) 0 calc(6px + var(--safe-l));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 3;
  white-space: nowrap;
  overflow: hidden;
}
.topbar .spacer { flex: 1; }
.room b { font-family: ui-monospace, Menlo, monospace; letter-spacing: 0.15em; font-size: 17px; }
.status { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.status i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status[data-state="ok"] i, .status[data-state="connected"] i { background: var(--ok); }
.status[data-state="waiting"] i, .status[data-state="connecting"] i { background: var(--warn); animation: blink 1s infinite; }
.status[data-state="error"] i { background: var(--err); }
.status[data-state="error"] span { color: var(--err); }
@keyframes blink { 50% { opacity: 0.3; } }

/* The stage is the largest 4:3 box that fits the space left by the top bar (and controls). */
.stage-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  place-items: center;
  container-type: size;
}
.stage {
  position: relative;
  width: min(100cqw, 100cqh * 4 / 3);
  aspect-ratio: 4 / 3;
  background: #000;
  overflow: hidden;
}
.stage video, #ejs-target {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
}
.stage video { object-fit: contain; background: #000; }
#ejs-target canvas { width: 100% !important; height: 100% !important; }
/* EmulatorJS chrome we don't use: its menu bar, virtual pad, popups. */
#ejs-target .ejs_menu_bar,
#ejs-target .ejs_virtualGamepad_parent,
#ejs-target .ejs_context_menu,
#ejs-target .ejs_menu_button,
#ejs-target .ejs_virtualGamepad_open { display: none !important; }
.stage-msg {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.72);
  z-index: 2;
}
.stage-msg.clickable { cursor: pointer; }

/* ---------- touch controls ---------- */
.touch {
  --pad-scale: 1;
  --pad-opacity: 0.55;
  --btn: calc(58px * var(--pad-scale));
  --stick: calc(124px * var(--pad-scale));
  position: relative;
  touch-action: none;
}
.stick-zone, .action-zone, .sys-zone { position: absolute; touch-action: none; }
.stick-base {
  position: absolute;
  left: 50%;
  top: 55%;
  width: var(--stick);
  height: var(--stick);
  margin: calc(var(--stick) / -2) 0 0 calc(var(--stick) / -2);
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.35);
  background: radial-gradient(circle, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.02));
  opacity: calc(var(--pad-opacity) * 0.8);
  pointer-events: none;
}
.stick-zone.active .stick-base { opacity: var(--pad-opacity); }
.stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 44%;
  height: 44%;
  margin: -22% 0 0 -22%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.55);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}
.buttons { display: grid; gap: calc(10px * var(--pad-scale)); }
.btn-row { display: flex; gap: calc(10px * var(--pad-scale)); justify-content: flex-end; }
.pad-btn, .sys-btn, .special-btn {
  appearance: none;
  border: 2px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  font: 800 calc(14px * var(--pad-scale)) / 1 system-ui, sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  opacity: var(--pad-opacity);
  cursor: pointer;
}
.pad-btn {
  width: var(--btn);
  height: var(--btn);
  border-radius: 50%;
  background: rgba(40, 40, 52, 0.85);
}
.pad-btn[data-label="HP"], .pad-btn[data-label="LP"] { background: rgba(200, 30, 45, 0.85); }
.pad-btn[data-label="HK"], .pad-btn[data-label="LK"] { background: rgba(30, 110, 200, 0.85); }
.pad-btn[data-label="BLK"] { background: rgba(210, 160, 20, 0.85); }
.pad-btn[data-label="RUN"] { background: rgba(47, 143, 78, 0.85); }
/* NES / Dendy: red A and B like the original pad, a bit bigger since there are only two. */
.pad-btn[data-label="A"], .pad-btn[data-label="B"] { background: rgba(200, 30, 45, 0.85); width: calc(var(--btn) * 1.2); height: calc(var(--btn) * 1.2); }
.pad-btn.down, .sys-btn.down, .special-btn.down { opacity: 1; transform: scale(0.94); filter: brightness(1.35); }
.sys-zone { display: flex; gap: 10px; }
.sys-btn {
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(40, 40, 52, 0.85);
  font-size: 12px;
}
.specials {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  margin-bottom: calc(10px * var(--pad-scale));
  max-width: calc(var(--btn) * 3 + 40px);
  margin-left: auto;
}
.special-btn {
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(90, 30, 120, 0.85);
  font-size: calc(12px * var(--pad-scale));
  font-weight: 700;
}

/* Portrait phone: game on top, controls fill the rest below it.
   body.portrait / body.landscape are set by JS: a portrait phone can show the landscape layout
   turned 90° (body.rotated), so the orientation media query alone can't tell. */
body.portrait.with-touch .stage-wrap { flex: 0 0 auto; height: calc(100vw * 3 / 4); }
body.portrait.with-touch .touch { flex: 1 1 auto; min-height: 220px; }
body.portrait .stick-zone { left: 0; top: 0; bottom: 0; width: 50%; }
body.portrait .action-zone { right: calc(14px + var(--safe-r)); bottom: calc(56px + var(--safe-b)); }
body.portrait .sys-zone { left: 50%; transform: translateX(-50%); bottom: calc(12px + var(--safe-b)); }

/* Landscape phone: game centred full-height, controls overlay the black side bars.
   The top bar floats over the black side bars so the game gets the full height. */
body.landscape.with-touch { --topbar: 0px; }
body.landscape.with-touch .topbar {
  position: absolute;
  inset: 0 0 auto 0;
  height: 40px;
  background: transparent;
  border: 0;
  pointer-events: none;
}
body.landscape.with-touch .topbar > * { pointer-events: auto; }
body.landscape.with-touch .topbar .status span { display: none; }
body.landscape.with-touch .topbar .icon-btn, body.landscape.with-touch .topbar .btn { opacity: 0.6; }
body.landscape.with-touch .touch { position: absolute; inset: var(--topbar) 0 0 0; pointer-events: none; z-index: 2; }
body.landscape.with-touch .touch > * { pointer-events: auto; }
body.landscape .stick-zone { left: 0; top: 0; bottom: 0; width: 40%; }
/* Resting spot; while touched, JS moves it under the thumb with inline left/top. */
body.landscape .stick-base { left: calc(var(--stick) / 2 + 28px + var(--safe-l)); top: calc(100% - var(--stick) / 2 - 36px); }
body.landscape .action-zone { right: calc(16px + var(--safe-r)); bottom: calc(20px + var(--safe-b)); }
body.landscape .sys-zone { left: 50%; transform: translateX(-50%); bottom: 6px; }

/* Portrait phone set to "always landscape" (iPhone can't lock the orientation, Android may fail
   to): the play screen is turned 90° clockwise, so it reads right with the phone turned left.
   --vw / --vh hold the real window size (JS); dialogs are turned the same way. */
body.rotated { --safe-l: env(safe-area-inset-top, 0px); --safe-r: env(safe-area-inset-bottom, 0px); --safe-b: 0px; }
body.rotated #play {
  inset: auto;
  top: 0;
  left: 0;
  width: var(--vh);
  height: var(--vw);
  transform-origin: 0 0;
  transform: translateX(var(--vw)) rotate(90deg);
}
body.rotated dialog { inset: auto; left: 50%; top: 50%; margin: 0; transform: translate(-50%, -50%) rotate(90deg); width: min(440px, calc(var(--vh) - 32px)); max-width: none; max-height: calc(var(--vw) - 32px); }
body.rotated dialog.wide { width: min(720px, calc(var(--vh) - 24px)); }

/* ---------- settings ---------- */
dialog {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.settings { display: grid; gap: 14px; padding: 20px; }
.settings h2 { margin: 0; }
.field { display: grid; gap: 6px; }
.field span { color: var(--muted); font-size: 13px; }
.field output { color: var(--text); margin-left: 6px; }
.field select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  font: inherit;
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.help { font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 10px; }
.help p { margin: 0 0 6px; }

@media (max-width: 520px) {
  .room .muted, .status span { font-size: 12px; }
}

/* ---------- controls reference ("Перед боем") ---------- */
dialog.wide { width: min(720px, calc(100vw - 24px)); }
.briefing { display: grid; gap: 18px; padding: 20px; }
.briefing header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.briefing header h2 { margin: 0; font-size: 24px; }
.briefing-close { flex: none; font-size: 22px; margin: -6px -6px 0 0; }
.briefing header p { margin: 0; text-transform: uppercase; letter-spacing: 0.12em; font-size: 12px; }
.briefing h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.briefing footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 12px;
  position: sticky;
  bottom: -20px;
  margin-bottom: -20px;
  background: var(--panel);
  padding: 12px 0 20px;
  border-top: 1px solid var(--line);
}
.briefing footer .check { flex: 1 1 100%; }
.briefing footer .btn.big { flex: 1 1 auto; }
.briefing details summary { cursor: pointer; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; font-weight: 700; }
.briefing details[open] summary { margin-bottom: 8px; }
.briefing .small { font-size: 12px; margin: 8px 0 0; }
.check { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 18px; height: 18px; }
table.ref { width: 100%; border-collapse: collapse; font-size: 14px; }
table.ref th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 0 8px 6px; }
table.ref td { padding: 7px 8px; border-top: 1px solid var(--line); vertical-align: middle; }
table.ref td:first-child { width: 1%; white-space: nowrap; }
table.ref .move { white-space: nowrap; }
kbd {
  display: inline-block;
  min-width: 26px;
  padding: 3px 7px;
  border: 1px solid #44445a;
  border-bottom-width: 3px;
  border-radius: 6px;
  background: var(--panel-2);
  font: 700 13px/1.2 ui-monospace, Menlo, monospace;
  text-align: center;
  margin: 1px 0;
}
.chip {
  display: inline-block;
  min-width: 36px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #3a3a4a;
  color: #fff;
  font: 800 12px/1.3 system-ui, sans-serif;
  text-align: center;
}
.chip-HP, .chip-LP { background: #c81e2d; }
.chip-HK, .chip-LK { background: #1e6ec8; }
.chip-BLK { background: #d2a014; color: #111; }
.chip-RUN { background: #2f8f4e; }
.arrow { font-size: 18px; color: var(--accent-2); margin: 0 1px; }
.touch-help { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: center; }
.touch-help p { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.touch-help-stick { text-align: center; }
.touch-help .ring { width: 74px; height: 74px; border: 2px solid #555; border-radius: 50%; margin: 0 auto; position: relative; }
.touch-help .ring i { position: absolute; width: 30px; height: 30px; left: 40px; top: 12px; border-radius: 50%; background: #888; }
.touch-help-btns { text-align: center; }
.touch-help-btns div { display: flex; gap: 6px; justify-content: center; margin-bottom: 6px; }
@media (max-width: 520px) {
  table.ref { font-size: 13px; }
  table.ref td { padding: 6px 4px; }
  .briefing { padding: 16px; }
}
.keybind {
  appearance: none;
  background: transparent;
  border: 1px dashed transparent;
  border-radius: 8px;
  padding: 2px 4px;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.keybind:hover { border-color: #55556a; background: rgba(255, 255, 255, 0.04); }
.keybind.listening {
  border: 1px solid var(--accent-2);
  color: var(--accent-2);
  font-size: 13px;
  font-weight: 700;
  padding: 5px 8px;
  animation: blink 1s infinite;
}
.unbound { color: var(--err); font-size: 12px; font-weight: 700; }

.steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; }
.ios-share { display: inline-block; transform: rotate(-90deg); }
.chip-A, .chip-B { background: #c81e2d; }
.dialog-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.dialog-actions .btn { flex: 1 1 140px; }
.btn.resume { background: #1f6f3f; border-color: #2f8f4e; flex-basis: 100% !important; }
.btn.resume small { font-weight: 400; opacity: 0.8; margin-left: 6px; }
/* Two players on one keyboard makes no sense on a phone. */
@media (pointer: coarse) { .game button[data-act="duo"] { display: none; } }
