:root {
  --bg: #0a0a0a;
  --bg-elevated: #161616;
  --bg-elevated-2: #202020;
  --border: #2e2e2e;
  --text: #f2f2f2;
  --text-dim: #9a9a9a;
  --accent: #f97316;
  --accent-dim: #7c3a10;
  --white-card-bg: #ffffff;
  --white-card-text: #0a0a0a;
  --black-card-bg: #0a0a0a;
  --black-card-text: #ffffff;
  --danger: #ef4444;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  height: 100%;
  overscroll-behavior: none;
}

button {
  font-family: inherit;
  cursor: pointer;
}

input, select {
  font-family: inherit;
  font-size: 1rem;
}

h1, h2, h3 { margin: 0 0 0.5em; }

.view {
  display: none;
  min-height: 100vh;
  flex-direction: column;
}
.view.active { display: flex; }

.error-msg {
  color: var(--danger);
  min-height: 1.2em;
  font-size: 0.9rem;
}

.lobby-hint {
  color: var(--text-dim);
  font-size: 0.9rem;
}

.btn-primary {
  background: var(--accent);
  color: #1a0d02;
  border: none;
  border-radius: 10px;
  padding: 0.9rem 1.2rem;
  font-weight: 700;
  font-size: 1rem;
  transition: transform 0.1s ease, opacity 0.15s ease;
}
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem 1.2rem;
  font-weight: 600;
  font-size: 1rem;
}
.btn-secondary:disabled { opacity: 0.35; cursor: not-allowed; }

.icon-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
}

/* ---------------- Landing ---------------- */

#view-landing {
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.landing-card {
  width: 100%;
  max-width: 420px;
}

.rejoin-banner {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elevated);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1.25rem;
}
.rejoin-banner p {
  margin: 0 0 0.75rem;
  font-weight: 600;
}
.rejoin-actions {
  display: flex;
  gap: 0.75rem;
}
.rejoin-actions button { flex: 1; }

.landing-card h1 {
  color: var(--accent);
  font-size: 2rem;
}

.tagline {
  color: var(--text-dim);
  margin-bottom: 1.5rem;
}

.landing-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}

.tab-btn {
  flex: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 0.7rem;
  border-radius: 10px;
  font-weight: 600;
}
.tab-btn.active {
  color: var(--bg);
  background: var(--accent);
  border-color: var(--accent);
}

.landing-form {
  display: none;
  flex-direction: column;
  gap: 0.4rem;
}
.landing-form.active { display: flex; }

.landing-form label {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-top: 0.6rem;
}

.landing-form input, .landing-form select {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 0.8rem;
}

.landing-form .btn-primary { margin-top: 1.2rem; }

.pack-picker {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

#pack-search {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  font-size: 0.9rem;
}

.pack-presets {
  display: flex;
  gap: 0.4rem;
}

.btn-preset {
  flex: 1;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0.45rem 0.5rem;
  font-size: 0.78rem;
  font-weight: 600;
}
.btn-preset:active { transform: scale(0.97); }

.pack-list {
  max-height: 220px;
  overflow-y: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.5rem;
  font-size: 0.85rem;
}

.pack-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.25rem;
}
.pack-item label { margin: 0; color: var(--text); font-size: 0.85rem; }

.official-tag {
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}

#pack-selected-count { margin: 0; }

/* ---------------- Top bar (lobby + game) ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.9rem;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.room-code-badge {
  font-weight: 700;
  letter-spacing: 0.05em;
}
.room-code-badge span { color: var(--accent); }

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

.round-info {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 600;
}

.phase-pill {
  background: var(--accent-dim);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
}

/* Persistent "[nickname] is the judge" line — always visible during a round. */
.judge-indicator {
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.disconnected-banner {
  padding: 0.6rem 0.9rem;
  background: #3a1414;
  color: #ef8b8b;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
}

/* ---------------- Lobby ---------------- */

.lobby-body {
  padding: 1.25rem;
  flex: 1;
}

.lobby-player-list {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-x: hidden;
}

.offline-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #ef8b8b;
  margin-right: 0.15rem;
  flex-shrink: 0;
}

/* Shared player-card layout — used in the lobby list, the drawer, the host
   menu, and the game-end scoreboard. Fixed two-line structure (name+badges,
   then score) so an arbitrary pile of badges next to a long nickname wraps
   onto its own line instead of ever forcing horizontal overflow. */
.player-card {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.85rem;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  transition: opacity 0.15s ease;
}
.player-card.offline {
  opacity: 0.55;
}

.player-row-top {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.35rem;
  min-width: 0;
}

.player-name {
  font-weight: 600;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.player-row-bottom {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.player-badges {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.35rem;
  min-width: 0;
}
.badge {
  font-size: 0.7rem;
  border-radius: 999px;
  padding: 0.15rem 0.5rem;
  font-weight: 700;
  text-transform: uppercase;
  white-space: nowrap;
}
.badge-host { background: var(--accent-dim); color: var(--accent); }
.badge-czar { background: #3a2a6d; color: #b39dff; }
.badge-offline { background: #3a1414; color: #ef8b8b; }
.badge-submitted { background: #14351f; color: #7be08c; }

.kick-btn {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 8px;
  padding: 0.2rem 0.5rem;
  font-size: 0.75rem;
}

/* ---------------- Cards ---------------- */

.card {
  border-radius: 14px;
  padding: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-weight: 500;
  line-height: 1.3;
  word-break: break-word;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.white-card {
  background: var(--white-card-bg);
  color: var(--white-card-text);
}

.black-card {
  background: var(--black-card-bg);
  color: var(--black-card-text);
  border: 1px solid var(--border);
  font-weight: 600;
}

.black-card-zone {
  position: sticky;
  top: 49px; /* below topbar */
  z-index: 15;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
}

.black-card-zone .black-card {
  min-height: 11vh;
  padding: 0.7rem 0.85rem;
  font-size: 1.05rem;
}

/* ---------------- Hand grid ---------------- */

.hand-zone {
  flex: 1;
  padding: 0.5rem 0.9rem 6rem;
  overflow-y: auto;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem 0.5rem;
  transition: opacity 0.2s ease, filter 0.2s ease;
}

/* Submitted and waiting — nothing left to do this round. */
.card-grid.dimmed {
  opacity: 0.45;
  filter: saturate(0.3);
  pointer-events: none;
}

.card-grid .card {
  min-height: 78px;
  padding: 12px 14px;
  font-size: 13.5px;
  cursor: pointer;
  position: relative;
  border: 2px solid transparent;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

/* Long card text clamps instead of stretching the grid; full text is
   always available via the tap-to-enlarge modal. */
.card-grid .card .card-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-grid .card.selected {
  border-color: var(--accent);
  transform: translateY(-4px);
}

.pick-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--accent);
  color: #1a0d02;
  font-weight: 800;
  font-size: 0.75rem;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

/* ---------------- Judging: face-down submissions + flip ---------------- */

.card-grid .submission-slot {
  position: relative;
  min-height: 78px;
  border-radius: 14px;
}

/* Highlight the current player's own submission — visible face-down or
   face-up, per spec, so it sits on the outer slot rather than either face. */
.card-grid .submission-slot.mine {
  border-radius: 16px;
  box-shadow: 0 0 0 2px var(--accent), 0 0 14px 2px rgba(249, 115, 22, 0.45);
}

.submission-slot.not-clickable {
  cursor: default;
}

.submission-flip {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: inherit;
  transition: transform 0.4s ease;
  transform-style: preserve-3d;
}
.submission-flip.flipped {
  transform: rotateY(180deg);
}

.submission-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.submission-face-front {
  transform: rotateY(180deg);
}
.submission-face-back {
  font-size: 1.6rem;
  color: var(--text-dim);
}

/* ---------------- Bottom bar ---------------- */

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  padding: 0.75rem 0.9rem calc(0.75rem + env(safe-area-inset-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
}

.bottom-bar .btn-primary { width: 100%; }

/* ---------------- Round end / Game end ---------------- */

.roundend-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem 1.25rem;
  text-align: center;
}

.winner-name {
  color: var(--accent);
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 1rem;
}

.winner-cards {
  max-width: 420px;
  margin-bottom: 1.5rem;
}

#gameend-scores { max-width: 380px; width: 100%; }

/* ---------------- Player drawer ---------------- */

.drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 29;
}
.drawer-overlay.active { display: block; }

.player-drawer {
  position: fixed;
  top: 0;
  right: -320px;
  width: 300px;
  max-width: 85vw;
  height: 100%;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  z-index: 30;
  transition: right 0.2s ease;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}
.player-drawer.open { right: 0; }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.drawer-player-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 0;
}

/* ---------------- Card modal (mobile tap-to-enlarge) ---------------- */

.card-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 40;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.card-modal-overlay.active { display: flex; }

/* The panel itself is "the modal" — card + action buttons live inside
   one bounded, shadowed container so buttons never leak over the
   darkened backdrop/grid behind it. */
.card-modal-content {
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  overflow: hidden;
}

.card-modal-content .card {
  max-height: 45vh;
  min-height: 30vh;
  overflow-y: auto;
  font-size: 1.4rem;
  font-weight: 600;
  flex-shrink: 1;
}

.card-modal-actions {
  display: flex;
  gap: 0.75rem;
  flex-shrink: 0;
}
.card-modal-actions button { flex: 1; }

/* ---------------- Host controls menu ---------------- */

.host-menu-content h3 { margin-bottom: 0.25rem; }
.host-menu-section {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
}
.host-menu-section h4 {
  margin: 0 0 0.5rem;
  color: var(--text-dim);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.host-menu-actions {
  display: flex;
  gap: 0.75rem;
  flex-shrink: 0;
}
.host-menu-actions button { flex: 1; }

.judge-modal-cards {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 50vh;
  overflow-y: auto;
  flex-shrink: 1;
}
.judge-modal-cards .card {
  min-height: 70px;
  max-height: 140px;
  font-size: 1.1rem;
}
/* Same clamp/ellipsis treatment as the hand grid — a long Pick 2/3
   submission shouldn't stretch the whole judging list. */
.judge-modal-cards .card .card-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  line-clamp: 4;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------------- Toast ---------------- */

.toast {
  position: fixed;
  /* Fallback for when JS hasn't measured the bottom bar yet; showToast()
     sets this inline to clear the fixed submit bar whenever it's visible. */
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--danger);
  color: #fff;
  padding: 0.7rem 1.2rem;
  border-radius: 10px;
  font-size: 0.9rem;
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease, bottom 0.15s ease;
  max-width: 90vw;
  text-align: center;
}
.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* underscores in black card blanks are already literal "___" text from data */

/* =========================================================
   Desktop layout (>=768px)
   ========================================================= */

@media (min-width: 768px) {
  /* Player list becomes a fixed right sidebar on desktop instead of a
     slide-in drawer; content views get right padding so nothing sits
     underneath it. */
  #lobby-drawer-btn, #game-drawer-btn { display: none; }

  .drawer-overlay { display: none !important; }
  .player-drawer {
    position: fixed;
    top: 0;
    right: 0;
    left: auto;
    height: 100vh;
    width: 260px;
    border-left: 1px solid var(--border);
    z-index: 25;
    display: none;
  }
  body.in-room .player-drawer { display: flex; }
  body.in-room #view-lobby,
  body.in-room #view-game,
  body.in-room #view-roundend,
  body.in-room #view-gameend {
    padding-right: 260px;
  }

  #view-game {
    display: none;
    flex-direction: row;
    flex-wrap: wrap;
  }
  #view-game.active { display: flex; }

  /* Row-flex children default to shrinking to content width; force the
     topbar onto its own full-width line before the black-card/hand row. */
  #view-game .topbar,
  #view-game .judge-indicator,
  #view-game .disconnected-banner {
    flex: 0 0 100%;
  }

  .black-card-zone {
    width: 340px;
    top: 65px;
    position: sticky;
    align-self: flex-start;
    padding: 0.9rem 1rem;
  }
  .black-card-zone .black-card {
    min-height: 180px;
    padding: 1.1rem 1.25rem;
    font-size: 1.3rem;
  }

  .hand-zone {
    flex: 1;
    padding: 1rem 1.5rem 2rem;
    min-width: 0;
  }

  .card-grid {
    grid-template-columns: repeat(5, 1fr);
    max-width: 900px;
    gap: 0.75rem;
  }

  .card-grid .card {
    min-height: 120px;
    padding: 14px 16px;
    font-size: 0.9rem;
  }

  .card-grid .submission-slot {
    min-height: 120px;
  }

  .card-grid .card .card-text {
    -webkit-line-clamp: 4;
    line-clamp: 4;
  }

  .card-grid .card:hover {
    transform: scale(1.05) translateY(-4px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.5);
    border-color: var(--accent);
  }

  #view-game .bottom-bar {
    flex: 0 0 100%;
    position: sticky;
    left: auto;
    right: auto;
    width: 100%;
  }
  .bottom-bar .btn-primary { max-width: 320px; margin: 0 auto; display: block; }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .card-grid { grid-template-columns: repeat(4, 1fr); }
}
