:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --danger: #ef4444;
  --gold: #facc15;
  --target-size: 24px;
}

@media (pointer: coarse) {
  :root {
    --target-size: 44px;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-padding-bottom: calc(3.5rem + env(safe-area-inset-bottom, 0px));
}

body {
  margin: 0;
  min-block-size: 100dvb;
  background: radial-gradient(circle at top, #172554 0%, var(--bg) 65%);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  line-height: 1.4;
  overflow-wrap: break-word;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 {
  text-wrap: balance;
}

p, li {
  text-wrap: pretty;
}

[hidden] {
  display: none !important;
}

:where(button, select, input:not([type="checkbox"], [type="radio"]), label:has(input[type="checkbox"])) {
  min-block-size: var(--target-size);
  min-inline-size: var(--target-size);
}

/* Screens */
.screen-center {
  display: grid;
  place-items: safe center;
  min-block-size: 100dvb;
  padding-block-start: max(1rem, env(safe-area-inset-top, 0px));
  padding-block-end: max(1rem, env(safe-area-inset-bottom, 0px));
  padding-inline: max(1rem, env(safe-area-inset-left, 0px)) max(1rem, env(safe-area-inset-right, 0px));
}

.panel {
  inline-size: min(100%, 540px);
  display: grid;
  gap: 1rem;
  padding: 1.25rem;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}

.hero-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-badge {
  display: grid;
  place-items: center;
  inline-size: 2.75rem;
  block-size: 2.75rem;
  font-size: 1.5rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  flex-shrink: 0;
}

.hero-title {
  margin: 0;
  font-size: clamp(1.15rem, 4vw, 1.45rem);
  font-weight: 900;
  letter-spacing: 0.05em;
}

.hero-sub {
  margin: 0.15rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.status-pill {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  background: #0f172a;
  border: 1px solid var(--border);
  color: var(--gold);
  justify-self: start;
}

.status-pill.ok {
  color: #4ade80;
  border-color: #166534;
}

.status-pill.err {
  color: #f87171;
  border-color: #7f1d1d;
}

.field {
  display: grid;
  gap: 0.3rem;
  font-size: 0.82rem;
  font-weight: 700;
  color: #cbd5e1;
}

input[type="text"],
select {
  inline-size: 100%;
  background: #0f172a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  font: inherit;
  font-size: 0.95rem;
}

#input-code {
  text-transform: uppercase;
  letter-spacing: 0.25em;
  font-weight: 900;
  text-align: center;
}

.home-actions {
  display: grid;
  gap: 0.75rem;
}

.join-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
}

.divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--muted);
  font-size: 0.78rem;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  block-size: 1px;
  background: var(--border);
}

.error-msg {
  margin: 0;
  color: #fca5a5;
  background: rgba(127, 29, 29, 0.35);
  border: 1px solid #991b1b;
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
}

.mobile-tip {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
}

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  padding-inline: 0.85rem;
  padding-block: 0.45rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 120ms ease, transform 80ms ease;
}

.btn:hover:not(:disabled) {
  background: #273549;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: #0284c7;
  border-color: #38bdf8;
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: #0369a1;
}

.btn-lg {
  padding-block: 0.65rem;
  padding-inline: 1.1rem;
  font-size: 0.95rem;
}

/* Lobby */
.lobby-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.eyebrow {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--accent);
}

.room-code-big {
  margin: 0;
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: 0.2em;
  color: var(--gold);
}

.lobby-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.copy-feedback {
  margin: -0.5rem 0 0;
  min-block-size: 1.1em;
  font-size: 0.8rem;
  color: #4ade80;
}

.section-title {
  margin: 0 0 0.5rem;
  font-size: 0.88rem;
  font-weight: 800;
  color: var(--accent);
}

.muted-note {
  font-weight: 500;
  font-size: 0.8rem;
  color: var(--muted);
}

.lobby-slots {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.55rem;
}

.lobby-slot-card {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.7rem;
  background: #0f172a;
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--slot-color);
  border-radius: 10px;
}

.lobby-slot-card.empty {
  opacity: 0.6;
}

.slot-avatar {
  display: grid;
  place-items: center;
  inline-size: 2rem;
  block-size: 2rem;
  border-radius: 50%;
  background: var(--slot-color);
  color: #0f172a;
  font-weight: 900;
  font-size: 0.78rem;
  flex-shrink: 0;
}

.slot-info {
  min-inline-size: 0;
}

.slot-name {
  font-weight: 800;
  font-size: 0.88rem;
}

.slot-tag {
  font-size: 0.72rem;
  color: var(--muted);
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
  gap: 0.6rem;
}

.checkbox-field {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  margin-block-start: 0.65rem;
  font-size: 0.86rem;
  cursor: pointer;
}

.lobby-footer {
  display: grid;
  gap: 0.4rem;
}

/* Game Shell (Desktop + Mobile) */
.game-shell {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 0.4rem;
  block-size: 100dvb;
  max-block-size: 100dvb;
  max-inline-size: 980px;
  margin-inline: auto;
  padding-block-start: max(0.35rem, env(safe-area-inset-top, 0px));
  padding-block-end: max(0.5rem, env(safe-area-inset-bottom, 0px));
  padding-inline-start: max(0.45rem, env(safe-area-inset-left, 0px));
  padding-inline-end: max(0.45rem, env(safe-area-inset-right, 0px));
  overflow: clip;
}

.game-topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: safe center;
  justify-content: space-between;
  gap: 0.45rem;
  padding: 0.45rem 0.65rem;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.room-pill {
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.1em;
  padding: 0.25rem 0.55rem;
  border-radius: 6px;
  background: #1e293b;
  color: var(--accent);
}

.match-clock {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.2rem 0.65rem;
  background: #0f172a;
  border: 1px solid var(--border);
  border-radius: 999px;
}

.clock-round {
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--accent);
}

.clock-time {
  font-variant-numeric: tabular-nums;
  font-size: 1rem;
  font-weight: 900;
  color: var(--gold);
}

.clock-time.hurry {
  color: var(--danger);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: safe center;
  gap: 0.35rem;
}

/* Compact 4-Player HUD Strip */
.hud-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.4rem;
}

@media (max-width: 560px) {
  .hud-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.hud-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--player-color, var(--accent));
  border-radius: 8px;
}

.hud-card.you {
  box-shadow: 0 0 0 1px var(--accent);
}

.hud-card.ko {
  opacity: 0.48;
}

.hud-card.off {
  opacity: 0.3;
}

.hud-avatar {
  display: grid;
  place-items: center;
  inline-size: 1.65rem;
  block-size: 1.65rem;
  border-radius: 50%;
  background: var(--player-color);
  color: #0f172a;
  font-weight: 900;
  font-size: 0.7rem;
}

.hud-meta {
  min-inline-size: 0;
}

.hud-name {
  display: block;
  font-weight: 800;
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: clip;
  text-overflow: ellipsis;
}

.hud-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.2rem;
  font-size: 0.68rem;
  color: #cbd5e1;
}

.hud-wins {
  font-size: 0.78rem;
  font-weight: 900;
  color: var(--gold);
  white-space: nowrap;
}

/* Play Stage + Arena */
.play-stage {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  place-items: center;
  gap: 0.45rem;
  min-block-size: 0;
  block-size: 100%;
  inline-size: 100%;
}

.arena-frame {
  display: grid;
  place-items: center;
  max-inline-size: min(100%, 740px);
  max-block-size: 100%;
  min-block-size: 0;
  aspect-ratio: 15 / 13;
  padding: 0.3rem;
  background: #0f172a;
  border: 2px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.5);
  overflow: clip;
}

#game-canvas {
  display: block;
  inline-size: 100%;
  block-size: 100%;
  aspect-ratio: 15 / 13;
  object-fit: contain;
  border-radius: 6px;
  background: #1e293b;
  touch-action: none;
}

/* Virtual Mobile Touch Controls */
.touch-controls {
  inline-size: min(100%, 740px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-inline: 0.85rem;
  padding-block: 0.25rem max(0.35rem, env(safe-area-inset-bottom, 0px));
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.stick-base {
  position: relative;
  inline-size: 126px;
  block-size: 126px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(30, 41, 59, 0.9) 0%, rgba(15, 23, 42, 0.92) 100%);
  border: 2px solid #475569;
  box-shadow: inset 0 4px 14px rgba(0, 0, 0, 0.5);
  touch-action: none;
}

.stick-arrows {
  position: absolute;
  inset: 0;
  pointer-events: none;
  color: rgba(148, 163, 184, 0.45);
  font-size: 0.85rem;
}

.stick-arrows .arr {
  position: absolute;
  transition: color 60ms ease, transform 60ms ease;
}
.stick-arrows .arr.active {
  color: #38bdf8;
  text-shadow: 0 0 8px rgba(56, 189, 248, 0.8);
}
.stick-arrows .up { top: 8px; left: 50%; transform: translateX(-50%); }
.stick-arrows .down { bottom: 8px; left: 50%; transform: translateX(-50%); }
.stick-arrows .left { left: 10px; top: 50%; transform: translateY(-50%); }
.stick-arrows .right { right: 10px; top: 50%; transform: translateY(-50%); }

.stick-knob {
  position: absolute;
  top: 50%;
  left: 50%;
  inline-size: 54px;
  block-size: 54px;
  margin-top: -27px;
  margin-left: -27px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #38bdf8, #0284c7);
  border: 2px solid #bae6fd;
  box-shadow: 0 6px 16px rgba(2, 132, 199, 0.45);
  pointer-events: none;
  transition: transform 40ms linear;
}

.bomb-btn {
  display: grid;
  place-items: center;
  align-content: center;
  gap: 0.1rem;
  inline-size: 92px;
  block-size: 92px;
  border-radius: 50%;
  border: 3px solid #fca5a5;
  background: radial-gradient(circle at 35% 30%, #ef4444, #991b1b);
  color: #ffffff;
  font-size: 1.7rem;
  font-weight: 900;
  box-shadow: 0 10px 24px rgba(239, 68, 68, 0.45);
  cursor: pointer;
  touch-action: none;
}

.bomb-btn:active,
.bomb-btn.pressed {
  transform: scale(0.93);
  background: radial-gradient(circle at 35% 30%, #f87171, #b91c1c);
}

.bomb-btn-label {
  font-size: 0.68rem;
  letter-spacing: 0.08em;
}

/* Compact topbar & controls on small portrait phones */
@media (max-width: 560px) {
  .game-topbar {
    padding: 0.3rem 0.45rem;
    gap: 0.3rem;
  }
  .game-topbar .btn {
    padding-inline: 0.55rem;
    padding-block: 0.3rem;
    font-size: 0.78rem;
  }
  .hud-card {
    padding: 0.25rem 0.4rem;
  }
  .bomb-btn {
    inline-size: 84px;
    block-size: 84px;
    font-size: 1.5rem;
  }
}

/* Landscape phone optimization: dock stick on left, bomb on right, arena in middle */
@media (orientation: landscape) and (max-height: 540px) {
  .hud-strip {
    display: none;
  }
  .desktop-hints {
    display: none;
  }
  .play-stage {
    grid-template-rows: minmax(0, 1fr);
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
  }
  .arena-frame {
    grid-column: 2;
    block-size: 100%;
    inline-size: auto;
  }
  .touch-controls {
    display: contents;
  }
  .dpad-zone {
    grid-column: 1;
    grid-row: 1;
  }
  .action-zone {
    grid-column: 3;
    grid-row: 1;
  }
}

.desktop-hints {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem 1rem;
  font-size: 0.76rem;
  color: var(--muted);
}

@media (pointer: coarse) {
  .desktop-hints {
    display: none;
  }
}

/* Native Modal Dialog */
dialog {
  padding: 1.25rem;
  inline-size: min(92dvw, 480px);
  max-block-size: 86dvb;
  overflow-y: auto;
  background: var(--surface);
  color: var(--text);
  border: 1px solid #475569;
  border-radius: 14px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
}

dialog[open] {
  display: grid;
  gap: 0.9rem;
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(4px);
  overflow: hidden;
  overscroll-behavior: contain;
}

dialog h1 {
  margin: 0;
  font-size: 1.2rem;
}

dialog h2 {
  margin: 0 0 0.4rem;
  font-size: 0.92rem;
  color: var(--accent);
}

.guide-grid {
  display: grid;
  gap: 0.85rem;
}

.key-list {
  margin: 0;
  padding-inline-start: 1.1rem;
  display: grid;
  gap: 0.35rem;
  font-size: 0.86rem;
  color: #cbd5e1;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}

.pu-bomb { color: #38bdf8; }
.pu-fire { color: #fb923c; }
.pu-speed { color: #4ade80; }
.pu-kick { color: #facc15; }
.pu-pierce { color: #e879f9; }
