:root {
  --accent: #ff4d5a;
  --bg0: #10131c;
  --bg1: #1b2030;
  --text: #f2f4fa;
  --muted: rgba(242, 244, 250, 0.6);
  --glass: rgba(255, 255, 255, 0.07);
  --line: rgba(255, 255, 255, 0.14);
  --sal: env(safe-area-inset-left);
  --sar: env(safe-area-inset-right);
  --sab: env(safe-area-inset-bottom);
  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg0);
  color: var(--text);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button { font: inherit; color: inherit; }

/* The pad */

#pad {
  position: fixed;
  inset: 0;
  touch-action: none;
  background:
    radial-gradient(120% 90% at 50% 120%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 60%),
    radial-gradient(80% 60% at 0% 0%, color-mix(in srgb, var(--bg1) 90%, transparent), transparent 70%),
    linear-gradient(160deg, var(--bg1), var(--bg0));
  transition: background 0.4s;
}
#pad::after {   /* faint dot grid: depth without clutter */
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1.2px);
  background-size: 22px 22px;
  mask-image: linear-gradient(to bottom, transparent, #000 30%, #000 70%, transparent);
}

.ctl { position: absolute; --c: #e9edf7; z-index: 1; }

/* Buttons */

.ctl.button .face,
.ctl.latch .face,
.ctl.option .face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.1em;
  border-radius: 0.9em;
  color: color-mix(in srgb, var(--c) 70%, white);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0) 55%),
    color-mix(in srgb, var(--c) 16%, rgba(20, 22, 30, 0.55));
  border: 1.5px solid color-mix(in srgb, var(--c) 45%, transparent);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.18),
    inset 0 -6px 14px rgba(0, 0, 0, 0.25),
    0 8px 20px rgba(0, 0, 0, 0.35);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  transition: transform 70ms ease-out, background 70ms, box-shadow 120ms, border-color 70ms;
}
.ctl.round .face { border-radius: 50%; }
.ctl.button b { font-weight: 800; letter-spacing: 0.04em; line-height: 1; }
.ctl.button small {
  font-size: 0.55em;
  font-weight: 700;
  letter-spacing: 0.06em;
  opacity: 0.6;
  line-height: 1;
}
.ctl.button .ico { width: 1.6em; height: 1.6em; margin-bottom: 0.1em; opacity: 0.9; }

.ctl.button.primary .face {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.22), rgba(255, 255, 255, 0) 60%),
    color-mix(in srgb, var(--c, var(--accent)) 42%, rgba(15, 16, 22, 0.6));
  border-color: color-mix(in srgb, var(--c) 75%, white 10%);
  color: white;
}
.ctl.button.primary:not([style*="--c"]) { --c: var(--accent); }

.ctl.button.down .face {
  transform: scale(0.92);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.05)),
    color-mix(in srgb, var(--c) 70%, rgba(15, 16, 22, 0.4));
  border-color: color-mix(in srgb, var(--c) 90%, white);
  color: white;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 0 0 4px color-mix(in srgb, var(--c) 25%, transparent),
    0 0 32px color-mix(in srgb, var(--c) 55%, transparent);
}

.ctl.shoulder .face { border-radius: 0.8em; }
.ctl.shoulder b { font-size: 0.95em; }
.ctl.system .face { border-radius: 999px; font-size: 1.4em; }
.ctl.system b { font-weight: 600; }

/* Toggles: lock gas, auto-fire, tilt */

.ctl.latch .face,
.ctl.option .face {
  flex-direction: row;
  gap: 0.45em;
  border-radius: 999px;
  font-size: 0.85em;
  font-weight: 800;
  letter-spacing: 0.08em;
  --c: #c9cedc;
}
.ctl.latch .face::before {
  content: "";
  width: 0.6em; height: 0.6em; border-radius: 50%;
  background: currentColor; opacity: 0.35;
}
.ctl.option .ico { width: 1.3em; height: 1.3em; }
.ctl.latch.on .face,
.ctl.option.on .face {
  --c: var(--accent);
  color: #111;
  background: var(--accent);
  border-color: transparent;
  box-shadow: 0 0 24px color-mix(in srgb, var(--accent) 60%, transparent);
}
.ctl.latch.on .face::before { opacity: 1; box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.2); }

/* Sticks */

.ctl.stick .base {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  opacity: 0.55;
  transition: opacity 120ms;
}
.ctl.stick.down .base { opacity: 1; transition: none; }
.ctl.stick .ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.16);
  background: radial-gradient(circle, rgba(255, 255, 255, 0.02) 30%, rgba(255, 255, 255, 0.07) 70%, rgba(255, 255, 255, 0.02));
  box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.35);
}
.ctl.stick.down .ring { border-color: color-mix(in srgb, var(--accent) 60%, transparent); }
.ctl.stick .knob {
  position: absolute;
  left: 50%; top: 50%;
  width: 46%; height: 46%;
  margin: -23% 0 0 -23%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff, #c9cfdd 45%, #8d94a6);
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.45), inset 0 -4px 8px rgba(0, 0, 0, 0.25);
}
.ctl.stick.down .knob { box-shadow: 0 10px 22px rgba(0, 0, 0, 0.45), 0 0 26px color-mix(in srgb, var(--accent) 50%, transparent); }
.ctl.stick .cap {
  position: absolute;
  left: 50%;
  bottom: -1.9em;
  transform: translateX(-50%);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.18em;
  color: var(--muted);
  white-space: nowrap;
}
.ctl.stick.down .cap { opacity: 0; }

/* Look area */

.ctl.look { z-index: 0; }
.ctl.look .hint {
  position: absolute;
  left: 50%; top: 22%;
  transform: translateX(-50%);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.22);
  white-space: nowrap;
}
.ctl.look.down { background: radial-gradient(60% 60% at 50% 50%, rgba(255, 255, 255, 0.04), transparent); }

/* D-pad */

.ctl.dpad .arm {
  position: absolute;
  width: 34%; height: 34%;
  border-radius: 22%;
  background: color-mix(in srgb, #e9edf7 12%, rgba(20, 22, 30, 0.6));
  border: 1.5px solid rgba(255, 255, 255, 0.16);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12);
}
.ctl.dpad .up { left: 33%; top: 0; }
.ctl.dpad .down { left: 33%; bottom: 0; }
.ctl.dpad .left { left: 0; top: 33%; }
.ctl.dpad .right { right: 0; top: 33%; }
.ctl.dpad .hub { position: absolute; left: 33%; top: 33%; width: 34%; height: 34%; background: rgba(20, 22, 30, 0.6); }
.ctl.dpad[data-dir~="up"] .up,
.ctl.dpad[data-dir~="down"] .down,
.ctl.dpad[data-dir~="left"] .left,
.ctl.dpad[data-dir~="right"] .right {
  background: var(--accent);
  border-color: transparent;
  box-shadow: 0 0 18px color-mix(in srgb, var(--accent) 60%, transparent);
}

/* Cuphead: 1930s cartoon. Cream buttons, ink outlines, hard shadows, film grain. */

#pad.ink {
  background:
    radial-gradient(90% 80% at 50% 50%, transparent 55%, rgba(0, 0, 0, 0.55)),
    radial-gradient(120% 90% at 50% 120%, rgba(232, 184, 74, 0.25), transparent 60%),
    linear-gradient(160deg, var(--bg1), var(--bg0));
}
#pad.ink::after {
  background-image:
    repeating-radial-gradient(circle at 17% 32%, rgba(255, 240, 210, 0.05) 0 1px, transparent 1px 3px),
    repeating-radial-gradient(circle at 73% 61%, rgba(0, 0, 0, 0.1) 0 1px, transparent 1px 4px);
  background-size: auto;
  mask-image: none;
  animation: grain 0.5s steps(3) infinite;
}
@keyframes grain { 33% { transform: translate(1px, -1px); } 66% { transform: translate(-1px, 1px); } }
#pad.ink .ctl.button .face {
  color: #1a120b;
  background: linear-gradient(180deg, #fbf0d6, #e9d3a6);
  border: 3px solid #1a120b;
  box-shadow: 4px 5px 0 #1a120b;
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  font-family: "Didot", "Bodoni 72", Georgia, serif;
}
#pad.ink .ctl.button.primary .face { background: linear-gradient(180deg, color-mix(in srgb, var(--c) 55%, #fbf0d6), color-mix(in srgb, var(--c) 80%, #e9d3a6)); }
#pad.ink .ctl.button b { font-style: italic; font-weight: 900; letter-spacing: 0.02em; }
#pad.ink .ctl.button.down .face { transform: translate(3px, 4px); box-shadow: 1px 1px 0 #1a120b; }
#pad.ink .ctl.stick .ring { border: 3px solid rgba(251, 240, 214, 0.35); }
#pad.ink .ctl.stick .knob { background: radial-gradient(circle at 35% 30%, #fff8e6, #e9d3a6 55%, #b9965a); border: 3px solid #1a120b; }

/* Edit mode */

#pad.editing .ctl:not(.look) { outline: 1.5px dashed rgba(255, 255, 255, 0.35); outline-offset: 4px; border-radius: 1em; }
#pad.editing .ctl.round, #pad.editing .ctl.stick { border-radius: 50%; }
#pad.editing .ctl.selected { outline: 2px solid var(--accent); }
#pad.editing .ctl.stick .base { opacity: 1; }

#editbar {
  position: fixed;
  left: 50%; top: max(10px, env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 16px;
  border-radius: 999px;
  background: rgba(14, 16, 24, 0.85);
  border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  font-size: 13px;
  white-space: nowrap;
}
#editbar[hidden] { display: none; }
#editbar button { border: 0; border-radius: 999px; padding: 7px 14px; background: var(--glass); font-weight: 700; }
#editbar button.primary { background: var(--accent); color: #111; }
#edit-size { width: 120px; accent-color: var(--accent); }

/* HUD */

#hud {
  position: fixed;
  left: 50%;
  top: max(8px, env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
  border-radius: 999px;
  background: rgba(10, 12, 18, 0.55);
  border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
#hud[hidden] { display: none; }
.badge {
  --p: #666;
  min-width: 44px;
  padding: 6px 12px;
  border-radius: 999px;
  text-align: center;
  font-weight: 900;
  font-size: 13px;
  letter-spacing: 0.06em;
  color: #0c0d12;
  background: var(--p);
  box-shadow: 0 0 16px color-mix(in srgb, var(--p) 55%, transparent);
  transition: background 0.3s;
}
#hud.offline .badge { animation: pulse 1.2s ease-in-out infinite; color: #ddd; }
@keyframes pulse { 50% { opacity: 0.45; } }
.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: var(--glass);
}
.icon-btn .ico { width: 18px; height: 18px; }

#tip {
  position: fixed;
  left: 50%; bottom: max(8px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  margin: 0;
  z-index: 2;
  max-width: 40vw;
  text-align: center;
  font-size: 11px;
  line-height: 1.35;
  color: rgba(255, 255, 255, 0.35);
  pointer-events: none;
}

/* Settings sheet */

#sheet {
  position: fixed;
  inset: 0;
  z-index: 6;
  background: rgba(0, 0, 0, 0);
  pointer-events: none;
  transition: background 0.25s;
}
#sheet.open { background: rgba(0, 0, 0, 0.45); pointer-events: auto; }
#sheet .panel {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: min(380px, 70vw);
  padding: max(14px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) 18px 18px;
  overflow-y: auto;
  background: rgba(16, 18, 26, 0.92);
  border-left: 1px solid var(--line);
  -webkit-backdrop-filter: blur(24px);
  backdrop-filter: blur(24px);
  transform: translateX(100%);
  transition: transform 0.32s cubic-bezier(0.2, 0.9, 0.25, 1);
  -webkit-user-select: text;
  user-select: text;
}
#sheet.open .panel { transform: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.panel h2 { margin: 0; font-size: 20px; font-weight: 800; }
.panel section { margin-top: 18px; }
.panel h3 { margin: 0 0 8px; font-size: 11px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); }
.panel p { margin: 8px 0 0; font-size: 12px; line-height: 1.45; color: var(--muted); }
.panel select, .panel input:not([type]) {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--glass);
  color: var(--text);
  font: inherit;
  font-size: 15px;
}
.panel .wide {
  width: 100%;
  margin-top: 8px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--glass);
  font-weight: 700;
}
.panel .wide .ico { width: 18px; height: 18px; }
.seg { display: flex; padding: 3px; border-radius: 12px; background: var(--glass); border: 1px solid var(--line); }
.seg button { flex: 1; padding: 9px 4px; border: 0; border-radius: 9px; background: none; font-weight: 700; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--accent); color: #111; }
.row { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; font-size: 15px; }
.row input { width: 22px; height: 22px; accent-color: var(--accent); }

/* Start gate */

#gate {
  position: fixed;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  padding: 16px;
  background:
    radial-gradient(80% 70% at 50% 100%, color-mix(in srgb, var(--accent) 30%, transparent), transparent 70%),
    rgba(8, 9, 14, 0.82);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
}
#gate[hidden] { display: none; }
#gate .card { text-align: center; max-width: 480px; max-height: 100%; overflow-y: auto; display: flex; flex-direction: column; align-items: center; }
#gate h1 { margin: 18px 0 6px; font-size: clamp(22px, 5vh, 34px); font-weight: 900; letter-spacing: -0.01em; }
#gate p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.45; }
.logo { display: inline-flex; gap: 6px; }
.logo span { width: 26px; height: 46px; border-radius: 14px; background: #ff4d5a; box-shadow: 0 0 30px rgba(255, 77, 90, 0.5); }
.logo span + span { background: #3d8bff; box-shadow: 0 0 30px rgba(61, 139, 255, 0.5); }
#gate:not(.ready) .logo span { animation: bob 1.4s ease-in-out infinite; }
#gate:not(.ready) .logo span + span { animation-delay: 0.2s; }
@keyframes bob { 50% { transform: translateY(-6px); } }
.play {
  margin-top: 22px;
  min-width: 200px;
  padding: 15px 28px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #111;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: 0.04em;
  box-shadow: 0 10px 30px color-mix(in srgb, var(--accent) 45%, transparent);
  transition: transform 0.1s, opacity 0.2s;
}
.play:disabled { opacity: 0.35; box-shadow: none; }
.play:active:not(:disabled) { transform: scale(0.96); }

/* Portrait */

#rotate {
  position: fixed;
  inset: 0;
  z-index: 9;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: var(--bg0);
  color: var(--muted);
  font-weight: 700;
}
#rotate .phone {
  width: 46px; height: 80px;
  border: 3px solid var(--text);
  border-radius: 12px;
  animation: turn 1.8s ease-in-out infinite;
}
@keyframes turn { 40%, 70% { transform: rotate(-90deg); } }
@media (orientation: portrait) and (max-width: 900px) { #rotate { display: flex; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Start screen: games, code, desktop QR */

#games { display: grid; gap: 10px; margin-top: 18px; }
.game {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 14px;
  width: min(440px, 80vw);
  padding: 12px 12px 12px 18px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  text-align: left;
  transition: transform 0.1s, background 0.2s;
}
.game:active:not(:disabled) { transform: scale(0.98); background: rgba(255, 255, 255, 0.1); }
.game .g { font-size: 17px; font-weight: 800; }
.game .h { grid-row: 2; font-size: 12px; color: var(--muted); }
.game .go {
  grid-row: 1 / 3; grid-column: 2;
  padding: 10px 20px;
  border-radius: 999px;
  background: var(--accent);
  color: #111;
  font-weight: 900;
}
.game:disabled { opacity: 0.55; }
.game:disabled .go { display: none; }
#code-form { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }
#code-form[hidden] { display: none; }
#code {
  width: 140px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font: 800 18px/1 var(--font);
  letter-spacing: 0.25em;
  text-align: center;
  text-transform: uppercase;
}
#code::placeholder { color: rgba(255, 255, 255, 0.25); }
#code-form button { padding: 10px 18px; border: 0; border-radius: 12px; background: rgba(255, 255, 255, 0.12); font-weight: 800; }
#gate-error { min-height: 1.4em; margin-top: 10px !important; color: #ff8a8a !important; }
#gate.desktop .card { max-width: 640px; }
.host { display: flex; align-items: center; gap: 22px; text-align: left; padding: 16px; border-radius: 20px; background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); }
.host .qr { width: 180px; height: 180px; padding: 8px; border-radius: 14px; background: #fff; }
.host .qr svg { width: 100%; height: 100%; display: block; }
.host .code { font-size: 44px; font-weight: 900; letter-spacing: 0.18em; }
.host .h { font-size: 15px; font-weight: 700; margin-top: 4px; }
.host .g { font-size: 13px; color: var(--muted); margin-top: 2px; }
.panel .wide.danger { color: #ff8a8a; border-color: rgba(255, 120, 120, 0.3); }
@media (max-height: 420px) { #gate .logo { display: none; } #gate h1 { margin-top: 0; } }
