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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #1e2235;
  --border: #2e3350;
  --accent: #5b8dee;
  --accent-hover: #4a7cd8;
  --accent-dim: rgba(91, 141, 238, 0.18);
  --accent2: #7c5ce0;
  --blue: #2979ff;
  --red: #ff1744;
  --green: #00e676;
  --yellow: #ffc400;
  --gold: #ffc400;
  --success: #00e676;
  --text: #f0f4ff;
  --text-dim: #7a82a8;
  --error: #e05c6e;
}

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(1rem);
  max-width: min(90vw, 420px);
  padding: 0.75rem 1.1rem;
  border-radius: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.9rem;
  line-height: 1.4;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Typography & labels ─────────────────────────────────────────────────── */

.section-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  margin-bottom: 0.5rem;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */

button, .btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.55rem 1.1rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: opacity 0.15s, transform 0.1s, background 0.15s;
}

button:hover, .btn:hover {
  opacity: 0.88;
}

button:active, .btn:active {
  transform: scale(0.97);
}

button:disabled, .btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
}

.btn-success {
  background: var(--success);
  color: var(--bg);
}

.btn-success:hover {
  opacity: 0.9;
}

.btn-ghost {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.btn-ghost:hover {
  border-color: var(--accent);
  color: var(--text);
  opacity: 1;
}

/* ── Inputs ──────────────────────────────────────────────────────────────── */

input[type="text"] {
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 0.9rem;
  transition: border-color 0.15s;
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

/* ── Badges ──────────────────────────────────────────────────────────────── */

.badge {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
}

.badge-ready {
  background: rgba(0, 230, 118, 0.15);
  color: var(--green);
}

.badge-host {
  background: var(--accent-dim);
  color: var(--accent);
}

.badge-waiting {
  background: rgba(168, 159, 143, 0.12);
  color: var(--text-dim);
}

.badge-phase {
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 0.72rem;
}

/* ── Panels & cards ──────────────────────────────────────────────────────── */

.panel, .lobby-card, .form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
}

/* ── Home page ───────────────────────────────────────────────────────────── */

.home {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2.5rem 1.25rem;
  position: relative;
  overflow: hidden;
}

.home-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 70% 50% at 15% 85%, rgba(41, 121, 255, 0.14) 0%, transparent 60%),
    radial-gradient(ellipse 60% 45% at 85% 15%, rgba(255, 23, 68, 0.1) 0%, transparent 55%),
    radial-gradient(ellipse 50% 40% at 50% 50%, rgba(124, 92, 224, 0.06) 0%, transparent 50%),
    var(--bg);
}

.home > *:not(.home-bg) {
  position: relative;
  z-index: 1;
}

.home .hero {
  text-align: center;
  margin-bottom: 2.75rem;
}

.home .crowd-deco {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: 1.4rem;
  flex-wrap: wrap;
  max-width: 320px;
  margin-left: auto;
  margin-right: auto;
}

.home .crowd-dot {
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  flex-shrink: 0;
  animation: crowd-float 4s ease-in-out infinite;
}

.home .crowd-dot:nth-child(1)  { background: var(--blue);   animation-delay: 0s; }
.home .crowd-dot:nth-child(2)  { background: var(--text-dim); animation-delay: 0.3s; width: 0.75rem; height: 0.75rem; }
.home .crowd-dot:nth-child(3)  { background: var(--red);    animation-delay: 0.6s; }
.home .crowd-dot:nth-child(4)  { background: var(--text-dim); animation-delay: 0.9s; width: 0.85rem; height: 0.85rem; }
.home .crowd-dot:nth-child(5)  { background: var(--yellow); animation-delay: 1.2s; width: 1.35rem; height: 1.35rem; box-shadow: 0 0 12px rgba(255,196,0,0.5); }
.home .crowd-dot:nth-child(6)  { background: var(--text-dim); animation-delay: 1.5s; width: 0.7rem; height: 0.7rem; }
.home .crowd-dot:nth-child(7)  { background: var(--green);  animation-delay: 1.8s; }
.home .crowd-dot:nth-child(8)  { background: var(--text-dim); animation-delay: 2.1s; }
.home .crowd-dot:nth-child(9)  { background: var(--accent2); animation-delay: 2.4s; width: 0.9rem; height: 0.9rem; }

@keyframes crowd-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.home .hero h1 {
  font-size: clamp(2.4rem, 8vw, 4rem);
  font-weight: 900;
  letter-spacing: -0.03em;
  margin: 0;
  color: var(--text);
  line-height: 1.05;
}

.home .role-tags {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 1.1rem;
}

.home .tag {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.22rem 0.55rem;
  border-radius: 20px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.home .tag-blue   { background: rgba(91, 141, 238, 0.18); color: var(--blue); }
.home .tag-red    { background: rgba(255, 23, 68, 0.18);  color: var(--red); }
.home .tag-yellow { background: rgba(255, 196, 0, 0.18);  color: var(--yellow); }

.home .rejoin-panel {
  width: 100%;
  max-width: 640px;
  margin-bottom: 1.2rem;
}

.home .rejoin-card {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 16px;
  padding: 1.4rem 1.6rem;
  box-shadow: 0 0 24px rgba(91, 141, 238, 0.12);
}

.home .rejoin-desc {
  margin: 0 0 1rem;
  color: var(--text-dim);
  font-size: 0.92rem;
  line-height: 1.5;
}

.home .rejoin-desc strong {
  color: var(--text);
  font-family: ui-monospace, monospace;
  letter-spacing: 0.06em;
}

.home .btn-rejoin {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  transition: opacity 0.15s;
}

.home .btn-rejoin:hover {
  opacity: 0.88;
}

.home .cards.home-locked {
  opacity: 0.45;
  pointer-events: none;
  user-select: none;
}

.home .cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.2rem;
  width: 100%;
  max-width: 640px;
}

.home .card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.6rem 1.4rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

.home .card:hover {
  transform: translateY(-3px);
}

.home .card-create {
  border-color: rgba(91, 141, 238, 0.35);
}

.home .card-create:hover {
  border-color: var(--blue);
  box-shadow: 0 12px 40px rgba(91, 141, 238, 0.18);
}

.home .card-join {
  border-color: rgba(255, 196, 0, 0.3);
}

.home .card-join:hover {
  border-color: var(--yellow);
  box-shadow: 0 12px 40px rgba(255, 196, 0, 0.12);
}

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

.home .card-icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.home .card-create .card-icon {
  background: rgba(91, 141, 238, 0.15);
  color: var(--blue);
}

.home .card-join .card-icon {
  background: rgba(255, 196, 0, 0.12);
  color: var(--yellow);
}

.home .card-title {
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.home .card-create .card-title { color: var(--blue); }
.home .card-join .card-title   { color: var(--yellow); }

.home .card-desc {
  font-size: 0.82rem;
  color: var(--text-dim);
  line-height: 1.45;
  margin: 0;
}

.home .btn-create {
  width: 100%;
  padding: 0.8rem 1.5rem;
  font-size: 1rem;
  font-weight: 700;
  background: var(--accent);
  border-radius: 10px;
  letter-spacing: 0.02em;
}

.home .btn-create:hover {
  background: var(--blue);
  opacity: 1;
  box-shadow: 0 4px 20px rgba(91, 141, 238, 0.35);
}

.home .code-input-wrap {
  display: flex;
  gap: 0.5rem;
  width: 100%;
  min-width: 0;
}

.home .code-input {
  flex: 1 1 0;
  min-width: 0;
  width: 100%;
  max-width: 100%;
  font-family: monospace;
  font-size: clamp(0.85rem, 2.8vw, 1.1rem);
  font-weight: 700;
  letter-spacing: 0.12em;
  text-align: center;
  text-transform: lowercase;
  padding: 0.65rem 0.5rem;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--surface2);
  color: var(--yellow);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.home .code-input:focus {
  outline: none;
  border-color: var(--yellow);
  box-shadow: 0 0 0 3px rgba(255, 196, 0, 0.12);
}

.home .code-input::placeholder {
  color: var(--border);
  letter-spacing: 0.12em;
}

.home .btn-enter {
  padding: 0.65rem 1.1rem;
  font-weight: 700;
  background: var(--surface2);
  border: 2px solid var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  flex-shrink: 0;
}

.home .btn-enter:hover {
  border-color: var(--yellow);
  color: var(--yellow);
  opacity: 1;
  background: rgba(255, 196, 0, 0.08);
}

.home #create-result {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin: 0;
  min-height: 1.2em;
}

@media (max-width: 560px) {
  .home .cards {
    grid-template-columns: 1fr;
    max-width: 400px;
  }
}

@media (min-width: 561px) and (max-width: 720px) {
  .home .code-input-wrap {
    flex-direction: column;
  }

  .home .btn-enter {
    width: 100%;
  }
}

/* ── Game room / lobby ───────────────────────────────────────────────────── */

.game-room {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}

body.game-room.in-lobby {
  overflow-x: hidden;
}

#lobby-wrap {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 1.5rem 1rem;
  padding-top: max(2.75rem, calc(env(safe-area-inset-top) + 1.5rem));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  min-height: 100vh;
  min-height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.lobby-card {
  width: 100%;
  max-width: 440px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.room-code {
  display: block;
  width: 100%;
  background: var(--surface2);
  border: 2px solid var(--border);
  border-radius: 10px;
  color: var(--gold);
  font-family: monospace;
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-align: center;
  padding: 0.65rem 1rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.room-code:hover {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.room-code:active {
  transform: scale(0.98);
}

.room-code.copied {
  border-color: var(--success);
  color: var(--success);
}

.room-hint {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
  margin: -0.5rem 0 0;
}

#lobby-panel {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  margin-top: 0.25rem;
}

.lobby-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.map-picker {
  margin-top: 0.75rem;
}

.map-picker select {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

.map-picker select:disabled {
  opacity: 0.6;
  cursor: wait;
}

.map-display {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.9rem;
  color: var(--text);
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
}

.lobby-status {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin: 0.75rem 0 0;
  line-height: 1.4;
}

.lobby-status.ready-hint {
  color: var(--success);
}

#player-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.player-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  background: var(--surface2);
  border: 1px solid transparent;
  transition: border-color 0.15s;
  min-width: 0;
}

.player-row.ready {
  border-color: rgba(0, 230, 118, 0.2);
  background: rgba(0, 230, 118, 0.06);
}

.player-name {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-row .badge {
  flex-shrink: 0;
}

.player-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  flex-shrink: 0;
}

.player-row.ready .player-dot {
  background: var(--success);
}

.lobby-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.25rem;
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  min-width: 0;
}

.lobby-home-link {
  position: fixed;
  top: max(0.75rem, env(safe-area-inset-top));
  right: max(0.75rem, env(safe-area-inset-right));
  z-index: 10;
  font-size: 0.82rem;
  color: var(--text-dim);
  text-decoration: none;
  transition: color 0.15s;
  padding: 0.35rem 0.5rem;
}

.lobby-home-link:hover {
  color: var(--accent);
}

.lobby-actions input {
  flex: 1 1 8rem;
  min-width: 0;
}

#btn-ready.ready-active {
  background: var(--success);
  color: var(--bg);
}

#btn-start {
  background: var(--gold);
  color: var(--bg);
  font-weight: 700;
}

#btn-start:disabled {
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid var(--border);
  opacity: 1;
  cursor: not-allowed;
}

#btn-start:hover:not(:disabled) {
  opacity: 0.9;
}

@media (max-width: 560px) {
  #lobby-wrap {
    padding-top: max(2.75rem, calc(env(safe-area-inset-top) + 1rem));
    align-items: stretch;
  }

  .lobby-card {
    padding: 1.15rem 1rem;
  }

  .room-code {
    font-size: clamp(1.15rem, 6vw, 1.6rem);
    letter-spacing: 0.1em;
    padding: 0.65rem 0.5rem;
    min-height: 44px;
  }

  .lobby-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .lobby-actions input,
  .lobby-actions button {
    width: 100%;
    flex: none;
    min-height: 44px;
  }

  .lobby-actions input {
    font-size: 16px;
  }

  .player-row {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .player-name {
    flex: 1 1 100%;
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
  }
}

/* ── HUD ─────────────────────────────────────────────────────────────────── */

body.in-lobby #hud {
  display: none;
}

#hud {
  display: flex;
  gap: 1rem;
  padding: 0.55rem 1.2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
  align-items: center;
  flex-wrap: nowrap;
  min-height: 48px;
  box-sizing: border-box;
}

#role-display {
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  background: var(--accent-dim);
  color: var(--accent);
  padding: 0.2rem 0.55rem;
  border-radius: 4px;
}

.role-sniper #role-display {
  background: rgba(255, 196, 0, 0.15);
  color: var(--yellow);
}

#timer-display {
  font-family: monospace;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: var(--surface2);
  border: 1px solid var(--border);
}

#channel-display {
  display: none;
}

.role-assassin #channel-display {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--gold);
  width: 130px;
  flex-shrink: 0;
  visibility: hidden;
}

.role-assassin #channel-display.active {
  visibility: visible;
}

#channel-label {
  font-size: 0.75rem;
  white-space: nowrap;
}

#channel-bar-wrap {
  width: 80px;
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
}

#channel-bar {
  height: 100%;
  background: var(--gold);
  width: 0%;
  transition: width 0.07s linear;
}

#world {
  display: block;
  width: 100%;
  cursor: default;
}

#world.sniper-view {
  cursor: none !important;
}

.role-assassin #hud::after {
  content: 'WASD move · E disguise · Q assassinate';
  margin-left: auto;
  color: var(--text-dim);
  font-size: 0.78rem;
  white-space: nowrap;
  flex-shrink: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.role-sniper #hud::after {
  content: 'Click shoot · Scroll zoom · Shift+drag pan';
  margin-left: auto;
  color: var(--text-dim);
  font-size: 0.78rem;
  white-space: nowrap;
}

/* ── Overlay ─────────────────────────────────────────────────────────────── */

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 2rem;
}

#overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2.5rem 2rem;
  text-align: center;
  max-width: 400px;
  width: 100%;
}

#overlay h2, #overlay-title {
  font-size: clamp(1.8rem, 5vw, 2.5rem);
  font-weight: 800;
  margin: 0 0 0.75rem;
  color: var(--text);
}

#overlay-detail {
  font-size: 0.95rem;
  color: var(--text-dim);
  margin: 0 0 1.5rem;
  line-height: 1.5;
}

.overlay-home-link {
  display: inline-block;
  padding: 0.6rem 1.4rem;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  transition: opacity 0.15s, transform 0.1s;
}

.overlay-home-link:hover {
  opacity: 0.88;
}

.overlay-home-link:active {
  transform: scale(0.97);
}
